[clang] [clang] Fix dangling false positives for conditional operators. (PR #120233)
Haojian Wu via cfe-commits
cfe-commits at lists.llvm.org
Tue Dec 17 05:30:48 PST 2024
https://github.com/hokein created https://github.com/llvm/llvm-project/pull/120233
Fixes https://github.com/llvm/llvm-project/issues/120206
>From 36697bc0bc4047257b2307b26fdb5c4343dee340 Mon Sep 17 00:00:00 2001
From: Haojian Wu <hokein.wu at gmail.com>
Date: Tue, 17 Dec 2024 14:28:00 +0100
Subject: [PATCH] [clang] Fix dangling false positives for conditional
operators.
---
clang/docs/ReleaseNotes.rst | 2 ++
clang/lib/Sema/CheckExprLifetime.cpp | 9 ++++++
.../Sema/warn-lifetime-analysis-nocfg.cpp | 29 +++++++++++++++++++
3 files changed, 40 insertions(+)
diff --git a/clang/docs/ReleaseNotes.rst b/clang/docs/ReleaseNotes.rst
index befa411e882b4c..2fb14da05bbef5 100644
--- a/clang/docs/ReleaseNotes.rst
+++ b/clang/docs/ReleaseNotes.rst
@@ -677,6 +677,8 @@ Improvements to Clang's diagnostics
views.push_back(std::string("123")); // warning
}
+- Fix -Wdangling false positives on conditional operators (#120206).
+
Improvements to Clang's time-trace
----------------------------------
diff --git a/clang/lib/Sema/CheckExprLifetime.cpp b/clang/lib/Sema/CheckExprLifetime.cpp
index 843fdb4a65cd73..e4eb7d66575bb9 100644
--- a/clang/lib/Sema/CheckExprLifetime.cpp
+++ b/clang/lib/Sema/CheckExprLifetime.cpp
@@ -582,6 +582,15 @@ static void visitFunctionCallArguments(IndirectLocalPath &Path, Expr *Call,
// Temp().ptr; // Here ptr might not dangle.
if (isa<MemberExpr>(Arg->IgnoreImpCasts()))
return;
+ // Avoid false positives when the object is constructed from a conditional
+ // operator argument. A common case is:
+ // // 'ptr' might not be owned by the Owner object.
+ // std::string_view s = cond() ? Owner().ptr : sv;
+ if (const auto *Cond =
+ dyn_cast<AbstractConditionalOperator>(Arg->IgnoreImpCasts());
+ Cond && isPointerLikeType(Cond->getType()))
+ return;
+
auto ReturnType = Callee->getReturnType();
// Once we initialized a value with a non gsl-owner reference, it can no
diff --git a/clang/test/Sema/warn-lifetime-analysis-nocfg.cpp b/clang/test/Sema/warn-lifetime-analysis-nocfg.cpp
index 45b4dc838f44ed..4c19367bb7f3dd 100644
--- a/clang/test/Sema/warn-lifetime-analysis-nocfg.cpp
+++ b/clang/test/Sema/warn-lifetime-analysis-nocfg.cpp
@@ -777,3 +777,32 @@ void test4() {
}
} // namespace LifetimeboundInterleave
+
+namespace GH120206 {
+struct S {
+ std::string_view s;
+};
+
+struct [[gsl::Owner]] Q1 {
+ const S* get() const [[clang::lifetimebound]];
+};
+std::string_view test1(int c, std::string_view sv) {
+ std::string_view k = c > 1 ? Q1().get()->s : sv;
+ if (c == 1)
+ return c > 1 ? Q1().get()->s : sv;
+ Q1 q;
+ return c > 1 ? q.get()->s : sv;
+}
+
+struct Q2 {
+ const S* get() const [[clang::lifetimebound]];
+};
+std::string_view test2(int c, std::string_view sv) {
+ std::string_view k = c > 1 ? Q2().get()->s : sv;
+ if (c == 1)
+ return c > 1 ? Q2().get()->s : sv;
+ Q2 q;
+ return c > 1 ? q.get()->s : sv;
+}
+
+} // namespace GH120206
More information about the cfe-commits
mailing list