numeric_literal_test.cpp 10 KB

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