[clang-tools-extra] [clang-tidy][NFC] Do more work at compile time (PR #147406)

via cfe-commits cfe-commits at lists.llvm.org
Mon Jul 7 14:46:21 PDT 2025


llvmbot wrote:


<!--LLVM PR SUMMARY COMMENT-->

@llvm/pr-subscribers-clang-tidy

Author: Victor Chernyakin (localspook)

<details>
<summary>Changes</summary>

```cpp
  for (std::string OptionName : {
#define GET_CHECKER_OPTIONS
#define CHECKER_OPTION(TYPE, CHECKER, OPTION_NAME, DESCRIPTION, DEFAULT,       \
                       RELEASE, HIDDEN)                                        \
  Twine(AnalyzerCheckNamePrefix).concat(CHECKER ":" OPTION_NAME).str(),

#include "clang/StaticAnalyzer/Checkers/Checkers.inc"
#undef CHECKER_OPTION
#undef GET_CHECKER_OPTIONS
       }) {
    Result.Options.insert(OptionName);
  }
```
This code is doing a lot of unnecessary work at runtime. For each of the (currently) 59 checker options, it runs `Twine(AnalyzerCheckNamePrefix).concat(CHECKER ":" OPTION_NAME).str(),`, which allocates a string (all of this is unrolled, leading to code bloat). Then it copies all those strings (because `std::string OptionName`, not `const std::string& OptionName`). All of this can be done at compile time!

---
Full diff: https://github.com/llvm/llvm-project/pull/147406.diff


1 Files Affected:

- (modified) clang-tools-extra/clang-tidy/ClangTidy.cpp (+6-5) 


``````````diff
diff --git a/clang-tools-extra/clang-tidy/ClangTidy.cpp b/clang-tools-extra/clang-tidy/ClangTidy.cpp
index f4ab93b51f4a7..815033c2e5175 100644
--- a/clang-tools-extra/clang-tidy/ClangTidy.cpp
+++ b/clang-tools-extra/clang-tidy/ClangTidy.cpp
@@ -668,18 +668,19 @@ getAllChecksAndOptions(bool AllowEnablingAnalyzerAlphaCheckers) {
     Buffer.append(AnalyzerCheck);
     Result.Checks.insert(Buffer);
   }
-  for (std::string OptionName : {
+
+  static constexpr llvm::StringLiteral OptionNames[] = {
 #define GET_CHECKER_OPTIONS
 #define CHECKER_OPTION(TYPE, CHECKER, OPTION_NAME, DESCRIPTION, DEFAULT,       \
                        RELEASE, HIDDEN)                                        \
-  Twine(AnalyzerCheckNamePrefix).concat(CHECKER ":" OPTION_NAME).str(),
+  "clang-analyzer-" CHECKER ":" OPTION_NAME,
 
 #include "clang/StaticAnalyzer/Checkers/Checkers.inc"
 #undef CHECKER_OPTION
 #undef GET_CHECKER_OPTIONS
-       }) {
-    Result.Options.insert(OptionName);
-  }
+  };
+
+  Result.Options.insert_range(OptionNames);
 #endif // CLANG_TIDY_ENABLE_STATIC_ANALYZER
 
   Context.setOptionsCollector(&Result.Options);

``````````

</details>


https://github.com/llvm/llvm-project/pull/147406


More information about the cfe-commits mailing list