diff options
author | Daniel Jasper <djasper@google.com> | 2015-11-23 15:55:50 +0000 |
---|---|---|
committer | Daniel Jasper <djasper@google.com> | 2015-11-23 15:55:50 +0000 |
commit | 9bb3001d53c9a844a6ac00d467169ba2e006844e (patch) | |
tree | 0a691b2e2ef7b39ca2a07f0c08f68741e63f49c9 | |
parent | 253dad2323b4a9288d57f8d1fd63743fd979bde4 (diff) | |
download | bcm5719-llvm-9bb3001d53c9a844a6ac00d467169ba2e006844e.tar.gz bcm5719-llvm-9bb3001d53c9a844a6ac00d467169ba2e006844e.zip |
clang-format: Fix incorrect cast detection.
Before:
bool b = f(g<int>)&&c;
After:
bool b = f(g<int>) && c;
llvm-svn: 253872
-rw-r--r-- | clang/lib/Format/TokenAnnotator.cpp | 32 | ||||
-rw-r--r-- | clang/unittests/Format/FormatTest.cpp | 1 |
2 files changed, 16 insertions, 17 deletions
diff --git a/clang/lib/Format/TokenAnnotator.cpp b/clang/lib/Format/TokenAnnotator.cpp index 5aa4dadb551..5a603b30ce1 100644 --- a/clang/lib/Format/TokenAnnotator.cpp +++ b/clang/lib/Format/TokenAnnotator.cpp @@ -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 { diff --git a/clang/unittests/Format/FormatTest.cpp b/clang/unittests/Format/FormatTest.cpp index 66b3add3026..02f329acfe5 100644 --- a/clang/unittests/Format/FormatTest.cpp +++ b/clang/unittests/Format/FormatTest.cpp @@ -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;"); |