[cfe-commits] r172731 - in /cfe/trunk: lib/Format/Format.cpp unittests/Format/FormatTest.cpp
Nico Weber
nicolasweber at gmx.de
Thu Jan 17 09:17:19 PST 2013
Author: nico
Date: Thu Jan 17 11:17:19 2013
New Revision: 172731
URL: http://llvm.org/viewvc/llvm-project?rev=172731&view=rev
Log:
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())
Modified:
cfe/trunk/lib/Format/Format.cpp
cfe/trunk/unittests/Format/FormatTest.cpp
Modified: cfe/trunk/lib/Format/Format.cpp
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/lib/Format/Format.cpp?rev=172731&r1=172730&r2=172731&view=diff
==============================================================================
--- cfe/trunk/lib/Format/Format.cpp (original)
+++ cfe/trunk/lib/Format/Format.cpp Thu Jan 17 11:17:19 2013
@@ -771,13 +771,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;
@@ -896,6 +911,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: {
if (!parseParens())
return false;
Modified: cfe/trunk/unittests/Format/FormatTest.cpp
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/unittests/Format/FormatTest.cpp?rev=172731&r1=172730&r2=172731&view=diff
==============================================================================
--- cfe/trunk/unittests/Format/FormatTest.cpp (original)
+++ cfe/trunk/unittests/Format/FormatTest.cpp Thu Jan 17 11:17:19 2013
@@ -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) {
More information about the cfe-commits
mailing list