[PATCH] Better implementation of JavaScript === and !== operators.

Alexander Kornienko alexfh at google.com
Thu Nov 21 03:34:14 PST 2013


Hi djasper, klimek,

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

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

Files:
  lib/Format/Format.cpp
  lib/Format/TokenAnnotator.cpp
  unittests/Format/FormatTest.cpp

Index: lib/Format/Format.cpp
===================================================================
--- lib/Format/Format.cpp
+++ lib/Format/Format.cpp
@@ -1029,31 +1029,53 @@
     assert(Tokens.empty());
     do {
       Tokens.push_back(getNextToken());
-      maybeJoinPreviousTokens();
+      tryMergePreviousTokens();
     } while (Tokens.back()->Tok.isNot(tok::eof));
     return Tokens;
   }
 
   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 @@
     Tokens.pop_back();
     Tokens.pop_back();
     Tokens.back() = String;
+    return true;
   }
 
   FormatToken *getNextToken() {
Index: lib/Format/TokenAnnotator.cpp
===================================================================
--- lib/Format/TokenAnnotator.cpp
+++ lib/Format/TokenAnnotator.cpp
@@ -1364,10 +1364,6 @@
   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 @@
     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))
-------------- next part --------------
A non-text attachment was scrubbed...
Name: D2240.1.patch
Type: text/x-patch
Size: 3089 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/cfe-commits/attachments/20131121/dd760491/attachment.bin>


More information about the cfe-commits mailing list