Skip to content

Commit 06c4ba1

Browse files
committed
[clang] NFCI: Optimize storage and lookup of analyzer options
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 (cherry picked from commit 53a4a2b)
1 parent 21ed5aa commit 06c4ba1

File tree

1 file changed

+10
-10
lines changed

1 file changed

+10
-10
lines changed

clang/include/clang/StaticAnalyzer/Core/AnalyzerOptions.h

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -261,9 +261,10 @@ class AnalyzerOptions : public RefCountedBase<AnalyzerOptions> {
261261
#undef ANALYZER_OPTION
262262
#undef ANALYZER_OPTION_DEPENDS_ON_USER_MODE
263263

264-
// Create an array of all -analyzer-config command line options. Sort it in
265-
// the constructor.
266-
std::vector<llvm::StringLiteral> AnalyzerConfigCmdFlags = {
264+
bool isUnknownAnalyzerConfig(llvm::StringRef Name) {
265+
static std::vector<llvm::StringLiteral> AnalyzerConfigCmdFlags = []() {
266+
// Create an array of all -analyzer-config command line options.
267+
std::vector<llvm::StringLiteral> AnalyzerConfigCmdFlags = {
267268
#define ANALYZER_OPTION_DEPENDS_ON_USER_MODE(TYPE, NAME, CMDFLAG, DESC, \
268269
SHALLOW_VAL, DEEP_VAL) \
269270
ANALYZER_OPTION(TYPE, NAME, CMDFLAG, DESC, SHALLOW_VAL)
@@ -274,10 +275,11 @@ class AnalyzerOptions : public RefCountedBase<AnalyzerOptions> {
274275
#include "clang/StaticAnalyzer/Core/AnalyzerOptions.def"
275276
#undef ANALYZER_OPTION
276277
#undef ANALYZER_OPTION_DEPENDS_ON_USER_MODE
277-
};
278-
279-
bool isUnknownAnalyzerConfig(StringRef Name) const {
280-
assert(llvm::is_sorted(AnalyzerConfigCmdFlags));
278+
};
279+
// FIXME: Sort this at compile-time when we get constexpr sort (C++20).
280+
llvm::sort(AnalyzerConfigCmdFlags);
281+
return AnalyzerConfigCmdFlags;
282+
}();
281283

282284
return !std::binary_search(AnalyzerConfigCmdFlags.begin(),
283285
AnalyzerConfigCmdFlags.end(), Name);
@@ -293,9 +295,7 @@ class AnalyzerOptions : public RefCountedBase<AnalyzerOptions> {
293295
AnalyzerDisplayProgress(false), eagerlyAssumeBinOpBifurcation(false),
294296
TrimGraph(false), visualizeExplodedGraphWithGraphViz(false),
295297
UnoptimizedCFG(false), PrintStats(false), NoRetryExhausted(false),
296-
AnalyzerWerror(false) {
297-
llvm::sort(AnalyzerConfigCmdFlags);
298-
}
298+
AnalyzerWerror(false) {}
299299

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

0 commit comments

Comments
 (0)