blob: b35ca7bc316f3134e00c9261c75d054706ab16d0 [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
Devang Patel4f6fa232009-04-17 21:35:15 +0000278 PresumedLoc PLoc = SM.getPresumedLoc(Decl->getLocation());
Chris Lattnerd37d9b52009-05-05 05:16:17 +0000279 llvm::DICompileUnit DefUnit;
280 unsigned Line = 0;
281 if (!PLoc.isInvalid()) {
282 DefUnit = getOrCreateCompileUnit(Decl->getLocation());
283 Line = PLoc.getLine();
284 }
Chris Lattner9c85ba32008-11-10 06:08:34 +0000285
286 // Records and classes and unions can all be recursive. To handle them, we
287 // first generate a debug descriptor for the struct as a forward declaration.
288 // Then (if it is a definition) we go through and get debug info for all of
289 // its members. Finally, we create a descriptor for the complete type (which
290 // may refer to the forward decl if the struct is recursive) and replace all
291 // uses of the forward declaration with the final definition.
292 llvm::DIType FwdDecl =
293 DebugFactory.CreateCompositeType(Tag, Unit, Name, DefUnit, Line, 0, 0, 0, 0,
294 llvm::DIType(), llvm::DIArray());
295
296 // If this is just a forward declaration, return it.
297 if (!Decl->getDefinition(M->getContext()))
298 return FwdDecl;
Sanjiv Gupta507de852008-06-09 10:47:41 +0000299
Chris Lattner9c85ba32008-11-10 06:08:34 +0000300 // Otherwise, insert it into the TypeCache so that recursive uses will find
301 // it.
302 TypeCache[QualType(Ty, 0).getAsOpaquePtr()] = FwdDecl;
303
304 // Convert all the elements.
305 llvm::SmallVector<llvm::DIDescriptor, 16> EltTys;
306
307 const ASTRecordLayout &RL = M->getContext().getASTRecordLayout(Decl);
308
309 unsigned FieldNo = 0;
Douglas Gregor6ab35242009-04-09 21:40:53 +0000310 for (RecordDecl::field_iterator I = Decl->field_begin(M->getContext()),
311 E = Decl->field_end(M->getContext());
Douglas Gregora4c46df2008-12-11 17:59:21 +0000312 I != E; ++I, ++FieldNo) {
Chris Lattner9c85ba32008-11-10 06:08:34 +0000313 FieldDecl *Field = *I;
314 llvm::DIType FieldTy = getOrCreateType(Field->getType(), Unit);
Chris Lattner8ec03f52008-11-24 03:54:41 +0000315
316 std::string FieldName = Field->getNameAsString();
Chris Lattner9c85ba32008-11-10 06:08:34 +0000317
Devang Patelde135022009-04-27 22:40:36 +0000318 // Ignore unnamed fields.
319 if (FieldName.empty())
320 continue;
321
Chris Lattner9c85ba32008-11-10 06:08:34 +0000322 // Get the location for the field.
323 SourceLocation FieldDefLoc = Field->getLocation();
Devang Patel4f6fa232009-04-17 21:35:15 +0000324 PresumedLoc PLoc = SM.getPresumedLoc(FieldDefLoc);
Chris Lattnerd37d9b52009-05-05 05:16:17 +0000325 llvm::DICompileUnit FieldDefUnit;
326 unsigned FieldLine = 0;
327
328 if (!PLoc.isInvalid()) {
329 FieldDefUnit = getOrCreateCompileUnit(FieldDefLoc);
330 FieldLine = PLoc.getLine();
331 }
Devang Patelec9b5d52009-03-16 23:47:53 +0000332
333 QualType FType = Field->getType();
334 uint64_t FieldSize = 0;
335 unsigned FieldAlign = 0;
336 if (!FType->isIncompleteArrayType()) {
Chris Lattner9c85ba32008-11-10 06:08:34 +0000337
Devang Patelec9b5d52009-03-16 23:47:53 +0000338 // Bit size, align and offset of the type.
339 FieldSize = M->getContext().getTypeSize(FType);
340 Expr *BitWidth = Field->getBitWidth();
341 if (BitWidth)
Eli Friedman9a901bb2009-04-26 19:19:15 +0000342 FieldSize = BitWidth->EvaluateAsInt(M->getContext()).getZExtValue();
Devang Patelec9b5d52009-03-16 23:47:53 +0000343
344 FieldAlign = M->getContext().getTypeAlign(FType);
345 }
346
Chris Lattner9c85ba32008-11-10 06:08:34 +0000347 uint64_t FieldOffset = RL.getFieldOffset(FieldNo);
348
349 // Create a DW_TAG_member node to remember the offset of this field in the
350 // struct. FIXME: This is an absolutely insane way to capture this
351 // information. When we gut debug info, this should be fixed.
352 FieldTy = DebugFactory.CreateDerivedType(llvm::dwarf::DW_TAG_member, Unit,
353 FieldName, FieldDefUnit,
354 FieldLine, FieldSize, FieldAlign,
355 FieldOffset, 0, FieldTy);
Chris Lattner9c85ba32008-11-10 06:08:34 +0000356 EltTys.push_back(FieldTy);
357 }
358
359 llvm::DIArray Elements =
360 DebugFactory.GetOrCreateArray(&EltTys[0], EltTys.size());
361
362 // Bit size, align and offset of the type.
363 uint64_t Size = M->getContext().getTypeSize(Ty);
364 uint64_t Align = M->getContext().getTypeAlign(Ty);
365
366 llvm::DIType RealDecl =
367 DebugFactory.CreateCompositeType(Tag, Unit, Name, DefUnit, Line, Size,
368 Align, 0, 0, llvm::DIType(), Elements);
369
370 // Now that we have a real decl for the struct, replace anything using the
371 // old decl with the new one. This will recursively update the debug info.
372 FwdDecl.getGV()->replaceAllUsesWith(RealDecl.getGV());
373 FwdDecl.getGV()->eraseFromParent();
374
375 return RealDecl;
376}
377
Devang Patel9ca36b62009-02-26 21:10:26 +0000378/// CreateType - get objective-c interface type.
379llvm::DIType CGDebugInfo::CreateType(const ObjCInterfaceType *Ty,
380 llvm::DICompileUnit Unit) {
381 ObjCInterfaceDecl *Decl = Ty->getDecl();
382
383 unsigned Tag = llvm::dwarf::DW_TAG_structure_type;
384 SourceManager &SM = M->getContext().getSourceManager();
385
386 // Get overall information about the record type for the debug info.
387 std::string Name = Decl->getNameAsString();
388
389 llvm::DICompileUnit DefUnit = getOrCreateCompileUnit(Decl->getLocation());
Devang Patel4f6fa232009-04-17 21:35:15 +0000390 PresumedLoc PLoc = SM.getPresumedLoc(Decl->getLocation());
391 unsigned Line = PLoc.isInvalid() ? 0 : PLoc.getLine();
392
Devang Patel9ca36b62009-02-26 21:10:26 +0000393
Chris Lattnerac7c8142009-05-02 01:13:16 +0000394 unsigned RuntimeLang = DefUnit.getRunTimeVersion();
395
Devang Patel9ca36b62009-02-26 21:10:26 +0000396 // To handle recursive interface, we
397 // first generate a debug descriptor for the struct as a forward declaration.
398 // Then (if it is a definition) we go through and get debug info for all of
399 // its members. Finally, we create a descriptor for the complete type (which
400 // may refer to the forward decl if the struct is recursive) and replace all
401 // uses of the forward declaration with the final definition.
402 llvm::DIType FwdDecl =
403 DebugFactory.CreateCompositeType(Tag, Unit, Name, DefUnit, Line, 0, 0, 0, 0,
Chris Lattnerac7c8142009-05-02 01:13:16 +0000404 llvm::DIType(), llvm::DIArray(),
405 RuntimeLang);
Devang Patel9ca36b62009-02-26 21:10:26 +0000406
407 // If this is just a forward declaration, return it.
408 if (Decl->isForwardDecl())
409 return FwdDecl;
410
411 // Otherwise, insert it into the TypeCache so that recursive uses will find
412 // it.
413 TypeCache[QualType(Ty, 0).getAsOpaquePtr()] = FwdDecl;
414
415 // Convert all the elements.
416 llvm::SmallVector<llvm::DIDescriptor, 16> EltTys;
417
Devang Patelfbe899f2009-03-10 21:30:26 +0000418 ObjCInterfaceDecl *SClass = Decl->getSuperClass();
419 if (SClass) {
420 llvm::DIType SClassTy =
421 getOrCreateType(M->getContext().getObjCInterfaceType(SClass), Unit);
422 llvm::DIType InhTag =
423 DebugFactory.CreateDerivedType(llvm::dwarf::DW_TAG_inheritance,
Chris Lattner9e55b8a2009-05-05 05:05:36 +0000424 Unit, "", llvm::DICompileUnit(), 0, 0, 0,
Devang Patelfbe899f2009-03-10 21:30:26 +0000425 0 /* offset */, 0, SClassTy);
426 EltTys.push_back(InhTag);
427 }
428
Devang Patel9ca36b62009-02-26 21:10:26 +0000429 const ASTRecordLayout &RL = M->getContext().getASTObjCInterfaceLayout(Decl);
430
431 unsigned FieldNo = 0;
432 for (ObjCInterfaceDecl::ivar_iterator I = Decl->ivar_begin(),
433 E = Decl->ivar_end(); I != E; ++I, ++FieldNo) {
434 ObjCIvarDecl *Field = *I;
435 llvm::DIType FieldTy = getOrCreateType(Field->getType(), Unit);
436
437 std::string FieldName = Field->getNameAsString();
438
Devang Patelde135022009-04-27 22:40:36 +0000439 // Ignore unnamed fields.
440 if (FieldName.empty())
441 continue;
442
Devang Patel9ca36b62009-02-26 21:10:26 +0000443 // Get the location for the field.
444 SourceLocation FieldDefLoc = Field->getLocation();
445 llvm::DICompileUnit FieldDefUnit = getOrCreateCompileUnit(FieldDefLoc);
Devang Patel4f6fa232009-04-17 21:35:15 +0000446 PresumedLoc PLoc = SM.getPresumedLoc(FieldDefLoc);
447 unsigned FieldLine = PLoc.isInvalid() ? 0 : PLoc.getLine();
448
Devang Patel99c20eb2009-03-20 18:24:39 +0000449
450 QualType FType = Field->getType();
451 uint64_t FieldSize = 0;
452 unsigned FieldAlign = 0;
Devang Patelc20482b2009-03-19 00:23:53 +0000453
Devang Patel99c20eb2009-03-20 18:24:39 +0000454 if (!FType->isIncompleteArrayType()) {
455
456 // Bit size, align and offset of the type.
457 FieldSize = M->getContext().getTypeSize(FType);
458 Expr *BitWidth = Field->getBitWidth();
459 if (BitWidth)
Eli Friedman9a901bb2009-04-26 19:19:15 +0000460 FieldSize = BitWidth->EvaluateAsInt(M->getContext()).getZExtValue();
461
Devang Patel99c20eb2009-03-20 18:24:39 +0000462 FieldAlign = M->getContext().getTypeAlign(FType);
463 }
464
465 uint64_t FieldOffset = RL.getFieldOffset(FieldNo);
466
Devang Patelc20482b2009-03-19 00:23:53 +0000467 unsigned Flags = 0;
468 if (Field->getAccessControl() == ObjCIvarDecl::Protected)
469 Flags = llvm::DIType::FlagProtected;
470 else if (Field->getAccessControl() == ObjCIvarDecl::Private)
471 Flags = llvm::DIType::FlagPrivate;
472
Devang Patel9ca36b62009-02-26 21:10:26 +0000473 // Create a DW_TAG_member node to remember the offset of this field in the
474 // struct. FIXME: This is an absolutely insane way to capture this
475 // information. When we gut debug info, this should be fixed.
476 FieldTy = DebugFactory.CreateDerivedType(llvm::dwarf::DW_TAG_member, Unit,
477 FieldName, FieldDefUnit,
478 FieldLine, FieldSize, FieldAlign,
Devang Patelc20482b2009-03-19 00:23:53 +0000479 FieldOffset, Flags, FieldTy);
Devang Patel9ca36b62009-02-26 21:10:26 +0000480 EltTys.push_back(FieldTy);
481 }
482
483 llvm::DIArray Elements =
484 DebugFactory.GetOrCreateArray(&EltTys[0], EltTys.size());
485
486 // Bit size, align and offset of the type.
487 uint64_t Size = M->getContext().getTypeSize(Ty);
488 uint64_t Align = M->getContext().getTypeAlign(Ty);
489
490 llvm::DIType RealDecl =
491 DebugFactory.CreateCompositeType(Tag, Unit, Name, DefUnit, Line, Size,
Chris Lattnerac7c8142009-05-02 01:13:16 +0000492 Align, 0, 0, llvm::DIType(), Elements,
493 RuntimeLang);
Devang Patel9ca36b62009-02-26 21:10:26 +0000494
495 // Now that we have a real decl for the struct, replace anything using the
496 // old decl with the new one. This will recursively update the debug info.
497 FwdDecl.getGV()->replaceAllUsesWith(RealDecl.getGV());
498 FwdDecl.getGV()->eraseFromParent();
499
500 return RealDecl;
501}
502
Chris Lattner9c85ba32008-11-10 06:08:34 +0000503llvm::DIType CGDebugInfo::CreateType(const EnumType *Ty,
504 llvm::DICompileUnit Unit) {
505 EnumDecl *Decl = Ty->getDecl();
506
507 llvm::SmallVector<llvm::DIDescriptor, 32> Enumerators;
508
509 // Create DIEnumerator elements for each enumerator.
Douglas Gregor6ab35242009-04-09 21:40:53 +0000510 for (EnumDecl::enumerator_iterator
511 Enum = Decl->enumerator_begin(M->getContext()),
512 EnumEnd = Decl->enumerator_end(M->getContext());
Douglas Gregor44b43212008-12-11 16:49:14 +0000513 Enum != EnumEnd; ++Enum) {
514 Enumerators.push_back(DebugFactory.CreateEnumerator(Enum->getNameAsString(),
515 Enum->getInitVal().getZExtValue()));
Chris Lattner9c85ba32008-11-10 06:08:34 +0000516 }
517
518 // Return a CompositeType for the enum itself.
519 llvm::DIArray EltArray =
520 DebugFactory.GetOrCreateArray(&Enumerators[0], Enumerators.size());
521
Chris Lattner8ec03f52008-11-24 03:54:41 +0000522 std::string EnumName = Decl->getNameAsString();
Chris Lattner9c85ba32008-11-10 06:08:34 +0000523 SourceLocation DefLoc = Decl->getLocation();
524 llvm::DICompileUnit DefUnit = getOrCreateCompileUnit(DefLoc);
525 SourceManager &SM = M->getContext().getSourceManager();
Devang Patel4f6fa232009-04-17 21:35:15 +0000526 PresumedLoc PLoc = SM.getPresumedLoc(DefLoc);
527 unsigned Line = PLoc.isInvalid() ? 0 : PLoc.getLine();
528
Chris Lattner9c85ba32008-11-10 06:08:34 +0000529
530 // Size and align of the type.
Eli Friedman3189e4b2009-05-04 04:39:55 +0000531 uint64_t Size = 0;
532 unsigned Align = 0;
533 if (!Ty->isIncompleteType()) {
534 Size = M->getContext().getTypeSize(Ty);
535 Align = M->getContext().getTypeAlign(Ty);
536 }
Chris Lattner9c85ba32008-11-10 06:08:34 +0000537
538 return DebugFactory.CreateCompositeType(llvm::dwarf::DW_TAG_enumeration_type,
539 Unit, EnumName, DefUnit, Line,
540 Size, Align, 0, 0,
541 llvm::DIType(), EltArray);
542}
543
544llvm::DIType CGDebugInfo::CreateType(const TagType *Ty,
545 llvm::DICompileUnit Unit) {
546 if (const RecordType *RT = dyn_cast<RecordType>(Ty))
547 return CreateType(RT, Unit);
548 else if (const EnumType *ET = dyn_cast<EnumType>(Ty))
549 return CreateType(ET, Unit);
550
551 return llvm::DIType();
552}
553
554llvm::DIType CGDebugInfo::CreateType(const ArrayType *Ty,
555 llvm::DICompileUnit Unit) {
Anders Carlsson835c9092009-01-05 01:23:29 +0000556 uint64_t Size;
557 uint64_t Align;
558
559
Nuno Lopes010d5142009-01-28 00:35:17 +0000560 // FIXME: make getTypeAlign() aware of VLAs and incomplete array types
Anders Carlsson835c9092009-01-05 01:23:29 +0000561 if (const VariableArrayType *VAT = dyn_cast<VariableArrayType>(Ty)) {
Anders Carlsson835c9092009-01-05 01:23:29 +0000562 Size = 0;
563 Align =
Nuno Lopes010d5142009-01-28 00:35:17 +0000564 M->getContext().getTypeAlign(M->getContext().getBaseElementType(VAT));
565 } else if (Ty->isIncompleteArrayType()) {
566 Size = 0;
567 Align = M->getContext().getTypeAlign(Ty->getElementType());
Anders Carlsson835c9092009-01-05 01:23:29 +0000568 } else {
569 // Size and align of the whole array, not the element type.
570 Size = M->getContext().getTypeSize(Ty);
571 Align = M->getContext().getTypeAlign(Ty);
572 }
Chris Lattner9c85ba32008-11-10 06:08:34 +0000573
574 // Add the dimensions of the array. FIXME: This loses CV qualifiers from
575 // interior arrays, do we care? Why aren't nested arrays represented the
576 // obvious/recursive way?
577 llvm::SmallVector<llvm::DIDescriptor, 8> Subscripts;
578 QualType EltTy(Ty, 0);
579 while ((Ty = dyn_cast<ArrayType>(EltTy))) {
Sanjiv Gupta507de852008-06-09 10:47:41 +0000580 uint64_t Upper = 0;
Chris Lattner9c85ba32008-11-10 06:08:34 +0000581 if (const ConstantArrayType *CAT = dyn_cast<ConstantArrayType>(Ty))
582 Upper = CAT->getSize().getZExtValue() - 1;
583 // FIXME: Verify this is right for VLAs.
584 Subscripts.push_back(DebugFactory.GetOrCreateSubrange(0, Upper));
585 EltTy = Ty->getElementType();
Sanjiv Gupta507de852008-06-09 10:47:41 +0000586 }
Sanjiv Gupta1c6a38b2008-05-25 05:15:42 +0000587
Chris Lattner9c85ba32008-11-10 06:08:34 +0000588 llvm::DIArray SubscriptArray =
589 DebugFactory.GetOrCreateArray(&Subscripts[0], Subscripts.size());
590
591 return DebugFactory.CreateCompositeType(llvm::dwarf::DW_TAG_array_type,
592 Unit, "", llvm::DICompileUnit(),
593 0, Size, Align, 0, 0,
594 getOrCreateType(EltTy, Unit),
595 SubscriptArray);
596}
597
598
Sanjiv Gupta1c6a38b2008-05-25 05:15:42 +0000599/// getOrCreateType - Get the type from the cache or create a new
600/// one if necessary.
Chris Lattner9c85ba32008-11-10 06:08:34 +0000601llvm::DIType CGDebugInfo::getOrCreateType(QualType Ty,
602 llvm::DICompileUnit Unit) {
603 if (Ty.isNull())
604 return llvm::DIType();
605
Sanjiv Gupta1c6a38b2008-05-25 05:15:42 +0000606 // Check to see if the compile unit already has created this type.
Chris Lattner9c85ba32008-11-10 06:08:34 +0000607 llvm::DIType &Slot = TypeCache[Ty.getAsOpaquePtr()];
608 if (!Slot.isNull()) return Slot;
Sanjiv Gupta1c6a38b2008-05-25 05:15:42 +0000609
Chris Lattner9c85ba32008-11-10 06:08:34 +0000610 // Handle CVR qualifiers, which recursively handles what they refer to.
611 if (Ty.getCVRQualifiers())
612 return Slot = CreateCVRType(Ty, Unit);
Sanjiv Gupta1c6a38b2008-05-25 05:15:42 +0000613
614 // Work out details of type.
Chris Lattner9c85ba32008-11-10 06:08:34 +0000615 switch (Ty->getTypeClass()) {
Douglas Gregor72564e72009-02-26 23:50:07 +0000616#define TYPE(Class, Base)
617#define ABSTRACT_TYPE(Class, Base)
618#define NON_CANONICAL_TYPE(Class, Base)
619#define DEPENDENT_TYPE(Class, Base) case Type::Class:
620#include "clang/AST/TypeNodes.def"
621 assert(false && "Dependent types cannot show up in debug information");
622
Sebastian Redl7c80bd62009-03-16 23:22:08 +0000623 case Type::LValueReference:
624 case Type::RValueReference:
Chris Lattner9c85ba32008-11-10 06:08:34 +0000625 case Type::Vector:
626 case Type::ExtVector:
Fariborz Jahanianf11284a2009-02-17 18:27:45 +0000627 case Type::ExtQual:
Eli Friedman00524e32009-02-27 23:15:07 +0000628 case Type::FixedWidthInt:
629 case Type::BlockPointer:
630 case Type::MemberPointer:
Douglas Gregor7532dc62009-03-30 22:58:21 +0000631 case Type::TemplateSpecialization:
Douglas Gregore4e5b052009-03-19 00:18:19 +0000632 case Type::QualifiedName:
Eli Friedman00524e32009-02-27 23:15:07 +0000633 // Unsupported types
Chris Lattner9c85ba32008-11-10 06:08:34 +0000634 return llvm::DIType();
Chris Lattneraa2b5792009-04-22 06:58:56 +0000635 case Type::ObjCQualifiedId: // Encode id<p> in debug info just like id.
636 return Slot = getOrCreateType(M->getContext().getObjCIdType(), Unit);
637
638 case Type::ObjCQualifiedInterface: // Drop protocols from interface.
Devang Patele7987062009-03-02 17:58:28 +0000639 case Type::ObjCInterface:
Chris Lattneraa2b5792009-04-22 06:58:56 +0000640 return Slot = CreateType(cast<ObjCInterfaceType>(Ty), Unit);
641 case Type::Builtin: return Slot = CreateType(cast<BuiltinType>(Ty), Unit);
Chris Lattnerb7003772009-04-23 06:13:01 +0000642 case Type::Complex: return Slot = CreateType(cast<ComplexType>(Ty), Unit);
Chris Lattneraa2b5792009-04-22 06:58:56 +0000643 case Type::Pointer: return Slot = CreateType(cast<PointerType>(Ty), Unit);
644 case Type::Typedef: return Slot = CreateType(cast<TypedefType>(Ty), Unit);
Douglas Gregor72564e72009-02-26 23:50:07 +0000645 case Type::Record:
Douglas Gregor72564e72009-02-26 23:50:07 +0000646 case Type::Enum:
Chris Lattneraa2b5792009-04-22 06:58:56 +0000647 return Slot = CreateType(cast<TagType>(Ty), Unit);
Chris Lattner9c85ba32008-11-10 06:08:34 +0000648 case Type::FunctionProto:
649 case Type::FunctionNoProto:
Chris Lattner3cc5c402008-11-11 07:01:36 +0000650 return Slot = CreateType(cast<FunctionType>(Ty), Unit);
Chris Lattner9c85ba32008-11-10 06:08:34 +0000651
652 case Type::ConstantArray:
653 case Type::VariableArray:
654 case Type::IncompleteArray:
Chris Lattner3cc5c402008-11-11 07:01:36 +0000655 return Slot = CreateType(cast<ArrayType>(Ty), Unit);
Douglas Gregor72564e72009-02-26 23:50:07 +0000656 case Type::TypeOfExpr:
657 return Slot = getOrCreateType(cast<TypeOfExprType>(Ty)->getUnderlyingExpr()
Chris Lattner3cc5c402008-11-11 07:01:36 +0000658 ->getType(), Unit);
Douglas Gregor72564e72009-02-26 23:50:07 +0000659 case Type::TypeOf:
Chris Lattner3cc5c402008-11-11 07:01:36 +0000660 return Slot = getOrCreateType(cast<TypeOfType>(Ty)->getUnderlyingType(),
661 Unit);
Sanjiv Gupta1c6a38b2008-05-25 05:15:42 +0000662 }
Chris Lattner9c85ba32008-11-10 06:08:34 +0000663
Sanjiv Gupta1c6a38b2008-05-25 05:15:42 +0000664 return Slot;
665}
666
667/// EmitFunctionStart - Constructs the debug code for entering a function -
668/// "llvm.dbg.func.start.".
Chris Lattner9c85ba32008-11-10 06:08:34 +0000669void CGDebugInfo::EmitFunctionStart(const char *Name, QualType ReturnType,
Sanjiv Gupta1c6a38b2008-05-25 05:15:42 +0000670 llvm::Function *Fn,
Chris Lattner9c85ba32008-11-10 06:08:34 +0000671 CGBuilderTy &Builder) {
Daniel Dunbarbbd53af2009-05-14 01:45:24 +0000672 const char *LinkageName = Name;
673
Daniel Dunbara2893932009-05-13 23:08:57 +0000674 // Skip the asm prefix if it exists.
Daniel Dunbarbbd53af2009-05-14 01:45:24 +0000675 //
676 // FIXME: This should probably be the unmangled name?
Daniel Dunbara2893932009-05-13 23:08:57 +0000677 if (Name[0] == '\01')
678 ++Name;
679
Chris Lattner9c85ba32008-11-10 06:08:34 +0000680 // FIXME: Why is this using CurLoc???
681 llvm::DICompileUnit Unit = getOrCreateCompileUnit(CurLoc);
Sanjiv Gupta1c6a38b2008-05-25 05:15:42 +0000682 SourceManager &SM = M->getContext().getSourceManager();
Devang Patel0f78fea2009-04-08 19:47:04 +0000683 unsigned LineNo = SM.getPresumedLoc(CurLoc).getLine();
Chris Lattner9c85ba32008-11-10 06:08:34 +0000684
685 llvm::DISubprogram SP =
Daniel Dunbarbbd53af2009-05-14 01:45:24 +0000686 DebugFactory.CreateSubprogram(Unit, Name, Name, LinkageName, Unit, LineNo,
Chris Lattner9c85ba32008-11-10 06:08:34 +0000687 getOrCreateType(ReturnType, Unit),
688 Fn->hasInternalLinkage(), true/*definition*/);
689
690 DebugFactory.InsertSubprogramStart(SP, Builder.GetInsertBlock());
691
Sanjiv Gupta1c6a38b2008-05-25 05:15:42 +0000692 // Push function on region stack.
Chris Lattner9c85ba32008-11-10 06:08:34 +0000693 RegionStack.push_back(SP);
Sanjiv Gupta1c6a38b2008-05-25 05:15:42 +0000694}
695
696
Chris Lattner9c85ba32008-11-10 06:08:34 +0000697void CGDebugInfo::EmitStopPoint(llvm::Function *Fn, CGBuilderTy &Builder) {
Sanjiv Gupta1c6a38b2008-05-25 05:15:42 +0000698 if (CurLoc.isInvalid() || CurLoc.isMacroID()) return;
699
Sanjiv Guptae8b9f5b2008-05-08 08:54:20 +0000700 // Don't bother if things are the same as last time.
701 SourceManager &SM = M->getContext().getSourceManager();
702 if (CurLoc == PrevLoc
Chris Lattner30fc9332009-02-04 01:06:56 +0000703 || (SM.getInstantiationLineNumber(CurLoc) ==
704 SM.getInstantiationLineNumber(PrevLoc)
Sanjiv Guptae8b9f5b2008-05-08 08:54:20 +0000705 && SM.isFromSameFile(CurLoc, PrevLoc)))
706 return;
Sanjiv Guptae8b9f5b2008-05-08 08:54:20 +0000707
708 // Update last state.
709 PrevLoc = CurLoc;
710
711 // Get the appropriate compile unit.
Chris Lattner9c85ba32008-11-10 06:08:34 +0000712 llvm::DICompileUnit Unit = getOrCreateCompileUnit(CurLoc);
Devang Patel0f78fea2009-04-08 19:47:04 +0000713 PresumedLoc PLoc = SM.getPresumedLoc(CurLoc);
714 DebugFactory.InsertStopPoint(Unit, PLoc.getLine(), PLoc.getColumn(),
Chris Lattner9c85ba32008-11-10 06:08:34 +0000715 Builder.GetInsertBlock());
Sanjiv Guptae8b9f5b2008-05-08 08:54:20 +0000716}
717
718/// EmitRegionStart- Constructs the debug code for entering a declarative
719/// region - "llvm.dbg.region.start.".
Chris Lattner9c85ba32008-11-10 06:08:34 +0000720void CGDebugInfo::EmitRegionStart(llvm::Function *Fn, CGBuilderTy &Builder) {
721 llvm::DIDescriptor D;
Daniel Dunbar5273f512008-10-17 01:07:56 +0000722 if (!RegionStack.empty())
Chris Lattner9c85ba32008-11-10 06:08:34 +0000723 D = RegionStack.back();
724 D = DebugFactory.CreateBlock(D);
725 RegionStack.push_back(D);
726 DebugFactory.InsertRegionStart(D, Builder.GetInsertBlock());
Sanjiv Guptae8b9f5b2008-05-08 08:54:20 +0000727}
728
729/// EmitRegionEnd - Constructs the debug code for exiting a declarative
730/// region - "llvm.dbg.region.end."
Chris Lattner9c85ba32008-11-10 06:08:34 +0000731void CGDebugInfo::EmitRegionEnd(llvm::Function *Fn, CGBuilderTy &Builder) {
Daniel Dunbar5273f512008-10-17 01:07:56 +0000732 assert(!RegionStack.empty() && "Region stack mismatch, stack empty!");
733
Sanjiv Guptae8b9f5b2008-05-08 08:54:20 +0000734 // Provide an region stop point.
735 EmitStopPoint(Fn, Builder);
736
Chris Lattner9c85ba32008-11-10 06:08:34 +0000737 DebugFactory.InsertRegionEnd(RegionStack.back(), Builder.GetInsertBlock());
Sanjiv Gupta1c6a38b2008-05-25 05:15:42 +0000738 RegionStack.pop_back();
Sanjiv Guptae8b9f5b2008-05-08 08:54:20 +0000739}
740
Sanjiv Guptacc9b1632008-05-30 10:30:31 +0000741/// EmitDeclare - Emit local variable declaration debug info.
Chris Lattner9c85ba32008-11-10 06:08:34 +0000742void CGDebugInfo::EmitDeclare(const VarDecl *Decl, unsigned Tag,
743 llvm::Value *Storage, CGBuilderTy &Builder) {
Daniel Dunbar5273f512008-10-17 01:07:56 +0000744 assert(!RegionStack.empty() && "Region stack mismatch, stack empty!");
745
Devang Patel07739032009-03-27 23:16:32 +0000746 // Do not emit variable debug information while generating optimized code.
747 // The llvm optimizer and code generator are not yet ready to support
748 // optimized code debugging.
749 const CompileOptions &CO = M->getCompileOpts();
750 if (CO.OptimizationLevel)
751 return;
752
Chris Lattner650cea92009-05-05 04:57:08 +0000753 llvm::DICompileUnit Unit = getOrCreateCompileUnit(Decl->getLocation());
754 llvm::DIType Ty = getOrCreateType(Decl->getType(), Unit);
755
Chris Lattner9c85ba32008-11-10 06:08:34 +0000756 // Get location information.
Sanjiv Guptacc9b1632008-05-30 10:30:31 +0000757 SourceManager &SM = M->getContext().getSourceManager();
Devang Patel4f6fa232009-04-17 21:35:15 +0000758 PresumedLoc PLoc = SM.getPresumedLoc(Decl->getLocation());
Chris Lattner650cea92009-05-05 04:57:08 +0000759 unsigned Line = 0;
760 if (!PLoc.isInvalid())
761 Line = PLoc.getLine();
762 else
763 Unit = llvm::DICompileUnit();
764
Chris Lattner9c85ba32008-11-10 06:08:34 +0000765
766 // Create the descriptor for the variable.
767 llvm::DIVariable D =
Chris Lattnerd9d22dd2008-11-24 05:29:24 +0000768 DebugFactory.CreateVariable(Tag, RegionStack.back(),Decl->getNameAsString(),
Chris Lattner650cea92009-05-05 04:57:08 +0000769 Unit, Line, Ty);
Chris Lattner9c85ba32008-11-10 06:08:34 +0000770 // Insert an llvm.dbg.declare into the current block.
771 DebugFactory.InsertDeclare(Storage, D, Builder.GetInsertBlock());
Sanjiv Guptacc9b1632008-05-30 10:30:31 +0000772}
773
Chris Lattner9c85ba32008-11-10 06:08:34 +0000774void CGDebugInfo::EmitDeclareOfAutoVariable(const VarDecl *Decl,
775 llvm::Value *Storage,
776 CGBuilderTy &Builder) {
777 EmitDeclare(Decl, llvm::dwarf::DW_TAG_auto_variable, Storage, Builder);
778}
779
780/// EmitDeclareOfArgVariable - Emit call to llvm.dbg.declare for an argument
781/// variable declaration.
782void CGDebugInfo::EmitDeclareOfArgVariable(const VarDecl *Decl, llvm::Value *AI,
783 CGBuilderTy &Builder) {
784 EmitDeclare(Decl, llvm::dwarf::DW_TAG_arg_variable, AI, Builder);
785}
786
787
788
Sanjiv Gupta686226b2008-06-05 08:59:10 +0000789/// EmitGlobalVariable - Emit information about a global variable.
Chris Lattner9c85ba32008-11-10 06:08:34 +0000790void CGDebugInfo::EmitGlobalVariable(llvm::GlobalVariable *Var,
791 const VarDecl *Decl) {
Devang Patel07739032009-03-27 23:16:32 +0000792
793 // Do not emit variable debug information while generating optimized code.
794 // The llvm optimizer and code generator are not yet ready to support
795 // optimized code debugging.
796 const CompileOptions &CO = M->getCompileOpts();
797 if (CO.OptimizationLevel)
798 return;
799
Sanjiv Gupta686226b2008-06-05 08:59:10 +0000800 // Create global variable debug descriptor.
Chris Lattner9c85ba32008-11-10 06:08:34 +0000801 llvm::DICompileUnit Unit = getOrCreateCompileUnit(Decl->getLocation());
Sanjiv Gupta686226b2008-06-05 08:59:10 +0000802 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();
Chris Lattner8ec03f52008-11-24 03:54:41 +0000805
806 std::string Name = Decl->getNameAsString();
Anders Carlsson4d6e8dd2008-11-26 17:40:42 +0000807
808 QualType T = Decl->getType();
809 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
Chris Lattner9c85ba32008-11-10 06:08:34 +0000821 DebugFactory.CreateGlobalVariable(Unit, Name, Name, "", Unit, LineNo,
Anders Carlsson4d6e8dd2008-11-26 17:40:42 +0000822 getOrCreateType(T, Unit),
Chris Lattner9c85ba32008-11-10 06:08:34 +0000823 Var->hasInternalLinkage(),
824 true/*definition*/, Var);
Sanjiv Gupta686226b2008-06-05 08:59:10 +0000825}
826
Devang Patel9ca36b62009-02-26 21:10:26 +0000827/// EmitGlobalVariable - Emit information about an objective-c interface.
828void CGDebugInfo::EmitGlobalVariable(llvm::GlobalVariable *Var,
829 ObjCInterfaceDecl *Decl) {
830 // Create global variable debug descriptor.
831 llvm::DICompileUnit Unit = getOrCreateCompileUnit(Decl->getLocation());
832 SourceManager &SM = M->getContext().getSourceManager();
Devang Patel4f6fa232009-04-17 21:35:15 +0000833 PresumedLoc PLoc = SM.getPresumedLoc(Decl->getLocation());
834 unsigned LineNo = PLoc.isInvalid() ? 0 : PLoc.getLine();
Devang Patel9ca36b62009-02-26 21:10:26 +0000835
836 std::string Name = Decl->getNameAsString();
837
Chris Lattner03d9f342009-04-01 06:23:52 +0000838 QualType T = M->getContext().getObjCInterfaceType(Decl);
Devang Patel9ca36b62009-02-26 21:10:26 +0000839 if (T->isIncompleteArrayType()) {
840
841 // CodeGen turns int[] into int[1] so we'll do the same here.
842 llvm::APSInt ConstVal(32);
843
844 ConstVal = 1;
845 QualType ET = M->getContext().getAsArrayType(T)->getElementType();
846
847 T = M->getContext().getConstantArrayType(ET, ConstVal,
848 ArrayType::Normal, 0);
849 }
850
851 DebugFactory.CreateGlobalVariable(Unit, Name, Name, "", Unit, LineNo,
852 getOrCreateType(T, Unit),
853 Var->hasInternalLinkage(),
854 true/*definition*/, Var);
855}
856