Skip to content

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

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
AaronBallman opened this issue Sep 4, 2024 · 2 comments
Open
Labels
clang:frontend Language frontend issues, e.g. anything involving "Sema"

Comments

@AaronBallman
Copy link
Collaborator

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
@AaronBallman AaronBallman added the clang:frontend Language frontend issues, e.g. anything involving "Sema" label Sep 4, 2024
@AaronBallman
Copy link
Collaborator Author

CC @rapidsna for awareness

@llvmbot
Copy link
Member

llvmbot commented Sep 4, 2024

@llvm/issue-subscribers-clang-frontend

Author: Aaron Ballman (AaronBallman)

While working on https://github.com//pull/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:

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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
clang:frontend Language frontend issues, e.g. anything involving "Sema"
Projects
None yet
Development

No branches or pull requests

2 participants