Skip to content

Commit 53a4a2b

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
1 parent 33fe899 commit 53a4a2b

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
@@ -260,9 +260,10 @@ class AnalyzerOptions : public RefCountedBase<AnalyzerOptions> {
260260
#undef ANALYZER_OPTION
261261
#undef ANALYZER_OPTION_DEPENDS_ON_USER_MODE
262262

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

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

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

0 commit comments

Comments
 (0)