[clang] [LifetimeSafety] Warn on incorrectly placed `[[clang::lifetimebound]]` attributes (PR #196144)

Utkarsh Saxena via cfe-commits cfe-commits at lists.llvm.org
Sat May 9 00:17:19 PDT 2026


================
@@ -0,0 +1,80 @@
+// RUN: %clang_cc1 -fsyntax-only -Wlifetime-safety-lifetimebound-violation -verify %s
+
+#include "Inputs/lifetime-analysis.h"
+
+struct [[gsl::Owner]] MyObj {
+  int id;
+  ~MyObj() {}  // Non-trivial destructor
+};
+
+struct [[gsl::Pointer()]] View {
+  View(const MyObj &); // Borrows from MyObj
+  View();
+  void use() const;
+};
+
+bool cond();
+
+View not_lb(const MyObj &obj);
+
+View lb(const MyObj &obj [[clang::lifetimebound]]);
+
+View return_through_unannotated_passthrough(
+    const MyObj &obj [[clang::lifetimebound]]) { // expected-warning {{could not verify that the return value can be lifetime bound to 'obj'}}
+  return not_lb(obj);
+}
+
+View return_through_lifetimebound_passthrough(
+    const MyObj &obj [[clang::lifetimebound]]) {
+  return lb(obj);
+}
+
+View lb2(const MyObj &obj [[clang::lifetimebound]]) {
+  return lb(obj);
+}
+
+View lose_lb(const MyObj &obj [[clang::lifetimebound]]) { // expected-warning {{could not verify that the return value can be lifetime bound to 'obj'}}
+  return not_lb(obj);
+}
+
+View return_through_alias(const MyObj &obj [[clang::lifetimebound]]) {
+  const MyObj &alias = obj;
+  return alias;
+}
+
+View return_alias_through_unannotated_passthrough(
+    const MyObj &obj [[clang::lifetimebound]]) { // expected-warning {{could not verify that the return value can be lifetime bound to 'obj'}}
+  const MyObj &alias = obj;
+  return not_lb(alias);
+}
+
+View return_through_two_lifetimebound_calls(
+    const MyObj &obj [[clang::lifetimebound]]) {
+  return lb2(obj);
+}
----------------
usx95 wrote:

This does not look useful as compared to other test with a `lb(obj)`. Maybe have `lb(lb(obj))` or something similar

https://github.com/llvm/llvm-project/pull/196144


More information about the cfe-commits mailing list