From 8009ca27ad29257a04865adb93e7a6912b48a2e6 Mon Sep 17 00:00:00 2001 From: Daniel Jasper Date: Thu, 16 Apr 2015 08:20:51 +0000 Subject: [PATCH] clang-format: [JS] handle comments in template strings. Patch by Martin Probst. Thank you. git-svn-id: https://llvm.org/svn/llvm-project/cfe/trunk@235078 91177308-0d34-0410-b5e6-96231b3b80d8 --- lib/Format/Format.cpp | 19 +++++++++++++++++-- unittests/Format/FormatTestJS.cpp | 27 +++++++++++++++++++++++++++ 2 files changed, 44 insertions(+), 2 deletions(-) diff --git a/lib/Format/Format.cpp b/lib/Format/Format.cpp index 76fed58f18..ad9398c7bd 100644 --- a/lib/Format/Format.cpp +++ b/lib/Format/Format.cpp @@ -780,7 +780,15 @@ private: return false; FormatToken *EndBacktick = Tokens.back(); - if (!(EndBacktick->is(tok::unknown) && EndBacktick->TokenText == "`")) + // 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)) + return false; + size_t CommentBacktickPos = EndBacktick->TokenText.find('`'); + // Unknown token that's not actually a backtick, or a comment that doesn't + // contain a backtick. + if (CommentBacktickPos == StringRef::npos) return false; unsigned TokenCount = 0; @@ -812,7 +820,14 @@ private: Tokens.resize(Tokens.size() - TokenCount); Tokens.back()->Type = TT_TemplateString; - const char *EndOffset = EndBacktick->TokenText.data() + 1; + const char *EndOffset = + EndBacktick->TokenText.data() + 1 + CommentBacktickPos; + if (CommentBacktickPos != 0) { + // If the backtick was not the first character (e.g. in a comment), + // re-lex after the backtick position. + SourceLocation Loc = EndBacktick->Tok.getLocation(); + resetLexer(SourceMgr.getFileOffset(Loc) + CommentBacktickPos + 1); + } Tokens.back()->TokenText = StringRef(Tokens.back()->TokenText.data(), EndOffset - Tokens.back()->TokenText.data()); diff --git a/unittests/Format/FormatTestJS.cpp b/unittests/Format/FormatTestJS.cpp index 3c8c14d2ba..695bad5553 100644 --- a/unittests/Format/FormatTestJS.cpp +++ b/unittests/Format/FormatTestJS.cpp @@ -648,6 +648,33 @@ TEST_F(FormatTestJS, TemplateStrings) { // Two template strings. verifyFormat("var x = `hello` == `hello`;"); + + // Comments in template strings. + EXPECT_EQ("var x = `//a`;\n" + "var y;", + format("var x =\n `//a`;\n" + "var y ;")); + EXPECT_EQ("var x = `/*a`;\n" + "var y;", + format("var x =\n `/*a`;\n" + "var y;")); + // Backticks in a comment - not a template string. + EXPECT_EQ("var x = 1 // `/*a`;\n" + " ;", + format("var x =\n 1 // `/*a`;\n" + " ;")); + EXPECT_EQ("/* ` */ var x = 1; /* ` */", + format("/* ` */ var x\n= 1; /* ` */")); + // Comment spans multiple template strings. + EXPECT_EQ("var x = `/*a`;\n" + "var y = ` */ `;", + format("var x =\n `/*a`;\n" + "var y =\n ` */ `;")); + // Escaped backtick. + EXPECT_EQ("var x = ` \\` a`;\n" + "var y;", + format("var x = ` \\` a`;\n" + "var y;")); } TEST_F(FormatTestJS, CastSyntax) { -- 2.40.0