[clang] fix emiision of nested unused enum types with -fno-eliminate-unused-d… (PR #137818)
via cfe-commits
cfe-commits at lists.llvm.org
Tue Apr 29 07:20:41 PDT 2025
https://github.com/ykhatav created https://github.com/llvm/llvm-project/pull/137818
…ebug-types
>From 0c5885d927031f5b8f1b650badb237154d3cb878 Mon Sep 17 00:00:00 2001
From: "Khatavkar, Yashasvi" <yashasvi.khatavkar at intel.com>
Date: Tue, 29 Apr 2025 07:19:31 -0700
Subject: [PATCH] fix emiision of nested unused enum types with
-fno-eliminate-unused-debug-types
---
clang/lib/CodeGen/CGDebugInfo.cpp | 10 +++++++++
clang/lib/CodeGen/CGDebugInfo.h | 2 ++
clang/test/CodeGen/unused_nested_enump.cpp | 26 ++++++++++++++++++++++
3 files changed, 38 insertions(+)
create mode 100644 clang/test/CodeGen/unused_nested_enump.cpp
diff --git a/clang/lib/CodeGen/CGDebugInfo.cpp b/clang/lib/CodeGen/CGDebugInfo.cpp
index f3ec498d4064b..7840b09776be4 100644
--- a/clang/lib/CodeGen/CGDebugInfo.cpp
+++ b/clang/lib/CodeGen/CGDebugInfo.cpp
@@ -1901,6 +1901,14 @@ CGDebugInfo::CreateRecordStaticField(const VarDecl *Var, llvm::DIType *RecordTy,
return GV;
}
+void CGDebugInfo::CollectRecordEnumType(
+ const EnumDecl *ED, SmallVectorImpl<llvm::Metadata *> &elements) {
+ QualType Ty = CGM.getContext().getTypeDeclType(ED);
+ SourceLocation Loc = ED->getLocation();
+ if (llvm::DIType *enumType = getOrCreateType(Ty, getOrCreateFile(Loc)))
+ elements.push_back(enumType);
+}
+
void CGDebugInfo::CollectRecordNormalField(
const FieldDecl *field, uint64_t OffsetInBits, llvm::DIFile *tunit,
SmallVectorImpl<llvm::Metadata *> &elements, llvm::DIType *RecordTy,
@@ -1987,6 +1995,8 @@ void CGDebugInfo::CollectRecordFields(
// Bump field number for next field.
++fieldNo;
+ } else if (const auto *enumType = dyn_cast<EnumDecl>(I)) {
+ CollectRecordEnumType(enumType, elements);
} else if (CGM.getCodeGenOpts().EmitCodeView) {
// Debug info for nested types is included in the member list only for
// CodeView.
diff --git a/clang/lib/CodeGen/CGDebugInfo.h b/clang/lib/CodeGen/CGDebugInfo.h
index b287ce7b92eee..baf2069745535 100644
--- a/clang/lib/CodeGen/CGDebugInfo.h
+++ b/clang/lib/CodeGen/CGDebugInfo.h
@@ -370,6 +370,8 @@ class CGDebugInfo {
llvm::DIDerivedType *CreateRecordStaticField(const VarDecl *Var,
llvm::DIType *RecordTy,
const RecordDecl *RD);
+ void CollectRecordEnumType(const EnumDecl *ED,
+ SmallVectorImpl<llvm::Metadata *> &elements);
void CollectRecordNormalField(const FieldDecl *Field, uint64_t OffsetInBits,
llvm::DIFile *F,
SmallVectorImpl<llvm::Metadata *> &E,
diff --git a/clang/test/CodeGen/unused_nested_enump.cpp b/clang/test/CodeGen/unused_nested_enump.cpp
new file mode 100644
index 0000000000000..1e7b39dbb7b1c
--- /dev/null
+++ b/clang/test/CodeGen/unused_nested_enump.cpp
@@ -0,0 +1,26 @@
+// RUN: %clang_cc1 -debug-info-kind=unused-types -emit-llvm -o - %s | FileCheck %s
+
+struct Type {
+ enum { Unused };
+ int value = 0;
+};
+int main() {
+ Type t;
+ return t.value;
+}
+
+// CHECK: !DICompositeType(tag: DW_TAG_enumeration_type
+// CHECK-SAME: scope: ![[STRUCT:[0-9]+]]
+// CHECK-SAME: elements: ![[ELEMENTS:[0-9]+]]
+
+// CHECK: ![[STRUCT]] = distinct !DICompositeType(tag: DW_TAG_structure_type
+// CHECK-SAME: elements: ![[STRUCT_ELEMENTS:[0-9]+]]
+
+// CHECK: ![[STRUCT_ELEMENTS]] = !{![[ENUM_MEMBER:[0-9]+]], ![[VALUE_MEMBER:[0-9]+]]}
+
+// CHECK: ![[VALUE_MEMBER]] = !DIDerivedType(tag: DW_TAG_member
+// CHECK-SAME: name: "value"
+// CHECK-SAME: scope: ![[STRUCT]]
+
+// CHECK: ![[ELEMENTS]] = !{![[ENUMERATOR:[0-9]+]]}
+// CHECK: ![[ENUMERATOR]] = !DIEnumerator(name: "Unused", value: 0
More information about the cfe-commits
mailing list