Skip to content

[lldb][TypeSystemClang] Create EnumExtensibilityAttr from DW_AT_APPLE_enum_kind #126221

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

Merged
merged 1 commit into from
Feb 8, 2025

Conversation

Michael137
Copy link
Member

This patch consumes the DW_AT_APPLE_enum_kind attribute added in #124752 and turns it into a Clang attribute in the AST. This will currently be used by the Swift language plugin when it creates EnumDecls from debug-info and passes it to Swift compiler, which expects these attributes

@llvmbot
Copy link
Member

llvmbot commented Feb 7, 2025

@llvm/pr-subscribers-lldb

Author: Michael Buch (Michael137)

Changes

This patch consumes the DW_AT_APPLE_enum_kind attribute added in #124752 and turns it into a Clang attribute in the AST. This will currently be used by the Swift language plugin when it creates EnumDecls from debug-info and passes it to Swift compiler, which expects these attributes


Full diff: https://github.com/llvm/llvm-project/pull/126221.diff

5 Files Affected:

  • (modified) lldb/source/Plugins/SymbolFile/DWARF/DWARFASTParserClang.cpp (+7-2)
  • (modified) lldb/source/Plugins/SymbolFile/DWARF/DWARFASTParserClang.h (+4)
  • (modified) lldb/source/Plugins/TypeSystem/Clang/TypeSystemClang.cpp (+6-1)
  • (modified) lldb/source/Plugins/TypeSystem/Clang/TypeSystemClang.h (+7-6)
  • (added) lldb/test/Shell/Expr/TestEnumExtensibility.m (+33)
diff --git a/lldb/source/Plugins/SymbolFile/DWARF/DWARFASTParserClang.cpp b/lldb/source/Plugins/SymbolFile/DWARF/DWARFASTParserClang.cpp
index 39296ba5b437fe6..ec0004c70c6dac6 100644
--- a/lldb/source/Plugins/SymbolFile/DWARF/DWARFASTParserClang.cpp
+++ b/lldb/source/Plugins/SymbolFile/DWARF/DWARFASTParserClang.cpp
@@ -492,6 +492,10 @@ 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;
     }
   }
 }
@@ -1001,9 +1005,10 @@ TypeSP DWARFASTParserClang::ParseEnum(const SymbolContext &sc,
   }
 
   CompilerType clang_type = m_ast.CreateEnumerationType(
-      attrs.name.GetStringRef(), GetClangDeclContextContainingDIE(def_die, nullptr),
+      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,
diff --git a/lldb/source/Plugins/SymbolFile/DWARF/DWARFASTParserClang.h b/lldb/source/Plugins/SymbolFile/DWARF/DWARFASTParserClang.h
index 36fb381d3e291db..135dd06186c4bf4 100644
--- a/lldb/source/Plugins/SymbolFile/DWARF/DWARFASTParserClang.h
+++ b/lldb/source/Plugins/SymbolFile/DWARF/DWARFASTParserClang.h
@@ -568,6 +568,10 @@ struct ParsedDWARFTypeAttributes {
   ///< Indicates ref-qualifier of C++ member function if present.
   ///< Is RQ_None otherwise.
   clang::RefQualifierKind ref_qual = clang::RQ_None;
+
+  ///< Has a value if this DIE represents an enum that was declared
+  ///< with enum_extensibility.
+  std::optional<clang::EnumExtensibilityAttr::Kind> enum_kind;
 };
 
 #endif // LLDB_SOURCE_PLUGINS_SYMBOLFILE_DWARF_DWARFASTPARSERCLANG_H
diff --git a/lldb/source/Plugins/TypeSystem/Clang/TypeSystemClang.cpp b/lldb/source/Plugins/TypeSystem/Clang/TypeSystemClang.cpp
index 1da8fbe0bcd6dda..f91b608683420e2 100644
--- a/lldb/source/Plugins/TypeSystem/Clang/TypeSystemClang.cpp
+++ b/lldb/source/Plugins/TypeSystem/Clang/TypeSystemClang.cpp
@@ -2297,7 +2297,8 @@ CompilerType TypeSystemClang::GetOrCreateStructForIdentifier(
 CompilerType TypeSystemClang::CreateEnumerationType(
     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();
@@ -2315,6 +2316,10 @@ CompilerType TypeSystemClang::CreateEnumerationType(
   if (decl_ctx)
     decl_ctx->addDecl(enum_decl);
 
+  if (enum_kind)
+    enum_decl->addAttr(
+        clang::EnumExtensibilityAttr::CreateImplicit(ast, *enum_kind));
+
   // TODO: check if we should be setting the promotion type too?
   enum_decl->setIntegerType(ClangUtil::GetQualType(integer_clang_type));
 
diff --git a/lldb/source/Plugins/TypeSystem/Clang/TypeSystemClang.h b/lldb/source/Plugins/TypeSystem/Clang/TypeSystemClang.h
index e70ad4c2973a5cf..99d9becffd128c3 100644
--- a/lldb/source/Plugins/TypeSystem/Clang/TypeSystemClang.h
+++ b/lldb/source/Plugins/TypeSystem/Clang/TypeSystemClang.h
@@ -22,6 +22,7 @@
 
 #include "clang/AST/ASTContext.h"
 #include "clang/AST/ASTFwd.h"
+#include "clang/AST/Attr.h"
 #include "clang/AST/Decl.h"
 #include "clang/AST/TemplateBase.h"
 #include "clang/AST/Type.h"
@@ -498,12 +499,12 @@ class TypeSystemClang : public TypeSystem {
                                bool is_vector);
 
   // Enumeration Types
-  CompilerType CreateEnumerationType(llvm::StringRef name,
-                                     clang::DeclContext *decl_ctx,
-                                     OptionalClangModuleID owning_module,
-                                     const Declaration &decl,
-                                     const CompilerType &integer_qual_type,
-                                     bool is_scoped);
+  CompilerType CreateEnumerationType(
+      llvm::StringRef name, clang::DeclContext *decl_ctx,
+      OptionalClangModuleID owning_module, const Declaration &decl,
+      const CompilerType &integer_qual_type, bool is_scoped,
+      std::optional<clang::EnumExtensibilityAttr::Kind> enum_kind =
+          std::nullopt);
 
   // Integer type functions
 
diff --git a/lldb/test/Shell/Expr/TestEnumExtensibility.m b/lldb/test/Shell/Expr/TestEnumExtensibility.m
new file mode 100644
index 000000000000000..738b4fa2c778699
--- /dev/null
+++ b/lldb/test/Shell/Expr/TestEnumExtensibility.m
@@ -0,0 +1,33 @@
+// UNSUPPORTED: system-linux, system-windows
+
+// RUN: %clangxx_host %s -c -g -o %t
+// RUN: %lldb %t \
+// RUN:   -o "target var gClosed gOpen gNS gNSOpts" \
+// RUN:   -o "image dump ast" \
+// RUN:   2>&1 | FileCheck %s
+
+#import <Foundation/Foundation.h>
+
+enum __attribute__((enum_extensibility(closed))) Closed { C1 } gClosed;
+
+enum __attribute__((enum_extensibility(open))) Open { O1 } gOpen;
+
+typedef NS_ENUM(int, NS) { N1 } gNS;
+
+typedef NS_OPTIONS(int, NSO) { OPT1 } gNSOpts;
+
+// CHECK:      EnumDecl {{.*}} Closed
+// CHECK-NEXT: |-EnumExtensibilityAttr {{.*}} Closed
+// CHECK-NEXT: `-EnumConstantDecl {{.*}} C1 'Closed'
+
+// CHECK:      EnumDecl {{.*}} Open
+// CHECK-NEXT: |-EnumExtensibilityAttr {{.*}} Open
+// CHECK-NEXT: `-EnumConstantDecl {{.*}} O1 'Open'
+
+// CHECK:      EnumDecl {{.*}} NS
+// CHECK-NEXT: |-EnumExtensibilityAttr {{.*}} Open
+// CHECK-NEXT: `-EnumConstantDecl {{.*}} N1 'NS'
+
+// CHECK:      EnumDecl {{.*}} NSO
+// CHECK-NEXT: |-EnumExtensibilityAttr {{.*}} Open
+// CHECK-NEXT: `-EnumConstantDecl {{.*}} OPT1 'NSO'

Copy link
Collaborator

@adrian-prantl adrian-prantl left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nice!

@Michael137 Michael137 merged commit 0cdb467 into llvm:main Feb 8, 2025
9 checks passed
@Michael137 Michael137 deleted the lldb/enum-extensibility branch February 8, 2025 11:39
Icohedron pushed a commit to Icohedron/llvm-project that referenced this pull request Feb 11, 2025
…_enum_kind (llvm#126221)

This patch consumes the `DW_AT_APPLE_enum_kind` attribute added in
llvm#124752 and turns it into a
Clang attribute in the AST. This will currently be used by the Swift
language plugin when it creates `EnumDecl`s from debug-info and passes
it to Swift compiler, which expects these attributes
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants