[PATCH] D110127: [Clang] Support typedef with btf_tag attributes

Yonghong Song via Phabricator via cfe-commits cfe-commits at lists.llvm.org
Wed Sep 22 15:32:01 PDT 2021


yonghong-song added a comment.

In D110127#3016371 <https://reviews.llvm.org/D110127#3016371>, @aaron.ballman wrote:

> In D110127#3013876 <https://reviews.llvm.org/D110127#3013876>, @yonghong-song wrote:
>
>> You can see that it doesn't matter where the attribute is placed, the attribute is always attached to the typedef.
>> I think the reason is for declarations, we only allow the btf_tag attribute to be placed for record, field, var, func, typedef,
>> and "unsigned *" does not qualify, so the attribute is applied to typedef.
>
> Agreed. If this attribute appertains to the type, then it goes through SemaType.cpp and may require extra work to encode the information into the type system. If it appertains to the declaration, then what's in SemaDeclAttr.cpp is fine, but then I have questions about the use of the attribute with cast notation from D110116 <https://reviews.llvm.org/D110116> where this is being used definitely as a type attribute.

For typedef use case, it intends to be used as a decl attribute.

> One confounding factor here is that `__attribute__` will slide around to whatever makes sense. You should try your examples with `[[]]` in C2x mode as well -- the placement of the attribute syntax is strongly tied to what the attribute applies to.

I tried `[[]]` syntax, it does seem better than otherwise,  e.g.,

  [$ ~/work/tests/llvm/btf_tag] cat t1.c
  #define __tag1 [[clang::btf_tag("tag1")]]
  typedef unsigned __tag1 * __u;
  __u u;
  [$~/work/tests/llvm/btf_tag] clang -c -std=c2x t1.c
  t1.c:2:18: error: 'btf_tag' attribute cannot be applied to types
  typedef unsigned __tag1 * __u;
                   ^
  t1.c:1:18: note: expanded from macro '__tag1'
  #define __tag1 [[clang::btf_tag("tag1")]]
                   ^
  1 error generated.
  [$ ~/work/tests/llvm/btf_tag]

and

  [$ ~/work/tests/llvm/btf_tag] cat t1.c
  #define __tag1 [[clang::btf_tag("tag1")]]
  typedef unsigned * __u __tag1;
  __u u;
  [$ ~/work/tests/llvm/btf_tag] clang -c -std=c2x t1.c
  [$ ~/work/tests/llvm/btf_tag]

If using old `__attribute__`  syntax, both above examples will compile successfully.

I will change my test to use -std=c2x.

>>> I'm asking because this raises other questions. For example:
>>>
>>>   void func(int i);
>>>   void func(int __attribute__((btf_tag("__user"))) i);
>>>
>>> Is the second a valid redeclaration of the first? Additionally:
>>
>> This should be okay as btf_tag is accumulative attribute.
>
> Okay, this is sounding more and more like a declaration attribute.
>
>>>   __attribute__((overloadable)) void func(int i) { puts("one"); }
>>>   __attribute__((overloadable)) void func(int __attribute__((btf_tag("__user"))) i) { puts("two"); }
>>>
>>> Is this a valid overload set because of the type attribute? Along the same lines, does adding this attribute to the type cause any ABI differences in how the type is passed?
>>
>> btf_tag is for C only so overload function case won't happen.
>
> That's incorrect -- Clang supports attribute overloadable in C: https://godbolt.org/z/eThKsn3zM

In this case, the overload is not valid and the attribute does not have an impact on overloading decision.

  [$ ~/work/tests/llvm/btf_tag] cat t.c
  #include <stdio.h>
  
  __attribute__((overloadable)) void func(int i) { puts("int"); }
  __attribute__((overloadable)) void func(int i __attribute__((btf_tag("tag2")))) { puts("float"); }
  
  int main(void) {
    func(1);
  }
  [$ ~/work/tests/llvm/btf_tag] clang -c t.c
  t.c:4:36: error: redefinition of 'func'
  __attribute__((overloadable)) void func(int i __attribute__((btf_tag("tag2")))) { puts("float"); }
                                     ^
  t.c:3:36: note: previous definition is here
  __attribute__((overloadable)) void func(int i) { puts("int"); }
                                     ^
  1 error generated.
  [$ ~/work/tests/llvm/btf_tag]

So overloading is rejected. This is similar to below example with aligned attribute,

  [$ ~/work/tests/llvm/btf_tag] cat t.c
  #include <stdio.h>
  
  __attribute__((overloadable)) void func(int i) { puts("int"); }
  __attribute__((overloadable)) void func(int i __attribute__((aligned(8)))) { puts("float"); }
  
  int main(void) {
    func(1);
  }
  [$ ~/work/tests/llvm/btf_tag] clang -c t.c
  t.c:4:36: error: redefinition of 'func'
  __attribute__((overloadable)) void func(int i __attribute__((aligned(8)))) { puts("float"); }
                                     ^
  t.c:3:36: note: previous definition is here
  __attribute__((overloadable)) void func(int i) { puts("int"); }
                                     ^
  1 error generated.
  [$ ~/work/tests/llvm/btf_tag]


Repository:
  rG LLVM Github Monorepo

CHANGES SINCE LAST ACTION
  https://reviews.llvm.org/D110127/new/

https://reviews.llvm.org/D110127



More information about the cfe-commits mailing list