[clang] Include [[clang::require_explicit_initialization]] warnings in system headers (PR #141133)

via cfe-commits cfe-commits at lists.llvm.org
Fri May 23 10:14:22 PDT 2025


https://github.com/higher-performance updated https://github.com/llvm/llvm-project/pull/141133

>From 00e0cf90f1d53ec8b2472bd740066016ee88f584 Mon Sep 17 00:00:00 2001
From: higher-performance <higher.performance.github at gmail.com>
Date: Thu, 22 May 2025 16:30:29 -0400
Subject: [PATCH] Include [[clang::require_explicit_initialization]] warnings
 in system headers

Fixes #141103
---
 .../clang/Basic/DiagnosticSemaKinds.td        |  3 ++-
 clang/test/SemaCXX/uninitialized.cpp          | 23 +++++++++++++++++--
 2 files changed, 23 insertions(+), 3 deletions(-)

diff --git a/clang/include/clang/Basic/DiagnosticSemaKinds.td b/clang/include/clang/Basic/DiagnosticSemaKinds.td
index 78b36ceb88125..8a3aafeabdad3 100644
--- a/clang/include/clang/Basic/DiagnosticSemaKinds.td
+++ b/clang/include/clang/Basic/DiagnosticSemaKinds.td
@@ -2423,7 +2423,8 @@ def note_uninit_reference_member : Note<
   "uninitialized reference member is here">;
 def warn_field_requires_explicit_init : Warning<
   "field %select{%1|in %1}0 requires explicit initialization but is not "
-   "explicitly initialized">, InGroup<UninitializedExplicitInit>;
+   "explicitly initialized">, InGroup<UninitializedExplicitInit>,
+   ShowInSystemHeader;
 def warn_field_is_uninit : Warning<"field %0 is uninitialized when used here">,
   InGroup<Uninitialized>;
 def warn_base_class_is_uninit : Warning<
diff --git a/clang/test/SemaCXX/uninitialized.cpp b/clang/test/SemaCXX/uninitialized.cpp
index c7b987e2172e6..6986a6f0b77b0 100644
--- a/clang/test/SemaCXX/uninitialized.cpp
+++ b/clang/test/SemaCXX/uninitialized.cpp
@@ -2,6 +2,23 @@
 // RUN: %clang_cc1 -fsyntax-only -Wall -Wc++20-compat -Wuninitialized -Wno-unused-value -Wno-unused-lambda-capture -Wno-uninitialized-const-reference -std=c++1z -verify %s -fexperimental-new-constant-interpreter
 // RUN: %clang_cc1 -fsyntax-only -Wall -Wc++20-compat -Wuninitialized -Wno-unused-value -Wno-unused-lambda-capture -Wno-uninitialized-const-reference -std=c++20 -verify %s
 
+#if defined(BE_THE_HEADER)
+
+// Wuninitialized-explicit-init should warn in system headers (std::construct_at...) too.
+
+#pragma clang system_header
+namespace std {
+template <class T, class... U>
+constexpr T* construct_at(T* ptr, U&&... args) {
+  return ::new (static_cast<void*>(ptr)) T(std::forward<U>(args)...);
+}
+}
+
+#else
+
+#define BE_THE_HEADER
+#include __FILE__
+
 void* operator new(__SIZE_TYPE__, void*);
 
 // definitions for std::move
@@ -1565,10 +1582,10 @@ void aggregate() {
       // expected-warning at +1 {{field in 'Embed' requires explicit initialization but is not explicitly initialized}} expected-note@#FIELD_EMBED2 {{'embed2' declared here}}
       ::new(static_cast<void*>(&f1)) decltype(f1);
       // expected-warning at +1 {{field in 'Embed' requires explicit initialization but is not explicitly initialized}} expected-note@#FIELD_EMBED2 {{'embed2' declared here}}
-      ::new(static_cast<void*>(&f1)) decltype(f1)();
+      std::construct_at(&f1);
 #if __cplusplus >= 202002L
       // expected-warning at +1 {{field 'embed2' requires explicit initialization but is not explicitly initialized}} expected-note@#FIELD_EMBED2 {{'embed2' declared here}}
-      ::new(static_cast<void*>(&f1)) decltype(f1)(1);
+      std::construct_at(&f1, 1);
 #endif
       // expected-warning at +1 {{field 'embed2' requires explicit initialization but is not explicitly initialized}} expected-note@#FIELD_EMBED2 {{'embed2' declared here}}
       ::new(static_cast<void*>(&f1)) decltype(f1){1};
@@ -1726,3 +1743,5 @@ void aggregate() {
   InheritWithExplicit<Special> specialized_implicit;  // no-warning
   (void)specialized_implicit;
 }
+
+#endif



More information about the cfe-commits mailing list