Skip to content

Commit 06be346

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
1 parent aa29435 commit 06be346

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
@@ -877,14 +877,20 @@ static void GenerateAnalyzerArgs(AnalyzerOptions &Opts,
877877
AnalyzerOptions ConfigOpts;
878878
parseAnalyzerConfigs(ConfigOpts, nullptr);
879879

880-
for (const auto &C : Opts.Config) {
880+
// Sort options by key to avoid relying on StringMap iteration order.
881+
SmallVector<std::pair<StringRef, StringRef>, 4> SortedConfigOpts;
882+
for (const auto &C : Opts.Config)
883+
SortedConfigOpts.emplace_back(C.getKey(), C.getValue());
884+
llvm::sort(SortedConfigOpts, llvm::less_first());
885+
886+
for (const auto &[Key, Value] : SortedConfigOpts) {
881887
// Don't generate anything that came from parseAnalyzerConfigs. It would be
882888
// redundant and may not be valid on the command line.
883-
auto Entry = ConfigOpts.Config.find(C.getKey());
884-
if (Entry != ConfigOpts.Config.end() && Entry->getValue() == C.getValue())
889+
auto Entry = ConfigOpts.Config.find(Key);
890+
if (Entry != ConfigOpts.Config.end() && Entry->getValue() == Value)
885891
continue;
886892

887-
GenerateArg(Args, OPT_analyzer_config, C.getKey() + "=" + C.getValue(), SA);
893+
GenerateArg(Args, OPT_analyzer_config, Key + "=" + Value, SA);
888894
}
889895

890896
// Nothing to generate for FullCompilerInvocation.

0 commit comments

Comments
 (0)