blob: a652ede1c1f2c348bcbce193ca9be86c365cf43d [file] [log] [blame]
Sanjiv Guptae8b9f5b2008-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 Gupta1c6a38b2008-05-25 05:15:42 +000016#include "clang/AST/ASTContext.h"
Devang Patel9ca36b62009-02-26 21:10:26 +000017#include "clang/AST/DeclObjC.h"
Chris Lattner3cc5c402008-11-11 07:01:36 +000018#include "clang/AST/Expr.h"
Daniel Dunbare91593e2008-08-11 04:54:23 +000019#include "clang/AST/RecordLayout.h"
Sanjiv Gupta1c6a38b2008-05-25 05:15:42 +000020#include "clang/Basic/SourceManager.h"
21#include "clang/Basic/FileManager.h"
Devang Patel07739032009-03-27 23:16:32 +000022#include "clang/Frontend/CompileOptions.h"
Sanjiv Guptae8b9f5b2008-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 Guptae8b9f5b2008-05-08 08:54:20 +000028#include "llvm/ADT/StringExtras.h"
29#include "llvm/ADT/SmallVector.h"
Sanjiv Gupta1c6a38b2008-05-25 05:15:42 +000030#include "llvm/Support/Dwarf.h"
Devang Patel446c6192009-04-17 21:06:59 +000031#include "llvm/System/Path.h"
Sanjiv Gupta1c6a38b2008-05-25 05:15:42 +000032#include "llvm/Target/TargetMachine.h"
Sanjiv Guptae8b9f5b2008-05-08 08:54:20 +000033using namespace clang;
34using namespace clang::CodeGen;
35
36CGDebugInfo::CGDebugInfo(CodeGenModule *m)
Devang Patel87de6492009-04-23 18:09:16 +000037 : M(m), isMainCompileUnitCreated(false), DebugFactory(M->getModule()) {
Sanjiv Guptae8b9f5b2008-05-08 08:54:20 +000038}
39
Chris Lattner9c85ba32008-11-10 06:08:34 +000040CGDebugInfo::~CGDebugInfo() {
Daniel Dunbar66031a52008-10-17 16:15:48 +000041 assert(RegionStack.empty() && "Region stack mismatch, stack not empty!");
Sanjiv Guptae8b9f5b2008-05-08 08:54:20 +000042}
43
Chris Lattner9c85ba32008-11-10 06:08:34 +000044void CGDebugInfo::setLocation(SourceLocation Loc) {
45 if (Loc.isValid())
Chris Lattnerf7cf85b2009-01-16 07:36:28 +000046 CurLoc = M->getContext().getSourceManager().getInstantiationLoc(Loc);
Sanjiv Gupta1c6a38b2008-05-25 05:15:42 +000047}
48
Sanjiv Guptae8b9f5b2008-05-08 08:54:20 +000049/// getOrCreateCompileUnit - Get the compile unit from the cache or create a new
Daniel Dunbar25f51dd2008-10-24 08:38:36 +000050/// one if necessary. This returns null for invalid source locations.
Chris Lattner9c85ba32008-11-10 06:08:34 +000051llvm::DICompileUnit CGDebugInfo::getOrCreateCompileUnit(SourceLocation Loc) {
Devang Patel446c6192009-04-17 21:06:59 +000052 // Get source file information.
53 const char *FileName = "<unknown>";
Devang Patel77820222009-02-24 23:16:03 +000054 SourceManager &SM = M->getContext().getSourceManager();
Chris Lattneradb1a6f2009-04-19 06:50:29 +000055 unsigned FID = 0;
Daniel Dunbar831570c2009-01-22 00:09:25 +000056 if (Loc.isValid()) {
Devang Patel446c6192009-04-17 21:06:59 +000057 PresumedLoc PLoc = SM.getPresumedLoc(Loc);
58 FileName = PLoc.getFilename();
59 FID = PLoc.getIncludeLoc().getRawEncoding();
Daniel Dunbar831570c2009-01-22 00:09:25 +000060 }
61
Sanjiv Guptae8b9f5b2008-05-08 08:54:20 +000062 // See if this compile unit has been used before.
Devang Patel446c6192009-04-17 21:06:59 +000063 llvm::DICompileUnit &Unit = CompileUnitCache[FID];
Chris Lattner9c85ba32008-11-10 06:08:34 +000064 if (!Unit.isNull()) return Unit;
Daniel Dunbarc9abc042009-04-08 05:11:16 +000065
Devang Patel446c6192009-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
Devang Patel87de6492009-04-23 18:09:16 +000074 // See if thie compile unit is representing main source file. Each source
75 // file has corresponding compile unit. There is only one main source
76 // file at a time.
Devang Patel446c6192009-04-17 21:06:59 +000077 bool isMain = false;
78 const LangOptions &LO = M->getLangOptions();
79 const char *MainFileName = LO.getMainFileName();
Devang Patel87de6492009-04-23 18:09:16 +000080 if (isMainCompileUnitCreated == false) {
81 if (MainFileName) {
82 if (!strcmp(AbsFileName.getLast().c_str(), MainFileName))
83 isMain = true;
84 } else {
85 if (Loc.isValid() && SM.isFromMainFile(Loc))
86 isMain = true;
87 }
88 if (isMain)
89 isMainCompileUnitCreated = true;
Devang Patel446c6192009-04-17 21:06:59 +000090 }
Daniel Dunbarc9abc042009-04-08 05:11:16 +000091
Chris Lattner515455a2009-03-25 03:28:08 +000092 unsigned LangTag;
93 if (LO.CPlusPlus) {
94 if (LO.ObjC1)
95 LangTag = llvm::dwarf::DW_LANG_ObjC_plus_plus;
96 else
97 LangTag = llvm::dwarf::DW_LANG_C_plus_plus;
98 } else if (LO.ObjC1) {
Devang Patel8d9aefc2009-03-24 20:35:51 +000099 LangTag = llvm::dwarf::DW_LANG_ObjC;
Chris Lattner515455a2009-03-25 03:28:08 +0000100 } else if (LO.C99) {
Devang Patel8d9aefc2009-03-24 20:35:51 +0000101 LangTag = llvm::dwarf::DW_LANG_C99;
Chris Lattner515455a2009-03-25 03:28:08 +0000102 } else {
103 LangTag = llvm::dwarf::DW_LANG_C89;
104 }
Devang Patel446c6192009-04-17 21:06:59 +0000105
Sanjiv Guptae8b9f5b2008-05-08 08:54:20 +0000106 // Create new compile unit.
Sanjiv Guptae8b9f5b2008-05-08 08:54:20 +0000107 // FIXME: Do not know how to get clang version yet.
Devang Patelc20482b2009-03-19 00:23:53 +0000108 // FIXME: Encode command line options.
109 // FIXME: Encode optimization level.
Devang Patel446c6192009-04-17 21:06:59 +0000110 return Unit = DebugFactory.CreateCompileUnit(LangTag, AbsFileName.getLast(),
111 AbsFileName.getDirname(),
Devang Patel8d9aefc2009-03-24 20:35:51 +0000112 "clang", isMain);
Sanjiv Guptae8b9f5b2008-05-08 08:54:20 +0000113}
114
Devang Patel65e99f22009-02-25 01:36:11 +0000115/// CreateType - Get the Basic type from the cache or create a new
Chris Lattner9c85ba32008-11-10 06:08:34 +0000116/// one if necessary.
117llvm::DIType CGDebugInfo::CreateType(const BuiltinType *BT,
Devang Patel65e99f22009-02-25 01:36:11 +0000118 llvm::DICompileUnit Unit) {
Chris Lattner9c85ba32008-11-10 06:08:34 +0000119 unsigned Encoding = 0;
120 switch (BT->getKind()) {
121 default:
122 case BuiltinType::Void:
123 return llvm::DIType();
124 case BuiltinType::UChar:
125 case BuiltinType::Char_U: Encoding = llvm::dwarf::DW_ATE_unsigned_char; break;
126 case BuiltinType::Char_S:
127 case BuiltinType::SChar: Encoding = llvm::dwarf::DW_ATE_signed_char; break;
128 case BuiltinType::UShort:
129 case BuiltinType::UInt:
130 case BuiltinType::ULong:
131 case BuiltinType::ULongLong: Encoding = llvm::dwarf::DW_ATE_unsigned; break;
132 case BuiltinType::Short:
133 case BuiltinType::Int:
134 case BuiltinType::Long:
135 case BuiltinType::LongLong: Encoding = llvm::dwarf::DW_ATE_signed; break;
136 case BuiltinType::Bool: Encoding = llvm::dwarf::DW_ATE_boolean; break;
137 case BuiltinType::Float:
138 case BuiltinType::Double: Encoding = llvm::dwarf::DW_ATE_float; break;
139 }
140 // Bit size, align and offset of the type.
141 uint64_t Size = M->getContext().getTypeSize(BT);
142 uint64_t Align = M->getContext().getTypeAlign(BT);
143 uint64_t Offset = 0;
144
145 return DebugFactory.CreateBasicType(Unit, BT->getName(), Unit, 0, Size, Align,
146 Offset, /*flags*/ 0, Encoding);
147}
Sanjiv Guptae8b9f5b2008-05-08 08:54:20 +0000148
Chris Lattnerb7003772009-04-23 06:13:01 +0000149llvm::DIType CGDebugInfo::CreateType(const ComplexType *Ty,
150 llvm::DICompileUnit Unit) {
151 // Bit size, align and offset of the type.
152 unsigned Encoding = llvm::dwarf::DW_ATE_complex_float;
153 if (Ty->isComplexIntegerType())
154 Encoding = llvm::dwarf::DW_ATE_lo_user;
155
156 uint64_t Size = M->getContext().getTypeSize(Ty);
157 uint64_t Align = M->getContext().getTypeAlign(Ty);
158 uint64_t Offset = 0;
159
160 return DebugFactory.CreateBasicType(Unit, "complex",
161 Unit, 0, Size, Align,
162 Offset, /*flags*/ 0, Encoding);
163}
164
Sanjiv Guptaf58c27a2008-06-07 04:46:53 +0000165/// getOrCreateCVRType - Get the CVR qualified type from the cache or create
166/// a new one if necessary.
Chris Lattner9c85ba32008-11-10 06:08:34 +0000167llvm::DIType CGDebugInfo::CreateCVRType(QualType Ty, llvm::DICompileUnit Unit) {
168 // We will create one Derived type for one qualifier and recurse to handle any
169 // additional ones.
170 llvm::DIType FromTy;
171 unsigned Tag;
172 if (Ty.isConstQualified()) {
173 Tag = llvm::dwarf::DW_TAG_const_type;
174 Ty.removeConst();
175 FromTy = getOrCreateType(Ty, Unit);
176 } else if (Ty.isVolatileQualified()) {
177 Tag = llvm::dwarf::DW_TAG_volatile_type;
178 Ty.removeVolatile();
179 FromTy = getOrCreateType(Ty, Unit);
180 } else {
181 assert(Ty.isRestrictQualified() && "Unknown type qualifier for debug info");
182 Tag = llvm::dwarf::DW_TAG_restrict_type;
183 Ty.removeRestrict();
184 FromTy = getOrCreateType(Ty, Unit);
Sanjiv Gupta1c6a38b2008-05-25 05:15:42 +0000185 }
Chris Lattner9c85ba32008-11-10 06:08:34 +0000186
Daniel Dunbar3845f862008-10-31 03:54:29 +0000187 // No need to fill in the Name, Line, Size, Alignment, Offset in case of
188 // CVR derived types.
Chris Lattner9c85ba32008-11-10 06:08:34 +0000189 return DebugFactory.CreateDerivedType(Tag, Unit, "", llvm::DICompileUnit(),
190 0, 0, 0, 0, 0, FromTy);
Sanjiv Gupta1c6a38b2008-05-25 05:15:42 +0000191}
192
Chris Lattner9c85ba32008-11-10 06:08:34 +0000193llvm::DIType CGDebugInfo::CreateType(const PointerType *Ty,
194 llvm::DICompileUnit Unit) {
195 llvm::DIType EltTy = getOrCreateType(Ty->getPointeeType(), Unit);
Sanjiv Gupta1c6a38b2008-05-25 05:15:42 +0000196
Sanjiv Gupta1c6a38b2008-05-25 05:15:42 +0000197 // Bit size, align and offset of the type.
Chris Lattner9c85ba32008-11-10 06:08:34 +0000198 uint64_t Size = M->getContext().getTypeSize(Ty);
199 uint64_t Align = M->getContext().getTypeAlign(Ty);
Sanjiv Gupta1c6a38b2008-05-25 05:15:42 +0000200
Chris Lattner9c85ba32008-11-10 06:08:34 +0000201 return DebugFactory.CreateDerivedType(llvm::dwarf::DW_TAG_pointer_type, Unit,
202 "", llvm::DICompileUnit(),
203 0, Size, Align, 0, 0, EltTy);
Sanjiv Gupta1c6a38b2008-05-25 05:15:42 +0000204}
205
Chris Lattner9c85ba32008-11-10 06:08:34 +0000206llvm::DIType CGDebugInfo::CreateType(const TypedefType *Ty,
207 llvm::DICompileUnit Unit) {
208 // Typedefs are derived from some other type. If we have a typedef of a
209 // typedef, make sure to emit the whole chain.
210 llvm::DIType Src = getOrCreateType(Ty->getDecl()->getUnderlyingType(), Unit);
211
212 // We don't set size information, but do specify where the typedef was
213 // declared.
Chris Lattner8ec03f52008-11-24 03:54:41 +0000214 std::string TyName = Ty->getDecl()->getNameAsString();
Chris Lattner9c85ba32008-11-10 06:08:34 +0000215 SourceLocation DefLoc = Ty->getDecl()->getLocation();
216 llvm::DICompileUnit DefUnit = getOrCreateCompileUnit(DefLoc);
Sanjiv Gupta1c6a38b2008-05-25 05:15:42 +0000217
Sanjiv Gupta1c6a38b2008-05-25 05:15:42 +0000218 SourceManager &SM = M->getContext().getSourceManager();
Devang Patel4f6fa232009-04-17 21:35:15 +0000219 PresumedLoc PLoc = SM.getPresumedLoc(DefLoc);
220 unsigned Line = PLoc.isInvalid() ? 0 : PLoc.getLine();
Sanjiv Gupta1c6a38b2008-05-25 05:15:42 +0000221
Chris Lattner9c85ba32008-11-10 06:08:34 +0000222 return DebugFactory.CreateDerivedType(llvm::dwarf::DW_TAG_typedef, Unit,
223 TyName, DefUnit, Line, 0, 0, 0, 0, Src);
Sanjiv Gupta1c6a38b2008-05-25 05:15:42 +0000224}
225
Chris Lattner9c85ba32008-11-10 06:08:34 +0000226llvm::DIType CGDebugInfo::CreateType(const FunctionType *Ty,
227 llvm::DICompileUnit Unit) {
228 llvm::SmallVector<llvm::DIDescriptor, 16> EltTys;
Sanjiv Gupta1c6a38b2008-05-25 05:15:42 +0000229
Chris Lattner9c85ba32008-11-10 06:08:34 +0000230 // Add the result type at least.
231 EltTys.push_back(getOrCreateType(Ty->getResultType(), Unit));
232
233 // Set up remainder of arguments if there is a prototype.
234 // FIXME: IF NOT, HOW IS THIS REPRESENTED? llvm-gcc doesn't represent '...'!
Douglas Gregor72564e72009-02-26 23:50:07 +0000235 if (const FunctionProtoType *FTP = dyn_cast<FunctionProtoType>(Ty)) {
Chris Lattner9c85ba32008-11-10 06:08:34 +0000236 for (unsigned i = 0, e = FTP->getNumArgs(); i != e; ++i)
237 EltTys.push_back(getOrCreateType(FTP->getArgType(i), Unit));
238 } else {
239 // FIXME: Handle () case in C. llvm-gcc doesn't do it either.
Sanjiv Gupta1c6a38b2008-05-25 05:15:42 +0000240 }
241
Chris Lattner9c85ba32008-11-10 06:08:34 +0000242 llvm::DIArray EltTypeArray =
243 DebugFactory.GetOrCreateArray(&EltTys[0], EltTys.size());
244
245 return DebugFactory.CreateCompositeType(llvm::dwarf::DW_TAG_subroutine_type,
246 Unit, "", llvm::DICompileUnit(),
247 0, 0, 0, 0, 0,
248 llvm::DIType(), EltTypeArray);
Sanjiv Gupta1c6a38b2008-05-25 05:15:42 +0000249}
250
Devang Patel65e99f22009-02-25 01:36:11 +0000251/// CreateType - get structure or union type.
Chris Lattner9c85ba32008-11-10 06:08:34 +0000252llvm::DIType CGDebugInfo::CreateType(const RecordType *Ty,
253 llvm::DICompileUnit Unit) {
Douglas Gregora4c46df2008-12-11 17:59:21 +0000254 RecordDecl *Decl = Ty->getDecl();
Sanjiv Guptaf58c27a2008-06-07 04:46:53 +0000255
Chris Lattner9c85ba32008-11-10 06:08:34 +0000256 unsigned Tag;
257 if (Decl->isStruct())
258 Tag = llvm::dwarf::DW_TAG_structure_type;
259 else if (Decl->isUnion())
260 Tag = llvm::dwarf::DW_TAG_union_type;
261 else {
262 assert(Decl->isClass() && "Unknown RecordType!");
263 Tag = llvm::dwarf::DW_TAG_class_type;
Sanjiv Guptaf58c27a2008-06-07 04:46:53 +0000264 }
265
Sanjiv Gupta507de852008-06-09 10:47:41 +0000266 SourceManager &SM = M->getContext().getSourceManager();
Sanjiv Gupta507de852008-06-09 10:47:41 +0000267
Chris Lattner9c85ba32008-11-10 06:08:34 +0000268 // Get overall information about the record type for the debug info.
Chris Lattner8ec03f52008-11-24 03:54:41 +0000269 std::string Name = Decl->getNameAsString();
Sanjiv Gupta507de852008-06-09 10:47:41 +0000270
Chris Lattner9c85ba32008-11-10 06:08:34 +0000271 llvm::DICompileUnit DefUnit = getOrCreateCompileUnit(Decl->getLocation());
Devang Patel4f6fa232009-04-17 21:35:15 +0000272 PresumedLoc PLoc = SM.getPresumedLoc(Decl->getLocation());
273 unsigned Line = PLoc.isInvalid() ? 0 : PLoc.getLine();
Chris Lattner9c85ba32008-11-10 06:08:34 +0000274
275 // Records and classes and unions can all be recursive. To handle them, we
276 // first generate a debug descriptor for the struct as a forward declaration.
277 // Then (if it is a definition) we go through and get debug info for all of
278 // its members. Finally, we create a descriptor for the complete type (which
279 // may refer to the forward decl if the struct is recursive) and replace all
280 // uses of the forward declaration with the final definition.
281 llvm::DIType FwdDecl =
282 DebugFactory.CreateCompositeType(Tag, Unit, Name, DefUnit, Line, 0, 0, 0, 0,
283 llvm::DIType(), llvm::DIArray());
284
285 // If this is just a forward declaration, return it.
286 if (!Decl->getDefinition(M->getContext()))
287 return FwdDecl;
Sanjiv Gupta507de852008-06-09 10:47:41 +0000288
Chris Lattner9c85ba32008-11-10 06:08:34 +0000289 // Otherwise, insert it into the TypeCache so that recursive uses will find
290 // it.
291 TypeCache[QualType(Ty, 0).getAsOpaquePtr()] = FwdDecl;
292
293 // Convert all the elements.
294 llvm::SmallVector<llvm::DIDescriptor, 16> EltTys;
295
296 const ASTRecordLayout &RL = M->getContext().getASTRecordLayout(Decl);
297
298 unsigned FieldNo = 0;
Douglas Gregor6ab35242009-04-09 21:40:53 +0000299 for (RecordDecl::field_iterator I = Decl->field_begin(M->getContext()),
300 E = Decl->field_end(M->getContext());
Douglas Gregora4c46df2008-12-11 17:59:21 +0000301 I != E; ++I, ++FieldNo) {
Chris Lattner9c85ba32008-11-10 06:08:34 +0000302 FieldDecl *Field = *I;
303 llvm::DIType FieldTy = getOrCreateType(Field->getType(), Unit);
Chris Lattner8ec03f52008-11-24 03:54:41 +0000304
305 std::string FieldName = Field->getNameAsString();
Chris Lattner9c85ba32008-11-10 06:08:34 +0000306
307 // Get the location for the field.
308 SourceLocation FieldDefLoc = Field->getLocation();
309 llvm::DICompileUnit FieldDefUnit = getOrCreateCompileUnit(FieldDefLoc);
Devang Patel4f6fa232009-04-17 21:35:15 +0000310 PresumedLoc PLoc = SM.getPresumedLoc(FieldDefLoc);
311 unsigned FieldLine = PLoc.isInvalid() ? 0 : PLoc.getLine();
312
Devang Patelec9b5d52009-03-16 23:47:53 +0000313
314 QualType FType = Field->getType();
315 uint64_t FieldSize = 0;
316 unsigned FieldAlign = 0;
317 if (!FType->isIncompleteArrayType()) {
Chris Lattner9c85ba32008-11-10 06:08:34 +0000318
Devang Patelec9b5d52009-03-16 23:47:53 +0000319 // Bit size, align and offset of the type.
320 FieldSize = M->getContext().getTypeSize(FType);
321 Expr *BitWidth = Field->getBitWidth();
322 if (BitWidth)
323 FieldSize =
324 BitWidth->getIntegerConstantExprValue(M->getContext()).getZExtValue();
325
326 FieldAlign = M->getContext().getTypeAlign(FType);
327 }
328
Chris Lattner9c85ba32008-11-10 06:08:34 +0000329 uint64_t FieldOffset = RL.getFieldOffset(FieldNo);
330
331 // Create a DW_TAG_member node to remember the offset of this field in the
332 // struct. FIXME: This is an absolutely insane way to capture this
333 // information. When we gut debug info, this should be fixed.
334 FieldTy = DebugFactory.CreateDerivedType(llvm::dwarf::DW_TAG_member, Unit,
335 FieldName, FieldDefUnit,
336 FieldLine, FieldSize, FieldAlign,
337 FieldOffset, 0, FieldTy);
Chris Lattner9c85ba32008-11-10 06:08:34 +0000338 EltTys.push_back(FieldTy);
339 }
340
341 llvm::DIArray Elements =
342 DebugFactory.GetOrCreateArray(&EltTys[0], EltTys.size());
343
344 // Bit size, align and offset of the type.
345 uint64_t Size = M->getContext().getTypeSize(Ty);
346 uint64_t Align = M->getContext().getTypeAlign(Ty);
347
348 llvm::DIType RealDecl =
349 DebugFactory.CreateCompositeType(Tag, Unit, Name, DefUnit, Line, Size,
350 Align, 0, 0, llvm::DIType(), Elements);
351
352 // Now that we have a real decl for the struct, replace anything using the
353 // old decl with the new one. This will recursively update the debug info.
354 FwdDecl.getGV()->replaceAllUsesWith(RealDecl.getGV());
355 FwdDecl.getGV()->eraseFromParent();
356
357 return RealDecl;
358}
359
Devang Patel9ca36b62009-02-26 21:10:26 +0000360/// CreateType - get objective-c interface type.
361llvm::DIType CGDebugInfo::CreateType(const ObjCInterfaceType *Ty,
362 llvm::DICompileUnit Unit) {
363 ObjCInterfaceDecl *Decl = Ty->getDecl();
364
365 unsigned Tag = llvm::dwarf::DW_TAG_structure_type;
366 SourceManager &SM = M->getContext().getSourceManager();
367
368 // Get overall information about the record type for the debug info.
369 std::string Name = Decl->getNameAsString();
370
371 llvm::DICompileUnit DefUnit = getOrCreateCompileUnit(Decl->getLocation());
Devang Patel4f6fa232009-04-17 21:35:15 +0000372 PresumedLoc PLoc = SM.getPresumedLoc(Decl->getLocation());
373 unsigned Line = PLoc.isInvalid() ? 0 : PLoc.getLine();
374
Devang Patel9ca36b62009-02-26 21:10:26 +0000375
376 // To handle recursive interface, we
377 // first generate a debug descriptor for the struct as a forward declaration.
378 // Then (if it is a definition) we go through and get debug info for all of
379 // its members. Finally, we create a descriptor for the complete type (which
380 // may refer to the forward decl if the struct is recursive) and replace all
381 // uses of the forward declaration with the final definition.
382 llvm::DIType FwdDecl =
383 DebugFactory.CreateCompositeType(Tag, Unit, Name, DefUnit, Line, 0, 0, 0, 0,
384 llvm::DIType(), llvm::DIArray());
385
386 // If this is just a forward declaration, return it.
387 if (Decl->isForwardDecl())
388 return FwdDecl;
389
390 // Otherwise, insert it into the TypeCache so that recursive uses will find
391 // it.
392 TypeCache[QualType(Ty, 0).getAsOpaquePtr()] = FwdDecl;
393
394 // Convert all the elements.
395 llvm::SmallVector<llvm::DIDescriptor, 16> EltTys;
396
Devang Patelfbe899f2009-03-10 21:30:26 +0000397 ObjCInterfaceDecl *SClass = Decl->getSuperClass();
398 if (SClass) {
399 llvm::DIType SClassTy =
400 getOrCreateType(M->getContext().getObjCInterfaceType(SClass), Unit);
401 llvm::DIType InhTag =
402 DebugFactory.CreateDerivedType(llvm::dwarf::DW_TAG_inheritance,
403 Unit, "", Unit, 0, 0, 0,
404 0 /* offset */, 0, SClassTy);
405 EltTys.push_back(InhTag);
406 }
407
Devang Patel9ca36b62009-02-26 21:10:26 +0000408 const ASTRecordLayout &RL = M->getContext().getASTObjCInterfaceLayout(Decl);
409
410 unsigned FieldNo = 0;
411 for (ObjCInterfaceDecl::ivar_iterator I = Decl->ivar_begin(),
412 E = Decl->ivar_end(); I != E; ++I, ++FieldNo) {
413 ObjCIvarDecl *Field = *I;
414 llvm::DIType FieldTy = getOrCreateType(Field->getType(), Unit);
415
416 std::string FieldName = Field->getNameAsString();
417
418 // Get the location for the field.
419 SourceLocation FieldDefLoc = Field->getLocation();
420 llvm::DICompileUnit FieldDefUnit = getOrCreateCompileUnit(FieldDefLoc);
Devang Patel4f6fa232009-04-17 21:35:15 +0000421 PresumedLoc PLoc = SM.getPresumedLoc(FieldDefLoc);
422 unsigned FieldLine = PLoc.isInvalid() ? 0 : PLoc.getLine();
423
Devang Patel99c20eb2009-03-20 18:24:39 +0000424
425 QualType FType = Field->getType();
426 uint64_t FieldSize = 0;
427 unsigned FieldAlign = 0;
Devang Patelc20482b2009-03-19 00:23:53 +0000428
Devang Patel99c20eb2009-03-20 18:24:39 +0000429 if (!FType->isIncompleteArrayType()) {
430
431 // Bit size, align and offset of the type.
432 FieldSize = M->getContext().getTypeSize(FType);
433 Expr *BitWidth = Field->getBitWidth();
434 if (BitWidth)
435 FieldSize =
436 BitWidth->getIntegerConstantExprValue(M->getContext()).getZExtValue();
437
438 FieldAlign = M->getContext().getTypeAlign(FType);
439 }
440
441 uint64_t FieldOffset = RL.getFieldOffset(FieldNo);
442
Devang Patelc20482b2009-03-19 00:23:53 +0000443 unsigned Flags = 0;
444 if (Field->getAccessControl() == ObjCIvarDecl::Protected)
445 Flags = llvm::DIType::FlagProtected;
446 else if (Field->getAccessControl() == ObjCIvarDecl::Private)
447 Flags = llvm::DIType::FlagPrivate;
448
Devang Patel9ca36b62009-02-26 21:10:26 +0000449 // Create a DW_TAG_member node to remember the offset of this field in the
450 // struct. FIXME: This is an absolutely insane way to capture this
451 // information. When we gut debug info, this should be fixed.
452 FieldTy = DebugFactory.CreateDerivedType(llvm::dwarf::DW_TAG_member, Unit,
453 FieldName, FieldDefUnit,
454 FieldLine, FieldSize, FieldAlign,
Devang Patelc20482b2009-03-19 00:23:53 +0000455 FieldOffset, Flags, FieldTy);
Devang Patel9ca36b62009-02-26 21:10:26 +0000456 EltTys.push_back(FieldTy);
457 }
458
459 llvm::DIArray Elements =
460 DebugFactory.GetOrCreateArray(&EltTys[0], EltTys.size());
461
462 // Bit size, align and offset of the type.
463 uint64_t Size = M->getContext().getTypeSize(Ty);
464 uint64_t Align = M->getContext().getTypeAlign(Ty);
465
466 llvm::DIType RealDecl =
467 DebugFactory.CreateCompositeType(Tag, Unit, Name, DefUnit, Line, Size,
468 Align, 0, 0, llvm::DIType(), Elements);
469
470 // Now that we have a real decl for the struct, replace anything using the
471 // old decl with the new one. This will recursively update the debug info.
472 FwdDecl.getGV()->replaceAllUsesWith(RealDecl.getGV());
473 FwdDecl.getGV()->eraseFromParent();
474
475 return RealDecl;
476}
477
Chris Lattner9c85ba32008-11-10 06:08:34 +0000478llvm::DIType CGDebugInfo::CreateType(const EnumType *Ty,
479 llvm::DICompileUnit Unit) {
480 EnumDecl *Decl = Ty->getDecl();
481
482 llvm::SmallVector<llvm::DIDescriptor, 32> Enumerators;
483
484 // Create DIEnumerator elements for each enumerator.
Douglas Gregor6ab35242009-04-09 21:40:53 +0000485 for (EnumDecl::enumerator_iterator
486 Enum = Decl->enumerator_begin(M->getContext()),
487 EnumEnd = Decl->enumerator_end(M->getContext());
Douglas Gregor44b43212008-12-11 16:49:14 +0000488 Enum != EnumEnd; ++Enum) {
489 Enumerators.push_back(DebugFactory.CreateEnumerator(Enum->getNameAsString(),
490 Enum->getInitVal().getZExtValue()));
Chris Lattner9c85ba32008-11-10 06:08:34 +0000491 }
492
493 // Return a CompositeType for the enum itself.
494 llvm::DIArray EltArray =
495 DebugFactory.GetOrCreateArray(&Enumerators[0], Enumerators.size());
496
Chris Lattner8ec03f52008-11-24 03:54:41 +0000497 std::string EnumName = Decl->getNameAsString();
Chris Lattner9c85ba32008-11-10 06:08:34 +0000498 SourceLocation DefLoc = Decl->getLocation();
499 llvm::DICompileUnit DefUnit = getOrCreateCompileUnit(DefLoc);
500 SourceManager &SM = M->getContext().getSourceManager();
Devang Patel4f6fa232009-04-17 21:35:15 +0000501 PresumedLoc PLoc = SM.getPresumedLoc(DefLoc);
502 unsigned Line = PLoc.isInvalid() ? 0 : PLoc.getLine();
503
Chris Lattner9c85ba32008-11-10 06:08:34 +0000504
505 // Size and align of the type.
506 uint64_t Size = M->getContext().getTypeSize(Ty);
Chris Lattnerf7cf85b2009-01-16 07:36:28 +0000507 unsigned Align = M->getContext().getTypeAlign(Ty);
Chris Lattner9c85ba32008-11-10 06:08:34 +0000508
509 return DebugFactory.CreateCompositeType(llvm::dwarf::DW_TAG_enumeration_type,
510 Unit, EnumName, DefUnit, Line,
511 Size, Align, 0, 0,
512 llvm::DIType(), EltArray);
513}
514
515llvm::DIType CGDebugInfo::CreateType(const TagType *Ty,
516 llvm::DICompileUnit Unit) {
517 if (const RecordType *RT = dyn_cast<RecordType>(Ty))
518 return CreateType(RT, Unit);
519 else if (const EnumType *ET = dyn_cast<EnumType>(Ty))
520 return CreateType(ET, Unit);
521
522 return llvm::DIType();
523}
524
525llvm::DIType CGDebugInfo::CreateType(const ArrayType *Ty,
526 llvm::DICompileUnit Unit) {
Anders Carlsson835c9092009-01-05 01:23:29 +0000527 uint64_t Size;
528 uint64_t Align;
529
530
Nuno Lopes010d5142009-01-28 00:35:17 +0000531 // FIXME: make getTypeAlign() aware of VLAs and incomplete array types
Anders Carlsson835c9092009-01-05 01:23:29 +0000532 if (const VariableArrayType *VAT = dyn_cast<VariableArrayType>(Ty)) {
Anders Carlsson835c9092009-01-05 01:23:29 +0000533 Size = 0;
534 Align =
Nuno Lopes010d5142009-01-28 00:35:17 +0000535 M->getContext().getTypeAlign(M->getContext().getBaseElementType(VAT));
536 } else if (Ty->isIncompleteArrayType()) {
537 Size = 0;
538 Align = M->getContext().getTypeAlign(Ty->getElementType());
Anders Carlsson835c9092009-01-05 01:23:29 +0000539 } else {
540 // Size and align of the whole array, not the element type.
541 Size = M->getContext().getTypeSize(Ty);
542 Align = M->getContext().getTypeAlign(Ty);
543 }
Chris Lattner9c85ba32008-11-10 06:08:34 +0000544
545 // Add the dimensions of the array. FIXME: This loses CV qualifiers from
546 // interior arrays, do we care? Why aren't nested arrays represented the
547 // obvious/recursive way?
548 llvm::SmallVector<llvm::DIDescriptor, 8> Subscripts;
549 QualType EltTy(Ty, 0);
550 while ((Ty = dyn_cast<ArrayType>(EltTy))) {
Sanjiv Gupta507de852008-06-09 10:47:41 +0000551 uint64_t Upper = 0;
Chris Lattner9c85ba32008-11-10 06:08:34 +0000552 if (const ConstantArrayType *CAT = dyn_cast<ConstantArrayType>(Ty))
553 Upper = CAT->getSize().getZExtValue() - 1;
554 // FIXME: Verify this is right for VLAs.
555 Subscripts.push_back(DebugFactory.GetOrCreateSubrange(0, Upper));
556 EltTy = Ty->getElementType();
Sanjiv Gupta507de852008-06-09 10:47:41 +0000557 }
Sanjiv Gupta1c6a38b2008-05-25 05:15:42 +0000558
Chris Lattner9c85ba32008-11-10 06:08:34 +0000559 llvm::DIArray SubscriptArray =
560 DebugFactory.GetOrCreateArray(&Subscripts[0], Subscripts.size());
561
562 return DebugFactory.CreateCompositeType(llvm::dwarf::DW_TAG_array_type,
563 Unit, "", llvm::DICompileUnit(),
564 0, Size, Align, 0, 0,
565 getOrCreateType(EltTy, Unit),
566 SubscriptArray);
567}
568
569
Sanjiv Gupta1c6a38b2008-05-25 05:15:42 +0000570/// getOrCreateType - Get the type from the cache or create a new
571/// one if necessary.
Chris Lattner9c85ba32008-11-10 06:08:34 +0000572llvm::DIType CGDebugInfo::getOrCreateType(QualType Ty,
573 llvm::DICompileUnit Unit) {
574 if (Ty.isNull())
575 return llvm::DIType();
576
Sanjiv Gupta1c6a38b2008-05-25 05:15:42 +0000577 // Check to see if the compile unit already has created this type.
Chris Lattner9c85ba32008-11-10 06:08:34 +0000578 llvm::DIType &Slot = TypeCache[Ty.getAsOpaquePtr()];
579 if (!Slot.isNull()) return Slot;
Sanjiv Gupta1c6a38b2008-05-25 05:15:42 +0000580
Chris Lattner9c85ba32008-11-10 06:08:34 +0000581 // Handle CVR qualifiers, which recursively handles what they refer to.
582 if (Ty.getCVRQualifiers())
583 return Slot = CreateCVRType(Ty, Unit);
Sanjiv Gupta1c6a38b2008-05-25 05:15:42 +0000584
585 // Work out details of type.
Chris Lattner9c85ba32008-11-10 06:08:34 +0000586 switch (Ty->getTypeClass()) {
Douglas Gregor72564e72009-02-26 23:50:07 +0000587#define TYPE(Class, Base)
588#define ABSTRACT_TYPE(Class, Base)
589#define NON_CANONICAL_TYPE(Class, Base)
590#define DEPENDENT_TYPE(Class, Base) case Type::Class:
591#include "clang/AST/TypeNodes.def"
592 assert(false && "Dependent types cannot show up in debug information");
593
Sebastian Redl7c80bd62009-03-16 23:22:08 +0000594 case Type::LValueReference:
595 case Type::RValueReference:
Chris Lattner9c85ba32008-11-10 06:08:34 +0000596 case Type::Vector:
597 case Type::ExtVector:
Fariborz Jahanianf11284a2009-02-17 18:27:45 +0000598 case Type::ExtQual:
Eli Friedman00524e32009-02-27 23:15:07 +0000599 case Type::FixedWidthInt:
600 case Type::BlockPointer:
601 case Type::MemberPointer:
Douglas Gregor7532dc62009-03-30 22:58:21 +0000602 case Type::TemplateSpecialization:
Douglas Gregore4e5b052009-03-19 00:18:19 +0000603 case Type::QualifiedName:
Eli Friedman00524e32009-02-27 23:15:07 +0000604 // Unsupported types
Chris Lattner9c85ba32008-11-10 06:08:34 +0000605 return llvm::DIType();
Chris Lattneraa2b5792009-04-22 06:58:56 +0000606 case Type::ObjCQualifiedId: // Encode id<p> in debug info just like id.
607 return Slot = getOrCreateType(M->getContext().getObjCIdType(), Unit);
608
609 case Type::ObjCQualifiedInterface: // Drop protocols from interface.
Devang Patele7987062009-03-02 17:58:28 +0000610 case Type::ObjCInterface:
Chris Lattneraa2b5792009-04-22 06:58:56 +0000611 return Slot = CreateType(cast<ObjCInterfaceType>(Ty), Unit);
612 case Type::Builtin: return Slot = CreateType(cast<BuiltinType>(Ty), Unit);
Chris Lattnerb7003772009-04-23 06:13:01 +0000613 case Type::Complex: return Slot = CreateType(cast<ComplexType>(Ty), Unit);
Chris Lattneraa2b5792009-04-22 06:58:56 +0000614 case Type::Pointer: return Slot = CreateType(cast<PointerType>(Ty), Unit);
615 case Type::Typedef: return Slot = CreateType(cast<TypedefType>(Ty), Unit);
Douglas Gregor72564e72009-02-26 23:50:07 +0000616 case Type::Record:
Douglas Gregor72564e72009-02-26 23:50:07 +0000617 case Type::Enum:
Chris Lattneraa2b5792009-04-22 06:58:56 +0000618 return Slot = CreateType(cast<TagType>(Ty), Unit);
Chris Lattner9c85ba32008-11-10 06:08:34 +0000619 case Type::FunctionProto:
620 case Type::FunctionNoProto:
Chris Lattner3cc5c402008-11-11 07:01:36 +0000621 return Slot = CreateType(cast<FunctionType>(Ty), Unit);
Chris Lattner9c85ba32008-11-10 06:08:34 +0000622
623 case Type::ConstantArray:
624 case Type::VariableArray:
625 case Type::IncompleteArray:
Chris Lattner3cc5c402008-11-11 07:01:36 +0000626 return Slot = CreateType(cast<ArrayType>(Ty), Unit);
Douglas Gregor72564e72009-02-26 23:50:07 +0000627 case Type::TypeOfExpr:
628 return Slot = getOrCreateType(cast<TypeOfExprType>(Ty)->getUnderlyingExpr()
Chris Lattner3cc5c402008-11-11 07:01:36 +0000629 ->getType(), Unit);
Douglas Gregor72564e72009-02-26 23:50:07 +0000630 case Type::TypeOf:
Chris Lattner3cc5c402008-11-11 07:01:36 +0000631 return Slot = getOrCreateType(cast<TypeOfType>(Ty)->getUnderlyingType(),
632 Unit);
Sanjiv Gupta1c6a38b2008-05-25 05:15:42 +0000633 }
Chris Lattner9c85ba32008-11-10 06:08:34 +0000634
Sanjiv Gupta1c6a38b2008-05-25 05:15:42 +0000635 return Slot;
636}
637
638/// EmitFunctionStart - Constructs the debug code for entering a function -
639/// "llvm.dbg.func.start.".
Chris Lattner9c85ba32008-11-10 06:08:34 +0000640void CGDebugInfo::EmitFunctionStart(const char *Name, QualType ReturnType,
Sanjiv Gupta1c6a38b2008-05-25 05:15:42 +0000641 llvm::Function *Fn,
Chris Lattner9c85ba32008-11-10 06:08:34 +0000642 CGBuilderTy &Builder) {
643 // FIXME: Why is this using CurLoc???
644 llvm::DICompileUnit Unit = getOrCreateCompileUnit(CurLoc);
Sanjiv Gupta1c6a38b2008-05-25 05:15:42 +0000645 SourceManager &SM = M->getContext().getSourceManager();
Devang Patel0f78fea2009-04-08 19:47:04 +0000646 unsigned LineNo = SM.getPresumedLoc(CurLoc).getLine();
Chris Lattner9c85ba32008-11-10 06:08:34 +0000647
648 llvm::DISubprogram SP =
649 DebugFactory.CreateSubprogram(Unit, Name, Name, "", Unit, LineNo,
650 getOrCreateType(ReturnType, Unit),
651 Fn->hasInternalLinkage(), true/*definition*/);
652
653 DebugFactory.InsertSubprogramStart(SP, Builder.GetInsertBlock());
654
Sanjiv Gupta1c6a38b2008-05-25 05:15:42 +0000655 // Push function on region stack.
Chris Lattner9c85ba32008-11-10 06:08:34 +0000656 RegionStack.push_back(SP);
Sanjiv Gupta1c6a38b2008-05-25 05:15:42 +0000657}
658
659
Chris Lattner9c85ba32008-11-10 06:08:34 +0000660void CGDebugInfo::EmitStopPoint(llvm::Function *Fn, CGBuilderTy &Builder) {
Sanjiv Gupta1c6a38b2008-05-25 05:15:42 +0000661 if (CurLoc.isInvalid() || CurLoc.isMacroID()) return;
662
Sanjiv Guptae8b9f5b2008-05-08 08:54:20 +0000663 // Don't bother if things are the same as last time.
664 SourceManager &SM = M->getContext().getSourceManager();
665 if (CurLoc == PrevLoc
Chris Lattner30fc9332009-02-04 01:06:56 +0000666 || (SM.getInstantiationLineNumber(CurLoc) ==
667 SM.getInstantiationLineNumber(PrevLoc)
Sanjiv Guptae8b9f5b2008-05-08 08:54:20 +0000668 && SM.isFromSameFile(CurLoc, PrevLoc)))
669 return;
Sanjiv Guptae8b9f5b2008-05-08 08:54:20 +0000670
671 // Update last state.
672 PrevLoc = CurLoc;
673
674 // Get the appropriate compile unit.
Chris Lattner9c85ba32008-11-10 06:08:34 +0000675 llvm::DICompileUnit Unit = getOrCreateCompileUnit(CurLoc);
Devang Patel0f78fea2009-04-08 19:47:04 +0000676 PresumedLoc PLoc = SM.getPresumedLoc(CurLoc);
677 DebugFactory.InsertStopPoint(Unit, PLoc.getLine(), PLoc.getColumn(),
Chris Lattner9c85ba32008-11-10 06:08:34 +0000678 Builder.GetInsertBlock());
Sanjiv Guptae8b9f5b2008-05-08 08:54:20 +0000679}
680
681/// EmitRegionStart- Constructs the debug code for entering a declarative
682/// region - "llvm.dbg.region.start.".
Chris Lattner9c85ba32008-11-10 06:08:34 +0000683void CGDebugInfo::EmitRegionStart(llvm::Function *Fn, CGBuilderTy &Builder) {
684 llvm::DIDescriptor D;
Daniel Dunbar5273f512008-10-17 01:07:56 +0000685 if (!RegionStack.empty())
Chris Lattner9c85ba32008-11-10 06:08:34 +0000686 D = RegionStack.back();
687 D = DebugFactory.CreateBlock(D);
688 RegionStack.push_back(D);
689 DebugFactory.InsertRegionStart(D, Builder.GetInsertBlock());
Sanjiv Guptae8b9f5b2008-05-08 08:54:20 +0000690}
691
692/// EmitRegionEnd - Constructs the debug code for exiting a declarative
693/// region - "llvm.dbg.region.end."
Chris Lattner9c85ba32008-11-10 06:08:34 +0000694void CGDebugInfo::EmitRegionEnd(llvm::Function *Fn, CGBuilderTy &Builder) {
Daniel Dunbar5273f512008-10-17 01:07:56 +0000695 assert(!RegionStack.empty() && "Region stack mismatch, stack empty!");
696
Sanjiv Guptae8b9f5b2008-05-08 08:54:20 +0000697 // Provide an region stop point.
698 EmitStopPoint(Fn, Builder);
699
Chris Lattner9c85ba32008-11-10 06:08:34 +0000700 DebugFactory.InsertRegionEnd(RegionStack.back(), Builder.GetInsertBlock());
Sanjiv Gupta1c6a38b2008-05-25 05:15:42 +0000701 RegionStack.pop_back();
Sanjiv Guptae8b9f5b2008-05-08 08:54:20 +0000702}
703
Sanjiv Guptacc9b1632008-05-30 10:30:31 +0000704/// EmitDeclare - Emit local variable declaration debug info.
Chris Lattner9c85ba32008-11-10 06:08:34 +0000705void CGDebugInfo::EmitDeclare(const VarDecl *Decl, unsigned Tag,
706 llvm::Value *Storage, CGBuilderTy &Builder) {
Daniel Dunbar5273f512008-10-17 01:07:56 +0000707 assert(!RegionStack.empty() && "Region stack mismatch, stack empty!");
708
Devang Patel07739032009-03-27 23:16:32 +0000709 // Do not emit variable debug information while generating optimized code.
710 // The llvm optimizer and code generator are not yet ready to support
711 // optimized code debugging.
712 const CompileOptions &CO = M->getCompileOpts();
713 if (CO.OptimizationLevel)
714 return;
715
Chris Lattner9c85ba32008-11-10 06:08:34 +0000716 // Get location information.
Sanjiv Guptacc9b1632008-05-30 10:30:31 +0000717 SourceManager &SM = M->getContext().getSourceManager();
Devang Patel4f6fa232009-04-17 21:35:15 +0000718 PresumedLoc PLoc = SM.getPresumedLoc(Decl->getLocation());
719 unsigned Line = PLoc.isInvalid() ? 0 : PLoc.getLine();
Chris Lattner9c85ba32008-11-10 06:08:34 +0000720 llvm::DICompileUnit Unit = getOrCreateCompileUnit(Decl->getLocation());
721
722 // Create the descriptor for the variable.
723 llvm::DIVariable D =
Chris Lattnerd9d22dd2008-11-24 05:29:24 +0000724 DebugFactory.CreateVariable(Tag, RegionStack.back(),Decl->getNameAsString(),
Chris Lattner9c85ba32008-11-10 06:08:34 +0000725 Unit, Line,
726 getOrCreateType(Decl->getType(), Unit));
727 // Insert an llvm.dbg.declare into the current block.
728 DebugFactory.InsertDeclare(Storage, D, Builder.GetInsertBlock());
Sanjiv Guptacc9b1632008-05-30 10:30:31 +0000729}
730
Chris Lattner9c85ba32008-11-10 06:08:34 +0000731void CGDebugInfo::EmitDeclareOfAutoVariable(const VarDecl *Decl,
732 llvm::Value *Storage,
733 CGBuilderTy &Builder) {
734 EmitDeclare(Decl, llvm::dwarf::DW_TAG_auto_variable, Storage, Builder);
735}
736
737/// EmitDeclareOfArgVariable - Emit call to llvm.dbg.declare for an argument
738/// variable declaration.
739void CGDebugInfo::EmitDeclareOfArgVariable(const VarDecl *Decl, llvm::Value *AI,
740 CGBuilderTy &Builder) {
741 EmitDeclare(Decl, llvm::dwarf::DW_TAG_arg_variable, AI, Builder);
742}
743
744
745
Sanjiv Gupta686226b2008-06-05 08:59:10 +0000746/// EmitGlobalVariable - Emit information about a global variable.
Chris Lattner9c85ba32008-11-10 06:08:34 +0000747void CGDebugInfo::EmitGlobalVariable(llvm::GlobalVariable *Var,
748 const VarDecl *Decl) {
Devang Patel07739032009-03-27 23:16:32 +0000749
750 // Do not emit variable debug information while generating optimized code.
751 // The llvm optimizer and code generator are not yet ready to support
752 // optimized code debugging.
753 const CompileOptions &CO = M->getCompileOpts();
754 if (CO.OptimizationLevel)
755 return;
756
Sanjiv Gupta686226b2008-06-05 08:59:10 +0000757 // Create global variable debug descriptor.
Chris Lattner9c85ba32008-11-10 06:08:34 +0000758 llvm::DICompileUnit Unit = getOrCreateCompileUnit(Decl->getLocation());
Sanjiv Gupta686226b2008-06-05 08:59:10 +0000759 SourceManager &SM = M->getContext().getSourceManager();
Devang Patel4f6fa232009-04-17 21:35:15 +0000760 PresumedLoc PLoc = SM.getPresumedLoc(Decl->getLocation());
761 unsigned LineNo = PLoc.isInvalid() ? 0 : PLoc.getLine();
Chris Lattner8ec03f52008-11-24 03:54:41 +0000762
763 std::string Name = Decl->getNameAsString();
Anders Carlsson4d6e8dd2008-11-26 17:40:42 +0000764
765 QualType T = Decl->getType();
766 if (T->isIncompleteArrayType()) {
767
768 // CodeGen turns int[] into int[1] so we'll do the same here.
769 llvm::APSInt ConstVal(32);
770
771 ConstVal = 1;
772 QualType ET = M->getContext().getAsArrayType(T)->getElementType();
773
774 T = M->getContext().getConstantArrayType(ET, ConstVal,
775 ArrayType::Normal, 0);
776 }
777
Chris Lattner9c85ba32008-11-10 06:08:34 +0000778 DebugFactory.CreateGlobalVariable(Unit, Name, Name, "", Unit, LineNo,
Anders Carlsson4d6e8dd2008-11-26 17:40:42 +0000779 getOrCreateType(T, Unit),
Chris Lattner9c85ba32008-11-10 06:08:34 +0000780 Var->hasInternalLinkage(),
781 true/*definition*/, Var);
Sanjiv Gupta686226b2008-06-05 08:59:10 +0000782}
783
Devang Patel9ca36b62009-02-26 21:10:26 +0000784/// EmitGlobalVariable - Emit information about an objective-c interface.
785void CGDebugInfo::EmitGlobalVariable(llvm::GlobalVariable *Var,
786 ObjCInterfaceDecl *Decl) {
787 // Create global variable debug descriptor.
788 llvm::DICompileUnit Unit = getOrCreateCompileUnit(Decl->getLocation());
789 SourceManager &SM = M->getContext().getSourceManager();
Devang Patel4f6fa232009-04-17 21:35:15 +0000790 PresumedLoc PLoc = SM.getPresumedLoc(Decl->getLocation());
791 unsigned LineNo = PLoc.isInvalid() ? 0 : PLoc.getLine();
Devang Patel9ca36b62009-02-26 21:10:26 +0000792
793 std::string Name = Decl->getNameAsString();
794
Chris Lattner03d9f342009-04-01 06:23:52 +0000795 QualType T = M->getContext().getObjCInterfaceType(Decl);
Devang Patel9ca36b62009-02-26 21:10:26 +0000796 if (T->isIncompleteArrayType()) {
797
798 // CodeGen turns int[] into int[1] so we'll do the same here.
799 llvm::APSInt ConstVal(32);
800
801 ConstVal = 1;
802 QualType ET = M->getContext().getAsArrayType(T)->getElementType();
803
804 T = M->getContext().getConstantArrayType(ET, ConstVal,
805 ArrayType::Normal, 0);
806 }
807
808 DebugFactory.CreateGlobalVariable(Unit, Name, Name, "", Unit, LineNo,
809 getOrCreateType(T, Unit),
810 Var->hasInternalLinkage(),
811 true/*definition*/, Var);
812}
813