Skip to content

Inconsistent behavior with counted/sizedby attributes in type positions #107249

Open
@AaronBallman

Description

@AaronBallman

While working on #107238, I noticed that these attributes (and their "or null" counterparts) are DeclOrTypeAttr attributes which are intended to be C-only. However, there's no code in processTypeAttrs() in SemaType.cpp to handle these attributes, and so it wasn't clear that we had the right restrictions for checking the language mode.

This led me to discover some odd inconsistencies with how we handle the type form of the attribute: https://godbolt.org/z/WhjYE1xTY

#define __counted_by(f) __attribute__((counted_by(f)))

int foo;
int y = (int __counted_by(foo))12;

int * __counted_by(foo) ptr;

gives:

<source>:4:14: warning: 'counted_by' attribute ignored when parsing type [-Wignored-attributes]
    4 | int y = (int __counted_by(foo))12;
      |              ^~~~~~~~~~~~~~~~~
<source>:1:40: note: expanded from macro '__counted_by'
    1 | #define __counted_by(f) __attribute__((counted_by(f)))
      |                                        ^~~~~~~~~~~~~
<source>:6:7: error: 'counted_by' attribute only applies to non-static data members
    6 | int * __counted_by(foo) ptr;
      |       ^
<source>:1:40: note: expanded from macro '__counted_by'
    1 | #define __counted_by(f) __attribute__((counted_by(f)))
      |                                        ^
1 warning and 1 error generated.
Compiler returned: 1

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