Skip to content

🍒[Clang] Add macros for noescape and lifetime annotations #10643

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
May 8, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions clang/lib/Headers/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -290,6 +290,9 @@ set(files
# TO_UPSTREAM(BoundsSafety)
list(APPEND files ptrcheck.h)

# TO_UPSTREAM(Lifetimebound)
list(APPEND files lifetimebound.h)

set(cuda_wrapper_files
cuda_wrappers/algorithm
cuda_wrappers/cmath
Expand Down
19 changes: 19 additions & 0 deletions clang/lib/Headers/lifetimebound.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
/*===---- lifetimebound.h - Lifetime attributes -----------------------------===
*
* Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
* See https://llvm.org/LICENSE.txt for license information.
* SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
*
*===------------------------------------------------------------------------===
*/

#ifndef __LIFETIMEBOUND_H
#define __LIFETIMEBOUND_H

#define __lifetimebound __attribute__((lifetimebound))

#define __lifetime_capture_by(X) __attribute__((lifetime_capture_by(X)))

#define __noescape __attribute__((noescape))

#endif /* __LIFETIMEBOUND_H */
9 changes: 8 additions & 1 deletion clang/lib/Headers/module.modulemap
Original file line number Diff line number Diff line change
Expand Up @@ -334,4 +334,11 @@ module ptrcheck {
header "ptrcheck.h"
export *
}
/* TO_UPSTREAM(BoundsSafety) OFF */
/* TO_UPSTREAM(BoundsSafety) OFF */

/* TO_UPSTREAM(Lifetimebound) ON */
module lifetimebound {
header "lifetimebound.h"
export *
}
/* TO_UPSTREAM(Lifetimebound) OFF */
18 changes: 18 additions & 0 deletions clang/test/Headers/lifetimebound.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
// RUN: %clang_cc1 -fsyntax-only -verify %s
// expected-no-diagnostics

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

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

struct has_lifetime_capture_by_method {
void take_ptr(char* ptr __lifetime_capture_by(this));
void take_ptr(has_lifetimebound_method a, char* ptr __lifetime_capture_by(a));
};

struct has_noescape_method {
void takes_noescape(char* ptr __noescape);
};