string_literal_test.cpp 5.9 KB

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