]> granicus.if.org Git - clang/commitdiff
clang-format: Improve c-style cast detection.
authorDaniel Jasper <djasper@google.com>
Mon, 15 Jul 2013 15:04:42 +0000 (15:04 +0000)
committerDaniel Jasper <djasper@google.com>
Mon, 15 Jul 2013 15:04:42 +0000 (15:04 +0000)
Before:
  #define x ((int) - 1)
  #define p(q) ((int *) & q)
After:
  #define x ((int)-1)
  #define p(q) ((int *)&q)

git-svn-id: https://llvm.org/svn/llvm-project/cfe/trunk@186324 91177308-0d34-0410-b5e6-96231b3b80d8

lib/Format/TokenAnnotator.cpp
unittests/Format/FormatTest.cpp

index 3bed432b19cb94148c97643c244d224bfaad2771..1d5904b3186709be1556a9328f924bfc3748b228 100644 (file)
@@ -644,7 +644,8 @@ private:
             LeftOfParens &&
             LeftOfParens->isOneOf(tok::kw_sizeof, tok::kw_alignof);
         if (ParensAreType && !ParensCouldEndDecl && !IsSizeOfOrAlignOf &&
-            Contexts.back().IsExpression)
+            (Contexts.back().IsExpression ||
+             (Current.Next && Current.Next->isBinaryOperator())))
           IsCast = true;
         if (Current.Next && Current.Next->isNot(tok::string_literal) &&
             (Current.Next->Tok.isLiteral() ||
@@ -1084,7 +1085,7 @@ unsigned TokenAnnotator::splitPenalty(const AnnotatedLine &Line,
           (Content.back() == ':' || Content.back() == '='))
         return 25;
     }
-    return 1;  // Breaking at a << is really cheap.
+    return 1; // Breaking at a << is really cheap.
   }
   if (Left.Type == TT_ConditionalExpr)
     return prec::Conditional;
index 01dd38e3c30519e30de2ff5f757328d32cb2ee8a..c8827c8dec63511877320ba45267eaccf2fc1241 100644 (file)
@@ -3565,6 +3565,8 @@ TEST_F(FormatTest, FormatsCasts) {
   verifyFormat("my_int a = (my_int)2.0f;");
   verifyFormat("my_int a = (my_int)sizeof(int);");
   verifyFormat("return (my_int)aaa;");
+  verifyFormat("#define x ((int)-1)");
+  verifyFormat("#define p(q) ((int *)&q)");
 
   // FIXME: Without type knowledge, this can still fall apart miserably.
   verifyFormat("void f() { my_int a = (my_int) * b; }");