string_literal_test.cpp 7.0 KB

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