diff options
author | Daniel Jasper <djasper@google.com> | 2015-04-16 08:20:51 +0000 |
---|---|---|
committer | Daniel Jasper <djasper@google.com> | 2015-04-16 08:20:51 +0000 |
commit | 0d6ac27b86c2c9cdb3147713db8a85c24331bf5d (patch) | |
tree | 9f87a393586ca2d1fa9ac73645bac259a2a5cbbd /clang/lib/Format/Format.cpp | |
parent | 7d9d941b9b66f9bb5495aee34cec83bcdf23e180 (diff) | |
download | llvm-0d6ac27b86c2c9cdb3147713db8a85c24331bf5d.zip llvm-0d6ac27b86c2c9cdb3147713db8a85c24331bf5d.tar.gz llvm-0d6ac27b86c2c9cdb3147713db8a85c24331bf5d.tar.bz2 |
clang-format: [JS] handle comments in template strings.
Patch by Martin Probst. Thank you.
llvm-svn: 235078
Diffstat (limited to 'clang/lib/Format/Format.cpp')
-rw-r--r-- | clang/lib/Format/Format.cpp | 19 |
1 files changed, 17 insertions, 2 deletions
diff --git a/clang/lib/Format/Format.cpp b/clang/lib/Format/Format.cpp index 76fed58..ad9398c 100644 --- a/clang/lib/Format/Format.cpp +++ b/clang/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()); |