[clang] [Clang] Diagnose dangling references through libstdc++ debug containers (PR #245866)
Rei HIGUCHI via cfe-commits
cfe-commits at lists.llvm.org
Tue Nov 24 20:03:54 PST 2026
https://github.com/reihig-ut created https://github.com/llvm/llvm-project/pull/245866
Fixes #109627.
With libstdc++ debug mode enabled, std::vector inherits at() from a vector implementation in std::__cxx1998. Clang infers a GSL Owner attribute for the public vector type but misses the type that declares at(). This drops the usual -Wdangling-gsl warning when a reference is bound to an element of a temporary vector.
Recognize std::__cxx1998 when inferring GSL attributes for standard container types and their iterators. Allow inline ABI namespaces within it. The regression test models the debug wrappers and checks inherited at(), begin(), and front(). It also checks references to a live container, a copied element, and lookalike namespace names.
With GCC 15 libstdc++ headers, the patched frontend diagnoses the temporary in both normal and debug mode. The lifetime diagnostic tests were run using Clang's -verify consumer.
AI assistance was used to prepare the change and tests.
>From a3b385954298a3ed7a11c780650810aebd82174c Mon Sep 17 00:00:00 2001
From: Rei HIGUCHI <104817381+reihig-ut at users.noreply.github.com>
Date: Wed, 25 Nov 2026 13:03:21 +0900
Subject: [PATCH] [Clang] Diagnose dangling references through libstdc++ debug
containers
---
clang/lib/Sema/SemaAttr.cpp | 19 +++++--
.../SemaCXX/warn-dangling-libstdcxx-debug.cpp | 50 +++++++++++++++++++
2 files changed, 66 insertions(+), 3 deletions(-)
create mode 100644 clang/test/SemaCXX/warn-dangling-libstdcxx-debug.cpp
diff --git a/clang/lib/Sema/SemaAttr.cpp b/clang/lib/Sema/SemaAttr.cpp
index e5253e2a311747..ce00f54ad7a16c 100644
--- a/clang/lib/Sema/SemaAttr.cpp
+++ b/clang/lib/Sema/SemaAttr.cpp
@@ -109,6 +109,20 @@ static void addGslOwnerPointerAttributeIfNotExisting(ASTContext &Context,
Redecl->addAttr(Attribute::CreateImplicit(Context, /*DerefType=*/nullptr));
}
+static bool isInStdContainerNamespace(const CXXRecordDecl *Record) {
+ const DeclContext *DC = Record->getDeclContext()->getNonTransparentContext();
+ while (DC->isInlineNamespace())
+ DC = DC->getParent();
+ if (DC->isStdNamespace())
+ return true;
+
+ // libstdc++ debug containers inherit accessors from the corresponding
+ // release containers in std::__cxx1998.
+ const auto *NS = dyn_cast<NamespaceDecl>(DC);
+ return NS && NS->getIdentifier() && NS->getName() == "__cxx1998" &&
+ NS->isInStdNamespace();
+}
+
void Sema::inferGslPointerAttribute(NamedDecl *ND,
CXXRecordDecl *UnderlyingRecord) {
if (!UnderlyingRecord)
@@ -144,7 +158,7 @@ void Sema::inferGslPointerAttribute(NamedDecl *ND,
"reverse_iterator",
"const_reverse_iterator"};
- if (Parent->isInStdNamespace() && Iterators.count(ND->getName()) &&
+ if (isInStdContainerNamespace(Parent) && Iterators.count(ND->getName()) &&
Containers.count(Parent->getName()))
addGslOwnerPointerAttributeIfNotExisting<PointerAttr>(Context,
UnderlyingRecord);
@@ -204,8 +218,7 @@ void Sema::inferGslOwnerPointerAttribute(CXXRecordDecl *Record) {
if (!Record->getIdentifier())
return;
- // Handle classes that directly appear in std namespace.
- if (Record->isInStdNamespace()) {
+ if (isInStdContainerNamespace(Record)) {
if (Record->hasAttr<OwnerAttr>() || Record->hasAttr<PointerAttr>())
return;
diff --git a/clang/test/SemaCXX/warn-dangling-libstdcxx-debug.cpp b/clang/test/SemaCXX/warn-dangling-libstdcxx-debug.cpp
new file mode 100644
index 00000000000000..212d0f0b5c149a
--- /dev/null
+++ b/clang/test/SemaCXX/warn-dangling-libstdcxx-debug.cpp
@@ -0,0 +1,50 @@
+// RUN: %clang_cc1 -std=c++17 -Wdangling-gsl -verify %s
+
+namespace std {
+namespace __cxx1998 {
+template <class T> struct vector {
+ T &at(int);
+ struct iterator {};
+ iterator begin();
+};
+inline namespace __cxx11 {
+template <class T> struct list {
+ T &front();
+};
+} // namespace __cxx11
+} // namespace __cxx1998
+
+inline namespace __debug {
+template <class T> struct vector : __cxx1998::vector<T> {};
+template <class T> struct list : __cxx1998::list<T> {};
+} // namespace __debug
+} // namespace std
+
+void check(std::vector<int> &live) {
+ auto &ref = std::vector<int>{}.at(0);
+ // expected-warning at -1 {{object backing the pointer will be destroyed}}
+ auto iter = std::vector<int>{}.begin();
+ // expected-warning at -1 {{object backing the pointer will be destroyed}}
+ auto &front = std::list<int>{}.front();
+ // expected-warning at -1 {{object backing the pointer will be destroyed}}
+ auto &ok = live.at(0);
+ auto liveIter = live.begin();
+ auto value = std::vector<int>{}.at(0);
+}
+
+namespace other::__cxx1998 {
+struct vector {
+ int &at(int);
+};
+} // namespace other::__cxx1998
+
+namespace std::other {
+struct vector {
+ int &at(int);
+};
+} // namespace std::other
+
+void checkUnrelated() {
+ auto &ref = other::__cxx1998::vector{}.at(0);
+ auto &nested = std::other::vector{}.at(0);
+}
More information about the cfe-commits
mailing list