From: Daniel Jasper Date: Sun, 14 Jun 2015 07:16:57 +0000 (+0000) Subject: clang-format: [JS] Fix corner case in template string parsing. X-Git-Tag: llvmorg-3.7.0-rc1~2404 X-Git-Url: http://review.tizen.org/git/?a=commitdiff_plain;h=2ebb0c57fbfcebba9de721e65bea0886b8033f36;p=platform%2Fupstream%2Fllvm.git clang-format: [JS] Fix corner case in template string parsing. 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 " llvm-svn: 239693 --- diff --git a/clang/lib/Format/Format.cpp b/clang/lib/Format/Format.cpp index a774f8c..aa91658 100644 --- a/clang/lib/Format/Format.cpp +++ b/clang/lib/Format/Format.cpp @@ -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 diff --git a/clang/unittests/Format/FormatTestJS.cpp b/clang/unittests/Format/FormatTestJS.cpp index 31386b4..5b01832 100644 --- a/clang/unittests/Format/FormatTestJS.cpp +++ b/clang/unittests/Format/FormatTestJS.cpp @@ -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" " ;",