-
Notifications
You must be signed in to change notification settings - Fork 14.3k
[clang-format] Fix a misannotation of PointerOrReference #101291
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Conversation
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
@llvm/pr-subscribers-clang-format Author: Owen Pan (owenca) ChangesFixes #101138. Full diff: https://github.com/llvm/llvm-project/pull/101291.diff 2 Files Affected:
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..ce8fdaaa91647 100644
--- a/clang/unittests/Format/TokenAnnotatorTest.cpp
+++ b/clang/unittests/Format/TokenAnnotatorTest.cpp
@@ -904,6 +904,21 @@ TEST_F(TokenAnnotatorTest, UnderstandsOverloadedOperators) {
EXPECT_TOKEN(Tokens[9], tok::l_paren, TT_OverloadedOperatorLParen);
EXPECT_TOKEN(Tokens[11], tok::amp, 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[1], tok::less, TT_TemplateOpener);
+ EXPECT_TOKEN(Tokens[4], tok::greater, TT_TemplateCloser);
+ EXPECT_TOKEN(Tokens[6], tok::less, TT_TemplateOpener);
+ EXPECT_TOKEN(Tokens[8], tok::less, TT_TemplateOpener);
+ EXPECT_TOKEN(Tokens[10], tok::greater, TT_TemplateCloser);
+ EXPECT_TOKEN(Tokens[13], tok::greater, TT_TemplateCloser);
+ EXPECT_TOKEN(Tokens[15], tok::kw_operator, TT_FunctionDeclarationName);
+ EXPECT_TOKEN(Tokens[16], tok::tilde, TT_OverloadedOperator);
+ EXPECT_TOKEN(Tokens[17], tok::l_paren, TT_OverloadedOperatorLParen);
+ EXPECT_TOKEN(Tokens[19], tok::amp, TT_PointerOrReference);
+
Tokens = annotate("decltype(auto) operator()(T &x);");
ASSERT_EQ(Tokens.size(), 14u) << Tokens;
EXPECT_TOKEN(Tokens[1], tok::l_paren, TT_TypeDeclarationParen);
|
mydeveloperday
approved these changes
Aug 1, 2024
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Fixes #101138.