Skip to content

Commit 8abdf7c

Browse files
authored
[clang-format] Fix a misannotation of PointerOrReference (#101291)
Fixes #101138.
1 parent 358593b commit 8abdf7c

File tree

2 files changed

+21
-14
lines changed

2 files changed

+21
-14
lines changed

clang/lib/Format/TokenAnnotator.cpp

Lines changed: 15 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -3682,17 +3682,17 @@ static bool isFunctionDeclarationName(const LangOptions &LangOpts,
36823682
const FormatToken &Current,
36833683
const AnnotatedLine &Line,
36843684
FormatToken *&ClosingParen) {
3685-
assert(Current.Previous);
3686-
36873685
if (Current.is(TT_FunctionDeclarationName))
36883686
return true;
36893687

36903688
if (!Current.Tok.getIdentifierInfo())
36913689
return false;
36923690

3693-
const auto &Previous = *Current.Previous;
3691+
const auto *Prev = Current.getPreviousNonComment();
3692+
assert(Prev);
3693+
const auto &Previous = *Prev;
36943694

3695-
if (const auto *PrevPrev = Previous.Previous;
3695+
if (const auto *PrevPrev = Previous.getPreviousNonComment();
36963696
PrevPrev && PrevPrev->is(TT_ObjCDecl)) {
36973697
return false;
36983698
}
@@ -3859,20 +3859,20 @@ void TokenAnnotator::calculateFormattingInformation(AnnotatedLine &Line) const {
38593859
First->TotalLength = First->IsMultiline
38603860
? Style.ColumnLimit
38613861
: Line.FirstStartColumn + First->ColumnWidth;
3862-
FormatToken *Current = First->Next;
3863-
bool InFunctionDecl = Line.MightBeFunctionDecl;
38643862
bool AlignArrayOfStructures =
38653863
(Style.AlignArrayOfStructures != FormatStyle::AIAS_None &&
38663864
Line.Type == LT_ArrayOfStructInitializer);
38673865
if (AlignArrayOfStructures)
38683866
calculateArrayInitializerColumnList(Line);
38693867

3868+
const auto *FirstNonComment = Line.getFirstNonComment();
38703869
bool SeenName = false;
38713870
bool LineIsFunctionDeclaration = false;
3872-
FormatToken *ClosingParen = nullptr;
38733871
FormatToken *AfterLastAttribute = nullptr;
3872+
FormatToken *ClosingParen = nullptr;
38743873

3875-
for (auto *Tok = Current; Tok; Tok = Tok->Next) {
3874+
for (auto *Tok = FirstNonComment ? FirstNonComment->Next : nullptr; Tok;
3875+
Tok = Tok->Next) {
38763876
if (Tok->is(TT_StartOfName))
38773877
SeenName = true;
38783878
if (Tok->Previous->EndsCppAttributeGroup)
@@ -3894,7 +3894,9 @@ void TokenAnnotator::calculateFormattingInformation(AnnotatedLine &Line) const {
38943894
}
38953895
}
38963896

3897-
if (IsCpp && (LineIsFunctionDeclaration || First->is(TT_CtorDtorDeclName)) &&
3897+
if (IsCpp &&
3898+
(LineIsFunctionDeclaration ||
3899+
(FirstNonComment && FirstNonComment->is(TT_CtorDtorDeclName))) &&
38983900
Line.endsWith(tok::semi, tok::r_brace)) {
38993901
auto *Tok = Line.Last->Previous;
39003902
while (Tok->isNot(tok::r_brace))
@@ -3917,7 +3919,7 @@ void TokenAnnotator::calculateFormattingInformation(AnnotatedLine &Line) const {
39173919
if (IsCpp) {
39183920
if (!LineIsFunctionDeclaration) {
39193921
// Annotate */&/&& in `operator` function calls as binary operators.
3920-
for (const auto *Tok = First; Tok; Tok = Tok->Next) {
3922+
for (const auto *Tok = FirstNonComment; Tok; Tok = Tok->Next) {
39213923
if (Tok->isNot(tok::kw_operator))
39223924
continue;
39233925
do {
@@ -3960,7 +3962,8 @@ void TokenAnnotator::calculateFormattingInformation(AnnotatedLine &Line) const {
39603962
}
39613963
}
39623964

3963-
while (Current) {
3965+
bool InFunctionDecl = Line.MightBeFunctionDecl;
3966+
for (auto *Current = First->Next; Current; Current = Current->Next) {
39643967
const FormatToken *Prev = Current->Previous;
39653968
if (Current->is(TT_LineComment)) {
39663969
if (Prev->is(BK_BracedInit) && Prev->opensScope()) {
@@ -4050,13 +4053,11 @@ void TokenAnnotator::calculateFormattingInformation(AnnotatedLine &Line) const {
40504053
} else {
40514054
Current->SplitPenalty += 20 * Current->BindingStrength;
40524055
}
4053-
4054-
Current = Current->Next;
40554056
}
40564057

40574058
calculateUnbreakableTailLengths(Line);
40584059
unsigned IndentLevel = Line.Level;
4059-
for (Current = First; Current; Current = Current->Next) {
4060+
for (auto *Current = First; Current; Current = Current->Next) {
40604061
if (Current->Role)
40614062
Current->Role->precomputeFormattingInfos(Current);
40624063
if (Current->MatchingParen &&

clang/unittests/Format/TokenAnnotatorTest.cpp

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -269,6 +269,12 @@ TEST_F(TokenAnnotatorTest, UnderstandsUsesOfStarAndAmp) {
269269
ASSERT_EQ(Tokens.size(), 11u) << Tokens;
270270
EXPECT_TOKEN(Tokens[3], tok::ampamp, TT_PointerOrReference);
271271

272+
Tokens = annotate("template <typename T>\n"
273+
"enable_if_t<is_integral_v<T>, bool> // comment\n"
274+
"operator~(T &a);");
275+
ASSERT_EQ(Tokens.size(), 24u) << Tokens;
276+
EXPECT_TOKEN(Tokens[19], tok::amp, TT_PointerOrReference);
277+
272278
Tokens = annotate("template <enable_if_t<foo && !bar>* = nullptr> void f();");
273279
ASSERT_EQ(Tokens.size(), 19u) << Tokens;
274280
EXPECT_TOKEN(Tokens[5], tok::ampamp, TT_BinaryOperator);

0 commit comments

Comments
 (0)