llvm / llvm/llvm-project

[BoundsSafety] Introduce a notion of `this` for C

Open
#197,250 2 comments 0 reactions 1 assignee Claimed by @Caryoake View on GitHub
clang:bounds-safety
Dominant language
LLVM
Stars
40.5k
Forks
18.7k
PR merge metrics
PR metrics pending

Description

Certain thread safety and bounds safety annotations in C need to reference other fields within the same structure. For example:

```C
struct f {
int n;
int *__counted_by(n) buf;
};
```

In C++, this provides a well-defined way to express such self-referential relationships. C has no equivalent, so the current implementation works around this limitation by allowing `DeclRefExpr` to refer directly to a `FieldDecl`.

This is semantically incorrect — `FieldDecl` should only be referenced via `MemberExpr`, never via `DeclRefExpr`. Introducing a C equivalent of `CXXThisExpr` would resolve this improper usage and provide a clean, principled foundation for self-referential annotations in C.

Proposed solution: Add a new AST node (e.g., `CThisExpr` or similar) that mirrors `CXXThisExpr` semantics for C, enabling bounds safety and thread safety annotations to correctly express field-to-field references within a struct.

Contributor guide

Open the contributing guide

Assessment

This issue has not been assessed yet.

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.