-
Notifications
You must be signed in to change notification settings - Fork 14.4k
[clang] Don't emit the warn_dangling_lifetime_pointer diagnostic for the assignment case. #97408
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Conversation
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.
@llvm/pr-subscribers-clang Author: Haojian Wu (hokein) ChangesThe 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 Full diff: https://github.com/llvm/llvm-project/pull/97408.diff 2 Files Affected:
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();
+}
|
clang/lib/Sema/CheckExprLifetime.cpp
Outdated
@@ -1023,7 +1023,7 @@ static void checkExprLifetimeImpl(Sema &SemaRef, | |||
return false; | |||
} | |||
|
|||
if (IsGslPtrInitWithGslTempOwner && DiagLoc.isValid()) { | |||
if (InitEntity && IsGslPtrInitWithGslTempOwner && DiagLoc.isValid()) { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think it would be simpler to just handle AEntity
before:
if(AEntity) {
assert(shouldLifetimeExtendThroughPath(Path) == PathLifetimeKind::NoExtend);
assert(LK == LK_Extended);
if (!pathContainsInit(path)) {
SemaRef.Diag(DiagLoc, diag::warn_dangling_pointer_assignment)
<< AEntity->LHS << DiagRange;
}
return;
}
assert(InitEntity);
switch (LK) {
...
<remove assert(InitEntity*) checks>
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Done.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thanks. LGTM.
…the assignment case. (llvm#97408) The `lifetime_pointer` case is handled before the assignment case. In scenarios where we have the `gsl::Pointer` attribute, we may emit the `-Wdangling-gsl` warning for assignment cases. This means we cannot use `-Wno-dangling-assignment` to suppress the newly-added warning, this patch fixes it.
…the assignment case. (llvm#97408) The `lifetime_pointer` case is handled before the assignment case. In scenarios where we have the `gsl::Pointer` attribute, we may emit the `-Wdangling-gsl` warning for assignment cases. This means we cannot use `-Wno-dangling-assignment` to suppress the newly-added warning, this patch fixes it.
The
lifetime_pointer
case is handled before the assignment case. In scenarios where we have thegsl::Pointer
attribute, we may emit the-Wdangling-gsl
warning for assignment cases. This means we cannot use-Wno-dangling-assignment
to suppress the newly-added warning, this patch fixes it.