r195354 - Better implementation of JavaScript === and !== operators.

Alexander Kornienko alexfh at google.com
Thu Nov 21 04:43:57 PST 2013


Author: alexfh
Date: Thu Nov 21 06:43:57 2013
New Revision: 195354

URL: http://llvm.org/viewvc/llvm-project?rev=195354&view=rev
Log:
Better implementation of JavaScript === and !== operators.

Summary:
Now based on token merging. Now they are not only prevented from being
split, but are actually formatted as comparison operators.

Reviewers: djasper, klimek

Reviewed By: klimek

CC: cfe-commits, klimek

Differential Revision: http://llvm-reviews.chandlerc.com/D2240

Modified:
    cfe/trunk/lib/Format/Format.cpp
    cfe/trunk/lib/Format/TokenAnnotator.cpp
    cfe/trunk/unittests/Format/FormatTest.cpp

Modified: cfe/trunk/lib/Format/Format.cpp
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/lib/Format/Format.cpp?rev=195354&r1=195353&r2=195354&view=diff
==============================================================================
--- cfe/trunk/lib/Format/Format.cpp (original)
+++ cfe/trunk/lib/Format/Format.cpp Thu Nov 21 06:43:57 2013
@@ -1029,7 +1029,7 @@ public:
     assert(Tokens.empty());
     do {
       Tokens.push_back(getNextToken());
-      maybeJoinPreviousTokens();
+      tryMergePreviousTokens();
     } while (Tokens.back()->Tok.isNot(tok::eof));
     return Tokens;
   }
@@ -1037,23 +1037,45 @@ public:
   IdentifierTable &getIdentTable() { return IdentTable; }
 
 private:
-  void maybeJoinPreviousTokens() {
+  void tryMergePreviousTokens() {
+    tryMerge_TMacro() || tryMergeJavaScriptIdentityOperators();
+  }
+
+  bool tryMergeJavaScriptIdentityOperators() {
+    if (Tokens.size() < 2)
+      return false;
+    FormatToken &First = *Tokens[Tokens.size() - 2];
+    if (!First.isOneOf(tok::exclaimequal, tok::equalequal))
+      return false;
+    FormatToken &Second = *Tokens.back();
+    if (!Second.is(tok::equal))
+      return false;
+    if (Second.WhitespaceRange.getBegin() != Second.WhitespaceRange.getEnd())
+      return false;
+    First.TokenText =
+        StringRef(First.TokenText.data(), First.TokenText.size() + 1);
+    First.ColumnWidth += 1;
+    Tokens.pop_back();
+    return true;
+  }
+
+  bool tryMerge_TMacro() {
     if (Tokens.size() < 4)
-      return;
+      return false;
     FormatToken *Last = Tokens.back();
     if (!Last->is(tok::r_paren))
-      return;
+      return false;
 
     FormatToken *String = Tokens[Tokens.size() - 2];
     if (!String->is(tok::string_literal) || String->IsMultiline)
-      return;
+      return false;
 
     if (!Tokens[Tokens.size() - 3]->is(tok::l_paren))
-      return;
+      return false;
 
     FormatToken *Macro = Tokens[Tokens.size() - 4];
     if (Macro->TokenText != "_T")
-      return;
+      return false;
 
     const char *Start = Macro->TokenText.data();
     const char *End = Last->TokenText.data() + Last->TokenText.size();
@@ -1069,6 +1091,7 @@ private:
     Tokens.pop_back();
     Tokens.pop_back();
     Tokens.back() = String;
+    return true;
   }
 
   FormatToken *getNextToken() {

Modified: cfe/trunk/lib/Format/TokenAnnotator.cpp
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/lib/Format/TokenAnnotator.cpp?rev=195354&r1=195353&r2=195354&view=diff
==============================================================================
--- cfe/trunk/lib/Format/TokenAnnotator.cpp (original)
+++ cfe/trunk/lib/Format/TokenAnnotator.cpp Thu Nov 21 06:43:57 2013
@@ -1364,10 +1364,6 @@ bool TokenAnnotator::spaceRequiredBefore
   if (Tok.isOneOf(tok::arrowstar, tok::periodstar) ||
       Tok.Previous->isOneOf(tok::arrowstar, tok::periodstar))
     return false;
-  // JavaScript identity operators ===, !==.
-  if (Tok.Previous->isOneOf(tok::equalequal, tok::exclaimequal) &&
-      Tok.is(tok::equal))
-    return false;
   if (!Style.SpaceBeforeAssignmentOperators &&
       Tok.getPrecedence() == prec::Assignment)
     return false;
@@ -1457,9 +1453,6 @@ bool TokenAnnotator::canBreakBefore(cons
     return false;
   if (Left.is(tok::equal) && Line.Type == LT_VirtualFunctionDecl)
     return false;
-  // JavaScript identity operators ===, !==.
-  if (Left.isOneOf(tok::equalequal, tok::exclaimequal) && Right.is(tok::equal))
-    return false;
   if (Left.Previous) {
     if (Left.is(tok::l_paren) && Right.is(tok::l_paren) &&
         Left.Previous->is(tok::kw___attribute))

Modified: cfe/trunk/unittests/Format/FormatTest.cpp
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/unittests/Format/FormatTest.cpp?rev=195354&r1=195353&r2=195354&view=diff
==============================================================================
--- cfe/trunk/unittests/Format/FormatTest.cpp (original)
+++ cfe/trunk/unittests/Format/FormatTest.cpp Thu Nov 21 06:43:57 2013
@@ -7326,12 +7326,19 @@ TEST_F(FormatTest, SpacesInAngles) {
   verifyFormat("A<A<int>>();", Spaces);
 }
 
-
 TEST_F(FormatTest, UnderstandsJavaScript) {
+  verifyFormat("a == = b;");
+  verifyFormat("a != = b;");
+
   verifyFormat("a === b;");
-  verifyFormat("aaaaaaa === b;", getLLVMStyleWithColumns(10));
+  verifyFormat("aaaaaaa ===\n    b;", getLLVMStyleWithColumns(10));
   verifyFormat("a !== b;");
-  verifyFormat("aaaaaaa !== b;", getLLVMStyleWithColumns(10));
+  verifyFormat("aaaaaaa !==\n    b;", getLLVMStyleWithColumns(10));
+  verifyFormat("if (a + b + c +\n"
+               "        d !==\n"
+               "    e + f + g)\n"
+               "  q();",
+               getLLVMStyleWithColumns(20));
 }
 
 } // end namespace tooling





More information about the cfe-commits mailing list