diff options
author | Daniel Jasper <djasper@google.com> | 2014-05-12 11:29:50 +0000 |
---|---|---|
committer | Daniel Jasper <djasper@google.com> | 2014-05-12 11:29:50 +0000 |
commit | fb4333b093352de9298fff0f5307d60953a3520b (patch) | |
tree | c720526310768755365d051eb49bd488328de18f /clang/lib/Format | |
parent | 238c7c165b80444d4bac96632418720ca10f5748 (diff) | |
download | bcm5719-llvm-fb4333b093352de9298fff0f5307d60953a3520b.tar.gz bcm5719-llvm-fb4333b093352de9298fff0f5307d60953a3520b.zip |
clang-format: [JS] Basic support for escape sequences in regex literals.
Before:
var regex = /\\/ g; // This isn't even recognized as regex.
After:
var regex = /\\/g; // It now is.
llvm-svn: 208539
Diffstat (limited to 'clang/lib/Format')
-rw-r--r-- | clang/lib/Format/Format.cpp | 22 |
1 files changed, 21 insertions, 1 deletions
diff --git a/clang/lib/Format/Format.cpp b/clang/lib/Format/Format.cpp index 30dc88994e2..60706b1b3be 100644 --- a/clang/lib/Format/Format.cpp +++ b/clang/lib/Format/Format.cpp @@ -1216,6 +1216,8 @@ private: return; if (Style.Language == FormatStyle::LK_JavaScript) { + if (tryMergeEscapeSequence()) + return; if (tryMergeJSRegexLiteral()) return; @@ -1256,6 +1258,23 @@ private: return true; } + // Tries to merge an escape sequence, i.e. a "\\" and the following + // charachter. Use e.g. inside JavaScript regex literals. + bool tryMergeEscapeSequence() { + if (Tokens.size() < 2) + return false; + FormatToken *Previous = Tokens[Tokens.size() - 2]; + if (Previous->isNot(tok::unknown) || Previous->TokenText != "\\" || + Tokens.back()->NewlinesBefore != 0) + return false; + Previous->ColumnWidth += Tokens.back()->ColumnWidth; + StringRef Text = Previous->TokenText; + Previous->TokenText = + StringRef(Text.data(), Text.size() + Tokens.back()->TokenText.size()); + Tokens.resize(Tokens.size() - 1); + return true; + } + // Try to determine whether the current token ends a JavaScript regex literal. // We heuristically assume that this is a regex literal if we find two // unescaped slashes on a line and the token before the first slash is one of @@ -1263,7 +1282,8 @@ private: // a division. bool tryMergeJSRegexLiteral() { if (Tokens.size() < 2 || Tokens.back()->isNot(tok::slash) || - Tokens[Tokens.size() - 2]->is(tok::unknown)) + (Tokens[Tokens.size() - 2]->is(tok::unknown) && + Tokens[Tokens.size() - 2]->TokenText == "\\")) return false; unsigned TokenCount = 0; unsigned LastColumn = Tokens.back()->OriginalColumn; |