From 1b9cd382658abcc6ef873f9704bdaef45b1d201c Mon Sep 17 00:00:00 2001 From: Brock Wyma Date: Tue, 13 Mar 2018 14:14:16 +0000 Subject: [PATCH] [CodeView] Omit forward references for unnamed structs and unions Codeview references to unnamed structs and unions are expected to refer to the complete type definition instead of a forward reference so Visual Studio can resolve the type properly. Differential Revision: https://reviews.llvm.org/D32498 llvm-svn: 327397 --- lib/CodeGen/AsmPrinter/CodeViewDebug.cpp | 50 ++++- test/DebugInfo/COFF/bitfields.ll | 47 ++-- .../DebugInfo/COFF/types-recursive-unnamed.ll | 78 +++++++ test/DebugInfo/COFF/unnamed.ll | 200 ++++++++++++++++++ 4 files changed, 337 insertions(+), 38 deletions(-) create mode 100644 test/DebugInfo/COFF/types-recursive-unnamed.ll create mode 100644 test/DebugInfo/COFF/unnamed.ll diff --git a/lib/CodeGen/AsmPrinter/CodeViewDebug.cpp b/lib/CodeGen/AsmPrinter/CodeViewDebug.cpp index 0e8a83cbe18..e397e30b098 100644 --- a/lib/CodeGen/AsmPrinter/CodeViewDebug.cpp +++ b/lib/CodeGen/AsmPrinter/CodeViewDebug.cpp @@ -1936,7 +1936,28 @@ ClassInfo CodeViewDebug::collectClassInfo(const DICompositeType *Ty) { return Info; } +static bool shouldAlwaysEmitCompleteClassType(const DICompositeType *Ty) { + // This routine is used by lowerTypeClass and lowerTypeUnion to determine + // if a complete type should be emitted instead of a forward reference. + return Ty->getName().empty() && Ty->getIdentifier().empty() && + !Ty->isForwardDecl(); +} + TypeIndex CodeViewDebug::lowerTypeClass(const DICompositeType *Ty) { + // Emit the complete type for unnamed structs. C++ classes with methods + // which have a circular reference back to the class type are expected to + // be named by the front-end and should not be "unnamed". C unnamed + // structs should not have circular references. + if (shouldAlwaysEmitCompleteClassType(Ty)) { + // If this unnamed complete type is already in the process of being defined + // then the description of the type is malformed and cannot be emitted + // into CodeView correctly so report a fatal error. + auto I = CompleteTypeIndices.find(Ty); + if (I != CompleteTypeIndices.end() && I->second == TypeIndex()) + report_fatal_error("cannot debug circular reference to unnamed type"); + return getCompleteTypeIndex(Ty); + } + // First, construct the forward decl. Don't look into Ty to compute the // forward decl options, since it might not be available in all TUs. TypeRecordKind Kind = getRecordKind(Ty); @@ -1981,6 +2002,10 @@ TypeIndex CodeViewDebug::lowerCompleteTypeClass(const DICompositeType *Ty) { } TypeIndex CodeViewDebug::lowerTypeUnion(const DICompositeType *Ty) { + // Emit the complete type for unnamed unions. + if (shouldAlwaysEmitCompleteClassType(Ty)) + return getCompleteTypeIndex(Ty); + ClassOptions CO = ClassOptions::ForwardReference | getCommonClassOptions(Ty); std::string FullName = getFullyQualifiedName(Ty); @@ -2211,9 +2236,7 @@ TypeIndex CodeViewDebug::getCompleteTypeIndex(DITypeRef TypeRef) { return getTypeIndex(Ty); } - // Check if we've already translated the complete record type. Lowering a - // complete type should never trigger lowering another complete type, so we - // can reuse the hash table lookup result. + // Check if we've already translated the complete record type. const auto *CTy = cast(Ty); auto InsertResult = CompleteTypeIndices.insert({CTy, TypeIndex()}); if (!InsertResult.second) @@ -2224,13 +2247,16 @@ TypeIndex CodeViewDebug::getCompleteTypeIndex(DITypeRef TypeRef) { // Make sure the forward declaration is emitted first. It's unclear if this // is necessary, but MSVC does it, and we should follow suit until we can show // otherwise. - TypeIndex FwdDeclTI = getTypeIndex(CTy); + // We only emit a forward declaration for named types. + if (!CTy->getName().empty() || !CTy->getIdentifier().empty()) { + TypeIndex FwdDeclTI = getTypeIndex(CTy); - // Just use the forward decl if we don't have complete type info. This might - // happen if the frontend is using modules and expects the complete definition - // to be emitted elsewhere. - if (CTy->isForwardDecl()) - return FwdDeclTI; + // Just use the forward decl if we don't have complete type info. This + // might happen if the frontend is using modules and expects the complete + // definition to be emitted elsewhere. + if (CTy->isForwardDecl()) + return FwdDeclTI; + } TypeIndex TI; switch (CTy->getTag()) { @@ -2245,7 +2271,11 @@ TypeIndex CodeViewDebug::getCompleteTypeIndex(DITypeRef TypeRef) { llvm_unreachable("not a record"); } - InsertResult.first->second = TI; + // Update the type index associated with this CompositeType. This cannot + // use the 'InsertResult' iterator above because it is potentially + // invalidated by map insertions which can occur while lowering the class + // type above. + CompleteTypeIndices[CTy] = TI; return TI; } diff --git a/test/DebugInfo/COFF/bitfields.ll b/test/DebugInfo/COFF/bitfields.ll index ce2d5b35562..cb3b14ee9eb 100644 --- a/test/DebugInfo/COFF/bitfields.ll +++ b/test/DebugInfo/COFF/bitfields.ll @@ -63,15 +63,29 @@ ; CHECK: BitSize: 2 ; CHECK: BitOffset: 23 ; CHECK: } +; CHECK: FieldList ([[anon_fl:.*]]) { +; CHECK: TypeLeafKind: LF_FIELDLIST (0x1203) +; CHECK: DataMember { +; CHECK: TypeLeafKind: LF_MEMBER (0x150D) +; CHECK: Type: char (0x70) +; CHECK: FieldOffset: 0x0 +; CHECK: Name: c +; CHECK: } +; CHECK: DataMember { +; CHECK: TypeLeafKind: LF_MEMBER (0x150D) +; CHECK: Type: short (0x11) +; CHECK: FieldOffset: 0x1 +; CHECK: Name: s +; CHECK: } +; CHECK: } ; CHECK: Struct ([[anon_ty:.*]]) { ; CHECK: TypeLeafKind: LF_STRUCTURE (0x1505) -; CHECK: MemberCount: 0 -; CHECK: Properties [ (0x88) -; CHECK: ForwardReference (0x80) +; CHECK: MemberCount: 2 +; CHECK: Properties [ (0x8) ; CHECK: Nested (0x8) ; CHECK: ] -; CHECK: FieldList: 0x0 -; CHECK: SizeOf: 0 +; CHECK: FieldList: ([[anon_fl]]) +; CHECK: SizeOf: 3 ; CHECK: Name: S1:: ; CHECK: } ; CHECK: BitField ([[S1_u:.*]]) { @@ -126,29 +140,6 @@ ; CHECK: SizeOf: 16 ; CHECK: Name: S1 ; CHECK: } -; CHECK: FieldList ([[anon_fl:.*]]) { -; CHECK: TypeLeafKind: LF_FIELDLIST (0x1203) -; CHECK: DataMember { -; CHECK: Type: char (0x70) -; CHECK: FieldOffset: 0x0 -; CHECK: Name: c -; CHECK: } -; CHECK: DataMember { -; CHECK: Type: short (0x11) -; CHECK: FieldOffset: 0x1 -; CHECK: Name: s -; CHECK: } -; CHECK: } -; CHECK: Struct ({{.*}}) { -; CHECK: TypeLeafKind: LF_STRUCTURE (0x1505) -; CHECK: MemberCount: 2 -; CHECK: Properties [ (0x8) -; CHECK: Nested (0x8) -; CHECK: ] -; CHECK: FieldList: ([[anon_fl]]) -; CHECK: SizeOf: 3 -; CHECK: Name: S1:: -; CHECK: } ; CHECK: BitField ([[S2_y:.*]]) { ; CHECK: TypeLeafKind: LF_BITFIELD (0x1205) ; CHECK: Type: int (0x74) diff --git a/test/DebugInfo/COFF/types-recursive-unnamed.ll b/test/DebugInfo/COFF/types-recursive-unnamed.ll new file mode 100644 index 00000000000..33b36cb71be --- /dev/null +++ b/test/DebugInfo/COFF/types-recursive-unnamed.ll @@ -0,0 +1,78 @@ +; RUN: not llc < %s -filetype=obj 2>&1 | FileCheck %s +; +; Verify the compiler produces an error message when trying to emit circular +; references to unnamed structs which are not supported in CodeView debug +; information. +; +; -- types-recursive-unnamed.cpp begin ----------------------------------------- +; struct named_struct { +; struct { +; void method() {} +; } unnamed_struct_with_method; +; void anchor(); +; }; +; void named_struct::anchor() {} +; -- types-recursive-unnamed.cpp end ------------------------------------------- +; +; To rebuild the reproducer: +; 1. First, compile the source code: +; $ clang -S -emit-llvm -g -gcodeview unnamed.cpp +; 2. Remove all "name" and "identifier" attributes with a value matching +; the form: "@named_struct@@QEAAXXZ", scope: !10, file: !1, line: 3, type: !13, isLocal: false, isDefinition: false, scopeLine: 3, flags: DIFlagPrototyped, isOptimized: false) +!13 = !DISubroutineType(types: !14) +!14 = !{null, !15} +!15 = !DIDerivedType(tag: DW_TAG_pointer_type, baseType: !10, size: 64, flags: DIFlagArtificial | DIFlagObjectPointer) +!16 = !DIDerivedType(tag: DW_TAG_member, name: "unnamed_struct_with_method", scope: !8, file: !1, line: 4, baseType: !10, size: 8) +!17 = !DISubprogram(name: "anchor", linkageName: "\01?anchor@named_struct@@QEAAXXZ", scope: !8, file: !1, line: 5, type: !18, isLocal: false, isDefinition: false, scopeLine: 5, flags: DIFlagPrototyped, isOptimized: false) +!18 = !DISubroutineType(types: !19) +!19 = !{null, !20} +!20 = !DIDerivedType(tag: DW_TAG_pointer_type, baseType: !8, size: 64, flags: DIFlagArtificial | DIFlagObjectPointer) +!21 = !DILocalVariable(name: "this", arg: 1, scope: !7, type: !22, flags: DIFlagArtificial | DIFlagObjectPointer) +!22 = !DIDerivedType(tag: DW_TAG_pointer_type, baseType: !8, size: 64) +!23 = !DIExpression() +!24 = !DILocation(line: 0, scope: !7) +!25 = !DILocation(line: 7, column: 30, scope: !7) diff --git a/test/DebugInfo/COFF/unnamed.ll b/test/DebugInfo/COFF/unnamed.ll new file mode 100644 index 00000000000..127d8ab204a --- /dev/null +++ b/test/DebugInfo/COFF/unnamed.ll @@ -0,0 +1,200 @@ +; RUN: llc < %s -filetype=obj | llvm-readobj - -codeview | FileCheck %s +; +; Verify CodeView emission does not emit forward references for unnamed +; structs/unions. If a forward reference is emitted for an unnamed composite +; type then Visual Studio will not be able to display the value. +; +; Previous values displayed by Visual Studio: +; local.unnamed_struct {...} +; local.unnamed_union {...} +; +; New values displayed by Visual Studio: +; local.unnamed_struct {m3=66 'B' } +; local.unnamed_union {m1=65 m2=65 'A' } +; +; The reproducer: +; $ cat unnamed.c +; struct named_struct { +; int id; +; union { +; int m1; +; char m2; +; } unnamed_union; +; struct { +; char m3; +; } unnamed_struct; +; }; +; +; int main() +; { +; struct named_struct local; +; +; local.id = 1; +; local.unnamed_union.m1 = 65; +; local.unnamed_struct.m3 = 'B'; +; +; return 0; +; } +; +; To regenerate the IR below: +; $ clang unnamed.c -S -emit-llvm -g -gcodeview +; +; CHECK: FieldList ([[UnnamedUnionFieldList:.*]]) { +; CHECK-NEXT: TypeLeafKind: LF_FIELDLIST (0x1203) +; CHECK-NEXT: DataMember { +; CHECK-NEXT: TypeLeafKind: LF_MEMBER (0x150D) +; CHECK-NEXT: AccessSpecifier: Public (0x3) +; CHECK-NEXT: Type: int (0x74) +; CHECK-NEXT: FieldOffset: 0x0 +; CHECK-NEXT: Name: m1 +; CHECK-NEXT: } +; CHECK-NEXT: DataMember { +; CHECK-NEXT: TypeLeafKind: LF_MEMBER (0x150D) +; CHECK-NEXT: AccessSpecifier: Public (0x3) +; CHECK-NEXT: Type: char (0x70) +; CHECK-NEXT: FieldOffset: 0x0 +; CHECK-NEXT: Name: m2 +; CHECK-NEXT: } +; CHECK-NEXT: } +; CHECK: Union ([[UnnamedUnion:.*]]) { +; CHECK-NEXT: TypeLeafKind: LF_UNION (0x1506) +; CHECK-NEXT: MemberCount: 2 +; CHECK-NEXT: Properties [ (0x408) +; CHECK-NEXT: Nested (0x8) +; CHECK-NEXT: Sealed (0x400) +; CHECK-NEXT: ] +; CHECK-NEXT: FieldList: ([[UnnamedUnionFieldList]]) +; CHECK-NEXT: SizeOf: 4 +; CHECK-NEXT: Name: named_struct:: +; CHECK-NEXT: } +; CHECK: FieldList ([[UnnamedStructFieldList:.*]]) { +; CHECK-NEXT: TypeLeafKind: LF_FIELDLIST (0x1203) +; CHECK-NEXT: DataMember { +; CHECK-NEXT: TypeLeafKind: LF_MEMBER (0x150D) +; CHECK-NEXT: AccessSpecifier: Public (0x3) +; CHECK-NEXT: Type: char (0x70) +; CHECK-NEXT: FieldOffset: 0x0 +; CHECK-NEXT: Name: m3 +; CHECK-NEXT: } +; CHECK-NEXT: } +; CHECK: Struct ([[UnnamedStruct:.*]]) { +; CHECK-NEXT: TypeLeafKind: LF_STRUCTURE (0x1505) +; CHECK-NEXT: MemberCount: 1 +; CHECK-NEXT: Properties [ (0x8) +; CHECK-NEXT: Nested (0x8) +; CHECK-NEXT: ] +; CHECK-NEXT: FieldList: ([[UnnamedStructFieldList]]) +; CHECK-NEXT: DerivedFrom: 0x0 +; CHECK-NEXT: VShape: 0x0 +; CHECK-NEXT: SizeOf: 1 +; CHECK-NEXT: Name: named_struct:: +; CHECK-NEXT: } +; CHECK: FieldList ([[NamedStructFieldList:.*]]) { +; CHECK-NEXT: TypeLeafKind: LF_FIELDLIST (0x1203) +; CHECK-NEXT: DataMember { +; CHECK-NEXT: TypeLeafKind: LF_MEMBER (0x150D) +; CHECK-NEXT: AccessSpecifier: Public (0x3) +; CHECK-NEXT: Type: int (0x74) +; CHECK-NEXT: FieldOffset: 0x0 +; CHECK-NEXT: Name: id +; CHECK-NEXT: } +; CHECK-NEXT: DataMember { +; CHECK-NEXT: TypeLeafKind: LF_MEMBER (0x150D) +; CHECK-NEXT: AccessSpecifier: Public (0x3) +; CHECK-NEXT: Type: named_struct:: ([[UnnamedUnion]]) +; CHECK-NEXT: FieldOffset: 0x4 +; CHECK-NEXT: Name: unnamed_union +; CHECK-NEXT: } +; CHECK-NEXT: DataMember { +; CHECK-NEXT: TypeLeafKind: LF_MEMBER (0x150D) +; CHECK-NEXT: AccessSpecifier: Public (0x3) +; CHECK-NEXT: Type: named_struct:: ([[UnnamedStruct]]) +; CHECK-NEXT: FieldOffset: 0x8 +; CHECK-NEXT: Name: unnamed_struct +; CHECK-NEXT: } +; CHECK-NEXT: } +; CHECK: Struct ({{.*}}) { +; CHECK-NEXT: TypeLeafKind: LF_STRUCTURE (0x1505) +; CHECK-NEXT: MemberCount: 3 +; CHECK-NEXT: Properties [ (0x0) +; CHECK-NEXT: ] +; CHECK-NEXT: FieldList: ([[NamedStructFieldList]]) +; CHECK-NEXT: DerivedFrom: 0x0 +; CHECK-NEXT: VShape: 0x0 +; CHECK-NEXT: SizeOf: 12 +; CHECK-NEXT: Name: named_struct +; CHECK-NEXT: } + +; ModuleID = 'unnamed.c' +source_filename = "unnamed.c" +target datalayout = "e-m:w-i64:64-f80:128-n8:16:32:64-S128" +target triple = "x86_64-pc-windows-msvc19.0.24210" + +%struct.named_struct = type { i32, %union.anon, %struct.anon } +%union.anon = type { i32 } +%struct.anon = type { i8 } + +; Function Attrs: noinline nounwind uwtable +define i32 @main() #0 !dbg !7 { +entry: + %retval = alloca i32, align 4 + %local = alloca %struct.named_struct, align 4 + store i32 0, i32* %retval, align 4 + call void @llvm.dbg.declare(metadata %struct.named_struct* %local, metadata !11, metadata !25), !dbg !26 + %id = getelementptr inbounds %struct.named_struct, %struct.named_struct* %local, i32 0, i32 0, !dbg !27 + store i32 1, i32* %id, align 4, !dbg !28 + %unnamed_union = getelementptr inbounds %struct.named_struct, %struct.named_struct* %local, i32 0, i32 1, !dbg !29 + %m1 = bitcast %union.anon* %unnamed_union to i32*, !dbg !30 + store i32 65, i32* %m1, align 4, !dbg !31 + %unnamed_struct = getelementptr inbounds %struct.named_struct, %struct.named_struct* %local, i32 0, i32 2, !dbg !32 + %m3 = getelementptr inbounds %struct.anon, %struct.anon* %unnamed_struct, i32 0, i32 0, !dbg !33 + store i8 66, i8* %m3, align 4, !dbg !34 + ret i32 0, !dbg !35 +} + +; Function Attrs: nounwind readnone +declare void @llvm.dbg.declare(metadata, metadata, metadata) #1 + +attributes #0 = { noinline nounwind uwtable "correctly-rounded-divide-sqrt-fp-math"="false" "disable-tail-calls"="false" "less-precise-fpmad"="false" "no-frame-pointer-elim"="false" "no-infs-fp-math"="false" "no-jump-tables"="false" "no-nans-fp-math"="false" "no-signed-zeros-fp-math"="false" "no-trapping-math"="false" "stack-protector-buffer-size"="8" "target-cpu"="x86-64" "target-features"="+fxsr,+mmx,+sse,+sse2,+x87" "unsafe-fp-math"="false" "use-soft-float"="false" } +attributes #1 = { nounwind readnone } + +!llvm.dbg.cu = !{!0} +!llvm.module.flags = !{!3, !4, !5} +!llvm.ident = !{!6} + +!0 = distinct !DICompileUnit(language: DW_LANG_C99, file: !1, producer: "clang version 5.0.0 (trunk)", isOptimized: false, runtimeVersion: 0, emissionKind: FullDebug, enums: !2) +!1 = !DIFile(filename: "unnamed.c", directory: "C:\5Cpath\5Cto\5Cdirectory", checksumkind: CSK_MD5, checksum: "a1874da39665a126d6949d929fbd4818") +!2 = !{} +!3 = !{i32 2, !"CodeView", i32 1} +!4 = !{i32 2, !"Debug Info Version", i32 3} +!5 = !{i32 1, !"PIC Level", i32 2} +!6 = !{!"clang version 5.0.0 (trunk)"} +!7 = distinct !DISubprogram(name: "main", scope: !1, file: !1, line: 12, type: !8, isLocal: false, isDefinition: true, scopeLine: 13, isOptimized: false, unit: !0, variables: !2) +!8 = !DISubroutineType(types: !9) +!9 = !{!10} +!10 = !DIBasicType(name: "int", size: 32, encoding: DW_ATE_signed) +!11 = !DILocalVariable(name: "local", scope: !7, file: !1, line: 14, type: !12) +!12 = distinct !DICompositeType(tag: DW_TAG_structure_type, name: "named_struct", file: !1, line: 1, size: 96, elements: !13) +!13 = !{!14, !15, !21} +!14 = !DIDerivedType(tag: DW_TAG_member, name: "id", scope: !12, file: !1, line: 2, baseType: !10, size: 32) +!15 = !DIDerivedType(tag: DW_TAG_member, name: "unnamed_union", scope: !12, file: !1, line: 6, baseType: !16, size: 32, offset: 32) +!16 = distinct !DICompositeType(tag: DW_TAG_union_type, scope: !12, file: !1, line: 3, size: 32, elements: !17) +!17 = !{!18, !19} +!18 = !DIDerivedType(tag: DW_TAG_member, name: "m1", scope: !16, file: !1, line: 4, baseType: !10, size: 32) +!19 = !DIDerivedType(tag: DW_TAG_member, name: "m2", scope: !16, file: !1, line: 5, baseType: !20, size: 8) +!20 = !DIBasicType(name: "char", size: 8, encoding: DW_ATE_signed_char) +!21 = !DIDerivedType(tag: DW_TAG_member, name: "unnamed_struct", scope: !12, file: !1, line: 9, baseType: !22, size: 8, offset: 64) +!22 = distinct !DICompositeType(tag: DW_TAG_structure_type, scope: !12, file: !1, line: 7, size: 8, elements: !23) +!23 = !{!24} +!24 = !DIDerivedType(tag: DW_TAG_member, name: "m3", scope: !22, file: !1, line: 8, baseType: !20, size: 8) +!25 = !DIExpression() +!26 = !DILocation(line: 14, column: 23, scope: !7) +!27 = !DILocation(line: 16, column: 9, scope: !7) +!28 = !DILocation(line: 16, column: 12, scope: !7) +!29 = !DILocation(line: 17, column: 9, scope: !7) +!30 = !DILocation(line: 17, column: 23, scope: !7) +!31 = !DILocation(line: 17, column: 26, scope: !7) +!32 = !DILocation(line: 18, column: 9, scope: !7) +!33 = !DILocation(line: 18, column: 24, scope: !7) +!34 = !DILocation(line: 18, column: 27, scope: !7) +!35 = !DILocation(line: 20, column: 3, scope: !7)