Skip to content

Commit cc75e52

Browse files
committed
[clang-format][NFC] Refactor isPointerOrReference
1 parent d62f040 commit cc75e52

File tree

4 files changed

+20
-20
lines changed

4 files changed

+20
-20
lines changed

clang/lib/Format/FormatToken.h

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -692,6 +692,10 @@ struct FormatToken {
692692
TT_LeadingJavaAnnotation);
693693
}
694694

695+
bool isPointerOrReference() const {
696+
return isOneOf(tok::star, tok::amp, tok::ampamp);
697+
}
698+
695699
bool isUnaryOperator() const {
696700
switch (Tok.getKind()) {
697701
case tok::plus:

clang/lib/Format/QualifierAlignmentFixer.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -380,7 +380,7 @@ const FormatToken *LeftRightQualifierAlignmentFixer::analyzeLeft(
380380

381381
// For left qualifiers preceeded by nothing, a template declaration, or *,&,&&
382382
// we only perform sorting.
383-
if (!TypeToken || TypeToken->isOneOf(tok::star, tok::amp, tok::ampamp) ||
383+
if (!TypeToken || TypeToken->isPointerOrReference() ||
384384
TypeToken->ClosesRequiresClause || TypeToken->ClosesTemplateDeclaration) {
385385

386386
// Don't sort past a non-configured qualifier token.

clang/lib/Format/TokenAnnotator.cpp

Lines changed: 14 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -405,7 +405,7 @@ class AnnotatingParser {
405405
// void (^ObjCBlock)(void);
406406
bool MightBeFunctionType = !Contexts[Contexts.size() - 2].IsExpression;
407407
bool ProbablyFunctionType =
408-
CurrentToken->isOneOf(tok::star, tok::amp, tok::ampamp, tok::caret);
408+
CurrentToken->isPointerOrReference() || CurrentToken->is(tok::caret);
409409
bool HasMultipleLines = false;
410410
bool HasMultipleParametersOnALine = false;
411411
bool MightBeObjCForRangeLoop =
@@ -422,8 +422,7 @@ class AnnotatingParser {
422422
FormatToken *PrevPrev = Prev->getPreviousNonComment();
423423
FormatToken *Next = CurrentToken->Next;
424424
if (PrevPrev && PrevPrev->is(tok::identifier) &&
425-
PrevPrev->isNot(TT_TypeName) &&
426-
Prev->isOneOf(tok::star, tok::amp, tok::ampamp) &&
425+
PrevPrev->isNot(TT_TypeName) && Prev->isPointerOrReference() &&
427426
CurrentToken->is(tok::identifier) && Next->isNot(tok::equal)) {
428427
Prev->setType(TT_BinaryOperator);
429428
LookForDecls = false;
@@ -460,10 +459,8 @@ class AnnotatingParser {
460459
// auto my_lambda = MACRO((Type *type, int i) { .. body .. });
461460
for (FormatToken *Tok = &OpeningParen; Tok != CurrentToken;
462461
Tok = Tok->Next) {
463-
if (Tok->is(TT_BinaryOperator) &&
464-
Tok->isOneOf(tok::star, tok::amp, tok::ampamp)) {
462+
if (Tok->is(TT_BinaryOperator) && Tok->isPointerOrReference())
465463
Tok->setType(TT_PointerOrReference);
466-
}
467464
}
468465
}
469466

@@ -1861,8 +1858,8 @@ class AnnotatingParser {
18611858
if (Previous->opensScope())
18621859
break;
18631860
if (Previous->isOneOf(TT_BinaryOperator, TT_UnaryOperator) &&
1864-
Previous->isOneOf(tok::star, tok::amp, tok::ampamp) &&
1865-
Previous->Previous && Previous->Previous->isNot(tok::equal)) {
1861+
Previous->isPointerOrReference() && Previous->Previous &&
1862+
Previous->Previous->isNot(tok::equal)) {
18661863
Previous->setType(TT_PointerOrReference);
18671864
}
18681865
}
@@ -2023,7 +2020,7 @@ class AnnotatingParser {
20232020
} else if (isDeductionGuide(Current)) {
20242021
// Deduction guides trailing arrow " A(...) -> A<T>;".
20252022
Current.setType(TT_TrailingReturnArrow);
2026-
} else if (Current.isOneOf(tok::star, tok::amp, tok::ampamp)) {
2023+
} else if (Current.isPointerOrReference()) {
20272024
Current.setType(determineStarAmpUsage(
20282025
Current,
20292026
Contexts.back().CanBeExpression && Contexts.back().IsExpression,
@@ -3281,7 +3278,7 @@ static bool isFunctionDeclarationName(bool IsCpp, const FormatToken &Current,
32813278
continue;
32823279
}
32833280
if ((Next->isSimpleTypeSpecifier() || Next->is(tok::identifier)) &&
3284-
Next->Next && Next->Next->isOneOf(tok::star, tok::amp, tok::ampamp)) {
3281+
Next->Next && Next->Next->isPointerOrReference()) {
32853282
// For operator void*(), operator char*(), operator Foo*().
32863283
Next = Next->Next;
32873284
continue;
@@ -3310,7 +3307,7 @@ static bool isFunctionDeclarationName(bool IsCpp, const FormatToken &Current,
33103307
assert(Previous->MatchingParen->is(TT_TypeDeclarationParen));
33113308
return true;
33123309
}
3313-
if (!Previous->isOneOf(tok::star, tok::amp, tok::ampamp, TT_TemplateCloser))
3310+
if (!Previous->isPointerOrReference() && Previous->isNot(TT_TemplateCloser))
33143311
return false;
33153312
Next = skipOperatorName(Next);
33163313
} else {
@@ -3481,8 +3478,8 @@ void TokenAnnotator::calculateFormattingInformation(AnnotatedLine &Line) const {
34813478
continue;
34823479
auto *Next = Tok->Next;
34833480
const bool NextIsBinaryOperator =
3484-
Next && Next->isOneOf(tok::star, tok::amp, tok::ampamp) &&
3485-
Next->Next && Next->Next->is(tok::identifier);
3481+
Next && Next->isPointerOrReference() && Next->Next &&
3482+
Next->Next->is(tok::identifier);
34863483
if (!NextIsBinaryOperator)
34873484
continue;
34883485
Next->setType(TT_BinaryOperator);
@@ -4105,15 +4102,15 @@ bool TokenAnnotator::spaceRequiredBetween(const AnnotatedLine &Line,
41054102
}
41064103
// Ensure right pointer alignment with ellipsis e.g. int *...P
41074104
if (Left.is(tok::ellipsis) && Left.Previous &&
4108-
Left.Previous->isOneOf(tok::star, tok::amp, tok::ampamp)) {
4105+
Left.Previous->isPointerOrReference()) {
41094106
return Style.PointerAlignment != FormatStyle::PAS_Right;
41104107
}
41114108

41124109
if (Right.is(tok::star) && Left.is(tok::l_paren))
41134110
return false;
4114-
if (Left.is(tok::star) && Right.isOneOf(tok::star, tok::amp, tok::ampamp))
4111+
if (Left.is(tok::star) && Right.isPointerOrReference())
41154112
return false;
4116-
if (Right.isOneOf(tok::star, tok::amp, tok::ampamp)) {
4113+
if (Right.isPointerOrReference()) {
41174114
const FormatToken *Previous = &Left;
41184115
while (Previous && Previous->isNot(tok::kw_operator)) {
41194116
if (Previous->is(tok::identifier) || Previous->isSimpleTypeSpecifier()) {
@@ -5258,7 +5255,7 @@ bool TokenAnnotator::mustBreakBefore(const AnnotatedLine &Line,
52585255
}
52595256

52605257
if (Style.BraceWrapping.BeforeLambdaBody && Right.is(TT_LambdaLBrace) &&
5261-
Left.isOneOf(tok::star, tok::amp, tok::ampamp, TT_TemplateCloser)) {
5258+
(Left.isPointerOrReference() || Left.is(TT_TemplateCloser))) {
52625259
return true;
52635260
}
52645261

clang/lib/Format/WhitespaceManager.cpp

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -470,8 +470,7 @@ AlignTokenSequence(const FormatStyle &Style, unsigned Start, unsigned End,
470470
Previous >= 0 &&
471471
Changes[Previous].Tok->getType() == TT_PointerOrReference;
472472
--Previous) {
473-
assert(
474-
Changes[Previous].Tok->isOneOf(tok::star, tok::amp, tok::ampamp));
473+
assert(Changes[Previous].Tok->isPointerOrReference());
475474
if (Changes[Previous].Tok->isNot(tok::star)) {
476475
if (ReferenceNotRightAligned)
477476
continue;

0 commit comments

Comments
 (0)