numeric_literal_test.cpp 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373
  1. // Part of the Carbon Language project, under the Apache License v2.0 with LLVM
  2. // Exceptions. See /LICENSE for license information.
  3. // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
  4. #include "toolchain/lex/numeric_literal.h"
  5. #include <gmock/gmock.h>
  6. #include <gtest/gtest.h>
  7. #include "common/check.h"
  8. #include "toolchain/diagnostics/diagnostic_emitter.h"
  9. #include "toolchain/lex/test_helpers.h"
  10. namespace Carbon::Lex {
  11. namespace {
  12. using ::testing::_;
  13. using ::testing::Field;
  14. using ::testing::Matcher;
  15. using ::testing::Property;
  16. using ::testing::Truly;
  17. using ::testing::VariantWith;
  18. class NumericLiteralTest : public ::testing::Test {
  19. public:
  20. NumericLiteralTest() : error_tracker(ConsoleDiagnosticConsumer()) {}
  21. auto Lex(llvm::StringRef text, bool can_form_real_literal) -> NumericLiteral {
  22. std::optional<NumericLiteral> result =
  23. NumericLiteral::Lex(text, can_form_real_literal);
  24. CARBON_CHECK(result);
  25. if (can_form_real_literal) {
  26. EXPECT_EQ(result->text(), text);
  27. }
  28. return *result;
  29. }
  30. auto Parse(llvm::StringRef text, bool can_form_real_literal = true)
  31. -> NumericLiteral::Value {
  32. Testing::SingleTokenDiagnosticConverter converter(text);
  33. DiagnosticEmitter<const char*> emitter(converter, error_tracker);
  34. return Lex(text, can_form_real_literal).ComputeValue(emitter);
  35. }
  36. ErrorTrackingDiagnosticConsumer error_tracker;
  37. };
  38. // Matcher for signed llvm::APInt.
  39. auto IsSignedInt(int64_t value) -> Matcher<llvm::APInt> {
  40. return Property(&llvm::APInt::getSExtValue, value);
  41. }
  42. // Matcher for unsigned llvm::APInt.
  43. auto IsUnsignedInt(uint64_t value) -> Matcher<llvm::APInt> {
  44. return Property(&llvm::APInt::getZExtValue, value);
  45. }
  46. // Matcher for an integer literal value.
  47. template <typename ValueMatcher>
  48. auto HasIntValue(const ValueMatcher& value_matcher)
  49. -> Matcher<NumericLiteral::Value> {
  50. return VariantWith<NumericLiteral::IntValue>(
  51. Field(&NumericLiteral::IntValue::value, value_matcher));
  52. }
  53. struct RealMatcher {
  54. Matcher<int> radix = _;
  55. Matcher<llvm::APInt> mantissa = _;
  56. Matcher<llvm::APInt> exponent = _;
  57. };
  58. // Matcher for a real literal value.
  59. auto HasRealValue(const RealMatcher& real_matcher)
  60. -> Matcher<NumericLiteral::Value> {
  61. return VariantWith<NumericLiteral::RealValue>(AllOf(
  62. Field(&NumericLiteral::RealValue::radix, real_matcher.radix),
  63. Field(&NumericLiteral::RealValue::mantissa, real_matcher.mantissa),
  64. Field(&NumericLiteral::RealValue::exponent, real_matcher.exponent)));
  65. }
  66. // Matcher for an unrecoverable parse error.
  67. auto HasUnrecoverableError() -> Matcher<NumericLiteral::Value> {
  68. return VariantWith<NumericLiteral::UnrecoverableError>(_);
  69. }
  70. TEST_F(NumericLiteralTest, HandlesIntLiteral) {
  71. struct Testcase {
  72. llvm::StringLiteral token;
  73. uint64_t value;
  74. int radix;
  75. };
  76. Testcase testcases[] = {
  77. {.token = "12", .value = 12, .radix = 10},
  78. {.token = "0x12_3ABC", .value = 0x12'3ABC, .radix = 16},
  79. {.token = "0b10_10_11", .value = 0b10'10'11, .radix = 2},
  80. {.token = "1_234_567", .value = 1'234'567, .radix = 10},
  81. };
  82. for (bool can_form_real_literal : {false, true}) {
  83. for (Testcase testcase : testcases) {
  84. error_tracker.Reset();
  85. EXPECT_THAT(Parse(testcase.token, can_form_real_literal),
  86. HasIntValue(IsUnsignedInt(testcase.value)))
  87. << testcase.token;
  88. EXPECT_FALSE(error_tracker.seen_error()) << testcase.token;
  89. }
  90. }
  91. }
  92. TEST_F(NumericLiteralTest, ValidatesBaseSpecifier) {
  93. llvm::StringLiteral valid[] = {
  94. // Decimal integer literals.
  95. "0",
  96. "1",
  97. "123456789000000000000000000000000000000000000",
  98. // Hexadecimal integer literals.
  99. "0x0123456789ABCDEF",
  100. "0x0000000000000000000000000000000",
  101. // Binary integer literals.
  102. "0b10110100101001010",
  103. "0b0000000",
  104. };
  105. for (llvm::StringLiteral literal : valid) {
  106. error_tracker.Reset();
  107. EXPECT_THAT(Parse(literal), HasIntValue(_)) << literal;
  108. EXPECT_FALSE(error_tracker.seen_error()) << literal;
  109. }
  110. llvm::StringLiteral invalid[] = {
  111. "00", "0X123", "0o123", "0B1",
  112. "007", "123L", "123456789A", "0x",
  113. "0b", "0x123abc", "0b011101201001", "0b10A",
  114. "0x_", "0b_",
  115. };
  116. for (llvm::StringLiteral literal : invalid) {
  117. error_tracker.Reset();
  118. EXPECT_THAT(Parse(literal), HasUnrecoverableError()) << literal;
  119. EXPECT_TRUE(error_tracker.seen_error()) << literal;
  120. }
  121. }
  122. TEST_F(NumericLiteralTest, ValidatesIntDigitSeparators) {
  123. llvm::StringLiteral valid[] = {
  124. // Decimal literals.
  125. "1_234",
  126. "123_456",
  127. "1_234_567",
  128. "12_34",
  129. "123_4_6_789",
  130. "12_3456_789",
  131. // Hexadecimal literals.
  132. "0x1_0000",
  133. "0x1000_0000",
  134. "0x1_0000_0000",
  135. "0x12_3",
  136. "0x1234_567",
  137. // Binary literals.
  138. "0b1_0_1_0_1_0",
  139. "0b111_0000",
  140. };
  141. for (llvm::StringLiteral literal : valid) {
  142. error_tracker.Reset();
  143. EXPECT_THAT(Parse(literal), HasIntValue(_)) << literal;
  144. EXPECT_FALSE(error_tracker.seen_error()) << literal;
  145. }
  146. llvm::StringLiteral invalid[] = {
  147. // Decimal literals.
  148. "12__345",
  149. "1_",
  150. // Hexadecimal literals.
  151. "0x_1234",
  152. "0x123_",
  153. "0x_234_5678",
  154. // Binary literals.
  155. "0b_10101",
  156. "0b1__01",
  157. "0b1011_",
  158. "0b1_01_01_",
  159. };
  160. for (llvm::StringLiteral literal : invalid) {
  161. error_tracker.Reset();
  162. EXPECT_THAT(Parse(literal), HasIntValue(_)) << literal;
  163. EXPECT_TRUE(error_tracker.seen_error()) << literal;
  164. }
  165. }
  166. TEST_F(NumericLiteralTest, HandlesRealLiteral) {
  167. struct Testcase {
  168. llvm::StringLiteral token;
  169. uint64_t mantissa;
  170. int64_t exponent;
  171. unsigned radix;
  172. uint64_t int_value;
  173. };
  174. Testcase testcases[] = {
  175. // Decimal real literals.
  176. {.token = "0.0",
  177. .mantissa = 0,
  178. .exponent = -1,
  179. .radix = 10,
  180. .int_value = 0},
  181. {.token = "12.345",
  182. .mantissa = 12345,
  183. .exponent = -3,
  184. .radix = 10,
  185. .int_value = 12},
  186. {.token = "12.345e6",
  187. .mantissa = 12345,
  188. .exponent = 3,
  189. .radix = 10,
  190. .int_value = 12},
  191. {.token = "12.345e+6",
  192. .mantissa = 12345,
  193. .exponent = 3,
  194. .radix = 10,
  195. .int_value = 12},
  196. {.token = "1_234.5e-2",
  197. .mantissa = 12345,
  198. .exponent = -3,
  199. .radix = 10,
  200. .int_value = 1234},
  201. {.token = "1.0e-2_000_000",
  202. .mantissa = 10,
  203. .exponent = -2'000'001,
  204. .radix = 10,
  205. .int_value = 1},
  206. // Hexadecimal real literals.
  207. {.token = "0x1_2345_6789.CDEF",
  208. .mantissa = 0x1'2345'6789'CDEF,
  209. .exponent = -16,
  210. .radix = 16,
  211. .int_value = 0x1'2345'6789},
  212. {.token = "0x0.0001p4",
  213. .mantissa = 1,
  214. .exponent = -12,
  215. .radix = 16,
  216. .int_value = 0},
  217. {.token = "0x0.0001p+4",
  218. .mantissa = 1,
  219. .exponent = -12,
  220. .radix = 16,
  221. .int_value = 0},
  222. {.token = "0x0.0001p-4",
  223. .mantissa = 1,
  224. .exponent = -20,
  225. .radix = 16,
  226. .int_value = 0},
  227. // The exponent here works out as exactly INT64_MIN.
  228. {.token = "0x1.01p-9223372036854775800",
  229. .mantissa = 0x101,
  230. .exponent = -9223372036854775807L - 1L,
  231. .radix = 16,
  232. .int_value = 1},
  233. // The exponent here doesn't fit in a signed 64-bit integer until we
  234. // adjust for the radix point.
  235. {.token = "0x1.01p9223372036854775809",
  236. .mantissa = 0x101,
  237. .exponent = 9223372036854775801L,
  238. .radix = 16,
  239. .int_value = 1},
  240. // Binary real literals. These are invalid, but we accept them for error
  241. // recovery.
  242. {.token = "0b10_11_01.01",
  243. .mantissa = 0b10110101,
  244. .exponent = -2,
  245. .radix = 2,
  246. .int_value = 0b101101},
  247. };
  248. // Check we get the right real value.
  249. for (Testcase testcase : testcases) {
  250. error_tracker.Reset();
  251. EXPECT_THAT(Parse(testcase.token),
  252. HasRealValue({.radix = (testcase.radix == 10 ? 10 : 2),
  253. .mantissa = IsUnsignedInt(testcase.mantissa),
  254. .exponent = IsSignedInt(testcase.exponent)}))
  255. << testcase.token;
  256. EXPECT_EQ(error_tracker.seen_error(), testcase.radix == 2)
  257. << testcase.token;
  258. }
  259. // If we are required to stop at the `.` character, check we get the right int
  260. // value instead.
  261. for (Testcase testcase : testcases) {
  262. error_tracker.Reset();
  263. EXPECT_THAT(Parse(testcase.token, false),
  264. HasIntValue(IsUnsignedInt(testcase.int_value)))
  265. << testcase.token;
  266. EXPECT_FALSE(error_tracker.seen_error());
  267. }
  268. }
  269. TEST_F(NumericLiteralTest, HandlesRealLiteralOverflow) {
  270. llvm::StringLiteral input = "0x1.000001p-9223372036854775800";
  271. error_tracker.Reset();
  272. EXPECT_THAT(
  273. Parse(input),
  274. HasRealValue({.radix = 2,
  275. .mantissa = IsUnsignedInt(0x1000001),
  276. .exponent = Truly([](llvm::APInt exponent) {
  277. return (exponent + 9223372036854775800).getSExtValue() ==
  278. -24;
  279. })}));
  280. EXPECT_FALSE(error_tracker.seen_error());
  281. }
  282. TEST_F(NumericLiteralTest, ValidatesRealLiterals) {
  283. llvm::StringLiteral invalid[] = {
  284. // No digits in integer part.
  285. "0x.0",
  286. "0b.0",
  287. "0x_.0",
  288. "0b_.0",
  289. // No digits in fractional part.
  290. "0.e",
  291. "0.e0",
  292. "0.e+0",
  293. "0x0.p",
  294. "0x0.p-0",
  295. // Invalid digits in mantissa.
  296. "123A.4",
  297. "123.4A",
  298. "123A.4e0",
  299. "123.4Ae0",
  300. "0x123ABCDEFG.0",
  301. "0x123.ABCDEFG",
  302. "0x123ABCDEFG.0p0",
  303. "0x123.ABCDEFGp0",
  304. // Invalid exponent letter.
  305. "0.0f0",
  306. "0.0p0",
  307. "0.0z+0",
  308. "0x0.0e0",
  309. "0x0.0f0",
  310. "0x0.0z-0",
  311. // No digits in exponent part.
  312. "0.0e",
  313. "0x0.0p",
  314. "0.0e_",
  315. "0x0.0p_",
  316. // Invalid digits in exponent part.
  317. "0.0eHELLO",
  318. "0.0eA",
  319. "0.0e+A",
  320. "0x0.0pA",
  321. "0x0.0p-A",
  322. };
  323. for (llvm::StringLiteral literal : invalid) {
  324. error_tracker.Reset();
  325. EXPECT_THAT(Parse(literal), HasUnrecoverableError()) << literal;
  326. EXPECT_TRUE(error_tracker.seen_error()) << literal;
  327. }
  328. }
  329. TEST_F(NumericLiteralTest, TooManyDigits) {
  330. std::string long_number(2000, '1');
  331. EXPECT_THAT(Parse(long_number), HasUnrecoverableError());
  332. EXPECT_TRUE(error_tracker.seen_error());
  333. }
  334. } // namespace
  335. } // namespace Carbon::Lex