Skip to content

Allow counted_by argument to support simple arithmetic expressions #141827

Open
@seanm

Description

@seanm

Consider the following C:

struct mystruct
{
    unsigned len;
    unsigned data[] __attribute__((counted_by(len)));
};

This works with recent clang.

But it would be nice if simple arithmetic expressions could be used in the counted_by argument, like counted_by(len - 1) or counted_by(len * 4), things like that. Trying to do so gives:

<source>:4:47: error: 'counted_by' argument must be a simple declaration reference
    4 |     unsigned data[] __attribute__((counted_by(len - 1)));
      |                                               ^~~~~~~

See: https://godbolt.org/z/sx6sY1fjG

The gcc folks seemed to consider this too: https://gcc.gnu.org/bugzilla/show_bug.cgi?id=108896#c36 though from godbolt it seems they don't support it either.

There are several places in the libusb codebase that would benefit from this. The most complicated case would be counted_by((len - 2) / 2).

Metadata

Metadata

Assignees

No one assigned

    Labels

    clang:frontendLanguage frontend issues, e.g. anything involving "Sema"

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions