[clang] [ExtractAPI] Deterministically emit macros (PR #215381)
via cfe-commits
cfe-commits at lists.llvm.org
Mon Aug 10 13:11:11 PDT 2026
llvmorg-github-actions[bot] wrote:
<!--LLVM PR SUMMARY COMMENT-->
@llvm/pr-subscribers-clang
Author: Prajwal Nadig (snprajwal)
<details>
<summary>Changes</summary>
Macros are processed by iterating over the preprocessor's stored `DenseMap`, which is pointer-keyed with `IdentifierInfo`. This map is not ordered, leading to the contents of the symbol graph changing across runs with identical inputs. Sort macros lexicographically by name to ensure consistent output.
rdar://184545768
---
Full diff: https://github.com/llvm/llvm-project/pull/215381.diff
2 Files Affected:
- (modified) clang/lib/ExtractAPI/ExtractAPIConsumer.cpp (+9-2)
- (modified) clang/test/ExtractAPI/macros.c (+8)
``````````diff
diff --git a/clang/lib/ExtractAPI/ExtractAPIConsumer.cpp b/clang/lib/ExtractAPI/ExtractAPIConsumer.cpp
index 85da480fb67a6..4b3d6993acce5 100644
--- a/clang/lib/ExtractAPI/ExtractAPIConsumer.cpp
+++ b/clang/lib/ExtractAPI/ExtractAPIConsumer.cpp
@@ -287,8 +287,15 @@ class MacroCallback : public PPCallbacks {
: Ctx(Ctx), SM(SM), API(API), PP(PP) {}
void EndOfMainFile() override {
- for (const auto &M : PP.macros()) {
- auto *II = M.getFirst();
+ SmallVector<const IdentifierInfo *> Macros;
+ for (const auto &M : PP.macros())
+ Macros.push_back(M.getFirst());
+ llvm::sort(Macros,
+ [](const IdentifierInfo *LHS, const IdentifierInfo *RHS) {
+ return LHS->getName() < RHS->getName();
+ });
+
+ for (const auto *II : Macros) {
auto MD = PP.getMacroDefinition(II);
auto *MI = MD.getMacroInfo();
diff --git a/clang/test/ExtractAPI/macros.c b/clang/test/ExtractAPI/macros.c
index 15eb5f6a7f66f..cb0b018fd5390 100644
--- a/clang/test/ExtractAPI/macros.c
+++ b/clang/test/ExtractAPI/macros.c
@@ -354,5 +354,13 @@
// FUNGNU-NEXT: "FUNGNU"
// FUNGNU-NEXT: ]
+// RUN: FileCheck %s --input-file %t/output.symbols.json --check-prefix ORDER
+// ORDER: "!testLabel": "c:@macro at FUN"
+// ORDER: "!testLabel": "c:@macro at FUNC99"
+// ORDER: "!testLabel": "c:@macro at FUNGNU"
+// ORDER: "!testLabel": "c:@macro at HELLO"
+// ORDER: "!testLabel": "c:@macro at MACRO_FUN"
+// ORDER: "!testLabel": "c:@macro at WORLD"
+
// expected-no-diagnostics
``````````
</details>
https://github.com/llvm/llvm-project/pull/215381
More information about the cfe-commits
mailing list