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

Daniel Jasper djasper at google.com
Tue Dec 4 05:02:32 PST 2012


Author: djasper
Date: Tue Dec  4 07:02:32 2012
New Revision: 169261

URL: http://llvm.org/viewvc/llvm-project?rev=169261&view=rev
Log:
Small fixes to unary operator recognition and handling of include
directives.

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=169261&r1=169260&r2=169261&view=diff
==============================================================================
--- cfe/trunk/lib/Format/Format.cpp (original)
+++ cfe/trunk/lib/Format/Format.cpp Tue Dec  4 07:02:32 2012
@@ -21,6 +21,8 @@
 #include "clang/Basic/SourceManager.h"
 #include "clang/Lex/Lexer.h"
 
+#include <string>
+
 namespace clang {
 namespace format {
 
@@ -486,13 +488,11 @@
         Annotation.SpaceRequiredBefore = false;
       } else if (Annotation.Type == TokenAnnotation::TT_UnaryOperator) {
         Annotation.SpaceRequiredBefore =
-            Line.Tokens[i - 1].Tok.isNot(tok::l_paren);
+            Line.Tokens[i - 1].Tok.isNot(tok::l_paren) &&
+            Line.Tokens[i - 1].Tok.isNot(tok::l_square);
       } else if (Line.Tokens[i - 1].Tok.is(tok::greater) &&
                  Line.Tokens[i].Tok.is(tok::greater)) {
-        if (Annotation.Type == TokenAnnotation::TT_TemplateOpener &&
-            Annotations[i - 1].Type == TokenAnnotation::TT_TemplateOpener)
-          Annotation.SpaceRequiredBefore = Style.SplitTemplateClosingGreater;
-        else if (Annotation.Type == TokenAnnotation::TT_TemplateCloser &&
+        if (Annotation.Type == TokenAnnotation::TT_TemplateCloser &&
                  Annotations[i - 1].Type == TokenAnnotation::TT_TemplateCloser)
           Annotation.SpaceRequiredBefore = Style.SplitTemplateClosingGreater;
         else
@@ -505,6 +505,9 @@
           Annotations[i - 1].Type == TokenAnnotation::TT_TemplateCloser &&
           Line.Tokens[i].Tok.is(tok::l_paren)) {
         Annotation.SpaceRequiredBefore = false;
+      } else if (Line.Tokens[i].Tok.is(tok::less) &&
+                 Line.Tokens[0].Tok.is(tok::hash)) {
+        Annotation.SpaceRequiredBefore = true;
       } else {
         Annotation.SpaceRequiredBefore =
             spaceRequiredBetween(Line.Tokens[i - 1].Tok, Line.Tokens[i].Tok);
@@ -533,7 +536,7 @@
 
       if (Tok.Tok.is(tok::star) || Tok.Tok.is(tok::amp))
         Annotation.Type = determineStarAmpUsage(i);
-      else if (Tok.Tok.is(tok::minus) && Line.Tokens[i - 1].Tok.is(tok::equal))
+      else if (isUnaryOperator(i))
         Annotation.Type = TokenAnnotation::TT_UnaryOperator;
       else if (isBinaryOperator(Line.Tokens[i]))
         Annotation.Type = TokenAnnotation::TT_BinaryOperator;
@@ -548,6 +551,17 @@
     }
   }
 
+  bool isUnaryOperator(unsigned Index) {
+    const Token &Tok = Line.Tokens[Index].Tok;
+    if (Tok.isNot(tok::minus) && Tok.isNot(tok::plus))
+      return false;
+    const Token &PreviousTok = Line.Tokens[Index - 1].Tok;
+    if (PreviousTok.is(tok::equal) || PreviousTok.is(tok::l_paren) ||
+        PreviousTok.is(tok::comma) || PreviousTok.is(tok::l_square))
+      return true;
+    return Annotations[Index - 1].Type == TokenAnnotation::TT_BinaryOperator;
+  }
+
   bool isBinaryOperator(const FormatToken &Tok) {
     switch (Tok.Tok.getKind()) {
     case tok::equal:

Modified: cfe/trunk/unittests/Format/FormatTest.cpp
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/unittests/Format/FormatTest.cpp?rev=169261&r1=169260&r2=169261&view=diff
==============================================================================
--- cfe/trunk/unittests/Format/FormatTest.cpp (original)
+++ cfe/trunk/unittests/Format/FormatTest.cpp Tue Dec  4 07:02:32 2012
@@ -315,6 +315,9 @@
 
 TEST_F(FormatTest, UndestandsUnaryOperators) {
   verifyFormat("int a = -2;");
+  verifyFormat("f(-1, -2, -3);");
+  verifyFormat("a[-1] = 5;");
+  verifyFormat("int a = 5 + -2;");
 }
 
 TEST_F(FormatTest, UndestandsOverloadedOperators) {
@@ -333,6 +336,13 @@
   // verifyFormat("int a = b * *c;");
 }
 
+TEST_F(FormatTest, HandlesIncludeDirectives) {
+  EXPECT_EQ("#include <string>\n", format("#include <string>\n"));
+  EXPECT_EQ("#include \"a/b/string\"\n", format("#include \"a/b/string\"\n"));
+  EXPECT_EQ("#include \"string.h\"\n", format("#include \"string.h\"\n"));
+  EXPECT_EQ("#include \"string.h\"\n", format("#include \"string.h\"\n"));
+}
+
 //TEST_F(FormatTest, IncorrectDerivedClass) {
 //  verifyFormat("public B {\n"
 //               "};");





More information about the cfe-commits mailing list