llvmbot wrote:
<!--LLVM PR SUMMARY COMMENT--> @llvm/pr-subscribers-clang Author: Haojian Wu (hokein) <details> <summary>Changes</summary> Fixes https://github.com/llvm/llvm-project/issues/120206 --- Full diff: https://github.com/llvm/llvm-project/pull/120233.diff 3 Files Affected: - (modified) clang/docs/ReleaseNotes.rst (+2) - (modified) clang/lib/Sema/CheckExprLifetime.cpp (+9) - (modified) clang/test/Sema/warn-lifetime-analysis-nocfg.cpp (+29) ``````````diff 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 `````````` </details> https://github.com/llvm/llvm-project/pull/120233 _______________________________________________ cfe-commits mailing list cfe-commits@lists.llvm.org https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits