Skip to content

Commit 2123fa2

Browse files
committed
[clang-reorder-fields] Reorder leading comments.
Similarly to #122918, leading comments are currently not being moved. ``` struct Foo { // This one is the cool field. int a; int b; }; ``` becomes: ``` struct Foo { // This one is the cool field. int b; int a; }; ``` but should be: ``` struct Foo { int b; // This one is the cool field. int a; }; ```
1 parent 4146793 commit 2123fa2

File tree

2 files changed

+31
-4
lines changed

2 files changed

+31
-4
lines changed

clang-tools-extra/clang-reorder-fields/ReorderFieldsAction.cpp

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -118,6 +118,29 @@ findMembersUsedInInitExpr(const CXXCtorInitializer *Initializer,
118118
return Results;
119119
}
120120

121+
/// Returns the start of the leading comments before `Loc`.
122+
static SourceLocation getStartOfLeadingComment(SourceLocation Loc,
123+
const SourceManager &SM,
124+
const LangOptions &LangOpts) {
125+
// We consider any leading comment token that is on the same line or
126+
// indented similarly to the first comment to be part of the leading comment.
127+
const unsigned Line = SM.getPresumedLineNumber(Loc);
128+
const unsigned Column = SM.getPresumedColumnNumber(Loc);
129+
std::optional<Token> Tok =
130+
Lexer::findPreviousToken(Loc, SM, LangOpts, /*IncludeComments=*/true);
131+
while (Tok && Tok->is(tok::comment)) {
132+
const SourceLocation CommentLoc =
133+
Lexer::GetBeginningOfToken(Tok->getLocation(), SM, LangOpts);
134+
if (SM.getPresumedLineNumber(CommentLoc) != Line &&
135+
SM.getPresumedColumnNumber(CommentLoc) != Column) {
136+
break;
137+
}
138+
Loc = CommentLoc;
139+
Tok = Lexer::findPreviousToken(Loc, SM, LangOpts, /*IncludeComments=*/true);
140+
}
141+
return Loc;
142+
}
143+
121144
/// Returns the end of the trailing comments after `Loc`.
122145
static SourceLocation getEndOfTrailingComment(SourceLocation Loc,
123146
const SourceManager &SM,
@@ -159,6 +182,7 @@ static SourceRange getFullFieldSourceRange(const FieldDecl &Field,
159182
if (CurrentToken->is(tok::semi))
160183
break;
161184
}
185+
Begin = getStartOfLeadingComment(Begin, SM, LangOpts);
162186
End = getEndOfTrailingComment(End, SM, LangOpts);
163187
return SourceRange(Begin, End);
164188
}

clang-tools-extra/test/clang-reorder-fields/Comments.cpp

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
// RUN: clang-reorder-fields -record-name Foo -fields-order e1,e3,e2,a,c,b %s -- | FileCheck %s
1+
// RUN: clang-reorder-fields -record-name Foo -fields-order c,e1,e3,e2,a,b %s -- | FileCheck %s
22

33
class Foo {
44
int a; // Trailing comment for a.
@@ -12,12 +12,15 @@ class Foo {
1212
int e3 /*c-like*/;
1313
};
1414

15-
// CHECK: /*c-like*/ int e1;
15+
// Note: the position of the empty line is somewhat arbitrary.
16+
17+
// CHECK: // Prefix comments for c.
18+
// CHECK-NEXT: int c;
19+
// CHECK-NEXT: /*c-like*/ int e1;
1620
// CHECK-NEXT: int e3 /*c-like*/;
21+
// CHECK-EMPTY:
1722
// CHECK-NEXT: int /*c-like*/ e2;
1823
// CHECK-NEXT: int a; // Trailing comment for a.
19-
// CHECK-NEXT: // Prefix comments for c.
20-
// CHECK-NEXT: int c;
2124
// CHECK-NEXT: int b; // Multiline
2225
// CHECK-NEXT: // trailing for b.
2326

0 commit comments

Comments
 (0)