-
Notifications
You must be signed in to change notification settings - Fork 13.6k
[clang] Suppress a dangling false positive when owner is moved in member initializer. #114213
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
member initializer. 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; }; ```
@llvm/pr-subscribers-clang Author: Haojian Wu (hokein) ChangesThis patch extends the filtering heuristic to apply for the Lifetimebound code path. This will suppress a common false positive:
See #112751. Full diff: https://github.com/llvm/llvm-project/pull/114213.diff 2 Files Affected:
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();
|
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.
LG!
LLVM Buildbot has detected a new failure on builder Full details are available at: https://lab.llvm.org/buildbot/#/builders/73/builds/7886 Here is the relevant piece of the build log for the reference
|
…ber initializer. (llvm#114213) 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 llvm#114201.
…ber initializer. (llvm#114213) 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 llvm#114201.
This patch extends the filtering heuristic to apply for the Lifetimebound code path.
This will suppress a common false positive:
See #114201.