numeric_literal_test.cpp 10 KB

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