[clang-tools-extra] [clang-tidy] Add option 'IgnoredTypes' to bugprone-throwing-static-initialization (PR #192031)
Balázs Kéri via cfe-commits
cfe-commits at lists.llvm.org
Wed Apr 15 01:49:10 PDT 2026
https://github.com/balazske updated https://github.com/llvm/llvm-project/pull/192031
>From 3053c2a9516ca4d05181ead13c284955fe7bc0dc Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?Bal=C3=A1zs=20K=C3=A9ri?= <balazs.keri at ericsson.com>
Date: Mon, 13 Apr 2026 11:40:13 +0200
Subject: [PATCH 1/3] [clang-tidy] Add option 'IgnoredTypes' to
bugprone-throwing-static-initialization
---
.../ThrowingStaticInitializationCheck.cpp | 18 +++++++-
.../ThrowingStaticInitializationCheck.h | 7 ++-
clang-tools-extra/docs/ReleaseNotes.rst | 5 +++
.../throwing-static-initialization.rst | 13 ++++++
.../throwing-static-initialization-ignore.cpp | 44 +++++++++++++++++++
5 files changed, 84 insertions(+), 3 deletions(-)
create mode 100644 clang-tools-extra/test/clang-tidy/checkers/bugprone/throwing-static-initialization-ignore.cpp
diff --git a/clang-tools-extra/clang-tidy/bugprone/ThrowingStaticInitializationCheck.cpp b/clang-tools-extra/clang-tidy/bugprone/ThrowingStaticInitializationCheck.cpp
index 80905e260d5d4..6d0db9b08f651 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),
+ IgnoredTypes(
+ utils::options::parseStringList(Options.get("IgnoredTypes", ""))) {}
+
+void ThrowingStaticInitializationCheck::storeOptions(
+ ClangTidyOptions::OptionMap &Opts) {
+ Options.store(Opts, "IgnoredTypes",
+ utils::options::serializeStringList(IgnoredTypes));
+}
+
void ThrowingStaticInitializationCheck::registerMatchers(MatchFinder *Finder) {
// Match any static or thread_local variable declaration that has an
// initializer that can throw.
@@ -23,7 +37,9 @@ void ThrowingStaticInitializationCheck::registerMatchers(MatchFinder *Finder) {
varDecl(
anyOf(hasThreadStorageDuration(), hasStaticStorageDuration()),
unless(anyOf(isConstexpr(), hasType(cxxRecordDecl(isLambda())),
- hasAncestor(functionDecl()))),
+ hasAncestor(functionDecl()),
+ hasType(tidy::matchers::matchesAnyListedTypeName(
+ IgnoredTypes)))),
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..9652cfa4835b6 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> IgnoredTypes;
};
} // namespace clang::tidy::bugprone
diff --git a/clang-tools-extra/docs/ReleaseNotes.rst b/clang-tools-extra/docs/ReleaseNotes.rst
index caf0275035064..1787d4f9228fa 100644
--- a/clang-tools-extra/docs/ReleaseNotes.rst
+++ b/clang-tools-extra/docs/ReleaseNotes.rst
@@ -271,6 +271,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 `IgnoredTypes` 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..f9869b2285dfe 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:: IgnoredTypes
+
+This option makes it possible to ignore specific types used at variable
+declarations. It may contain a semicolon-separated list of regular expressions.
+Declarations with a type that is matched by this list are excluded from
+producing warnings by the check. The entries of the list are matched as
+substrings of the type name.
+
+This option contains by default an empty string.
+
References
----------
diff --git a/clang-tools-extra/test/clang-tidy/checkers/bugprone/throwing-static-initialization-ignore.cpp b/clang-tools-extra/test/clang-tidy/checkers/bugprone/throwing-static-initialization-ignore.cpp
new file mode 100644
index 0000000000000..097063846fc3a
--- /dev/null
+++ b/clang-tools-extra/test/clang-tidy/checkers/bugprone/throwing-static-initialization-ignore.cpp
@@ -0,0 +1,44 @@
+// RUN: %check_clang_tidy %s bugprone-throwing-static-initialization %t -- \
+// RUN: -config="{CheckOptions: \
+// RUN: {bugprone-throwing-static-initialization.IgnoredTypes: \"Ignore;^ns::S1$;^ns::Template<1>$\"}}" \
+// RUN: -- -fexceptions
+
+struct S1 {
+ S1() noexcept(false);
+};
+
+struct S1_Ignore {
+ S1_Ignore() noexcept(false);
+};
+
+namespace ns {
+struct S1 {
+ S1();
+};
+struct S1_Ignore {
+ S1_Ignore();
+};
+template<int>
+struct Template {
+ Template() noexcept(false);
+};
+}
+
+template<class>
+struct TemplateIgnored {
+ TemplateIgnored() noexcept(false);
+};
+
+S1_Ignore 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_Ignore VarIgnoreConstr;
+S1_Ignore VarIgnoreInitF = getS1();
+ns::S1 VarIgnore2;
+ns::S1_Ignore VarIgnore3;
+TemplateIgnored<int> VarIgnore4;
+ns::Template<1> VarIgnore5;
>From 1cd4791ec2bda46f6680fe9428cdfef0ead80e8c Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?Bal=C3=A1zs=20K=C3=A9ri?= <balazs.keri at ericsson.com>
Date: Tue, 14 Apr 2026 17:24:54 +0200
Subject: [PATCH 2/3] renamed option
---
.../ThrowingStaticInitializationCheck.cpp | 16 ++++++++--------
.../ThrowingStaticInitializationCheck.h | 2 +-
clang-tools-extra/docs/ReleaseNotes.rst | 2 +-
.../throwing-static-initialization.rst | 18 +++++++++---------
4 files changed, 19 insertions(+), 19 deletions(-)
diff --git a/clang-tools-extra/clang-tidy/bugprone/ThrowingStaticInitializationCheck.cpp b/clang-tools-extra/clang-tidy/bugprone/ThrowingStaticInitializationCheck.cpp
index 6d0db9b08f651..c8d16fec7002f 100644
--- a/clang-tools-extra/clang-tidy/bugprone/ThrowingStaticInitializationCheck.cpp
+++ b/clang-tools-extra/clang-tidy/bugprone/ThrowingStaticInitializationCheck.cpp
@@ -19,13 +19,13 @@ namespace clang::tidy::bugprone {
ThrowingStaticInitializationCheck::ThrowingStaticInitializationCheck(
StringRef Name, ClangTidyContext *Context)
: ClangTidyCheck(Name, Context),
- IgnoredTypes(
- utils::options::parseStringList(Options.get("IgnoredTypes", ""))) {}
+ AllowedTypes(
+ utils::options::parseStringList(Options.get("AllowedTypes", ""))) {}
void ThrowingStaticInitializationCheck::storeOptions(
ClangTidyOptions::OptionMap &Opts) {
- Options.store(Opts, "IgnoredTypes",
- utils::options::serializeStringList(IgnoredTypes));
+ Options.store(Opts, "AllowedTypes",
+ utils::options::serializeStringList(AllowedTypes));
}
void ThrowingStaticInitializationCheck::registerMatchers(MatchFinder *Finder) {
@@ -36,10 +36,10 @@ void ThrowingStaticInitializationCheck::registerMatchers(MatchFinder *Finder) {
TK_AsIs,
varDecl(
anyOf(hasThreadStorageDuration(), hasStaticStorageDuration()),
- unless(anyOf(isConstexpr(), hasType(cxxRecordDecl(isLambda())),
- hasAncestor(functionDecl()),
- hasType(tidy::matchers::matchesAnyListedTypeName(
- IgnoredTypes)))),
+ 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 9652cfa4835b6..44ba61bcc99ba 100644
--- a/clang-tools-extra/clang-tidy/bugprone/ThrowingStaticInitializationCheck.h
+++ b/clang-tools-extra/clang-tidy/bugprone/ThrowingStaticInitializationCheck.h
@@ -29,7 +29,7 @@ class ThrowingStaticInitializationCheck : public ClangTidyCheck {
void check(const ast_matchers::MatchFinder::MatchResult &Result) override;
private:
- const std::vector<StringRef> IgnoredTypes;
+ 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 1787d4f9228fa..3c123b50aa536 100644
--- a/clang-tools-extra/docs/ReleaseNotes.rst
+++ b/clang-tools-extra/docs/ReleaseNotes.rst
@@ -273,7 +273,7 @@ Changes in existing checks
- Improved :doc:`bugprone-throwing-static-initialization
<clang-tidy/checks/bugprone/throwing-static-initialization>` check by adding
- the `IgnoredTypes` option. With this option it is possible to exclude
+ 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
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 f9869b2285dfe..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
@@ -9,15 +9,15 @@ initializer for the object may throw an exception.
Options
-------
-.. option:: IgnoredTypes
-
-This option makes it possible to ignore specific types used at variable
-declarations. It may contain a semicolon-separated list of regular expressions.
-Declarations with a type that is matched by this list are excluded from
-producing warnings by the check. The entries of the list are matched as
-substrings of the type name.
-
-This option contains by default an empty string.
+.. 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
----------
>From e8a1616f456d7757eaa62cb91447edcbaec7fd31 Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?Bal=C3=A1zs=20K=C3=A9ri?= <balazs.keri at ericsson.com>
Date: Wed, 15 Apr 2026 10:47:56 +0200
Subject: [PATCH 3/3] fixed test
---
... throwing-static-initialization-allow.cpp} | 28 +++++++++----------
1 file changed, 14 insertions(+), 14 deletions(-)
rename clang-tools-extra/test/clang-tidy/checkers/bugprone/{throwing-static-initialization-ignore.cpp => throwing-static-initialization-allow.cpp} (58%)
diff --git a/clang-tools-extra/test/clang-tidy/checkers/bugprone/throwing-static-initialization-ignore.cpp b/clang-tools-extra/test/clang-tidy/checkers/bugprone/throwing-static-initialization-allow.cpp
similarity index 58%
rename from clang-tools-extra/test/clang-tidy/checkers/bugprone/throwing-static-initialization-ignore.cpp
rename to clang-tools-extra/test/clang-tidy/checkers/bugprone/throwing-static-initialization-allow.cpp
index 097063846fc3a..d7bfd61a49f2f 100644
--- a/clang-tools-extra/test/clang-tidy/checkers/bugprone/throwing-static-initialization-ignore.cpp
+++ b/clang-tools-extra/test/clang-tidy/checkers/bugprone/throwing-static-initialization-allow.cpp
@@ -1,22 +1,22 @@
// RUN: %check_clang_tidy %s bugprone-throwing-static-initialization %t -- \
// RUN: -config="{CheckOptions: \
-// RUN: {bugprone-throwing-static-initialization.IgnoredTypes: \"Ignore;^ns::S1$;^ns::Template<1>$\"}}" \
+// RUN: {bugprone-throwing-static-initialization.AllowedTypes: \"Allow;^ns::S1$;^ns::Template<1>$\"}}" \
// RUN: -- -fexceptions
struct S1 {
S1() noexcept(false);
};
-struct S1_Ignore {
- S1_Ignore() noexcept(false);
+struct S1_Allow {
+ S1_Allow() noexcept(false);
};
namespace ns {
struct S1 {
S1();
};
-struct S1_Ignore {
- S1_Ignore();
+struct S1_Allow {
+ S1_Allow();
};
template<int>
struct Template {
@@ -25,20 +25,20 @@ struct Template {
}
template<class>
-struct TemplateIgnored {
- TemplateIgnored() noexcept(false);
+struct TemplateAllowed {
+ TemplateAllowed() noexcept(false);
};
-S1_Ignore getS1() 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_Ignore VarIgnoreConstr;
-S1_Ignore VarIgnoreInitF = getS1();
-ns::S1 VarIgnore2;
-ns::S1_Ignore VarIgnore3;
-TemplateIgnored<int> VarIgnore4;
-ns::Template<1> VarIgnore5;
+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