Skip to content

Commit 8a3e4b5

Browse files
authored
[clang][NFC] Annotate Type bit-fields with clang::preferred_type (#70349)
This patch adds `clang::preferred_type` annotations to Type-related bit-fields. At the moment only debug info takes advantage of this annotation. See more in #69104 This patch also propagates underlying type of several enums from bit-field declaration to enum declaration. I don't see how having them diverge helps.
1 parent 7d039ef commit 8a3e4b5

File tree

2 files changed

+57
-2
lines changed

2 files changed

+57
-2
lines changed

clang/include/clang/AST/Type.h

Lines changed: 49 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1615,22 +1615,28 @@ class alignas(TypeAlignment) Type : public ExtQualsTypeCommonBase {
16151615
template <class T> friend class TypePropertyCache;
16161616

16171617
/// TypeClass bitfield - Enum that specifies what subclass this belongs to.
1618+
LLVM_PREFERRED_TYPE(TypeClass)
16181619
unsigned TC : 8;
16191620

16201621
/// Store information on the type dependency.
1622+
LLVM_PREFERRED_TYPE(TypeDependence)
16211623
unsigned Dependence : llvm::BitWidth<TypeDependence>;
16221624

16231625
/// True if the cache (i.e. the bitfields here starting with
16241626
/// 'Cache') is valid.
1627+
LLVM_PREFERRED_TYPE(bool)
16251628
mutable unsigned CacheValid : 1;
16261629

16271630
/// Linkage of this type.
1631+
LLVM_PREFERRED_TYPE(Linkage)
16281632
mutable unsigned CachedLinkage : 3;
16291633

16301634
/// Whether this type involves and local or unnamed types.
1635+
LLVM_PREFERRED_TYPE(bool)
16311636
mutable unsigned CachedLocalOrUnnamed : 1;
16321637

16331638
/// Whether this type comes from an AST file.
1639+
LLVM_PREFERRED_TYPE(bool)
16341640
mutable unsigned FromAST : 1;
16351641

16361642
bool isCacheValid() const {
@@ -1656,10 +1662,12 @@ class alignas(TypeAlignment) Type : public ExtQualsTypeCommonBase {
16561662
class ArrayTypeBitfields {
16571663
friend class ArrayType;
16581664

1665+
LLVM_PREFERRED_TYPE(TypeBitfields)
16591666
unsigned : NumTypeBits;
16601667

16611668
/// CVR qualifiers from declarations like
16621669
/// 'int X[static restrict 4]'. For function parameters only.
1670+
LLVM_PREFERRED_TYPE(Qualifiers)
16631671
unsigned IndexTypeQuals : 3;
16641672

16651673
/// Storage class qualifiers from declarations like
@@ -1672,15 +1680,18 @@ class alignas(TypeAlignment) Type : public ExtQualsTypeCommonBase {
16721680
class ConstantArrayTypeBitfields {
16731681
friend class ConstantArrayType;
16741682

1683+
LLVM_PREFERRED_TYPE(ArrayTypeBitfields)
16751684
unsigned : NumArrayTypeBits;
16761685

16771686
/// Whether we have a stored size expression.
1687+
LLVM_PREFERRED_TYPE(bool)
16781688
unsigned HasStoredSizeExpr : 1;
16791689
};
16801690

16811691
class BuiltinTypeBitfields {
16821692
friend class BuiltinType;
16831693

1694+
LLVM_PREFERRED_TYPE(TypeBitfields)
16841695
unsigned : NumTypeBits;
16851696

16861697
/// The kind (BuiltinType::Kind) of builtin type this is.
@@ -1695,15 +1706,18 @@ class alignas(TypeAlignment) Type : public ExtQualsTypeCommonBase {
16951706
friend class FunctionProtoType;
16961707
friend class FunctionType;
16971708

1709+
LLVM_PREFERRED_TYPE(TypeBitfields)
16981710
unsigned : NumTypeBits;
16991711

17001712
/// Extra information which affects how the function is called, like
17011713
/// regparm and the calling convention.
1714+
LLVM_PREFERRED_TYPE(CallingConv)
17021715
unsigned ExtInfo : 13;
17031716

17041717
/// The ref-qualifier associated with a \c FunctionProtoType.
17051718
///
17061719
/// This is a value of type \c RefQualifierKind.
1720+
LLVM_PREFERRED_TYPE(RefQualifierKind)
17071721
unsigned RefQualifier : 2;
17081722

17091723
/// Used only by FunctionProtoType, put here to pack with the
@@ -1712,8 +1726,10 @@ class alignas(TypeAlignment) Type : public ExtQualsTypeCommonBase {
17121726
///
17131727
/// C++ 8.3.5p4: The return type, the parameter type list and the
17141728
/// cv-qualifier-seq, [...], are part of the function type.
1729+
LLVM_PREFERRED_TYPE(Qualifiers)
17151730
unsigned FastTypeQuals : Qualifiers::FastWidth;
17161731
/// Whether this function has extended Qualifiers.
1732+
LLVM_PREFERRED_TYPE(bool)
17171733
unsigned HasExtQuals : 1;
17181734

17191735
/// The number of parameters this function has, not counting '...'.
@@ -1723,24 +1739,30 @@ class alignas(TypeAlignment) Type : public ExtQualsTypeCommonBase {
17231739
unsigned NumParams : 16;
17241740

17251741
/// The type of exception specification this function has.
1742+
LLVM_PREFERRED_TYPE(ExceptionSpecificationType)
17261743
unsigned ExceptionSpecType : 4;
17271744

17281745
/// Whether this function has extended parameter information.
1746+
LLVM_PREFERRED_TYPE(bool)
17291747
unsigned HasExtParameterInfos : 1;
17301748

17311749
/// Whether this function has extra bitfields for the prototype.
1750+
LLVM_PREFERRED_TYPE(bool)
17321751
unsigned HasExtraBitfields : 1;
17331752

17341753
/// Whether the function is variadic.
1754+
LLVM_PREFERRED_TYPE(bool)
17351755
unsigned Variadic : 1;
17361756

17371757
/// Whether this function has a trailing return type.
1758+
LLVM_PREFERRED_TYPE(bool)
17381759
unsigned HasTrailingReturn : 1;
17391760
};
17401761

17411762
class ObjCObjectTypeBitfields {
17421763
friend class ObjCObjectType;
17431764

1765+
LLVM_PREFERRED_TYPE(TypeBitfields)
17441766
unsigned : NumTypeBits;
17451767

17461768
/// The number of type arguments stored directly on this object type.
@@ -1750,12 +1772,14 @@ class alignas(TypeAlignment) Type : public ExtQualsTypeCommonBase {
17501772
unsigned NumProtocols : 6;
17511773

17521774
/// Whether this is a "kindof" type.
1775+
LLVM_PREFERRED_TYPE(bool)
17531776
unsigned IsKindOf : 1;
17541777
};
17551778

17561779
class ReferenceTypeBitfields {
17571780
friend class ReferenceType;
17581781

1782+
LLVM_PREFERRED_TYPE(TypeBitfields)
17591783
unsigned : NumTypeBits;
17601784

17611785
/// True if the type was originally spelled with an lvalue sigil.
@@ -1769,19 +1793,23 @@ class alignas(TypeAlignment) Type : public ExtQualsTypeCommonBase {
17691793
/// ref &&a; // lvalue, inner ref
17701794
/// rvref &a; // lvalue, inner ref, spelled lvalue
17711795
/// rvref &&a; // rvalue, inner ref
1796+
LLVM_PREFERRED_TYPE(bool)
17721797
unsigned SpelledAsLValue : 1;
17731798

17741799
/// True if the inner type is a reference type. This only happens
17751800
/// in non-canonical forms.
1801+
LLVM_PREFERRED_TYPE(bool)
17761802
unsigned InnerRef : 1;
17771803
};
17781804

17791805
class TypeWithKeywordBitfields {
17801806
friend class TypeWithKeyword;
17811807

1808+
LLVM_PREFERRED_TYPE(TypeBitfields)
17821809
unsigned : NumTypeBits;
17831810

17841811
/// An ElaboratedTypeKeyword. 8 bits for efficient access.
1812+
LLVM_PREFERRED_TYPE(ElaboratedTypeKeyword)
17851813
unsigned Keyword : 8;
17861814
};
17871815

@@ -1790,21 +1818,24 @@ class alignas(TypeAlignment) Type : public ExtQualsTypeCommonBase {
17901818
class ElaboratedTypeBitfields {
17911819
friend class ElaboratedType;
17921820

1793-
unsigned : NumTypeBits;
1821+
LLVM_PREFERRED_TYPE(TypeWithKeywordBitfields)
17941822
unsigned : NumTypeWithKeywordBits;
17951823

17961824
/// Whether the ElaboratedType has a trailing OwnedTagDecl.
1825+
LLVM_PREFERRED_TYPE(bool)
17971826
unsigned HasOwnedTagDecl : 1;
17981827
};
17991828

18001829
class VectorTypeBitfields {
18011830
friend class VectorType;
18021831
friend class DependentVectorType;
18031832

1833+
LLVM_PREFERRED_TYPE(TypeBitfields)
18041834
unsigned : NumTypeBits;
18051835

18061836
/// The kind of vector, either a generic vector type or some
18071837
/// target-specific vector type such as for AltiVec or Neon.
1838+
LLVM_PREFERRED_TYPE(VectorKind)
18081839
unsigned VecKind : 4;
18091840
/// The number of elements in the vector.
18101841
uint32_t NumElements;
@@ -1813,19 +1844,22 @@ class alignas(TypeAlignment) Type : public ExtQualsTypeCommonBase {
18131844
class AttributedTypeBitfields {
18141845
friend class AttributedType;
18151846

1847+
LLVM_PREFERRED_TYPE(TypeBitfields)
18161848
unsigned : NumTypeBits;
18171849

1818-
/// An AttributedType::Kind
1850+
LLVM_PREFERRED_TYPE(attr::Kind)
18191851
unsigned AttrKind : 32 - NumTypeBits;
18201852
};
18211853

18221854
class AutoTypeBitfields {
18231855
friend class AutoType;
18241856

1857+
LLVM_PREFERRED_TYPE(TypeBitfields)
18251858
unsigned : NumTypeBits;
18261859

18271860
/// Was this placeholder type spelled as 'auto', 'decltype(auto)',
18281861
/// or '__auto_type'? AutoTypeKeyword value.
1862+
LLVM_PREFERRED_TYPE(AutoTypeKeyword)
18291863
unsigned Keyword : 2;
18301864

18311865
/// The number of template arguments in the type-constraints, which is
@@ -1842,33 +1876,41 @@ class alignas(TypeAlignment) Type : public ExtQualsTypeCommonBase {
18421876
friend class TypeOfType;
18431877
friend class TypeOfExprType;
18441878

1879+
LLVM_PREFERRED_TYPE(TypeBitfields)
18451880
unsigned : NumTypeBits;
1881+
LLVM_PREFERRED_TYPE(bool)
18461882
unsigned IsUnqual : 1; // If true: typeof_unqual, else: typeof
18471883
};
18481884

18491885
class UsingBitfields {
18501886
friend class UsingType;
18511887

1888+
LLVM_PREFERRED_TYPE(TypeBitfields)
18521889
unsigned : NumTypeBits;
18531890

18541891
/// True if the underlying type is different from the declared one.
1892+
LLVM_PREFERRED_TYPE(bool)
18551893
unsigned hasTypeDifferentFromDecl : 1;
18561894
};
18571895

18581896
class TypedefBitfields {
18591897
friend class TypedefType;
18601898

1899+
LLVM_PREFERRED_TYPE(TypeBitfields)
18611900
unsigned : NumTypeBits;
18621901

18631902
/// True if the underlying type is different from the declared one.
1903+
LLVM_PREFERRED_TYPE(bool)
18641904
unsigned hasTypeDifferentFromDecl : 1;
18651905
};
18661906

18671907
class SubstTemplateTypeParmTypeBitfields {
18681908
friend class SubstTemplateTypeParmType;
18691909

1910+
LLVM_PREFERRED_TYPE(TypeBitfields)
18701911
unsigned : NumTypeBits;
18711912

1913+
LLVM_PREFERRED_TYPE(bool)
18721914
unsigned HasNonCanonicalUnderlyingType : 1;
18731915

18741916
// The index of the template parameter this substitution represents.
@@ -1885,6 +1927,7 @@ class alignas(TypeAlignment) Type : public ExtQualsTypeCommonBase {
18851927
class SubstTemplateTypeParmPackTypeBitfields {
18861928
friend class SubstTemplateTypeParmPackType;
18871929

1930+
LLVM_PREFERRED_TYPE(TypeBitfields)
18881931
unsigned : NumTypeBits;
18891932

18901933
// The index of the template parameter this substitution represents.
@@ -1900,9 +1943,11 @@ class alignas(TypeAlignment) Type : public ExtQualsTypeCommonBase {
19001943
class TemplateSpecializationTypeBitfields {
19011944
friend class TemplateSpecializationType;
19021945

1946+
LLVM_PREFERRED_TYPE(TypeBitfields)
19031947
unsigned : NumTypeBits;
19041948

19051949
/// Whether this template specialization type is a substituted type alias.
1950+
LLVM_PREFERRED_TYPE(bool)
19061951
unsigned TypeAlias : 1;
19071952

19081953
/// The number of template arguments named in this class template
@@ -1918,6 +1963,7 @@ class alignas(TypeAlignment) Type : public ExtQualsTypeCommonBase {
19181963
class DependentTemplateSpecializationTypeBitfields {
19191964
friend class DependentTemplateSpecializationType;
19201965

1966+
LLVM_PREFERRED_TYPE(TypeWithKeywordBitfields)
19211967
unsigned : NumTypeWithKeywordBits;
19221968

19231969
/// The number of template arguments named in this class template
@@ -1933,6 +1979,7 @@ class alignas(TypeAlignment) Type : public ExtQualsTypeCommonBase {
19331979
class PackExpansionTypeBitfields {
19341980
friend class PackExpansionType;
19351981

1982+
LLVM_PREFERRED_TYPE(TypeBitfields)
19361983
unsigned : NumTypeBits;
19371984

19381985
/// The number of expansions that this pack expansion will

llvm/include/llvm/Support/Compiler.h

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -569,4 +569,12 @@ void AnnotateIgnoreWritesEnd(const char *file, int line);
569569
#define LLVM_NO_PROFILE_INSTRUMENT_FUNCTION
570570
#endif
571571

572+
/// \macro LLVM_PREFERRED_TYPE
573+
/// Adjust type of bit-field in debug info.
574+
#if __has_attribute(preferred_type)
575+
#define LLVM_PREFERRED_TYPE(T) __attribute__((preferred_type(T)))
576+
#else
577+
#define LLVM_PREFERRED_TYPE(T)
578+
#endif
579+
572580
#endif

0 commit comments

Comments
 (0)