Formatter: Get bit tests in ifs right.
It's generally not possible to know if 'a' '*' 'b' is a multiplication
expression or a variable declaration with a purely lexer-based approach. The
formatter currently uses a heuristic that classifies this token sequence as a
multiplication in rhs contexts (after '=' or 'return') and as a declaration
else.
Because of this, it gets bit tests in ifs, such as "if (a & b)" wrong. However,
declarations in ifs always have to be followed by '=', so this patch changes
the formatter to classify '&' as an operator if it's at the start of an if
statement.
Before:
if (a& b)
if (int* b = f())
Now:
if (a & b)
if (int* b = f())
llvm-svn: 172731
diff --git a/clang/unittests/Format/FormatTest.cpp b/clang/unittests/Format/FormatTest.cpp
index 28d9fbe..7b57e85 100644
--- a/clang/unittests/Format/FormatTest.cpp
+++ b/clang/unittests/Format/FormatTest.cpp
@@ -1235,6 +1235,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) {