llvm-project icon indicating copy to clipboard operation
llvm-project copied to clipboard

Upstream the terminated_by(T) attribute

Open rapidsna opened this issue 1 month ago • 1 comments

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 ValueTerminatedType type 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 T is 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 a terminated_by pointer 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.

rapidsna avatar Oct 09 '26 22:10 rapidsna

I'm interested in working on this.

zmodem avatar Nov 06 '26 10:11 zmodem