[clang] NFCI: Optimize storage and lookup of analyzer options
authorJan Svoboda <jan_svoboda@apple.com>
Fri, 21 Apr 2023 15:58:36 +0000 (08:58 -0700)
committerJan Svoboda <jan_svoboda@apple.com>
Fri, 21 Apr 2023 17:39:55 +0000 (10:39 -0700)
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

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

index e81d7bbb8823e0f61973151ccff260316bc6936a..a947bd0867025c2087a6278d3c13b2cbda33bba1 100644 (file)
@@ -260,9 +260,10 @@ public:
 #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)
@@ -273,10 +274,11 @@ public:
 #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);
@@ -292,9 +294,7 @@ public:
         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.