llvm / llvm/llvm-project

Support late parsing for `__counted_by` attributes in type position

Open
#166,411 0 comments 1 reaction 1 assignee Claimed by @rapidsna View on GitHub
clang:bounds-safety
Dominant language
LLVM
Stars
40.5k
Forks
18.7k
PR merge metrics
PR metrics pending

Description

### Problem:

`__counted_by` and related attributes must be treated as type attributes to enable future support for valid use cases such as:

```C
void foo(int *__counted_by(*out_count) *out_buffer, size_t *out_count);

typedef int *__counted_by(10) ARRY_OF_INT_10_T;
```

*Note: These use cases are not yet supported, as __counted_by currently only works within struct definitions.*

However, even within structs, we should correctly reject attributes on nested type positions where the count field is not yet in scope:

```C
struct nested_counted_by {
int count;
int *__counted_by(count) *buf; // error
};

struct late_parsed_nested_counted_by {
int *__counted_by(count) *buf; // error
int count;
};
```

### Current behavior:

- Late parsing only recognizes attributes in declaration position, not type position
- __counted_by attributes on nested type positions are incorrectly attached to the outermost pointer type because they are being treated as GNU-style declaration attributes rather than type attributes

### Expected behavior:

- Extend late parsing to support attributes in type position
- Reject invalid nested pointer cases with clear diagnostics
- Lay groundwork for future support of __counted_by in function parameters and typedefs

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.