[clang-tools-extra] 169148a - [clang-tidy] Add option 'AllowedTypes' to bugprone-throwing-static-initialization (#192031)

via cfe-commits cfe-commits at lists.llvm.org
Thu Apr 16 01:30:05 PDT 2026


Author: Balázs Kéri
Date: 2026-04-16T10:30:00+02:00
New Revision: 169148a6b21471ddc06b99bc7adabae552775b57

URL: https://github.com/llvm/llvm-project/commit/169148a6b21471ddc06b99bc7adabae552775b57
DIFF: https://github.com/llvm/llvm-project/commit/169148a6b21471ddc06b99bc7adabae552775b57.diff

LOG: [clang-tidy] Add option 'AllowedTypes' to bugprone-throwing-static-initialization (#192031)

Added: 
    clang-tools-extra/test/clang-tidy/checkers/bugprone/throwing-static-initialization-allow.cpp

Modified: 
    clang-tools-extra/clang-tidy/bugprone/ThrowingStaticInitializationCheck.cpp
    clang-tools-extra/clang-tidy/bugprone/ThrowingStaticInitializationCheck.h
    clang-tools-extra/docs/ReleaseNotes.rst
    clang-tools-extra/docs/clang-tidy/checks/bugprone/throwing-static-initialization.rst

Removed: 
    


################################################################################
diff  --git a/clang-tools-extra/clang-tidy/bugprone/ThrowingStaticInitializationCheck.cpp b/clang-tools-extra/clang-tidy/bugprone/ThrowingStaticInitializationCheck.cpp
index 80905e260d5d4..c8d16fec7002f 100644
--- a/clang-tools-extra/clang-tidy/bugprone/ThrowingStaticInitializationCheck.cpp
+++ b/clang-tools-extra/clang-tidy/bugprone/ThrowingStaticInitializationCheck.cpp
@@ -7,6 +7,8 @@
 //===----------------------------------------------------------------------===//
 
 #include "ThrowingStaticInitializationCheck.h"
+#include "../utils/Matchers.h"
+#include "../utils/OptionsUtils.h"
 #include "clang/AST/ASTContext.h"
 #include "clang/ASTMatchers/ASTMatchFinder.h"
 
@@ -14,6 +16,18 @@ using namespace clang::ast_matchers;
 
 namespace clang::tidy::bugprone {
 
+ThrowingStaticInitializationCheck::ThrowingStaticInitializationCheck(
+    StringRef Name, ClangTidyContext *Context)
+    : ClangTidyCheck(Name, Context),
+      AllowedTypes(
+          utils::options::parseStringList(Options.get("AllowedTypes", ""))) {}
+
+void ThrowingStaticInitializationCheck::storeOptions(
+    ClangTidyOptions::OptionMap &Opts) {
+  Options.store(Opts, "AllowedTypes",
+                utils::options::serializeStringList(AllowedTypes));
+}
+
 void ThrowingStaticInitializationCheck::registerMatchers(MatchFinder *Finder) {
   // Match any static or thread_local variable declaration that has an
   // initializer that can throw.
@@ -22,8 +36,10 @@ void ThrowingStaticInitializationCheck::registerMatchers(MatchFinder *Finder) {
           TK_AsIs,
           varDecl(
               anyOf(hasThreadStorageDuration(), hasStaticStorageDuration()),
-              unless(anyOf(isConstexpr(), hasType(cxxRecordDecl(isLambda())),
-                           hasAncestor(functionDecl()))),
+              unless(anyOf(
+                  isConstexpr(), hasType(cxxRecordDecl(isLambda())),
+                  hasAncestor(functionDecl()),
+                  hasType(matchers::matchesAnyListedTypeName(AllowedTypes)))),
               anyOf(hasDescendant(cxxConstructExpr(hasDeclaration(
                         cxxConstructorDecl(unless(isNoThrow())).bind("func")))),
                     hasDescendant(cxxNewExpr(hasDeclaration(

diff  --git a/clang-tools-extra/clang-tidy/bugprone/ThrowingStaticInitializationCheck.h b/clang-tools-extra/clang-tidy/bugprone/ThrowingStaticInitializationCheck.h
index a25d7fe889e16..44ba61bcc99ba 100644
--- a/clang-tools-extra/clang-tidy/bugprone/ThrowingStaticInitializationCheck.h
+++ b/clang-tools-extra/clang-tidy/bugprone/ThrowingStaticInitializationCheck.h
@@ -20,13 +20,16 @@ namespace clang::tidy::bugprone {
 /// https://clang.llvm.org/extra/clang-tidy/checks/bugprone/throwing-static-initialization.html
 class ThrowingStaticInitializationCheck : public ClangTidyCheck {
 public:
-  ThrowingStaticInitializationCheck(StringRef Name, ClangTidyContext *Context)
-      : ClangTidyCheck(Name, Context) {}
+  ThrowingStaticInitializationCheck(StringRef Name, ClangTidyContext *Context);
+  void storeOptions(ClangTidyOptions::OptionMap &Opts) override;
   bool isLanguageVersionSupported(const LangOptions &LangOpts) const override {
     return LangOpts.CPlusPlus && LangOpts.CXXExceptions;
   }
   void registerMatchers(ast_matchers::MatchFinder *Finder) override;
   void check(const ast_matchers::MatchFinder::MatchResult &Result) override;
+
+private:
+  const std::vector<StringRef> AllowedTypes;
 };
 
 } // namespace clang::tidy::bugprone

diff  --git a/clang-tools-extra/docs/ReleaseNotes.rst b/clang-tools-extra/docs/ReleaseNotes.rst
index 3d6235cb3a78c..3d126910d2e2e 100644
--- a/clang-tools-extra/docs/ReleaseNotes.rst
+++ b/clang-tools-extra/docs/ReleaseNotes.rst
@@ -286,6 +286,11 @@ Changes in existing checks
   string constructor calls when the string class constructor has a default
   allocator argument.
 
+- Improved :doc:`bugprone-throwing-static-initialization
+  <clang-tidy/checks/bugprone/throwing-static-initialization>` check by adding
+  the `AllowedTypes` option. With this option it is possible to exclude
+  static declarations with specific types from the check.
+
 - Improved :doc:`bugprone-unchecked-optional-access
   <clang-tidy/checks/bugprone/unchecked-optional-access>` to recognize common
   GoogleTest macros such as ``ASSERT_TRUE`` and ``ASSERT_FALSE``, reducing the

diff  --git a/clang-tools-extra/docs/clang-tidy/checks/bugprone/throwing-static-initialization.rst b/clang-tools-extra/docs/clang-tidy/checks/bugprone/throwing-static-initialization.rst
index 4f88719dd6f5c..0bbae1e8571f1 100644
--- a/clang-tools-extra/docs/clang-tidy/checks/bugprone/throwing-static-initialization.rst
+++ b/clang-tools-extra/docs/clang-tidy/checks/bugprone/throwing-static-initialization.rst
@@ -6,6 +6,19 @@ bugprone-throwing-static-initialization
 Finds all ``static`` or ``thread_local`` variable declarations where the
 initializer for the object may throw an exception.
 
+Options
+-------
+
+.. option:: AllowedTypes
+
+  A semicolon-separated list of names of types that will be excluded from
+  this check (declarations with matching type will be excluded). Regular
+  expressions are accepted, e.g. ``[Rr]ef(erence)?$`` matches every type with
+  suffix ``Ref``, ``ref``, ``Reference`` and ``reference``. If a name in the
+  list contains the sequence `::`, it is matched against the qualified type
+  name (i.e. ``namespace::Type``), otherwise it is matched against only the
+  type name (i.e. ``Type``). Default is an empty string.
+
 References
 ----------
 

diff  --git a/clang-tools-extra/test/clang-tidy/checkers/bugprone/throwing-static-initialization-allow.cpp b/clang-tools-extra/test/clang-tidy/checkers/bugprone/throwing-static-initialization-allow.cpp
new file mode 100644
index 0000000000000..d7bfd61a49f2f
--- /dev/null
+++ b/clang-tools-extra/test/clang-tidy/checkers/bugprone/throwing-static-initialization-allow.cpp
@@ -0,0 +1,44 @@
+// RUN: %check_clang_tidy %s bugprone-throwing-static-initialization %t -- \
+// RUN:   -config="{CheckOptions: \
+// RUN:             {bugprone-throwing-static-initialization.AllowedTypes: \"Allow;^ns::S1$;^ns::Template<1>$\"}}" \
+// RUN:   -- -fexceptions
+
+struct S1 {
+  S1() noexcept(false);
+};
+
+struct S1_Allow {
+  S1_Allow() noexcept(false);
+};
+
+namespace ns {
+struct S1 {
+  S1();
+};
+struct S1_Allow {
+  S1_Allow();
+};
+template<int>
+struct Template {
+  Template() noexcept(false);
+};
+}
+
+template<class>
+struct TemplateAllowed {
+  TemplateAllowed() noexcept(false);
+};
+
+S1_Allow getS1() noexcept(false);
+
+S1 VarThrow;
+// CHECK-MESSAGES: :[[@LINE-1]]:4: warning: initialization of 'VarThrow' with static storage duration may throw an exception that cannot be caught
+ns::Template<2> VarTempl;
+// CHECK-MESSAGES: :[[@LINE-1]]:17: warning: initialization of 'VarTempl' with static storage duration may throw an exception that cannot be caught
+
+S1_Allow VarAllowedConstr;
+S1_Allow VarAllowedInitF = getS1();
+ns::S1 VarAllowed2;
+ns::S1_Allow VarAllowed3;
+TemplateAllowed<int> VarAllowed4;
+ns::Template<1> VarAllowed5;


        


More information about the cfe-commits mailing list