From: Daniel Jasper Date: Mon, 15 Jul 2013 15:04:42 +0000 (+0000) Subject: clang-format: Improve c-style cast detection. X-Git-Url: https://granicus.if.org/sourcecode?a=commitdiff_plain;h=0c368787d9d1f92a3408b71b3f074a06edaa6bde;p=clang clang-format: Improve c-style cast detection. 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 --- diff --git a/lib/Format/TokenAnnotator.cpp b/lib/Format/TokenAnnotator.cpp index 3bed432b19..1d5904b318 100644 --- a/lib/Format/TokenAnnotator.cpp +++ b/lib/Format/TokenAnnotator.cpp @@ -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; diff --git a/unittests/Format/FormatTest.cpp b/unittests/Format/FormatTest.cpp index 01dd38e3c3..c8827c8dec 100644 --- a/unittests/Format/FormatTest.cpp +++ b/unittests/Format/FormatTest.cpp @@ -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; }");