[clang] 8abdf7c - [clang-format] Fix a misannotation of PointerOrReference (#101291)
via cfe-commits
cfe-commits at lists.llvm.org
Thu Aug 1 21:55:46 PDT 2024
Author: Owen Pan
Date: 2024-08-01T21:55:43-07:00
New Revision: 8abdf7cc71a72a67ae7b3e60002943e84c8ab218
URL: https://github.com/llvm/llvm-project/commit/8abdf7cc71a72a67ae7b3e60002943e84c8ab218
DIFF: https://github.com/llvm/llvm-project/commit/8abdf7cc71a72a67ae7b3e60002943e84c8ab218.diff
LOG: [clang-format] Fix a misannotation of PointerOrReference (#101291)
Fixes #101138.
Added:
Modified:
clang/lib/Format/TokenAnnotator.cpp
clang/unittests/Format/TokenAnnotatorTest.cpp
Removed:
################################################################################
diff --git a/clang/lib/Format/TokenAnnotator.cpp b/clang/lib/Format/TokenAnnotator.cpp
index 8cd5cf2484160..4ed3e9d0e8e85 100644
--- a/clang/lib/Format/TokenAnnotator.cpp
+++ b/clang/lib/Format/TokenAnnotator.cpp
@@ -3682,17 +3682,17 @@ static bool isFunctionDeclarationName(const LangOptions &LangOpts,
const FormatToken &Current,
const AnnotatedLine &Line,
FormatToken *&ClosingParen) {
- assert(Current.Previous);
-
if (Current.is(TT_FunctionDeclarationName))
return true;
if (!Current.Tok.getIdentifierInfo())
return false;
- const auto &Previous = *Current.Previous;
+ const auto *Prev = Current.getPreviousNonComment();
+ assert(Prev);
+ const auto &Previous = *Prev;
- if (const auto *PrevPrev = Previous.Previous;
+ if (const auto *PrevPrev = Previous.getPreviousNonComment();
PrevPrev && PrevPrev->is(TT_ObjCDecl)) {
return false;
}
@@ -3859,20 +3859,20 @@ void TokenAnnotator::calculateFormattingInformation(AnnotatedLine &Line) const {
First->TotalLength = First->IsMultiline
? Style.ColumnLimit
: Line.FirstStartColumn + First->ColumnWidth;
- FormatToken *Current = First->Next;
- bool InFunctionDecl = Line.MightBeFunctionDecl;
bool AlignArrayOfStructures =
(Style.AlignArrayOfStructures != FormatStyle::AIAS_None &&
Line.Type == LT_ArrayOfStructInitializer);
if (AlignArrayOfStructures)
calculateArrayInitializerColumnList(Line);
+ const auto *FirstNonComment = Line.getFirstNonComment();
bool SeenName = false;
bool LineIsFunctionDeclaration = false;
- FormatToken *ClosingParen = nullptr;
FormatToken *AfterLastAttribute = nullptr;
+ FormatToken *ClosingParen = nullptr;
- for (auto *Tok = Current; Tok; Tok = Tok->Next) {
+ for (auto *Tok = FirstNonComment ? FirstNonComment->Next : nullptr; Tok;
+ Tok = Tok->Next) {
if (Tok->is(TT_StartOfName))
SeenName = true;
if (Tok->Previous->EndsCppAttributeGroup)
@@ -3894,7 +3894,9 @@ void TokenAnnotator::calculateFormattingInformation(AnnotatedLine &Line) const {
}
}
- if (IsCpp && (LineIsFunctionDeclaration || First->is(TT_CtorDtorDeclName)) &&
+ if (IsCpp &&
+ (LineIsFunctionDeclaration ||
+ (FirstNonComment && FirstNonComment->is(TT_CtorDtorDeclName))) &&
Line.endsWith(tok::semi, tok::r_brace)) {
auto *Tok = Line.Last->Previous;
while (Tok->isNot(tok::r_brace))
@@ -3917,7 +3919,7 @@ void TokenAnnotator::calculateFormattingInformation(AnnotatedLine &Line) const {
if (IsCpp) {
if (!LineIsFunctionDeclaration) {
// Annotate */&/&& in `operator` function calls as binary operators.
- for (const auto *Tok = First; Tok; Tok = Tok->Next) {
+ for (const auto *Tok = FirstNonComment; Tok; Tok = Tok->Next) {
if (Tok->isNot(tok::kw_operator))
continue;
do {
@@ -3960,7 +3962,8 @@ void TokenAnnotator::calculateFormattingInformation(AnnotatedLine &Line) const {
}
}
- while (Current) {
+ bool InFunctionDecl = Line.MightBeFunctionDecl;
+ for (auto *Current = First->Next; Current; Current = Current->Next) {
const FormatToken *Prev = Current->Previous;
if (Current->is(TT_LineComment)) {
if (Prev->is(BK_BracedInit) && Prev->opensScope()) {
@@ -4050,13 +4053,11 @@ void TokenAnnotator::calculateFormattingInformation(AnnotatedLine &Line) const {
} else {
Current->SplitPenalty += 20 * Current->BindingStrength;
}
-
- Current = Current->Next;
}
calculateUnbreakableTailLengths(Line);
unsigned IndentLevel = Line.Level;
- for (Current = First; Current; Current = Current->Next) {
+ for (auto *Current = First; Current; Current = Current->Next) {
if (Current->Role)
Current->Role->precomputeFormattingInfos(Current);
if (Current->MatchingParen &&
diff --git a/clang/unittests/Format/TokenAnnotatorTest.cpp b/clang/unittests/Format/TokenAnnotatorTest.cpp
index f432b95cc1e2b..6b71d0d18cf7a 100644
--- a/clang/unittests/Format/TokenAnnotatorTest.cpp
+++ b/clang/unittests/Format/TokenAnnotatorTest.cpp
@@ -269,6 +269,12 @@ TEST_F(TokenAnnotatorTest, UnderstandsUsesOfStarAndAmp) {
ASSERT_EQ(Tokens.size(), 11u) << Tokens;
EXPECT_TOKEN(Tokens[3], tok::ampamp, TT_PointerOrReference);
+ Tokens = annotate("template <typename T>\n"
+ "enable_if_t<is_integral_v<T>, bool> // comment\n"
+ "operator~(T &a);");
+ ASSERT_EQ(Tokens.size(), 24u) << Tokens;
+ EXPECT_TOKEN(Tokens[19], tok::amp, TT_PointerOrReference);
+
Tokens = annotate("template <enable_if_t<foo && !bar>* = nullptr> void f();");
ASSERT_EQ(Tokens.size(), 19u) << Tokens;
EXPECT_TOKEN(Tokens[5], tok::ampamp, TT_BinaryOperator);
More information about the cfe-commits
mailing list