blob: 97153379709de22e2fdcd653e14eeee310eb67d0 [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.
Eli Friedman3189e4b2009-05-04 04:39:55 +0000523 uint64_t Size = 0;
524 unsigned Align = 0;
525 if (!Ty->isIncompleteType()) {
526 Size = M->getContext().getTypeSize(Ty);
527 Align = M->getContext().getTypeAlign(Ty);
528 }
Chris Lattner9c85ba32008-11-10 06:08:34 +0000529
530 return DebugFactory.CreateCompositeType(llvm::dwarf::DW_TAG_enumeration_type,
531 Unit, EnumName, DefUnit, Line,
532 Size, Align, 0, 0,
533 llvm::DIType(), EltArray);
534}
535
536llvm::DIType CGDebugInfo::CreateType(const TagType *Ty,
537 llvm::DICompileUnit Unit) {
538 if (const RecordType *RT = dyn_cast<RecordType>(Ty))
539 return CreateType(RT, Unit);
540 else if (const EnumType *ET = dyn_cast<EnumType>(Ty))
541 return CreateType(ET, Unit);
542
543 return llvm::DIType();
544}
545
546llvm::DIType CGDebugInfo::CreateType(const ArrayType *Ty,
547 llvm::DICompileUnit Unit) {
Anders Carlsson835c9092009-01-05 01:23:29 +0000548 uint64_t Size;
549 uint64_t Align;
550
551
Nuno Lopes010d5142009-01-28 00:35:17 +0000552 // FIXME: make getTypeAlign() aware of VLAs and incomplete array types
Anders Carlsson835c9092009-01-05 01:23:29 +0000553 if (const VariableArrayType *VAT = dyn_cast<VariableArrayType>(Ty)) {
Anders Carlsson835c9092009-01-05 01:23:29 +0000554 Size = 0;
555 Align =
Nuno Lopes010d5142009-01-28 00:35:17 +0000556 M->getContext().getTypeAlign(M->getContext().getBaseElementType(VAT));
557 } else if (Ty->isIncompleteArrayType()) {
558 Size = 0;
559 Align = M->getContext().getTypeAlign(Ty->getElementType());
Anders Carlsson835c9092009-01-05 01:23:29 +0000560 } else {
561 // Size and align of the whole array, not the element type.
562 Size = M->getContext().getTypeSize(Ty);
563 Align = M->getContext().getTypeAlign(Ty);
564 }
Chris Lattner9c85ba32008-11-10 06:08:34 +0000565
566 // Add the dimensions of the array. FIXME: This loses CV qualifiers from
567 // interior arrays, do we care? Why aren't nested arrays represented the
568 // obvious/recursive way?
569 llvm::SmallVector<llvm::DIDescriptor, 8> Subscripts;
570 QualType EltTy(Ty, 0);
571 while ((Ty = dyn_cast<ArrayType>(EltTy))) {
Sanjiv Gupta507de852008-06-09 10:47:41 +0000572 uint64_t Upper = 0;
Chris Lattner9c85ba32008-11-10 06:08:34 +0000573 if (const ConstantArrayType *CAT = dyn_cast<ConstantArrayType>(Ty))
574 Upper = CAT->getSize().getZExtValue() - 1;
575 // FIXME: Verify this is right for VLAs.
576 Subscripts.push_back(DebugFactory.GetOrCreateSubrange(0, Upper));
577 EltTy = Ty->getElementType();
Sanjiv Gupta507de852008-06-09 10:47:41 +0000578 }
Sanjiv Gupta1c6a38b2008-05-25 05:15:42 +0000579
Chris Lattner9c85ba32008-11-10 06:08:34 +0000580 llvm::DIArray SubscriptArray =
581 DebugFactory.GetOrCreateArray(&Subscripts[0], Subscripts.size());
582
583 return DebugFactory.CreateCompositeType(llvm::dwarf::DW_TAG_array_type,
584 Unit, "", llvm::DICompileUnit(),
585 0, Size, Align, 0, 0,
586 getOrCreateType(EltTy, Unit),
587 SubscriptArray);
588}
589
590
Sanjiv Gupta1c6a38b2008-05-25 05:15:42 +0000591/// getOrCreateType - Get the type from the cache or create a new
592/// one if necessary.
Chris Lattner9c85ba32008-11-10 06:08:34 +0000593llvm::DIType CGDebugInfo::getOrCreateType(QualType Ty,
594 llvm::DICompileUnit Unit) {
595 if (Ty.isNull())
596 return llvm::DIType();
597
Sanjiv Gupta1c6a38b2008-05-25 05:15:42 +0000598 // Check to see if the compile unit already has created this type.
Chris Lattner9c85ba32008-11-10 06:08:34 +0000599 llvm::DIType &Slot = TypeCache[Ty.getAsOpaquePtr()];
600 if (!Slot.isNull()) return Slot;
Sanjiv Gupta1c6a38b2008-05-25 05:15:42 +0000601
Chris Lattner9c85ba32008-11-10 06:08:34 +0000602 // Handle CVR qualifiers, which recursively handles what they refer to.
603 if (Ty.getCVRQualifiers())
604 return Slot = CreateCVRType(Ty, Unit);
Sanjiv Gupta1c6a38b2008-05-25 05:15:42 +0000605
606 // Work out details of type.
Chris Lattner9c85ba32008-11-10 06:08:34 +0000607 switch (Ty->getTypeClass()) {
Douglas Gregor72564e72009-02-26 23:50:07 +0000608#define TYPE(Class, Base)
609#define ABSTRACT_TYPE(Class, Base)
610#define NON_CANONICAL_TYPE(Class, Base)
611#define DEPENDENT_TYPE(Class, Base) case Type::Class:
612#include "clang/AST/TypeNodes.def"
613 assert(false && "Dependent types cannot show up in debug information");
614
Sebastian Redl7c80bd62009-03-16 23:22:08 +0000615 case Type::LValueReference:
616 case Type::RValueReference:
Chris Lattner9c85ba32008-11-10 06:08:34 +0000617 case Type::Vector:
618 case Type::ExtVector:
Fariborz Jahanianf11284a2009-02-17 18:27:45 +0000619 case Type::ExtQual:
Eli Friedman00524e32009-02-27 23:15:07 +0000620 case Type::FixedWidthInt:
621 case Type::BlockPointer:
622 case Type::MemberPointer:
Douglas Gregor7532dc62009-03-30 22:58:21 +0000623 case Type::TemplateSpecialization:
Douglas Gregore4e5b052009-03-19 00:18:19 +0000624 case Type::QualifiedName:
Eli Friedman00524e32009-02-27 23:15:07 +0000625 // Unsupported types
Chris Lattner9c85ba32008-11-10 06:08:34 +0000626 return llvm::DIType();
Chris Lattneraa2b5792009-04-22 06:58:56 +0000627 case Type::ObjCQualifiedId: // Encode id<p> in debug info just like id.
628 return Slot = getOrCreateType(M->getContext().getObjCIdType(), Unit);
629
630 case Type::ObjCQualifiedInterface: // Drop protocols from interface.
Devang Patele7987062009-03-02 17:58:28 +0000631 case Type::ObjCInterface:
Chris Lattneraa2b5792009-04-22 06:58:56 +0000632 return Slot = CreateType(cast<ObjCInterfaceType>(Ty), Unit);
633 case Type::Builtin: return Slot = CreateType(cast<BuiltinType>(Ty), Unit);
Chris Lattnerb7003772009-04-23 06:13:01 +0000634 case Type::Complex: return Slot = CreateType(cast<ComplexType>(Ty), Unit);
Chris Lattneraa2b5792009-04-22 06:58:56 +0000635 case Type::Pointer: return Slot = CreateType(cast<PointerType>(Ty), Unit);
636 case Type::Typedef: return Slot = CreateType(cast<TypedefType>(Ty), Unit);
Douglas Gregor72564e72009-02-26 23:50:07 +0000637 case Type::Record:
Douglas Gregor72564e72009-02-26 23:50:07 +0000638 case Type::Enum:
Chris Lattneraa2b5792009-04-22 06:58:56 +0000639 return Slot = CreateType(cast<TagType>(Ty), Unit);
Chris Lattner9c85ba32008-11-10 06:08:34 +0000640 case Type::FunctionProto:
641 case Type::FunctionNoProto:
Chris Lattner3cc5c402008-11-11 07:01:36 +0000642 return Slot = CreateType(cast<FunctionType>(Ty), Unit);
Chris Lattner9c85ba32008-11-10 06:08:34 +0000643
644 case Type::ConstantArray:
645 case Type::VariableArray:
646 case Type::IncompleteArray:
Chris Lattner3cc5c402008-11-11 07:01:36 +0000647 return Slot = CreateType(cast<ArrayType>(Ty), Unit);
Douglas Gregor72564e72009-02-26 23:50:07 +0000648 case Type::TypeOfExpr:
649 return Slot = getOrCreateType(cast<TypeOfExprType>(Ty)->getUnderlyingExpr()
Chris Lattner3cc5c402008-11-11 07:01:36 +0000650 ->getType(), Unit);
Douglas Gregor72564e72009-02-26 23:50:07 +0000651 case Type::TypeOf:
Chris Lattner3cc5c402008-11-11 07:01:36 +0000652 return Slot = getOrCreateType(cast<TypeOfType>(Ty)->getUnderlyingType(),
653 Unit);
Sanjiv Gupta1c6a38b2008-05-25 05:15:42 +0000654 }
Chris Lattner9c85ba32008-11-10 06:08:34 +0000655
Sanjiv Gupta1c6a38b2008-05-25 05:15:42 +0000656 return Slot;
657}
658
659/// EmitFunctionStart - Constructs the debug code for entering a function -
660/// "llvm.dbg.func.start.".
Chris Lattner9c85ba32008-11-10 06:08:34 +0000661void CGDebugInfo::EmitFunctionStart(const char *Name, QualType ReturnType,
Sanjiv Gupta1c6a38b2008-05-25 05:15:42 +0000662 llvm::Function *Fn,
Chris Lattner9c85ba32008-11-10 06:08:34 +0000663 CGBuilderTy &Builder) {
664 // FIXME: Why is this using CurLoc???
665 llvm::DICompileUnit Unit = getOrCreateCompileUnit(CurLoc);
Sanjiv Gupta1c6a38b2008-05-25 05:15:42 +0000666 SourceManager &SM = M->getContext().getSourceManager();
Devang Patel0f78fea2009-04-08 19:47:04 +0000667 unsigned LineNo = SM.getPresumedLoc(CurLoc).getLine();
Chris Lattner9c85ba32008-11-10 06:08:34 +0000668
669 llvm::DISubprogram SP =
670 DebugFactory.CreateSubprogram(Unit, Name, Name, "", Unit, LineNo,
671 getOrCreateType(ReturnType, Unit),
672 Fn->hasInternalLinkage(), true/*definition*/);
673
674 DebugFactory.InsertSubprogramStart(SP, Builder.GetInsertBlock());
675
Sanjiv Gupta1c6a38b2008-05-25 05:15:42 +0000676 // Push function on region stack.
Chris Lattner9c85ba32008-11-10 06:08:34 +0000677 RegionStack.push_back(SP);
Sanjiv Gupta1c6a38b2008-05-25 05:15:42 +0000678}
679
680
Chris Lattner9c85ba32008-11-10 06:08:34 +0000681void CGDebugInfo::EmitStopPoint(llvm::Function *Fn, CGBuilderTy &Builder) {
Sanjiv Gupta1c6a38b2008-05-25 05:15:42 +0000682 if (CurLoc.isInvalid() || CurLoc.isMacroID()) return;
683
Sanjiv Guptae8b9f5b2008-05-08 08:54:20 +0000684 // Don't bother if things are the same as last time.
685 SourceManager &SM = M->getContext().getSourceManager();
686 if (CurLoc == PrevLoc
Chris Lattner30fc9332009-02-04 01:06:56 +0000687 || (SM.getInstantiationLineNumber(CurLoc) ==
688 SM.getInstantiationLineNumber(PrevLoc)
Sanjiv Guptae8b9f5b2008-05-08 08:54:20 +0000689 && SM.isFromSameFile(CurLoc, PrevLoc)))
690 return;
Sanjiv Guptae8b9f5b2008-05-08 08:54:20 +0000691
692 // Update last state.
693 PrevLoc = CurLoc;
694
695 // Get the appropriate compile unit.
Chris Lattner9c85ba32008-11-10 06:08:34 +0000696 llvm::DICompileUnit Unit = getOrCreateCompileUnit(CurLoc);
Devang Patel0f78fea2009-04-08 19:47:04 +0000697 PresumedLoc PLoc = SM.getPresumedLoc(CurLoc);
698 DebugFactory.InsertStopPoint(Unit, PLoc.getLine(), PLoc.getColumn(),
Chris Lattner9c85ba32008-11-10 06:08:34 +0000699 Builder.GetInsertBlock());
Sanjiv Guptae8b9f5b2008-05-08 08:54:20 +0000700}
701
702/// EmitRegionStart- Constructs the debug code for entering a declarative
703/// region - "llvm.dbg.region.start.".
Chris Lattner9c85ba32008-11-10 06:08:34 +0000704void CGDebugInfo::EmitRegionStart(llvm::Function *Fn, CGBuilderTy &Builder) {
705 llvm::DIDescriptor D;
Daniel Dunbar5273f512008-10-17 01:07:56 +0000706 if (!RegionStack.empty())
Chris Lattner9c85ba32008-11-10 06:08:34 +0000707 D = RegionStack.back();
708 D = DebugFactory.CreateBlock(D);
709 RegionStack.push_back(D);
710 DebugFactory.InsertRegionStart(D, Builder.GetInsertBlock());
Sanjiv Guptae8b9f5b2008-05-08 08:54:20 +0000711}
712
713/// EmitRegionEnd - Constructs the debug code for exiting a declarative
714/// region - "llvm.dbg.region.end."
Chris Lattner9c85ba32008-11-10 06:08:34 +0000715void CGDebugInfo::EmitRegionEnd(llvm::Function *Fn, CGBuilderTy &Builder) {
Daniel Dunbar5273f512008-10-17 01:07:56 +0000716 assert(!RegionStack.empty() && "Region stack mismatch, stack empty!");
717
Sanjiv Guptae8b9f5b2008-05-08 08:54:20 +0000718 // Provide an region stop point.
719 EmitStopPoint(Fn, Builder);
720
Chris Lattner9c85ba32008-11-10 06:08:34 +0000721 DebugFactory.InsertRegionEnd(RegionStack.back(), Builder.GetInsertBlock());
Sanjiv Gupta1c6a38b2008-05-25 05:15:42 +0000722 RegionStack.pop_back();
Sanjiv Guptae8b9f5b2008-05-08 08:54:20 +0000723}
724
Sanjiv Guptacc9b1632008-05-30 10:30:31 +0000725/// EmitDeclare - Emit local variable declaration debug info.
Chris Lattner9c85ba32008-11-10 06:08:34 +0000726void CGDebugInfo::EmitDeclare(const VarDecl *Decl, unsigned Tag,
727 llvm::Value *Storage, CGBuilderTy &Builder) {
Daniel Dunbar5273f512008-10-17 01:07:56 +0000728 assert(!RegionStack.empty() && "Region stack mismatch, stack empty!");
729
Devang Patel07739032009-03-27 23:16:32 +0000730 // Do not emit variable debug information while generating optimized code.
731 // The llvm optimizer and code generator are not yet ready to support
732 // optimized code debugging.
733 const CompileOptions &CO = M->getCompileOpts();
734 if (CO.OptimizationLevel)
735 return;
736
Chris Lattner650cea92009-05-05 04:57:08 +0000737 llvm::DICompileUnit Unit = getOrCreateCompileUnit(Decl->getLocation());
738 llvm::DIType Ty = getOrCreateType(Decl->getType(), Unit);
739
Chris Lattner9c85ba32008-11-10 06:08:34 +0000740 // Get location information.
Sanjiv Guptacc9b1632008-05-30 10:30:31 +0000741 SourceManager &SM = M->getContext().getSourceManager();
Devang Patel4f6fa232009-04-17 21:35:15 +0000742 PresumedLoc PLoc = SM.getPresumedLoc(Decl->getLocation());
Chris Lattner650cea92009-05-05 04:57:08 +0000743 unsigned Line = 0;
744 if (!PLoc.isInvalid())
745 Line = PLoc.getLine();
746 else
747 Unit = llvm::DICompileUnit();
748
Chris Lattner9c85ba32008-11-10 06:08:34 +0000749
750 // Create the descriptor for the variable.
751 llvm::DIVariable D =
Chris Lattnerd9d22dd2008-11-24 05:29:24 +0000752 DebugFactory.CreateVariable(Tag, RegionStack.back(),Decl->getNameAsString(),
Chris Lattner650cea92009-05-05 04:57:08 +0000753 Unit, Line, Ty);
Chris Lattner9c85ba32008-11-10 06:08:34 +0000754 // Insert an llvm.dbg.declare into the current block.
755 DebugFactory.InsertDeclare(Storage, D, Builder.GetInsertBlock());
Sanjiv Guptacc9b1632008-05-30 10:30:31 +0000756}
757
Chris Lattner9c85ba32008-11-10 06:08:34 +0000758void CGDebugInfo::EmitDeclareOfAutoVariable(const VarDecl *Decl,
759 llvm::Value *Storage,
760 CGBuilderTy &Builder) {
761 EmitDeclare(Decl, llvm::dwarf::DW_TAG_auto_variable, Storage, Builder);
762}
763
764/// EmitDeclareOfArgVariable - Emit call to llvm.dbg.declare for an argument
765/// variable declaration.
766void CGDebugInfo::EmitDeclareOfArgVariable(const VarDecl *Decl, llvm::Value *AI,
767 CGBuilderTy &Builder) {
768 EmitDeclare(Decl, llvm::dwarf::DW_TAG_arg_variable, AI, Builder);
769}
770
771
772
Sanjiv Gupta686226b2008-06-05 08:59:10 +0000773/// EmitGlobalVariable - Emit information about a global variable.
Chris Lattner9c85ba32008-11-10 06:08:34 +0000774void CGDebugInfo::EmitGlobalVariable(llvm::GlobalVariable *Var,
775 const VarDecl *Decl) {
Devang Patel07739032009-03-27 23:16:32 +0000776
777 // Do not emit variable debug information while generating optimized code.
778 // The llvm optimizer and code generator are not yet ready to support
779 // optimized code debugging.
780 const CompileOptions &CO = M->getCompileOpts();
781 if (CO.OptimizationLevel)
782 return;
783
Sanjiv Gupta686226b2008-06-05 08:59:10 +0000784 // Create global variable debug descriptor.
Chris Lattner9c85ba32008-11-10 06:08:34 +0000785 llvm::DICompileUnit Unit = getOrCreateCompileUnit(Decl->getLocation());
Sanjiv Gupta686226b2008-06-05 08:59:10 +0000786 SourceManager &SM = M->getContext().getSourceManager();
Devang Patel4f6fa232009-04-17 21:35:15 +0000787 PresumedLoc PLoc = SM.getPresumedLoc(Decl->getLocation());
788 unsigned LineNo = PLoc.isInvalid() ? 0 : PLoc.getLine();
Chris Lattner8ec03f52008-11-24 03:54:41 +0000789
790 std::string Name = Decl->getNameAsString();
Anders Carlsson4d6e8dd2008-11-26 17:40:42 +0000791
792 QualType T = Decl->getType();
793 if (T->isIncompleteArrayType()) {
794
795 // CodeGen turns int[] into int[1] so we'll do the same here.
796 llvm::APSInt ConstVal(32);
797
798 ConstVal = 1;
799 QualType ET = M->getContext().getAsArrayType(T)->getElementType();
800
801 T = M->getContext().getConstantArrayType(ET, ConstVal,
802 ArrayType::Normal, 0);
803 }
804
Chris Lattner9c85ba32008-11-10 06:08:34 +0000805 DebugFactory.CreateGlobalVariable(Unit, Name, Name, "", Unit, LineNo,
Anders Carlsson4d6e8dd2008-11-26 17:40:42 +0000806 getOrCreateType(T, Unit),
Chris Lattner9c85ba32008-11-10 06:08:34 +0000807 Var->hasInternalLinkage(),
808 true/*definition*/, Var);
Sanjiv Gupta686226b2008-06-05 08:59:10 +0000809}
810
Devang Patel9ca36b62009-02-26 21:10:26 +0000811/// EmitGlobalVariable - Emit information about an objective-c interface.
812void CGDebugInfo::EmitGlobalVariable(llvm::GlobalVariable *Var,
813 ObjCInterfaceDecl *Decl) {
814 // Create global variable debug descriptor.
815 llvm::DICompileUnit Unit = getOrCreateCompileUnit(Decl->getLocation());
816 SourceManager &SM = M->getContext().getSourceManager();
Devang Patel4f6fa232009-04-17 21:35:15 +0000817 PresumedLoc PLoc = SM.getPresumedLoc(Decl->getLocation());
818 unsigned LineNo = PLoc.isInvalid() ? 0 : PLoc.getLine();
Devang Patel9ca36b62009-02-26 21:10:26 +0000819
820 std::string Name = Decl->getNameAsString();
821
Chris Lattner03d9f342009-04-01 06:23:52 +0000822 QualType T = M->getContext().getObjCInterfaceType(Decl);
Devang Patel9ca36b62009-02-26 21:10:26 +0000823 if (T->isIncompleteArrayType()) {
824
825 // CodeGen turns int[] into int[1] so we'll do the same here.
826 llvm::APSInt ConstVal(32);
827
828 ConstVal = 1;
829 QualType ET = M->getContext().getAsArrayType(T)->getElementType();
830
831 T = M->getContext().getConstantArrayType(ET, ConstVal,
832 ArrayType::Normal, 0);
833 }
834
835 DebugFactory.CreateGlobalVariable(Unit, Name, Name, "", Unit, LineNo,
836 getOrCreateType(T, Unit),
837 Var->hasInternalLinkage(),
838 true/*definition*/, Var);
839}
840