Skip to content

Commit

Permalink
[clang-format][NFC] Refactor isPointerOrReference
Browse files Browse the repository at this point in the history
  • Loading branch information
owenca committed Nov 10, 2023
1 parent d62f040 commit cc75e52
Show file tree
Hide file tree
Showing 4 changed files with 20 additions and 20 deletions.
4 changes: 4 additions & 0 deletions clang/lib/Format/FormatToken.h
Original file line number Diff line number Diff line change
Expand Up @@ -692,6 +692,10 @@ struct FormatToken {
TT_LeadingJavaAnnotation);
}

bool isPointerOrReference() const {
return isOneOf(tok::star, tok::amp, tok::ampamp);
}

bool isUnaryOperator() const {
switch (Tok.getKind()) {
case tok::plus:
Expand Down
2 changes: 1 addition & 1 deletion clang/lib/Format/QualifierAlignmentFixer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -380,7 +380,7 @@ const FormatToken *LeftRightQualifierAlignmentFixer::analyzeLeft(

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

// Don't sort past a non-configured qualifier token.
Expand Down
31 changes: 14 additions & 17 deletions clang/lib/Format/TokenAnnotator.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -405,7 +405,7 @@ class AnnotatingParser {
// void (^ObjCBlock)(void);
bool MightBeFunctionType = !Contexts[Contexts.size() - 2].IsExpression;
bool ProbablyFunctionType =
CurrentToken->isOneOf(tok::star, tok::amp, tok::ampamp, tok::caret);
CurrentToken->isPointerOrReference() || CurrentToken->is(tok::caret);
bool HasMultipleLines = false;
bool HasMultipleParametersOnALine = false;
bool MightBeObjCForRangeLoop =
Expand All @@ -422,8 +422,7 @@ class AnnotatingParser {
FormatToken *PrevPrev = Prev->getPreviousNonComment();
FormatToken *Next = CurrentToken->Next;
if (PrevPrev && PrevPrev->is(tok::identifier) &&
PrevPrev->isNot(TT_TypeName) &&
Prev->isOneOf(tok::star, tok::amp, tok::ampamp) &&
PrevPrev->isNot(TT_TypeName) && Prev->isPointerOrReference() &&
CurrentToken->is(tok::identifier) && Next->isNot(tok::equal)) {
Prev->setType(TT_BinaryOperator);
LookForDecls = false;
Expand Down Expand Up @@ -460,10 +459,8 @@ class AnnotatingParser {
// auto my_lambda = MACRO((Type *type, int i) { .. body .. });
for (FormatToken *Tok = &OpeningParen; Tok != CurrentToken;
Tok = Tok->Next) {
if (Tok->is(TT_BinaryOperator) &&
Tok->isOneOf(tok::star, tok::amp, tok::ampamp)) {
if (Tok->is(TT_BinaryOperator) && Tok->isPointerOrReference())
Tok->setType(TT_PointerOrReference);
}
}
}

Expand Down Expand Up @@ -1861,8 +1858,8 @@ class AnnotatingParser {
if (Previous->opensScope())
break;
if (Previous->isOneOf(TT_BinaryOperator, TT_UnaryOperator) &&
Previous->isOneOf(tok::star, tok::amp, tok::ampamp) &&
Previous->Previous && Previous->Previous->isNot(tok::equal)) {
Previous->isPointerOrReference() && Previous->Previous &&
Previous->Previous->isNot(tok::equal)) {
Previous->setType(TT_PointerOrReference);
}
}
Expand Down Expand Up @@ -2023,7 +2020,7 @@ class AnnotatingParser {
} else if (isDeductionGuide(Current)) {
// Deduction guides trailing arrow " A(...) -> A<T>;".
Current.setType(TT_TrailingReturnArrow);
} else if (Current.isOneOf(tok::star, tok::amp, tok::ampamp)) {
} else if (Current.isPointerOrReference()) {
Current.setType(determineStarAmpUsage(
Current,
Contexts.back().CanBeExpression && Contexts.back().IsExpression,
Expand Down Expand Up @@ -3281,7 +3278,7 @@ static bool isFunctionDeclarationName(bool IsCpp, const FormatToken &Current,
continue;
}
if ((Next->isSimpleTypeSpecifier() || Next->is(tok::identifier)) &&
Next->Next && Next->Next->isOneOf(tok::star, tok::amp, tok::ampamp)) {
Next->Next && Next->Next->isPointerOrReference()) {
// For operator void*(), operator char*(), operator Foo*().
Next = Next->Next;
continue;
Expand Down Expand Up @@ -3310,7 +3307,7 @@ static bool isFunctionDeclarationName(bool IsCpp, const FormatToken &Current,
assert(Previous->MatchingParen->is(TT_TypeDeclarationParen));
return true;
}
if (!Previous->isOneOf(tok::star, tok::amp, tok::ampamp, TT_TemplateCloser))
if (!Previous->isPointerOrReference() && Previous->isNot(TT_TemplateCloser))
return false;
Next = skipOperatorName(Next);
} else {
Expand Down Expand Up @@ -3481,8 +3478,8 @@ void TokenAnnotator::calculateFormattingInformation(AnnotatedLine &Line) const {
continue;
auto *Next = Tok->Next;
const bool NextIsBinaryOperator =
Next && Next->isOneOf(tok::star, tok::amp, tok::ampamp) &&
Next->Next && Next->Next->is(tok::identifier);
Next && Next->isPointerOrReference() && Next->Next &&
Next->Next->is(tok::identifier);
if (!NextIsBinaryOperator)
continue;
Next->setType(TT_BinaryOperator);
Expand Down Expand Up @@ -4105,15 +4102,15 @@ bool TokenAnnotator::spaceRequiredBetween(const AnnotatedLine &Line,
}
// Ensure right pointer alignment with ellipsis e.g. int *...P
if (Left.is(tok::ellipsis) && Left.Previous &&
Left.Previous->isOneOf(tok::star, tok::amp, tok::ampamp)) {
Left.Previous->isPointerOrReference()) {
return Style.PointerAlignment != FormatStyle::PAS_Right;
}

if (Right.is(tok::star) && Left.is(tok::l_paren))
return false;
if (Left.is(tok::star) && Right.isOneOf(tok::star, tok::amp, tok::ampamp))
if (Left.is(tok::star) && Right.isPointerOrReference())
return false;
if (Right.isOneOf(tok::star, tok::amp, tok::ampamp)) {
if (Right.isPointerOrReference()) {
const FormatToken *Previous = &Left;
while (Previous && Previous->isNot(tok::kw_operator)) {
if (Previous->is(tok::identifier) || Previous->isSimpleTypeSpecifier()) {
Expand Down Expand Up @@ -5258,7 +5255,7 @@ bool TokenAnnotator::mustBreakBefore(const AnnotatedLine &Line,
}

if (Style.BraceWrapping.BeforeLambdaBody && Right.is(TT_LambdaLBrace) &&
Left.isOneOf(tok::star, tok::amp, tok::ampamp, TT_TemplateCloser)) {
(Left.isPointerOrReference() || Left.is(TT_TemplateCloser))) {
return true;
}

Expand Down
3 changes: 1 addition & 2 deletions clang/lib/Format/WhitespaceManager.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -470,8 +470,7 @@ AlignTokenSequence(const FormatStyle &Style, unsigned Start, unsigned End,
Previous >= 0 &&
Changes[Previous].Tok->getType() == TT_PointerOrReference;
--Previous) {
assert(
Changes[Previous].Tok->isOneOf(tok::star, tok::amp, tok::ampamp));
assert(Changes[Previous].Tok->isPointerOrReference());
if (Changes[Previous].Tok->isNot(tok::star)) {
if (ReferenceNotRightAligned)
continue;
Expand Down

0 comments on commit cc75e52

Please sign in to comment.