Skip to content

[DO-NOT-MERGE] Prototype DW_AT_APPLE_enum_kind in Clang/LLVM/LLDB #9910

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 6 additions & 1 deletion clang/lib/CodeGen/CGDebugInfo.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -3621,9 +3621,14 @@ llvm::DIType *CGDebugInfo::CreateTypeDefinition(const EnumType *Ty) {
unsigned Line = getLineNumber(ED->getLocation());
llvm::DIScope *EnumContext = getDeclContextDescriptor(ED);
llvm::DIType *ClassTy = getOrCreateType(ED->getIntegerType(), DefUnit);

std::optional<int32_t> EnumKind;
if (auto *Attr = ED->getAttr<EnumExtensibilityAttr>())
EnumKind = Attr->getExtensibility();

return DBuilder.createEnumerationType(
EnumContext, ED->getName(), DefUnit, Line, Size, Align, EltArray, ClassTy,
/*RunTimeLang=*/0, Identifier, ED->isScoped());
/*RunTimeLang=*/0, Identifier, ED->isScoped(), EnumKind);
}

llvm::DIMacro *CGDebugInfo::CreateMacro(llvm::DIMacroFile *Parent,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -450,6 +450,9 @@ ParsedDWARFTypeAttributes::ParsedDWARFTypeAttributes(const DWARFDIE &die) {
case DW_AT_reference:
ref_qual = clang::RQ_LValue;
break;
case DW_AT_APPLE_enum_kind:
enum_kind = static_cast<clang::EnumExtensibilityAttr::Kind>(form_value.Unsigned());
break;
}
}
}
Expand Down Expand Up @@ -1043,7 +1046,7 @@ TypeSP DWARFASTParserClang::ParseEnum(const SymbolContext &sc,
CompilerType clang_type = m_ast.CreateEnumerationType(
attrs.name.GetStringRef(), GetClangDeclContextContainingDIE(def_die, nullptr),
GetOwningClangModule(def_die), attrs.decl, enumerator_clang_type,
attrs.is_scoped_enum);
attrs.is_scoped_enum, attrs.enum_kind);
TypeSP type_sp =
dwarf->MakeType(def_die.GetID(), attrs.name, attrs.byte_size, nullptr,
attrs.type.Reference().GetID(), Type::eEncodingIsUID,
Expand Down
2 changes: 2 additions & 0 deletions lldb/source/Plugins/SymbolFile/DWARF/DWARFASTParserClang.h
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
#ifndef LLDB_SOURCE_PLUGINS_SYMBOLFILE_DWARF_DWARFASTPARSERCLANG_H
#define LLDB_SOURCE_PLUGINS_SYMBOLFILE_DWARF_DWARFASTPARSERCLANG_H

#include "clang/AST/Attr.h"
#include "clang/AST/CharUnits.h"
#include "clang/AST/Type.h"
#include "llvm/ADT/DenseMap.h"
Expand Down Expand Up @@ -538,6 +539,7 @@ struct ParsedDWARFTypeAttributes {
clang::RQ_None; ///< Indicates ref-qualifier of
///< C++ member function if present.
///< Is RQ_None otherwise.
std::optional<clang::EnumExtensibilityAttr::Kind> enum_kind;
};

#endif // LLDB_SOURCE_PLUGINS_SYMBOLFILE_DWARF_DWARFASTPARSERCLANG_H
5 changes: 4 additions & 1 deletion lldb/source/Plugins/TypeSystem/Clang/TypeSystemClang.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2409,7 +2409,7 @@ CompilerType TypeSystemClang::GetOrCreateStructForIdentifier(
clang::EnumDecl *TypeSystemClang::CreateEnumerationDecl(
llvm::StringRef name, clang::DeclContext *decl_ctx,
OptionalClangModuleID owning_module, const Declaration &decl,
const CompilerType &integer_clang_type, bool is_scoped) {
const CompilerType &integer_clang_type, bool is_scoped, std::optional<clang::EnumExtensibilityAttr::Kind> enum_kind) {
// TODO: Do something intelligent with the Declaration object passed in
// like maybe filling in the SourceLocation with it...
ASTContext &ast = getASTContext();
Expand All @@ -2432,6 +2432,9 @@ clang::EnumDecl *TypeSystemClang::CreateEnumerationDecl(

enum_decl->setAccess(AS_public); // TODO respect what's in the debug info

if (enum_kind)
enum_decl->addAttr(clang::EnumExtensibilityAttr::CreateImplicit(getASTContext(), *enum_kind));

return enum_decl;
}

Expand Down
7 changes: 4 additions & 3 deletions lldb/source/Plugins/TypeSystem/Clang/TypeSystemClang.h
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
#include <utility>
#include <vector>

#include "clang/AST/Attr.h"
#include "clang/AST/ASTContext.h"
#include "clang/AST/ASTFwd.h"
#include "clang/AST/Decl.h"
Expand Down Expand Up @@ -544,16 +545,16 @@ class TypeSystemClang : public TypeSystem {
OptionalClangModuleID owning_module,
const Declaration &decl,
const CompilerType &integer_qual_type,
bool is_scoped);
bool is_scoped, std::optional<clang::EnumExtensibilityAttr::Kind> = std::nullopt);

CompilerType CreateEnumerationType(llvm::StringRef name,
clang::DeclContext *decl_ctx,
OptionalClangModuleID owning_module,
const Declaration &decl,
const CompilerType &integer_qual_type,
bool is_scoped) {
bool is_scoped, std::optional<clang::EnumExtensibilityAttr::Kind> enum_kind = std::nullopt) {
clang::EnumDecl *enum_decl = CreateEnumerationDecl(
name, decl_ctx, owning_module, decl, integer_qual_type, is_scoped);
name, decl_ctx, owning_module, decl, integer_qual_type, is_scoped, enum_kind);
return GetType(getASTContext().getTagDeclType(enum_decl));
}

Expand Down
1 change: 1 addition & 0 deletions llvm/include/llvm/AsmParser/LLToken.h
Original file line number Diff line number Diff line change
Expand Up @@ -488,6 +488,7 @@ enum Kind {
DwarfMacinfo, // DW_MACINFO_foo
ChecksumKind, // CSK_foo
DbgRecordType, // dbg_foo
DwarfEnumKind, // DW_APPLE_ENUM_KIND_foo

// Type valued tokens (TyVal).
Type,
Expand Down
14 changes: 13 additions & 1 deletion llvm/include/llvm/BinaryFormat/Dwarf.def
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,8 @@
(defined HANDLE_DW_CFA && defined HANDLE_DW_CFA_PRED) || \
defined HANDLE_DW_APPLE_PROPERTY || defined HANDLE_DW_UT || \
defined HANDLE_DWARF_SECTION || defined HANDLE_DW_IDX || \
defined HANDLE_DW_END || defined HANDLE_DW_SECT)
defined HANDLE_DW_END || defined HANDLE_DW_SECT || \
defined HANDLE_DW_APPLE_ENUM_KIND)
#error "Missing macro definition of HANDLE_DW*"
#endif

Expand Down Expand Up @@ -146,6 +147,10 @@
#define HANDLE_DW_SECT(ID, NAME)
#endif

#ifndef HANDLE_DW_APPLE_ENUM_KIND
#define HANDLE_DW_APPLE_ENUM_KIND(ID, NAME)
#endif

HANDLE_DW_TAG(0x0000, null, 2, DWARF, DW_KIND_NONE)
HANDLE_DW_TAG(0x0001, array_type, 2, DWARF, DW_KIND_TYPE)
HANDLE_DW_TAG(0x0002, class_type, 2, DWARF, DW_KIND_TYPE)
Expand Down Expand Up @@ -636,6 +641,7 @@ HANDLE_DW_AT(0x3fed, APPLE_property, 0, APPLE)
HANDLE_DW_AT(0x3fee, APPLE_objc_direct, 0, APPLE)
HANDLE_DW_AT(0x3fef, APPLE_sdk, 0, APPLE)
HANDLE_DW_AT(0x3ff0, APPLE_origin, 0, APPLE)
HANDLE_DW_AT(0x3ff1, APPLE_enum_kind, 0, APPLE)

// Attribute form encodings.
HANDLE_DW_FORM(0x01, addr, 2, DWARF)
Expand Down Expand Up @@ -1268,6 +1274,11 @@ HANDLE_DW_APPLE_PROPERTY(0x1000, nullability)
HANDLE_DW_APPLE_PROPERTY(0x2000, null_resettable)
HANDLE_DW_APPLE_PROPERTY(0x4000, class)

// Enum kinds.
// Keep in sync with EnumExtensibilityAttr::Kind.
HANDLE_DW_APPLE_ENUM_KIND(0x00, Closed)
HANDLE_DW_APPLE_ENUM_KIND(0x01, Open)

// DWARF v5 Unit Types.
HANDLE_DW_UT(0x01, compile)
HANDLE_DW_UT(0x02, type)
Expand Down Expand Up @@ -1366,3 +1377,4 @@ HANDLE_DW_SECT(8, RNGLISTS)
#undef HANDLE_DW_IDX
#undef HANDLE_DW_END
#undef HANDLE_DW_SECT
#undef HANDLE_DW_APPLE_ENUM_KIND
15 changes: 12 additions & 3 deletions llvm/include/llvm/BinaryFormat/Dwarf.h
Original file line number Diff line number Diff line change
Expand Up @@ -44,9 +44,10 @@ namespace dwarf {
enum LLVMConstants : uint32_t {
/// LLVM mock tags (see also llvm/BinaryFormat/Dwarf.def).
/// \{
DW_TAG_invalid = ~0U, ///< Tag for invalid results.
DW_VIRTUALITY_invalid = ~0U, ///< Virtuality for invalid results.
DW_MACINFO_invalid = ~0U, ///< Macinfo type for invalid results.
DW_TAG_invalid = ~0U, ///< Tag for invalid results.
DW_VIRTUALITY_invalid = ~0U, ///< Virtuality for invalid results.
DW_MACINFO_invalid = ~0U, ///< Macinfo type for invalid results.
DW_APPLE_ENUM_KIND_invalid = ~0U, ///< Virtuality for invalid results.
/// \}

/// Special values for an initial length field.
Expand Down Expand Up @@ -198,6 +199,12 @@ enum VirtualityAttribute {
DW_VIRTUALITY_max = 0x02
};

enum EnumKindAttribute {
#define HANDLE_DW_APPLE_ENUM_KIND(ID, NAME) DW_APPLE_ENUM_KIND_##NAME = ID,
#include "llvm/BinaryFormat/Dwarf.def"
DW_APPLE_ENUM_KIND_max = 0x01
};

enum DefaultedMemberAttribute {
#define HANDLE_DW_DEFAULTED(ID, NAME) DW_DEFAULTED_##NAME = ID,
#include "llvm/BinaryFormat/Dwarf.def"
Expand Down Expand Up @@ -974,6 +981,7 @@ StringRef AccessibilityString(unsigned Access);
StringRef DefaultedMemberString(unsigned DefaultedEncodings);
StringRef VisibilityString(unsigned Visibility);
StringRef VirtualityString(unsigned Virtuality);
StringRef EnumKindString(unsigned Virtuality);
StringRef LanguageString(unsigned Language);
StringRef CaseString(unsigned Case);
StringRef ConventionString(unsigned Convention);
Expand Down Expand Up @@ -1013,6 +1021,7 @@ unsigned getOperationEncoding(StringRef OperationEncodingString);
unsigned getSubOperationEncoding(unsigned OpEncoding,
StringRef SubOperationEncodingString);
unsigned getVirtuality(StringRef VirtualityString);
unsigned getEnumKind(StringRef EnumKindString);
unsigned getLanguage(StringRef LanguageString);
unsigned getCallingConvention(StringRef LanguageString);
unsigned getAttributeEncoding(StringRef EncodingString);
Expand Down
18 changes: 10 additions & 8 deletions llvm/include/llvm/IR/DIBuilder.h
Original file line number Diff line number Diff line change
Expand Up @@ -631,7 +631,8 @@ namespace llvm {
DIScope *Scope, StringRef Name, DIFile *File, unsigned LineNumber,
uint64_t SizeInBits, uint32_t AlignInBits, DINodeArray Elements,
DIType *UnderlyingType, unsigned RunTimeLang = 0,
StringRef UniqueIdentifier = "", bool IsScoped = false);
StringRef UniqueIdentifier = "", bool IsScoped = false,
std::optional<uint32_t> EnumKind = std::nullopt);
/// Create debugging information entry for a set.
/// \param Scope Scope in which this set is defined.
/// \param Name Set name.
Expand Down Expand Up @@ -666,19 +667,20 @@ namespace llvm {
static DIType *createObjectPointerType(DIType *Ty);

/// Create a permanent forward-declared type.
DICompositeType *createForwardDecl(unsigned Tag, StringRef Name,
DIScope *Scope, DIFile *F, unsigned Line,
unsigned RuntimeLang = 0,
uint64_t SizeInBits = 0,
uint32_t AlignInBits = 0,
StringRef UniqueIdentifier = "");
DICompositeType *
createForwardDecl(unsigned Tag, StringRef Name, DIScope *Scope, DIFile *F,
unsigned Line, unsigned RuntimeLang = 0,
uint64_t SizeInBits = 0, uint32_t AlignInBits = 0,
StringRef UniqueIdentifier = "",
std::optional<uint32_t> EnumKind = std::nullopt);

/// Create a temporary forward-declared type.
DICompositeType *createReplaceableCompositeType(
unsigned Tag, StringRef Name, DIScope *Scope, DIFile *F, unsigned Line,
unsigned RuntimeLang = 0, uint64_t SizeInBits = 0,
uint32_t AlignInBits = 0, DINode::DIFlags Flags = DINode::FlagFwdDecl,
StringRef UniqueIdentifier = "", DINodeArray Annotations = nullptr);
StringRef UniqueIdentifier = "", DINodeArray Annotations = nullptr,
std::optional<uint32_t> EnumKind = std::nullopt);

/// Retain DIScope* in a module even if it is not referenced
/// through debug info anchors.
Expand Down
Loading