aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorVladimir Vuksanovic <109677816+vvuksanovic@users.noreply.github.com>2025-06-16 00:07:51 +0200
committerGitHub <noreply@github.com>2025-06-15 18:07:51 -0400
commit34c85ed2bc1adfa375745db6de7f62d350a8f768 (patch)
tree2bcba1d9bd9bf1b3694e33bbd8af2532ee2b52d4
parentf4a63523b88631e224496435bea0940ac05897bf (diff)
downloadllvm-34c85ed2bc1adfa375745db6de7f62d350a8f768.zip
llvm-34c85ed2bc1adfa375745db6de7f62d350a8f768.tar.gz
llvm-34c85ed2bc1adfa375745db6de7f62d350a8f768.tar.bz2
[clang-reorder-fields] Use expanded location for macros (#142147)
Fixes macros being replaced instead of their expansion. Closes #52632
-rw-r--r--clang-tools-extra/clang-reorder-fields/ReorderFieldsAction.cpp4
-rw-r--r--clang-tools-extra/test/clang-reorder-fields/MacroExpansionField.cpp24
2 files changed, 28 insertions, 0 deletions
diff --git a/clang-tools-extra/clang-reorder-fields/ReorderFieldsAction.cpp b/clang-tools-extra/clang-reorder-fields/ReorderFieldsAction.cpp
index ea02076..3b1cd18 100644
--- a/clang-tools-extra/clang-reorder-fields/ReorderFieldsAction.cpp
+++ b/clang-tools-extra/clang-reorder-fields/ReorderFieldsAction.cpp
@@ -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());
diff --git a/clang-tools-extra/test/clang-reorder-fields/MacroExpansionField.cpp b/clang-tools-extra/test/clang-reorder-fields/MacroExpansionField.cpp
new file mode 100644
index 0000000..a4c3cbc
--- /dev/null
+++ b/clang-tools-extra/test/clang-reorder-fields/MacroExpansionField.cpp
@@ -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