string_literal_test.cpp 7.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311
  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/string_literal.h"
  5. #include <gmock/gmock.h>
  6. #include <gtest/gtest.h>
  7. #include "common/check.h"
  8. #include "common/ostream.h"
  9. #include "toolchain/diagnostics/diagnostic_emitter.h"
  10. #include "toolchain/lexer/test_helpers.h"
  11. namespace Carbon::Testing {
  12. namespace {
  13. class StringLiteralTest : public ::testing::Test {
  14. protected:
  15. StringLiteralTest() : error_tracker(ConsoleDiagnosticConsumer()) {}
  16. auto Lex(llvm::StringRef text) -> LexedStringLiteral {
  17. llvm::Optional<LexedStringLiteral> result = LexedStringLiteral::Lex(text);
  18. CARBON_CHECK(result);
  19. EXPECT_EQ(result->text(), text);
  20. return *result;
  21. }
  22. auto Parse(llvm::StringRef text) -> std::string {
  23. LexedStringLiteral token = Lex(text);
  24. Testing::SingleTokenDiagnosticTranslator translator(text);
  25. DiagnosticEmitter<const char*> emitter(translator, error_tracker);
  26. return token.ComputeValue(emitter);
  27. }
  28. ErrorTrackingDiagnosticConsumer error_tracker;
  29. };
  30. TEST_F(StringLiteralTest, StringLiteralBounds) {
  31. llvm::StringLiteral valid[] = {
  32. R"("")",
  33. R"("""
  34. """)",
  35. R"("""
  36. "foo"
  37. """)",
  38. // Escaped terminators don't end the string.
  39. R"("\"")",
  40. R"("\\")",
  41. R"("\\\"")",
  42. R"("""
  43. \"""
  44. """)",
  45. R"("""
  46. "\""
  47. """)",
  48. R"("""
  49. ""\"
  50. """)",
  51. R"("""
  52. ""\
  53. """)",
  54. R"(#"""
  55. """\#n
  56. """#)",
  57. // Only a matching number of '#'s terminates the string.
  58. R"(#""#)",
  59. R"(#"xyz"foo"#)",
  60. R"(##"xyz"#foo"##)",
  61. R"(#"\""#)",
  62. // Escape sequences likewise require a matching number of '#'s.
  63. R"(#"\#"#"#)",
  64. R"(#"\"#)",
  65. R"(#"""
  66. \#"""#
  67. """#)",
  68. // #"""# does not start a multiline string literal.
  69. R"(#"""#)",
  70. R"(##"""##)",
  71. };
  72. for (llvm::StringLiteral test : valid) {
  73. SCOPED_TRACE(test);
  74. llvm::Optional<LexedStringLiteral> result = LexedStringLiteral::Lex(test);
  75. EXPECT_TRUE(result.hasValue());
  76. if (result) {
  77. EXPECT_EQ(result->text(), test);
  78. }
  79. }
  80. llvm::StringLiteral invalid[] = {
  81. // clang-format off
  82. R"(")",
  83. R"("""
  84. "")",
  85. R"("\)",
  86. R"("\")",
  87. R"("\\)",
  88. R"("\\\")",
  89. R"("""
  90. )",
  91. R"(#"""
  92. """)",
  93. R"(" \
  94. ")",
  95. // clang-format on
  96. };
  97. for (llvm::StringLiteral test : invalid) {
  98. SCOPED_TRACE(test);
  99. llvm::Optional<LexedStringLiteral> result = LexedStringLiteral::Lex(test);
  100. EXPECT_TRUE(result.hasValue());
  101. if (result) {
  102. EXPECT_FALSE(result->is_terminated());
  103. }
  104. }
  105. }
  106. TEST_F(StringLiteralTest, StringLiteralContents) {
  107. std::pair<llvm::StringLiteral, llvm::StringLiteral> testcases[] = {
  108. // Empty strings.
  109. {R"("")", ""},
  110. {R"(
  111. """
  112. """
  113. )",
  114. ""},
  115. // Nearly-empty strings.
  116. {R"(
  117. """
  118. """
  119. )",
  120. "\n"},
  121. // Lines containing only whitespace are treated as empty even if they
  122. // contain tabs.
  123. {"\"\"\"\n\t \t\n\"\"\"", "\n"},
  124. // Indent removal.
  125. {R"(
  126. """file type indicator
  127. indented contents \
  128. """
  129. )",
  130. " indented contents "},
  131. // Removal of tabs in indent and suffix.
  132. {"\"\"\"\n \t hello \t \n \t \"\"\"", " hello\n"},
  133. {R"(
  134. """
  135. hello
  136. world
  137. end of test
  138. """
  139. )",
  140. " hello\nworld\n\n end of test\n"},
  141. // Escape sequences.
  142. {R"(
  143. "\x14,\u{1234},\u{00000010},\n,\r,\t,\0,\",\',\\"
  144. )",
  145. llvm::StringLiteral::withInnerNUL(
  146. "\x14,\xE1\x88\xB4,\x10,\x0A,\x0D,\x09,\x00,\x22,\x27,\x5C")},
  147. {R"(
  148. "\0A\x1234"
  149. )",
  150. llvm::StringLiteral::withInnerNUL("\0A\x12"
  151. "34")},
  152. {R"(
  153. "\u{D7FF},\u{E000},\u{10FFFF}"
  154. )",
  155. "\xED\x9F\xBF,\xEE\x80\x80,\xF4\x8F\xBF\xBF"},
  156. // Escape sequences in 'raw' strings.
  157. {R"(
  158. #"\#x00,\#xFF,\#u{56789},\#u{ABCD},\#u{00000000000000000EF}"#
  159. )",
  160. llvm::StringLiteral::withInnerNUL(
  161. "\x00,\xFF,\xF1\x96\x9E\x89,\xEA\xAF\x8D,\xC3\xAF")},
  162. {R"(
  163. ##"\n,\#n,\##n,\##\##n,\##\###n"##
  164. )",
  165. "\\n,\\#n,\n,\\##n,\\###n"},
  166. // Trailing whitespace handling.
  167. {"\"\"\"\n Hello \\\n World \t \n Bye! \\\n \"\"\"",
  168. "Hello World\nBye! "},
  169. };
  170. for (auto [test, contents] : testcases) {
  171. error_tracker.Reset();
  172. auto value = Parse(test.trim());
  173. EXPECT_FALSE(error_tracker.seen_error()) << "`" << test << "`";
  174. EXPECT_EQ(value, contents);
  175. }
  176. }
  177. TEST_F(StringLiteralTest, StringLiteralBadIndent) {
  178. std::pair<llvm::StringLiteral, llvm::StringLiteral> testcases[] = {
  179. // Indent doesn't match the last line.
  180. {"\"\"\"\n \tx\n \"\"\"", "x\n"},
  181. {"\"\"\"\n x\n \"\"\"", "x\n"},
  182. {"\"\"\"\n x\n\t\"\"\"", "x\n"},
  183. {"\"\"\"\n ok\n bad\n \"\"\"", "ok\nbad\n"},
  184. {"\"\"\"\n bad\n ok\n \"\"\"", "bad\nok\n"},
  185. {"\"\"\"\n escaped,\\\n bad\n \"\"\"", "escaped,bad\n"},
  186. // Indent on last line is followed by text.
  187. {"\"\"\"\n x\n x\"\"\"", "x\nx"},
  188. {"\"\"\"\n x\n x\"\"\"", " x\nx"},
  189. {"\"\"\"\n x\n x\"\"\"", "x\nx"},
  190. };
  191. for (auto [test, contents] : testcases) {
  192. error_tracker.Reset();
  193. auto value = Parse(test);
  194. EXPECT_TRUE(error_tracker.seen_error()) << "`" << test << "`";
  195. EXPECT_EQ(value, contents);
  196. }
  197. }
  198. TEST_F(StringLiteralTest, StringLiteralBadEscapeSequence) {
  199. llvm::StringLiteral testcases[] = {
  200. R"("\a")",
  201. R"("\b")",
  202. R"("\e")",
  203. R"("\f")",
  204. R"("\v")",
  205. R"("\?")",
  206. R"("\1")",
  207. R"("\9")",
  208. // \0 can't be followed by a decimal digit.
  209. R"("\01")",
  210. R"("\09")",
  211. // \x requires two (uppercase) hexadecimal digits.
  212. R"("\x")",
  213. R"("\x0")",
  214. R"("\x0G")",
  215. R"("\xab")",
  216. R"("\x\n")",
  217. R"("\x\"")",
  218. // \u requires a braced list of one or more hexadecimal digits.
  219. R"("\u")",
  220. R"("\u?")",
  221. R"("\u\"")",
  222. R"("\u{")",
  223. R"("\u{}")",
  224. R"("\u{A")",
  225. R"("\u{G}")",
  226. R"("\u{0000012323127z}")",
  227. R"("\u{-3}")",
  228. // \u must specify a non-surrogate code point.
  229. R"("\u{110000}")",
  230. R"("\u{000000000000000000000000000000000110000}")",
  231. R"("\u{D800}")",
  232. R"("\u{DFFF}")",
  233. };
  234. for (llvm::StringLiteral test : testcases) {
  235. error_tracker.Reset();
  236. auto value = Parse(test);
  237. EXPECT_TRUE(error_tracker.seen_error()) << "`" << test << "`";
  238. // TODO: Test value produced by error recovery.
  239. }
  240. }
  241. TEST_F(StringLiteralTest, TabInString) {
  242. auto value = Parse("\"x\ty\"");
  243. EXPECT_TRUE(error_tracker.seen_error());
  244. EXPECT_EQ(value, "x\ty");
  245. }
  246. TEST_F(StringLiteralTest, TabAtEndOfString) {
  247. auto value = Parse("\"\t\t\t\"");
  248. EXPECT_TRUE(error_tracker.seen_error());
  249. EXPECT_EQ(value, "\t\t\t");
  250. }
  251. TEST_F(StringLiteralTest, TabInBlockString) {
  252. auto value = Parse("\"\"\"\nx\ty\n\"\"\"");
  253. EXPECT_TRUE(error_tracker.seen_error());
  254. EXPECT_EQ(value, "x\ty\n");
  255. }
  256. TEST_F(StringLiteralTest, UnicodeTooManyDigits) {
  257. std::string text = "u{";
  258. text.append(10000, '9');
  259. text.append("}");
  260. auto value = Parse("\"\\" + text + "\"");
  261. EXPECT_TRUE(error_tracker.seen_error());
  262. EXPECT_EQ(value, text);
  263. }
  264. } // namespace
  265. } // namespace Carbon::Testing