[clang] Include [[clang::require_explicit_initialization]] warnings in system headers (PR #141133)
via cfe-commits
cfe-commits at lists.llvm.org
Fri May 23 13:54:17 PDT 2025
https://github.com/higher-performance updated https://github.com/llvm/llvm-project/pull/141133
>From 1006e27f02b1e6e94b4712d9ccbc5005a616d670 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 | 27 ++++++++++++++++---
2 files changed, 25 insertions(+), 5 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..df606b0827b1a 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(static_cast<U&&>(args)...); // #FIELD_EMBED2_CONSTRUCT_AT
+}
+}
+
+#else
+
+#define BE_THE_HEADER
+#include __FILE__
+
void* operator new(__SIZE_TYPE__, void*);
// definitions for std::move
@@ -1564,11 +1581,11 @@ void aggregate() {
explicit F(const char(&)[1]) : 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);
- // 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@#FIELD_EMBED2_CONSTRUCT_AT {{field in 'Embed' requires explicit initialization but is not explicitly initialized}} expected-note at +1 {{in instantiation of function template specialization 'std::construct_at<Embed>' requested here}} expected-note@#FIELD_EMBED2 {{'embed2' declared here}}
+ 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);
+ // expected-warning@#FIELD_EMBED2_CONSTRUCT_AT {{field 'embed2' requires explicit initialization but is not explicitly initialized}} expected-note at +1 {{in instantiation of function template specialization 'std::construct_at<Embed, int>' requested here}} expected-note@#FIELD_EMBED2 {{'embed2' declared here}}
+ 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