numeric_literal_test.cpp 9.9 KB

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