Skip to content

Commit 27f0534

Browse files
erikdesjardinsjansvoboda11
authored andcommitted
[Clang] avoid relying on StringMap iteration order when roundtripping -analyzer-config
I am working on another patch that changes StringMap's hash function, which changes the iteration order here, and breaks some tests, specifically: clang/test/Analysis/NSString.m clang/test/Analysis/shallow-mode.m with errors like: generated arguments do not match in round-trip generated arguments #1 in round-trip: <...> "-analyzer-config" "ipa=inlining" "-analyzer-config" "max-nodes=75000" <...> generated arguments #2 in round-trip: <...> "-analyzer-config" "max-nodes=75000" "-analyzer-config" "ipa=inlining" <...> To avoid this, sort the options by key, instead of using the default map iteration order. Reviewed By: jansvoboda11, MaskRay Differential Revision: https://reviews.llvm.org/D142861 (cherry picked from commit 06be346)
1 parent 0f296f6 commit 27f0534

File tree

1 file changed

+10
-4
lines changed

1 file changed

+10
-4
lines changed

clang/lib/Frontend/CompilerInvocation.cpp

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -960,14 +960,20 @@ static void GenerateAnalyzerArgs(AnalyzerOptions &Opts,
960960
AnalyzerOptions ConfigOpts;
961961
parseAnalyzerConfigs(ConfigOpts, nullptr);
962962

963-
for (const auto &C : Opts.Config) {
963+
// Sort options by key to avoid relying on StringMap iteration order.
964+
SmallVector<std::pair<StringRef, StringRef>, 4> SortedConfigOpts;
965+
for (const auto &C : Opts.Config)
966+
SortedConfigOpts.emplace_back(C.getKey(), C.getValue());
967+
llvm::sort(SortedConfigOpts, llvm::less_first());
968+
969+
for (const auto &[Key, Value] : SortedConfigOpts) {
964970
// Don't generate anything that came from parseAnalyzerConfigs. It would be
965971
// redundant and may not be valid on the command line.
966-
auto Entry = ConfigOpts.Config.find(C.getKey());
967-
if (Entry != ConfigOpts.Config.end() && Entry->getValue() == C.getValue())
972+
auto Entry = ConfigOpts.Config.find(Key);
973+
if (Entry != ConfigOpts.Config.end() && Entry->getValue() == Value)
968974
continue;
969975

970-
GenerateArg(Args, OPT_analyzer_config, C.getKey() + "=" + C.getValue(), SA);
976+
GenerateArg(Args, OPT_analyzer_config, Key + "=" + Value, SA);
971977
}
972978

973979
// Nothing to generate for FullCompilerInvocation.

0 commit comments

Comments
 (0)