https://github.com/hokein updated https://github.com/llvm/llvm-project/pull/120233
>From 1bd4bf164082d236d17ada8240d7ba096a94609d Mon Sep 17 00:00:00 2001 From: Haojian Wu <hokein...@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 5f91ff90634036..9128a8e2ec40f2 100644 --- a/clang/docs/ReleaseNotes.rst +++ b/clang/docs/ReleaseNotes.rst @@ -700,6 +700,8 @@ Improvements to Clang's diagnostics return ptr + index < ptr; // 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 add6d7506bd6f0..7109de03cadd12 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 _______________________________________________ cfe-commits mailing list cfe-commits@lists.llvm.org https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits