[Clang] Ignore BTFTag attr if used as a type attribute
authorYonghong Song <yhs@fb.com>
Mon, 20 Sep 2021 23:23:46 +0000 (16:23 -0700)
committerYonghong Song <yhs@fb.com>
Wed, 22 Sep 2021 20:48:29 +0000 (13:48 -0700)
commitb875343873a584965daf507d73ff1fe71eab1953
tree9641c2290cc5565527836145bc4090f169972fb3
parenta40a08ed988f4da0183622ff62bc151712bd9de0
[Clang] Ignore BTFTag attr if used as a type attribute

Currently, linux kernel has a __user attribute ([1]) defined as
   __attribute__((noderef, address_space(__user)))
which is used by sparse tool ([2]) to do some
type checking of pointers to user space memory.
During normal compilation, __user will be defined
to nothing so it won't have an impact on compilation.

The btf_tag attribute, which is motivated by
carrying linux kernel annotations into dwarf/BTF,
is introduced in [3]. We intended to define __user as
   __attribute__((btf_tag("user")))
so such information will be encoded in dwarf/BTF
and can be used later by bpf verification or other
tracing tools.

But linux kernel __user attribute is also used during
type conversion which btf_tag doesn't support ([4]) since
such type conversion is only used for compiler analysis
and not encoded in dwarf/btf. Theoretically, it is
possible for clang to understand these tags and
do a sparse-like type checking work. But I would like
to leave that to future work and for now suggest simply
ignore these btf_tag attributes if they are used
as type attributes.

  [1] https://github.com/torvalds/linux/blob/master/include/linux/compiler_types.h#L10
  [2] https://sparse.docs.kernel.org/en/latest/
  [3] https://reviews.llvm.org/D106614
  [4] https://github.com/torvalds/linux/blob/master/fs/binfmt_flat.c#L135

Differential Revision: https://reviews.llvm.org/D110116
clang/include/clang/Basic/Attr.td
clang/include/clang/Basic/AttrDocs.td
clang/lib/AST/TypePrinter.cpp
clang/lib/Sema/SemaType.cpp
clang/test/Sema/attr-btf_tag.c