summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorDaniel Jasper <djasper@google.com>2015-12-28 07:44:25 +0000
committerDaniel Jasper <djasper@google.com>2015-12-28 07:44:25 +0000
commita85c331e8f9c5f63fa37c2334e3ea40423c20d8a (patch)
tree482a553a4dded751bd66e07f749822105870fc1d
parent6402bcad714d776a9ee2e6d427e0dfb363915032 (diff)
downloadbcm5719-llvm-a85c331e8f9c5f63fa37c2334e3ea40423c20d8a.tar.gz
bcm5719-llvm-a85c331e8f9c5f63fa37c2334e3ea40423c20d8a.zip
clang-format: Fix incorrect function type detection.
Before: int x = f (&h)(); After: int x = f(&h)(); llvm-svn: 256488
-rw-r--r--clang/lib/Format/TokenAnnotator.cpp23
-rw-r--r--clang/unittests/Format/FormatTest.cpp1
2 files changed, 9 insertions, 15 deletions
diff --git a/clang/lib/Format/TokenAnnotator.cpp b/clang/lib/Format/TokenAnnotator.cpp
index 7406cfe95b6..14e94078a6e 100644
--- a/clang/lib/Format/TokenAnnotator.cpp
+++ b/clang/lib/Format/TokenAnnotator.cpp
@@ -148,6 +148,10 @@ private:
} else if (Left->Previous && Left->Previous->MatchingParen &&
Left->Previous->MatchingParen->is(TT_ObjCBlockLParen)) {
Contexts.back().IsExpression = false;
+ } else if (!Line.MustBeDeclaration && !Line.InPPDirective) {
+ bool IsForOrCatch =
+ Left->Previous && Left->Previous->isOneOf(tok::kw_for, tok::kw_catch);
+ Contexts.back().IsExpression = !IsForOrCatch;
}
if (StartsObjCMethodExpr) {
@@ -155,7 +159,8 @@ private:
Left->Type = TT_ObjCMethodExpr;
}
- bool MightBeFunctionType = CurrentToken->isOneOf(tok::star, tok::amp);
+ bool MightBeFunctionType = CurrentToken->isOneOf(tok::star, tok::amp) &&
+ !Contexts[Contexts.size() - 2].IsExpression;
bool HasMultipleLines = false;
bool HasMultipleParametersOnALine = false;
bool MightBeObjCForRangeLoop =
@@ -189,7 +194,7 @@ private:
if (MightBeFunctionType && CurrentToken->Next &&
(CurrentToken->Next->is(tok::l_paren) ||
(CurrentToken->Next->is(tok::l_square) &&
- !Contexts.back().IsExpression)))
+ Line.MustBeDeclaration)))
Left->Type = TT_FunctionTypeLParen;
Left->MatchingParen = CurrentToken;
CurrentToken->MatchingParen = Left;
@@ -861,17 +866,6 @@ private:
Contexts.back().IsExpression = false;
} else if (Current.is(TT_LambdaArrow) || Current.is(Keywords.kw_assert)) {
Contexts.back().IsExpression = Style.Language == FormatStyle::LK_Java;
- } else if (Current.is(tok::l_paren) && !Line.MustBeDeclaration &&
- !Line.InPPDirective &&
- (!Current.Previous ||
- Current.Previous->isNot(tok::kw_decltype))) {
- bool ParametersOfFunctionType =
- Current.Previous && Current.Previous->is(tok::r_paren) &&
- Current.Previous->MatchingParen &&
- Current.Previous->MatchingParen->is(TT_FunctionTypeLParen);
- bool IsForOrCatch = Current.Previous &&
- Current.Previous->isOneOf(tok::kw_for, tok::kw_catch);
- Contexts.back().IsExpression = !ParametersOfFunctionType && !IsForOrCatch;
} else if (Current.isOneOf(tok::r_paren, tok::greater, tok::comma)) {
for (FormatToken *Previous = Current.Previous;
Previous && Previous->isOneOf(tok::star, tok::amp);
@@ -1112,8 +1106,7 @@ private:
Tok.Previous->isSimpleTypeSpecifier();
bool ParensCouldEndDecl =
Tok.Next->isOneOf(tok::equal, tok::semi, tok::l_brace, tok::greater);
- if (ParensAreType && !ParensCouldEndDecl &&
- (Contexts.size() > 1 && Contexts[Contexts.size() - 2].IsExpression))
+ if (ParensAreType && !ParensCouldEndDecl)
return true;
// At this point, we heuristically assume that there are no casts at the
diff --git a/clang/unittests/Format/FormatTest.cpp b/clang/unittests/Format/FormatTest.cpp
index 9d7521fee10..6a0506d8973 100644
--- a/clang/unittests/Format/FormatTest.cpp
+++ b/clang/unittests/Format/FormatTest.cpp
@@ -5928,6 +5928,7 @@ TEST_F(FormatTest, FormatsFunctionTypes) {
verifyFormat("#define DEREF_AND_CALL_F(x) f(*x)");
verifyFormat("some_var = function(*some_pointer_var)[0];");
verifyFormat("void f() { function(*some_pointer_var)[0] = 10; }");
+ verifyFormat("int x = f(&h)();");
}
TEST_F(FormatTest, FormatsPointersToArrayTypes) {
OpenPOWER on IntegriCloud