numeric_literal_test.cpp 9.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335
  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/lexer/numeric_literal.h"
  5. #include <gmock/gmock.h>
  6. #include <gtest/gtest.h>
  7. #include <iterator>
  8. #include <memory>
  9. #include <vector>
  10. #include "common/check.h"
  11. #include "common/ostream.h"
  12. #include "toolchain/diagnostics/diagnostic_emitter.h"
  13. #include "toolchain/lexer/test_helpers.h"
  14. namespace Carbon::Testing {
  15. namespace {
  16. using ::testing::_;
  17. using ::testing::Field;
  18. using ::testing::Matcher;
  19. using ::testing::Property;
  20. using ::testing::Truly;
  21. using ::testing::VariantWith;
  22. class NumericLiteralTest : public ::testing::Test {
  23. protected:
  24. NumericLiteralTest() : error_tracker(ConsoleDiagnosticConsumer()) {}
  25. auto Lex(llvm::StringRef text) -> LexedNumericLiteral {
  26. llvm::Optional<LexedNumericLiteral> result = LexedNumericLiteral::Lex(text);
  27. CARBON_CHECK(result);
  28. EXPECT_EQ(result->text(), text);
  29. return *result;
  30. }
  31. auto Parse(llvm::StringRef text) -> LexedNumericLiteral::Value {
  32. Testing::SingleTokenDiagnosticTranslator translator(text);
  33. DiagnosticEmitter<const char*> emitter(translator, error_tracker);
  34. return Lex(text).ComputeValue(emitter);
  35. }
  36. ErrorTrackingDiagnosticConsumer error_tracker;
  37. };
  38. // Matcher for signed llvm::APInt.
  39. auto IsSignedInteger(int64_t value) -> Matcher<llvm::APInt> {
  40. return Property(&llvm::APInt::getSExtValue, value);
  41. }
  42. // Matcher for unsigned llvm::APInt.
  43. auto IsUnsignedInteger(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<LexedNumericLiteral::Value> {
  50. return VariantWith<LexedNumericLiteral::IntegerValue>(
  51. Field(&LexedNumericLiteral::IntegerValue::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<LexedNumericLiteral::Value> {
  61. return VariantWith<LexedNumericLiteral::RealValue>(AllOf(
  62. Field(&LexedNumericLiteral::RealValue::radix, real_matcher.radix),
  63. Field(&LexedNumericLiteral::RealValue::mantissa, real_matcher.mantissa),
  64. Field(&LexedNumericLiteral::RealValue::exponent, real_matcher.exponent)));
  65. }
  66. // Matcher for an unrecoverable parse error.
  67. auto HasUnrecoverableError() -> Matcher<LexedNumericLiteral::Value> {
  68. return VariantWith<LexedNumericLiteral::UnrecoverableError>(_);
  69. }
  70. TEST_F(NumericLiteralTest, HandlesIntegerLiteral) {
  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 (Testcase testcase : testcases) {
  83. error_tracker.Reset();
  84. EXPECT_THAT(Parse(testcase.token),
  85. HasIntValue(IsUnsignedInteger(testcase.value)))
  86. << testcase.token;
  87. EXPECT_FALSE(error_tracker.seen_error()) << testcase.token;
  88. }
  89. }
  90. TEST_F(NumericLiteralTest, ValidatesBaseSpecifier) {
  91. llvm::StringLiteral valid[] = {
  92. // Decimal integer literals.
  93. "0",
  94. "1",
  95. "123456789000000000000000000000000000000000000",
  96. // Hexadecimal integer literals.
  97. "0x0123456789ABCDEF",
  98. "0x0000000000000000000000000000000",
  99. // Binary integer literals.
  100. "0b10110100101001010",
  101. "0b0000000",
  102. };
  103. for (llvm::StringLiteral literal : valid) {
  104. error_tracker.Reset();
  105. EXPECT_THAT(Parse(literal), HasIntValue(_)) << literal;
  106. EXPECT_FALSE(error_tracker.seen_error()) << literal;
  107. }
  108. llvm::StringLiteral invalid[] = {
  109. "00", "0X123", "0o123", "0B1",
  110. "007", "123L", "123456789A", "0x",
  111. "0b", "0x123abc", "0b011101201001", "0b10A",
  112. "0x_", "0b_",
  113. };
  114. for (llvm::StringLiteral literal : invalid) {
  115. error_tracker.Reset();
  116. EXPECT_THAT(Parse(literal), HasUnrecoverableError()) << literal;
  117. EXPECT_TRUE(error_tracker.seen_error()) << literal;
  118. }
  119. }
  120. TEST_F(NumericLiteralTest, ValidatesIntegerDigitSeparators) {
  121. llvm::StringLiteral valid[] = {
  122. // Decimal literals optionally have digit separators every 3 places.
  123. "1_234",
  124. "123_456",
  125. "1_234_567",
  126. // Hexadecimal literals optionally have digit separators every 4 places.
  127. "0x1_0000",
  128. "0x1000_0000",
  129. "0x1_0000_0000",
  130. // Binary integer literals can have digit separators anywhere..
  131. "0b1_0_1_0_1_0",
  132. "0b111_0000",
  133. };
  134. for (llvm::StringLiteral literal : valid) {
  135. error_tracker.Reset();
  136. EXPECT_THAT(Parse(literal), HasIntValue(_)) << literal;
  137. EXPECT_FALSE(error_tracker.seen_error()) << literal;
  138. }
  139. llvm::StringLiteral invalid[] = {
  140. // Decimal literals.
  141. "12_34",
  142. "123_4_6_789",
  143. "12_3456_789",
  144. "12__345",
  145. "1_",
  146. // Hexadecimal literals.
  147. "0x_1234",
  148. "0x123_",
  149. "0x12_3",
  150. "0x_234_5678",
  151. "0x1234_567",
  152. // Binary literals.
  153. "0b_10101",
  154. "0b1__01",
  155. "0b1011_",
  156. "0b1_01_01_",
  157. };
  158. for (llvm::StringLiteral literal : invalid) {
  159. error_tracker.Reset();
  160. EXPECT_THAT(Parse(literal), HasIntValue(_)) << literal;
  161. EXPECT_TRUE(error_tracker.seen_error()) << literal;
  162. }
  163. }
  164. TEST_F(NumericLiteralTest, HandlesRealLiteral) {
  165. struct Testcase {
  166. llvm::StringLiteral token;
  167. uint64_t mantissa;
  168. int64_t exponent;
  169. unsigned radix;
  170. };
  171. Testcase testcases[] = {
  172. // Decimal real literals.
  173. {.token = "0.0", .mantissa = 0, .exponent = -1, .radix = 10},
  174. {.token = "12.345", .mantissa = 12345, .exponent = -3, .radix = 10},
  175. {.token = "12.345e6", .mantissa = 12345, .exponent = 3, .radix = 10},
  176. {.token = "12.345e+6", .mantissa = 12345, .exponent = 3, .radix = 10},
  177. {.token = "1_234.5e-2", .mantissa = 12345, .exponent = -3, .radix = 10},
  178. {.token = "1.0e-2_000_000",
  179. .mantissa = 10,
  180. .exponent = -2'000'001,
  181. .radix = 10},
  182. // Hexadecimal real literals.
  183. {.token = "0x1_2345_6789.CDEF",
  184. .mantissa = 0x1'2345'6789'CDEF,
  185. .exponent = -16,
  186. .radix = 16},
  187. {.token = "0x0.0001p4", .mantissa = 1, .exponent = -12, .radix = 16},
  188. {.token = "0x0.0001p+4", .mantissa = 1, .exponent = -12, .radix = 16},
  189. {.token = "0x0.0001p-4", .mantissa = 1, .exponent = -20, .radix = 16},
  190. // The exponent here works out as exactly INT64_MIN.
  191. {.token = "0x1.01p-9223372036854775800",
  192. .mantissa = 0x101,
  193. .exponent = -9223372036854775807L - 1L,
  194. .radix = 16},
  195. // The exponent here doesn't fit in a signed 64-bit integer until we
  196. // adjust for the radix point.
  197. {.token = "0x1.01p9223372036854775809",
  198. .mantissa = 0x101,
  199. .exponent = 9223372036854775801L,
  200. .radix = 16},
  201. // Binary real literals. These are invalid, but we accept them for error
  202. // recovery.
  203. {.token = "0b10_11_01.01",
  204. .mantissa = 0b10110101,
  205. .exponent = -2,
  206. .radix = 2},
  207. };
  208. for (Testcase testcase : testcases) {
  209. error_tracker.Reset();
  210. EXPECT_THAT(Parse(testcase.token),
  211. HasRealValue({.radix = (testcase.radix == 10 ? 10 : 2),
  212. .mantissa = IsUnsignedInteger(testcase.mantissa),
  213. .exponent = IsSignedInteger(testcase.exponent)}))
  214. << testcase.token;
  215. EXPECT_EQ(error_tracker.seen_error(), testcase.radix == 2)
  216. << testcase.token;
  217. }
  218. }
  219. TEST_F(NumericLiteralTest, HandlesRealLiteralOverflow) {
  220. llvm::StringLiteral input = "0x1.000001p-9223372036854775800";
  221. error_tracker.Reset();
  222. EXPECT_THAT(
  223. Parse(input),
  224. HasRealValue({.radix = 2,
  225. .mantissa = IsUnsignedInteger(0x1000001),
  226. .exponent = Truly([](llvm::APInt exponent) {
  227. return (exponent + 9223372036854775800).getSExtValue() ==
  228. -24;
  229. })}));
  230. EXPECT_FALSE(error_tracker.seen_error());
  231. }
  232. TEST_F(NumericLiteralTest, ValidatesRealLiterals) {
  233. llvm::StringLiteral invalid_digit_separators[] = {
  234. // Invalid digit separators.
  235. "12_34.5", "123.4_567", "123.456_7", "1_2_3.4",
  236. "123.4e56_78", "0x12_34.5", "0x12.3_4", "0x12.34p5_6",
  237. };
  238. for (llvm::StringLiteral literal : invalid_digit_separators) {
  239. error_tracker.Reset();
  240. EXPECT_THAT(Parse(literal), HasRealValue({})) << literal;
  241. EXPECT_TRUE(error_tracker.seen_error()) << literal;
  242. }
  243. llvm::StringLiteral invalid[] = {
  244. // No digits in integer part.
  245. "0x.0",
  246. "0b.0",
  247. "0x_.0",
  248. "0b_.0",
  249. // No digits in fractional part.
  250. "0.e",
  251. "0.e0",
  252. "0.e+0",
  253. "0x0.p",
  254. "0x0.p-0",
  255. // Invalid digits in mantissa.
  256. "123A.4",
  257. "123.4A",
  258. "123A.4e0",
  259. "123.4Ae0",
  260. "0x123ABCDEFG.0",
  261. "0x123.ABCDEFG",
  262. "0x123ABCDEFG.0p0",
  263. "0x123.ABCDEFGp0",
  264. // Invalid exponent letter.
  265. "0.0f0",
  266. "0.0p0",
  267. "0.0z+0",
  268. "0x0.0e0",
  269. "0x0.0f0",
  270. "0x0.0z-0",
  271. // No digits in exponent part.
  272. "0.0e",
  273. "0x0.0p",
  274. "0.0e_",
  275. "0x0.0p_",
  276. // Invalid digits in exponent part.
  277. "0.0eHELLO",
  278. "0.0eA",
  279. "0.0e+A",
  280. "0x0.0pA",
  281. "0x0.0p-A",
  282. };
  283. for (llvm::StringLiteral literal : invalid) {
  284. error_tracker.Reset();
  285. EXPECT_THAT(Parse(literal), HasUnrecoverableError()) << literal;
  286. EXPECT_TRUE(error_tracker.seen_error()) << literal;
  287. }
  288. }
  289. TEST_F(NumericLiteralTest, TooManyDigits) {
  290. std::string long_number(2000, '1');
  291. EXPECT_THAT(Parse(long_number), HasUnrecoverableError());
  292. EXPECT_TRUE(error_tracker.seen_error());
  293. }
  294. } // namespace
  295. } // namespace Carbon::Testing