Skip to content

Commit 170b5fd

Browse files
authored
[Modules] Make -module-file-info print macro names in deterministic order (#161332)
Developers reported non-deterministic output from `-module-file-info`, thinking this reflected non-determinism in the .pcm files themselves. However, it turned out it was the printing that was non-deterministic: ``` $ cat /tmp/a.h #define FOO 1 #define BAR 2 $ build/bin/clang -cc1 -std=c++20 -x c++ -emit-header-unit /tmp/a.h -o /tmp/a.pcm $ build/bin/clang -cc1 -module-file-info /tmp/a.pcm | grep -A2 Definitions Macro Definitions: FOO BAR $ build/bin/clang -cc1 -module-file-info /tmp/a.pcm | grep -A2 Definitions Macro Definitions: BAR FOO ``` Making the output deterministic also simplifies the test. This is a follow-up to 360c5fe
1 parent 635910d commit 170b5fd

File tree

2 files changed

+21
-18
lines changed

2 files changed

+21
-18
lines changed

clang/lib/Frontend/FrontendActions.cpp

Lines changed: 11 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -971,14 +971,17 @@ void DumpModuleInfoAction::ExecuteAction() {
971971
// Emit the macro definitions in the module file so that we can know how
972972
// much definitions in the module file quickly.
973973
// TODO: Emit the macro definition bodies completely.
974-
if (auto FilteredMacros = llvm::make_filter_range(
975-
R->getPreprocessor().macros(),
976-
[](const auto &Macro) { return Macro.first->isFromAST(); });
977-
!FilteredMacros.empty()) {
978-
Out << " Macro Definitions:\n";
979-
for (/*<IdentifierInfo *, MacroState> pair*/ const auto &Macro :
980-
FilteredMacros)
981-
Out << " " << Macro.first->getName() << "\n";
974+
{
975+
std::vector<StringRef> MacroNames;
976+
for (const auto &M : R->getPreprocessor().macros()) {
977+
if (M.first->isFromAST())
978+
MacroNames.push_back(M.first->getName());
979+
}
980+
llvm::sort(MacroNames);
981+
if (!MacroNames.empty())
982+
Out << " Macro Definitions:\n";
983+
for (StringRef Name : MacroNames)
984+
Out << " " << Name << "\n";
982985
}
983986

984987
// Now let's print out any modules we did not see as part of the Primary.

clang/test/Modules/cxx20-module-file-info-macros.cpp

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -36,28 +36,28 @@
3636
#define REDEFINE
3737

3838
// CHECK: Macro Definitions:
39-
// CHECK-DAG: REDEFINE
40-
// CHECK-DAG: FUNC_Macro
41-
// CHECK-DAG: CONSTANT
42-
// CHECK-DAG: FOO
39+
// CHECK: CONSTANT
40+
// CHECK: FOO
41+
// CHECK: FUNC_Macro
42+
// CHECK: REDEFINE
4343
// CHECK-NEXT: ===
4444

4545
//--- include_foo.h
4646
#include "foo.h"
4747
#undef REDEFINE
4848
// CHECK: Macro Definitions:
49-
// CHECK-DAG: CONSTANT
50-
// CHECK-DAG: FUNC_Macro
51-
// CHECK-DAG: FOO
49+
// CHECK: CONSTANT
50+
// CHECK: FOO
51+
// CHECK: FUNC_Macro
5252
// CHECK-NEXT: ===
5353

5454
//--- import_foo.h
5555
import "foo.h";
5656
#undef REDEFINE
5757
// CHECK: Macro Definitions:
58-
// CHECK-DAG: CONSTANT
59-
// CHECK-DAG: FUNC_Macro
60-
// CHECK-DAG: FOO
58+
// CHECK: CONSTANT
59+
// CHECK: FOO
60+
// CHECK: FUNC_Macro
6161
// CHECK-NEXT: ===
6262

6363
//--- named_module.cppm

0 commit comments

Comments
 (0)