Skip to content

Commit

Permalink
[clang][NFC] Annotate Type bit-fields with clang::preferred_type (#…
Browse files Browse the repository at this point in the history
…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.
  • Loading branch information
Endilll committed Nov 2, 2023
1 parent 7d039ef commit 8a3e4b5
Show file tree
Hide file tree
Showing 2 changed files with 57 additions and 2 deletions.
51 changes: 49 additions & 2 deletions clang/include/clang/AST/Type.h
Original file line number Diff line number Diff line change
Expand Up @@ -1615,22 +1615,28 @@ class alignas(TypeAlignment) Type : public ExtQualsTypeCommonBase {
template <class T> friend class TypePropertyCache;

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

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

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

/// Linkage of this type.
LLVM_PREFERRED_TYPE(Linkage)
mutable unsigned CachedLinkage : 3;

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

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

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

LLVM_PREFERRED_TYPE(TypeBitfields)
unsigned : NumTypeBits;

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

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

LLVM_PREFERRED_TYPE(ArrayTypeBitfields)
unsigned : NumArrayTypeBits;

/// Whether we have a stored size expression.
LLVM_PREFERRED_TYPE(bool)
unsigned HasStoredSizeExpr : 1;
};

class BuiltinTypeBitfields {
friend class BuiltinType;

LLVM_PREFERRED_TYPE(TypeBitfields)
unsigned : NumTypeBits;

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

LLVM_PREFERRED_TYPE(TypeBitfields)
unsigned : NumTypeBits;

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

/// The ref-qualifier associated with a \c FunctionProtoType.
///
/// This is a value of type \c RefQualifierKind.
LLVM_PREFERRED_TYPE(RefQualifierKind)
unsigned RefQualifier : 2;

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

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

/// The type of exception specification this function has.
LLVM_PREFERRED_TYPE(ExceptionSpecificationType)
unsigned ExceptionSpecType : 4;

/// Whether this function has extended parameter information.
LLVM_PREFERRED_TYPE(bool)
unsigned HasExtParameterInfos : 1;

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

/// Whether the function is variadic.
LLVM_PREFERRED_TYPE(bool)
unsigned Variadic : 1;

/// Whether this function has a trailing return type.
LLVM_PREFERRED_TYPE(bool)
unsigned HasTrailingReturn : 1;
};

class ObjCObjectTypeBitfields {
friend class ObjCObjectType;

LLVM_PREFERRED_TYPE(TypeBitfields)
unsigned : NumTypeBits;

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

/// Whether this is a "kindof" type.
LLVM_PREFERRED_TYPE(bool)
unsigned IsKindOf : 1;
};

class ReferenceTypeBitfields {
friend class ReferenceType;

LLVM_PREFERRED_TYPE(TypeBitfields)
unsigned : NumTypeBits;

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

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

class TypeWithKeywordBitfields {
friend class TypeWithKeyword;

LLVM_PREFERRED_TYPE(TypeBitfields)
unsigned : NumTypeBits;

/// An ElaboratedTypeKeyword. 8 bits for efficient access.
LLVM_PREFERRED_TYPE(ElaboratedTypeKeyword)
unsigned Keyword : 8;
};

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

unsigned : NumTypeBits;
LLVM_PREFERRED_TYPE(TypeWithKeywordBitfields)
unsigned : NumTypeWithKeywordBits;

/// Whether the ElaboratedType has a trailing OwnedTagDecl.
LLVM_PREFERRED_TYPE(bool)
unsigned HasOwnedTagDecl : 1;
};

class VectorTypeBitfields {
friend class VectorType;
friend class DependentVectorType;

LLVM_PREFERRED_TYPE(TypeBitfields)
unsigned : NumTypeBits;

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

LLVM_PREFERRED_TYPE(TypeBitfields)
unsigned : NumTypeBits;

/// An AttributedType::Kind
LLVM_PREFERRED_TYPE(attr::Kind)
unsigned AttrKind : 32 - NumTypeBits;
};

class AutoTypeBitfields {
friend class AutoType;

LLVM_PREFERRED_TYPE(TypeBitfields)
unsigned : NumTypeBits;

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

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

LLVM_PREFERRED_TYPE(TypeBitfields)
unsigned : NumTypeBits;
LLVM_PREFERRED_TYPE(bool)
unsigned IsUnqual : 1; // If true: typeof_unqual, else: typeof
};

class UsingBitfields {
friend class UsingType;

LLVM_PREFERRED_TYPE(TypeBitfields)
unsigned : NumTypeBits;

/// True if the underlying type is different from the declared one.
LLVM_PREFERRED_TYPE(bool)
unsigned hasTypeDifferentFromDecl : 1;
};

class TypedefBitfields {
friend class TypedefType;

LLVM_PREFERRED_TYPE(TypeBitfields)
unsigned : NumTypeBits;

/// True if the underlying type is different from the declared one.
LLVM_PREFERRED_TYPE(bool)
unsigned hasTypeDifferentFromDecl : 1;
};

class SubstTemplateTypeParmTypeBitfields {
friend class SubstTemplateTypeParmType;

LLVM_PREFERRED_TYPE(TypeBitfields)
unsigned : NumTypeBits;

LLVM_PREFERRED_TYPE(bool)
unsigned HasNonCanonicalUnderlyingType : 1;

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

LLVM_PREFERRED_TYPE(TypeBitfields)
unsigned : NumTypeBits;

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

LLVM_PREFERRED_TYPE(TypeBitfields)
unsigned : NumTypeBits;

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

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

LLVM_PREFERRED_TYPE(TypeWithKeywordBitfields)
unsigned : NumTypeWithKeywordBits;

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

LLVM_PREFERRED_TYPE(TypeBitfields)
unsigned : NumTypeBits;

/// The number of expansions that this pack expansion will
Expand Down
8 changes: 8 additions & 0 deletions llvm/include/llvm/Support/Compiler.h
Original file line number Diff line number Diff line change
Expand Up @@ -569,4 +569,12 @@ void AnnotateIgnoreWritesEnd(const char *file, int line);
#define LLVM_NO_PROFILE_INSTRUMENT_FUNCTION
#endif

/// \macro LLVM_PREFERRED_TYPE
/// Adjust type of bit-field in debug info.
#if __has_attribute(preferred_type)
#define LLVM_PREFERRED_TYPE(T) __attribute__((preferred_type(T)))
#else
#define LLVM_PREFERRED_TYPE(T)
#endif

#endif

0 comments on commit 8a3e4b5

Please sign in to comment.