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

Yonghong Song via Phabricator via cfe-commits cfe-commits at lists.llvm.org
Mon Sep 20 23:25:41 PDT 2021


yonghong-song created this revision.
yonghong-song added a reviewer: aaron.ballman.
Herald added a subscriber: jdoerfert.
yonghong-song requested review of this revision.
Herald added a project: clang.
Herald added a subscriber: cfe-commits.

Previously, btf_tag attribute supports record, field, global variable,
function and function parameter ([1]). This patch added support for typedef.
There are two reasons for this addition.

First, to build linux kernel with btf_tag annotated user pointer, we have

  #define __user __attribute__((btf_tag("user")))

and the linux kernel contains code like below ([2])

  typedef __signalfn_t __user *__sighandler_t;

and the __user attribute applies to typedef type __sighandler_t.
So clang needs to support btf_tag attribute with typedef type
to avoid compilation error.

Second, for typedef of an anonymous struct/union, we can only apply
btf_tag attribute to the anonymous struct/union like below:

  typedef struct { ... } __btf_tag target_type

In this case, the __btf_tag attribute applies to anonymous struct,
which increases downstream implementation complexity. But if
typedef with btf_tag attribute is supported, we can have

  typedef struct { ... } target_type __btf_tag

which applies __btf_tag to typedef "target_type" which make it
easier to directly associate btf_tag with a named type.

This patch permitted btf_tag with typedef types due to the 
above two reasons.

[1] https://reviews.llvm.org/D106614
 [2] https://github.com/torvalds/linux/blob/master/include/uapi/asm-generic/signal-defs.h#L82


Repository:
  rG LLVM Github Monorepo

https://reviews.llvm.org/D110127

Files:
  clang/include/clang/Basic/Attr.td
  clang/include/clang/Basic/AttrDocs.td
  clang/test/Misc/pragma-attribute-supported-attributes-list.test
  clang/test/Sema/attr-btf_tag.c


Index: clang/test/Sema/attr-btf_tag.c
===================================================================
--- clang/test/Sema/attr-btf_tag.c
+++ clang/test/Sema/attr-btf_tag.c
@@ -25,18 +25,28 @@
 
 enum e1 {
   E1
-} __tag1; // expected-error {{'btf_tag' attribute only applies to variables, functions, structs, unions, classes, and non-static data members}}
+} __tag1; // expected-error {{'btf_tag' attribute only applies to variables, functions, structs, unions, classes, non-static data members, and typedefs}}
 
 enum e2 {
   E2
-} __tag_no_arg; // expected-error {{'btf_tag' attribute only applies to variables, functions, structs, unions, classes, and non-static data members}}
+} __tag_no_arg; // expected-error {{'btf_tag' attribute only applies to variables, functions, structs, unions, classes, non-static data members, and typedefs}}
 
 enum e3 {
   E3
-} __tag_2_arg; // expected-error {{'btf_tag' attribute only applies to variables, functions, structs, unions, classes, and non-static data members}}
+} __tag_2_arg; // expected-error {{'btf_tag' attribute only applies to variables, functions, structs, unions, classes, non-static data members, and typedefs}}
 
 int __tag1 __tag2 foo(struct t1 *arg, struct t2 *arg2);
 int __tag2 __tag3 foo(struct t1 *arg, struct t2 *arg2);
 int __tag1 foo(struct t1 *arg __tag1, struct t2 *arg2) {
   return arg->a + arg2->a;
 }
+
+typedef unsigned __tag1 * __u1;
+__u1 convert1(long arg) {
+  return (__u1)arg;
+}
+
+typedef struct { int a; int b; } __u2 __tag1;
+__u2 * convert2(long arg) {
+  return (__u2 *)arg;
+}
Index: clang/test/Misc/pragma-attribute-supported-attributes-list.test
===================================================================
--- clang/test/Misc/pragma-attribute-supported-attributes-list.test
+++ clang/test/Misc/pragma-attribute-supported-attributes-list.test
@@ -22,7 +22,7 @@
 // CHECK-NEXT: Assumption (SubjectMatchRule_function, SubjectMatchRule_objc_method)
 // CHECK-NEXT: Availability ((SubjectMatchRule_record, SubjectMatchRule_enum, SubjectMatchRule_enum_constant, SubjectMatchRule_field, SubjectMatchRule_function, SubjectMatchRule_namespace, SubjectMatchRule_objc_category, SubjectMatchRule_objc_implementation, SubjectMatchRule_objc_interface, SubjectMatchRule_objc_method, SubjectMatchRule_objc_property, SubjectMatchRule_objc_protocol, SubjectMatchRule_record, SubjectMatchRule_type_alias, SubjectMatchRule_variable))
 // CHECK-NEXT: BPFPreserveAccessIndex (SubjectMatchRule_record)
-// CHECK-NEXT: BTFTag (SubjectMatchRule_variable, SubjectMatchRule_function, SubjectMatchRule_record, SubjectMatchRule_field)
+// CHECK-NEXT: BTFTag (SubjectMatchRule_variable, SubjectMatchRule_function, SubjectMatchRule_record, SubjectMatchRule_field, SubjectMatchRule_type_alias)
 // CHECK-NEXT: BuiltinAlias (SubjectMatchRule_function)
 // CHECK-NEXT: CFAuditedTransfer (SubjectMatchRule_function)
 // CHECK-NEXT: CFConsumed (SubjectMatchRule_variable_is_parameter)
Index: clang/include/clang/Basic/AttrDocs.td
===================================================================
--- clang/include/clang/Basic/AttrDocs.td
+++ clang/include/clang/Basic/AttrDocs.td
@@ -2016,9 +2016,10 @@
   let Content = [{
 Clang supports the ``__attribute__((btf_tag("ARGUMENT")))`` attribute for all
 targets. This attribute may be attached to a struct/union, struct/union field,
-function, function parameter or variable declaration. If -g is specified,
-the ``ARGUMENT`` info will be preserved in IR and be emitted to dwarf.
-For BPF targets, the ``ARGUMENT`` info will be emitted to .BTF ELF section too.
+function, function parameter, variable or typedef declaration. If -g is
+specified, the ``ARGUMENT`` info will be preserved in IR and be emitted to
+dwarf.  For BPF targets, the ``ARGUMENT`` info will be emitted to .BTF ELF
+section too.
   }];
 }
 
Index: clang/include/clang/Basic/Attr.td
===================================================================
--- clang/include/clang/Basic/Attr.td
+++ clang/include/clang/Basic/Attr.td
@@ -1838,7 +1838,8 @@
 def BTFTag : InheritableAttr {
   let Spellings = [Clang<"btf_tag">];
   let Args = [StringArgument<"BTFTag">];
-  let Subjects = SubjectList<[Var, Function, Record, Field], ErrorDiag>;
+  let Subjects = SubjectList<[Var, Function, Record, Field, TypedefName],
+                             ErrorDiag>;
   let Documentation = [BTFTagDocs];
   let LangOpts = [COnly];
 }


-------------- next part --------------
A non-text attachment was scrubbed...
Name: D110127.373784.patch
Type: text/x-patch
Size: 4408 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/cfe-commits/attachments/20210921/4758e59a/attachment-0001.bin>


More information about the cfe-commits mailing list