[clang] [clang] Permit lifetimebound in all language modes (PR #115482)

Gábor Horváth via cfe-commits cfe-commits at lists.llvm.org
Fri Nov 8 05:36:57 PST 2024


https://github.com/Xazax-hun created https://github.com/llvm/llvm-project/pull/115482

Lifetimebound annotations can help diagnose common cases of dangling including escaping the address of a stack variable from a function. This is useful in all C family languages, restricting these diagnostics to C++ is an artificial limitation.

>From 92a177c3034c81733d006a2b2c6a5127550566d5 Mon Sep 17 00:00:00 2001
From: Gabor Horvath <gaborh at apple.com>
Date: Fri, 8 Nov 2024 13:00:22 +0000
Subject: [PATCH] [clang] Permit lifetimebound in all language modes

Lifetimebound annotations can help diagnose common cases of dangling
including escaping the address of a stack variable from a function. This
is useful in all C family languages, restricting these diagnostics to
C++ is an artificial limitation.
---
 clang/include/clang/Basic/Attr.td    | 1 -
 clang/test/Sema/attr-lifetimebound.c | 8 ++++++++
 2 files changed, 8 insertions(+), 1 deletion(-)
 create mode 100644 clang/test/Sema/attr-lifetimebound.c

diff --git a/clang/include/clang/Basic/Attr.td b/clang/include/clang/Basic/Attr.td
index 24cfb5ddb6d4ca..a631e81d40aa68 100644
--- a/clang/include/clang/Basic/Attr.td
+++ b/clang/include/clang/Basic/Attr.td
@@ -1886,7 +1886,6 @@ def LifetimeBound : DeclOrTypeAttr {
   let Spellings = [Clang<"lifetimebound", 0>];
   let Subjects = SubjectList<[ParmVar, ImplicitObjectParameter], ErrorDiag>;
   let Documentation = [LifetimeBoundDocs];
-  let LangOpts = [CPlusPlus];
   let SimpleHandler = 1;
 }
 
diff --git a/clang/test/Sema/attr-lifetimebound.c b/clang/test/Sema/attr-lifetimebound.c
new file mode 100644
index 00000000000000..e1c714cb27dc8b
--- /dev/null
+++ b/clang/test/Sema/attr-lifetimebound.c
@@ -0,0 +1,8 @@
+// RUN: %clang_cc1 -std=c99 -verify %s
+
+int *f(int* p __attribute__((lifetimebound)));
+
+int *g() {
+  int i;
+  return f(&i); // expected-warning {{address of stack memory associated with local variable 'i' returned}}
+}



More information about the cfe-commits mailing list