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

Daniel Jasper djasper at google.com
Thu Jan 10 03:14:08 PST 2013


Author: djasper
Date: Thu Jan 10 05:14:08 2013
New Revision: 172056

URL: http://llvm.org/viewvc/llvm-project?rev=172056&view=rev
Log:
Improve clang-format's understanding of casts.

This fixes llvm.org/PR14684.

Before: int *pa = (int *) & a;
After:  int *pa = (int *)&a;

We still don't understand all kinds of casts. I added a FIXME to
address that.

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=172056&r1=172055&r2=172056&view=diff
==============================================================================
--- cfe/trunk/lib/Format/Format.cpp (original)
+++ cfe/trunk/lib/Format/Format.cpp Thu Jan 10 05:14:08 2013
@@ -27,21 +27,22 @@
 namespace format {
 
 enum TokenType {
-  TT_Unknown,
-  TT_TemplateOpener,
-  TT_TemplateCloser,
   TT_BinaryOperator,
-  TT_UnaryOperator,
-  TT_TrailingUnaryOperator,
-  TT_OverloadedOperator,
-  TT_PointerOrReference,
+  TT_BlockComment,
+  TT_CastRParen,
   TT_ConditionalExpr,
   TT_CtorInitializerColon,
-  TT_LineComment,
-  TT_BlockComment,
   TT_DirectorySeparator,
+  TT_LineComment,
+  TT_ObjCMethodSpecifier,
+  TT_OverloadedOperator,
+  TT_PointerOrReference,
   TT_PureVirtualSpecifier,
-  TT_ObjCMethodSpecifier
+  TT_TemplateCloser,
+  TT_TemplateOpener,
+  TT_TrailingUnaryOperator,
+  TT_UnaryOperator,
+  TT_Unknown
 };
 
 enum LineType {
@@ -897,6 +898,11 @@
           Current.Type = TT_LineComment;
         else
           Current.Type = TT_BlockComment;
+      } else if (Current.is(tok::r_paren) &&
+                 (Current.Parent->Type == TT_PointerOrReference ||
+                  Current.Parent->Type == TT_TemplateCloser)) {
+        // FIXME: We need to get smarter and understand more cases of casts.
+        Current.Type = TT_CastRParen;
       }
     }
 
@@ -919,7 +925,8 @@
 
     if (PrevToken.Tok.is(tok::l_paren) || PrevToken.Tok.is(tok::l_square) ||
         PrevToken.Tok.is(tok::comma) || PrevToken.Tok.is(tok::kw_return) ||
-        PrevToken.Tok.is(tok::colon) || Tok.Parent->Type == TT_BinaryOperator)
+        PrevToken.Tok.is(tok::colon) || Tok.Parent->Type == TT_BinaryOperator ||
+        Tok.Parent->Type == TT_CastRParen)
       return TT_UnaryOperator;
 
     if (PrevToken.Tok.isLiteral() || NextToken.Tok.isLiteral() ||
@@ -1050,7 +1057,8 @@
       return false;
     if (Tok.is(tok::colon))
       return RootToken.isNot(tok::kw_case) && (!Tok.Children.empty());
-    if (Tok.Parent->Type == TT_UnaryOperator)
+    if (Tok.Parent->Type == TT_UnaryOperator ||
+        Tok.Parent->Type == TT_CastRParen)
       return false;
     if (Tok.Type == TT_UnaryOperator)
       return Tok.Parent->isNot(tok::l_paren) &&

Modified: cfe/trunk/unittests/Format/FormatTest.cpp
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/unittests/Format/FormatTest.cpp?rev=172056&r1=172055&r2=172056&view=diff
==============================================================================
--- cfe/trunk/unittests/Format/FormatTest.cpp (original)
+++ cfe/trunk/unittests/Format/FormatTest.cpp Thu Jan 10 05:14:08 2013
@@ -980,6 +980,7 @@
   verifyFormat("a * -b;");
   verifyFormat("a * ++b;");
   verifyFormat("a * --b;");
+  verifyFormat("int *pa = (int *)&a;");
 
   verifyFormat("InvalidRegions[*R] = 0;");
 
@@ -988,8 +989,8 @@
   verifyFormat("A<int *, int *> a;");
   verifyFormat("A<int **, int **> a;");
   verifyFormat("Type *A = static_cast<Type *>(P);");
-  verifyFormat("Type *A = (Type *) P;");
-  verifyFormat("Type *A = (vector<Type *, int *>) P;");
+  verifyFormat("Type *A = (Type *)P;");
+  verifyFormat("Type *A = (vector<Type *, int *>)P;");
 
   verifyFormat(
       "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"





More information about the cfe-commits mailing list