Index: lib/CodeGen/AsmPrinter/CodeViewDebug.cpp =================================================================== --- lib/CodeGen/AsmPrinter/CodeViewDebug.cpp +++ lib/CodeGen/AsmPrinter/CodeViewDebug.cpp @@ -850,8 +850,50 @@ ? TypeIndex(SimpleTypeKind::UInt64Quad) : TypeIndex(SimpleTypeKind::UInt32Long); uint64_t Size = Ty->getSizeInBits() / 8; - ArrayRecord Record(ElementTypeIndex, IndexType, Size, Ty->getName()); - return TypeTable.writeArray(Record); + assert(ElementTypeRef.resolve()); + uint64_t ElementSize = ElementTypeRef.resolve()->getSizeInBits() / 8; + + bool UndefinedSubrange = false; + + // FIXME: + // There is a bug in the front-end where an array of an incomplete structure + // declaration ends up not getting a size assigned to it. This needs to + // be fixed in the front-end, but in the meantime we don't want to trigger an + // assertion because of this. + if (Ty->getSizeInBits() == 0) { + UndefinedSubrange = true; + } + + // Add subranges to array type. + DINodeArray Elements = Ty->getElements(); + for (int i = Elements.size() - 1; i >= 0; --i) { + const DINode *Element = Elements[i]; + + assert(Element->getTag() == dwarf::DW_TAG_subrange_type); + + const DISubrange *Subrange = cast(Element); + int64_t LowerBound = Subrange->getLowerBound(); + int64_t DefaultLowerBound = 0; // FIXME : default bound + int64_t Count = Subrange->getCount(); + + assert(LowerBound == DefaultLowerBound); + + // FIXME: this is a WA solution until solving dynamic array boundary. + if (Count == -1) { + Count = 1; + UndefinedSubrange = true; + } + + StringRef Name = (i == 0) ? Ty->getName() : ""; + // Update the element size and element type index for subsequent subranges. + ElementSize *= Count; + ElementTypeIndex = TypeTable.writeArray( + ArrayRecord(ElementTypeIndex, IndexType, ElementSize, Name)); + } + + assert(UndefinedSubrange || ElementSize == Size); + + return ElementTypeIndex; } TypeIndex CodeViewDebug::lowerTypeBasic(const DIBasicType *Ty) { Index: test/DebugInfo/COFF/types-array-advanced.ll =================================================================== --- test/DebugInfo/COFF/types-array-advanced.ll +++ test/DebugInfo/COFF/types-array-advanced.ll @@ -0,0 +1,182 @@ +; RUN: llc < %s -filetype=obj | llvm-readobj - -codeview | FileCheck %s + +; C++ source to regenerate: +; $ cat t.cpp +; char multi_dim_arr[2][5][7]; +; +; struct incomplete_struct(*incomplete_struct_arry)[3]; +; +; void foo(int x) { +; int dyn_size_arr[x]; +; dyn_size_arr[0] = 0; +; } +; $ clang t.cpp -S -emit-llvm -g -gcodeview -o t.ll + +; CHECK: CodeViewTypes [ +; CHECK: Section: .debug$T (5) +; CHECK: Magic: 0x4 +; CHECK: ArgList (0x1000) { +; CHECK: TypeLeafKind: LF_ARGLIST (0x1201) +; CHECK: NumArgs: 1 +; CHECK: Arguments [ +; CHECK: ArgType: int (0x74) +; CHECK: ] +; CHECK: } +; CHECK: Procedure (0x1001) { +; CHECK: TypeLeafKind: LF_PROCEDURE (0x1008) +; CHECK: ReturnType: void (0x3) +; CHECK: CallingConvention: NearC (0x0) +; CHECK: FunctionOptions [ (0x0) +; CHECK: ] +; CHECK: NumParameters: 1 +; CHECK: ArgListType: (int) (0x1000) +; CHECK: } +; CHECK: FuncId (0x1002) { +; CHECK: TypeLeafKind: LF_FUNC_ID (0x1601) +; CHECK: ParentScope: 0x0 +; CHECK: FunctionType: void (int) (0x1001) +; CHECK: Name: foo +; CHECK: } +; CHECK: Array (0x1003) { +; CHECK: TypeLeafKind: LF_ARRAY (0x1503) +; CHECK: ElementType: int (0x74) +; CHECK: IndexType: unsigned long (0x22) +; CHECK: SizeOf: 4 +; CHECK: Name: +; CHECK: } +; CHECK: Array (0x1004) { +; CHECK: TypeLeafKind: LF_ARRAY (0x1503) +; CHECK: ElementType: char (0x70) +; CHECK: IndexType: unsigned long (0x22) +; CHECK: SizeOf: 7 +; CHECK: Name: +; CHECK: } +; CHECK: Array (0x1005) { +; CHECK: TypeLeafKind: LF_ARRAY (0x1503) +; CHECK: ElementType: 0x1004 +; CHECK: IndexType: unsigned long (0x22) +; CHECK: SizeOf: 35 +; CHECK: Name: +; CHECK: } +; CHECK: Array (0x1006) { +; CHECK: TypeLeafKind: LF_ARRAY (0x1503) +; CHECK: ElementType: 0x1005 +; CHECK: IndexType: unsigned long (0x22) +; CHECK: SizeOf: 70 +; CHECK: Name: +; CHECK: } +; CHECK: Struct (0x1007) { +; CHECK: TypeLeafKind: LF_STRUCTURE (0x1505) +; CHECK: MemberCount: 0 +; CHECK: Properties [ (0x280) +; CHECK: ForwardReference (0x80) +; CHECK: HasUniqueName (0x200) +; CHECK: ] +; CHECK: FieldList: 0x0 +; CHECK: DerivedFrom: 0x0 +; CHECK: VShape: 0x0 +; CHECK: SizeOf: 0 +; CHECK: Name: incomplete_struct +; CHECK: LinkageName: .?AUincomplete_struct@@ +; CHECK: } +; CHECK: Array (0x1008) { +; CHECK: TypeLeafKind: LF_ARRAY (0x1503) +; CHECK: ElementType: incomplete_struct (0x1007) +; CHECK: IndexType: unsigned long (0x22) +; CHECK: SizeOf: 0 +; CHECK: Name: +; CHECK: } +; CHECK: Pointer (0x1009) { +; CHECK: TypeLeafKind: LF_POINTER (0x1002) +; CHECK: PointeeType: 0x1008 +; CHECK: PointerAttributes: 0x800A +; CHECK: PtrType: Near32 (0xA) +; CHECK: PtrMode: Pointer (0x0) +; CHECK: IsFlat: 0 +; CHECK: IsConst: 0 +; CHECK: IsVolatile: 0 +; CHECK: IsUnaligned: 0 +; CHECK: SizeOf: 4 +; CHECK: } +; CHECK: ] + +target datalayout = "e-m:x-p:32:32-i64:64-f80:32-n8:16:32-a:0:32-S32" +target triple = "i686-pc-windows-msvc18.0.31101" + +@"\01?multi_dim_arr@@3PAY146DA" = global [2 x [5 x [7 x i8]]] zeroinitializer, align 1 +@"\01?incomplete_struct_arry@@3PAY02Uincomplete_struct@@A" = global [3 x i8]* null, align 4 + +; Function Attrs: nounwind +define void @"\01?foo@@YAXH@Z"(i32 %x) #0 !dbg !20 { +entry: + %x.addr = alloca i32, align 4 + %saved_stack = alloca i8*, align 4 + store i32 %x, i32* %x.addr, align 4 + call void @llvm.dbg.declare(metadata i32* %x.addr, metadata !24, metadata !25), !dbg !26 + %0 = load i32, i32* %x.addr, align 4, !dbg !27 + %1 = call i8* @llvm.stacksave(), !dbg !28 + store i8* %1, i8** %saved_stack, align 4, !dbg !28 + %vla = alloca i32, i32 %0, align 4, !dbg !28 + call void @llvm.dbg.declare(metadata i32* %vla, metadata !29, metadata !33), !dbg !34 + %arrayidx = getelementptr inbounds i32, i32* %vla, i32 0, !dbg !35 + store i32 0, i32* %arrayidx, align 4, !dbg !36 + %2 = load i8*, i8** %saved_stack, align 4, !dbg !37 + call void @llvm.stackrestore(i8* %2), !dbg !37 + ret void, !dbg !37 +} + +; Function Attrs: nounwind readnone +declare void @llvm.dbg.declare(metadata, metadata, metadata) #1 + +; Function Attrs: nounwind +declare i8* @llvm.stacksave() #2 + +; Function Attrs: nounwind +declare void @llvm.stackrestore(i8*) #2 + +attributes #0 = { nounwind } +attributes #1 = { nounwind readnone } +attributes #2 = { nounwind } + +!llvm.dbg.cu = !{!0} +!llvm.module.flags = !{!17, !18} +!llvm.ident = !{!19} + +!0 = distinct !DICompileUnit(language: DW_LANG_C_plus_plus, file: !1, producer: "clang version 3.9.0 (trunk 273084)", isOptimized: false, runtimeVersion: 0, emissionKind: FullDebug, enums: !2, globals: !3) +!1 = !DIFile(filename: "t.cpp", directory: "/") +!2 = !{} +!3 = !{!4, !11} +!4 = distinct !DIGlobalVariable(name: "multi_dim_arr", linkageName: "\01?multi_dim_arr@@3PAY146DA", scope: !0, file: !1, line: 1, type: !5, isLocal: false, isDefinition: true, variable: [2 x [5 x [7 x i8]]]* @"\01?multi_dim_arr@@3PAY146DA") +!5 = !DICompositeType(tag: DW_TAG_array_type, baseType: !6, size: 560, align: 8, elements: !7) +!6 = !DIBasicType(name: "char", size: 8, align: 8, encoding: DW_ATE_signed_char) +!7 = !{!8, !9, !10} +!8 = !DISubrange(count: 2) +!9 = !DISubrange(count: 5) +!10 = !DISubrange(count: 7) +!11 = distinct !DIGlobalVariable(name: "incomplete_struct_arry", linkageName: "\01?incomplete_struct_arry@@3PAY02Uincomplete_struct@@A", scope: !0, file: !1, line: 3, type: !12, isLocal: false, isDefinition: true, variable: [3 x i8]** @"\01?incomplete_struct_arry@@3PAY02Uincomplete_struct@@A") +!12 = !DIDerivedType(tag: DW_TAG_pointer_type, baseType: !13, size: 32, align: 32) +!13 = !DICompositeType(tag: DW_TAG_array_type, baseType: !14, elements: !15) +!14 = !DICompositeType(tag: DW_TAG_structure_type, name: "incomplete_struct", file: !1, line: 3, flags: DIFlagFwdDecl, identifier: ".?AUincomplete_struct@@") +!15 = !{!16} +!16 = !DISubrange(count: 3) +!17 = !{i32 2, !"CodeView", i32 1} +!18 = !{i32 2, !"Debug Info Version", i32 3} +!19 = !{!"clang version 3.9.0 (trunk 273084)"} +!20 = distinct !DISubprogram(name: "foo", linkageName: "\01?foo@@YAXH@Z", scope: !1, file: !1, line: 5, type: !21, isLocal: false, isDefinition: true, scopeLine: 5, flags: DIFlagPrototyped, isOptimized: false, unit: !0, variables: !2) +!21 = !DISubroutineType(types: !22) +!22 = !{null, !23} +!23 = !DIBasicType(name: "int", size: 32, align: 32, encoding: DW_ATE_signed) +!24 = !DILocalVariable(name: "x", arg: 1, scope: !20, file: !1, line: 5, type: !23) +!25 = !DIExpression() +!26 = !DILocation(line: 5, column: 14, scope: !20) +!27 = !DILocation(line: 6, column: 20, scope: !20) +!28 = !DILocation(line: 6, column: 3, scope: !20) +!29 = !DILocalVariable(name: "dyn_size_arr", scope: !20, file: !1, line: 6, type: !30) +!30 = !DICompositeType(tag: DW_TAG_array_type, baseType: !23, align: 32, elements: !31) +!31 = !{!32} +!32 = !DISubrange(count: -1) +!33 = !DIExpression(DW_OP_deref) +!34 = !DILocation(line: 6, column: 7, scope: !20) +!35 = !DILocation(line: 7, column: 3, scope: !20) +!36 = !DILocation(line: 7, column: 19, scope: !20) +!37 = !DILocation(line: 8, column: 1, scope: !20)