[clang] 2c242b9 - [clang] Add a lifetime_capture_by testcase for temporary capturing object. (#117733)

via cfe-commits cfe-commits at lists.llvm.org
Thu Nov 28 01:17:44 PST 2024


Author: Haojian Wu
Date: 2024-11-28T10:17:41+01:00
New Revision: 2c242b98c608021e6768f0369498f5b8b5144d34

URL: https://github.com/llvm/llvm-project/commit/2c242b98c608021e6768f0369498f5b8b5144d34
DIFF: https://github.com/llvm/llvm-project/commit/2c242b98c608021e6768f0369498f5b8b5144d34.diff

LOG: [clang] Add a lifetime_capture_by testcase for temporary capturing object. (#117733)

Add a test case to indicate this is an expected behavior.

Added: 
    

Modified: 
    clang/test/Sema/warn-lifetime-analysis-capture-by.cpp

Removed: 
    


################################################################################
diff  --git a/clang/test/Sema/warn-lifetime-analysis-capture-by.cpp b/clang/test/Sema/warn-lifetime-analysis-capture-by.cpp
index 4d562bac1e305b..e867296d6d8ea7 100644
--- a/clang/test/Sema/warn-lifetime-analysis-capture-by.cpp
+++ b/clang/test/Sema/warn-lifetime-analysis-capture-by.cpp
@@ -143,6 +143,20 @@ void use() {
 }
 } // namespace this_is_captured
 
+namespace temporary_capturing_object {
+struct S {
+  void add(const int& x [[clang::lifetime_capture_by(this)]]);
+};
+
+void test() {
+  // We still give an warning even the capturing object is a temoprary.
+  // It is possible that the capturing object uses the captured object in its
+  // destructor.
+  S().add(1); // expected-warning {{object whose reference is captured}}
+  S{}.add(1); // expected-warning {{object whose reference is captured}}
+}
+} // namespace ignore_temporary_class_object
+
 // ****************************************************************************
 // Capture by Global and Unknown.
 // ****************************************************************************


        


More information about the cfe-commits mailing list