clang-format: Fix incorrect cast detection.

Before:
  bool b = f(g<int>)&&c;

After:
  bool b = f(g<int>) && c;

llvm-svn: 253872
This commit is contained in:
Daniel Jasper 2015-11-23 15:55:50 +00:00
parent 253dad2323
commit 9bb3001d53
2 changed files with 16 additions and 17 deletions

View File

@ -1060,13 +1060,19 @@ private:
FormatToken *LeftOfParens = nullptr;
if (Tok.MatchingParen)
LeftOfParens = Tok.MatchingParen->getPreviousNonComment();
if (LeftOfParens && LeftOfParens->is(tok::r_paren) &&
LeftOfParens->MatchingParen)
LeftOfParens = LeftOfParens->MatchingParen->Previous;
if (LeftOfParens && LeftOfParens->is(tok::r_square) &&
LeftOfParens->MatchingParen &&
LeftOfParens->MatchingParen->is(TT_LambdaLSquare))
return false;
if (LeftOfParens) {
if (LeftOfParens->Tok.getIdentifierInfo() &&
!LeftOfParens->isOneOf(Keywords.kw_in, tok::kw_return, tok::kw_case,
tok::kw_delete))
return false;
if (LeftOfParens->isOneOf(tok::kw_sizeof, tok::kw_alignof, tok::at,
tok::r_square, TT_OverloadedOperator,
TT_TemplateCloser))
return false;
if (LeftOfParens->is(tok::r_paren) && LeftOfParens->MatchingParen)
LeftOfParens = LeftOfParens->MatchingParen->Previous;
}
if (Tok.Next) {
if (Tok.Next->is(tok::question))
return false;
@ -1085,22 +1091,14 @@ private:
bool ParensCouldEndDecl =
Tok.Next &&
Tok.Next->isOneOf(tok::equal, tok::semi, tok::l_brace, tok::greater);
bool IsSizeOfOrAlignOf =
LeftOfParens && LeftOfParens->isOneOf(tok::kw_sizeof, tok::kw_alignof);
if (ParensAreType && !ParensCouldEndDecl && !IsSizeOfOrAlignOf &&
if (ParensAreType && !ParensCouldEndDecl &&
(Contexts.size() > 1 && Contexts[Contexts.size() - 2].IsExpression))
IsCast = true;
else if (Tok.Next && Tok.Next->isNot(tok::string_literal) &&
(Tok.Next->Tok.isLiteral() ||
Tok.Next->isOneOf(tok::kw_sizeof, tok::kw_alignof)))
IsCast = true;
// If there is an identifier after the (), it is likely a cast, unless
// there is also an identifier before the ().
else if (LeftOfParens && Tok.Next &&
(LeftOfParens->Tok.getIdentifierInfo() == nullptr ||
LeftOfParens->isOneOf(tok::kw_return, tok::kw_case)) &&
!LeftOfParens->isOneOf(TT_OverloadedOperator, tok::at,
TT_TemplateCloser)) {
else if (LeftOfParens && Tok.Next) {
if (Tok.Next->isOneOf(tok::identifier, tok::numeric_constant)) {
IsCast = true;
} else {

View File

@ -5792,6 +5792,7 @@ TEST_F(FormatTest, FormatsCasts) {
verifyFormat("virtual void foo(int *a, char *) const;");
verifyFormat("int a = sizeof(int *) + b;");
verifyFormat("int a = alignof(int *) + b;", getGoogleStyle());
verifyFormat("bool b = f(g<int>) && c;");
verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa *foo = (aaaaaaaaaaaaaaaaa *)\n"
" bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb;");