[cfe-commits] [PATCH] Formatter: Get bit tests in ifs right, such as "if (a & b)"

Nico Weber thakis at chromium.org
Wed Jan 16 21:09:11 PST 2013


  (comment fix for real, hopefully)

Hi djasper,

http://llvm-reviews.chandlerc.com/D306

CHANGE SINCE LAST DIFF
  http://llvm-reviews.chandlerc.com/D306?vs=716&id=717#toc

Files:
  lib/Format/Format.cpp
  unittests/Format/FormatTest.cpp

Index: lib/Format/Format.cpp
===================================================================
--- lib/Format/Format.cpp
+++ lib/Format/Format.cpp
@@ -766,13 +766,28 @@
       return false;
     }
 
-    bool parseParens() {
+    bool parseParens(bool LookForDecls = false) {
       if (CurrentToken == NULL)
         return false;
       AnnotatedToken *Left = CurrentToken->Parent;
       if (CurrentToken->is(tok::caret))
         Left->Type = TT_ObjCBlockLParen;
       while (CurrentToken != NULL) {
+        // LookForDecls is set when "if (" has been seen. Check for
+        // 'identifier' '*' 'identifier' followed by not '=' -- this
+        // '*' has to be a binary operator but determineStarAmpUsage() will
+        // categorize it as an unary operator, so set the right type here.
+        if (LookForDecls && !CurrentToken->Children.empty()) {
+          AnnotatedToken &Prev = *CurrentToken->Parent;
+          AnnotatedToken &Next = CurrentToken->Children[0];
+          if (Prev.Parent->is(tok::identifier) &&
+              (Prev.is(tok::star) || Prev.is(tok::amp)) &&
+              CurrentToken->is(tok::identifier) && Next.isNot(tok::equal)) {
+            Prev.Type = TT_BinaryOperator;
+            LookForDecls = false;
+          }
+        }
+
         if (CurrentToken->is(tok::r_paren)) {
           Left->MatchingParen = CurrentToken;
           CurrentToken->MatchingParen = Left;
@@ -891,6 +906,14 @@
         if (ColonIsObjCMethodExpr)
           Tok->Type = TT_ObjCMethodExpr;
         break;
+      case tok::kw_if:
+      case tok::kw_while:
+        if (CurrentToken->is(tok::l_paren)) {
+          next();
+          if (!parseParens(/*LookForDecls=*/true))
+            return false;
+        }
+        break;
       case tok::l_paren: {
         bool ParensWereObjCReturnType = Tok->Parent && Tok->Parent->Type ==
                                         TT_ObjCMethodSpecifier;
Index: unittests/Format/FormatTest.cpp
===================================================================
--- unittests/Format/FormatTest.cpp
+++ unittests/Format/FormatTest.cpp
@@ -1193,6 +1193,14 @@
       "  *A, // Operator detection might be confused by the '{'\n"
       "  *BB // Operator detection might be confused by previous comment\n"
       "};");
+
+  verifyFormat("if (int *a = &b)");
+  verifyFormat("if (int &a = *b)");
+  verifyFormat("if (a & b[i])");
+  verifyFormat("if (a::b::c::d & b[i])");
+  verifyFormat("if (*b[i])");
+  verifyFormat("if (int *a = (&b))");
+  verifyFormat("while (int *a = &b)");
 }
 
 TEST_F(FormatTest, FormatsCasts) {
-------------- next part --------------
A non-text attachment was scrubbed...
Name: D306.3.patch
Type: text/x-patch
Size: 2589 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/cfe-commits/attachments/20130116/cbcf0842/attachment.bin>


More information about the cfe-commits mailing list