Skip to content

🍒[Clang] Use [[clang::lifetimebound]] syntax when supported #11121

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Aug 6, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 18 additions & 0 deletions clang/lib/Headers/lifetimebound.h
Original file line number Diff line number Diff line change
Expand Up @@ -10,10 +10,28 @@
#ifndef __LIFETIMEBOUND_H
#define __LIFETIMEBOUND_H

#if defined(__cplusplus) && defined(__has_cpp_attribute)
#define __use_cpp_spelling(x) __has_cpp_attribute(x)
#else
#define __use_cpp_spelling(x) 0
#endif

#if __use_cpp_spelling(clang::lifetimebound)
#define __lifetimebound [[clang::lifetimebound]]
#else
#define __lifetimebound __attribute__((lifetimebound))
#endif

#if __use_cpp_spelling(clang::lifetime_capture_by)
#define __lifetime_capture_by(X) [[clang::lifetime_capture_by(X)]]
#else
#define __lifetime_capture_by(X) __attribute__((lifetime_capture_by(X)))
#endif

#if __use_cpp_spelling(clang::noescape)
#define __noescape [[clang::noescape]]
#else
#define __noescape __attribute__((noescape))
#endif

#endif /* __LIFETIMEBOUND_H */
11 changes: 11 additions & 0 deletions clang/test/Headers/lifetimebound.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
// RUN: %clang_cc1 -fsyntax-only -verify %s
// expected-no-diagnostics

// Verify that we can include <lifetimebound.h>
#include <lifetimebound.h>

struct foo {};

struct foo* get_foo(char *ptr __lifetimebound);

void non_escaping(char *ptr __noescape);
1 change: 1 addition & 0 deletions clang/test/Headers/lifetimebound.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@

struct has_lifetimebound_method {
const char* get_ptr(char* ptr __lifetimebound) const;
const char* get_ptr() const __lifetimebound;
};

struct has_lifetime_capture_by_method {
Expand Down