[clang-reorder-fields] Use expanded location for macros (#142147)

Fixes macros being replaced instead of their expansion.

Closes #52632
This commit is contained in:
Vladimir Vuksanovic
2025-06-16 00:07:51 +02:00
committed by GitHub
parent f4a63523b8
commit 34c85ed2bc
2 changed files with 28 additions and 0 deletions

View File

@@ -86,6 +86,10 @@ getNewFieldsOrder(const RecordDecl *Definition,
static void
addReplacement(SourceRange Old, SourceRange New, const ASTContext &Context,
std::map<std::string, tooling::Replacements> &Replacements) {
if (Old.getBegin().isMacroID())
Old = Context.getSourceManager().getExpansionRange(Old).getAsRange();
if (New.getBegin().isMacroID())
New = Context.getSourceManager().getExpansionRange(New).getAsRange();
StringRef NewText =
Lexer::getSourceText(CharSourceRange::getTokenRange(New),
Context.getSourceManager(), Context.getLangOpts());

View File

@@ -0,0 +1,24 @@
// RUN: clang-reorder-fields -record-name ::bar::Foo -fields-order z,y,x %s -- | FileCheck %s
namespace bar {
#define INT_DECL(NAME) int NAME // CHECK: {{^#define INT_DECL\(NAME\) int NAME}}
#define MACRO_DECL int x; // CHECK-NEXT: {{^#define MACRO_DECL int x;}}
struct Foo {
MACRO_DECL // CHECK: {{^ INT_DECL\(z\);}}
int y; // CHECK-NEXT: {{^ int y;}}
INT_DECL(z); // CHECK-NEXT: {{^ MACRO_DECL}}
};
#define FOO 0 // CHECK: {{^#define FOO 0}}
#define BAR 1 // CHECK-NEXT: {{^#define BAR 1}}
#define BAZ 2 // CHECK-NEXT: {{^#define BAZ 2}}
struct Foo foo = {
FOO, // CHECK: {{^ BAZ,}}
BAR, // CHECK-NEXT: {{^ BAR,}}
BAZ, // CHECK-NEXT: {{^ FOO,}}
};
} // end namespace bar