Skip to content

Commit 27a5579

Browse files
committedJul 9, 2018
[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 llvm-svn: 336518
1 parent b8145ec commit 27a5579

File tree

2 files changed

+22
-13
lines changed

2 files changed

+22
-13
lines changed
 

‎clang/lib/Format/FormatToken.h

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -243,8 +243,9 @@ struct FormatToken {
243243
/// e.g. because several of them are block-type.
244244
unsigned LongestObjCSelectorName = 0;
245245

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

250251
/// Stores the number of required fake parentheses and the

‎clang/lib/Format/TokenAnnotator.cpp

Lines changed: 19 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -515,11 +515,23 @@ class AnnotatingParser {
515515
}
516516
Left->MatchingParen = CurrentToken;
517517
CurrentToken->MatchingParen = Left;
518+
// FirstObjCSelectorName is set when a colon is found. This does
519+
// not work, however, when the method has no parameters.
520+
// Here, we set FirstObjCSelectorName when the end of the method call is
521+
// reached, in case it was not set already.
522+
if (!Contexts.back().FirstObjCSelectorName) {
523+
FormatToken* Previous = CurrentToken->getPreviousNonComment();
524+
if (Previous && Previous->is(TT_SelectorName)) {
525+
Previous->ObjCSelectorNameParts = 1;
526+
Contexts.back().FirstObjCSelectorName = Previous;
527+
}
528+
} else {
529+
Left->ParameterCount =
530+
Contexts.back().FirstObjCSelectorName->ObjCSelectorNameParts;
531+
}
518532
if (Contexts.back().FirstObjCSelectorName) {
519533
Contexts.back().FirstObjCSelectorName->LongestObjCSelectorName =
520534
Contexts.back().LongestObjCSelectorName;
521-
Contexts.back().FirstObjCSelectorName->ObjCSelectorNameParts =
522-
Left->ParameterCount;
523535
if (Left->BlockParameterCount > 1)
524536
Contexts.back().FirstObjCSelectorName->LongestObjCSelectorName = 0;
525537
}
@@ -539,11 +551,6 @@ class AnnotatingParser {
539551
TT_DesignatedInitializerLSquare)) {
540552
Left->Type = TT_ObjCMethodExpr;
541553
StartsObjCMethodExpr = true;
542-
// ParameterCount might have been set to 1 before expression was
543-
// recognized as ObjCMethodExpr (as '1 + number of commas' formula is
544-
// used for other expression types). Parameter counter has to be,
545-
// therefore, reset to 0.
546-
Left->ParameterCount = 0;
547554
Contexts.back().ColonIsObjCMethodExpr = true;
548555
if (Parent && Parent->is(tok::r_paren))
549556
// FIXME(bug 36976): ObjC return types shouldn't use TT_CastRParen.
@@ -617,12 +624,12 @@ class AnnotatingParser {
617624
}
618625

619626
void updateParameterCount(FormatToken *Left, FormatToken *Current) {
627+
// For ObjC methods, the number of parameters is calculated differently as
628+
// method declarations have a different structure (the parameters are not
629+
// inside a bracket scope).
620630
if (Current->is(tok::l_brace) && Current->BlockKind == BK_Block)
621631
++Left->BlockParameterCount;
622-
if (Left->Type == TT_ObjCMethodExpr) {
623-
if (Current->is(tok::colon))
624-
++Left->ParameterCount;
625-
} else if (Current->is(tok::comma)) {
632+
if (Current->is(tok::comma)) {
626633
++Left->ParameterCount;
627634
if (!Left->Role)
628635
Left->Role.reset(new CommaSeparatedList(Style));
@@ -718,6 +725,7 @@ class AnnotatingParser {
718725
Contexts.back().LongestObjCSelectorName)
719726
Contexts.back().LongestObjCSelectorName =
720727
Tok->Previous->ColumnWidth;
728+
++Contexts.back().FirstObjCSelectorName->ObjCSelectorNameParts;
721729
}
722730
} else if (Contexts.back().ColonIsForRangeExpr) {
723731
Tok->Type = TT_RangeBasedForLoopColon;

0 commit comments

Comments
 (0)
Please sign in to comment.