r250009 - clang-format: [JS] handle character classes in regexes.
Daniel Jasper via cfe-commits
cfe-commits at lists.llvm.org
Sun Oct 11 20:13:48 PDT 2015
Author: djasper
Date: Sun Oct 11 22:13:48 2015
New Revision: 250009
URL: http://llvm.org/viewvc/llvm-project?rev=250009&view=rev
Log:
clang-format: [JS] handle character classes in regexes.
Slashes in regular expressions do not need to be escaped and do not
terminate the regular expression even without a preceding backslash.
Patch by Martin Probst. Thank you.
Modified:
cfe/trunk/lib/Format/Format.cpp
cfe/trunk/unittests/Format/FormatTestJS.cpp
Modified: cfe/trunk/lib/Format/Format.cpp
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/lib/Format/Format.cpp?rev=250009&r1=250008&r2=250009&view=diff
==============================================================================
--- cfe/trunk/lib/Format/Format.cpp (original)
+++ cfe/trunk/lib/Format/Format.cpp Sun Oct 11 22:13:48 2015
@@ -876,12 +876,23 @@ private:
return false;
unsigned TokenCount = 0;
+ bool InCharacterClass = false;
for (auto I = Tokens.rbegin() + 1, E = Tokens.rend(); I != E; ++I) {
++TokenCount;
auto Prev = I + 1;
while (Prev != E && Prev[0]->is(tok::comment))
++Prev;
- if (I[0]->isOneOf(tok::slash, tok::slashequal) &&
+ // Slashes in character classes (delimited by [ and ]) do not need
+ // escaping. Escaping of the squares themselves is already handled by
+ // \c tryMergeEscapeSequence(), a plain tok::r_square must be non-escaped.
+ if (I[0]->is(tok::r_square))
+ InCharacterClass = true;
+ if (I[0]->is(tok::l_square)) {
+ if (!InCharacterClass)
+ return false;
+ InCharacterClass = false;
+ }
+ if (!InCharacterClass && I[0]->isOneOf(tok::slash, tok::slashequal) &&
(Prev == E ||
((Prev[0]->isOneOf(tok::l_paren, tok::semi, tok::l_brace,
tok::r_brace, tok::exclaim, tok::l_square,
Modified: cfe/trunk/unittests/Format/FormatTestJS.cpp
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/unittests/Format/FormatTestJS.cpp?rev=250009&r1=250008&r2=250009&view=diff
==============================================================================
--- cfe/trunk/unittests/Format/FormatTestJS.cpp (original)
+++ cfe/trunk/unittests/Format/FormatTestJS.cpp Sun Oct 11 22:13:48 2015
@@ -617,9 +617,15 @@ TEST_F(FormatTestJS, RegexLiteralSpecial
verifyFormat("var regex = /x|y/;");
verifyFormat("var regex = /a{2}/;");
verifyFormat("var regex = /a{1,3}/;");
+
verifyFormat("var regex = /[abc]/;");
verifyFormat("var regex = /[^abc]/;");
verifyFormat("var regex = /[\\b]/;");
+ verifyFormat("var regex = /[/]/;");
+ verifyFormat("var regex = /[\\/]/;");
+ verifyFormat("var regex = /\\[/;");
+ verifyFormat("var regex = /\\\\[/]/;");
+
verifyFormat("var regex = /\\b/;");
verifyFormat("var regex = /\\B/;");
verifyFormat("var regex = /\\d/;");
More information about the cfe-commits
mailing list