[clang] [clang] Don't warn if the capturing object is also temporary. (PR #117733)
via cfe-commits
cfe-commits at lists.llvm.org
Tue Nov 26 08:17:03 PST 2024
llvmbot wrote:
<!--LLVM PR SUMMARY COMMENT-->
@llvm/pr-subscribers-clang
Author: Haojian Wu (hokein)
<details>
<summary>Changes</summary>
Fixes #<!-- -->117728
---
Full diff: https://github.com/llvm/llvm-project/pull/117733.diff
2 Files Affected:
- (modified) clang/lib/Sema/SemaChecking.cpp (+7-3)
- (modified) clang/test/Sema/warn-lifetime-analysis-capture-by.cpp (+11)
``````````diff
diff --git a/clang/lib/Sema/SemaChecking.cpp b/clang/lib/Sema/SemaChecking.cpp
index a49605e4867651..54d8bbdfc0f4fd 100644
--- a/clang/lib/Sema/SemaChecking.cpp
+++ b/clang/lib/Sema/SemaChecking.cpp
@@ -3248,9 +3248,13 @@ void Sema::checkLifetimeCaptureBy(FunctionDecl *FD, bool IsMemberFunction,
checkCaptureByLifetime(*this, CE, Captured);
}
};
- for (unsigned I = 0; I < FD->getNumParams(); ++I)
- HandleCaptureByAttr(FD->getParamDecl(I)->getAttr<LifetimeCaptureByAttr>(),
- I + IsMemberFunction);
+ // Suppress the warning if the capturing object is also a temporary to reduce
+ // noise, e.g `vector<string_view>().push_back(std::string());`.
+ if (!isa_and_present<MaterializeTemporaryExpr>(ThisArg)) {
+ for (unsigned I = 0; I < FD->getNumParams(); ++I)
+ HandleCaptureByAttr(FD->getParamDecl(I)->getAttr<LifetimeCaptureByAttr>(),
+ I + IsMemberFunction);
+ }
// Check when the implicit object param is captured.
if (IsMemberFunction) {
TypeSourceInfo *TSI = FD->getTypeSourceInfo();
diff --git a/clang/test/Sema/warn-lifetime-analysis-capture-by.cpp b/clang/test/Sema/warn-lifetime-analysis-capture-by.cpp
index 4d562bac1e305b..61d13c9e585e44 100644
--- a/clang/test/Sema/warn-lifetime-analysis-capture-by.cpp
+++ b/clang/test/Sema/warn-lifetime-analysis-capture-by.cpp
@@ -143,6 +143,17 @@ void use() {
}
} // namespace this_is_captured
+namespace ignore_temporary_class_object {
+struct S {
+ void add(const int& x [[clang::lifetime_capture_by(this)]]);
+};
+
+void test() {
+ S().add(1);
+ S{}.add(1);
+}
+} // namespace ignore_temporary_class_object
+
// ****************************************************************************
// Capture by Global and Unknown.
// ****************************************************************************
``````````
</details>
https://github.com/llvm/llvm-project/pull/117733
More information about the cfe-commits
mailing list