Skip to content

Commit

Permalink
[clang-format/ObjC] Fix counting selector name parts for ObjC
Browse files Browse the repository at this point in the history
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

llvm-svn: 336518
  • Loading branch information
Jacek Olesiak committed Jul 9, 2018
1 parent b8145ec commit 27a5579
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 13 deletions.
5 changes: 3 additions & 2 deletions clang/lib/Format/FormatToken.h
Expand Up @@ -243,8 +243,9 @@ struct FormatToken {
/// e.g. because several of them are block-type.
unsigned LongestObjCSelectorName = 0;

/// How many parts ObjC selector have (i.e. how many parameters method
/// has).
/// If this is the first ObjC selector name in an ObjC method
/// definition or call, this contains the number of parts that the whole
/// selector consist of.
unsigned ObjCSelectorNameParts = 0;

/// Stores the number of required fake parentheses and the
Expand Down
30 changes: 19 additions & 11 deletions clang/lib/Format/TokenAnnotator.cpp
Expand Up @@ -515,11 +515,23 @@ class AnnotatingParser {
}
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;
}
Expand All @@ -539,11 +551,6 @@ class AnnotatingParser {
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.
Expand Down Expand Up @@ -617,12 +624,12 @@ class AnnotatingParser {
}

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));
Expand Down Expand Up @@ -718,6 +725,7 @@ class AnnotatingParser {
Contexts.back().LongestObjCSelectorName)
Contexts.back().LongestObjCSelectorName =
Tok->Previous->ColumnWidth;
++Contexts.back().FirstObjCSelectorName->ObjCSelectorNameParts;
}
} else if (Contexts.back().ColonIsForRangeExpr) {
Tok->Type = TT_RangeBasedForLoopColon;
Expand Down

0 comments on commit 27a5579

Please sign in to comment.