Skip to content

Commit 1e20c5d

Browse files
author
git apple-llvm automerger
committed
Merge commit 'eb0fa8bfa356' from llvm.org/main into next
2 parents bcaa095 + eb0fa8b commit 1e20c5d

File tree

5 files changed

+88
-0
lines changed

5 files changed

+88
-0
lines changed

clang/include/clang/Basic/Attr.td

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1852,6 +1852,13 @@ def BTFDeclTag : InheritableAttr {
18521852
let LangOpts = [COnly];
18531853
}
18541854

1855+
def BTFTypeTag : TypeAttr {
1856+
let Spellings = [Clang<"btf_type_tag">];
1857+
let Args = [StringArgument<"BTFTypeTag">];
1858+
let Documentation = [BTFTypeTagDocs];
1859+
let LangOpts = [COnly];
1860+
}
1861+
18551862
def WebAssemblyExportName : InheritableAttr,
18561863
TargetSpecificAttr<TargetWebAssembly> {
18571864
let Spellings = [Clang<"export_name">];

clang/include/clang/Basic/AttrDocs.td

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2182,6 +2182,23 @@ section too.
21822182
}];
21832183
}
21842184

2185+
def BTFTypeTagDocs : Documentation {
2186+
let Category = DocCatType;
2187+
let Content = [{
2188+
Clang supports the ``__attribute__((btf_type_tag("ARGUMENT")))`` attribute for
2189+
all targets. It only has effect when ``-g`` is specified on the command line and
2190+
is currently silently ignored when not applied to a pointer type (note: this
2191+
scenario may be diagnosed in the future).
2192+
2193+
The ``ARGUMENT`` string will be preserved in IR and emitted to DWARF for the
2194+
types used in variable declarations, function declarations, or typedef
2195+
declarations.
2196+
2197+
For BPF targets, the ``ARGUMENT`` string will also be emitted to .BTF ELF
2198+
section.
2199+
}];
2200+
}
2201+
21852202
def MipsInterruptDocs : Documentation {
21862203
let Category = DocCatFunction;
21872204
let Heading = "interrupt (MIPS)";

clang/lib/AST/TypePrinter.cpp

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1758,6 +1758,9 @@ void TypePrinter::printAttributedAfter(const AttributedType *T,
17581758
case attr::ArmMveStrictPolymorphism:
17591759
OS << "__clang_arm_mve_strict_polymorphism";
17601760
break;
1761+
case attr::BTFTypeTag:
1762+
OS << "btf_type_tag";
1763+
break;
17611764
}
17621765
OS << "))";
17631766
}

clang/lib/Sema/SemaType.cpp

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5901,6 +5901,9 @@ namespace {
59015901
void VisitQualifiedTypeLoc(QualifiedTypeLoc TL) {
59025902
Visit(TL.getUnqualifiedLoc());
59035903
}
5904+
// Allow to fill pointee's type locations, e.g.,
5905+
// int __attr * __attr * __attr *p;
5906+
void VisitPointerTypeLoc(PointerTypeLoc TL) { Visit(TL.getNextTypeLoc()); }
59045907
void VisitTypedefTypeLoc(TypedefTypeLoc TL) {
59055908
TL.setNameLoc(DS.getTypeSpecTypeLoc());
59065909
}
@@ -6501,6 +6504,34 @@ QualType Sema::BuildAddressSpaceAttr(QualType &T, Expr *AddrSpace,
65016504
return BuildAddressSpaceAttr(T, ASIdx, AddrSpace, AttrLoc);
65026505
}
65036506

6507+
static void HandleBTFTypeTagAttribute(QualType &Type, const ParsedAttr &Attr,
6508+
TypeProcessingState &State) {
6509+
Sema &S = State.getSema();
6510+
6511+
// Check the number of attribute arguments.
6512+
if (Attr.getNumArgs() != 1) {
6513+
S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments)
6514+
<< Attr << 1;
6515+
Attr.setInvalid();
6516+
return;
6517+
}
6518+
6519+
// Ensure the argument is a string.
6520+
auto *StrLiteral = dyn_cast<StringLiteral>(Attr.getArgAsExpr(0));
6521+
if (!StrLiteral) {
6522+
S.Diag(Attr.getLoc(), diag::err_attribute_argument_type)
6523+
<< Attr << AANT_ArgumentString;
6524+
Attr.setInvalid();
6525+
return;
6526+
}
6527+
6528+
ASTContext &Ctx = S.Context;
6529+
StringRef BTFTypeTag = StrLiteral->getString();
6530+
Type = State.getAttributedType(
6531+
::new (Ctx) BTFTypeTagAttr(Ctx, Attr, BTFTypeTag), Type, Type);
6532+
return;
6533+
}
6534+
65046535
/// HandleAddressSpaceTypeAttribute - Process an address_space attribute on the
65056536
/// specified type. The attribute contains 1 argument, the id of the address
65066537
/// space for the type.
@@ -8261,6 +8292,11 @@ static void processTypeAttrs(TypeProcessingState &state, QualType &type,
82618292
case ParsedAttr::IgnoredAttribute:
82628293
break;
82638294

8295+
case ParsedAttr::AT_BTFTypeTag:
8296+
HandleBTFTypeTagAttribute(type, attr, state);
8297+
attr.setUsedAsTypeAttr();
8298+
break;
8299+
82648300
case ParsedAttr::AT_MayAlias:
82658301
// FIXME: This attribute needs to actually be handled, but if we ignore
82668302
// it it breaks large amounts of Linux software.

clang/test/Sema/attr-btf_type_tag.c

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
// RUN: %clang_cc1 -x c -triple x86_64-pc-linux-gnu -dwarf-version=4 -fsyntax-only -verify %s
2+
3+
#define __tag1 __attribute__((btf_type_tag("tag1")))
4+
#define __tag2 __attribute__((btf_type_tag("tag2")))
5+
#define __tag3 __attribute__((btf_type_tag("tag3")))
6+
#define __tag4 __attribute__((btf_type_tag("tag4")))
7+
#define __tag5 __attribute__((btf_type_tag("tag5")))
8+
#define __tag6 __attribute__((btf_type_tag("tag6")))
9+
10+
int __attribute__((btf_type_tag("tag1", "tag2"))) *invalid1; // expected-error {{'btf_type_tag' attribute takes one argument}}
11+
int __attribute__((btf_type_tag(2))) *invalid2; // expected-error {{'btf_type_tag' attribute requires a string}}
12+
13+
int * __tag1 __tag2 * __tag3 __tag4 * __tag5 __tag6 *g;
14+
15+
typedef void __fn_t(int);
16+
typedef __fn_t __tag1 __tag2 * __tag3 __tag4 *__fn2_t;
17+
struct t {
18+
int __tag1 * __tag2 * __tag3 *a;
19+
int __tag1 __tag2 __tag3 *b;
20+
__fn2_t c;
21+
long d;
22+
};
23+
int __tag4 * __tag5 * __tag6 *foo1(struct t __tag1 * __tag2 * __tag3 *a1) {
24+
return (int __tag4 * __tag5 * __tag6 *)a1[0][0]->d;
25+
}

0 commit comments

Comments
 (0)