https://github.com/zeyi2 updated 
https://github.com/llvm/llvm-project/pull/174744

>From e60489e41e6f8b7f6c4eaf59c97e58c0948866b6 Mon Sep 17 00:00:00 2001
From: mtx <[email protected]>
Date: Tue, 6 Jan 2026 18:08:53 +0800
Subject: [PATCH] [clang-tidy] Support C++20 constinit in
 bugprone-dynamic-static-initializers

---
 .../DynamicStaticInitializersCheck.cpp        |  4 ++
 clang-tools-extra/docs/ReleaseNotes.rst       | 21 ++++---
 .../dynamic-static-initializers-constinit.hpp | 56 +++++++++++++++++++
 3 files changed, 72 insertions(+), 9 deletions(-)
 create mode 100644 
clang-tools-extra/test/clang-tidy/checkers/bugprone/dynamic-static-initializers-constinit.hpp

diff --git 
a/clang-tools-extra/clang-tidy/bugprone/DynamicStaticInitializersCheck.cpp 
b/clang-tools-extra/clang-tidy/bugprone/DynamicStaticInitializersCheck.cpp
index d00c8d0ffb925..fb4f59cc40996 100644
--- a/clang-tools-extra/clang-tidy/bugprone/DynamicStaticInitializersCheck.cpp
+++ b/clang-tools-extra/clang-tidy/bugprone/DynamicStaticInitializersCheck.cpp
@@ -20,6 +20,10 @@ namespace {
 AST_MATCHER(clang::VarDecl, hasConstantDeclaration) {
   if (Node.isConstexpr())
     return true;
+  if (llvm::any_of(Node.redecls(), [](const VarDecl *D) {
+        return D->hasAttr<ConstInitAttr>();
+      }))
+    return true;
   const Expr *Init = Node.getInit();
   if (Init && !Init->isValueDependent())
     return Node.evaluateValue();
diff --git a/clang-tools-extra/docs/ReleaseNotes.rst 
b/clang-tools-extra/docs/ReleaseNotes.rst
index 391c3a6b3db79..c6049a025525c 100644
--- a/clang-tools-extra/docs/ReleaseNotes.rst
+++ b/clang-tools-extra/docs/ReleaseNotes.rst
@@ -111,10 +111,10 @@ Hover
 Code completion
 ^^^^^^^^^^^^^^^
 
-- Added a new ``MacroFilter`` configuration option to ``Completion`` to 
-  allow fuzzy-matching with the ``FuzzyMatch`` option when suggesting 
-  macros. ``ExactPrefix`` is the default, which retains previous 
-  behavior of suggesting macros which match the prefix exactly.  
+- Added a new ``MacroFilter`` configuration option to ``Completion`` to
+  allow fuzzy-matching with the ``FuzzyMatch`` option when suggesting
+  macros. ``ExactPrefix`` is the default, which retains previous
+  behavior of suggesting macros which match the prefix exactly.
 
 Code actions
 ^^^^^^^^^^^^
@@ -205,7 +205,7 @@ Improvements to clang-tidy
 
 - Improved :program:`clang-tidy` by adding the `--removed-arg` option to remove
   arguments sent to the compiler when invoking Clang-Tidy. This option was also
-  added to :program:`run-clang-tidy.py` and :program:`clang-tidy-diff.py` and 
+  added to :program:`run-clang-tidy.py` and :program:`clang-tidy-diff.py` and
   can be configured in the config file through the `RemovedArgs` option.
 
 - Deprecated the :program:`clang-tidy` ``zircon`` module. All checks have been
@@ -364,7 +364,7 @@ New check aliases
   keeping initial check as an alias to the new one.
 
 - Renamed :doc:`google-build-namespaces 
<clang-tidy/checks/google/build-namespaces>` to
-  :doc:`misc-anonymous-namespace-in-header 
+  :doc:`misc-anonymous-namespace-in-header
   <clang-tidy/checks/misc/anonymous-namespace-in-header>`
   keeping initial check as an alias to the new one.
 
@@ -382,9 +382,12 @@ Changes in existing checks
   expansions.
 
 - Improved :doc:`bugprone-dynamic-static-initializers
-  <clang-tidy/checks/bugprone/dynamic-static-initializers>` check by
-  avoiding false positives for ``constexpr`` variables whose
-  initializers are value-dependent.
+  <clang-tidy/checks/bugprone/dynamic-static-initializers>` check:
+
+  - Avoided false positives for ``constexpr`` variables whose initializers
+    are value-dependent.
+
+  - Added support for C++20 ``constinit`` variables.
 
 - Improved :doc:`bugprone-easily-swappable-parameters
   <clang-tidy/checks/bugprone/easily-swappable-parameters>` check by
diff --git 
a/clang-tools-extra/test/clang-tidy/checkers/bugprone/dynamic-static-initializers-constinit.hpp
 
b/clang-tools-extra/test/clang-tidy/checkers/bugprone/dynamic-static-initializers-constinit.hpp
new file mode 100644
index 0000000000000..8122ce1e46467
--- /dev/null
+++ 
b/clang-tools-extra/test/clang-tidy/checkers/bugprone/dynamic-static-initializers-constinit.hpp
@@ -0,0 +1,56 @@
+// RUN: %check_clang_tidy -std=c++20 %s bugprone-dynamic-static-initializers 
%t -- -- -fno-threadsafe-statics
+
+constexpr int const_func() { return 42; }
+
+constinit int a = const_func(); // no warning
+
+constinit int b = 123; // no warning
+
+struct S {
+  static constinit int c;
+};
+constinit int S::c = const_func(); // no warning
+
+int runtime_func() { return 42; }
+
+int e = runtime_func();
+// CHECK-MESSAGES: :[[@LINE-1]]:5: warning: static variable 'e' may be 
dynamically initialized in this header file 
[bugprone-dynamic-static-initializers]
+
+template <typename T>
+struct TemplateS {
+  static constinit int x;
+};
+template <typename T>
+constinit int TemplateS<T>::x = const_func(); // no warning
+
+template <typename T>
+void template_func() {
+  static constinit int v = const_func(); // no warning
+}
+
+void call_template_func() {
+  template_func<int>();
+}
+
+template <int V>
+struct Value {
+  static constinit int v;
+};
+template <int V>
+constinit int Value<V>::v = V; // no warning
+
+thread_local constinit int tl = const_func(); // no warning
+
+struct InlineS {
+  static inline constinit int i = 42; // no warning
+};
+
+auto lambda = []() {
+  static constinit int l = const_func(); // no warning
+  return l;
+};
+
+struct Separate {
+  static constinit int s;
+};
+constinit int Separate::s = 100; // no warning

_______________________________________________
cfe-commits mailing list
[email protected]
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits

Reply via email to