https://github.com/hokein created https://github.com/llvm/llvm-project/pull/97408
The lifetime_pointer case is handled before the assignment case. In some scenario where we have the gsl::Pointer attr, we will end up with emitting the `-Wdangling-gsl` warning for the assignment case. This means we can not use `-Wno-dangling-assignment` to suppress the newly-added warning. >From d1ea90918702fec85714bdd9caa68942eb4a4218 Mon Sep 17 00:00:00 2001 From: Haojian Wu <hokein...@gmail.com> Date: Tue, 2 Jul 2024 14:12:36 +0200 Subject: [PATCH] [clang] Don't emit the warn_dangling_lifetime_pointer diagnostic for the assignment case. The lifetime_pointer case is handled before the assignment case. In some scenario where we have the gsl::Pointer attr, we will end up with emitting the `-Wdangling-gsl` warning for the assignment case. This means we can not use `-Wno-dangling-assignment` to suppress the newly-added warning. --- clang/lib/Sema/CheckExprLifetime.cpp | 2 +- .../test/SemaCXX/suppress-dangling-assignment.cpp | 15 +++++++++++++++ 2 files changed, 16 insertions(+), 1 deletion(-) create mode 100644 clang/test/SemaCXX/suppress-dangling-assignment.cpp diff --git a/clang/lib/Sema/CheckExprLifetime.cpp b/clang/lib/Sema/CheckExprLifetime.cpp index fea0239f7bc36..03bf87a7cf5e6 100644 --- a/clang/lib/Sema/CheckExprLifetime.cpp +++ b/clang/lib/Sema/CheckExprLifetime.cpp @@ -1023,7 +1023,7 @@ static void checkExprLifetimeImpl(Sema &SemaRef, return false; } - if (IsGslPtrInitWithGslTempOwner && DiagLoc.isValid()) { + if (InitEntity && IsGslPtrInitWithGslTempOwner && DiagLoc.isValid()) { SemaRef.Diag(DiagLoc, diag::warn_dangling_lifetime_pointer) << DiagRange; return false; diff --git a/clang/test/SemaCXX/suppress-dangling-assignment.cpp b/clang/test/SemaCXX/suppress-dangling-assignment.cpp new file mode 100644 index 0000000000000..11625d3272b83 --- /dev/null +++ b/clang/test/SemaCXX/suppress-dangling-assignment.cpp @@ -0,0 +1,15 @@ + +// RUN: %clang_cc1 -std=c++20 -verify -Wno-dangling-assignment %s +// expected-no-diagnostics + +namespace std { +// std::basic_string has a hard-coded gsl::owner attr. +struct basic_string { + const char* c_str(); +}; +} // namespace std + +void test(const char* a) { + // verify that the dangling-assignment diagnostic are suppressed. + a = std::basic_string().c_str(); +} _______________________________________________ cfe-commits mailing list cfe-commits@lists.llvm.org https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits