Skip to content

Commit 34c85ed

Browse files
authored
[clang-reorder-fields] Use expanded location for macros (#142147)
Fixes macros being replaced instead of their expansion. Closes #52632
1 parent f4a6352 commit 34c85ed

File tree

2 files changed

+28
-0
lines changed

2 files changed

+28
-0
lines changed

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

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -86,6 +86,10 @@ getNewFieldsOrder(const RecordDecl *Definition,
8686
static void
8787
addReplacement(SourceRange Old, SourceRange New, const ASTContext &Context,
8888
std::map<std::string, tooling::Replacements> &Replacements) {
89+
if (Old.getBegin().isMacroID())
90+
Old = Context.getSourceManager().getExpansionRange(Old).getAsRange();
91+
if (New.getBegin().isMacroID())
92+
New = Context.getSourceManager().getExpansionRange(New).getAsRange();
8993
StringRef NewText =
9094
Lexer::getSourceText(CharSourceRange::getTokenRange(New),
9195
Context.getSourceManager(), Context.getLangOpts());
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
// RUN: clang-reorder-fields -record-name ::bar::Foo -fields-order z,y,x %s -- | FileCheck %s
2+
3+
namespace bar {
4+
5+
#define INT_DECL(NAME) int NAME // CHECK: {{^#define INT_DECL\(NAME\) int NAME}}
6+
#define MACRO_DECL int x; // CHECK-NEXT: {{^#define MACRO_DECL int x;}}
7+
8+
struct Foo {
9+
MACRO_DECL // CHECK: {{^ INT_DECL\(z\);}}
10+
int y; // CHECK-NEXT: {{^ int y;}}
11+
INT_DECL(z); // CHECK-NEXT: {{^ MACRO_DECL}}
12+
};
13+
14+
#define FOO 0 // CHECK: {{^#define FOO 0}}
15+
#define BAR 1 // CHECK-NEXT: {{^#define BAR 1}}
16+
#define BAZ 2 // CHECK-NEXT: {{^#define BAZ 2}}
17+
18+
struct Foo foo = {
19+
FOO, // CHECK: {{^ BAZ,}}
20+
BAR, // CHECK-NEXT: {{^ BAR,}}
21+
BAZ, // CHECK-NEXT: {{^ FOO,}}
22+
};
23+
24+
} // end namespace bar

0 commit comments

Comments
 (0)