Sanjiv Gupta | e8b9f5b | 2008-05-08 08:54:20 +0000 | [diff] [blame] | 1 | //===--- CGDebugInfo.cpp - Emit Debug Information for a Module ------------===// |
| 2 | // |
| 3 | // The LLVM Compiler Infrastructure |
| 4 | // |
| 5 | // This file is distributed under the University of Illinois Open Source |
| 6 | // License. See LICENSE.TXT for details. |
| 7 | // |
| 8 | //===----------------------------------------------------------------------===// |
| 9 | // |
| 10 | // This coordinates the debug information generation while generating code. |
| 11 | // |
| 12 | //===----------------------------------------------------------------------===// |
| 13 | |
| 14 | #include "CGDebugInfo.h" |
| 15 | #include "CodeGenModule.h" |
Sanjiv Gupta | 1c6a38b | 2008-05-25 05:15:42 +0000 | [diff] [blame] | 16 | #include "clang/AST/ASTContext.h" |
Daniel Dunbar | e91593e | 2008-08-11 04:54:23 +0000 | [diff] [blame] | 17 | #include "clang/AST/Decl.h" |
Chris Lattner | 3cc5c40 | 2008-11-11 07:01:36 +0000 | [diff] [blame] | 18 | #include "clang/AST/Expr.h" |
Daniel Dunbar | e91593e | 2008-08-11 04:54:23 +0000 | [diff] [blame] | 19 | #include "clang/AST/RecordLayout.h" |
Sanjiv Gupta | 1c6a38b | 2008-05-25 05:15:42 +0000 | [diff] [blame] | 20 | #include "clang/Basic/SourceManager.h" |
| 21 | #include "clang/Basic/FileManager.h" |
Sanjiv Gupta | e8b9f5b | 2008-05-08 08:54:20 +0000 | [diff] [blame] | 22 | #include "llvm/Constants.h" |
| 23 | #include "llvm/DerivedTypes.h" |
| 24 | #include "llvm/Instructions.h" |
| 25 | #include "llvm/Intrinsics.h" |
| 26 | #include "llvm/Module.h" |
Sanjiv Gupta | e8b9f5b | 2008-05-08 08:54:20 +0000 | [diff] [blame] | 27 | #include "llvm/ADT/StringExtras.h" |
| 28 | #include "llvm/ADT/SmallVector.h" |
Sanjiv Gupta | 1c6a38b | 2008-05-25 05:15:42 +0000 | [diff] [blame] | 29 | #include "llvm/Support/Dwarf.h" |
Sanjiv Gupta | 1c6a38b | 2008-05-25 05:15:42 +0000 | [diff] [blame] | 30 | #include "llvm/Target/TargetMachine.h" |
Sanjiv Gupta | e8b9f5b | 2008-05-08 08:54:20 +0000 | [diff] [blame] | 31 | using namespace clang; |
| 32 | using namespace clang::CodeGen; |
| 33 | |
| 34 | CGDebugInfo::CGDebugInfo(CodeGenModule *m) |
Chris Lattner | 9c85ba3 | 2008-11-10 06:08:34 +0000 | [diff] [blame] | 35 | : M(m), DebugFactory(M->getModule()) { |
Sanjiv Gupta | e8b9f5b | 2008-05-08 08:54:20 +0000 | [diff] [blame] | 36 | } |
| 37 | |
Chris Lattner | 9c85ba3 | 2008-11-10 06:08:34 +0000 | [diff] [blame] | 38 | CGDebugInfo::~CGDebugInfo() { |
Daniel Dunbar | 66031a5 | 2008-10-17 16:15:48 +0000 | [diff] [blame] | 39 | assert(RegionStack.empty() && "Region stack mismatch, stack not empty!"); |
Sanjiv Gupta | e8b9f5b | 2008-05-08 08:54:20 +0000 | [diff] [blame] | 40 | } |
| 41 | |
Chris Lattner | 9c85ba3 | 2008-11-10 06:08:34 +0000 | [diff] [blame] | 42 | void CGDebugInfo::setLocation(SourceLocation Loc) { |
| 43 | if (Loc.isValid()) |
Chris Lattner | f7cf85b | 2009-01-16 07:36:28 +0000 | [diff] [blame] | 44 | CurLoc = M->getContext().getSourceManager().getInstantiationLoc(Loc); |
Sanjiv Gupta | 1c6a38b | 2008-05-25 05:15:42 +0000 | [diff] [blame] | 45 | } |
| 46 | |
Sanjiv Gupta | e8b9f5b | 2008-05-08 08:54:20 +0000 | [diff] [blame] | 47 | /// getOrCreateCompileUnit - Get the compile unit from the cache or create a new |
Daniel Dunbar | 25f51dd | 2008-10-24 08:38:36 +0000 | [diff] [blame] | 48 | /// one if necessary. This returns null for invalid source locations. |
Chris Lattner | 9c85ba3 | 2008-11-10 06:08:34 +0000 | [diff] [blame] | 49 | llvm::DICompileUnit CGDebugInfo::getOrCreateCompileUnit(SourceLocation Loc) { |
Daniel Dunbar | 831570c | 2009-01-22 00:09:25 +0000 | [diff] [blame] | 50 | // FIXME: Until we do a complete job of emitting debug information, |
| 51 | // we need to support making dummy compile units so that we generate |
| 52 | // "well formed" debug info. |
| 53 | const FileEntry *FE = 0; |
Daniel Dunbar | 25f51dd | 2008-10-24 08:38:36 +0000 | [diff] [blame] | 54 | |
Devang Patel | 7782022 | 2009-02-24 23:16:03 +0000 | [diff] [blame] | 55 | SourceManager &SM = M->getContext().getSourceManager(); |
Daniel Dunbar | 831570c | 2009-01-22 00:09:25 +0000 | [diff] [blame] | 56 | if (Loc.isValid()) { |
Daniel Dunbar | 831570c | 2009-01-22 00:09:25 +0000 | [diff] [blame] | 57 | Loc = SM.getInstantiationLoc(Loc); |
| 58 | FE = SM.getFileEntryForID(SM.getFileID(Loc)); |
Devang Patel | 7782022 | 2009-02-24 23:16:03 +0000 | [diff] [blame] | 59 | } else { |
| 60 | // If Loc is not valid then use main file id. |
| 61 | FE = SM.getFileEntryForID(SM.getMainFileID()); |
Daniel Dunbar | 831570c | 2009-01-22 00:09:25 +0000 | [diff] [blame] | 62 | } |
| 63 | |
Sanjiv Gupta | e8b9f5b | 2008-05-08 08:54:20 +0000 | [diff] [blame] | 64 | // See if this compile unit has been used before. |
Chris Lattner | 9c85ba3 | 2008-11-10 06:08:34 +0000 | [diff] [blame] | 65 | llvm::DICompileUnit &Unit = CompileUnitCache[FE]; |
| 66 | if (!Unit.isNull()) return Unit; |
| 67 | |
| 68 | // Get source file information. |
Daniel Dunbar | 831570c | 2009-01-22 00:09:25 +0000 | [diff] [blame] | 69 | const char *FileName = FE ? FE->getName() : "<unknown>"; |
Daniel Dunbar | f8e58d0 | 2009-02-07 00:40:41 +0000 | [diff] [blame] | 70 | const char *DirName = FE ? FE->getDir()->getName() : "<unknown>"; |
Daniel Dunbar | 2104bf9 | 2008-10-24 00:46:51 +0000 | [diff] [blame] | 71 | |
Sanjiv Gupta | e8b9f5b | 2008-05-08 08:54:20 +0000 | [diff] [blame] | 72 | // Create new compile unit. |
Chris Lattner | 9c85ba3 | 2008-11-10 06:08:34 +0000 | [diff] [blame] | 73 | // FIXME: Handle other language IDs as well. |
Sanjiv Gupta | e8b9f5b | 2008-05-08 08:54:20 +0000 | [diff] [blame] | 74 | // FIXME: Do not know how to get clang version yet. |
Chris Lattner | 9c85ba3 | 2008-11-10 06:08:34 +0000 | [diff] [blame] | 75 | return Unit = DebugFactory.CreateCompileUnit(llvm::dwarf::DW_LANG_C89, |
| 76 | FileName, DirName, "clang"); |
Sanjiv Gupta | e8b9f5b | 2008-05-08 08:54:20 +0000 | [diff] [blame] | 77 | } |
| 78 | |
Devang Patel | 65e99f2 | 2009-02-25 01:36:11 +0000 | [diff] [blame] | 79 | /// CreateType - Get the Basic type from the cache or create a new |
Chris Lattner | 9c85ba3 | 2008-11-10 06:08:34 +0000 | [diff] [blame] | 80 | /// one if necessary. |
| 81 | llvm::DIType CGDebugInfo::CreateType(const BuiltinType *BT, |
Devang Patel | 65e99f2 | 2009-02-25 01:36:11 +0000 | [diff] [blame] | 82 | llvm::DICompileUnit Unit) { |
Chris Lattner | 9c85ba3 | 2008-11-10 06:08:34 +0000 | [diff] [blame] | 83 | unsigned Encoding = 0; |
| 84 | switch (BT->getKind()) { |
| 85 | default: |
| 86 | case BuiltinType::Void: |
| 87 | return llvm::DIType(); |
| 88 | case BuiltinType::UChar: |
| 89 | case BuiltinType::Char_U: Encoding = llvm::dwarf::DW_ATE_unsigned_char; break; |
| 90 | case BuiltinType::Char_S: |
| 91 | case BuiltinType::SChar: Encoding = llvm::dwarf::DW_ATE_signed_char; break; |
| 92 | case BuiltinType::UShort: |
| 93 | case BuiltinType::UInt: |
| 94 | case BuiltinType::ULong: |
| 95 | case BuiltinType::ULongLong: Encoding = llvm::dwarf::DW_ATE_unsigned; break; |
| 96 | case BuiltinType::Short: |
| 97 | case BuiltinType::Int: |
| 98 | case BuiltinType::Long: |
| 99 | case BuiltinType::LongLong: Encoding = llvm::dwarf::DW_ATE_signed; break; |
| 100 | case BuiltinType::Bool: Encoding = llvm::dwarf::DW_ATE_boolean; break; |
| 101 | case BuiltinType::Float: |
| 102 | case BuiltinType::Double: Encoding = llvm::dwarf::DW_ATE_float; break; |
| 103 | } |
| 104 | // Bit size, align and offset of the type. |
| 105 | uint64_t Size = M->getContext().getTypeSize(BT); |
| 106 | uint64_t Align = M->getContext().getTypeAlign(BT); |
| 107 | uint64_t Offset = 0; |
| 108 | |
| 109 | return DebugFactory.CreateBasicType(Unit, BT->getName(), Unit, 0, Size, Align, |
| 110 | Offset, /*flags*/ 0, Encoding); |
| 111 | } |
Sanjiv Gupta | e8b9f5b | 2008-05-08 08:54:20 +0000 | [diff] [blame] | 112 | |
Sanjiv Gupta | f58c27a | 2008-06-07 04:46:53 +0000 | [diff] [blame] | 113 | /// getOrCreateCVRType - Get the CVR qualified type from the cache or create |
| 114 | /// a new one if necessary. |
Chris Lattner | 9c85ba3 | 2008-11-10 06:08:34 +0000 | [diff] [blame] | 115 | llvm::DIType CGDebugInfo::CreateCVRType(QualType Ty, llvm::DICompileUnit Unit) { |
| 116 | // We will create one Derived type for one qualifier and recurse to handle any |
| 117 | // additional ones. |
| 118 | llvm::DIType FromTy; |
| 119 | unsigned Tag; |
| 120 | if (Ty.isConstQualified()) { |
| 121 | Tag = llvm::dwarf::DW_TAG_const_type; |
| 122 | Ty.removeConst(); |
| 123 | FromTy = getOrCreateType(Ty, Unit); |
| 124 | } else if (Ty.isVolatileQualified()) { |
| 125 | Tag = llvm::dwarf::DW_TAG_volatile_type; |
| 126 | Ty.removeVolatile(); |
| 127 | FromTy = getOrCreateType(Ty, Unit); |
| 128 | } else { |
| 129 | assert(Ty.isRestrictQualified() && "Unknown type qualifier for debug info"); |
| 130 | Tag = llvm::dwarf::DW_TAG_restrict_type; |
| 131 | Ty.removeRestrict(); |
| 132 | FromTy = getOrCreateType(Ty, Unit); |
Sanjiv Gupta | 1c6a38b | 2008-05-25 05:15:42 +0000 | [diff] [blame] | 133 | } |
Chris Lattner | 9c85ba3 | 2008-11-10 06:08:34 +0000 | [diff] [blame] | 134 | |
Daniel Dunbar | 3845f86 | 2008-10-31 03:54:29 +0000 | [diff] [blame] | 135 | // No need to fill in the Name, Line, Size, Alignment, Offset in case of |
| 136 | // CVR derived types. |
Chris Lattner | 9c85ba3 | 2008-11-10 06:08:34 +0000 | [diff] [blame] | 137 | return DebugFactory.CreateDerivedType(Tag, Unit, "", llvm::DICompileUnit(), |
| 138 | 0, 0, 0, 0, 0, FromTy); |
Sanjiv Gupta | 1c6a38b | 2008-05-25 05:15:42 +0000 | [diff] [blame] | 139 | } |
| 140 | |
Chris Lattner | 9c85ba3 | 2008-11-10 06:08:34 +0000 | [diff] [blame] | 141 | llvm::DIType CGDebugInfo::CreateType(const PointerType *Ty, |
| 142 | llvm::DICompileUnit Unit) { |
| 143 | llvm::DIType EltTy = getOrCreateType(Ty->getPointeeType(), Unit); |
Sanjiv Gupta | 1c6a38b | 2008-05-25 05:15:42 +0000 | [diff] [blame] | 144 | |
Sanjiv Gupta | 1c6a38b | 2008-05-25 05:15:42 +0000 | [diff] [blame] | 145 | // Bit size, align and offset of the type. |
Chris Lattner | 9c85ba3 | 2008-11-10 06:08:34 +0000 | [diff] [blame] | 146 | uint64_t Size = M->getContext().getTypeSize(Ty); |
| 147 | uint64_t Align = M->getContext().getTypeAlign(Ty); |
Sanjiv Gupta | 1c6a38b | 2008-05-25 05:15:42 +0000 | [diff] [blame] | 148 | |
Chris Lattner | 9c85ba3 | 2008-11-10 06:08:34 +0000 | [diff] [blame] | 149 | return DebugFactory.CreateDerivedType(llvm::dwarf::DW_TAG_pointer_type, Unit, |
| 150 | "", llvm::DICompileUnit(), |
| 151 | 0, Size, Align, 0, 0, EltTy); |
Sanjiv Gupta | 1c6a38b | 2008-05-25 05:15:42 +0000 | [diff] [blame] | 152 | } |
| 153 | |
Chris Lattner | 9c85ba3 | 2008-11-10 06:08:34 +0000 | [diff] [blame] | 154 | llvm::DIType CGDebugInfo::CreateType(const TypedefType *Ty, |
| 155 | llvm::DICompileUnit Unit) { |
| 156 | // Typedefs are derived from some other type. If we have a typedef of a |
| 157 | // typedef, make sure to emit the whole chain. |
| 158 | llvm::DIType Src = getOrCreateType(Ty->getDecl()->getUnderlyingType(), Unit); |
| 159 | |
| 160 | // We don't set size information, but do specify where the typedef was |
| 161 | // declared. |
Chris Lattner | 8ec03f5 | 2008-11-24 03:54:41 +0000 | [diff] [blame] | 162 | std::string TyName = Ty->getDecl()->getNameAsString(); |
Chris Lattner | 9c85ba3 | 2008-11-10 06:08:34 +0000 | [diff] [blame] | 163 | SourceLocation DefLoc = Ty->getDecl()->getLocation(); |
| 164 | llvm::DICompileUnit DefUnit = getOrCreateCompileUnit(DefLoc); |
Sanjiv Gupta | 1c6a38b | 2008-05-25 05:15:42 +0000 | [diff] [blame] | 165 | |
Sanjiv Gupta | 1c6a38b | 2008-05-25 05:15:42 +0000 | [diff] [blame] | 166 | SourceManager &SM = M->getContext().getSourceManager(); |
Chris Lattner | f7cf85b | 2009-01-16 07:36:28 +0000 | [diff] [blame] | 167 | uint64_t Line = SM.getInstantiationLineNumber(DefLoc); |
Sanjiv Gupta | 1c6a38b | 2008-05-25 05:15:42 +0000 | [diff] [blame] | 168 | |
Chris Lattner | 9c85ba3 | 2008-11-10 06:08:34 +0000 | [diff] [blame] | 169 | return DebugFactory.CreateDerivedType(llvm::dwarf::DW_TAG_typedef, Unit, |
| 170 | TyName, DefUnit, Line, 0, 0, 0, 0, Src); |
Sanjiv Gupta | 1c6a38b | 2008-05-25 05:15:42 +0000 | [diff] [blame] | 171 | } |
| 172 | |
Chris Lattner | 9c85ba3 | 2008-11-10 06:08:34 +0000 | [diff] [blame] | 173 | llvm::DIType CGDebugInfo::CreateType(const FunctionType *Ty, |
| 174 | llvm::DICompileUnit Unit) { |
| 175 | llvm::SmallVector<llvm::DIDescriptor, 16> EltTys; |
Sanjiv Gupta | 1c6a38b | 2008-05-25 05:15:42 +0000 | [diff] [blame] | 176 | |
Chris Lattner | 9c85ba3 | 2008-11-10 06:08:34 +0000 | [diff] [blame] | 177 | // Add the result type at least. |
| 178 | EltTys.push_back(getOrCreateType(Ty->getResultType(), Unit)); |
| 179 | |
| 180 | // Set up remainder of arguments if there is a prototype. |
| 181 | // FIXME: IF NOT, HOW IS THIS REPRESENTED? llvm-gcc doesn't represent '...'! |
| 182 | if (const FunctionTypeProto *FTP = dyn_cast<FunctionTypeProto>(Ty)) { |
| 183 | for (unsigned i = 0, e = FTP->getNumArgs(); i != e; ++i) |
| 184 | EltTys.push_back(getOrCreateType(FTP->getArgType(i), Unit)); |
| 185 | } else { |
| 186 | // FIXME: Handle () case in C. llvm-gcc doesn't do it either. |
Sanjiv Gupta | 1c6a38b | 2008-05-25 05:15:42 +0000 | [diff] [blame] | 187 | } |
| 188 | |
Chris Lattner | 9c85ba3 | 2008-11-10 06:08:34 +0000 | [diff] [blame] | 189 | llvm::DIArray EltTypeArray = |
| 190 | DebugFactory.GetOrCreateArray(&EltTys[0], EltTys.size()); |
| 191 | |
| 192 | return DebugFactory.CreateCompositeType(llvm::dwarf::DW_TAG_subroutine_type, |
| 193 | Unit, "", llvm::DICompileUnit(), |
| 194 | 0, 0, 0, 0, 0, |
| 195 | llvm::DIType(), EltTypeArray); |
Sanjiv Gupta | 1c6a38b | 2008-05-25 05:15:42 +0000 | [diff] [blame] | 196 | } |
| 197 | |
Devang Patel | 65e99f2 | 2009-02-25 01:36:11 +0000 | [diff] [blame] | 198 | /// CreateType - get structure or union type. |
Chris Lattner | 9c85ba3 | 2008-11-10 06:08:34 +0000 | [diff] [blame] | 199 | llvm::DIType CGDebugInfo::CreateType(const RecordType *Ty, |
| 200 | llvm::DICompileUnit Unit) { |
Douglas Gregor | a4c46df | 2008-12-11 17:59:21 +0000 | [diff] [blame] | 201 | RecordDecl *Decl = Ty->getDecl(); |
Sanjiv Gupta | f58c27a | 2008-06-07 04:46:53 +0000 | [diff] [blame] | 202 | |
Chris Lattner | 9c85ba3 | 2008-11-10 06:08:34 +0000 | [diff] [blame] | 203 | unsigned Tag; |
| 204 | if (Decl->isStruct()) |
| 205 | Tag = llvm::dwarf::DW_TAG_structure_type; |
| 206 | else if (Decl->isUnion()) |
| 207 | Tag = llvm::dwarf::DW_TAG_union_type; |
| 208 | else { |
| 209 | assert(Decl->isClass() && "Unknown RecordType!"); |
| 210 | Tag = llvm::dwarf::DW_TAG_class_type; |
Sanjiv Gupta | f58c27a | 2008-06-07 04:46:53 +0000 | [diff] [blame] | 211 | } |
| 212 | |
Sanjiv Gupta | 507de85 | 2008-06-09 10:47:41 +0000 | [diff] [blame] | 213 | SourceManager &SM = M->getContext().getSourceManager(); |
Sanjiv Gupta | 507de85 | 2008-06-09 10:47:41 +0000 | [diff] [blame] | 214 | |
Chris Lattner | 9c85ba3 | 2008-11-10 06:08:34 +0000 | [diff] [blame] | 215 | // Get overall information about the record type for the debug info. |
Chris Lattner | 8ec03f5 | 2008-11-24 03:54:41 +0000 | [diff] [blame] | 216 | std::string Name = Decl->getNameAsString(); |
Sanjiv Gupta | 507de85 | 2008-06-09 10:47:41 +0000 | [diff] [blame] | 217 | |
Chris Lattner | 9c85ba3 | 2008-11-10 06:08:34 +0000 | [diff] [blame] | 218 | llvm::DICompileUnit DefUnit = getOrCreateCompileUnit(Decl->getLocation()); |
Chris Lattner | f7cf85b | 2009-01-16 07:36:28 +0000 | [diff] [blame] | 219 | unsigned Line = SM.getInstantiationLineNumber(Decl->getLocation()); |
Chris Lattner | 9c85ba3 | 2008-11-10 06:08:34 +0000 | [diff] [blame] | 220 | |
| 221 | |
| 222 | // Records and classes and unions can all be recursive. To handle them, we |
| 223 | // first generate a debug descriptor for the struct as a forward declaration. |
| 224 | // Then (if it is a definition) we go through and get debug info for all of |
| 225 | // its members. Finally, we create a descriptor for the complete type (which |
| 226 | // may refer to the forward decl if the struct is recursive) and replace all |
| 227 | // uses of the forward declaration with the final definition. |
| 228 | llvm::DIType FwdDecl = |
| 229 | DebugFactory.CreateCompositeType(Tag, Unit, Name, DefUnit, Line, 0, 0, 0, 0, |
| 230 | llvm::DIType(), llvm::DIArray()); |
| 231 | |
| 232 | // If this is just a forward declaration, return it. |
| 233 | if (!Decl->getDefinition(M->getContext())) |
| 234 | return FwdDecl; |
Sanjiv Gupta | 507de85 | 2008-06-09 10:47:41 +0000 | [diff] [blame] | 235 | |
Chris Lattner | 9c85ba3 | 2008-11-10 06:08:34 +0000 | [diff] [blame] | 236 | // Otherwise, insert it into the TypeCache so that recursive uses will find |
| 237 | // it. |
| 238 | TypeCache[QualType(Ty, 0).getAsOpaquePtr()] = FwdDecl; |
| 239 | |
| 240 | // Convert all the elements. |
| 241 | llvm::SmallVector<llvm::DIDescriptor, 16> EltTys; |
| 242 | |
| 243 | const ASTRecordLayout &RL = M->getContext().getASTRecordLayout(Decl); |
| 244 | |
| 245 | unsigned FieldNo = 0; |
Douglas Gregor | a4c46df | 2008-12-11 17:59:21 +0000 | [diff] [blame] | 246 | for (RecordDecl::field_iterator I = Decl->field_begin(), |
| 247 | E = Decl->field_end(); |
| 248 | I != E; ++I, ++FieldNo) { |
Chris Lattner | 9c85ba3 | 2008-11-10 06:08:34 +0000 | [diff] [blame] | 249 | FieldDecl *Field = *I; |
| 250 | llvm::DIType FieldTy = getOrCreateType(Field->getType(), Unit); |
Chris Lattner | 8ec03f5 | 2008-11-24 03:54:41 +0000 | [diff] [blame] | 251 | |
| 252 | std::string FieldName = Field->getNameAsString(); |
Chris Lattner | 9c85ba3 | 2008-11-10 06:08:34 +0000 | [diff] [blame] | 253 | |
| 254 | // Get the location for the field. |
| 255 | SourceLocation FieldDefLoc = Field->getLocation(); |
| 256 | llvm::DICompileUnit FieldDefUnit = getOrCreateCompileUnit(FieldDefLoc); |
Chris Lattner | f7cf85b | 2009-01-16 07:36:28 +0000 | [diff] [blame] | 257 | unsigned FieldLine = SM.getInstantiationLineNumber(FieldDefLoc); |
Chris Lattner | 9c85ba3 | 2008-11-10 06:08:34 +0000 | [diff] [blame] | 258 | |
| 259 | // Bit size, align and offset of the type. |
| 260 | uint64_t FieldSize = M->getContext().getTypeSize(Ty); |
Chris Lattner | f7cf85b | 2009-01-16 07:36:28 +0000 | [diff] [blame] | 261 | unsigned FieldAlign = M->getContext().getTypeAlign(Ty); |
Chris Lattner | 9c85ba3 | 2008-11-10 06:08:34 +0000 | [diff] [blame] | 262 | uint64_t FieldOffset = RL.getFieldOffset(FieldNo); |
| 263 | |
| 264 | // Create a DW_TAG_member node to remember the offset of this field in the |
| 265 | // struct. FIXME: This is an absolutely insane way to capture this |
| 266 | // information. When we gut debug info, this should be fixed. |
| 267 | FieldTy = DebugFactory.CreateDerivedType(llvm::dwarf::DW_TAG_member, Unit, |
| 268 | FieldName, FieldDefUnit, |
| 269 | FieldLine, FieldSize, FieldAlign, |
| 270 | FieldOffset, 0, FieldTy); |
Chris Lattner | 9c85ba3 | 2008-11-10 06:08:34 +0000 | [diff] [blame] | 271 | EltTys.push_back(FieldTy); |
| 272 | } |
| 273 | |
| 274 | llvm::DIArray Elements = |
| 275 | DebugFactory.GetOrCreateArray(&EltTys[0], EltTys.size()); |
| 276 | |
| 277 | // Bit size, align and offset of the type. |
| 278 | uint64_t Size = M->getContext().getTypeSize(Ty); |
| 279 | uint64_t Align = M->getContext().getTypeAlign(Ty); |
| 280 | |
| 281 | llvm::DIType RealDecl = |
| 282 | DebugFactory.CreateCompositeType(Tag, Unit, Name, DefUnit, Line, Size, |
| 283 | Align, 0, 0, llvm::DIType(), Elements); |
| 284 | |
| 285 | // Now that we have a real decl for the struct, replace anything using the |
| 286 | // old decl with the new one. This will recursively update the debug info. |
| 287 | FwdDecl.getGV()->replaceAllUsesWith(RealDecl.getGV()); |
| 288 | FwdDecl.getGV()->eraseFromParent(); |
| 289 | |
| 290 | return RealDecl; |
| 291 | } |
| 292 | |
| 293 | llvm::DIType CGDebugInfo::CreateType(const EnumType *Ty, |
| 294 | llvm::DICompileUnit Unit) { |
| 295 | EnumDecl *Decl = Ty->getDecl(); |
| 296 | |
| 297 | llvm::SmallVector<llvm::DIDescriptor, 32> Enumerators; |
| 298 | |
| 299 | // Create DIEnumerator elements for each enumerator. |
Douglas Gregor | 44b4321 | 2008-12-11 16:49:14 +0000 | [diff] [blame] | 300 | for (EnumDecl::enumerator_iterator Enum = Decl->enumerator_begin(), |
| 301 | EnumEnd = Decl->enumerator_end(); |
| 302 | Enum != EnumEnd; ++Enum) { |
| 303 | Enumerators.push_back(DebugFactory.CreateEnumerator(Enum->getNameAsString(), |
| 304 | Enum->getInitVal().getZExtValue())); |
Chris Lattner | 9c85ba3 | 2008-11-10 06:08:34 +0000 | [diff] [blame] | 305 | } |
| 306 | |
| 307 | // Return a CompositeType for the enum itself. |
| 308 | llvm::DIArray EltArray = |
| 309 | DebugFactory.GetOrCreateArray(&Enumerators[0], Enumerators.size()); |
| 310 | |
Chris Lattner | 8ec03f5 | 2008-11-24 03:54:41 +0000 | [diff] [blame] | 311 | std::string EnumName = Decl->getNameAsString(); |
Chris Lattner | 9c85ba3 | 2008-11-10 06:08:34 +0000 | [diff] [blame] | 312 | SourceLocation DefLoc = Decl->getLocation(); |
| 313 | llvm::DICompileUnit DefUnit = getOrCreateCompileUnit(DefLoc); |
| 314 | SourceManager &SM = M->getContext().getSourceManager(); |
Chris Lattner | f7cf85b | 2009-01-16 07:36:28 +0000 | [diff] [blame] | 315 | unsigned Line = SM.getInstantiationLineNumber(DefLoc); |
Chris Lattner | 9c85ba3 | 2008-11-10 06:08:34 +0000 | [diff] [blame] | 316 | |
| 317 | // Size and align of the type. |
| 318 | uint64_t Size = M->getContext().getTypeSize(Ty); |
Chris Lattner | f7cf85b | 2009-01-16 07:36:28 +0000 | [diff] [blame] | 319 | unsigned Align = M->getContext().getTypeAlign(Ty); |
Chris Lattner | 9c85ba3 | 2008-11-10 06:08:34 +0000 | [diff] [blame] | 320 | |
| 321 | return DebugFactory.CreateCompositeType(llvm::dwarf::DW_TAG_enumeration_type, |
| 322 | Unit, EnumName, DefUnit, Line, |
| 323 | Size, Align, 0, 0, |
| 324 | llvm::DIType(), EltArray); |
| 325 | } |
| 326 | |
| 327 | llvm::DIType CGDebugInfo::CreateType(const TagType *Ty, |
| 328 | llvm::DICompileUnit Unit) { |
| 329 | if (const RecordType *RT = dyn_cast<RecordType>(Ty)) |
| 330 | return CreateType(RT, Unit); |
| 331 | else if (const EnumType *ET = dyn_cast<EnumType>(Ty)) |
| 332 | return CreateType(ET, Unit); |
| 333 | |
| 334 | return llvm::DIType(); |
| 335 | } |
| 336 | |
| 337 | llvm::DIType CGDebugInfo::CreateType(const ArrayType *Ty, |
| 338 | llvm::DICompileUnit Unit) { |
Anders Carlsson | 835c909 | 2009-01-05 01:23:29 +0000 | [diff] [blame] | 339 | uint64_t Size; |
| 340 | uint64_t Align; |
| 341 | |
| 342 | |
Nuno Lopes | 010d514 | 2009-01-28 00:35:17 +0000 | [diff] [blame] | 343 | // FIXME: make getTypeAlign() aware of VLAs and incomplete array types |
Anders Carlsson | 835c909 | 2009-01-05 01:23:29 +0000 | [diff] [blame] | 344 | if (const VariableArrayType *VAT = dyn_cast<VariableArrayType>(Ty)) { |
Anders Carlsson | 835c909 | 2009-01-05 01:23:29 +0000 | [diff] [blame] | 345 | Size = 0; |
| 346 | Align = |
Nuno Lopes | 010d514 | 2009-01-28 00:35:17 +0000 | [diff] [blame] | 347 | M->getContext().getTypeAlign(M->getContext().getBaseElementType(VAT)); |
| 348 | } else if (Ty->isIncompleteArrayType()) { |
| 349 | Size = 0; |
| 350 | Align = M->getContext().getTypeAlign(Ty->getElementType()); |
Anders Carlsson | 835c909 | 2009-01-05 01:23:29 +0000 | [diff] [blame] | 351 | } else { |
| 352 | // Size and align of the whole array, not the element type. |
| 353 | Size = M->getContext().getTypeSize(Ty); |
| 354 | Align = M->getContext().getTypeAlign(Ty); |
| 355 | } |
Chris Lattner | 9c85ba3 | 2008-11-10 06:08:34 +0000 | [diff] [blame] | 356 | |
| 357 | // Add the dimensions of the array. FIXME: This loses CV qualifiers from |
| 358 | // interior arrays, do we care? Why aren't nested arrays represented the |
| 359 | // obvious/recursive way? |
| 360 | llvm::SmallVector<llvm::DIDescriptor, 8> Subscripts; |
| 361 | QualType EltTy(Ty, 0); |
| 362 | while ((Ty = dyn_cast<ArrayType>(EltTy))) { |
Sanjiv Gupta | 507de85 | 2008-06-09 10:47:41 +0000 | [diff] [blame] | 363 | uint64_t Upper = 0; |
Chris Lattner | 9c85ba3 | 2008-11-10 06:08:34 +0000 | [diff] [blame] | 364 | if (const ConstantArrayType *CAT = dyn_cast<ConstantArrayType>(Ty)) |
| 365 | Upper = CAT->getSize().getZExtValue() - 1; |
| 366 | // FIXME: Verify this is right for VLAs. |
| 367 | Subscripts.push_back(DebugFactory.GetOrCreateSubrange(0, Upper)); |
| 368 | EltTy = Ty->getElementType(); |
Sanjiv Gupta | 507de85 | 2008-06-09 10:47:41 +0000 | [diff] [blame] | 369 | } |
Sanjiv Gupta | 1c6a38b | 2008-05-25 05:15:42 +0000 | [diff] [blame] | 370 | |
Chris Lattner | 9c85ba3 | 2008-11-10 06:08:34 +0000 | [diff] [blame] | 371 | llvm::DIArray SubscriptArray = |
| 372 | DebugFactory.GetOrCreateArray(&Subscripts[0], Subscripts.size()); |
| 373 | |
| 374 | return DebugFactory.CreateCompositeType(llvm::dwarf::DW_TAG_array_type, |
| 375 | Unit, "", llvm::DICompileUnit(), |
| 376 | 0, Size, Align, 0, 0, |
| 377 | getOrCreateType(EltTy, Unit), |
| 378 | SubscriptArray); |
| 379 | } |
| 380 | |
| 381 | |
Sanjiv Gupta | 1c6a38b | 2008-05-25 05:15:42 +0000 | [diff] [blame] | 382 | /// getOrCreateType - Get the type from the cache or create a new |
| 383 | /// one if necessary. |
Chris Lattner | 9c85ba3 | 2008-11-10 06:08:34 +0000 | [diff] [blame] | 384 | llvm::DIType CGDebugInfo::getOrCreateType(QualType Ty, |
| 385 | llvm::DICompileUnit Unit) { |
| 386 | if (Ty.isNull()) |
| 387 | return llvm::DIType(); |
| 388 | |
Sanjiv Gupta | 1c6a38b | 2008-05-25 05:15:42 +0000 | [diff] [blame] | 389 | // Check to see if the compile unit already has created this type. |
Chris Lattner | 9c85ba3 | 2008-11-10 06:08:34 +0000 | [diff] [blame] | 390 | llvm::DIType &Slot = TypeCache[Ty.getAsOpaquePtr()]; |
| 391 | if (!Slot.isNull()) return Slot; |
Sanjiv Gupta | 1c6a38b | 2008-05-25 05:15:42 +0000 | [diff] [blame] | 392 | |
Chris Lattner | 9c85ba3 | 2008-11-10 06:08:34 +0000 | [diff] [blame] | 393 | // Handle CVR qualifiers, which recursively handles what they refer to. |
| 394 | if (Ty.getCVRQualifiers()) |
| 395 | return Slot = CreateCVRType(Ty, Unit); |
Sanjiv Gupta | 1c6a38b | 2008-05-25 05:15:42 +0000 | [diff] [blame] | 396 | |
| 397 | // Work out details of type. |
Chris Lattner | 9c85ba3 | 2008-11-10 06:08:34 +0000 | [diff] [blame] | 398 | switch (Ty->getTypeClass()) { |
| 399 | case Type::Complex: |
| 400 | case Type::Reference: |
| 401 | case Type::Vector: |
| 402 | case Type::ExtVector: |
Fariborz Jahanian | f11284a | 2009-02-17 18:27:45 +0000 | [diff] [blame] | 403 | case Type::ExtQual: |
Chris Lattner | 9c85ba3 | 2008-11-10 06:08:34 +0000 | [diff] [blame] | 404 | case Type::ObjCInterface: |
| 405 | case Type::ObjCQualifiedInterface: |
| 406 | case Type::ObjCQualifiedId: |
Chris Lattner | 9c85ba3 | 2008-11-10 06:08:34 +0000 | [diff] [blame] | 407 | default: |
| 408 | return llvm::DIType(); |
Sanjiv Gupta | 1c6a38b | 2008-05-25 05:15:42 +0000 | [diff] [blame] | 409 | |
Chris Lattner | 9c85ba3 | 2008-11-10 06:08:34 +0000 | [diff] [blame] | 410 | case Type::Builtin: Slot = CreateType(cast<BuiltinType>(Ty), Unit); break; |
| 411 | case Type::Pointer: Slot = CreateType(cast<PointerType>(Ty), Unit); break; |
| 412 | case Type::TypeName: Slot = CreateType(cast<TypedefType>(Ty), Unit); break; |
| 413 | case Type::Tagged: Slot = CreateType(cast<TagType>(Ty), Unit); break; |
| 414 | case Type::FunctionProto: |
| 415 | case Type::FunctionNoProto: |
Chris Lattner | 3cc5c40 | 2008-11-11 07:01:36 +0000 | [diff] [blame] | 416 | return Slot = CreateType(cast<FunctionType>(Ty), Unit); |
Chris Lattner | 9c85ba3 | 2008-11-10 06:08:34 +0000 | [diff] [blame] | 417 | |
| 418 | case Type::ConstantArray: |
| 419 | case Type::VariableArray: |
| 420 | case Type::IncompleteArray: |
Chris Lattner | 3cc5c40 | 2008-11-11 07:01:36 +0000 | [diff] [blame] | 421 | return Slot = CreateType(cast<ArrayType>(Ty), Unit); |
| 422 | case Type::TypeOfExp: |
| 423 | return Slot = getOrCreateType(cast<TypeOfExpr>(Ty)->getUnderlyingExpr() |
| 424 | ->getType(), Unit); |
| 425 | case Type::TypeOfTyp: |
| 426 | return Slot = getOrCreateType(cast<TypeOfType>(Ty)->getUnderlyingType(), |
| 427 | Unit); |
Sanjiv Gupta | 1c6a38b | 2008-05-25 05:15:42 +0000 | [diff] [blame] | 428 | } |
Chris Lattner | 9c85ba3 | 2008-11-10 06:08:34 +0000 | [diff] [blame] | 429 | |
Sanjiv Gupta | 1c6a38b | 2008-05-25 05:15:42 +0000 | [diff] [blame] | 430 | return Slot; |
| 431 | } |
| 432 | |
| 433 | /// EmitFunctionStart - Constructs the debug code for entering a function - |
| 434 | /// "llvm.dbg.func.start.". |
Chris Lattner | 9c85ba3 | 2008-11-10 06:08:34 +0000 | [diff] [blame] | 435 | void CGDebugInfo::EmitFunctionStart(const char *Name, QualType ReturnType, |
Sanjiv Gupta | 1c6a38b | 2008-05-25 05:15:42 +0000 | [diff] [blame] | 436 | llvm::Function *Fn, |
Chris Lattner | 9c85ba3 | 2008-11-10 06:08:34 +0000 | [diff] [blame] | 437 | CGBuilderTy &Builder) { |
| 438 | // FIXME: Why is this using CurLoc??? |
| 439 | llvm::DICompileUnit Unit = getOrCreateCompileUnit(CurLoc); |
Sanjiv Gupta | 1c6a38b | 2008-05-25 05:15:42 +0000 | [diff] [blame] | 440 | SourceManager &SM = M->getContext().getSourceManager(); |
Chris Lattner | f7cf85b | 2009-01-16 07:36:28 +0000 | [diff] [blame] | 441 | unsigned LineNo = SM.getInstantiationLineNumber(CurLoc); |
Chris Lattner | 9c85ba3 | 2008-11-10 06:08:34 +0000 | [diff] [blame] | 442 | |
| 443 | llvm::DISubprogram SP = |
| 444 | DebugFactory.CreateSubprogram(Unit, Name, Name, "", Unit, LineNo, |
| 445 | getOrCreateType(ReturnType, Unit), |
| 446 | Fn->hasInternalLinkage(), true/*definition*/); |
| 447 | |
| 448 | DebugFactory.InsertSubprogramStart(SP, Builder.GetInsertBlock()); |
| 449 | |
Sanjiv Gupta | 1c6a38b | 2008-05-25 05:15:42 +0000 | [diff] [blame] | 450 | // Push function on region stack. |
Chris Lattner | 9c85ba3 | 2008-11-10 06:08:34 +0000 | [diff] [blame] | 451 | RegionStack.push_back(SP); |
Sanjiv Gupta | 1c6a38b | 2008-05-25 05:15:42 +0000 | [diff] [blame] | 452 | } |
| 453 | |
| 454 | |
Chris Lattner | 9c85ba3 | 2008-11-10 06:08:34 +0000 | [diff] [blame] | 455 | void CGDebugInfo::EmitStopPoint(llvm::Function *Fn, CGBuilderTy &Builder) { |
Sanjiv Gupta | 1c6a38b | 2008-05-25 05:15:42 +0000 | [diff] [blame] | 456 | if (CurLoc.isInvalid() || CurLoc.isMacroID()) return; |
| 457 | |
Sanjiv Gupta | e8b9f5b | 2008-05-08 08:54:20 +0000 | [diff] [blame] | 458 | // Don't bother if things are the same as last time. |
| 459 | SourceManager &SM = M->getContext().getSourceManager(); |
| 460 | if (CurLoc == PrevLoc |
Chris Lattner | 30fc933 | 2009-02-04 01:06:56 +0000 | [diff] [blame] | 461 | || (SM.getInstantiationLineNumber(CurLoc) == |
| 462 | SM.getInstantiationLineNumber(PrevLoc) |
Sanjiv Gupta | e8b9f5b | 2008-05-08 08:54:20 +0000 | [diff] [blame] | 463 | && SM.isFromSameFile(CurLoc, PrevLoc))) |
| 464 | return; |
Sanjiv Gupta | e8b9f5b | 2008-05-08 08:54:20 +0000 | [diff] [blame] | 465 | |
| 466 | // Update last state. |
| 467 | PrevLoc = CurLoc; |
| 468 | |
| 469 | // Get the appropriate compile unit. |
Chris Lattner | 9c85ba3 | 2008-11-10 06:08:34 +0000 | [diff] [blame] | 470 | llvm::DICompileUnit Unit = getOrCreateCompileUnit(CurLoc); |
Chris Lattner | f7cf85b | 2009-01-16 07:36:28 +0000 | [diff] [blame] | 471 | DebugFactory.InsertStopPoint(Unit, SM.getInstantiationLineNumber(CurLoc), |
| 472 | SM.getInstantiationColumnNumber(CurLoc), |
Chris Lattner | 9c85ba3 | 2008-11-10 06:08:34 +0000 | [diff] [blame] | 473 | Builder.GetInsertBlock()); |
Sanjiv Gupta | e8b9f5b | 2008-05-08 08:54:20 +0000 | [diff] [blame] | 474 | } |
| 475 | |
| 476 | /// EmitRegionStart- Constructs the debug code for entering a declarative |
| 477 | /// region - "llvm.dbg.region.start.". |
Chris Lattner | 9c85ba3 | 2008-11-10 06:08:34 +0000 | [diff] [blame] | 478 | void CGDebugInfo::EmitRegionStart(llvm::Function *Fn, CGBuilderTy &Builder) { |
| 479 | llvm::DIDescriptor D; |
Daniel Dunbar | 5273f51 | 2008-10-17 01:07:56 +0000 | [diff] [blame] | 480 | if (!RegionStack.empty()) |
Chris Lattner | 9c85ba3 | 2008-11-10 06:08:34 +0000 | [diff] [blame] | 481 | D = RegionStack.back(); |
| 482 | D = DebugFactory.CreateBlock(D); |
| 483 | RegionStack.push_back(D); |
| 484 | DebugFactory.InsertRegionStart(D, Builder.GetInsertBlock()); |
Sanjiv Gupta | e8b9f5b | 2008-05-08 08:54:20 +0000 | [diff] [blame] | 485 | } |
| 486 | |
| 487 | /// EmitRegionEnd - Constructs the debug code for exiting a declarative |
| 488 | /// region - "llvm.dbg.region.end." |
Chris Lattner | 9c85ba3 | 2008-11-10 06:08:34 +0000 | [diff] [blame] | 489 | void CGDebugInfo::EmitRegionEnd(llvm::Function *Fn, CGBuilderTy &Builder) { |
Daniel Dunbar | 5273f51 | 2008-10-17 01:07:56 +0000 | [diff] [blame] | 490 | assert(!RegionStack.empty() && "Region stack mismatch, stack empty!"); |
| 491 | |
Sanjiv Gupta | e8b9f5b | 2008-05-08 08:54:20 +0000 | [diff] [blame] | 492 | // Provide an region stop point. |
| 493 | EmitStopPoint(Fn, Builder); |
| 494 | |
Chris Lattner | 9c85ba3 | 2008-11-10 06:08:34 +0000 | [diff] [blame] | 495 | DebugFactory.InsertRegionEnd(RegionStack.back(), Builder.GetInsertBlock()); |
Sanjiv Gupta | 1c6a38b | 2008-05-25 05:15:42 +0000 | [diff] [blame] | 496 | RegionStack.pop_back(); |
Sanjiv Gupta | e8b9f5b | 2008-05-08 08:54:20 +0000 | [diff] [blame] | 497 | } |
| 498 | |
Sanjiv Gupta | cc9b163 | 2008-05-30 10:30:31 +0000 | [diff] [blame] | 499 | /// EmitDeclare - Emit local variable declaration debug info. |
Chris Lattner | 9c85ba3 | 2008-11-10 06:08:34 +0000 | [diff] [blame] | 500 | void CGDebugInfo::EmitDeclare(const VarDecl *Decl, unsigned Tag, |
| 501 | llvm::Value *Storage, CGBuilderTy &Builder) { |
Daniel Dunbar | 5273f51 | 2008-10-17 01:07:56 +0000 | [diff] [blame] | 502 | assert(!RegionStack.empty() && "Region stack mismatch, stack empty!"); |
| 503 | |
Chris Lattner | 9c85ba3 | 2008-11-10 06:08:34 +0000 | [diff] [blame] | 504 | // Get location information. |
Sanjiv Gupta | cc9b163 | 2008-05-30 10:30:31 +0000 | [diff] [blame] | 505 | SourceManager &SM = M->getContext().getSourceManager(); |
Chris Lattner | f7cf85b | 2009-01-16 07:36:28 +0000 | [diff] [blame] | 506 | unsigned Line = SM.getInstantiationLineNumber(Decl->getLocation()); |
Chris Lattner | 9c85ba3 | 2008-11-10 06:08:34 +0000 | [diff] [blame] | 507 | llvm::DICompileUnit Unit = getOrCreateCompileUnit(Decl->getLocation()); |
| 508 | |
| 509 | // Create the descriptor for the variable. |
| 510 | llvm::DIVariable D = |
Chris Lattner | d9d22dd | 2008-11-24 05:29:24 +0000 | [diff] [blame] | 511 | DebugFactory.CreateVariable(Tag, RegionStack.back(),Decl->getNameAsString(), |
Chris Lattner | 9c85ba3 | 2008-11-10 06:08:34 +0000 | [diff] [blame] | 512 | Unit, Line, |
| 513 | getOrCreateType(Decl->getType(), Unit)); |
| 514 | // Insert an llvm.dbg.declare into the current block. |
| 515 | DebugFactory.InsertDeclare(Storage, D, Builder.GetInsertBlock()); |
Sanjiv Gupta | cc9b163 | 2008-05-30 10:30:31 +0000 | [diff] [blame] | 516 | } |
| 517 | |
Chris Lattner | 9c85ba3 | 2008-11-10 06:08:34 +0000 | [diff] [blame] | 518 | void CGDebugInfo::EmitDeclareOfAutoVariable(const VarDecl *Decl, |
| 519 | llvm::Value *Storage, |
| 520 | CGBuilderTy &Builder) { |
| 521 | EmitDeclare(Decl, llvm::dwarf::DW_TAG_auto_variable, Storage, Builder); |
| 522 | } |
| 523 | |
| 524 | /// EmitDeclareOfArgVariable - Emit call to llvm.dbg.declare for an argument |
| 525 | /// variable declaration. |
| 526 | void CGDebugInfo::EmitDeclareOfArgVariable(const VarDecl *Decl, llvm::Value *AI, |
| 527 | CGBuilderTy &Builder) { |
| 528 | EmitDeclare(Decl, llvm::dwarf::DW_TAG_arg_variable, AI, Builder); |
| 529 | } |
| 530 | |
| 531 | |
| 532 | |
Sanjiv Gupta | 686226b | 2008-06-05 08:59:10 +0000 | [diff] [blame] | 533 | /// EmitGlobalVariable - Emit information about a global variable. |
Chris Lattner | 9c85ba3 | 2008-11-10 06:08:34 +0000 | [diff] [blame] | 534 | void CGDebugInfo::EmitGlobalVariable(llvm::GlobalVariable *Var, |
| 535 | const VarDecl *Decl) { |
Sanjiv Gupta | 686226b | 2008-06-05 08:59:10 +0000 | [diff] [blame] | 536 | // Create global variable debug descriptor. |
Chris Lattner | 9c85ba3 | 2008-11-10 06:08:34 +0000 | [diff] [blame] | 537 | llvm::DICompileUnit Unit = getOrCreateCompileUnit(Decl->getLocation()); |
Sanjiv Gupta | 686226b | 2008-06-05 08:59:10 +0000 | [diff] [blame] | 538 | SourceManager &SM = M->getContext().getSourceManager(); |
Chris Lattner | f7cf85b | 2009-01-16 07:36:28 +0000 | [diff] [blame] | 539 | unsigned LineNo = SM.getInstantiationLineNumber(Decl->getLocation()); |
Chris Lattner | 8ec03f5 | 2008-11-24 03:54:41 +0000 | [diff] [blame] | 540 | |
| 541 | std::string Name = Decl->getNameAsString(); |
Anders Carlsson | 4d6e8dd | 2008-11-26 17:40:42 +0000 | [diff] [blame] | 542 | |
| 543 | QualType T = Decl->getType(); |
| 544 | if (T->isIncompleteArrayType()) { |
| 545 | |
| 546 | // CodeGen turns int[] into int[1] so we'll do the same here. |
| 547 | llvm::APSInt ConstVal(32); |
| 548 | |
| 549 | ConstVal = 1; |
| 550 | QualType ET = M->getContext().getAsArrayType(T)->getElementType(); |
| 551 | |
| 552 | T = M->getContext().getConstantArrayType(ET, ConstVal, |
| 553 | ArrayType::Normal, 0); |
| 554 | } |
| 555 | |
Chris Lattner | 9c85ba3 | 2008-11-10 06:08:34 +0000 | [diff] [blame] | 556 | DebugFactory.CreateGlobalVariable(Unit, Name, Name, "", Unit, LineNo, |
Anders Carlsson | 4d6e8dd | 2008-11-26 17:40:42 +0000 | [diff] [blame] | 557 | getOrCreateType(T, Unit), |
Chris Lattner | 9c85ba3 | 2008-11-10 06:08:34 +0000 | [diff] [blame] | 558 | Var->hasInternalLinkage(), |
| 559 | true/*definition*/, Var); |
Sanjiv Gupta | 686226b | 2008-06-05 08:59:10 +0000 | [diff] [blame] | 560 | } |
| 561 | |