Skip to content

Commit

Permalink
[clang] NFCI: Optimize storage and lookup of analyzer options
Browse files Browse the repository at this point in the history
This patch moves `llvm::sort()` from `AnalyzerOptions` constructor to initialization of local static variable in `isUnknownAnalyzerConfig()`.

This avoids unnecessary work, which can speed up Clang tools that initialize lots of `CompilerInvocation`s (and therefore `AnalyzerOptions`).

Reviewed By: steakhal

Differential Revision: https://reviews.llvm.org/D137258
  • Loading branch information
jansvoboda11 committed Apr 21, 2023
1 parent 33fe899 commit 53a4a2b
Showing 1 changed file with 10 additions and 10 deletions.
20 changes: 10 additions & 10 deletions clang/include/clang/StaticAnalyzer/Core/AnalyzerOptions.h
Expand Up @@ -260,9 +260,10 @@ class AnalyzerOptions : public RefCountedBase<AnalyzerOptions> {
#undef ANALYZER_OPTION
#undef ANALYZER_OPTION_DEPENDS_ON_USER_MODE

// Create an array of all -analyzer-config command line options. Sort it in
// the constructor.
std::vector<llvm::StringLiteral> AnalyzerConfigCmdFlags = {
bool isUnknownAnalyzerConfig(llvm::StringRef Name) {
static std::vector<llvm::StringLiteral> AnalyzerConfigCmdFlags = []() {
// Create an array of all -analyzer-config command line options.
std::vector<llvm::StringLiteral> AnalyzerConfigCmdFlags = {
#define ANALYZER_OPTION_DEPENDS_ON_USER_MODE(TYPE, NAME, CMDFLAG, DESC, \
SHALLOW_VAL, DEEP_VAL) \
ANALYZER_OPTION(TYPE, NAME, CMDFLAG, DESC, SHALLOW_VAL)
Expand All @@ -273,10 +274,11 @@ class AnalyzerOptions : public RefCountedBase<AnalyzerOptions> {
#include "clang/StaticAnalyzer/Core/AnalyzerOptions.def"
#undef ANALYZER_OPTION
#undef ANALYZER_OPTION_DEPENDS_ON_USER_MODE
};

bool isUnknownAnalyzerConfig(StringRef Name) const {
assert(llvm::is_sorted(AnalyzerConfigCmdFlags));
};
// FIXME: Sort this at compile-time when we get constexpr sort (C++20).
llvm::sort(AnalyzerConfigCmdFlags);
return AnalyzerConfigCmdFlags;
}();

return !std::binary_search(AnalyzerConfigCmdFlags.begin(),
AnalyzerConfigCmdFlags.end(), Name);
Expand All @@ -292,9 +294,7 @@ class AnalyzerOptions : public RefCountedBase<AnalyzerOptions> {
AnalyzerDisplayProgress(false), eagerlyAssumeBinOpBifurcation(false),
TrimGraph(false), visualizeExplodedGraphWithGraphViz(false),
UnoptimizedCFG(false), PrintStats(false), NoRetryExhausted(false),
AnalyzerWerror(false) {
llvm::sort(AnalyzerConfigCmdFlags);
}
AnalyzerWerror(false) {}

/// Interprets an option's string value as a boolean. The "true" string is
/// interpreted as true and the "false" string is interpreted as false.
Expand Down

0 comments on commit 53a4a2b

Please sign in to comment.