[cfe-commits] r169363 - in /cfe/trunk: lib/Format/Format.cpp unittests/Format/FormatTest.cpp

Daniel Jasper djasper at google.com
Tue Dec 4 23:51:39 PST 2012


Author: djasper
Date: Wed Dec  5 01:51:39 2012
New Revision: 169363

URL: http://llvm.org/viewvc/llvm-project?rev=169363&view=rev
Log:
Small tweaks to automatic formatting.

Recognize '!=' as a binary operator and assume that there are no
type definitions on the RHS of an assignment.

Modified:
    cfe/trunk/lib/Format/Format.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=169363&r1=169362&r2=169363&view=diff
==============================================================================
--- cfe/trunk/lib/Format/Format.cpp (original)
+++ cfe/trunk/lib/Format/Format.cpp Wed Dec  5 01:51:39 2012
@@ -547,12 +547,16 @@
 
 private:
   void determineTokenTypes() {
+    bool EqualEncountered = false;
     for (int i = 0, e = Line.Tokens.size(); i != e; ++i) {
       TokenAnnotation &Annotation = Annotations[i];
       const FormatToken &Tok = Line.Tokens[i];
 
+      if (Tok.Tok.is(tok::equal))
+        EqualEncountered = true;
+
       if (Tok.Tok.is(tok::star) || Tok.Tok.is(tok::amp))
-        Annotation.Type = determineStarAmpUsage(i);
+        Annotation.Type = determineStarAmpUsage(i, EqualEncountered);
       else if (isUnaryOperator(i))
         Annotation.Type = TokenAnnotation::TT_UnaryOperator;
       else if (isBinaryOperator(Line.Tokens[i]))
@@ -583,6 +587,7 @@
     switch (Tok.Tok.getKind()) {
     case tok::equal:
     case tok::equalequal:
+    case tok::exclaimequal:
     case tok::star:
       //case tok::amp:
     case tok::plus:
@@ -598,7 +603,8 @@
     }
   }
 
-  TokenAnnotation::TokenType determineStarAmpUsage(unsigned Index) {
+  TokenAnnotation::TokenType determineStarAmpUsage(unsigned Index,
+                                                   bool EqualEncountered) {
     if (Index == Annotations.size())
       return TokenAnnotation::TT_Unknown;
 
@@ -611,6 +617,11 @@
         Line.Tokens[Index + 1].Tok.isLiteral())
       return TokenAnnotation::TT_BinaryOperator;
 
+    // It is very unlikely that we are going to find a pointer or reference type
+    // definition on the RHS of an assignment.
+    if (EqualEncountered)
+      return TokenAnnotation::TT_BinaryOperator;
+
     return TokenAnnotation::TT_PointerOrReference;
   }
 

Modified: cfe/trunk/unittests/Format/FormatTest.cpp
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/unittests/Format/FormatTest.cpp?rev=169363&r1=169362&r2=169363&view=diff
==============================================================================
--- cfe/trunk/unittests/Format/FormatTest.cpp (original)
+++ cfe/trunk/unittests/Format/FormatTest.cpp Wed Dec  5 01:51:39 2012
@@ -333,6 +333,10 @@
   verifyFormat("f(-1, -2, -3);");
   verifyFormat("a[-1] = 5;");
   verifyFormat("int a = 5 + -2;");
+  verifyFormat("if (i == -1) {\n}");
+  verifyFormat("if (i != -1) {\n}");
+  verifyFormat("if (i > -1) {\n}");
+  verifyFormat("if (i < -1) {\n}");
 }
 
 TEST_F(FormatTest, UndestandsOverloadedOperators) {
@@ -345,10 +349,10 @@
   verifyFormat("f(*a);");
   verifyFormat("int a = b * 10;");
   verifyFormat("int a = 10 * b;");
-  // verifyFormat("int a = b * c;");
+  verifyFormat("int a = b * c;");
   verifyFormat("int a = *b;");
-  // verifyFormat("int a = *b * c;");
-  // verifyFormat("int a = b * *c;");
+  verifyFormat("int a = *b * c;");
+  verifyFormat("int a = b * *c;");
 }
 
 TEST_F(FormatTest, HandlesIncludeDirectives) {





More information about the cfe-commits mailing list