Skip to content

Commit 3251ba2

Browse files
committed
[Attr] Fix a btf_type_tag AST generation
Current ASTContext.getAttributedType() takes attribute kind, ModifiedType and EquivType as the hash to decide whether an AST node has been generated or note. But this is not enough for btf_type_tag as the attribute might have the same ModifiedType and EquivType, but still have different string associated with attribute. For example, for a data structure like below, struct map_value { int __attribute__((btf_type_tag("tag1"))) __attribute__((btf_type_tag("tag3"))) *a; int __attribute__((btf_type_tag("tag2"))) __attribute__((btf_type_tag("tag4"))) *b; }; The current ASTContext.getAttributedType() will produce an AST similar to below: struct map_value { int __attribute__((btf_type_tag("tag1"))) __attribute__((btf_type_tag("tag3"))) *a; int __attribute__((btf_type_tag("tag1"))) __attribute__((btf_type_tag("tag3"))) *b; }; and this is incorrect. It is very difficult to use the current AttributedType as it is hard to get the tag information. To fix the problem, this patch introduced BTFTagAttributedType which is similar to AttributedType in many ways but with an additional BTFTypeTagAttr. The tag itself can be retrieved with BTFTypeTagAttr. With the new BTFTagAttributed type, the debuginfo code can be greatly simplified compared to previous TypeLoc based approach. Differential Revision: https://reviews.llvm.org/D120296
1 parent c7dc9db commit 3251ba2

30 files changed

+309
-168
lines changed

clang/include/clang-c/Index.h

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3401,7 +3401,8 @@ enum CXTypeKind {
34013401
CXType_OCLIntelSubgroupAVCImeDualRefStreamin = 175,
34023402

34033403
CXType_ExtVector = 176,
3404-
CXType_Atomic = 177
3404+
CXType_Atomic = 177,
3405+
CXType_BTFTagAttributed = 178
34053406
};
34063407

34073408
/**

clang/include/clang/AST/ASTContext.h

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -264,6 +264,7 @@ class ASTContext : public RefCountedBase<ASTContext> {
264264
mutable llvm::FoldingSet<PipeType> PipeTypes;
265265
mutable llvm::FoldingSet<BitIntType> BitIntTypes;
266266
mutable llvm::FoldingSet<DependentBitIntType> DependentBitIntTypes;
267+
llvm::FoldingSet<BTFTagAttributedType> BTFTagAttributedTypes;
267268

268269
mutable llvm::FoldingSet<QualifiedTemplateName> QualifiedTemplateNames;
269270
mutable llvm::FoldingSet<DependentTemplateName> DependentTemplateNames;
@@ -1592,6 +1593,9 @@ class ASTContext : public RefCountedBase<ASTContext> {
15921593
QualType modifiedType,
15931594
QualType equivalentType);
15941595

1596+
QualType getBTFTagAttributedType(const BTFTypeTagAttr *BTFAttr,
1597+
QualType Wrapped);
1598+
15951599
QualType getSubstTemplateTypeParmType(const TemplateTypeParmType *Replaced,
15961600
QualType Replacement) const;
15971601
QualType getSubstTemplateTypeParmPackType(

clang/include/clang/AST/ASTNodeTraverser.h

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -386,6 +386,9 @@ class ASTNodeTraverser
386386
// FIXME: AttrKind
387387
Visit(T->getModifiedType());
388388
}
389+
void VisitBTFTagAttributedType(const BTFTagAttributedType *T) {
390+
Visit(T->getWrappedType());
391+
}
389392
void VisitSubstTemplateTypeParmType(const SubstTemplateTypeParmType *T) {
390393
Visit(T->getReplacedParameter());
391394
}

clang/include/clang/AST/PropertiesBase.td

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -79,6 +79,7 @@ def AttrKind : EnumPropertyType<"attr::Kind">;
7979
def AutoTypeKeyword : EnumPropertyType;
8080
def Bool : PropertyType<"bool">;
8181
def BuiltinTypeKind : EnumPropertyType<"BuiltinType::Kind">;
82+
def BTFTypeTagAttr : PropertyType<"const BTFTypeTagAttr *">;
8283
def CallingConv : EnumPropertyType;
8384
def DeclarationName : PropertyType;
8485
def DeclarationNameKind : EnumPropertyType<"DeclarationName::NameKind">;

clang/include/clang/AST/RecursiveASTVisitor.h

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1036,6 +1036,9 @@ DEF_TRAVERSE_TYPE(InjectedClassNameType, {})
10361036
DEF_TRAVERSE_TYPE(AttributedType,
10371037
{ TRY_TO(TraverseType(T->getModifiedType())); })
10381038

1039+
DEF_TRAVERSE_TYPE(BTFTagAttributedType,
1040+
{ TRY_TO(TraverseType(T->getWrappedType())); })
1041+
10391042
DEF_TRAVERSE_TYPE(ParenType, { TRY_TO(TraverseType(T->getInnerType())); })
10401043

10411044
DEF_TRAVERSE_TYPE(MacroQualifiedType,
@@ -1322,6 +1325,9 @@ DEF_TRAVERSE_TYPELOC(MacroQualifiedType,
13221325
DEF_TRAVERSE_TYPELOC(AttributedType,
13231326
{ TRY_TO(TraverseTypeLoc(TL.getModifiedLoc())); })
13241327

1328+
DEF_TRAVERSE_TYPELOC(BTFTagAttributedType,
1329+
{ TRY_TO(TraverseTypeLoc(TL.getWrappedLoc())); })
1330+
13251331
DEF_TRAVERSE_TYPELOC(ElaboratedType, {
13261332
if (TL.getQualifierLoc()) {
13271333
TRY_TO(TraverseNestedNameSpecifierLoc(TL.getQualifierLoc()));

clang/include/clang/AST/Type.h

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -57,6 +57,7 @@
5757

5858
namespace clang {
5959

60+
class BTFTypeTagAttr;
6061
class ExtQuals;
6162
class QualType;
6263
class ConceptDecl;
@@ -4789,6 +4790,40 @@ class AttributedType : public Type, public llvm::FoldingSetNode {
47894790
}
47904791
};
47914792

4793+
class BTFTagAttributedType : public Type, public llvm::FoldingSetNode {
4794+
private:
4795+
friend class ASTContext; // ASTContext creates these
4796+
4797+
QualType WrappedType;
4798+
const BTFTypeTagAttr *BTFAttr;
4799+
4800+
BTFTagAttributedType(QualType Canon, QualType Wrapped,
4801+
const BTFTypeTagAttr *BTFAttr)
4802+
: Type(BTFTagAttributed, Canon, Wrapped->getDependence()),
4803+
WrappedType(Wrapped), BTFAttr(BTFAttr) {}
4804+
4805+
public:
4806+
QualType getWrappedType() const { return WrappedType; }
4807+
const BTFTypeTagAttr *getAttr() const { return BTFAttr; }
4808+
4809+
bool isSugared() const { return true; }
4810+
QualType desugar() const { return getWrappedType(); }
4811+
4812+
void Profile(llvm::FoldingSetNodeID &ID) {
4813+
Profile(ID, WrappedType, BTFAttr);
4814+
}
4815+
4816+
static void Profile(llvm::FoldingSetNodeID &ID, QualType Wrapped,
4817+
const BTFTypeTagAttr *BTFAttr) {
4818+
ID.AddPointer(Wrapped.getAsOpaquePtr());
4819+
ID.AddPointer(BTFAttr);
4820+
}
4821+
4822+
static bool classof(const Type *T) {
4823+
return T->getTypeClass() == BTFTagAttributed;
4824+
}
4825+
};
4826+
47924827
class TemplateTypeParmType : public Type, public llvm::FoldingSetNode {
47934828
friend class ASTContext; // ASTContext creates these
47944829

@@ -7229,6 +7264,8 @@ template <typename T> const T *Type::getAsAdjusted() const {
72297264
while (Ty) {
72307265
if (const auto *A = dyn_cast<AttributedType>(Ty))
72317266
Ty = A->getModifiedType().getTypePtr();
7267+
else if (const auto *A = dyn_cast<BTFTagAttributedType>(Ty))
7268+
Ty = A->getWrappedType().getTypePtr();
72327269
else if (const auto *E = dyn_cast<ElaboratedType>(Ty))
72337270
Ty = E->desugar().getTypePtr();
72347271
else if (const auto *P = dyn_cast<ParenType>(Ty))

clang/include/clang/AST/TypeLoc.h

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -901,6 +901,29 @@ class AttributedTypeLoc : public ConcreteTypeLoc<UnqualTypeLoc,
901901
}
902902
};
903903

904+
struct BTFTagAttributedLocInfo {}; // Nothing.
905+
906+
/// Type source information for an btf_tag attributed type.
907+
class BTFTagAttributedTypeLoc
908+
: public ConcreteTypeLoc<UnqualTypeLoc, BTFTagAttributedTypeLoc,
909+
BTFTagAttributedType, BTFTagAttributedLocInfo> {
910+
public:
911+
TypeLoc getWrappedLoc() const { return getInnerTypeLoc(); }
912+
913+
/// The btf_type_tag attribute.
914+
const BTFTypeTagAttr *getAttr() const { return getTypePtr()->getAttr(); }
915+
916+
template <typename T> T *getAttrAs() {
917+
return dyn_cast_or_null<T>(getAttr());
918+
}
919+
920+
SourceRange getLocalSourceRange() const;
921+
922+
void initializeLocal(ASTContext &Context, SourceLocation loc) {}
923+
924+
QualType getInnerType() const { return getTypePtr()->getWrappedType(); }
925+
};
926+
904927
struct ObjCObjectTypeLocInfo {
905928
SourceLocation TypeArgsLAngleLoc;
906929
SourceLocation TypeArgsRAngleLoc;
@@ -2589,6 +2612,8 @@ inline T TypeLoc::getAsAdjusted() const {
25892612
Cur = PTL.getInnerLoc();
25902613
else if (auto ATL = Cur.getAs<AttributedTypeLoc>())
25912614
Cur = ATL.getModifiedLoc();
2615+
else if (auto ATL = Cur.getAs<BTFTagAttributedTypeLoc>())
2616+
Cur = ATL.getWrappedLoc();
25922617
else if (auto ETL = Cur.getAs<ElaboratedTypeLoc>())
25932618
Cur = ETL.getNamedTypeLoc();
25942619
else if (auto ATL = Cur.getAs<AdjustedTypeLoc>())

clang/include/clang/AST/TypeProperties.td

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -619,6 +619,19 @@ let Class = AttributedType in {
619619
}]>;
620620
}
621621

622+
let Class = BTFTagAttributedType in {
623+
def : Property<"attr", BTFTypeTagAttr> {
624+
let Read = [{ node->getAttr() }];
625+
}
626+
def : Property<"wrappedType", QualType> {
627+
let Read = [{ node->getWrappedType() }];
628+
}
629+
630+
def : Creator<[{
631+
return ctx.getBTFTagAttributedType(attr, wrappedType);
632+
}]>;
633+
}
634+
622635
let Class = DependentAddressSpaceType in {
623636
def : Property<"pointeeType", QualType> {
624637
let Read = [{ node->getPointeeType() }];

clang/include/clang/Basic/TypeNodes.td

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -91,6 +91,7 @@ def RecordType : TypeNode<TagType>, LeafType;
9191
def EnumType : TypeNode<TagType>, LeafType;
9292
def ElaboratedType : TypeNode<Type>, NeverCanonical;
9393
def AttributedType : TypeNode<Type>, NeverCanonical;
94+
def BTFTagAttributedType : TypeNode<Type>, NeverCanonical;
9495
def TemplateTypeParmType : TypeNode<Type>, AlwaysDependent, LeafType;
9596
def SubstTemplateTypeParmType : TypeNode<Type>, NeverCanonical;
9697
def SubstTemplateTypeParmPackType : TypeNode<Type>, AlwaysDependent;

clang/include/clang/Serialization/ASTRecordReader.h

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -326,6 +326,11 @@ class ASTRecordReader
326326
/// Reads attributes from the current stream position, advancing Idx.
327327
void readAttributes(AttrVec &Attrs);
328328

329+
/// Read an BTFTypeTagAttr object.
330+
BTFTypeTagAttr *readBTFTypeTagAttr() {
331+
return cast<BTFTypeTagAttr>(readAttr());
332+
}
333+
329334
/// Reads a token out of a record, advancing Idx.
330335
Token readToken() {
331336
return Reader->ReadToken(*F, Record, Idx);

clang/include/clang/Serialization/ASTRecordWriter.h

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -123,6 +123,9 @@ class ASTRecordWriter
123123
AddStmt(const_cast<Stmt*>(S));
124124
}
125125

126+
/// Write an BTFTypeTagAttr object.
127+
void writeBTFTypeTagAttr(const BTFTypeTagAttr *A) { AddAttr(A); }
128+
126129
/// Add a definition for the given function to the queue of statements
127130
/// to emit.
128131
void AddFunctionDefinition(const FunctionDecl *FD);

clang/include/clang/Serialization/TypeBitCodes.def

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -63,5 +63,6 @@ TYPE_BIT_CODE(DependentBitInt, DEPENDENT_BIT_INT, 51)
6363
TYPE_BIT_CODE(ConstantMatrix, CONSTANT_MATRIX, 52)
6464
TYPE_BIT_CODE(DependentSizedMatrix, DEPENDENT_SIZE_MATRIX, 53)
6565
TYPE_BIT_CODE(Using, USING, 54)
66+
TYPE_BIT_CODE(BTFTagAttributed, BTFTAG_ATTRIBUTED, 55)
6667

6768
#undef TYPE_BIT_CODE

clang/lib/AST/ASTContext.cpp

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2380,6 +2380,10 @@ TypeInfo ASTContext::getTypeInfoImpl(const Type *T) const {
23802380
return getTypeInfo(
23812381
cast<AttributedType>(T)->getEquivalentType().getTypePtr());
23822382

2383+
case Type::BTFTagAttributed:
2384+
return getTypeInfo(
2385+
cast<BTFTagAttributedType>(T)->getWrappedType().getTypePtr());
2386+
23832387
case Type::Atomic: {
23842388
// Start with the base type information.
23852389
TypeInfo Info = getTypeInfo(cast<AtomicType>(T)->getValueType());
@@ -4686,6 +4690,26 @@ QualType ASTContext::getAttributedType(attr::Kind attrKind,
46864690
return QualType(type, 0);
46874691
}
46884692

4693+
QualType ASTContext::getBTFTagAttributedType(const BTFTypeTagAttr *BTFAttr,
4694+
QualType Wrapped) {
4695+
llvm::FoldingSetNodeID ID;
4696+
BTFTagAttributedType::Profile(ID, Wrapped, BTFAttr);
4697+
4698+
void *InsertPos = nullptr;
4699+
BTFTagAttributedType *Ty =
4700+
BTFTagAttributedTypes.FindNodeOrInsertPos(ID, InsertPos);
4701+
if (Ty)
4702+
return QualType(Ty, 0);
4703+
4704+
QualType Canon = getCanonicalType(Wrapped);
4705+
Ty = new (*this, TypeAlignment) BTFTagAttributedType(Canon, Wrapped, BTFAttr);
4706+
4707+
Types.push_back(Ty);
4708+
BTFTagAttributedTypes.InsertNode(Ty, InsertPos);
4709+
4710+
return QualType(Ty, 0);
4711+
}
4712+
46894713
/// Retrieve a substitution-result type.
46904714
QualType
46914715
ASTContext::getSubstTemplateTypeParmType(const TemplateTypeParmType *Parm,

clang/lib/AST/ASTStructuralEquivalence.cpp

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -932,6 +932,13 @@ static bool IsStructurallyEquivalent(StructuralEquivalenceContext &Context,
932932
return false;
933933
break;
934934

935+
case Type::BTFTagAttributed:
936+
if (!IsStructurallyEquivalent(
937+
Context, cast<BTFTagAttributedType>(T1)->getWrappedType(),
938+
cast<BTFTagAttributedType>(T2)->getWrappedType()))
939+
return false;
940+
break;
941+
935942
case Type::Paren:
936943
if (!IsStructurallyEquivalent(Context, cast<ParenType>(T1)->getInnerType(),
937944
cast<ParenType>(T2)->getInnerType()))

clang/lib/AST/ItaniumMangle.cpp

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2286,6 +2286,7 @@ bool CXXNameMangler::mangleUnresolvedTypeOrSimpleId(QualType Ty,
22862286
case Type::FunctionNoProto:
22872287
case Type::Paren:
22882288
case Type::Attributed:
2289+
case Type::BTFTagAttributed:
22892290
case Type::Auto:
22902291
case Type::DeducedTemplateSpecialization:
22912292
case Type::PackExpansion:

clang/lib/AST/TypeLoc.cpp

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -507,6 +507,10 @@ SourceRange AttributedTypeLoc::getLocalSourceRange() const {
507507
return getAttr() ? getAttr()->getRange() : SourceRange();
508508
}
509509

510+
SourceRange BTFTagAttributedTypeLoc::getLocalSourceRange() const {
511+
return getAttr() ? getAttr()->getRange() : SourceRange();
512+
}
513+
510514
void TypeOfTypeLoc::initializeLocal(ASTContext &Context,
511515
SourceLocation Loc) {
512516
TypeofLikeTypeLoc<TypeOfTypeLoc, TypeOfType, TypeOfTypeLocInfo>
@@ -683,6 +687,10 @@ namespace {
683687
return Visit(T.getModifiedLoc());
684688
}
685689

690+
TypeLoc VisitBTFTagAttributedTypeLoc(BTFTagAttributedTypeLoc T) {
691+
return Visit(T.getWrappedLoc());
692+
}
693+
686694
TypeLoc VisitMacroQualifiedTypeLoc(MacroQualifiedTypeLoc T) {
687695
return Visit(T.getInnerLoc());
688696
}

clang/lib/AST/TypePrinter.cpp

Lines changed: 15 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -235,6 +235,7 @@ bool TypePrinter::canPrefixQualifiers(const Type *T,
235235
case Type::Pipe:
236236
case Type::BitInt:
237237
case Type::DependentBitInt:
238+
case Type::BTFTagAttributed:
238239
CanPrefixQualifiers = true;
239240
break;
240241

@@ -1689,6 +1690,9 @@ void TypePrinter::printAttributedAfter(const AttributedType *T,
16891690
#include "clang/Basic/AttrList.inc"
16901691
llvm_unreachable("non-type attribute attached to type");
16911692

1693+
case attr::BTFTypeTag:
1694+
llvm_unreachable("BTFTypeTag attribute handled separately");
1695+
16921696
case attr::OpenCLPrivateAddressSpace:
16931697
case attr::OpenCLGlobalAddressSpace:
16941698
case attr::OpenCLGlobalDeviceAddressSpace:
@@ -1763,13 +1767,21 @@ void TypePrinter::printAttributedAfter(const AttributedType *T,
17631767
case attr::ArmMveStrictPolymorphism:
17641768
OS << "__clang_arm_mve_strict_polymorphism";
17651769
break;
1766-
case attr::BTFTypeTag:
1767-
OS << "btf_type_tag";
1768-
break;
17691770
}
17701771
OS << "))";
17711772
}
17721773

1774+
void TypePrinter::printBTFTagAttributedBefore(const BTFTagAttributedType *T,
1775+
raw_ostream &OS) {
1776+
printBefore(T->getWrappedType(), OS);
1777+
OS << " btf_type_tag(" << T->getAttr()->getBTFTypeTag() << ")";
1778+
}
1779+
1780+
void TypePrinter::printBTFTagAttributedAfter(const BTFTagAttributedType *T,
1781+
raw_ostream &OS) {
1782+
printAfter(T->getWrappedType(), OS);
1783+
}
1784+
17731785
void TypePrinter::printObjCInterfaceBefore(const ObjCInterfaceType *T,
17741786
raw_ostream &OS) {
17751787
OS << T->getDecl()->getName();

0 commit comments

Comments
 (0)