llvm-project
llvm-project copied to clipboard
Upstream the terminated_by(T) attribute
terminated_by(T) says that a pointer or array is terminated by the value T, where T is a constant. Accessing the pointer or incrementing it past the terminator is not allowed.
The attribute generalizes __null_terminated, which is defined as __terminated_by(0).
const char *__null_terminated str; // same as __terminated_by(0)
int *__terminated_by(-1) list; // ends with the value -1
Background
In the downstream fork swiftlang/llvm-project, terminated_by(T) is represented in the AST by ValueTerminatedType, a sugar type.
Scope Upstreaming this requires:
- AST: add the
ValueTerminatedTypetype node. - Attribute: add parsing for terminated_by(T) on pointers and arrays, with diagnostics when:
- the attribute is applied to an incompatible type (anything other than a pointer or array), or
- the argument
Tis not a constant.
- Compatibility checking: diagnose invalid combinations with the bounds annotations already upstream (counted_by, sized_by, and their _or_null variants).
Out of scope
The following will be handled separately:
- Restricting pointer arithmetic: under
-fbounds-safety, the only arithmetic allowed on aterminated_bypointer is incrementing it by 1. This issue does not enforce that restriction. - Run-time checks: this issue does not add the run-time check at dereference.
I'm interested in working on this.