numeric_literal_test.cpp 10 KB

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