numeric_literal_test.cpp 10 KB

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