numeric_literal_test.cpp 9.8 KB

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