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 3bed432..1d5904b 100644
--- a/lib/Format/TokenAnnotator.cpp
+++ b/lib/Format/TokenAnnotator.cpp
@@ -644,7 +644,8 @@
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 @@
(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 01dd38e..c8827c8 100644
--- a/unittests/Format/FormatTest.cpp
+++ b/unittests/Format/FormatTest.cpp
@@ -3565,6 +3565,8 @@
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; }");