blob: 6f3ecdbc06fb902a10625c927d42a24cd7272935 [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 Lattner4c2577a2009-05-02 01:00:04 +0000106 std::string Producer = "clang";// FIXME: clang version.
107 bool isOptimized = false; // FIXME: Encode optimization level.
108 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
386 // To handle recursive interface, we
387 // first generate a debug descriptor for the struct as a forward declaration.
388 // Then (if it is a definition) we go through and get debug info for all of
389 // its members. Finally, we create a descriptor for the complete type (which
390 // may refer to the forward decl if the struct is recursive) and replace all
391 // uses of the forward declaration with the final definition.
392 llvm::DIType FwdDecl =
393 DebugFactory.CreateCompositeType(Tag, Unit, Name, DefUnit, Line, 0, 0, 0, 0,
394 llvm::DIType(), llvm::DIArray());
395
396 // If this is just a forward declaration, return it.
397 if (Decl->isForwardDecl())
398 return FwdDecl;
399
400 // Otherwise, insert it into the TypeCache so that recursive uses will find
401 // it.
402 TypeCache[QualType(Ty, 0).getAsOpaquePtr()] = FwdDecl;
403
404 // Convert all the elements.
405 llvm::SmallVector<llvm::DIDescriptor, 16> EltTys;
406
Devang Patelfbe899f2009-03-10 21:30:26 +0000407 ObjCInterfaceDecl *SClass = Decl->getSuperClass();
408 if (SClass) {
409 llvm::DIType SClassTy =
410 getOrCreateType(M->getContext().getObjCInterfaceType(SClass), Unit);
411 llvm::DIType InhTag =
412 DebugFactory.CreateDerivedType(llvm::dwarf::DW_TAG_inheritance,
413 Unit, "", Unit, 0, 0, 0,
414 0 /* offset */, 0, SClassTy);
415 EltTys.push_back(InhTag);
416 }
417
Devang Patel9ca36b62009-02-26 21:10:26 +0000418 const ASTRecordLayout &RL = M->getContext().getASTObjCInterfaceLayout(Decl);
419
420 unsigned FieldNo = 0;
421 for (ObjCInterfaceDecl::ivar_iterator I = Decl->ivar_begin(),
422 E = Decl->ivar_end(); I != E; ++I, ++FieldNo) {
423 ObjCIvarDecl *Field = *I;
424 llvm::DIType FieldTy = getOrCreateType(Field->getType(), Unit);
425
426 std::string FieldName = Field->getNameAsString();
427
Devang Patelde135022009-04-27 22:40:36 +0000428 // Ignore unnamed fields.
429 if (FieldName.empty())
430 continue;
431
Devang Patel9ca36b62009-02-26 21:10:26 +0000432 // Get the location for the field.
433 SourceLocation FieldDefLoc = Field->getLocation();
434 llvm::DICompileUnit FieldDefUnit = getOrCreateCompileUnit(FieldDefLoc);
Devang Patel4f6fa232009-04-17 21:35:15 +0000435 PresumedLoc PLoc = SM.getPresumedLoc(FieldDefLoc);
436 unsigned FieldLine = PLoc.isInvalid() ? 0 : PLoc.getLine();
437
Devang Patel99c20eb2009-03-20 18:24:39 +0000438
439 QualType FType = Field->getType();
440 uint64_t FieldSize = 0;
441 unsigned FieldAlign = 0;
Devang Patelc20482b2009-03-19 00:23:53 +0000442
Devang Patel99c20eb2009-03-20 18:24:39 +0000443 if (!FType->isIncompleteArrayType()) {
444
445 // Bit size, align and offset of the type.
446 FieldSize = M->getContext().getTypeSize(FType);
447 Expr *BitWidth = Field->getBitWidth();
448 if (BitWidth)
Eli Friedman9a901bb2009-04-26 19:19:15 +0000449 FieldSize = BitWidth->EvaluateAsInt(M->getContext()).getZExtValue();
450
Devang Patel99c20eb2009-03-20 18:24:39 +0000451 FieldAlign = M->getContext().getTypeAlign(FType);
452 }
453
454 uint64_t FieldOffset = RL.getFieldOffset(FieldNo);
455
Devang Patelc20482b2009-03-19 00:23:53 +0000456 unsigned Flags = 0;
457 if (Field->getAccessControl() == ObjCIvarDecl::Protected)
458 Flags = llvm::DIType::FlagProtected;
459 else if (Field->getAccessControl() == ObjCIvarDecl::Private)
460 Flags = llvm::DIType::FlagPrivate;
461
Devang Patel9ca36b62009-02-26 21:10:26 +0000462 // Create a DW_TAG_member node to remember the offset of this field in the
463 // struct. FIXME: This is an absolutely insane way to capture this
464 // information. When we gut debug info, this should be fixed.
465 FieldTy = DebugFactory.CreateDerivedType(llvm::dwarf::DW_TAG_member, Unit,
466 FieldName, FieldDefUnit,
467 FieldLine, FieldSize, FieldAlign,
Devang Patelc20482b2009-03-19 00:23:53 +0000468 FieldOffset, Flags, FieldTy);
Devang Patel9ca36b62009-02-26 21:10:26 +0000469 EltTys.push_back(FieldTy);
470 }
471
472 llvm::DIArray Elements =
473 DebugFactory.GetOrCreateArray(&EltTys[0], EltTys.size());
474
475 // Bit size, align and offset of the type.
476 uint64_t Size = M->getContext().getTypeSize(Ty);
477 uint64_t Align = M->getContext().getTypeAlign(Ty);
478
479 llvm::DIType RealDecl =
480 DebugFactory.CreateCompositeType(Tag, Unit, Name, DefUnit, Line, Size,
481 Align, 0, 0, llvm::DIType(), Elements);
482
483 // Now that we have a real decl for the struct, replace anything using the
484 // old decl with the new one. This will recursively update the debug info.
485 FwdDecl.getGV()->replaceAllUsesWith(RealDecl.getGV());
486 FwdDecl.getGV()->eraseFromParent();
487
488 return RealDecl;
489}
490
Chris Lattner9c85ba32008-11-10 06:08:34 +0000491llvm::DIType CGDebugInfo::CreateType(const EnumType *Ty,
492 llvm::DICompileUnit Unit) {
493 EnumDecl *Decl = Ty->getDecl();
494
495 llvm::SmallVector<llvm::DIDescriptor, 32> Enumerators;
496
497 // Create DIEnumerator elements for each enumerator.
Douglas Gregor6ab35242009-04-09 21:40:53 +0000498 for (EnumDecl::enumerator_iterator
499 Enum = Decl->enumerator_begin(M->getContext()),
500 EnumEnd = Decl->enumerator_end(M->getContext());
Douglas Gregor44b43212008-12-11 16:49:14 +0000501 Enum != EnumEnd; ++Enum) {
502 Enumerators.push_back(DebugFactory.CreateEnumerator(Enum->getNameAsString(),
503 Enum->getInitVal().getZExtValue()));
Chris Lattner9c85ba32008-11-10 06:08:34 +0000504 }
505
506 // Return a CompositeType for the enum itself.
507 llvm::DIArray EltArray =
508 DebugFactory.GetOrCreateArray(&Enumerators[0], Enumerators.size());
509
Chris Lattner8ec03f52008-11-24 03:54:41 +0000510 std::string EnumName = Decl->getNameAsString();
Chris Lattner9c85ba32008-11-10 06:08:34 +0000511 SourceLocation DefLoc = Decl->getLocation();
512 llvm::DICompileUnit DefUnit = getOrCreateCompileUnit(DefLoc);
513 SourceManager &SM = M->getContext().getSourceManager();
Devang Patel4f6fa232009-04-17 21:35:15 +0000514 PresumedLoc PLoc = SM.getPresumedLoc(DefLoc);
515 unsigned Line = PLoc.isInvalid() ? 0 : PLoc.getLine();
516
Chris Lattner9c85ba32008-11-10 06:08:34 +0000517
518 // Size and align of the type.
519 uint64_t Size = M->getContext().getTypeSize(Ty);
Chris Lattnerf7cf85b2009-01-16 07:36:28 +0000520 unsigned Align = M->getContext().getTypeAlign(Ty);
Chris Lattner9c85ba32008-11-10 06:08:34 +0000521
522 return DebugFactory.CreateCompositeType(llvm::dwarf::DW_TAG_enumeration_type,
523 Unit, EnumName, DefUnit, Line,
524 Size, Align, 0, 0,
525 llvm::DIType(), EltArray);
526}
527
528llvm::DIType CGDebugInfo::CreateType(const TagType *Ty,
529 llvm::DICompileUnit Unit) {
530 if (const RecordType *RT = dyn_cast<RecordType>(Ty))
531 return CreateType(RT, Unit);
532 else if (const EnumType *ET = dyn_cast<EnumType>(Ty))
533 return CreateType(ET, Unit);
534
535 return llvm::DIType();
536}
537
538llvm::DIType CGDebugInfo::CreateType(const ArrayType *Ty,
539 llvm::DICompileUnit Unit) {
Anders Carlsson835c9092009-01-05 01:23:29 +0000540 uint64_t Size;
541 uint64_t Align;
542
543
Nuno Lopes010d5142009-01-28 00:35:17 +0000544 // FIXME: make getTypeAlign() aware of VLAs and incomplete array types
Anders Carlsson835c9092009-01-05 01:23:29 +0000545 if (const VariableArrayType *VAT = dyn_cast<VariableArrayType>(Ty)) {
Anders Carlsson835c9092009-01-05 01:23:29 +0000546 Size = 0;
547 Align =
Nuno Lopes010d5142009-01-28 00:35:17 +0000548 M->getContext().getTypeAlign(M->getContext().getBaseElementType(VAT));
549 } else if (Ty->isIncompleteArrayType()) {
550 Size = 0;
551 Align = M->getContext().getTypeAlign(Ty->getElementType());
Anders Carlsson835c9092009-01-05 01:23:29 +0000552 } else {
553 // Size and align of the whole array, not the element type.
554 Size = M->getContext().getTypeSize(Ty);
555 Align = M->getContext().getTypeAlign(Ty);
556 }
Chris Lattner9c85ba32008-11-10 06:08:34 +0000557
558 // Add the dimensions of the array. FIXME: This loses CV qualifiers from
559 // interior arrays, do we care? Why aren't nested arrays represented the
560 // obvious/recursive way?
561 llvm::SmallVector<llvm::DIDescriptor, 8> Subscripts;
562 QualType EltTy(Ty, 0);
563 while ((Ty = dyn_cast<ArrayType>(EltTy))) {
Sanjiv Gupta507de852008-06-09 10:47:41 +0000564 uint64_t Upper = 0;
Chris Lattner9c85ba32008-11-10 06:08:34 +0000565 if (const ConstantArrayType *CAT = dyn_cast<ConstantArrayType>(Ty))
566 Upper = CAT->getSize().getZExtValue() - 1;
567 // FIXME: Verify this is right for VLAs.
568 Subscripts.push_back(DebugFactory.GetOrCreateSubrange(0, Upper));
569 EltTy = Ty->getElementType();
Sanjiv Gupta507de852008-06-09 10:47:41 +0000570 }
Sanjiv Gupta1c6a38b2008-05-25 05:15:42 +0000571
Chris Lattner9c85ba32008-11-10 06:08:34 +0000572 llvm::DIArray SubscriptArray =
573 DebugFactory.GetOrCreateArray(&Subscripts[0], Subscripts.size());
574
575 return DebugFactory.CreateCompositeType(llvm::dwarf::DW_TAG_array_type,
576 Unit, "", llvm::DICompileUnit(),
577 0, Size, Align, 0, 0,
578 getOrCreateType(EltTy, Unit),
579 SubscriptArray);
580}
581
582
Sanjiv Gupta1c6a38b2008-05-25 05:15:42 +0000583/// getOrCreateType - Get the type from the cache or create a new
584/// one if necessary.
Chris Lattner9c85ba32008-11-10 06:08:34 +0000585llvm::DIType CGDebugInfo::getOrCreateType(QualType Ty,
586 llvm::DICompileUnit Unit) {
587 if (Ty.isNull())
588 return llvm::DIType();
589
Sanjiv Gupta1c6a38b2008-05-25 05:15:42 +0000590 // Check to see if the compile unit already has created this type.
Chris Lattner9c85ba32008-11-10 06:08:34 +0000591 llvm::DIType &Slot = TypeCache[Ty.getAsOpaquePtr()];
592 if (!Slot.isNull()) return Slot;
Sanjiv Gupta1c6a38b2008-05-25 05:15:42 +0000593
Chris Lattner9c85ba32008-11-10 06:08:34 +0000594 // Handle CVR qualifiers, which recursively handles what they refer to.
595 if (Ty.getCVRQualifiers())
596 return Slot = CreateCVRType(Ty, Unit);
Sanjiv Gupta1c6a38b2008-05-25 05:15:42 +0000597
598 // Work out details of type.
Chris Lattner9c85ba32008-11-10 06:08:34 +0000599 switch (Ty->getTypeClass()) {
Douglas Gregor72564e72009-02-26 23:50:07 +0000600#define TYPE(Class, Base)
601#define ABSTRACT_TYPE(Class, Base)
602#define NON_CANONICAL_TYPE(Class, Base)
603#define DEPENDENT_TYPE(Class, Base) case Type::Class:
604#include "clang/AST/TypeNodes.def"
605 assert(false && "Dependent types cannot show up in debug information");
606
Sebastian Redl7c80bd62009-03-16 23:22:08 +0000607 case Type::LValueReference:
608 case Type::RValueReference:
Chris Lattner9c85ba32008-11-10 06:08:34 +0000609 case Type::Vector:
610 case Type::ExtVector:
Fariborz Jahanianf11284a2009-02-17 18:27:45 +0000611 case Type::ExtQual:
Eli Friedman00524e32009-02-27 23:15:07 +0000612 case Type::FixedWidthInt:
613 case Type::BlockPointer:
614 case Type::MemberPointer:
Douglas Gregor7532dc62009-03-30 22:58:21 +0000615 case Type::TemplateSpecialization:
Douglas Gregore4e5b052009-03-19 00:18:19 +0000616 case Type::QualifiedName:
Eli Friedman00524e32009-02-27 23:15:07 +0000617 // Unsupported types
Chris Lattner9c85ba32008-11-10 06:08:34 +0000618 return llvm::DIType();
Chris Lattneraa2b5792009-04-22 06:58:56 +0000619 case Type::ObjCQualifiedId: // Encode id<p> in debug info just like id.
620 return Slot = getOrCreateType(M->getContext().getObjCIdType(), Unit);
621
622 case Type::ObjCQualifiedInterface: // Drop protocols from interface.
Devang Patele7987062009-03-02 17:58:28 +0000623 case Type::ObjCInterface:
Chris Lattneraa2b5792009-04-22 06:58:56 +0000624 return Slot = CreateType(cast<ObjCInterfaceType>(Ty), Unit);
625 case Type::Builtin: return Slot = CreateType(cast<BuiltinType>(Ty), Unit);
Chris Lattnerb7003772009-04-23 06:13:01 +0000626 case Type::Complex: return Slot = CreateType(cast<ComplexType>(Ty), Unit);
Chris Lattneraa2b5792009-04-22 06:58:56 +0000627 case Type::Pointer: return Slot = CreateType(cast<PointerType>(Ty), Unit);
628 case Type::Typedef: return Slot = CreateType(cast<TypedefType>(Ty), Unit);
Douglas Gregor72564e72009-02-26 23:50:07 +0000629 case Type::Record:
Douglas Gregor72564e72009-02-26 23:50:07 +0000630 case Type::Enum:
Chris Lattneraa2b5792009-04-22 06:58:56 +0000631 return Slot = CreateType(cast<TagType>(Ty), Unit);
Chris Lattner9c85ba32008-11-10 06:08:34 +0000632 case Type::FunctionProto:
633 case Type::FunctionNoProto:
Chris Lattner3cc5c402008-11-11 07:01:36 +0000634 return Slot = CreateType(cast<FunctionType>(Ty), Unit);
Chris Lattner9c85ba32008-11-10 06:08:34 +0000635
636 case Type::ConstantArray:
637 case Type::VariableArray:
638 case Type::IncompleteArray:
Chris Lattner3cc5c402008-11-11 07:01:36 +0000639 return Slot = CreateType(cast<ArrayType>(Ty), Unit);
Douglas Gregor72564e72009-02-26 23:50:07 +0000640 case Type::TypeOfExpr:
641 return Slot = getOrCreateType(cast<TypeOfExprType>(Ty)->getUnderlyingExpr()
Chris Lattner3cc5c402008-11-11 07:01:36 +0000642 ->getType(), Unit);
Douglas Gregor72564e72009-02-26 23:50:07 +0000643 case Type::TypeOf:
Chris Lattner3cc5c402008-11-11 07:01:36 +0000644 return Slot = getOrCreateType(cast<TypeOfType>(Ty)->getUnderlyingType(),
645 Unit);
Sanjiv Gupta1c6a38b2008-05-25 05:15:42 +0000646 }
Chris Lattner9c85ba32008-11-10 06:08:34 +0000647
Sanjiv Gupta1c6a38b2008-05-25 05:15:42 +0000648 return Slot;
649}
650
651/// EmitFunctionStart - Constructs the debug code for entering a function -
652/// "llvm.dbg.func.start.".
Chris Lattner9c85ba32008-11-10 06:08:34 +0000653void CGDebugInfo::EmitFunctionStart(const char *Name, QualType ReturnType,
Sanjiv Gupta1c6a38b2008-05-25 05:15:42 +0000654 llvm::Function *Fn,
Chris Lattner9c85ba32008-11-10 06:08:34 +0000655 CGBuilderTy &Builder) {
656 // FIXME: Why is this using CurLoc???
657 llvm::DICompileUnit Unit = getOrCreateCompileUnit(CurLoc);
Sanjiv Gupta1c6a38b2008-05-25 05:15:42 +0000658 SourceManager &SM = M->getContext().getSourceManager();
Devang Patel0f78fea2009-04-08 19:47:04 +0000659 unsigned LineNo = SM.getPresumedLoc(CurLoc).getLine();
Chris Lattner9c85ba32008-11-10 06:08:34 +0000660
661 llvm::DISubprogram SP =
662 DebugFactory.CreateSubprogram(Unit, Name, Name, "", Unit, LineNo,
663 getOrCreateType(ReturnType, Unit),
664 Fn->hasInternalLinkage(), true/*definition*/);
665
666 DebugFactory.InsertSubprogramStart(SP, Builder.GetInsertBlock());
667
Sanjiv Gupta1c6a38b2008-05-25 05:15:42 +0000668 // Push function on region stack.
Chris Lattner9c85ba32008-11-10 06:08:34 +0000669 RegionStack.push_back(SP);
Sanjiv Gupta1c6a38b2008-05-25 05:15:42 +0000670}
671
672
Chris Lattner9c85ba32008-11-10 06:08:34 +0000673void CGDebugInfo::EmitStopPoint(llvm::Function *Fn, CGBuilderTy &Builder) {
Sanjiv Gupta1c6a38b2008-05-25 05:15:42 +0000674 if (CurLoc.isInvalid() || CurLoc.isMacroID()) return;
675
Sanjiv Guptae8b9f5b2008-05-08 08:54:20 +0000676 // Don't bother if things are the same as last time.
677 SourceManager &SM = M->getContext().getSourceManager();
678 if (CurLoc == PrevLoc
Chris Lattner30fc9332009-02-04 01:06:56 +0000679 || (SM.getInstantiationLineNumber(CurLoc) ==
680 SM.getInstantiationLineNumber(PrevLoc)
Sanjiv Guptae8b9f5b2008-05-08 08:54:20 +0000681 && SM.isFromSameFile(CurLoc, PrevLoc)))
682 return;
Sanjiv Guptae8b9f5b2008-05-08 08:54:20 +0000683
684 // Update last state.
685 PrevLoc = CurLoc;
686
687 // Get the appropriate compile unit.
Chris Lattner9c85ba32008-11-10 06:08:34 +0000688 llvm::DICompileUnit Unit = getOrCreateCompileUnit(CurLoc);
Devang Patel0f78fea2009-04-08 19:47:04 +0000689 PresumedLoc PLoc = SM.getPresumedLoc(CurLoc);
690 DebugFactory.InsertStopPoint(Unit, PLoc.getLine(), PLoc.getColumn(),
Chris Lattner9c85ba32008-11-10 06:08:34 +0000691 Builder.GetInsertBlock());
Sanjiv Guptae8b9f5b2008-05-08 08:54:20 +0000692}
693
694/// EmitRegionStart- Constructs the debug code for entering a declarative
695/// region - "llvm.dbg.region.start.".
Chris Lattner9c85ba32008-11-10 06:08:34 +0000696void CGDebugInfo::EmitRegionStart(llvm::Function *Fn, CGBuilderTy &Builder) {
697 llvm::DIDescriptor D;
Daniel Dunbar5273f512008-10-17 01:07:56 +0000698 if (!RegionStack.empty())
Chris Lattner9c85ba32008-11-10 06:08:34 +0000699 D = RegionStack.back();
700 D = DebugFactory.CreateBlock(D);
701 RegionStack.push_back(D);
702 DebugFactory.InsertRegionStart(D, Builder.GetInsertBlock());
Sanjiv Guptae8b9f5b2008-05-08 08:54:20 +0000703}
704
705/// EmitRegionEnd - Constructs the debug code for exiting a declarative
706/// region - "llvm.dbg.region.end."
Chris Lattner9c85ba32008-11-10 06:08:34 +0000707void CGDebugInfo::EmitRegionEnd(llvm::Function *Fn, CGBuilderTy &Builder) {
Daniel Dunbar5273f512008-10-17 01:07:56 +0000708 assert(!RegionStack.empty() && "Region stack mismatch, stack empty!");
709
Sanjiv Guptae8b9f5b2008-05-08 08:54:20 +0000710 // Provide an region stop point.
711 EmitStopPoint(Fn, Builder);
712
Chris Lattner9c85ba32008-11-10 06:08:34 +0000713 DebugFactory.InsertRegionEnd(RegionStack.back(), Builder.GetInsertBlock());
Sanjiv Gupta1c6a38b2008-05-25 05:15:42 +0000714 RegionStack.pop_back();
Sanjiv Guptae8b9f5b2008-05-08 08:54:20 +0000715}
716
Sanjiv Guptacc9b1632008-05-30 10:30:31 +0000717/// EmitDeclare - Emit local variable declaration debug info.
Chris Lattner9c85ba32008-11-10 06:08:34 +0000718void CGDebugInfo::EmitDeclare(const VarDecl *Decl, unsigned Tag,
719 llvm::Value *Storage, CGBuilderTy &Builder) {
Daniel Dunbar5273f512008-10-17 01:07:56 +0000720 assert(!RegionStack.empty() && "Region stack mismatch, stack empty!");
721
Devang Patel07739032009-03-27 23:16:32 +0000722 // Do not emit variable debug information while generating optimized code.
723 // The llvm optimizer and code generator are not yet ready to support
724 // optimized code debugging.
725 const CompileOptions &CO = M->getCompileOpts();
726 if (CO.OptimizationLevel)
727 return;
728
Chris Lattner9c85ba32008-11-10 06:08:34 +0000729 // Get location information.
Sanjiv Guptacc9b1632008-05-30 10:30:31 +0000730 SourceManager &SM = M->getContext().getSourceManager();
Devang Patel4f6fa232009-04-17 21:35:15 +0000731 PresumedLoc PLoc = SM.getPresumedLoc(Decl->getLocation());
732 unsigned Line = PLoc.isInvalid() ? 0 : PLoc.getLine();
Chris Lattner9c85ba32008-11-10 06:08:34 +0000733 llvm::DICompileUnit Unit = getOrCreateCompileUnit(Decl->getLocation());
734
735 // Create the descriptor for the variable.
736 llvm::DIVariable D =
Chris Lattnerd9d22dd2008-11-24 05:29:24 +0000737 DebugFactory.CreateVariable(Tag, RegionStack.back(),Decl->getNameAsString(),
Chris Lattner9c85ba32008-11-10 06:08:34 +0000738 Unit, Line,
739 getOrCreateType(Decl->getType(), Unit));
740 // Insert an llvm.dbg.declare into the current block.
741 DebugFactory.InsertDeclare(Storage, D, Builder.GetInsertBlock());
Sanjiv Guptacc9b1632008-05-30 10:30:31 +0000742}
743
Chris Lattner9c85ba32008-11-10 06:08:34 +0000744void CGDebugInfo::EmitDeclareOfAutoVariable(const VarDecl *Decl,
745 llvm::Value *Storage,
746 CGBuilderTy &Builder) {
747 EmitDeclare(Decl, llvm::dwarf::DW_TAG_auto_variable, Storage, Builder);
748}
749
750/// EmitDeclareOfArgVariable - Emit call to llvm.dbg.declare for an argument
751/// variable declaration.
752void CGDebugInfo::EmitDeclareOfArgVariable(const VarDecl *Decl, llvm::Value *AI,
753 CGBuilderTy &Builder) {
754 EmitDeclare(Decl, llvm::dwarf::DW_TAG_arg_variable, AI, Builder);
755}
756
757
758
Sanjiv Gupta686226b2008-06-05 08:59:10 +0000759/// EmitGlobalVariable - Emit information about a global variable.
Chris Lattner9c85ba32008-11-10 06:08:34 +0000760void CGDebugInfo::EmitGlobalVariable(llvm::GlobalVariable *Var,
761 const VarDecl *Decl) {
Devang Patel07739032009-03-27 23:16:32 +0000762
763 // Do not emit variable debug information while generating optimized code.
764 // The llvm optimizer and code generator are not yet ready to support
765 // optimized code debugging.
766 const CompileOptions &CO = M->getCompileOpts();
767 if (CO.OptimizationLevel)
768 return;
769
Sanjiv Gupta686226b2008-06-05 08:59:10 +0000770 // Create global variable debug descriptor.
Chris Lattner9c85ba32008-11-10 06:08:34 +0000771 llvm::DICompileUnit Unit = getOrCreateCompileUnit(Decl->getLocation());
Sanjiv Gupta686226b2008-06-05 08:59:10 +0000772 SourceManager &SM = M->getContext().getSourceManager();
Devang Patel4f6fa232009-04-17 21:35:15 +0000773 PresumedLoc PLoc = SM.getPresumedLoc(Decl->getLocation());
774 unsigned LineNo = PLoc.isInvalid() ? 0 : PLoc.getLine();
Chris Lattner8ec03f52008-11-24 03:54:41 +0000775
776 std::string Name = Decl->getNameAsString();
Anders Carlsson4d6e8dd2008-11-26 17:40:42 +0000777
778 QualType T = Decl->getType();
779 if (T->isIncompleteArrayType()) {
780
781 // CodeGen turns int[] into int[1] so we'll do the same here.
782 llvm::APSInt ConstVal(32);
783
784 ConstVal = 1;
785 QualType ET = M->getContext().getAsArrayType(T)->getElementType();
786
787 T = M->getContext().getConstantArrayType(ET, ConstVal,
788 ArrayType::Normal, 0);
789 }
790
Chris Lattner9c85ba32008-11-10 06:08:34 +0000791 DebugFactory.CreateGlobalVariable(Unit, Name, Name, "", Unit, LineNo,
Anders Carlsson4d6e8dd2008-11-26 17:40:42 +0000792 getOrCreateType(T, Unit),
Chris Lattner9c85ba32008-11-10 06:08:34 +0000793 Var->hasInternalLinkage(),
794 true/*definition*/, Var);
Sanjiv Gupta686226b2008-06-05 08:59:10 +0000795}
796
Devang Patel9ca36b62009-02-26 21:10:26 +0000797/// EmitGlobalVariable - Emit information about an objective-c interface.
798void CGDebugInfo::EmitGlobalVariable(llvm::GlobalVariable *Var,
799 ObjCInterfaceDecl *Decl) {
800 // Create global variable debug descriptor.
801 llvm::DICompileUnit Unit = getOrCreateCompileUnit(Decl->getLocation());
802 SourceManager &SM = M->getContext().getSourceManager();
Devang Patel4f6fa232009-04-17 21:35:15 +0000803 PresumedLoc PLoc = SM.getPresumedLoc(Decl->getLocation());
804 unsigned LineNo = PLoc.isInvalid() ? 0 : PLoc.getLine();
Devang Patel9ca36b62009-02-26 21:10:26 +0000805
806 std::string Name = Decl->getNameAsString();
807
Chris Lattner03d9f342009-04-01 06:23:52 +0000808 QualType T = M->getContext().getObjCInterfaceType(Decl);
Devang Patel9ca36b62009-02-26 21:10:26 +0000809 if (T->isIncompleteArrayType()) {
810
811 // CodeGen turns int[] into int[1] so we'll do the same here.
812 llvm::APSInt ConstVal(32);
813
814 ConstVal = 1;
815 QualType ET = M->getContext().getAsArrayType(T)->getElementType();
816
817 T = M->getContext().getConstantArrayType(ET, ConstVal,
818 ArrayType::Normal, 0);
819 }
820
821 DebugFactory.CreateGlobalVariable(Unit, Name, Name, "", Unit, LineNo,
822 getOrCreateType(T, Unit),
823 Var->hasInternalLinkage(),
824 true/*definition*/, Var);
825}
826