[clang] [clang] Implement lifetime analysis for lifetime_capture_by(X) (PR #115921)
Boaz Brickner via cfe-commits
cfe-commits at lists.llvm.org
Fri Nov 15 06:09:41 PST 2024
================
@@ -0,0 +1,220 @@
+// RUN: %clang_cc1 --std=c++20 -fsyntax-only -Wdangling -Wdangling-field -Wreturn-stack-address -verify %s
+
+#include "Inputs/lifetime-analysis.h"
+
+struct X {
+ const int *x;
+ void captureInt(const int& x [[clang::lifetime_capture_by(this)]]) { this->x = &x; }
+ void captureSV(std::string_view sv [[clang::lifetime_capture_by(this)]]);
+};
+///////////////////////////
+// Detect dangling cases.
+///////////////////////////
+void captureInt(const int &i [[clang::lifetime_capture_by(x)]], X &x);
+void captureRValInt(int &&i [[clang::lifetime_capture_by(x)]], X &x);
+void noCaptureInt(int i [[clang::lifetime_capture_by(x)]], X &x);
+
+std::string_view substr(const std::string& s [[clang::lifetimebound]]);
+std::string_view strcopy(const std::string& s);
+
+void captureSV(std::string_view s [[clang::lifetime_capture_by(x)]], X &x);
+void captureRValSV(std::string_view &&sv [[clang::lifetime_capture_by(x)]], X &x);
+void noCaptureSV(std::string_view sv, X &x);
+void captureS(const std::string &s [[clang::lifetime_capture_by(x)]], X &x);
+void captureRValS(std::string &&s [[clang::lifetime_capture_by(x)]], X &x);
+
+const std::string& getLB(const std::string &s [[clang::lifetimebound]]);
+const std::string& getLB(std::string_view sv [[clang::lifetimebound]]);
+const std::string* getPointerLB(const std::string &s [[clang::lifetimebound]]);
+const std::string* getPointerNoLB(const std::string &s);
+
+void capturePointer(const std::string* sp [[clang::lifetime_capture_by(x)]], X &x);
+
+struct ThisIsCaptured {
+ void capture(X &x) [[clang::lifetime_capture_by(x)]];
+};
+
+void captureByGlobal(std::string_view s [[clang::lifetime_capture_by(global)]]);
+void captureByUnknown(std::string_view s [[clang::lifetime_capture_by(unknown)]]);
+
+void use() {
+ std::string_view local_sv;
+ std::string local_s;
+ X x;
+ // Capture an 'int'.
+ int local;
+ captureInt(1, // expected-warning {{object whose reference is captured by 'x' will be destroyed at the end of the full-expression}}
+ x);
+ captureRValInt(1, x); // expected-warning {{object whose reference is captured by 'x'}}
+ captureInt(local, x);
+ noCaptureInt(1, x);
+ noCaptureInt(local, x);
+
+ // Capture using std::string_view.
+ captureSV(local_sv, x);
+ captureSV(std::string(), // expected-warning {{object whose reference is captured by 'x'}}
+ x);
+ captureSV(substr(
+ std::string() // expected-warning {{object whose reference is captured by 'x'}}
+ ), x);
+ captureSV(substr(local_s), x);
+ captureSV(strcopy(std::string()), x);
----------------
bricknerb wrote:
I think this will be much more readable if SV would be replaced by StringView.
https://github.com/llvm/llvm-project/pull/115921
More information about the cfe-commits
mailing list