]> granicus.if.org Git - clang/commitdiff
clang-format: [JS] Fix corner case in template string parsing.
authorDaniel Jasper <djasper@google.com>
Sun, 14 Jun 2015 07:16:57 +0000 (07:16 +0000)
committerDaniel Jasper <djasper@google.com>
Sun, 14 Jun 2015 07:16:57 +0000 (07:16 +0000)
Before, these would not properly detected because of the char/string
literal found when re-lexing after the first `:

  var x = `'`;  // comment with matching quote '
  var x = `"`;  // comment with matching quote "

git-svn-id: https://llvm.org/svn/llvm-project/cfe/trunk@239693 91177308-0d34-0410-b5e6-96231b3b80d8

lib/Format/Format.cpp
unittests/Format/FormatTestJS.cpp

index a774f8cdd59aca2f3238204b25c5de5b4d655132..aa91658491175133a4547aa2aa5ece6487ada609 100644 (file)
@@ -785,7 +785,8 @@ private:
     // Backticks get lexed as tok::unknown tokens. If a template string contains
     // a comment start, it gets lexed as a tok::comment, or tok::unknown if
     // unterminated.
-    if (!EndBacktick->isOneOf(tok::comment, tok::unknown))
+    if (!EndBacktick->isOneOf(tok::comment, tok::string_literal,
+                              tok::char_constant, tok::unknown))
       return false;
     size_t CommentBacktickPos = EndBacktick->TokenText.find('`');
     // Unknown token that's not actually a backtick, or a comment that doesn't
index 31386b448ac6237a69aa9858cab0a358eea2ab67..5b01832ef4584965a0ae4d25606cb7582e1b2e82 100644 (file)
@@ -809,6 +809,11 @@ TEST_F(FormatTestJS, TemplateStrings) {
             "var y;",
             format("var x =\n `/*a`;\n"
                    "var y;"));
+  // Unterminated string literals in a template string.
+  verifyFormat("var x = `'`;  // comment with matching quote '\n"
+               "var y;");
+  verifyFormat("var x = `\"`;  // comment with matching quote \"\n"
+               "var y;");
   // Backticks in a comment - not a template string.
   EXPECT_EQ("var x = 1  // `/*a`;\n"
             "    ;",