blob: f2ca2e7c589c96666fbe6e89a36302b9abdcdf38 [file] [log] [blame]
Sanjiv Gupta40e56a12008-05-08 08:54:20 +00001//===--- 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 Gupta93eb8252008-05-25 05:15:42 +000016#include "clang/AST/ASTContext.h"
Devang Patel3fc13e32009-02-26 21:10:26 +000017#include "clang/AST/DeclObjC.h"
Chris Lattnere3afa462008-11-11 07:01:36 +000018#include "clang/AST/Expr.h"
Daniel Dunbarde300732008-08-11 04:54:23 +000019#include "clang/AST/RecordLayout.h"
Sanjiv Gupta93eb8252008-05-25 05:15:42 +000020#include "clang/Basic/SourceManager.h"
21#include "clang/Basic/FileManager.h"
Devang Patelbcded192009-03-27 23:16:32 +000022#include "clang/Frontend/CompileOptions.h"
Sanjiv Gupta40e56a12008-05-08 08:54:20 +000023#include "llvm/Constants.h"
24#include "llvm/DerivedTypes.h"
25#include "llvm/Instructions.h"
26#include "llvm/Intrinsics.h"
27#include "llvm/Module.h"
Sanjiv Gupta40e56a12008-05-08 08:54:20 +000028#include "llvm/ADT/StringExtras.h"
29#include "llvm/ADT/SmallVector.h"
Sanjiv Gupta93eb8252008-05-25 05:15:42 +000030#include "llvm/Support/Dwarf.h"
Devang Patel6fe2e152009-04-17 21:06:59 +000031#include "llvm/System/Path.h"
Sanjiv Gupta93eb8252008-05-25 05:15:42 +000032#include "llvm/Target/TargetMachine.h"
Sanjiv Gupta40e56a12008-05-08 08:54:20 +000033using namespace clang;
34using namespace clang::CodeGen;
35
36CGDebugInfo::CGDebugInfo(CodeGenModule *m)
Chris Lattner562ce0a2008-11-10 06:08:34 +000037 : M(m), DebugFactory(M->getModule()) {
Sanjiv Gupta40e56a12008-05-08 08:54:20 +000038}
39
Chris Lattner562ce0a2008-11-10 06:08:34 +000040CGDebugInfo::~CGDebugInfo() {
Daniel Dunbar6fc1f972008-10-17 16:15:48 +000041 assert(RegionStack.empty() && "Region stack mismatch, stack not empty!");
Sanjiv Gupta40e56a12008-05-08 08:54:20 +000042}
43
Chris Lattner562ce0a2008-11-10 06:08:34 +000044void CGDebugInfo::setLocation(SourceLocation Loc) {
45 if (Loc.isValid())
Chris Lattner18c8dc02009-01-16 07:36:28 +000046 CurLoc = M->getContext().getSourceManager().getInstantiationLoc(Loc);
Sanjiv Gupta93eb8252008-05-25 05:15:42 +000047}
48
Sanjiv Gupta40e56a12008-05-08 08:54:20 +000049/// getOrCreateCompileUnit - Get the compile unit from the cache or create a new
Daniel Dunbar4a66ef12008-10-24 08:38:36 +000050/// one if necessary. This returns null for invalid source locations.
Chris Lattner562ce0a2008-11-10 06:08:34 +000051llvm::DICompileUnit CGDebugInfo::getOrCreateCompileUnit(SourceLocation Loc) {
Devang Patel6fe2e152009-04-17 21:06:59 +000052 // Get source file information.
53 const char *FileName = "<unknown>";
Devang Patel61910072009-02-24 23:16:03 +000054 SourceManager &SM = M->getContext().getSourceManager();
Chris Lattnerf9890532009-04-19 06:50:29 +000055 unsigned FID = 0;
Daniel Dunbar67bb1fa2009-01-22 00:09:25 +000056 if (Loc.isValid()) {
Devang Patel6fe2e152009-04-17 21:06:59 +000057 PresumedLoc PLoc = SM.getPresumedLoc(Loc);
58 FileName = PLoc.getFilename();
59 FID = PLoc.getIncludeLoc().getRawEncoding();
Daniel Dunbar67bb1fa2009-01-22 00:09:25 +000060 }
61
Sanjiv Gupta40e56a12008-05-08 08:54:20 +000062 // See if this compile unit has been used before.
Devang Patel6fe2e152009-04-17 21:06:59 +000063 llvm::DICompileUnit &Unit = CompileUnitCache[FID];
Chris Lattner562ce0a2008-11-10 06:08:34 +000064 if (!Unit.isNull()) return Unit;
Daniel Dunbardedc94c2009-04-08 05:11:16 +000065
Devang Patel6fe2e152009-04-17 21:06:59 +000066 // Get absolute path name.
67 llvm::sys::Path AbsFileName(FileName);
68 if (!AbsFileName.isAbsolute()) {
69 llvm::sys::Path tmp = llvm::sys::Path::GetCurrentDirectory();
70 tmp.appendComponent(FileName);
71 AbsFileName = tmp;
72 }
73
74 // See if thie compile unit is represnting main source file.
75 bool isMain = false;
76 const LangOptions &LO = M->getLangOptions();
77 const char *MainFileName = LO.getMainFileName();
78 if (MainFileName) {
79 if (!strcmp(AbsFileName.getLast().c_str(), MainFileName))
80 isMain = true;
81 } else {
82 if (Loc.isValid() && SM.isFromMainFile(Loc))
83 isMain = true;
84 }
Daniel Dunbardedc94c2009-04-08 05:11:16 +000085
Chris Lattnera9aae3e2009-03-25 03:28:08 +000086 unsigned LangTag;
87 if (LO.CPlusPlus) {
88 if (LO.ObjC1)
89 LangTag = llvm::dwarf::DW_LANG_ObjC_plus_plus;
90 else
91 LangTag = llvm::dwarf::DW_LANG_C_plus_plus;
92 } else if (LO.ObjC1) {
Devang Patel07461fa2009-03-24 20:35:51 +000093 LangTag = llvm::dwarf::DW_LANG_ObjC;
Chris Lattnera9aae3e2009-03-25 03:28:08 +000094 } else if (LO.C99) {
Devang Patel07461fa2009-03-24 20:35:51 +000095 LangTag = llvm::dwarf::DW_LANG_C99;
Chris Lattnera9aae3e2009-03-25 03:28:08 +000096 } else {
97 LangTag = llvm::dwarf::DW_LANG_C89;
98 }
Devang Patel6fe2e152009-04-17 21:06:59 +000099
Sanjiv Gupta40e56a12008-05-08 08:54:20 +0000100 // Create new compile unit.
Sanjiv Gupta40e56a12008-05-08 08:54:20 +0000101 // FIXME: Do not know how to get clang version yet.
Devang Patel000ee2f2009-03-19 00:23:53 +0000102 // FIXME: Encode command line options.
103 // FIXME: Encode optimization level.
Devang Patel6fe2e152009-04-17 21:06:59 +0000104 return Unit = DebugFactory.CreateCompileUnit(LangTag, AbsFileName.getLast(),
105 AbsFileName.getDirname(),
Devang Patel07461fa2009-03-24 20:35:51 +0000106 "clang", isMain);
Sanjiv Gupta40e56a12008-05-08 08:54:20 +0000107}
108
Devang Patela4cc0c42009-02-25 01:36:11 +0000109/// CreateType - Get the Basic type from the cache or create a new
Chris Lattner562ce0a2008-11-10 06:08:34 +0000110/// one if necessary.
111llvm::DIType CGDebugInfo::CreateType(const BuiltinType *BT,
Devang Patela4cc0c42009-02-25 01:36:11 +0000112 llvm::DICompileUnit Unit) {
Chris Lattner562ce0a2008-11-10 06:08:34 +0000113 unsigned Encoding = 0;
114 switch (BT->getKind()) {
115 default:
116 case BuiltinType::Void:
117 return llvm::DIType();
118 case BuiltinType::UChar:
119 case BuiltinType::Char_U: Encoding = llvm::dwarf::DW_ATE_unsigned_char; break;
120 case BuiltinType::Char_S:
121 case BuiltinType::SChar: Encoding = llvm::dwarf::DW_ATE_signed_char; break;
122 case BuiltinType::UShort:
123 case BuiltinType::UInt:
124 case BuiltinType::ULong:
125 case BuiltinType::ULongLong: Encoding = llvm::dwarf::DW_ATE_unsigned; break;
126 case BuiltinType::Short:
127 case BuiltinType::Int:
128 case BuiltinType::Long:
129 case BuiltinType::LongLong: Encoding = llvm::dwarf::DW_ATE_signed; break;
130 case BuiltinType::Bool: Encoding = llvm::dwarf::DW_ATE_boolean; break;
131 case BuiltinType::Float:
132 case BuiltinType::Double: Encoding = llvm::dwarf::DW_ATE_float; break;
133 }
134 // Bit size, align and offset of the type.
135 uint64_t Size = M->getContext().getTypeSize(BT);
136 uint64_t Align = M->getContext().getTypeAlign(BT);
137 uint64_t Offset = 0;
138
139 return DebugFactory.CreateBasicType(Unit, BT->getName(), Unit, 0, Size, Align,
140 Offset, /*flags*/ 0, Encoding);
141}
Sanjiv Gupta40e56a12008-05-08 08:54:20 +0000142
Chris Lattner016d55e2009-04-23 06:13:01 +0000143llvm::DIType CGDebugInfo::CreateType(const ComplexType *Ty,
144 llvm::DICompileUnit Unit) {
145 // Bit size, align and offset of the type.
146 unsigned Encoding = llvm::dwarf::DW_ATE_complex_float;
147 if (Ty->isComplexIntegerType())
148 Encoding = llvm::dwarf::DW_ATE_lo_user;
149
150 uint64_t Size = M->getContext().getTypeSize(Ty);
151 uint64_t Align = M->getContext().getTypeAlign(Ty);
152 uint64_t Offset = 0;
153
154 return DebugFactory.CreateBasicType(Unit, "complex",
155 Unit, 0, Size, Align,
156 Offset, /*flags*/ 0, Encoding);
157}
158
Sanjiv Gupta0f805bd2008-06-07 04:46:53 +0000159/// getOrCreateCVRType - Get the CVR qualified type from the cache or create
160/// a new one if necessary.
Chris Lattner562ce0a2008-11-10 06:08:34 +0000161llvm::DIType CGDebugInfo::CreateCVRType(QualType Ty, llvm::DICompileUnit Unit) {
162 // We will create one Derived type for one qualifier and recurse to handle any
163 // additional ones.
164 llvm::DIType FromTy;
165 unsigned Tag;
166 if (Ty.isConstQualified()) {
167 Tag = llvm::dwarf::DW_TAG_const_type;
168 Ty.removeConst();
169 FromTy = getOrCreateType(Ty, Unit);
170 } else if (Ty.isVolatileQualified()) {
171 Tag = llvm::dwarf::DW_TAG_volatile_type;
172 Ty.removeVolatile();
173 FromTy = getOrCreateType(Ty, Unit);
174 } else {
175 assert(Ty.isRestrictQualified() && "Unknown type qualifier for debug info");
176 Tag = llvm::dwarf::DW_TAG_restrict_type;
177 Ty.removeRestrict();
178 FromTy = getOrCreateType(Ty, Unit);
Sanjiv Gupta93eb8252008-05-25 05:15:42 +0000179 }
Chris Lattner562ce0a2008-11-10 06:08:34 +0000180
Daniel Dunbar44252b42008-10-31 03:54:29 +0000181 // No need to fill in the Name, Line, Size, Alignment, Offset in case of
182 // CVR derived types.
Chris Lattner562ce0a2008-11-10 06:08:34 +0000183 return DebugFactory.CreateDerivedType(Tag, Unit, "", llvm::DICompileUnit(),
184 0, 0, 0, 0, 0, FromTy);
Sanjiv Gupta93eb8252008-05-25 05:15:42 +0000185}
186
Chris Lattner562ce0a2008-11-10 06:08:34 +0000187llvm::DIType CGDebugInfo::CreateType(const PointerType *Ty,
188 llvm::DICompileUnit Unit) {
189 llvm::DIType EltTy = getOrCreateType(Ty->getPointeeType(), Unit);
Sanjiv Gupta93eb8252008-05-25 05:15:42 +0000190
Sanjiv Gupta93eb8252008-05-25 05:15:42 +0000191 // Bit size, align and offset of the type.
Chris Lattner562ce0a2008-11-10 06:08:34 +0000192 uint64_t Size = M->getContext().getTypeSize(Ty);
193 uint64_t Align = M->getContext().getTypeAlign(Ty);
Sanjiv Gupta93eb8252008-05-25 05:15:42 +0000194
Chris Lattner562ce0a2008-11-10 06:08:34 +0000195 return DebugFactory.CreateDerivedType(llvm::dwarf::DW_TAG_pointer_type, Unit,
196 "", llvm::DICompileUnit(),
197 0, Size, Align, 0, 0, EltTy);
Sanjiv Gupta93eb8252008-05-25 05:15:42 +0000198}
199
Chris Lattner562ce0a2008-11-10 06:08:34 +0000200llvm::DIType CGDebugInfo::CreateType(const TypedefType *Ty,
201 llvm::DICompileUnit Unit) {
202 // Typedefs are derived from some other type. If we have a typedef of a
203 // typedef, make sure to emit the whole chain.
204 llvm::DIType Src = getOrCreateType(Ty->getDecl()->getUnderlyingType(), Unit);
205
206 // We don't set size information, but do specify where the typedef was
207 // declared.
Chris Lattnerd120b9e2008-11-24 03:54:41 +0000208 std::string TyName = Ty->getDecl()->getNameAsString();
Chris Lattner562ce0a2008-11-10 06:08:34 +0000209 SourceLocation DefLoc = Ty->getDecl()->getLocation();
210 llvm::DICompileUnit DefUnit = getOrCreateCompileUnit(DefLoc);
Sanjiv Gupta93eb8252008-05-25 05:15:42 +0000211
Sanjiv Gupta93eb8252008-05-25 05:15:42 +0000212 SourceManager &SM = M->getContext().getSourceManager();
Devang Patel9b3f5f72009-04-17 21:35:15 +0000213 PresumedLoc PLoc = SM.getPresumedLoc(DefLoc);
214 unsigned Line = PLoc.isInvalid() ? 0 : PLoc.getLine();
Sanjiv Gupta93eb8252008-05-25 05:15:42 +0000215
Chris Lattner562ce0a2008-11-10 06:08:34 +0000216 return DebugFactory.CreateDerivedType(llvm::dwarf::DW_TAG_typedef, Unit,
217 TyName, DefUnit, Line, 0, 0, 0, 0, Src);
Sanjiv Gupta93eb8252008-05-25 05:15:42 +0000218}
219
Chris Lattner562ce0a2008-11-10 06:08:34 +0000220llvm::DIType CGDebugInfo::CreateType(const FunctionType *Ty,
221 llvm::DICompileUnit Unit) {
222 llvm::SmallVector<llvm::DIDescriptor, 16> EltTys;
Sanjiv Gupta93eb8252008-05-25 05:15:42 +0000223
Chris Lattner562ce0a2008-11-10 06:08:34 +0000224 // Add the result type at least.
225 EltTys.push_back(getOrCreateType(Ty->getResultType(), Unit));
226
227 // Set up remainder of arguments if there is a prototype.
228 // FIXME: IF NOT, HOW IS THIS REPRESENTED? llvm-gcc doesn't represent '...'!
Douglas Gregor4fa58902009-02-26 23:50:07 +0000229 if (const FunctionProtoType *FTP = dyn_cast<FunctionProtoType>(Ty)) {
Chris Lattner562ce0a2008-11-10 06:08:34 +0000230 for (unsigned i = 0, e = FTP->getNumArgs(); i != e; ++i)
231 EltTys.push_back(getOrCreateType(FTP->getArgType(i), Unit));
232 } else {
233 // FIXME: Handle () case in C. llvm-gcc doesn't do it either.
Sanjiv Gupta93eb8252008-05-25 05:15:42 +0000234 }
235
Chris Lattner562ce0a2008-11-10 06:08:34 +0000236 llvm::DIArray EltTypeArray =
237 DebugFactory.GetOrCreateArray(&EltTys[0], EltTys.size());
238
239 return DebugFactory.CreateCompositeType(llvm::dwarf::DW_TAG_subroutine_type,
240 Unit, "", llvm::DICompileUnit(),
241 0, 0, 0, 0, 0,
242 llvm::DIType(), EltTypeArray);
Sanjiv Gupta93eb8252008-05-25 05:15:42 +0000243}
244
Devang Patela4cc0c42009-02-25 01:36:11 +0000245/// CreateType - get structure or union type.
Chris Lattner562ce0a2008-11-10 06:08:34 +0000246llvm::DIType CGDebugInfo::CreateType(const RecordType *Ty,
247 llvm::DICompileUnit Unit) {
Douglas Gregor640a04b2008-12-11 17:59:21 +0000248 RecordDecl *Decl = Ty->getDecl();
Sanjiv Gupta0f805bd2008-06-07 04:46:53 +0000249
Chris Lattner562ce0a2008-11-10 06:08:34 +0000250 unsigned Tag;
251 if (Decl->isStruct())
252 Tag = llvm::dwarf::DW_TAG_structure_type;
253 else if (Decl->isUnion())
254 Tag = llvm::dwarf::DW_TAG_union_type;
255 else {
256 assert(Decl->isClass() && "Unknown RecordType!");
257 Tag = llvm::dwarf::DW_TAG_class_type;
Sanjiv Gupta0f805bd2008-06-07 04:46:53 +0000258 }
259
Sanjiv Guptab1e662e2008-06-09 10:47:41 +0000260 SourceManager &SM = M->getContext().getSourceManager();
Sanjiv Guptab1e662e2008-06-09 10:47:41 +0000261
Chris Lattner562ce0a2008-11-10 06:08:34 +0000262 // Get overall information about the record type for the debug info.
Chris Lattnerd120b9e2008-11-24 03:54:41 +0000263 std::string Name = Decl->getNameAsString();
Sanjiv Guptab1e662e2008-06-09 10:47:41 +0000264
Chris Lattner562ce0a2008-11-10 06:08:34 +0000265 llvm::DICompileUnit DefUnit = getOrCreateCompileUnit(Decl->getLocation());
Devang Patel9b3f5f72009-04-17 21:35:15 +0000266 PresumedLoc PLoc = SM.getPresumedLoc(Decl->getLocation());
267 unsigned Line = PLoc.isInvalid() ? 0 : PLoc.getLine();
Chris Lattner562ce0a2008-11-10 06:08:34 +0000268
269 // Records and classes and unions can all be recursive. To handle them, we
270 // first generate a debug descriptor for the struct as a forward declaration.
271 // Then (if it is a definition) we go through and get debug info for all of
272 // its members. Finally, we create a descriptor for the complete type (which
273 // may refer to the forward decl if the struct is recursive) and replace all
274 // uses of the forward declaration with the final definition.
275 llvm::DIType FwdDecl =
276 DebugFactory.CreateCompositeType(Tag, Unit, Name, DefUnit, Line, 0, 0, 0, 0,
277 llvm::DIType(), llvm::DIArray());
278
279 // If this is just a forward declaration, return it.
280 if (!Decl->getDefinition(M->getContext()))
281 return FwdDecl;
Sanjiv Guptab1e662e2008-06-09 10:47:41 +0000282
Chris Lattner562ce0a2008-11-10 06:08:34 +0000283 // Otherwise, insert it into the TypeCache so that recursive uses will find
284 // it.
285 TypeCache[QualType(Ty, 0).getAsOpaquePtr()] = FwdDecl;
286
287 // Convert all the elements.
288 llvm::SmallVector<llvm::DIDescriptor, 16> EltTys;
289
290 const ASTRecordLayout &RL = M->getContext().getASTRecordLayout(Decl);
291
292 unsigned FieldNo = 0;
Douglas Gregorc55b0b02009-04-09 21:40:53 +0000293 for (RecordDecl::field_iterator I = Decl->field_begin(M->getContext()),
294 E = Decl->field_end(M->getContext());
Douglas Gregor640a04b2008-12-11 17:59:21 +0000295 I != E; ++I, ++FieldNo) {
Chris Lattner562ce0a2008-11-10 06:08:34 +0000296 FieldDecl *Field = *I;
297 llvm::DIType FieldTy = getOrCreateType(Field->getType(), Unit);
Chris Lattnerd120b9e2008-11-24 03:54:41 +0000298
299 std::string FieldName = Field->getNameAsString();
Chris Lattner562ce0a2008-11-10 06:08:34 +0000300
301 // Get the location for the field.
302 SourceLocation FieldDefLoc = Field->getLocation();
303 llvm::DICompileUnit FieldDefUnit = getOrCreateCompileUnit(FieldDefLoc);
Devang Patel9b3f5f72009-04-17 21:35:15 +0000304 PresumedLoc PLoc = SM.getPresumedLoc(FieldDefLoc);
305 unsigned FieldLine = PLoc.isInvalid() ? 0 : PLoc.getLine();
306
Devang Patelb3a08ba2009-03-16 23:47:53 +0000307
308 QualType FType = Field->getType();
309 uint64_t FieldSize = 0;
310 unsigned FieldAlign = 0;
311 if (!FType->isIncompleteArrayType()) {
Chris Lattner562ce0a2008-11-10 06:08:34 +0000312
Devang Patelb3a08ba2009-03-16 23:47:53 +0000313 // Bit size, align and offset of the type.
314 FieldSize = M->getContext().getTypeSize(FType);
315 Expr *BitWidth = Field->getBitWidth();
316 if (BitWidth)
317 FieldSize =
318 BitWidth->getIntegerConstantExprValue(M->getContext()).getZExtValue();
319
320 FieldAlign = M->getContext().getTypeAlign(FType);
321 }
322
Chris Lattner562ce0a2008-11-10 06:08:34 +0000323 uint64_t FieldOffset = RL.getFieldOffset(FieldNo);
324
325 // Create a DW_TAG_member node to remember the offset of this field in the
326 // struct. FIXME: This is an absolutely insane way to capture this
327 // information. When we gut debug info, this should be fixed.
328 FieldTy = DebugFactory.CreateDerivedType(llvm::dwarf::DW_TAG_member, Unit,
329 FieldName, FieldDefUnit,
330 FieldLine, FieldSize, FieldAlign,
331 FieldOffset, 0, FieldTy);
Chris Lattner562ce0a2008-11-10 06:08:34 +0000332 EltTys.push_back(FieldTy);
333 }
334
335 llvm::DIArray Elements =
336 DebugFactory.GetOrCreateArray(&EltTys[0], EltTys.size());
337
338 // Bit size, align and offset of the type.
339 uint64_t Size = M->getContext().getTypeSize(Ty);
340 uint64_t Align = M->getContext().getTypeAlign(Ty);
341
342 llvm::DIType RealDecl =
343 DebugFactory.CreateCompositeType(Tag, Unit, Name, DefUnit, Line, Size,
344 Align, 0, 0, llvm::DIType(), Elements);
345
346 // Now that we have a real decl for the struct, replace anything using the
347 // old decl with the new one. This will recursively update the debug info.
348 FwdDecl.getGV()->replaceAllUsesWith(RealDecl.getGV());
349 FwdDecl.getGV()->eraseFromParent();
350
351 return RealDecl;
352}
353
Devang Patel3fc13e32009-02-26 21:10:26 +0000354/// CreateType - get objective-c interface type.
355llvm::DIType CGDebugInfo::CreateType(const ObjCInterfaceType *Ty,
356 llvm::DICompileUnit Unit) {
357 ObjCInterfaceDecl *Decl = Ty->getDecl();
358
359 unsigned Tag = llvm::dwarf::DW_TAG_structure_type;
360 SourceManager &SM = M->getContext().getSourceManager();
361
362 // Get overall information about the record type for the debug info.
363 std::string Name = Decl->getNameAsString();
364
365 llvm::DICompileUnit DefUnit = getOrCreateCompileUnit(Decl->getLocation());
Devang Patel9b3f5f72009-04-17 21:35:15 +0000366 PresumedLoc PLoc = SM.getPresumedLoc(Decl->getLocation());
367 unsigned Line = PLoc.isInvalid() ? 0 : PLoc.getLine();
368
Devang Patel3fc13e32009-02-26 21:10:26 +0000369
370 // To handle recursive interface, we
371 // first generate a debug descriptor for the struct as a forward declaration.
372 // Then (if it is a definition) we go through and get debug info for all of
373 // its members. Finally, we create a descriptor for the complete type (which
374 // may refer to the forward decl if the struct is recursive) and replace all
375 // uses of the forward declaration with the final definition.
376 llvm::DIType FwdDecl =
377 DebugFactory.CreateCompositeType(Tag, Unit, Name, DefUnit, Line, 0, 0, 0, 0,
378 llvm::DIType(), llvm::DIArray());
379
380 // If this is just a forward declaration, return it.
381 if (Decl->isForwardDecl())
382 return FwdDecl;
383
384 // Otherwise, insert it into the TypeCache so that recursive uses will find
385 // it.
386 TypeCache[QualType(Ty, 0).getAsOpaquePtr()] = FwdDecl;
387
388 // Convert all the elements.
389 llvm::SmallVector<llvm::DIDescriptor, 16> EltTys;
390
Devang Patele6178df2009-03-10 21:30:26 +0000391 ObjCInterfaceDecl *SClass = Decl->getSuperClass();
392 if (SClass) {
393 llvm::DIType SClassTy =
394 getOrCreateType(M->getContext().getObjCInterfaceType(SClass), Unit);
395 llvm::DIType InhTag =
396 DebugFactory.CreateDerivedType(llvm::dwarf::DW_TAG_inheritance,
397 Unit, "", Unit, 0, 0, 0,
398 0 /* offset */, 0, SClassTy);
399 EltTys.push_back(InhTag);
400 }
401
Devang Patel3fc13e32009-02-26 21:10:26 +0000402 const ASTRecordLayout &RL = M->getContext().getASTObjCInterfaceLayout(Decl);
403
404 unsigned FieldNo = 0;
405 for (ObjCInterfaceDecl::ivar_iterator I = Decl->ivar_begin(),
406 E = Decl->ivar_end(); I != E; ++I, ++FieldNo) {
407 ObjCIvarDecl *Field = *I;
408 llvm::DIType FieldTy = getOrCreateType(Field->getType(), Unit);
409
410 std::string FieldName = Field->getNameAsString();
411
412 // Get the location for the field.
413 SourceLocation FieldDefLoc = Field->getLocation();
414 llvm::DICompileUnit FieldDefUnit = getOrCreateCompileUnit(FieldDefLoc);
Devang Patel9b3f5f72009-04-17 21:35:15 +0000415 PresumedLoc PLoc = SM.getPresumedLoc(FieldDefLoc);
416 unsigned FieldLine = PLoc.isInvalid() ? 0 : PLoc.getLine();
417
Devang Patel806af7d2009-03-20 18:24:39 +0000418
419 QualType FType = Field->getType();
420 uint64_t FieldSize = 0;
421 unsigned FieldAlign = 0;
Devang Patel000ee2f2009-03-19 00:23:53 +0000422
Devang Patel806af7d2009-03-20 18:24:39 +0000423 if (!FType->isIncompleteArrayType()) {
424
425 // Bit size, align and offset of the type.
426 FieldSize = M->getContext().getTypeSize(FType);
427 Expr *BitWidth = Field->getBitWidth();
428 if (BitWidth)
429 FieldSize =
430 BitWidth->getIntegerConstantExprValue(M->getContext()).getZExtValue();
431
432 FieldAlign = M->getContext().getTypeAlign(FType);
433 }
434
435 uint64_t FieldOffset = RL.getFieldOffset(FieldNo);
436
Devang Patel000ee2f2009-03-19 00:23:53 +0000437 unsigned Flags = 0;
438 if (Field->getAccessControl() == ObjCIvarDecl::Protected)
439 Flags = llvm::DIType::FlagProtected;
440 else if (Field->getAccessControl() == ObjCIvarDecl::Private)
441 Flags = llvm::DIType::FlagPrivate;
442
Devang Patel3fc13e32009-02-26 21:10:26 +0000443 // Create a DW_TAG_member node to remember the offset of this field in the
444 // struct. FIXME: This is an absolutely insane way to capture this
445 // information. When we gut debug info, this should be fixed.
446 FieldTy = DebugFactory.CreateDerivedType(llvm::dwarf::DW_TAG_member, Unit,
447 FieldName, FieldDefUnit,
448 FieldLine, FieldSize, FieldAlign,
Devang Patel000ee2f2009-03-19 00:23:53 +0000449 FieldOffset, Flags, FieldTy);
Devang Patel3fc13e32009-02-26 21:10:26 +0000450 EltTys.push_back(FieldTy);
451 }
452
453 llvm::DIArray Elements =
454 DebugFactory.GetOrCreateArray(&EltTys[0], EltTys.size());
455
456 // Bit size, align and offset of the type.
457 uint64_t Size = M->getContext().getTypeSize(Ty);
458 uint64_t Align = M->getContext().getTypeAlign(Ty);
459
460 llvm::DIType RealDecl =
461 DebugFactory.CreateCompositeType(Tag, Unit, Name, DefUnit, Line, Size,
462 Align, 0, 0, llvm::DIType(), Elements);
463
464 // Now that we have a real decl for the struct, replace anything using the
465 // old decl with the new one. This will recursively update the debug info.
466 FwdDecl.getGV()->replaceAllUsesWith(RealDecl.getGV());
467 FwdDecl.getGV()->eraseFromParent();
468
469 return RealDecl;
470}
471
Chris Lattner562ce0a2008-11-10 06:08:34 +0000472llvm::DIType CGDebugInfo::CreateType(const EnumType *Ty,
473 llvm::DICompileUnit Unit) {
474 EnumDecl *Decl = Ty->getDecl();
475
476 llvm::SmallVector<llvm::DIDescriptor, 32> Enumerators;
477
478 // Create DIEnumerator elements for each enumerator.
Douglas Gregorc55b0b02009-04-09 21:40:53 +0000479 for (EnumDecl::enumerator_iterator
480 Enum = Decl->enumerator_begin(M->getContext()),
481 EnumEnd = Decl->enumerator_end(M->getContext());
Douglas Gregor8acb7272008-12-11 16:49:14 +0000482 Enum != EnumEnd; ++Enum) {
483 Enumerators.push_back(DebugFactory.CreateEnumerator(Enum->getNameAsString(),
484 Enum->getInitVal().getZExtValue()));
Chris Lattner562ce0a2008-11-10 06:08:34 +0000485 }
486
487 // Return a CompositeType for the enum itself.
488 llvm::DIArray EltArray =
489 DebugFactory.GetOrCreateArray(&Enumerators[0], Enumerators.size());
490
Chris Lattnerd120b9e2008-11-24 03:54:41 +0000491 std::string EnumName = Decl->getNameAsString();
Chris Lattner562ce0a2008-11-10 06:08:34 +0000492 SourceLocation DefLoc = Decl->getLocation();
493 llvm::DICompileUnit DefUnit = getOrCreateCompileUnit(DefLoc);
494 SourceManager &SM = M->getContext().getSourceManager();
Devang Patel9b3f5f72009-04-17 21:35:15 +0000495 PresumedLoc PLoc = SM.getPresumedLoc(DefLoc);
496 unsigned Line = PLoc.isInvalid() ? 0 : PLoc.getLine();
497
Chris Lattner562ce0a2008-11-10 06:08:34 +0000498
499 // Size and align of the type.
500 uint64_t Size = M->getContext().getTypeSize(Ty);
Chris Lattner18c8dc02009-01-16 07:36:28 +0000501 unsigned Align = M->getContext().getTypeAlign(Ty);
Chris Lattner562ce0a2008-11-10 06:08:34 +0000502
503 return DebugFactory.CreateCompositeType(llvm::dwarf::DW_TAG_enumeration_type,
504 Unit, EnumName, DefUnit, Line,
505 Size, Align, 0, 0,
506 llvm::DIType(), EltArray);
507}
508
509llvm::DIType CGDebugInfo::CreateType(const TagType *Ty,
510 llvm::DICompileUnit Unit) {
511 if (const RecordType *RT = dyn_cast<RecordType>(Ty))
512 return CreateType(RT, Unit);
513 else if (const EnumType *ET = dyn_cast<EnumType>(Ty))
514 return CreateType(ET, Unit);
515
516 return llvm::DIType();
517}
518
519llvm::DIType CGDebugInfo::CreateType(const ArrayType *Ty,
520 llvm::DICompileUnit Unit) {
Anders Carlsson86b360e2009-01-05 01:23:29 +0000521 uint64_t Size;
522 uint64_t Align;
523
524
Nuno Lopes15bc9c32009-01-28 00:35:17 +0000525 // FIXME: make getTypeAlign() aware of VLAs and incomplete array types
Anders Carlsson86b360e2009-01-05 01:23:29 +0000526 if (const VariableArrayType *VAT = dyn_cast<VariableArrayType>(Ty)) {
Anders Carlsson86b360e2009-01-05 01:23:29 +0000527 Size = 0;
528 Align =
Nuno Lopes15bc9c32009-01-28 00:35:17 +0000529 M->getContext().getTypeAlign(M->getContext().getBaseElementType(VAT));
530 } else if (Ty->isIncompleteArrayType()) {
531 Size = 0;
532 Align = M->getContext().getTypeAlign(Ty->getElementType());
Anders Carlsson86b360e2009-01-05 01:23:29 +0000533 } else {
534 // Size and align of the whole array, not the element type.
535 Size = M->getContext().getTypeSize(Ty);
536 Align = M->getContext().getTypeAlign(Ty);
537 }
Chris Lattner562ce0a2008-11-10 06:08:34 +0000538
539 // Add the dimensions of the array. FIXME: This loses CV qualifiers from
540 // interior arrays, do we care? Why aren't nested arrays represented the
541 // obvious/recursive way?
542 llvm::SmallVector<llvm::DIDescriptor, 8> Subscripts;
543 QualType EltTy(Ty, 0);
544 while ((Ty = dyn_cast<ArrayType>(EltTy))) {
Sanjiv Guptab1e662e2008-06-09 10:47:41 +0000545 uint64_t Upper = 0;
Chris Lattner562ce0a2008-11-10 06:08:34 +0000546 if (const ConstantArrayType *CAT = dyn_cast<ConstantArrayType>(Ty))
547 Upper = CAT->getSize().getZExtValue() - 1;
548 // FIXME: Verify this is right for VLAs.
549 Subscripts.push_back(DebugFactory.GetOrCreateSubrange(0, Upper));
550 EltTy = Ty->getElementType();
Sanjiv Guptab1e662e2008-06-09 10:47:41 +0000551 }
Sanjiv Gupta93eb8252008-05-25 05:15:42 +0000552
Chris Lattner562ce0a2008-11-10 06:08:34 +0000553 llvm::DIArray SubscriptArray =
554 DebugFactory.GetOrCreateArray(&Subscripts[0], Subscripts.size());
555
556 return DebugFactory.CreateCompositeType(llvm::dwarf::DW_TAG_array_type,
557 Unit, "", llvm::DICompileUnit(),
558 0, Size, Align, 0, 0,
559 getOrCreateType(EltTy, Unit),
560 SubscriptArray);
561}
562
563
Sanjiv Gupta93eb8252008-05-25 05:15:42 +0000564/// getOrCreateType - Get the type from the cache or create a new
565/// one if necessary.
Chris Lattner562ce0a2008-11-10 06:08:34 +0000566llvm::DIType CGDebugInfo::getOrCreateType(QualType Ty,
567 llvm::DICompileUnit Unit) {
568 if (Ty.isNull())
569 return llvm::DIType();
570
Sanjiv Gupta93eb8252008-05-25 05:15:42 +0000571 // Check to see if the compile unit already has created this type.
Chris Lattner562ce0a2008-11-10 06:08:34 +0000572 llvm::DIType &Slot = TypeCache[Ty.getAsOpaquePtr()];
573 if (!Slot.isNull()) return Slot;
Sanjiv Gupta93eb8252008-05-25 05:15:42 +0000574
Chris Lattner562ce0a2008-11-10 06:08:34 +0000575 // Handle CVR qualifiers, which recursively handles what they refer to.
576 if (Ty.getCVRQualifiers())
577 return Slot = CreateCVRType(Ty, Unit);
Sanjiv Gupta93eb8252008-05-25 05:15:42 +0000578
579 // Work out details of type.
Chris Lattner562ce0a2008-11-10 06:08:34 +0000580 switch (Ty->getTypeClass()) {
Douglas Gregor4fa58902009-02-26 23:50:07 +0000581#define TYPE(Class, Base)
582#define ABSTRACT_TYPE(Class, Base)
583#define NON_CANONICAL_TYPE(Class, Base)
584#define DEPENDENT_TYPE(Class, Base) case Type::Class:
585#include "clang/AST/TypeNodes.def"
586 assert(false && "Dependent types cannot show up in debug information");
587
Sebastian Redlce6fff02009-03-16 23:22:08 +0000588 case Type::LValueReference:
589 case Type::RValueReference:
Chris Lattner562ce0a2008-11-10 06:08:34 +0000590 case Type::Vector:
591 case Type::ExtVector:
Fariborz Jahanianb60352a2009-02-17 18:27:45 +0000592 case Type::ExtQual:
Eli Friedman673e91b2009-02-27 23:15:07 +0000593 case Type::FixedWidthInt:
594 case Type::BlockPointer:
595 case Type::MemberPointer:
Douglas Gregordd13e842009-03-30 22:58:21 +0000596 case Type::TemplateSpecialization:
Douglas Gregor734b4ba2009-03-19 00:18:19 +0000597 case Type::QualifiedName:
Eli Friedman673e91b2009-02-27 23:15:07 +0000598 // Unsupported types
Chris Lattner562ce0a2008-11-10 06:08:34 +0000599 return llvm::DIType();
Chris Lattnereee8c7c2009-04-22 06:58:56 +0000600 case Type::ObjCQualifiedId: // Encode id<p> in debug info just like id.
601 return Slot = getOrCreateType(M->getContext().getObjCIdType(), Unit);
602
603 case Type::ObjCQualifiedInterface: // Drop protocols from interface.
Devang Patel5a1ff3b2009-03-02 17:58:28 +0000604 case Type::ObjCInterface:
Chris Lattnereee8c7c2009-04-22 06:58:56 +0000605 return Slot = CreateType(cast<ObjCInterfaceType>(Ty), Unit);
606 case Type::Builtin: return Slot = CreateType(cast<BuiltinType>(Ty), Unit);
Chris Lattner016d55e2009-04-23 06:13:01 +0000607 case Type::Complex: return Slot = CreateType(cast<ComplexType>(Ty), Unit);
Chris Lattnereee8c7c2009-04-22 06:58:56 +0000608 case Type::Pointer: return Slot = CreateType(cast<PointerType>(Ty), Unit);
609 case Type::Typedef: return Slot = CreateType(cast<TypedefType>(Ty), Unit);
Douglas Gregor4fa58902009-02-26 23:50:07 +0000610 case Type::Record:
Douglas Gregor4fa58902009-02-26 23:50:07 +0000611 case Type::Enum:
Chris Lattnereee8c7c2009-04-22 06:58:56 +0000612 return Slot = CreateType(cast<TagType>(Ty), Unit);
Chris Lattner562ce0a2008-11-10 06:08:34 +0000613 case Type::FunctionProto:
614 case Type::FunctionNoProto:
Chris Lattnere3afa462008-11-11 07:01:36 +0000615 return Slot = CreateType(cast<FunctionType>(Ty), Unit);
Chris Lattner562ce0a2008-11-10 06:08:34 +0000616
617 case Type::ConstantArray:
618 case Type::VariableArray:
619 case Type::IncompleteArray:
Chris Lattnere3afa462008-11-11 07:01:36 +0000620 return Slot = CreateType(cast<ArrayType>(Ty), Unit);
Douglas Gregor4fa58902009-02-26 23:50:07 +0000621 case Type::TypeOfExpr:
622 return Slot = getOrCreateType(cast<TypeOfExprType>(Ty)->getUnderlyingExpr()
Chris Lattnere3afa462008-11-11 07:01:36 +0000623 ->getType(), Unit);
Douglas Gregor4fa58902009-02-26 23:50:07 +0000624 case Type::TypeOf:
Chris Lattnere3afa462008-11-11 07:01:36 +0000625 return Slot = getOrCreateType(cast<TypeOfType>(Ty)->getUnderlyingType(),
626 Unit);
Sanjiv Gupta93eb8252008-05-25 05:15:42 +0000627 }
Chris Lattner562ce0a2008-11-10 06:08:34 +0000628
Sanjiv Gupta93eb8252008-05-25 05:15:42 +0000629 return Slot;
630}
631
632/// EmitFunctionStart - Constructs the debug code for entering a function -
633/// "llvm.dbg.func.start.".
Chris Lattner562ce0a2008-11-10 06:08:34 +0000634void CGDebugInfo::EmitFunctionStart(const char *Name, QualType ReturnType,
Sanjiv Gupta93eb8252008-05-25 05:15:42 +0000635 llvm::Function *Fn,
Chris Lattner562ce0a2008-11-10 06:08:34 +0000636 CGBuilderTy &Builder) {
637 // FIXME: Why is this using CurLoc???
638 llvm::DICompileUnit Unit = getOrCreateCompileUnit(CurLoc);
Sanjiv Gupta93eb8252008-05-25 05:15:42 +0000639 SourceManager &SM = M->getContext().getSourceManager();
Devang Patel7f9e2092009-04-08 19:47:04 +0000640 unsigned LineNo = SM.getPresumedLoc(CurLoc).getLine();
Chris Lattner562ce0a2008-11-10 06:08:34 +0000641
642 llvm::DISubprogram SP =
643 DebugFactory.CreateSubprogram(Unit, Name, Name, "", Unit, LineNo,
644 getOrCreateType(ReturnType, Unit),
645 Fn->hasInternalLinkage(), true/*definition*/);
646
647 DebugFactory.InsertSubprogramStart(SP, Builder.GetInsertBlock());
648
Sanjiv Gupta93eb8252008-05-25 05:15:42 +0000649 // Push function on region stack.
Chris Lattner562ce0a2008-11-10 06:08:34 +0000650 RegionStack.push_back(SP);
Sanjiv Gupta93eb8252008-05-25 05:15:42 +0000651}
652
653
Chris Lattner562ce0a2008-11-10 06:08:34 +0000654void CGDebugInfo::EmitStopPoint(llvm::Function *Fn, CGBuilderTy &Builder) {
Sanjiv Gupta93eb8252008-05-25 05:15:42 +0000655 if (CurLoc.isInvalid() || CurLoc.isMacroID()) return;
656
Sanjiv Gupta40e56a12008-05-08 08:54:20 +0000657 // Don't bother if things are the same as last time.
658 SourceManager &SM = M->getContext().getSourceManager();
659 if (CurLoc == PrevLoc
Chris Lattner2d89c562009-02-04 01:06:56 +0000660 || (SM.getInstantiationLineNumber(CurLoc) ==
661 SM.getInstantiationLineNumber(PrevLoc)
Sanjiv Gupta40e56a12008-05-08 08:54:20 +0000662 && SM.isFromSameFile(CurLoc, PrevLoc)))
663 return;
Sanjiv Gupta40e56a12008-05-08 08:54:20 +0000664
665 // Update last state.
666 PrevLoc = CurLoc;
667
668 // Get the appropriate compile unit.
Chris Lattner562ce0a2008-11-10 06:08:34 +0000669 llvm::DICompileUnit Unit = getOrCreateCompileUnit(CurLoc);
Devang Patel7f9e2092009-04-08 19:47:04 +0000670 PresumedLoc PLoc = SM.getPresumedLoc(CurLoc);
671 DebugFactory.InsertStopPoint(Unit, PLoc.getLine(), PLoc.getColumn(),
Chris Lattner562ce0a2008-11-10 06:08:34 +0000672 Builder.GetInsertBlock());
Sanjiv Gupta40e56a12008-05-08 08:54:20 +0000673}
674
675/// EmitRegionStart- Constructs the debug code for entering a declarative
676/// region - "llvm.dbg.region.start.".
Chris Lattner562ce0a2008-11-10 06:08:34 +0000677void CGDebugInfo::EmitRegionStart(llvm::Function *Fn, CGBuilderTy &Builder) {
678 llvm::DIDescriptor D;
Daniel Dunbar1257f8c2008-10-17 01:07:56 +0000679 if (!RegionStack.empty())
Chris Lattner562ce0a2008-11-10 06:08:34 +0000680 D = RegionStack.back();
681 D = DebugFactory.CreateBlock(D);
682 RegionStack.push_back(D);
683 DebugFactory.InsertRegionStart(D, Builder.GetInsertBlock());
Sanjiv Gupta40e56a12008-05-08 08:54:20 +0000684}
685
686/// EmitRegionEnd - Constructs the debug code for exiting a declarative
687/// region - "llvm.dbg.region.end."
Chris Lattner562ce0a2008-11-10 06:08:34 +0000688void CGDebugInfo::EmitRegionEnd(llvm::Function *Fn, CGBuilderTy &Builder) {
Daniel Dunbar1257f8c2008-10-17 01:07:56 +0000689 assert(!RegionStack.empty() && "Region stack mismatch, stack empty!");
690
Sanjiv Gupta40e56a12008-05-08 08:54:20 +0000691 // Provide an region stop point.
692 EmitStopPoint(Fn, Builder);
693
Chris Lattner562ce0a2008-11-10 06:08:34 +0000694 DebugFactory.InsertRegionEnd(RegionStack.back(), Builder.GetInsertBlock());
Sanjiv Gupta93eb8252008-05-25 05:15:42 +0000695 RegionStack.pop_back();
Sanjiv Gupta40e56a12008-05-08 08:54:20 +0000696}
697
Sanjiv Guptab2a10452008-05-30 10:30:31 +0000698/// EmitDeclare - Emit local variable declaration debug info.
Chris Lattner562ce0a2008-11-10 06:08:34 +0000699void CGDebugInfo::EmitDeclare(const VarDecl *Decl, unsigned Tag,
700 llvm::Value *Storage, CGBuilderTy &Builder) {
Daniel Dunbar1257f8c2008-10-17 01:07:56 +0000701 assert(!RegionStack.empty() && "Region stack mismatch, stack empty!");
702
Devang Patelbcded192009-03-27 23:16:32 +0000703 // Do not emit variable debug information while generating optimized code.
704 // The llvm optimizer and code generator are not yet ready to support
705 // optimized code debugging.
706 const CompileOptions &CO = M->getCompileOpts();
707 if (CO.OptimizationLevel)
708 return;
709
Chris Lattner562ce0a2008-11-10 06:08:34 +0000710 // Get location information.
Sanjiv Guptab2a10452008-05-30 10:30:31 +0000711 SourceManager &SM = M->getContext().getSourceManager();
Devang Patel9b3f5f72009-04-17 21:35:15 +0000712 PresumedLoc PLoc = SM.getPresumedLoc(Decl->getLocation());
713 unsigned Line = PLoc.isInvalid() ? 0 : PLoc.getLine();
Chris Lattner562ce0a2008-11-10 06:08:34 +0000714 llvm::DICompileUnit Unit = getOrCreateCompileUnit(Decl->getLocation());
715
716 // Create the descriptor for the variable.
717 llvm::DIVariable D =
Chris Lattner271d4c22008-11-24 05:29:24 +0000718 DebugFactory.CreateVariable(Tag, RegionStack.back(),Decl->getNameAsString(),
Chris Lattner562ce0a2008-11-10 06:08:34 +0000719 Unit, Line,
720 getOrCreateType(Decl->getType(), Unit));
721 // Insert an llvm.dbg.declare into the current block.
722 DebugFactory.InsertDeclare(Storage, D, Builder.GetInsertBlock());
Sanjiv Guptab2a10452008-05-30 10:30:31 +0000723}
724
Chris Lattner562ce0a2008-11-10 06:08:34 +0000725void CGDebugInfo::EmitDeclareOfAutoVariable(const VarDecl *Decl,
726 llvm::Value *Storage,
727 CGBuilderTy &Builder) {
728 EmitDeclare(Decl, llvm::dwarf::DW_TAG_auto_variable, Storage, Builder);
729}
730
731/// EmitDeclareOfArgVariable - Emit call to llvm.dbg.declare for an argument
732/// variable declaration.
733void CGDebugInfo::EmitDeclareOfArgVariable(const VarDecl *Decl, llvm::Value *AI,
734 CGBuilderTy &Builder) {
735 EmitDeclare(Decl, llvm::dwarf::DW_TAG_arg_variable, AI, Builder);
736}
737
738
739
Sanjiv Gupta54d97542008-06-05 08:59:10 +0000740/// EmitGlobalVariable - Emit information about a global variable.
Chris Lattner562ce0a2008-11-10 06:08:34 +0000741void CGDebugInfo::EmitGlobalVariable(llvm::GlobalVariable *Var,
742 const VarDecl *Decl) {
Devang Patelbcded192009-03-27 23:16:32 +0000743
744 // Do not emit variable debug information while generating optimized code.
745 // The llvm optimizer and code generator are not yet ready to support
746 // optimized code debugging.
747 const CompileOptions &CO = M->getCompileOpts();
748 if (CO.OptimizationLevel)
749 return;
750
Sanjiv Gupta54d97542008-06-05 08:59:10 +0000751 // Create global variable debug descriptor.
Chris Lattner562ce0a2008-11-10 06:08:34 +0000752 llvm::DICompileUnit Unit = getOrCreateCompileUnit(Decl->getLocation());
Sanjiv Gupta54d97542008-06-05 08:59:10 +0000753 SourceManager &SM = M->getContext().getSourceManager();
Devang Patel9b3f5f72009-04-17 21:35:15 +0000754 PresumedLoc PLoc = SM.getPresumedLoc(Decl->getLocation());
755 unsigned LineNo = PLoc.isInvalid() ? 0 : PLoc.getLine();
Chris Lattnerd120b9e2008-11-24 03:54:41 +0000756
757 std::string Name = Decl->getNameAsString();
Anders Carlsson123485c2008-11-26 17:40:42 +0000758
759 QualType T = Decl->getType();
760 if (T->isIncompleteArrayType()) {
761
762 // CodeGen turns int[] into int[1] so we'll do the same here.
763 llvm::APSInt ConstVal(32);
764
765 ConstVal = 1;
766 QualType ET = M->getContext().getAsArrayType(T)->getElementType();
767
768 T = M->getContext().getConstantArrayType(ET, ConstVal,
769 ArrayType::Normal, 0);
770 }
771
Chris Lattner562ce0a2008-11-10 06:08:34 +0000772 DebugFactory.CreateGlobalVariable(Unit, Name, Name, "", Unit, LineNo,
Anders Carlsson123485c2008-11-26 17:40:42 +0000773 getOrCreateType(T, Unit),
Chris Lattner562ce0a2008-11-10 06:08:34 +0000774 Var->hasInternalLinkage(),
775 true/*definition*/, Var);
Sanjiv Gupta54d97542008-06-05 08:59:10 +0000776}
777
Devang Patel3fc13e32009-02-26 21:10:26 +0000778/// EmitGlobalVariable - Emit information about an objective-c interface.
779void CGDebugInfo::EmitGlobalVariable(llvm::GlobalVariable *Var,
780 ObjCInterfaceDecl *Decl) {
781 // Create global variable debug descriptor.
782 llvm::DICompileUnit Unit = getOrCreateCompileUnit(Decl->getLocation());
783 SourceManager &SM = M->getContext().getSourceManager();
Devang Patel9b3f5f72009-04-17 21:35:15 +0000784 PresumedLoc PLoc = SM.getPresumedLoc(Decl->getLocation());
785 unsigned LineNo = PLoc.isInvalid() ? 0 : PLoc.getLine();
Devang Patel3fc13e32009-02-26 21:10:26 +0000786
787 std::string Name = Decl->getNameAsString();
788
Chris Lattner46ee0f32009-04-01 06:23:52 +0000789 QualType T = M->getContext().getObjCInterfaceType(Decl);
Devang Patel3fc13e32009-02-26 21:10:26 +0000790 if (T->isIncompleteArrayType()) {
791
792 // CodeGen turns int[] into int[1] so we'll do the same here.
793 llvm::APSInt ConstVal(32);
794
795 ConstVal = 1;
796 QualType ET = M->getContext().getAsArrayType(T)->getElementType();
797
798 T = M->getContext().getConstantArrayType(ET, ConstVal,
799 ArrayType::Normal, 0);
800 }
801
802 DebugFactory.CreateGlobalVariable(Unit, Name, Name, "", Unit, LineNo,
803 getOrCreateType(T, Unit),
804 Var->hasInternalLinkage(),
805 true/*definition*/, Var);
806}
807