Small tweaks to automatic formatting.
authorDaniel Jasper <djasper@google.com>
Wed, 5 Dec 2012 07:51:39 +0000 (07:51 +0000)
committerDaniel Jasper <djasper@google.com>
Wed, 5 Dec 2012 07:51:39 +0000 (07:51 +0000)
Recognize '!=' as a binary operator and assume that there are no
type definitions on the RHS of an assignment.

llvm-svn: 169363

clang/lib/Format/Format.cpp
clang/unittests/Format/FormatTest.cpp

index d928aa9..22805f6 100644 (file)
@@ -547,12 +547,16 @@ public:
 
 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 @@ private:
     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 @@ private:
     }
   }
 
-  TokenAnnotation::TokenType determineStarAmpUsage(unsigned Index) {
+  TokenAnnotation::TokenType determineStarAmpUsage(unsigned Index,
+                                                   bool EqualEncountered) {
     if (Index == Annotations.size())
       return TokenAnnotation::TT_Unknown;
 
@@ -611,6 +617,11 @@ private:
         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;
   }
 
index 745ecec..be43633 100644 (file)
@@ -333,6 +333,10 @@ TEST_F(FormatTest, UndestandsUnaryOperators) {
   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 @@ TEST_F(FormatTest, UnderstandsUsesOfStar) {
   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) {