r253872 - clang-format: Fix incorrect cast detection.
Daniel Jasper via cfe-commits
cfe-commits at lists.llvm.org
Mon Nov 23 07:55:51 PST 2015
Author: djasper
Date: Mon Nov 23 09:55:50 2015
New Revision: 253872
URL: http://llvm.org/viewvc/llvm-project?rev=253872&view=rev
Log:
clang-format: Fix incorrect cast detection.
Before:
bool b = f(g<int>)&&c;
After:
bool b = f(g<int>) && c;
Modified:
cfe/trunk/lib/Format/TokenAnnotator.cpp
cfe/trunk/unittests/Format/FormatTest.cpp
Modified: cfe/trunk/lib/Format/TokenAnnotator.cpp
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/lib/Format/TokenAnnotator.cpp?rev=253872&r1=253871&r2=253872&view=diff
==============================================================================
--- cfe/trunk/lib/Format/TokenAnnotator.cpp (original)
+++ cfe/trunk/lib/Format/TokenAnnotator.cpp Mon Nov 23 09:55:50 2015
@@ -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 {
Modified: cfe/trunk/unittests/Format/FormatTest.cpp
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/unittests/Format/FormatTest.cpp?rev=253872&r1=253871&r2=253872&view=diff
==============================================================================
--- cfe/trunk/unittests/Format/FormatTest.cpp (original)
+++ cfe/trunk/unittests/Format/FormatTest.cpp Mon Nov 23 09:55:50 2015
@@ -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;");
More information about the cfe-commits
mailing list