summaryrefslogtreecommitdiffstats
path: root/lib/Format/TokenAnnotator.cpp
diff options
context:
space:
mode:
authorJacek Olesiak <jolesiak@google.com>2018-07-09 05:58:51 +0000
committerJacek Olesiak <jolesiak@google.com>2018-07-09 05:58:51 +0000
commitd3e19c829cf4adba7dacf8a93791104797626e3b (patch)
tree6aa7ed26b74ed77315fc70b96db74647a09ad492 /lib/Format/TokenAnnotator.cpp
parent1d7e46ba487d09f5a899ede8466831e1fdea8a6c (diff)
[clang-format/ObjC] Fix counting selector name parts for ObjC
Summary: Counts selector parts also for method declarations and counts correctly for methods without arguments. This is an internal change and doesn't influence formatting on its own (at the current state). Its lack would be visible after applying D48719. Reviewers: benhamilton, klimek Reviewed By: benhamilton Subscribers: acoomans, cfe-commits Differential Revision: https://reviews.llvm.org/D48716 git-svn-id: https://llvm.org/svn/llvm-project/cfe/trunk@336518 91177308-0d34-0410-b5e6-96231b3b80d8
Diffstat (limited to 'lib/Format/TokenAnnotator.cpp')
-rw-r--r--lib/Format/TokenAnnotator.cpp30
1 files changed, 19 insertions, 11 deletions
diff --git a/lib/Format/TokenAnnotator.cpp b/lib/Format/TokenAnnotator.cpp
index ec8d351725..b6127bc8d4 100644
--- a/lib/Format/TokenAnnotator.cpp
+++ b/lib/Format/TokenAnnotator.cpp
@@ -515,11 +515,23 @@ private:
}
Left->MatchingParen = CurrentToken;
CurrentToken->MatchingParen = Left;
+ // FirstObjCSelectorName is set when a colon is found. This does
+ // not work, however, when the method has no parameters.
+ // Here, we set FirstObjCSelectorName when the end of the method call is
+ // reached, in case it was not set already.
+ if (!Contexts.back().FirstObjCSelectorName) {
+ FormatToken* Previous = CurrentToken->getPreviousNonComment();
+ if (Previous && Previous->is(TT_SelectorName)) {
+ Previous->ObjCSelectorNameParts = 1;
+ Contexts.back().FirstObjCSelectorName = Previous;
+ }
+ } else {
+ Left->ParameterCount =
+ Contexts.back().FirstObjCSelectorName->ObjCSelectorNameParts;
+ }
if (Contexts.back().FirstObjCSelectorName) {
Contexts.back().FirstObjCSelectorName->LongestObjCSelectorName =
Contexts.back().LongestObjCSelectorName;
- Contexts.back().FirstObjCSelectorName->ObjCSelectorNameParts =
- Left->ParameterCount;
if (Left->BlockParameterCount > 1)
Contexts.back().FirstObjCSelectorName->LongestObjCSelectorName = 0;
}
@@ -539,11 +551,6 @@ private:
TT_DesignatedInitializerLSquare)) {
Left->Type = TT_ObjCMethodExpr;
StartsObjCMethodExpr = true;
- // ParameterCount might have been set to 1 before expression was
- // recognized as ObjCMethodExpr (as '1 + number of commas' formula is
- // used for other expression types). Parameter counter has to be,
- // therefore, reset to 0.
- Left->ParameterCount = 0;
Contexts.back().ColonIsObjCMethodExpr = true;
if (Parent && Parent->is(tok::r_paren))
// FIXME(bug 36976): ObjC return types shouldn't use TT_CastRParen.
@@ -617,12 +624,12 @@ private:
}
void updateParameterCount(FormatToken *Left, FormatToken *Current) {
+ // For ObjC methods, the number of parameters is calculated differently as
+ // method declarations have a different structure (the parameters are not
+ // inside a bracket scope).
if (Current->is(tok::l_brace) && Current->BlockKind == BK_Block)
++Left->BlockParameterCount;
- if (Left->Type == TT_ObjCMethodExpr) {
- if (Current->is(tok::colon))
- ++Left->ParameterCount;
- } else if (Current->is(tok::comma)) {
+ if (Current->is(tok::comma)) {
++Left->ParameterCount;
if (!Left->Role)
Left->Role.reset(new CommaSeparatedList(Style));
@@ -718,6 +725,7 @@ private:
Contexts.back().LongestObjCSelectorName)
Contexts.back().LongestObjCSelectorName =
Tok->Previous->ColumnWidth;
+ ++Contexts.back().FirstObjCSelectorName->ObjCSelectorNameParts;
}
} else if (Contexts.back().ColonIsForRangeExpr) {
Tok->Type = TT_RangeBasedForLoopColon;