r175029 - Formatter: Refactor the cast detection code to be a bit more readable.

Nico Weber nicolasweber at gmx.de
Tue Feb 12 20:13:13 PST 2013


Author: nico
Date: Tue Feb 12 22:13:13 2013
New Revision: 175029

URL: http://llvm.org/viewvc/llvm-project?rev=175029&view=rev
Log:
Formatter: Refactor the cast detection code to be a bit more readable.

No functionality change. Also add another cast test.


Modified:
    cfe/trunk/lib/Format/TokenAnnotator.cpp
    cfe/trunk/unittests/Format/FormatTest.cpp

Modified: cfe/trunk/lib/Format/TokenAnnotator.cpp
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/lib/Format/TokenAnnotator.cpp?rev=175029&r1=175028&r2=175029&view=diff
==============================================================================
--- cfe/trunk/lib/Format/TokenAnnotator.cpp (original)
+++ cfe/trunk/lib/Format/TokenAnnotator.cpp Tue Feb 12 22:13:13 2013
@@ -578,15 +578,16 @@ private:
           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) &&
-                 (Current.Children.empty() ||
-                  (Current.Children[0].isNot(tok::equal) &&
-                   Current.Children[0].isNot(tok::semi) &&
-                   Current.Children[0].isNot(tok::l_brace)))) {
-        // FIXME: We need to get smarter and understand more cases of casts.
-        Current.Type = TT_CastRParen;
+      } else if (Current.is(tok::r_paren)) {
+        bool ParensNotExpr = Current.Parent->Type == TT_PointerOrReference ||
+                             Current.Parent->Type == TT_TemplateCloser;
+        bool ParensCouldEndDecl =
+            !Current.Children.empty() && (Current.Children[0].is(tok::equal) ||
+                                          Current.Children[0].is(tok::semi) ||
+                                          Current.Children[0].is(tok::l_brace));
+        if (ParensNotExpr && !ParensCouldEndDecl)
+          // FIXME: We need to get smarter and understand more cases of casts.
+          Current.Type = TT_CastRParen;
       } else if (Current.is(tok::at) && Current.Children.size()) {
         switch (Current.Children[0].FormatTok.Tok.getObjCKeywordID()) {
         case tok::objc_interface:

Modified: cfe/trunk/unittests/Format/FormatTest.cpp
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/unittests/Format/FormatTest.cpp?rev=175029&r1=175028&r2=175029&view=diff
==============================================================================
--- cfe/trunk/unittests/Format/FormatTest.cpp (original)
+++ cfe/trunk/unittests/Format/FormatTest.cpp Tue Feb 12 22:13:13 2013
@@ -1723,6 +1723,7 @@ TEST_F(FormatTest, FormatsCasts) {
 
   // These are not casts.
   verifyFormat("void f(int *) {}");
+  verifyFormat("f(foo)[4];");
   verifyFormat("void f(int *);");
   verifyFormat("void f(int *) = 0;");
   verifyFormat("void f(SmallVector<int>) {}");





More information about the cfe-commits mailing list