llvmbot wrote:
<!--LLVM PR SUMMARY COMMENT--> @llvm/pr-subscribers-clang Author: Haojian Wu (hokein) <details> <summary>Changes</summary> This patch extends the filtering heuristic to apply for the Lifetimebound code path. This will suppress a common false positive: ``` namespace std { template<typename T> struct unique_ptr { T &operator*(); T *get() const [[clang::lifetimebound]]; }; } // namespace std struct X { X(std::unique_ptr<int> up) : pointer(up.get()), owner(std::move(up)) {} int *pointer; std::unique_ptr<int> owner; }; ``` See #<!-- -->112751. --- Full diff: https://github.com/llvm/llvm-project/pull/114213.diff 2 Files Affected: - (modified) clang/lib/Sema/CheckExprLifetime.cpp (+3-3) - (modified) clang/test/Sema/warn-lifetime-analysis-nocfg.cpp (+13) ``````````diff diff --git a/clang/lib/Sema/CheckExprLifetime.cpp b/clang/lib/Sema/CheckExprLifetime.cpp index aa0a2e223e708f..33d308fe7bdb60 100644 --- a/clang/lib/Sema/CheckExprLifetime.cpp +++ b/clang/lib/Sema/CheckExprLifetime.cpp @@ -1261,12 +1261,12 @@ static void checkExprLifetimeImpl(Sema &SemaRef, if (pathContainsInit(Path)) return false; + auto *DRE = dyn_cast<DeclRefExpr>(L); // Suppress false positives for code like the one below: - // Ctor(unique_ptr<T> up) : member(*up), member2(move(up)) {} - if (IsLocalGslOwner && pathOnlyHandlesGslPointer(Path)) + // Ctor(unique_ptr<T> up) : pointer(up.get()), owner(move(up)) {} + if (DRE && isRecordWithAttr<OwnerAttr>(DRE->getType())) return false; - auto *DRE = dyn_cast<DeclRefExpr>(L); auto *VD = DRE ? dyn_cast<VarDecl>(DRE->getDecl()) : nullptr; if (!VD) { // A member was initialized to a local block. diff --git a/clang/test/Sema/warn-lifetime-analysis-nocfg.cpp b/clang/test/Sema/warn-lifetime-analysis-nocfg.cpp index 688f55edfe84df..6a2af01ea5116c 100644 --- a/clang/test/Sema/warn-lifetime-analysis-nocfg.cpp +++ b/clang/test/Sema/warn-lifetime-analysis-nocfg.cpp @@ -384,6 +384,19 @@ struct X { std::unique_ptr<int> pointer; }; +struct [[gsl::Owner]] XOwner { + int* get() const [[clang::lifetimebound]]; +}; +struct X2 { + // A common usage that moves the passing owner to the class. + // verify no warning on this case. + X2(XOwner owner) : + pointee(owner.get()), + owner(std::move(owner)) {} + int* pointee; + XOwner owner; +}; + std::vector<int>::iterator getIt(); std::vector<int> getVec(); `````````` </details> https://github.com/llvm/llvm-project/pull/114213 _______________________________________________ cfe-commits mailing list cfe-commits@lists.llvm.org https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits