blob: ac1616b933d4fec32cb277d366f27d67439580b9 [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
Chris Lattnerb95ee582009-05-02 01:04:13 +0000106 std::string Producer = "clang 1.0";// FIXME: clang version.
107 bool isOptimized = LO.Optimize;
Chris Lattner4c2577a2009-05-02 01:00:04 +0000108 const char *Flags = ""; // FIXME: Encode command line options.
109
110 // Figure out which version of the ObjC runtime we have.
111 unsigned RuntimeVers = 0;
112 if (LO.ObjC1)
113 RuntimeVers = LO.ObjCNonFragileABI ? 2 : 1;
114
Sanjiv Guptae8b9f5b2008-05-08 08:54:20 +0000115 // Create new compile unit.
Devang Patel446c6192009-04-17 21:06:59 +0000116 return Unit = DebugFactory.CreateCompileUnit(LangTag, AbsFileName.getLast(),
117 AbsFileName.getDirname(),
Chris Lattner4c2577a2009-05-02 01:00:04 +0000118 Producer, isMain, isOptimized,
119 Flags, RuntimeVers);
Sanjiv Guptae8b9f5b2008-05-08 08:54:20 +0000120}
121
Devang Patel65e99f22009-02-25 01:36:11 +0000122/// CreateType - Get the Basic type from the cache or create a new
Chris Lattner9c85ba32008-11-10 06:08:34 +0000123/// one if necessary.
124llvm::DIType CGDebugInfo::CreateType(const BuiltinType *BT,
Devang Patel65e99f22009-02-25 01:36:11 +0000125 llvm::DICompileUnit Unit) {
Chris Lattner9c85ba32008-11-10 06:08:34 +0000126 unsigned Encoding = 0;
127 switch (BT->getKind()) {
128 default:
129 case BuiltinType::Void:
130 return llvm::DIType();
131 case BuiltinType::UChar:
132 case BuiltinType::Char_U: Encoding = llvm::dwarf::DW_ATE_unsigned_char; break;
133 case BuiltinType::Char_S:
134 case BuiltinType::SChar: Encoding = llvm::dwarf::DW_ATE_signed_char; break;
135 case BuiltinType::UShort:
136 case BuiltinType::UInt:
137 case BuiltinType::ULong:
138 case BuiltinType::ULongLong: Encoding = llvm::dwarf::DW_ATE_unsigned; break;
139 case BuiltinType::Short:
140 case BuiltinType::Int:
141 case BuiltinType::Long:
142 case BuiltinType::LongLong: Encoding = llvm::dwarf::DW_ATE_signed; break;
143 case BuiltinType::Bool: Encoding = llvm::dwarf::DW_ATE_boolean; break;
144 case BuiltinType::Float:
145 case BuiltinType::Double: Encoding = llvm::dwarf::DW_ATE_float; break;
146 }
147 // Bit size, align and offset of the type.
148 uint64_t Size = M->getContext().getTypeSize(BT);
149 uint64_t Align = M->getContext().getTypeAlign(BT);
150 uint64_t Offset = 0;
151
152 return DebugFactory.CreateBasicType(Unit, BT->getName(), Unit, 0, Size, Align,
153 Offset, /*flags*/ 0, Encoding);
154}
Sanjiv Guptae8b9f5b2008-05-08 08:54:20 +0000155
Chris Lattnerb7003772009-04-23 06:13:01 +0000156llvm::DIType CGDebugInfo::CreateType(const ComplexType *Ty,
157 llvm::DICompileUnit Unit) {
158 // Bit size, align and offset of the type.
159 unsigned Encoding = llvm::dwarf::DW_ATE_complex_float;
160 if (Ty->isComplexIntegerType())
161 Encoding = llvm::dwarf::DW_ATE_lo_user;
162
163 uint64_t Size = M->getContext().getTypeSize(Ty);
164 uint64_t Align = M->getContext().getTypeAlign(Ty);
165 uint64_t Offset = 0;
166
167 return DebugFactory.CreateBasicType(Unit, "complex",
168 Unit, 0, Size, Align,
169 Offset, /*flags*/ 0, Encoding);
170}
171
Sanjiv Guptaf58c27a2008-06-07 04:46:53 +0000172/// getOrCreateCVRType - Get the CVR qualified type from the cache or create
173/// a new one if necessary.
Chris Lattner9c85ba32008-11-10 06:08:34 +0000174llvm::DIType CGDebugInfo::CreateCVRType(QualType Ty, llvm::DICompileUnit Unit) {
175 // We will create one Derived type for one qualifier and recurse to handle any
176 // additional ones.
177 llvm::DIType FromTy;
178 unsigned Tag;
179 if (Ty.isConstQualified()) {
180 Tag = llvm::dwarf::DW_TAG_const_type;
181 Ty.removeConst();
182 FromTy = getOrCreateType(Ty, Unit);
183 } else if (Ty.isVolatileQualified()) {
184 Tag = llvm::dwarf::DW_TAG_volatile_type;
185 Ty.removeVolatile();
186 FromTy = getOrCreateType(Ty, Unit);
187 } else {
188 assert(Ty.isRestrictQualified() && "Unknown type qualifier for debug info");
189 Tag = llvm::dwarf::DW_TAG_restrict_type;
190 Ty.removeRestrict();
191 FromTy = getOrCreateType(Ty, Unit);
Sanjiv Gupta1c6a38b2008-05-25 05:15:42 +0000192 }
Chris Lattner9c85ba32008-11-10 06:08:34 +0000193
Daniel Dunbar3845f862008-10-31 03:54:29 +0000194 // No need to fill in the Name, Line, Size, Alignment, Offset in case of
195 // CVR derived types.
Chris Lattner9c85ba32008-11-10 06:08:34 +0000196 return DebugFactory.CreateDerivedType(Tag, Unit, "", llvm::DICompileUnit(),
197 0, 0, 0, 0, 0, FromTy);
Sanjiv Gupta1c6a38b2008-05-25 05:15:42 +0000198}
199
Chris Lattner9c85ba32008-11-10 06:08:34 +0000200llvm::DIType CGDebugInfo::CreateType(const PointerType *Ty,
201 llvm::DICompileUnit Unit) {
202 llvm::DIType EltTy = getOrCreateType(Ty->getPointeeType(), Unit);
Sanjiv Gupta1c6a38b2008-05-25 05:15:42 +0000203
Sanjiv Gupta1c6a38b2008-05-25 05:15:42 +0000204 // Bit size, align and offset of the type.
Chris Lattner9c85ba32008-11-10 06:08:34 +0000205 uint64_t Size = M->getContext().getTypeSize(Ty);
206 uint64_t Align = M->getContext().getTypeAlign(Ty);
Sanjiv Gupta1c6a38b2008-05-25 05:15:42 +0000207
Chris Lattner9c85ba32008-11-10 06:08:34 +0000208 return DebugFactory.CreateDerivedType(llvm::dwarf::DW_TAG_pointer_type, Unit,
209 "", llvm::DICompileUnit(),
210 0, Size, Align, 0, 0, EltTy);
Sanjiv Gupta1c6a38b2008-05-25 05:15:42 +0000211}
212
Chris Lattner9c85ba32008-11-10 06:08:34 +0000213llvm::DIType CGDebugInfo::CreateType(const TypedefType *Ty,
214 llvm::DICompileUnit Unit) {
215 // Typedefs are derived from some other type. If we have a typedef of a
216 // typedef, make sure to emit the whole chain.
217 llvm::DIType Src = getOrCreateType(Ty->getDecl()->getUnderlyingType(), Unit);
218
219 // We don't set size information, but do specify where the typedef was
220 // declared.
Chris Lattner8ec03f52008-11-24 03:54:41 +0000221 std::string TyName = Ty->getDecl()->getNameAsString();
Chris Lattner9c85ba32008-11-10 06:08:34 +0000222 SourceLocation DefLoc = Ty->getDecl()->getLocation();
223 llvm::DICompileUnit DefUnit = getOrCreateCompileUnit(DefLoc);
Sanjiv Gupta1c6a38b2008-05-25 05:15:42 +0000224
Sanjiv Gupta1c6a38b2008-05-25 05:15:42 +0000225 SourceManager &SM = M->getContext().getSourceManager();
Devang Patel4f6fa232009-04-17 21:35:15 +0000226 PresumedLoc PLoc = SM.getPresumedLoc(DefLoc);
227 unsigned Line = PLoc.isInvalid() ? 0 : PLoc.getLine();
Sanjiv Gupta1c6a38b2008-05-25 05:15:42 +0000228
Chris Lattner9c85ba32008-11-10 06:08:34 +0000229 return DebugFactory.CreateDerivedType(llvm::dwarf::DW_TAG_typedef, Unit,
230 TyName, DefUnit, Line, 0, 0, 0, 0, Src);
Sanjiv Gupta1c6a38b2008-05-25 05:15:42 +0000231}
232
Chris Lattner9c85ba32008-11-10 06:08:34 +0000233llvm::DIType CGDebugInfo::CreateType(const FunctionType *Ty,
234 llvm::DICompileUnit Unit) {
235 llvm::SmallVector<llvm::DIDescriptor, 16> EltTys;
Sanjiv Gupta1c6a38b2008-05-25 05:15:42 +0000236
Chris Lattner9c85ba32008-11-10 06:08:34 +0000237 // Add the result type at least.
238 EltTys.push_back(getOrCreateType(Ty->getResultType(), Unit));
239
240 // Set up remainder of arguments if there is a prototype.
241 // FIXME: IF NOT, HOW IS THIS REPRESENTED? llvm-gcc doesn't represent '...'!
Douglas Gregor72564e72009-02-26 23:50:07 +0000242 if (const FunctionProtoType *FTP = dyn_cast<FunctionProtoType>(Ty)) {
Chris Lattner9c85ba32008-11-10 06:08:34 +0000243 for (unsigned i = 0, e = FTP->getNumArgs(); i != e; ++i)
244 EltTys.push_back(getOrCreateType(FTP->getArgType(i), Unit));
245 } else {
246 // FIXME: Handle () case in C. llvm-gcc doesn't do it either.
Sanjiv Gupta1c6a38b2008-05-25 05:15:42 +0000247 }
248
Chris Lattner9c85ba32008-11-10 06:08:34 +0000249 llvm::DIArray EltTypeArray =
250 DebugFactory.GetOrCreateArray(&EltTys[0], EltTys.size());
251
252 return DebugFactory.CreateCompositeType(llvm::dwarf::DW_TAG_subroutine_type,
253 Unit, "", llvm::DICompileUnit(),
254 0, 0, 0, 0, 0,
255 llvm::DIType(), EltTypeArray);
Sanjiv Gupta1c6a38b2008-05-25 05:15:42 +0000256}
257
Devang Patel65e99f22009-02-25 01:36:11 +0000258/// CreateType - get structure or union type.
Chris Lattner9c85ba32008-11-10 06:08:34 +0000259llvm::DIType CGDebugInfo::CreateType(const RecordType *Ty,
260 llvm::DICompileUnit Unit) {
Douglas Gregora4c46df2008-12-11 17:59:21 +0000261 RecordDecl *Decl = Ty->getDecl();
Sanjiv Guptaf58c27a2008-06-07 04:46:53 +0000262
Chris Lattner9c85ba32008-11-10 06:08:34 +0000263 unsigned Tag;
264 if (Decl->isStruct())
265 Tag = llvm::dwarf::DW_TAG_structure_type;
266 else if (Decl->isUnion())
267 Tag = llvm::dwarf::DW_TAG_union_type;
268 else {
269 assert(Decl->isClass() && "Unknown RecordType!");
270 Tag = llvm::dwarf::DW_TAG_class_type;
Sanjiv Guptaf58c27a2008-06-07 04:46:53 +0000271 }
272
Sanjiv Gupta507de852008-06-09 10:47:41 +0000273 SourceManager &SM = M->getContext().getSourceManager();
Sanjiv Gupta507de852008-06-09 10:47:41 +0000274
Chris Lattner9c85ba32008-11-10 06:08:34 +0000275 // Get overall information about the record type for the debug info.
Chris Lattner8ec03f52008-11-24 03:54:41 +0000276 std::string Name = Decl->getNameAsString();
Sanjiv Gupta507de852008-06-09 10:47:41 +0000277
Chris Lattner9c85ba32008-11-10 06:08:34 +0000278 llvm::DICompileUnit DefUnit = getOrCreateCompileUnit(Decl->getLocation());
Devang Patel4f6fa232009-04-17 21:35:15 +0000279 PresumedLoc PLoc = SM.getPresumedLoc(Decl->getLocation());
280 unsigned Line = PLoc.isInvalid() ? 0 : PLoc.getLine();
Chris Lattner9c85ba32008-11-10 06:08:34 +0000281
282 // Records and classes and unions can all be recursive. To handle them, we
283 // first generate a debug descriptor for the struct as a forward declaration.
284 // Then (if it is a definition) we go through and get debug info for all of
285 // its members. Finally, we create a descriptor for the complete type (which
286 // may refer to the forward decl if the struct is recursive) and replace all
287 // uses of the forward declaration with the final definition.
288 llvm::DIType FwdDecl =
289 DebugFactory.CreateCompositeType(Tag, Unit, Name, DefUnit, Line, 0, 0, 0, 0,
290 llvm::DIType(), llvm::DIArray());
291
292 // If this is just a forward declaration, return it.
293 if (!Decl->getDefinition(M->getContext()))
294 return FwdDecl;
Sanjiv Gupta507de852008-06-09 10:47:41 +0000295
Chris Lattner9c85ba32008-11-10 06:08:34 +0000296 // Otherwise, insert it into the TypeCache so that recursive uses will find
297 // it.
298 TypeCache[QualType(Ty, 0).getAsOpaquePtr()] = FwdDecl;
299
300 // Convert all the elements.
301 llvm::SmallVector<llvm::DIDescriptor, 16> EltTys;
302
303 const ASTRecordLayout &RL = M->getContext().getASTRecordLayout(Decl);
304
305 unsigned FieldNo = 0;
Douglas Gregor6ab35242009-04-09 21:40:53 +0000306 for (RecordDecl::field_iterator I = Decl->field_begin(M->getContext()),
307 E = Decl->field_end(M->getContext());
Douglas Gregora4c46df2008-12-11 17:59:21 +0000308 I != E; ++I, ++FieldNo) {
Chris Lattner9c85ba32008-11-10 06:08:34 +0000309 FieldDecl *Field = *I;
310 llvm::DIType FieldTy = getOrCreateType(Field->getType(), Unit);
Chris Lattner8ec03f52008-11-24 03:54:41 +0000311
312 std::string FieldName = Field->getNameAsString();
Chris Lattner9c85ba32008-11-10 06:08:34 +0000313
Devang Patelde135022009-04-27 22:40:36 +0000314 // Ignore unnamed fields.
315 if (FieldName.empty())
316 continue;
317
Chris Lattner9c85ba32008-11-10 06:08:34 +0000318 // Get the location for the field.
319 SourceLocation FieldDefLoc = Field->getLocation();
320 llvm::DICompileUnit FieldDefUnit = getOrCreateCompileUnit(FieldDefLoc);
Devang Patel4f6fa232009-04-17 21:35:15 +0000321 PresumedLoc PLoc = SM.getPresumedLoc(FieldDefLoc);
322 unsigned FieldLine = PLoc.isInvalid() ? 0 : PLoc.getLine();
323
Devang Patelec9b5d52009-03-16 23:47:53 +0000324
325 QualType FType = Field->getType();
326 uint64_t FieldSize = 0;
327 unsigned FieldAlign = 0;
328 if (!FType->isIncompleteArrayType()) {
Chris Lattner9c85ba32008-11-10 06:08:34 +0000329
Devang Patelec9b5d52009-03-16 23:47:53 +0000330 // Bit size, align and offset of the type.
331 FieldSize = M->getContext().getTypeSize(FType);
332 Expr *BitWidth = Field->getBitWidth();
333 if (BitWidth)
Eli Friedman9a901bb2009-04-26 19:19:15 +0000334 FieldSize = BitWidth->EvaluateAsInt(M->getContext()).getZExtValue();
Devang Patelec9b5d52009-03-16 23:47:53 +0000335
336 FieldAlign = M->getContext().getTypeAlign(FType);
337 }
338
Chris Lattner9c85ba32008-11-10 06:08:34 +0000339 uint64_t FieldOffset = RL.getFieldOffset(FieldNo);
340
341 // Create a DW_TAG_member node to remember the offset of this field in the
342 // struct. FIXME: This is an absolutely insane way to capture this
343 // information. When we gut debug info, this should be fixed.
344 FieldTy = DebugFactory.CreateDerivedType(llvm::dwarf::DW_TAG_member, Unit,
345 FieldName, FieldDefUnit,
346 FieldLine, FieldSize, FieldAlign,
347 FieldOffset, 0, FieldTy);
Chris Lattner9c85ba32008-11-10 06:08:34 +0000348 EltTys.push_back(FieldTy);
349 }
350
351 llvm::DIArray Elements =
352 DebugFactory.GetOrCreateArray(&EltTys[0], EltTys.size());
353
354 // Bit size, align and offset of the type.
355 uint64_t Size = M->getContext().getTypeSize(Ty);
356 uint64_t Align = M->getContext().getTypeAlign(Ty);
357
358 llvm::DIType RealDecl =
359 DebugFactory.CreateCompositeType(Tag, Unit, Name, DefUnit, Line, Size,
360 Align, 0, 0, llvm::DIType(), Elements);
361
362 // Now that we have a real decl for the struct, replace anything using the
363 // old decl with the new one. This will recursively update the debug info.
364 FwdDecl.getGV()->replaceAllUsesWith(RealDecl.getGV());
365 FwdDecl.getGV()->eraseFromParent();
366
367 return RealDecl;
368}
369
Devang Patel9ca36b62009-02-26 21:10:26 +0000370/// CreateType - get objective-c interface type.
371llvm::DIType CGDebugInfo::CreateType(const ObjCInterfaceType *Ty,
372 llvm::DICompileUnit Unit) {
373 ObjCInterfaceDecl *Decl = Ty->getDecl();
374
375 unsigned Tag = llvm::dwarf::DW_TAG_structure_type;
376 SourceManager &SM = M->getContext().getSourceManager();
377
378 // Get overall information about the record type for the debug info.
379 std::string Name = Decl->getNameAsString();
380
381 llvm::DICompileUnit DefUnit = getOrCreateCompileUnit(Decl->getLocation());
Devang Patel4f6fa232009-04-17 21:35:15 +0000382 PresumedLoc PLoc = SM.getPresumedLoc(Decl->getLocation());
383 unsigned Line = PLoc.isInvalid() ? 0 : PLoc.getLine();
384
Devang Patel9ca36b62009-02-26 21:10:26 +0000385
Chris Lattnerac7c8142009-05-02 01:13:16 +0000386 unsigned RuntimeLang = DefUnit.getRunTimeVersion();
387
Devang Patel9ca36b62009-02-26 21:10:26 +0000388 // To handle recursive interface, we
389 // first generate a debug descriptor for the struct as a forward declaration.
390 // Then (if it is a definition) we go through and get debug info for all of
391 // its members. Finally, we create a descriptor for the complete type (which
392 // may refer to the forward decl if the struct is recursive) and replace all
393 // uses of the forward declaration with the final definition.
394 llvm::DIType FwdDecl =
395 DebugFactory.CreateCompositeType(Tag, Unit, Name, DefUnit, Line, 0, 0, 0, 0,
Chris Lattnerac7c8142009-05-02 01:13:16 +0000396 llvm::DIType(), llvm::DIArray(),
397 RuntimeLang);
Devang Patel9ca36b62009-02-26 21:10:26 +0000398
399 // If this is just a forward declaration, return it.
400 if (Decl->isForwardDecl())
401 return FwdDecl;
402
403 // Otherwise, insert it into the TypeCache so that recursive uses will find
404 // it.
405 TypeCache[QualType(Ty, 0).getAsOpaquePtr()] = FwdDecl;
406
407 // Convert all the elements.
408 llvm::SmallVector<llvm::DIDescriptor, 16> EltTys;
409
Devang Patelfbe899f2009-03-10 21:30:26 +0000410 ObjCInterfaceDecl *SClass = Decl->getSuperClass();
411 if (SClass) {
412 llvm::DIType SClassTy =
413 getOrCreateType(M->getContext().getObjCInterfaceType(SClass), Unit);
414 llvm::DIType InhTag =
415 DebugFactory.CreateDerivedType(llvm::dwarf::DW_TAG_inheritance,
416 Unit, "", Unit, 0, 0, 0,
417 0 /* offset */, 0, SClassTy);
418 EltTys.push_back(InhTag);
419 }
420
Devang Patel9ca36b62009-02-26 21:10:26 +0000421 const ASTRecordLayout &RL = M->getContext().getASTObjCInterfaceLayout(Decl);
422
423 unsigned FieldNo = 0;
424 for (ObjCInterfaceDecl::ivar_iterator I = Decl->ivar_begin(),
425 E = Decl->ivar_end(); I != E; ++I, ++FieldNo) {
426 ObjCIvarDecl *Field = *I;
427 llvm::DIType FieldTy = getOrCreateType(Field->getType(), Unit);
428
429 std::string FieldName = Field->getNameAsString();
430
Devang Patelde135022009-04-27 22:40:36 +0000431 // Ignore unnamed fields.
432 if (FieldName.empty())
433 continue;
434
Devang Patel9ca36b62009-02-26 21:10:26 +0000435 // Get the location for the field.
436 SourceLocation FieldDefLoc = Field->getLocation();
437 llvm::DICompileUnit FieldDefUnit = getOrCreateCompileUnit(FieldDefLoc);
Devang Patel4f6fa232009-04-17 21:35:15 +0000438 PresumedLoc PLoc = SM.getPresumedLoc(FieldDefLoc);
439 unsigned FieldLine = PLoc.isInvalid() ? 0 : PLoc.getLine();
440
Devang Patel99c20eb2009-03-20 18:24:39 +0000441
442 QualType FType = Field->getType();
443 uint64_t FieldSize = 0;
444 unsigned FieldAlign = 0;
Devang Patelc20482b2009-03-19 00:23:53 +0000445
Devang Patel99c20eb2009-03-20 18:24:39 +0000446 if (!FType->isIncompleteArrayType()) {
447
448 // Bit size, align and offset of the type.
449 FieldSize = M->getContext().getTypeSize(FType);
450 Expr *BitWidth = Field->getBitWidth();
451 if (BitWidth)
Eli Friedman9a901bb2009-04-26 19:19:15 +0000452 FieldSize = BitWidth->EvaluateAsInt(M->getContext()).getZExtValue();
453
Devang Patel99c20eb2009-03-20 18:24:39 +0000454 FieldAlign = M->getContext().getTypeAlign(FType);
455 }
456
457 uint64_t FieldOffset = RL.getFieldOffset(FieldNo);
458
Devang Patelc20482b2009-03-19 00:23:53 +0000459 unsigned Flags = 0;
460 if (Field->getAccessControl() == ObjCIvarDecl::Protected)
461 Flags = llvm::DIType::FlagProtected;
462 else if (Field->getAccessControl() == ObjCIvarDecl::Private)
463 Flags = llvm::DIType::FlagPrivate;
464
Devang Patel9ca36b62009-02-26 21:10:26 +0000465 // Create a DW_TAG_member node to remember the offset of this field in the
466 // struct. FIXME: This is an absolutely insane way to capture this
467 // information. When we gut debug info, this should be fixed.
468 FieldTy = DebugFactory.CreateDerivedType(llvm::dwarf::DW_TAG_member, Unit,
469 FieldName, FieldDefUnit,
470 FieldLine, FieldSize, FieldAlign,
Devang Patelc20482b2009-03-19 00:23:53 +0000471 FieldOffset, Flags, FieldTy);
Devang Patel9ca36b62009-02-26 21:10:26 +0000472 EltTys.push_back(FieldTy);
473 }
474
475 llvm::DIArray Elements =
476 DebugFactory.GetOrCreateArray(&EltTys[0], EltTys.size());
477
478 // Bit size, align and offset of the type.
479 uint64_t Size = M->getContext().getTypeSize(Ty);
480 uint64_t Align = M->getContext().getTypeAlign(Ty);
481
482 llvm::DIType RealDecl =
483 DebugFactory.CreateCompositeType(Tag, Unit, Name, DefUnit, Line, Size,
Chris Lattnerac7c8142009-05-02 01:13:16 +0000484 Align, 0, 0, llvm::DIType(), Elements,
485 RuntimeLang);
Devang Patel9ca36b62009-02-26 21:10:26 +0000486
487 // Now that we have a real decl for the struct, replace anything using the
488 // old decl with the new one. This will recursively update the debug info.
489 FwdDecl.getGV()->replaceAllUsesWith(RealDecl.getGV());
490 FwdDecl.getGV()->eraseFromParent();
491
492 return RealDecl;
493}
494
Chris Lattner9c85ba32008-11-10 06:08:34 +0000495llvm::DIType CGDebugInfo::CreateType(const EnumType *Ty,
496 llvm::DICompileUnit Unit) {
497 EnumDecl *Decl = Ty->getDecl();
498
499 llvm::SmallVector<llvm::DIDescriptor, 32> Enumerators;
500
501 // Create DIEnumerator elements for each enumerator.
Douglas Gregor6ab35242009-04-09 21:40:53 +0000502 for (EnumDecl::enumerator_iterator
503 Enum = Decl->enumerator_begin(M->getContext()),
504 EnumEnd = Decl->enumerator_end(M->getContext());
Douglas Gregor44b43212008-12-11 16:49:14 +0000505 Enum != EnumEnd; ++Enum) {
506 Enumerators.push_back(DebugFactory.CreateEnumerator(Enum->getNameAsString(),
507 Enum->getInitVal().getZExtValue()));
Chris Lattner9c85ba32008-11-10 06:08:34 +0000508 }
509
510 // Return a CompositeType for the enum itself.
511 llvm::DIArray EltArray =
512 DebugFactory.GetOrCreateArray(&Enumerators[0], Enumerators.size());
513
Chris Lattner8ec03f52008-11-24 03:54:41 +0000514 std::string EnumName = Decl->getNameAsString();
Chris Lattner9c85ba32008-11-10 06:08:34 +0000515 SourceLocation DefLoc = Decl->getLocation();
516 llvm::DICompileUnit DefUnit = getOrCreateCompileUnit(DefLoc);
517 SourceManager &SM = M->getContext().getSourceManager();
Devang Patel4f6fa232009-04-17 21:35:15 +0000518 PresumedLoc PLoc = SM.getPresumedLoc(DefLoc);
519 unsigned Line = PLoc.isInvalid() ? 0 : PLoc.getLine();
520
Chris Lattner9c85ba32008-11-10 06:08:34 +0000521
522 // Size and align of the type.
523 uint64_t Size = M->getContext().getTypeSize(Ty);
Chris Lattnerf7cf85b2009-01-16 07:36:28 +0000524 unsigned Align = M->getContext().getTypeAlign(Ty);
Chris Lattner9c85ba32008-11-10 06:08:34 +0000525
526 return DebugFactory.CreateCompositeType(llvm::dwarf::DW_TAG_enumeration_type,
527 Unit, EnumName, DefUnit, Line,
528 Size, Align, 0, 0,
529 llvm::DIType(), EltArray);
530}
531
532llvm::DIType CGDebugInfo::CreateType(const TagType *Ty,
533 llvm::DICompileUnit Unit) {
534 if (const RecordType *RT = dyn_cast<RecordType>(Ty))
535 return CreateType(RT, Unit);
536 else if (const EnumType *ET = dyn_cast<EnumType>(Ty))
537 return CreateType(ET, Unit);
538
539 return llvm::DIType();
540}
541
542llvm::DIType CGDebugInfo::CreateType(const ArrayType *Ty,
543 llvm::DICompileUnit Unit) {
Anders Carlsson835c9092009-01-05 01:23:29 +0000544 uint64_t Size;
545 uint64_t Align;
546
547
Nuno Lopes010d5142009-01-28 00:35:17 +0000548 // FIXME: make getTypeAlign() aware of VLAs and incomplete array types
Anders Carlsson835c9092009-01-05 01:23:29 +0000549 if (const VariableArrayType *VAT = dyn_cast<VariableArrayType>(Ty)) {
Anders Carlsson835c9092009-01-05 01:23:29 +0000550 Size = 0;
551 Align =
Nuno Lopes010d5142009-01-28 00:35:17 +0000552 M->getContext().getTypeAlign(M->getContext().getBaseElementType(VAT));
553 } else if (Ty->isIncompleteArrayType()) {
554 Size = 0;
555 Align = M->getContext().getTypeAlign(Ty->getElementType());
Anders Carlsson835c9092009-01-05 01:23:29 +0000556 } else {
557 // Size and align of the whole array, not the element type.
558 Size = M->getContext().getTypeSize(Ty);
559 Align = M->getContext().getTypeAlign(Ty);
560 }
Chris Lattner9c85ba32008-11-10 06:08:34 +0000561
562 // Add the dimensions of the array. FIXME: This loses CV qualifiers from
563 // interior arrays, do we care? Why aren't nested arrays represented the
564 // obvious/recursive way?
565 llvm::SmallVector<llvm::DIDescriptor, 8> Subscripts;
566 QualType EltTy(Ty, 0);
567 while ((Ty = dyn_cast<ArrayType>(EltTy))) {
Sanjiv Gupta507de852008-06-09 10:47:41 +0000568 uint64_t Upper = 0;
Chris Lattner9c85ba32008-11-10 06:08:34 +0000569 if (const ConstantArrayType *CAT = dyn_cast<ConstantArrayType>(Ty))
570 Upper = CAT->getSize().getZExtValue() - 1;
571 // FIXME: Verify this is right for VLAs.
572 Subscripts.push_back(DebugFactory.GetOrCreateSubrange(0, Upper));
573 EltTy = Ty->getElementType();
Sanjiv Gupta507de852008-06-09 10:47:41 +0000574 }
Sanjiv Gupta1c6a38b2008-05-25 05:15:42 +0000575
Chris Lattner9c85ba32008-11-10 06:08:34 +0000576 llvm::DIArray SubscriptArray =
577 DebugFactory.GetOrCreateArray(&Subscripts[0], Subscripts.size());
578
579 return DebugFactory.CreateCompositeType(llvm::dwarf::DW_TAG_array_type,
580 Unit, "", llvm::DICompileUnit(),
581 0, Size, Align, 0, 0,
582 getOrCreateType(EltTy, Unit),
583 SubscriptArray);
584}
585
586
Sanjiv Gupta1c6a38b2008-05-25 05:15:42 +0000587/// getOrCreateType - Get the type from the cache or create a new
588/// one if necessary.
Chris Lattner9c85ba32008-11-10 06:08:34 +0000589llvm::DIType CGDebugInfo::getOrCreateType(QualType Ty,
590 llvm::DICompileUnit Unit) {
591 if (Ty.isNull())
592 return llvm::DIType();
593
Sanjiv Gupta1c6a38b2008-05-25 05:15:42 +0000594 // Check to see if the compile unit already has created this type.
Chris Lattner9c85ba32008-11-10 06:08:34 +0000595 llvm::DIType &Slot = TypeCache[Ty.getAsOpaquePtr()];
596 if (!Slot.isNull()) return Slot;
Sanjiv Gupta1c6a38b2008-05-25 05:15:42 +0000597
Chris Lattner9c85ba32008-11-10 06:08:34 +0000598 // Handle CVR qualifiers, which recursively handles what they refer to.
599 if (Ty.getCVRQualifiers())
600 return Slot = CreateCVRType(Ty, Unit);
Sanjiv Gupta1c6a38b2008-05-25 05:15:42 +0000601
602 // Work out details of type.
Chris Lattner9c85ba32008-11-10 06:08:34 +0000603 switch (Ty->getTypeClass()) {
Douglas Gregor72564e72009-02-26 23:50:07 +0000604#define TYPE(Class, Base)
605#define ABSTRACT_TYPE(Class, Base)
606#define NON_CANONICAL_TYPE(Class, Base)
607#define DEPENDENT_TYPE(Class, Base) case Type::Class:
608#include "clang/AST/TypeNodes.def"
609 assert(false && "Dependent types cannot show up in debug information");
610
Sebastian Redl7c80bd62009-03-16 23:22:08 +0000611 case Type::LValueReference:
612 case Type::RValueReference:
Chris Lattner9c85ba32008-11-10 06:08:34 +0000613 case Type::Vector:
614 case Type::ExtVector:
Fariborz Jahanianf11284a2009-02-17 18:27:45 +0000615 case Type::ExtQual:
Eli Friedman00524e32009-02-27 23:15:07 +0000616 case Type::FixedWidthInt:
617 case Type::BlockPointer:
618 case Type::MemberPointer:
Douglas Gregor7532dc62009-03-30 22:58:21 +0000619 case Type::TemplateSpecialization:
Douglas Gregore4e5b052009-03-19 00:18:19 +0000620 case Type::QualifiedName:
Eli Friedman00524e32009-02-27 23:15:07 +0000621 // Unsupported types
Chris Lattner9c85ba32008-11-10 06:08:34 +0000622 return llvm::DIType();
Chris Lattneraa2b5792009-04-22 06:58:56 +0000623 case Type::ObjCQualifiedId: // Encode id<p> in debug info just like id.
624 return Slot = getOrCreateType(M->getContext().getObjCIdType(), Unit);
625
626 case Type::ObjCQualifiedInterface: // Drop protocols from interface.
Devang Patele7987062009-03-02 17:58:28 +0000627 case Type::ObjCInterface:
Chris Lattneraa2b5792009-04-22 06:58:56 +0000628 return Slot = CreateType(cast<ObjCInterfaceType>(Ty), Unit);
629 case Type::Builtin: return Slot = CreateType(cast<BuiltinType>(Ty), Unit);
Chris Lattnerb7003772009-04-23 06:13:01 +0000630 case Type::Complex: return Slot = CreateType(cast<ComplexType>(Ty), Unit);
Chris Lattneraa2b5792009-04-22 06:58:56 +0000631 case Type::Pointer: return Slot = CreateType(cast<PointerType>(Ty), Unit);
632 case Type::Typedef: return Slot = CreateType(cast<TypedefType>(Ty), Unit);
Douglas Gregor72564e72009-02-26 23:50:07 +0000633 case Type::Record:
Douglas Gregor72564e72009-02-26 23:50:07 +0000634 case Type::Enum:
Chris Lattneraa2b5792009-04-22 06:58:56 +0000635 return Slot = CreateType(cast<TagType>(Ty), Unit);
Chris Lattner9c85ba32008-11-10 06:08:34 +0000636 case Type::FunctionProto:
637 case Type::FunctionNoProto:
Chris Lattner3cc5c402008-11-11 07:01:36 +0000638 return Slot = CreateType(cast<FunctionType>(Ty), Unit);
Chris Lattner9c85ba32008-11-10 06:08:34 +0000639
640 case Type::ConstantArray:
641 case Type::VariableArray:
642 case Type::IncompleteArray:
Chris Lattner3cc5c402008-11-11 07:01:36 +0000643 return Slot = CreateType(cast<ArrayType>(Ty), Unit);
Douglas Gregor72564e72009-02-26 23:50:07 +0000644 case Type::TypeOfExpr:
645 return Slot = getOrCreateType(cast<TypeOfExprType>(Ty)->getUnderlyingExpr()
Chris Lattner3cc5c402008-11-11 07:01:36 +0000646 ->getType(), Unit);
Douglas Gregor72564e72009-02-26 23:50:07 +0000647 case Type::TypeOf:
Chris Lattner3cc5c402008-11-11 07:01:36 +0000648 return Slot = getOrCreateType(cast<TypeOfType>(Ty)->getUnderlyingType(),
649 Unit);
Sanjiv Gupta1c6a38b2008-05-25 05:15:42 +0000650 }
Chris Lattner9c85ba32008-11-10 06:08:34 +0000651
Sanjiv Gupta1c6a38b2008-05-25 05:15:42 +0000652 return Slot;
653}
654
655/// EmitFunctionStart - Constructs the debug code for entering a function -
656/// "llvm.dbg.func.start.".
Chris Lattner9c85ba32008-11-10 06:08:34 +0000657void CGDebugInfo::EmitFunctionStart(const char *Name, QualType ReturnType,
Sanjiv Gupta1c6a38b2008-05-25 05:15:42 +0000658 llvm::Function *Fn,
Chris Lattner9c85ba32008-11-10 06:08:34 +0000659 CGBuilderTy &Builder) {
660 // FIXME: Why is this using CurLoc???
661 llvm::DICompileUnit Unit = getOrCreateCompileUnit(CurLoc);
Sanjiv Gupta1c6a38b2008-05-25 05:15:42 +0000662 SourceManager &SM = M->getContext().getSourceManager();
Devang Patel0f78fea2009-04-08 19:47:04 +0000663 unsigned LineNo = SM.getPresumedLoc(CurLoc).getLine();
Chris Lattner9c85ba32008-11-10 06:08:34 +0000664
665 llvm::DISubprogram SP =
666 DebugFactory.CreateSubprogram(Unit, Name, Name, "", Unit, LineNo,
667 getOrCreateType(ReturnType, Unit),
668 Fn->hasInternalLinkage(), true/*definition*/);
669
670 DebugFactory.InsertSubprogramStart(SP, Builder.GetInsertBlock());
671
Sanjiv Gupta1c6a38b2008-05-25 05:15:42 +0000672 // Push function on region stack.
Chris Lattner9c85ba32008-11-10 06:08:34 +0000673 RegionStack.push_back(SP);
Sanjiv Gupta1c6a38b2008-05-25 05:15:42 +0000674}
675
676
Chris Lattner9c85ba32008-11-10 06:08:34 +0000677void CGDebugInfo::EmitStopPoint(llvm::Function *Fn, CGBuilderTy &Builder) {
Sanjiv Gupta1c6a38b2008-05-25 05:15:42 +0000678 if (CurLoc.isInvalid() || CurLoc.isMacroID()) return;
679
Sanjiv Guptae8b9f5b2008-05-08 08:54:20 +0000680 // Don't bother if things are the same as last time.
681 SourceManager &SM = M->getContext().getSourceManager();
682 if (CurLoc == PrevLoc
Chris Lattner30fc9332009-02-04 01:06:56 +0000683 || (SM.getInstantiationLineNumber(CurLoc) ==
684 SM.getInstantiationLineNumber(PrevLoc)
Sanjiv Guptae8b9f5b2008-05-08 08:54:20 +0000685 && SM.isFromSameFile(CurLoc, PrevLoc)))
686 return;
Sanjiv Guptae8b9f5b2008-05-08 08:54:20 +0000687
688 // Update last state.
689 PrevLoc = CurLoc;
690
691 // Get the appropriate compile unit.
Chris Lattner9c85ba32008-11-10 06:08:34 +0000692 llvm::DICompileUnit Unit = getOrCreateCompileUnit(CurLoc);
Devang Patel0f78fea2009-04-08 19:47:04 +0000693 PresumedLoc PLoc = SM.getPresumedLoc(CurLoc);
694 DebugFactory.InsertStopPoint(Unit, PLoc.getLine(), PLoc.getColumn(),
Chris Lattner9c85ba32008-11-10 06:08:34 +0000695 Builder.GetInsertBlock());
Sanjiv Guptae8b9f5b2008-05-08 08:54:20 +0000696}
697
698/// EmitRegionStart- Constructs the debug code for entering a declarative
699/// region - "llvm.dbg.region.start.".
Chris Lattner9c85ba32008-11-10 06:08:34 +0000700void CGDebugInfo::EmitRegionStart(llvm::Function *Fn, CGBuilderTy &Builder) {
701 llvm::DIDescriptor D;
Daniel Dunbar5273f512008-10-17 01:07:56 +0000702 if (!RegionStack.empty())
Chris Lattner9c85ba32008-11-10 06:08:34 +0000703 D = RegionStack.back();
704 D = DebugFactory.CreateBlock(D);
705 RegionStack.push_back(D);
706 DebugFactory.InsertRegionStart(D, Builder.GetInsertBlock());
Sanjiv Guptae8b9f5b2008-05-08 08:54:20 +0000707}
708
709/// EmitRegionEnd - Constructs the debug code for exiting a declarative
710/// region - "llvm.dbg.region.end."
Chris Lattner9c85ba32008-11-10 06:08:34 +0000711void CGDebugInfo::EmitRegionEnd(llvm::Function *Fn, CGBuilderTy &Builder) {
Daniel Dunbar5273f512008-10-17 01:07:56 +0000712 assert(!RegionStack.empty() && "Region stack mismatch, stack empty!");
713
Sanjiv Guptae8b9f5b2008-05-08 08:54:20 +0000714 // Provide an region stop point.
715 EmitStopPoint(Fn, Builder);
716
Chris Lattner9c85ba32008-11-10 06:08:34 +0000717 DebugFactory.InsertRegionEnd(RegionStack.back(), Builder.GetInsertBlock());
Sanjiv Gupta1c6a38b2008-05-25 05:15:42 +0000718 RegionStack.pop_back();
Sanjiv Guptae8b9f5b2008-05-08 08:54:20 +0000719}
720
Sanjiv Guptacc9b1632008-05-30 10:30:31 +0000721/// EmitDeclare - Emit local variable declaration debug info.
Chris Lattner9c85ba32008-11-10 06:08:34 +0000722void CGDebugInfo::EmitDeclare(const VarDecl *Decl, unsigned Tag,
723 llvm::Value *Storage, CGBuilderTy &Builder) {
Daniel Dunbar5273f512008-10-17 01:07:56 +0000724 assert(!RegionStack.empty() && "Region stack mismatch, stack empty!");
725
Devang Patel07739032009-03-27 23:16:32 +0000726 // Do not emit variable debug information while generating optimized code.
727 // The llvm optimizer and code generator are not yet ready to support
728 // optimized code debugging.
729 const CompileOptions &CO = M->getCompileOpts();
730 if (CO.OptimizationLevel)
731 return;
732
Chris Lattner9c85ba32008-11-10 06:08:34 +0000733 // Get location information.
Sanjiv Guptacc9b1632008-05-30 10:30:31 +0000734 SourceManager &SM = M->getContext().getSourceManager();
Devang Patel4f6fa232009-04-17 21:35:15 +0000735 PresumedLoc PLoc = SM.getPresumedLoc(Decl->getLocation());
736 unsigned Line = PLoc.isInvalid() ? 0 : PLoc.getLine();
Chris Lattner9c85ba32008-11-10 06:08:34 +0000737 llvm::DICompileUnit Unit = getOrCreateCompileUnit(Decl->getLocation());
738
739 // Create the descriptor for the variable.
740 llvm::DIVariable D =
Chris Lattnerd9d22dd2008-11-24 05:29:24 +0000741 DebugFactory.CreateVariable(Tag, RegionStack.back(),Decl->getNameAsString(),
Chris Lattner9c85ba32008-11-10 06:08:34 +0000742 Unit, Line,
743 getOrCreateType(Decl->getType(), Unit));
744 // Insert an llvm.dbg.declare into the current block.
745 DebugFactory.InsertDeclare(Storage, D, Builder.GetInsertBlock());
Sanjiv Guptacc9b1632008-05-30 10:30:31 +0000746}
747
Chris Lattner9c85ba32008-11-10 06:08:34 +0000748void CGDebugInfo::EmitDeclareOfAutoVariable(const VarDecl *Decl,
749 llvm::Value *Storage,
750 CGBuilderTy &Builder) {
751 EmitDeclare(Decl, llvm::dwarf::DW_TAG_auto_variable, Storage, Builder);
752}
753
754/// EmitDeclareOfArgVariable - Emit call to llvm.dbg.declare for an argument
755/// variable declaration.
756void CGDebugInfo::EmitDeclareOfArgVariable(const VarDecl *Decl, llvm::Value *AI,
757 CGBuilderTy &Builder) {
758 EmitDeclare(Decl, llvm::dwarf::DW_TAG_arg_variable, AI, Builder);
759}
760
761
762
Sanjiv Gupta686226b2008-06-05 08:59:10 +0000763/// EmitGlobalVariable - Emit information about a global variable.
Chris Lattner9c85ba32008-11-10 06:08:34 +0000764void CGDebugInfo::EmitGlobalVariable(llvm::GlobalVariable *Var,
765 const VarDecl *Decl) {
Devang Patel07739032009-03-27 23:16:32 +0000766
767 // Do not emit variable debug information while generating optimized code.
768 // The llvm optimizer and code generator are not yet ready to support
769 // optimized code debugging.
770 const CompileOptions &CO = M->getCompileOpts();
771 if (CO.OptimizationLevel)
772 return;
773
Sanjiv Gupta686226b2008-06-05 08:59:10 +0000774 // Create global variable debug descriptor.
Chris Lattner9c85ba32008-11-10 06:08:34 +0000775 llvm::DICompileUnit Unit = getOrCreateCompileUnit(Decl->getLocation());
Sanjiv Gupta686226b2008-06-05 08:59:10 +0000776 SourceManager &SM = M->getContext().getSourceManager();
Devang Patel4f6fa232009-04-17 21:35:15 +0000777 PresumedLoc PLoc = SM.getPresumedLoc(Decl->getLocation());
778 unsigned LineNo = PLoc.isInvalid() ? 0 : PLoc.getLine();
Chris Lattner8ec03f52008-11-24 03:54:41 +0000779
780 std::string Name = Decl->getNameAsString();
Anders Carlsson4d6e8dd2008-11-26 17:40:42 +0000781
782 QualType T = Decl->getType();
783 if (T->isIncompleteArrayType()) {
784
785 // CodeGen turns int[] into int[1] so we'll do the same here.
786 llvm::APSInt ConstVal(32);
787
788 ConstVal = 1;
789 QualType ET = M->getContext().getAsArrayType(T)->getElementType();
790
791 T = M->getContext().getConstantArrayType(ET, ConstVal,
792 ArrayType::Normal, 0);
793 }
794
Chris Lattner9c85ba32008-11-10 06:08:34 +0000795 DebugFactory.CreateGlobalVariable(Unit, Name, Name, "", Unit, LineNo,
Anders Carlsson4d6e8dd2008-11-26 17:40:42 +0000796 getOrCreateType(T, Unit),
Chris Lattner9c85ba32008-11-10 06:08:34 +0000797 Var->hasInternalLinkage(),
798 true/*definition*/, Var);
Sanjiv Gupta686226b2008-06-05 08:59:10 +0000799}
800
Devang Patel9ca36b62009-02-26 21:10:26 +0000801/// EmitGlobalVariable - Emit information about an objective-c interface.
802void CGDebugInfo::EmitGlobalVariable(llvm::GlobalVariable *Var,
803 ObjCInterfaceDecl *Decl) {
804 // Create global variable debug descriptor.
805 llvm::DICompileUnit Unit = getOrCreateCompileUnit(Decl->getLocation());
806 SourceManager &SM = M->getContext().getSourceManager();
Devang Patel4f6fa232009-04-17 21:35:15 +0000807 PresumedLoc PLoc = SM.getPresumedLoc(Decl->getLocation());
808 unsigned LineNo = PLoc.isInvalid() ? 0 : PLoc.getLine();
Devang Patel9ca36b62009-02-26 21:10:26 +0000809
810 std::string Name = Decl->getNameAsString();
811
Chris Lattner03d9f342009-04-01 06:23:52 +0000812 QualType T = M->getContext().getObjCInterfaceType(Decl);
Devang Patel9ca36b62009-02-26 21:10:26 +0000813 if (T->isIncompleteArrayType()) {
814
815 // CodeGen turns int[] into int[1] so we'll do the same here.
816 llvm::APSInt ConstVal(32);
817
818 ConstVal = 1;
819 QualType ET = M->getContext().getAsArrayType(T)->getElementType();
820
821 T = M->getContext().getConstantArrayType(ET, ConstVal,
822 ArrayType::Normal, 0);
823 }
824
825 DebugFactory.CreateGlobalVariable(Unit, Name, Name, "", Unit, LineNo,
826 getOrCreateType(T, Unit),
827 Var->hasInternalLinkage(),
828 true/*definition*/, Var);
829}
830