summaryrefslogtreecommitdiffstats
path: root/clang/lib
diff options
context:
space:
mode:
authorDaniel Jasper <djasper@google.com>2015-06-24 16:01:02 +0000
committerDaniel Jasper <djasper@google.com>2015-06-24 16:01:02 +0000
commit6b8d26c209cac64b3b830f6113e39a4d49cec759 (patch)
tree6564f9e93cf1e27a47a113d49b2bf10ad4ee7d4c /clang/lib
parentff666dcbef34429f53a9fc39e5a05a52849f4b59 (diff)
downloadbcm5719-llvm-6b8d26c209cac64b3b830f6113e39a4d49cec759.tar.gz
bcm5719-llvm-6b8d26c209cac64b3b830f6113e39a4d49cec759.zip
clang-format: [JS] Support regex literals containing quotes (' and ").
llvm-svn: 240548
Diffstat (limited to 'clang/lib')
-rw-r--r--clang/lib/Format/Format.cpp26
1 files changed, 23 insertions, 3 deletions
diff --git a/clang/lib/Format/Format.cpp b/clang/lib/Format/Format.cpp
index 0feeaa007f3..5275f9f0803 100644
--- a/clang/lib/Format/Format.cpp
+++ b/clang/lib/Format/Format.cpp
@@ -738,19 +738,33 @@ private:
bool tryMergeJSRegexLiteral() {
if (Tokens.size() < 2)
return false;
+
+ // If this is a string literal with a slash inside, compute the slash's
+ // offset and try to find the beginning of the regex literal.
+ // Also look at tok::unknown, as it can be an unterminated char literal.
+ size_t SlashInStringPos = StringRef::npos;
+ if (Tokens.back()->isOneOf(tok::string_literal, tok::char_constant,
+ tok::unknown)) {
+ // Start search from position 1 as otherwise, this is an unknown token
+ // for an unterminated /*-comment which is handled elsewhere.
+ SlashInStringPos = Tokens.back()->TokenText.find('/', 1);
+ if (SlashInStringPos == StringRef::npos)
+ return false;
+ }
+
// If a regex literal ends in "\//", this gets represented by an unknown
// token "\" and a comment.
bool MightEndWithEscapedSlash =
Tokens.back()->is(tok::comment) &&
Tokens.back()->TokenText.startswith("//") &&
Tokens[Tokens.size() - 2]->TokenText == "\\";
- if (!MightEndWithEscapedSlash &&
+ if (!MightEndWithEscapedSlash && SlashInStringPos == StringRef::npos &&
(Tokens.back()->isNot(tok::slash) ||
(Tokens[Tokens.size() - 2]->is(tok::unknown) &&
Tokens[Tokens.size() - 2]->TokenText == "\\")))
return false;
+
unsigned TokenCount = 0;
- unsigned LastColumn = Tokens.back()->OriginalColumn;
for (auto I = Tokens.rbegin() + 1, E = Tokens.rend(); I != E; ++I) {
++TokenCount;
if (I[0]->isOneOf(tok::slash, tok::slashequal) && I + 1 != E &&
@@ -758,11 +772,17 @@ private:
tok::exclaim, tok::l_square, tok::colon, tok::comma,
tok::question, tok::kw_return) ||
I[1]->isBinaryOperator())) {
+ unsigned LastColumn = Tokens.back()->OriginalColumn;
+ SourceLocation Loc = Tokens.back()->Tok.getLocation();
if (MightEndWithEscapedSlash) {
// This regex literal ends in '\//'. Skip past the '//' of the last
// token and re-start lexing from there.
- SourceLocation Loc = Tokens.back()->Tok.getLocation();
resetLexer(SourceMgr.getFileOffset(Loc) + 2);
+ } else if (SlashInStringPos != StringRef::npos) {
+ // This regex literal ends in a string_literal with a slash inside.
+ // Calculate end column and reset lexer appropriately.
+ resetLexer(SourceMgr.getFileOffset(Loc) + SlashInStringPos + 1);
+ LastColumn += SlashInStringPos;
}
Tokens.resize(Tokens.size() - TokenCount);
Tokens.back()->Tok.setKind(tok::unknown);
OpenPOWER on IntegriCloud