Skip to content

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

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 1 commit into from
Jun 15, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -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());
Expand Down
Original file line number Diff line number Diff line change
@@ -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