numeric_literal_test.cpp 11 KB

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