blob: c8d3d634d3fd3cd45484a793e6d3b90c15c57889 [file] [log] [blame]
Sanjiv Guptae8b9f5b2008-05-08 08:54:20 +00001//===--- CGDebugInfo.cpp - Emit Debug Information for a Module ------------===//
2//
3// The LLVM Compiler Infrastructure
4//
5// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
7//
8//===----------------------------------------------------------------------===//
9//
10// This coordinates the debug information generation while generating code.
11//
12//===----------------------------------------------------------------------===//
13
14#include "CGDebugInfo.h"
15#include "CodeGenModule.h"
Sanjiv Gupta1c6a38b2008-05-25 05:15:42 +000016#include "clang/AST/ASTContext.h"
Devang Patel9ca36b62009-02-26 21:10:26 +000017#include "clang/AST/DeclObjC.h"
Chris Lattner3cc5c402008-11-11 07:01:36 +000018#include "clang/AST/Expr.h"
Daniel Dunbare91593e2008-08-11 04:54:23 +000019#include "clang/AST/RecordLayout.h"
Sanjiv Gupta1c6a38b2008-05-25 05:15:42 +000020#include "clang/Basic/SourceManager.h"
21#include "clang/Basic/FileManager.h"
Devang Patel07739032009-03-27 23:16:32 +000022#include "clang/Frontend/CompileOptions.h"
Sanjiv Guptae8b9f5b2008-05-08 08:54:20 +000023#include "llvm/Constants.h"
24#include "llvm/DerivedTypes.h"
25#include "llvm/Instructions.h"
26#include "llvm/Intrinsics.h"
27#include "llvm/Module.h"
Sanjiv Guptae8b9f5b2008-05-08 08:54:20 +000028#include "llvm/ADT/StringExtras.h"
29#include "llvm/ADT/SmallVector.h"
Sanjiv Gupta1c6a38b2008-05-25 05:15:42 +000030#include "llvm/Support/Dwarf.h"
Devang Patel446c6192009-04-17 21:06:59 +000031#include "llvm/System/Path.h"
Sanjiv Gupta1c6a38b2008-05-25 05:15:42 +000032#include "llvm/Target/TargetMachine.h"
Sanjiv Guptae8b9f5b2008-05-08 08:54:20 +000033using namespace clang;
34using namespace clang::CodeGen;
35
36CGDebugInfo::CGDebugInfo(CodeGenModule *m)
Devang Patel87de6492009-04-23 18:09:16 +000037 : M(m), isMainCompileUnitCreated(false), DebugFactory(M->getModule()) {
Sanjiv Guptae8b9f5b2008-05-08 08:54:20 +000038}
39
Chris Lattner9c85ba32008-11-10 06:08:34 +000040CGDebugInfo::~CGDebugInfo() {
Daniel Dunbar66031a52008-10-17 16:15:48 +000041 assert(RegionStack.empty() && "Region stack mismatch, stack not empty!");
Sanjiv Guptae8b9f5b2008-05-08 08:54:20 +000042}
43
Chris Lattner9c85ba32008-11-10 06:08:34 +000044void CGDebugInfo::setLocation(SourceLocation Loc) {
45 if (Loc.isValid())
Chris Lattnerf7cf85b2009-01-16 07:36:28 +000046 CurLoc = M->getContext().getSourceManager().getInstantiationLoc(Loc);
Sanjiv Gupta1c6a38b2008-05-25 05:15:42 +000047}
48
Sanjiv Guptae8b9f5b2008-05-08 08:54:20 +000049/// getOrCreateCompileUnit - Get the compile unit from the cache or create a new
Daniel Dunbar25f51dd2008-10-24 08:38:36 +000050/// one if necessary. This returns null for invalid source locations.
Chris Lattner9c85ba32008-11-10 06:08:34 +000051llvm::DICompileUnit CGDebugInfo::getOrCreateCompileUnit(SourceLocation Loc) {
Devang Patel446c6192009-04-17 21:06:59 +000052 // Get source file information.
53 const char *FileName = "<unknown>";
Devang Patel77820222009-02-24 23:16:03 +000054 SourceManager &SM = M->getContext().getSourceManager();
Chris Lattneradb1a6f2009-04-19 06:50:29 +000055 unsigned FID = 0;
Daniel Dunbar831570c2009-01-22 00:09:25 +000056 if (Loc.isValid()) {
Devang Patel446c6192009-04-17 21:06:59 +000057 PresumedLoc PLoc = SM.getPresumedLoc(Loc);
58 FileName = PLoc.getFilename();
59 FID = PLoc.getIncludeLoc().getRawEncoding();
Daniel Dunbar831570c2009-01-22 00:09:25 +000060 }
61
Sanjiv Guptae8b9f5b2008-05-08 08:54:20 +000062 // See if this compile unit has been used before.
Devang Patel446c6192009-04-17 21:06:59 +000063 llvm::DICompileUnit &Unit = CompileUnitCache[FID];
Chris Lattner9c85ba32008-11-10 06:08:34 +000064 if (!Unit.isNull()) return Unit;
Daniel Dunbarc9abc042009-04-08 05:11:16 +000065
Devang Patel446c6192009-04-17 21:06:59 +000066 // Get absolute path name.
67 llvm::sys::Path AbsFileName(FileName);
68 if (!AbsFileName.isAbsolute()) {
69 llvm::sys::Path tmp = llvm::sys::Path::GetCurrentDirectory();
70 tmp.appendComponent(FileName);
71 AbsFileName = tmp;
72 }
73
Devang Patel87de6492009-04-23 18:09:16 +000074 // See if thie compile unit is representing main source file. Each source
75 // file has corresponding compile unit. There is only one main source
76 // file at a time.
Devang Patel446c6192009-04-17 21:06:59 +000077 bool isMain = false;
78 const LangOptions &LO = M->getLangOptions();
79 const char *MainFileName = LO.getMainFileName();
Devang Patel87de6492009-04-23 18:09:16 +000080 if (isMainCompileUnitCreated == false) {
81 if (MainFileName) {
82 if (!strcmp(AbsFileName.getLast().c_str(), MainFileName))
83 isMain = true;
84 } else {
85 if (Loc.isValid() && SM.isFromMainFile(Loc))
86 isMain = true;
87 }
88 if (isMain)
89 isMainCompileUnitCreated = true;
Devang Patel446c6192009-04-17 21:06:59 +000090 }
Daniel Dunbarc9abc042009-04-08 05:11:16 +000091
Chris Lattner515455a2009-03-25 03:28:08 +000092 unsigned LangTag;
93 if (LO.CPlusPlus) {
94 if (LO.ObjC1)
95 LangTag = llvm::dwarf::DW_LANG_ObjC_plus_plus;
96 else
97 LangTag = llvm::dwarf::DW_LANG_C_plus_plus;
98 } else if (LO.ObjC1) {
Devang Patel8d9aefc2009-03-24 20:35:51 +000099 LangTag = llvm::dwarf::DW_LANG_ObjC;
Chris Lattner515455a2009-03-25 03:28:08 +0000100 } else if (LO.C99) {
Devang Patel8d9aefc2009-03-24 20:35:51 +0000101 LangTag = llvm::dwarf::DW_LANG_C99;
Chris Lattner515455a2009-03-25 03:28:08 +0000102 } else {
103 LangTag = llvm::dwarf::DW_LANG_C89;
104 }
Devang Patel446c6192009-04-17 21:06:59 +0000105
Sanjiv Guptae8b9f5b2008-05-08 08:54:20 +0000106 // Create new compile unit.
Sanjiv Guptae8b9f5b2008-05-08 08:54:20 +0000107 // FIXME: Do not know how to get clang version yet.
Devang Patelc20482b2009-03-19 00:23:53 +0000108 // FIXME: Encode command line options.
109 // FIXME: Encode optimization level.
Devang Patel446c6192009-04-17 21:06:59 +0000110 return Unit = DebugFactory.CreateCompileUnit(LangTag, AbsFileName.getLast(),
111 AbsFileName.getDirname(),
Devang Patel8d9aefc2009-03-24 20:35:51 +0000112 "clang", isMain);
Sanjiv Guptae8b9f5b2008-05-08 08:54:20 +0000113}
114
Devang Patel65e99f22009-02-25 01:36:11 +0000115/// CreateType - Get the Basic type from the cache or create a new
Chris Lattner9c85ba32008-11-10 06:08:34 +0000116/// one if necessary.
117llvm::DIType CGDebugInfo::CreateType(const BuiltinType *BT,
Devang Patel65e99f22009-02-25 01:36:11 +0000118 llvm::DICompileUnit Unit) {
Chris Lattner9c85ba32008-11-10 06:08:34 +0000119 unsigned Encoding = 0;
120 switch (BT->getKind()) {
121 default:
122 case BuiltinType::Void:
123 return llvm::DIType();
124 case BuiltinType::UChar:
125 case BuiltinType::Char_U: Encoding = llvm::dwarf::DW_ATE_unsigned_char; break;
126 case BuiltinType::Char_S:
127 case BuiltinType::SChar: Encoding = llvm::dwarf::DW_ATE_signed_char; break;
128 case BuiltinType::UShort:
129 case BuiltinType::UInt:
130 case BuiltinType::ULong:
131 case BuiltinType::ULongLong: Encoding = llvm::dwarf::DW_ATE_unsigned; break;
132 case BuiltinType::Short:
133 case BuiltinType::Int:
134 case BuiltinType::Long:
135 case BuiltinType::LongLong: Encoding = llvm::dwarf::DW_ATE_signed; break;
136 case BuiltinType::Bool: Encoding = llvm::dwarf::DW_ATE_boolean; break;
137 case BuiltinType::Float:
138 case BuiltinType::Double: Encoding = llvm::dwarf::DW_ATE_float; break;
139 }
140 // Bit size, align and offset of the type.
141 uint64_t Size = M->getContext().getTypeSize(BT);
142 uint64_t Align = M->getContext().getTypeAlign(BT);
143 uint64_t Offset = 0;
144
145 return DebugFactory.CreateBasicType(Unit, BT->getName(), Unit, 0, Size, Align,
146 Offset, /*flags*/ 0, Encoding);
147}
Sanjiv Guptae8b9f5b2008-05-08 08:54:20 +0000148
Chris Lattnerb7003772009-04-23 06:13:01 +0000149llvm::DIType CGDebugInfo::CreateType(const ComplexType *Ty,
150 llvm::DICompileUnit Unit) {
151 // Bit size, align and offset of the type.
152 unsigned Encoding = llvm::dwarf::DW_ATE_complex_float;
153 if (Ty->isComplexIntegerType())
154 Encoding = llvm::dwarf::DW_ATE_lo_user;
155
156 uint64_t Size = M->getContext().getTypeSize(Ty);
157 uint64_t Align = M->getContext().getTypeAlign(Ty);
158 uint64_t Offset = 0;
159
160 return DebugFactory.CreateBasicType(Unit, "complex",
161 Unit, 0, Size, Align,
162 Offset, /*flags*/ 0, Encoding);
163}
164
Sanjiv Guptaf58c27a2008-06-07 04:46:53 +0000165/// getOrCreateCVRType - Get the CVR qualified type from the cache or create
166/// a new one if necessary.
Chris Lattner9c85ba32008-11-10 06:08:34 +0000167llvm::DIType CGDebugInfo::CreateCVRType(QualType Ty, llvm::DICompileUnit Unit) {
168 // We will create one Derived type for one qualifier and recurse to handle any
169 // additional ones.
170 llvm::DIType FromTy;
171 unsigned Tag;
172 if (Ty.isConstQualified()) {
173 Tag = llvm::dwarf::DW_TAG_const_type;
174 Ty.removeConst();
175 FromTy = getOrCreateType(Ty, Unit);
176 } else if (Ty.isVolatileQualified()) {
177 Tag = llvm::dwarf::DW_TAG_volatile_type;
178 Ty.removeVolatile();
179 FromTy = getOrCreateType(Ty, Unit);
180 } else {
181 assert(Ty.isRestrictQualified() && "Unknown type qualifier for debug info");
182 Tag = llvm::dwarf::DW_TAG_restrict_type;
183 Ty.removeRestrict();
184 FromTy = getOrCreateType(Ty, Unit);
Sanjiv Gupta1c6a38b2008-05-25 05:15:42 +0000185 }
Chris Lattner9c85ba32008-11-10 06:08:34 +0000186
Daniel Dunbar3845f862008-10-31 03:54:29 +0000187 // No need to fill in the Name, Line, Size, Alignment, Offset in case of
188 // CVR derived types.
Chris Lattner9c85ba32008-11-10 06:08:34 +0000189 return DebugFactory.CreateDerivedType(Tag, Unit, "", llvm::DICompileUnit(),
190 0, 0, 0, 0, 0, FromTy);
Sanjiv Gupta1c6a38b2008-05-25 05:15:42 +0000191}
192
Chris Lattner9c85ba32008-11-10 06:08:34 +0000193llvm::DIType CGDebugInfo::CreateType(const PointerType *Ty,
194 llvm::DICompileUnit Unit) {
195 llvm::DIType EltTy = getOrCreateType(Ty->getPointeeType(), Unit);
Sanjiv Gupta1c6a38b2008-05-25 05:15:42 +0000196
Sanjiv Gupta1c6a38b2008-05-25 05:15:42 +0000197 // Bit size, align and offset of the type.
Chris Lattner9c85ba32008-11-10 06:08:34 +0000198 uint64_t Size = M->getContext().getTypeSize(Ty);
199 uint64_t Align = M->getContext().getTypeAlign(Ty);
Sanjiv Gupta1c6a38b2008-05-25 05:15:42 +0000200
Chris Lattner9c85ba32008-11-10 06:08:34 +0000201 return DebugFactory.CreateDerivedType(llvm::dwarf::DW_TAG_pointer_type, Unit,
202 "", llvm::DICompileUnit(),
203 0, Size, Align, 0, 0, EltTy);
Sanjiv Gupta1c6a38b2008-05-25 05:15:42 +0000204}
205
Chris Lattner9c85ba32008-11-10 06:08:34 +0000206llvm::DIType CGDebugInfo::CreateType(const TypedefType *Ty,
207 llvm::DICompileUnit Unit) {
208 // Typedefs are derived from some other type. If we have a typedef of a
209 // typedef, make sure to emit the whole chain.
210 llvm::DIType Src = getOrCreateType(Ty->getDecl()->getUnderlyingType(), Unit);
211
212 // We don't set size information, but do specify where the typedef was
213 // declared.
Chris Lattner8ec03f52008-11-24 03:54:41 +0000214 std::string TyName = Ty->getDecl()->getNameAsString();
Chris Lattner9c85ba32008-11-10 06:08:34 +0000215 SourceLocation DefLoc = Ty->getDecl()->getLocation();
216 llvm::DICompileUnit DefUnit = getOrCreateCompileUnit(DefLoc);
Sanjiv Gupta1c6a38b2008-05-25 05:15:42 +0000217
Sanjiv Gupta1c6a38b2008-05-25 05:15:42 +0000218 SourceManager &SM = M->getContext().getSourceManager();
Devang Patel4f6fa232009-04-17 21:35:15 +0000219 PresumedLoc PLoc = SM.getPresumedLoc(DefLoc);
220 unsigned Line = PLoc.isInvalid() ? 0 : PLoc.getLine();
Sanjiv Gupta1c6a38b2008-05-25 05:15:42 +0000221
Chris Lattner9c85ba32008-11-10 06:08:34 +0000222 return DebugFactory.CreateDerivedType(llvm::dwarf::DW_TAG_typedef, Unit,
223 TyName, DefUnit, Line, 0, 0, 0, 0, Src);
Sanjiv Gupta1c6a38b2008-05-25 05:15:42 +0000224}
225
Chris Lattner9c85ba32008-11-10 06:08:34 +0000226llvm::DIType CGDebugInfo::CreateType(const FunctionType *Ty,
227 llvm::DICompileUnit Unit) {
228 llvm::SmallVector<llvm::DIDescriptor, 16> EltTys;
Sanjiv Gupta1c6a38b2008-05-25 05:15:42 +0000229
Chris Lattner9c85ba32008-11-10 06:08:34 +0000230 // Add the result type at least.
231 EltTys.push_back(getOrCreateType(Ty->getResultType(), Unit));
232
233 // Set up remainder of arguments if there is a prototype.
234 // FIXME: IF NOT, HOW IS THIS REPRESENTED? llvm-gcc doesn't represent '...'!
Douglas Gregor72564e72009-02-26 23:50:07 +0000235 if (const FunctionProtoType *FTP = dyn_cast<FunctionProtoType>(Ty)) {
Chris Lattner9c85ba32008-11-10 06:08:34 +0000236 for (unsigned i = 0, e = FTP->getNumArgs(); i != e; ++i)
237 EltTys.push_back(getOrCreateType(FTP->getArgType(i), Unit));
238 } else {
239 // FIXME: Handle () case in C. llvm-gcc doesn't do it either.
Sanjiv Gupta1c6a38b2008-05-25 05:15:42 +0000240 }
241
Chris Lattner9c85ba32008-11-10 06:08:34 +0000242 llvm::DIArray EltTypeArray =
243 DebugFactory.GetOrCreateArray(&EltTys[0], EltTys.size());
244
245 return DebugFactory.CreateCompositeType(llvm::dwarf::DW_TAG_subroutine_type,
246 Unit, "", llvm::DICompileUnit(),
247 0, 0, 0, 0, 0,
248 llvm::DIType(), EltTypeArray);
Sanjiv Gupta1c6a38b2008-05-25 05:15:42 +0000249}
250
Devang Patel65e99f22009-02-25 01:36:11 +0000251/// CreateType - get structure or union type.
Chris Lattner9c85ba32008-11-10 06:08:34 +0000252llvm::DIType CGDebugInfo::CreateType(const RecordType *Ty,
253 llvm::DICompileUnit Unit) {
Douglas Gregora4c46df2008-12-11 17:59:21 +0000254 RecordDecl *Decl = Ty->getDecl();
Sanjiv Guptaf58c27a2008-06-07 04:46:53 +0000255
Chris Lattner9c85ba32008-11-10 06:08:34 +0000256 unsigned Tag;
257 if (Decl->isStruct())
258 Tag = llvm::dwarf::DW_TAG_structure_type;
259 else if (Decl->isUnion())
260 Tag = llvm::dwarf::DW_TAG_union_type;
261 else {
262 assert(Decl->isClass() && "Unknown RecordType!");
263 Tag = llvm::dwarf::DW_TAG_class_type;
Sanjiv Guptaf58c27a2008-06-07 04:46:53 +0000264 }
265
Sanjiv Gupta507de852008-06-09 10:47:41 +0000266 SourceManager &SM = M->getContext().getSourceManager();
Sanjiv Gupta507de852008-06-09 10:47:41 +0000267
Chris Lattner9c85ba32008-11-10 06:08:34 +0000268 // Get overall information about the record type for the debug info.
Chris Lattner8ec03f52008-11-24 03:54:41 +0000269 std::string Name = Decl->getNameAsString();
Sanjiv Gupta507de852008-06-09 10:47:41 +0000270
Chris Lattner9c85ba32008-11-10 06:08:34 +0000271 llvm::DICompileUnit DefUnit = getOrCreateCompileUnit(Decl->getLocation());
Devang Patel4f6fa232009-04-17 21:35:15 +0000272 PresumedLoc PLoc = SM.getPresumedLoc(Decl->getLocation());
273 unsigned Line = PLoc.isInvalid() ? 0 : PLoc.getLine();
Chris Lattner9c85ba32008-11-10 06:08:34 +0000274
275 // Records and classes and unions can all be recursive. To handle them, we
276 // first generate a debug descriptor for the struct as a forward declaration.
277 // Then (if it is a definition) we go through and get debug info for all of
278 // its members. Finally, we create a descriptor for the complete type (which
279 // may refer to the forward decl if the struct is recursive) and replace all
280 // uses of the forward declaration with the final definition.
281 llvm::DIType FwdDecl =
282 DebugFactory.CreateCompositeType(Tag, Unit, Name, DefUnit, Line, 0, 0, 0, 0,
283 llvm::DIType(), llvm::DIArray());
284
285 // If this is just a forward declaration, return it.
286 if (!Decl->getDefinition(M->getContext()))
287 return FwdDecl;
Sanjiv Gupta507de852008-06-09 10:47:41 +0000288
Chris Lattner9c85ba32008-11-10 06:08:34 +0000289 // Otherwise, insert it into the TypeCache so that recursive uses will find
290 // it.
291 TypeCache[QualType(Ty, 0).getAsOpaquePtr()] = FwdDecl;
292
293 // Convert all the elements.
294 llvm::SmallVector<llvm::DIDescriptor, 16> EltTys;
295
296 const ASTRecordLayout &RL = M->getContext().getASTRecordLayout(Decl);
297
298 unsigned FieldNo = 0;
Douglas Gregor6ab35242009-04-09 21:40:53 +0000299 for (RecordDecl::field_iterator I = Decl->field_begin(M->getContext()),
300 E = Decl->field_end(M->getContext());
Douglas Gregora4c46df2008-12-11 17:59:21 +0000301 I != E; ++I, ++FieldNo) {
Chris Lattner9c85ba32008-11-10 06:08:34 +0000302 FieldDecl *Field = *I;
303 llvm::DIType FieldTy = getOrCreateType(Field->getType(), Unit);
Chris Lattner8ec03f52008-11-24 03:54:41 +0000304
305 std::string FieldName = Field->getNameAsString();
Chris Lattner9c85ba32008-11-10 06:08:34 +0000306
Devang Patelde135022009-04-27 22:40:36 +0000307 // Ignore unnamed fields.
308 if (FieldName.empty())
309 continue;
310
Chris Lattner9c85ba32008-11-10 06:08:34 +0000311 // Get the location for the field.
312 SourceLocation FieldDefLoc = Field->getLocation();
313 llvm::DICompileUnit FieldDefUnit = getOrCreateCompileUnit(FieldDefLoc);
Devang Patel4f6fa232009-04-17 21:35:15 +0000314 PresumedLoc PLoc = SM.getPresumedLoc(FieldDefLoc);
315 unsigned FieldLine = PLoc.isInvalid() ? 0 : PLoc.getLine();
316
Devang Patelec9b5d52009-03-16 23:47:53 +0000317
318 QualType FType = Field->getType();
319 uint64_t FieldSize = 0;
320 unsigned FieldAlign = 0;
321 if (!FType->isIncompleteArrayType()) {
Chris Lattner9c85ba32008-11-10 06:08:34 +0000322
Devang Patelec9b5d52009-03-16 23:47:53 +0000323 // Bit size, align and offset of the type.
324 FieldSize = M->getContext().getTypeSize(FType);
325 Expr *BitWidth = Field->getBitWidth();
326 if (BitWidth)
Eli Friedman9a901bb2009-04-26 19:19:15 +0000327 FieldSize = BitWidth->EvaluateAsInt(M->getContext()).getZExtValue();
Devang Patelec9b5d52009-03-16 23:47:53 +0000328
329 FieldAlign = M->getContext().getTypeAlign(FType);
330 }
331
Chris Lattner9c85ba32008-11-10 06:08:34 +0000332 uint64_t FieldOffset = RL.getFieldOffset(FieldNo);
333
334 // Create a DW_TAG_member node to remember the offset of this field in the
335 // struct. FIXME: This is an absolutely insane way to capture this
336 // information. When we gut debug info, this should be fixed.
337 FieldTy = DebugFactory.CreateDerivedType(llvm::dwarf::DW_TAG_member, Unit,
338 FieldName, FieldDefUnit,
339 FieldLine, FieldSize, FieldAlign,
340 FieldOffset, 0, FieldTy);
Chris Lattner9c85ba32008-11-10 06:08:34 +0000341 EltTys.push_back(FieldTy);
342 }
343
344 llvm::DIArray Elements =
345 DebugFactory.GetOrCreateArray(&EltTys[0], EltTys.size());
346
347 // Bit size, align and offset of the type.
348 uint64_t Size = M->getContext().getTypeSize(Ty);
349 uint64_t Align = M->getContext().getTypeAlign(Ty);
350
351 llvm::DIType RealDecl =
352 DebugFactory.CreateCompositeType(Tag, Unit, Name, DefUnit, Line, Size,
353 Align, 0, 0, llvm::DIType(), Elements);
354
355 // Now that we have a real decl for the struct, replace anything using the
356 // old decl with the new one. This will recursively update the debug info.
357 FwdDecl.getGV()->replaceAllUsesWith(RealDecl.getGV());
358 FwdDecl.getGV()->eraseFromParent();
359
360 return RealDecl;
361}
362
Devang Patel9ca36b62009-02-26 21:10:26 +0000363/// CreateType - get objective-c interface type.
364llvm::DIType CGDebugInfo::CreateType(const ObjCInterfaceType *Ty,
365 llvm::DICompileUnit Unit) {
366 ObjCInterfaceDecl *Decl = Ty->getDecl();
367
368 unsigned Tag = llvm::dwarf::DW_TAG_structure_type;
369 SourceManager &SM = M->getContext().getSourceManager();
370
371 // Get overall information about the record type for the debug info.
372 std::string Name = Decl->getNameAsString();
373
374 llvm::DICompileUnit DefUnit = getOrCreateCompileUnit(Decl->getLocation());
Devang Patel4f6fa232009-04-17 21:35:15 +0000375 PresumedLoc PLoc = SM.getPresumedLoc(Decl->getLocation());
376 unsigned Line = PLoc.isInvalid() ? 0 : PLoc.getLine();
377
Devang Patel9ca36b62009-02-26 21:10:26 +0000378
379 // To handle recursive interface, we
380 // first generate a debug descriptor for the struct as a forward declaration.
381 // Then (if it is a definition) we go through and get debug info for all of
382 // its members. Finally, we create a descriptor for the complete type (which
383 // may refer to the forward decl if the struct is recursive) and replace all
384 // uses of the forward declaration with the final definition.
385 llvm::DIType FwdDecl =
386 DebugFactory.CreateCompositeType(Tag, Unit, Name, DefUnit, Line, 0, 0, 0, 0,
387 llvm::DIType(), llvm::DIArray());
388
389 // If this is just a forward declaration, return it.
390 if (Decl->isForwardDecl())
391 return FwdDecl;
392
393 // Otherwise, insert it into the TypeCache so that recursive uses will find
394 // it.
395 TypeCache[QualType(Ty, 0).getAsOpaquePtr()] = FwdDecl;
396
397 // Convert all the elements.
398 llvm::SmallVector<llvm::DIDescriptor, 16> EltTys;
399
Devang Patelfbe899f2009-03-10 21:30:26 +0000400 ObjCInterfaceDecl *SClass = Decl->getSuperClass();
401 if (SClass) {
402 llvm::DIType SClassTy =
403 getOrCreateType(M->getContext().getObjCInterfaceType(SClass), Unit);
404 llvm::DIType InhTag =
405 DebugFactory.CreateDerivedType(llvm::dwarf::DW_TAG_inheritance,
406 Unit, "", Unit, 0, 0, 0,
407 0 /* offset */, 0, SClassTy);
408 EltTys.push_back(InhTag);
409 }
410
Devang Patel9ca36b62009-02-26 21:10:26 +0000411 const ASTRecordLayout &RL = M->getContext().getASTObjCInterfaceLayout(Decl);
412
413 unsigned FieldNo = 0;
414 for (ObjCInterfaceDecl::ivar_iterator I = Decl->ivar_begin(),
415 E = Decl->ivar_end(); I != E; ++I, ++FieldNo) {
416 ObjCIvarDecl *Field = *I;
417 llvm::DIType FieldTy = getOrCreateType(Field->getType(), Unit);
418
419 std::string FieldName = Field->getNameAsString();
420
Devang Patelde135022009-04-27 22:40:36 +0000421 // Ignore unnamed fields.
422 if (FieldName.empty())
423 continue;
424
Devang Patel9ca36b62009-02-26 21:10:26 +0000425 // Get the location for the field.
426 SourceLocation FieldDefLoc = Field->getLocation();
427 llvm::DICompileUnit FieldDefUnit = getOrCreateCompileUnit(FieldDefLoc);
Devang Patel4f6fa232009-04-17 21:35:15 +0000428 PresumedLoc PLoc = SM.getPresumedLoc(FieldDefLoc);
429 unsigned FieldLine = PLoc.isInvalid() ? 0 : PLoc.getLine();
430
Devang Patel99c20eb2009-03-20 18:24:39 +0000431
432 QualType FType = Field->getType();
433 uint64_t FieldSize = 0;
434 unsigned FieldAlign = 0;
Devang Patelc20482b2009-03-19 00:23:53 +0000435
Devang Patel99c20eb2009-03-20 18:24:39 +0000436 if (!FType->isIncompleteArrayType()) {
437
438 // Bit size, align and offset of the type.
439 FieldSize = M->getContext().getTypeSize(FType);
440 Expr *BitWidth = Field->getBitWidth();
441 if (BitWidth)
Eli Friedman9a901bb2009-04-26 19:19:15 +0000442 FieldSize = BitWidth->EvaluateAsInt(M->getContext()).getZExtValue();
443
Devang Patel99c20eb2009-03-20 18:24:39 +0000444 FieldAlign = M->getContext().getTypeAlign(FType);
445 }
446
447 uint64_t FieldOffset = RL.getFieldOffset(FieldNo);
448
Devang Patelc20482b2009-03-19 00:23:53 +0000449 unsigned Flags = 0;
450 if (Field->getAccessControl() == ObjCIvarDecl::Protected)
451 Flags = llvm::DIType::FlagProtected;
452 else if (Field->getAccessControl() == ObjCIvarDecl::Private)
453 Flags = llvm::DIType::FlagPrivate;
454
Devang Patel9ca36b62009-02-26 21:10:26 +0000455 // Create a DW_TAG_member node to remember the offset of this field in the
456 // struct. FIXME: This is an absolutely insane way to capture this
457 // information. When we gut debug info, this should be fixed.
458 FieldTy = DebugFactory.CreateDerivedType(llvm::dwarf::DW_TAG_member, Unit,
459 FieldName, FieldDefUnit,
460 FieldLine, FieldSize, FieldAlign,
Devang Patelc20482b2009-03-19 00:23:53 +0000461 FieldOffset, Flags, FieldTy);
Devang Patel9ca36b62009-02-26 21:10:26 +0000462 EltTys.push_back(FieldTy);
463 }
464
465 llvm::DIArray Elements =
466 DebugFactory.GetOrCreateArray(&EltTys[0], EltTys.size());
467
468 // Bit size, align and offset of the type.
469 uint64_t Size = M->getContext().getTypeSize(Ty);
470 uint64_t Align = M->getContext().getTypeAlign(Ty);
471
472 llvm::DIType RealDecl =
473 DebugFactory.CreateCompositeType(Tag, Unit, Name, DefUnit, Line, Size,
474 Align, 0, 0, llvm::DIType(), Elements);
475
476 // Now that we have a real decl for the struct, replace anything using the
477 // old decl with the new one. This will recursively update the debug info.
478 FwdDecl.getGV()->replaceAllUsesWith(RealDecl.getGV());
479 FwdDecl.getGV()->eraseFromParent();
480
481 return RealDecl;
482}
483
Chris Lattner9c85ba32008-11-10 06:08:34 +0000484llvm::DIType CGDebugInfo::CreateType(const EnumType *Ty,
485 llvm::DICompileUnit Unit) {
486 EnumDecl *Decl = Ty->getDecl();
487
488 llvm::SmallVector<llvm::DIDescriptor, 32> Enumerators;
489
490 // Create DIEnumerator elements for each enumerator.
Douglas Gregor6ab35242009-04-09 21:40:53 +0000491 for (EnumDecl::enumerator_iterator
492 Enum = Decl->enumerator_begin(M->getContext()),
493 EnumEnd = Decl->enumerator_end(M->getContext());
Douglas Gregor44b43212008-12-11 16:49:14 +0000494 Enum != EnumEnd; ++Enum) {
495 Enumerators.push_back(DebugFactory.CreateEnumerator(Enum->getNameAsString(),
496 Enum->getInitVal().getZExtValue()));
Chris Lattner9c85ba32008-11-10 06:08:34 +0000497 }
498
499 // Return a CompositeType for the enum itself.
500 llvm::DIArray EltArray =
501 DebugFactory.GetOrCreateArray(&Enumerators[0], Enumerators.size());
502
Chris Lattner8ec03f52008-11-24 03:54:41 +0000503 std::string EnumName = Decl->getNameAsString();
Chris Lattner9c85ba32008-11-10 06:08:34 +0000504 SourceLocation DefLoc = Decl->getLocation();
505 llvm::DICompileUnit DefUnit = getOrCreateCompileUnit(DefLoc);
506 SourceManager &SM = M->getContext().getSourceManager();
Devang Patel4f6fa232009-04-17 21:35:15 +0000507 PresumedLoc PLoc = SM.getPresumedLoc(DefLoc);
508 unsigned Line = PLoc.isInvalid() ? 0 : PLoc.getLine();
509
Chris Lattner9c85ba32008-11-10 06:08:34 +0000510
511 // Size and align of the type.
512 uint64_t Size = M->getContext().getTypeSize(Ty);
Chris Lattnerf7cf85b2009-01-16 07:36:28 +0000513 unsigned Align = M->getContext().getTypeAlign(Ty);
Chris Lattner9c85ba32008-11-10 06:08:34 +0000514
515 return DebugFactory.CreateCompositeType(llvm::dwarf::DW_TAG_enumeration_type,
516 Unit, EnumName, DefUnit, Line,
517 Size, Align, 0, 0,
518 llvm::DIType(), EltArray);
519}
520
521llvm::DIType CGDebugInfo::CreateType(const TagType *Ty,
522 llvm::DICompileUnit Unit) {
523 if (const RecordType *RT = dyn_cast<RecordType>(Ty))
524 return CreateType(RT, Unit);
525 else if (const EnumType *ET = dyn_cast<EnumType>(Ty))
526 return CreateType(ET, Unit);
527
528 return llvm::DIType();
529}
530
531llvm::DIType CGDebugInfo::CreateType(const ArrayType *Ty,
532 llvm::DICompileUnit Unit) {
Anders Carlsson835c9092009-01-05 01:23:29 +0000533 uint64_t Size;
534 uint64_t Align;
535
536
Nuno Lopes010d5142009-01-28 00:35:17 +0000537 // FIXME: make getTypeAlign() aware of VLAs and incomplete array types
Anders Carlsson835c9092009-01-05 01:23:29 +0000538 if (const VariableArrayType *VAT = dyn_cast<VariableArrayType>(Ty)) {
Anders Carlsson835c9092009-01-05 01:23:29 +0000539 Size = 0;
540 Align =
Nuno Lopes010d5142009-01-28 00:35:17 +0000541 M->getContext().getTypeAlign(M->getContext().getBaseElementType(VAT));
542 } else if (Ty->isIncompleteArrayType()) {
543 Size = 0;
544 Align = M->getContext().getTypeAlign(Ty->getElementType());
Anders Carlsson835c9092009-01-05 01:23:29 +0000545 } else {
546 // Size and align of the whole array, not the element type.
547 Size = M->getContext().getTypeSize(Ty);
548 Align = M->getContext().getTypeAlign(Ty);
549 }
Chris Lattner9c85ba32008-11-10 06:08:34 +0000550
551 // Add the dimensions of the array. FIXME: This loses CV qualifiers from
552 // interior arrays, do we care? Why aren't nested arrays represented the
553 // obvious/recursive way?
554 llvm::SmallVector<llvm::DIDescriptor, 8> Subscripts;
555 QualType EltTy(Ty, 0);
556 while ((Ty = dyn_cast<ArrayType>(EltTy))) {
Sanjiv Gupta507de852008-06-09 10:47:41 +0000557 uint64_t Upper = 0;
Chris Lattner9c85ba32008-11-10 06:08:34 +0000558 if (const ConstantArrayType *CAT = dyn_cast<ConstantArrayType>(Ty))
559 Upper = CAT->getSize().getZExtValue() - 1;
560 // FIXME: Verify this is right for VLAs.
561 Subscripts.push_back(DebugFactory.GetOrCreateSubrange(0, Upper));
562 EltTy = Ty->getElementType();
Sanjiv Gupta507de852008-06-09 10:47:41 +0000563 }
Sanjiv Gupta1c6a38b2008-05-25 05:15:42 +0000564
Chris Lattner9c85ba32008-11-10 06:08:34 +0000565 llvm::DIArray SubscriptArray =
566 DebugFactory.GetOrCreateArray(&Subscripts[0], Subscripts.size());
567
568 return DebugFactory.CreateCompositeType(llvm::dwarf::DW_TAG_array_type,
569 Unit, "", llvm::DICompileUnit(),
570 0, Size, Align, 0, 0,
571 getOrCreateType(EltTy, Unit),
572 SubscriptArray);
573}
574
575
Sanjiv Gupta1c6a38b2008-05-25 05:15:42 +0000576/// getOrCreateType - Get the type from the cache or create a new
577/// one if necessary.
Chris Lattner9c85ba32008-11-10 06:08:34 +0000578llvm::DIType CGDebugInfo::getOrCreateType(QualType Ty,
579 llvm::DICompileUnit Unit) {
580 if (Ty.isNull())
581 return llvm::DIType();
582
Sanjiv Gupta1c6a38b2008-05-25 05:15:42 +0000583 // Check to see if the compile unit already has created this type.
Chris Lattner9c85ba32008-11-10 06:08:34 +0000584 llvm::DIType &Slot = TypeCache[Ty.getAsOpaquePtr()];
585 if (!Slot.isNull()) return Slot;
Sanjiv Gupta1c6a38b2008-05-25 05:15:42 +0000586
Chris Lattner9c85ba32008-11-10 06:08:34 +0000587 // Handle CVR qualifiers, which recursively handles what they refer to.
588 if (Ty.getCVRQualifiers())
589 return Slot = CreateCVRType(Ty, Unit);
Sanjiv Gupta1c6a38b2008-05-25 05:15:42 +0000590
591 // Work out details of type.
Chris Lattner9c85ba32008-11-10 06:08:34 +0000592 switch (Ty->getTypeClass()) {
Douglas Gregor72564e72009-02-26 23:50:07 +0000593#define TYPE(Class, Base)
594#define ABSTRACT_TYPE(Class, Base)
595#define NON_CANONICAL_TYPE(Class, Base)
596#define DEPENDENT_TYPE(Class, Base) case Type::Class:
597#include "clang/AST/TypeNodes.def"
598 assert(false && "Dependent types cannot show up in debug information");
599
Sebastian Redl7c80bd62009-03-16 23:22:08 +0000600 case Type::LValueReference:
601 case Type::RValueReference:
Chris Lattner9c85ba32008-11-10 06:08:34 +0000602 case Type::Vector:
603 case Type::ExtVector:
Fariborz Jahanianf11284a2009-02-17 18:27:45 +0000604 case Type::ExtQual:
Eli Friedman00524e32009-02-27 23:15:07 +0000605 case Type::FixedWidthInt:
606 case Type::BlockPointer:
607 case Type::MemberPointer:
Douglas Gregor7532dc62009-03-30 22:58:21 +0000608 case Type::TemplateSpecialization:
Douglas Gregore4e5b052009-03-19 00:18:19 +0000609 case Type::QualifiedName:
Eli Friedman00524e32009-02-27 23:15:07 +0000610 // Unsupported types
Chris Lattner9c85ba32008-11-10 06:08:34 +0000611 return llvm::DIType();
Chris Lattneraa2b5792009-04-22 06:58:56 +0000612 case Type::ObjCQualifiedId: // Encode id<p> in debug info just like id.
613 return Slot = getOrCreateType(M->getContext().getObjCIdType(), Unit);
614
615 case Type::ObjCQualifiedInterface: // Drop protocols from interface.
Devang Patele7987062009-03-02 17:58:28 +0000616 case Type::ObjCInterface:
Chris Lattneraa2b5792009-04-22 06:58:56 +0000617 return Slot = CreateType(cast<ObjCInterfaceType>(Ty), Unit);
618 case Type::Builtin: return Slot = CreateType(cast<BuiltinType>(Ty), Unit);
Chris Lattnerb7003772009-04-23 06:13:01 +0000619 case Type::Complex: return Slot = CreateType(cast<ComplexType>(Ty), Unit);
Chris Lattneraa2b5792009-04-22 06:58:56 +0000620 case Type::Pointer: return Slot = CreateType(cast<PointerType>(Ty), Unit);
621 case Type::Typedef: return Slot = CreateType(cast<TypedefType>(Ty), Unit);
Douglas Gregor72564e72009-02-26 23:50:07 +0000622 case Type::Record:
Douglas Gregor72564e72009-02-26 23:50:07 +0000623 case Type::Enum:
Chris Lattneraa2b5792009-04-22 06:58:56 +0000624 return Slot = CreateType(cast<TagType>(Ty), Unit);
Chris Lattner9c85ba32008-11-10 06:08:34 +0000625 case Type::FunctionProto:
626 case Type::FunctionNoProto:
Chris Lattner3cc5c402008-11-11 07:01:36 +0000627 return Slot = CreateType(cast<FunctionType>(Ty), Unit);
Chris Lattner9c85ba32008-11-10 06:08:34 +0000628
629 case Type::ConstantArray:
630 case Type::VariableArray:
631 case Type::IncompleteArray:
Chris Lattner3cc5c402008-11-11 07:01:36 +0000632 return Slot = CreateType(cast<ArrayType>(Ty), Unit);
Douglas Gregor72564e72009-02-26 23:50:07 +0000633 case Type::TypeOfExpr:
634 return Slot = getOrCreateType(cast<TypeOfExprType>(Ty)->getUnderlyingExpr()
Chris Lattner3cc5c402008-11-11 07:01:36 +0000635 ->getType(), Unit);
Douglas Gregor72564e72009-02-26 23:50:07 +0000636 case Type::TypeOf:
Chris Lattner3cc5c402008-11-11 07:01:36 +0000637 return Slot = getOrCreateType(cast<TypeOfType>(Ty)->getUnderlyingType(),
638 Unit);
Sanjiv Gupta1c6a38b2008-05-25 05:15:42 +0000639 }
Chris Lattner9c85ba32008-11-10 06:08:34 +0000640
Sanjiv Gupta1c6a38b2008-05-25 05:15:42 +0000641 return Slot;
642}
643
644/// EmitFunctionStart - Constructs the debug code for entering a function -
645/// "llvm.dbg.func.start.".
Chris Lattner9c85ba32008-11-10 06:08:34 +0000646void CGDebugInfo::EmitFunctionStart(const char *Name, QualType ReturnType,
Sanjiv Gupta1c6a38b2008-05-25 05:15:42 +0000647 llvm::Function *Fn,
Chris Lattner9c85ba32008-11-10 06:08:34 +0000648 CGBuilderTy &Builder) {
649 // FIXME: Why is this using CurLoc???
650 llvm::DICompileUnit Unit = getOrCreateCompileUnit(CurLoc);
Sanjiv Gupta1c6a38b2008-05-25 05:15:42 +0000651 SourceManager &SM = M->getContext().getSourceManager();
Devang Patel0f78fea2009-04-08 19:47:04 +0000652 unsigned LineNo = SM.getPresumedLoc(CurLoc).getLine();
Chris Lattner9c85ba32008-11-10 06:08:34 +0000653
654 llvm::DISubprogram SP =
655 DebugFactory.CreateSubprogram(Unit, Name, Name, "", Unit, LineNo,
656 getOrCreateType(ReturnType, Unit),
657 Fn->hasInternalLinkage(), true/*definition*/);
658
659 DebugFactory.InsertSubprogramStart(SP, Builder.GetInsertBlock());
660
Sanjiv Gupta1c6a38b2008-05-25 05:15:42 +0000661 // Push function on region stack.
Chris Lattner9c85ba32008-11-10 06:08:34 +0000662 RegionStack.push_back(SP);
Sanjiv Gupta1c6a38b2008-05-25 05:15:42 +0000663}
664
665
Chris Lattner9c85ba32008-11-10 06:08:34 +0000666void CGDebugInfo::EmitStopPoint(llvm::Function *Fn, CGBuilderTy &Builder) {
Sanjiv Gupta1c6a38b2008-05-25 05:15:42 +0000667 if (CurLoc.isInvalid() || CurLoc.isMacroID()) return;
668
Sanjiv Guptae8b9f5b2008-05-08 08:54:20 +0000669 // Don't bother if things are the same as last time.
670 SourceManager &SM = M->getContext().getSourceManager();
671 if (CurLoc == PrevLoc
Chris Lattner30fc9332009-02-04 01:06:56 +0000672 || (SM.getInstantiationLineNumber(CurLoc) ==
673 SM.getInstantiationLineNumber(PrevLoc)
Sanjiv Guptae8b9f5b2008-05-08 08:54:20 +0000674 && SM.isFromSameFile(CurLoc, PrevLoc)))
675 return;
Sanjiv Guptae8b9f5b2008-05-08 08:54:20 +0000676
677 // Update last state.
678 PrevLoc = CurLoc;
679
680 // Get the appropriate compile unit.
Chris Lattner9c85ba32008-11-10 06:08:34 +0000681 llvm::DICompileUnit Unit = getOrCreateCompileUnit(CurLoc);
Devang Patel0f78fea2009-04-08 19:47:04 +0000682 PresumedLoc PLoc = SM.getPresumedLoc(CurLoc);
683 DebugFactory.InsertStopPoint(Unit, PLoc.getLine(), PLoc.getColumn(),
Chris Lattner9c85ba32008-11-10 06:08:34 +0000684 Builder.GetInsertBlock());
Sanjiv Guptae8b9f5b2008-05-08 08:54:20 +0000685}
686
687/// EmitRegionStart- Constructs the debug code for entering a declarative
688/// region - "llvm.dbg.region.start.".
Chris Lattner9c85ba32008-11-10 06:08:34 +0000689void CGDebugInfo::EmitRegionStart(llvm::Function *Fn, CGBuilderTy &Builder) {
690 llvm::DIDescriptor D;
Daniel Dunbar5273f512008-10-17 01:07:56 +0000691 if (!RegionStack.empty())
Chris Lattner9c85ba32008-11-10 06:08:34 +0000692 D = RegionStack.back();
693 D = DebugFactory.CreateBlock(D);
694 RegionStack.push_back(D);
695 DebugFactory.InsertRegionStart(D, Builder.GetInsertBlock());
Sanjiv Guptae8b9f5b2008-05-08 08:54:20 +0000696}
697
698/// EmitRegionEnd - Constructs the debug code for exiting a declarative
699/// region - "llvm.dbg.region.end."
Chris Lattner9c85ba32008-11-10 06:08:34 +0000700void CGDebugInfo::EmitRegionEnd(llvm::Function *Fn, CGBuilderTy &Builder) {
Daniel Dunbar5273f512008-10-17 01:07:56 +0000701 assert(!RegionStack.empty() && "Region stack mismatch, stack empty!");
702
Sanjiv Guptae8b9f5b2008-05-08 08:54:20 +0000703 // Provide an region stop point.
704 EmitStopPoint(Fn, Builder);
705
Chris Lattner9c85ba32008-11-10 06:08:34 +0000706 DebugFactory.InsertRegionEnd(RegionStack.back(), Builder.GetInsertBlock());
Sanjiv Gupta1c6a38b2008-05-25 05:15:42 +0000707 RegionStack.pop_back();
Sanjiv Guptae8b9f5b2008-05-08 08:54:20 +0000708}
709
Sanjiv Guptacc9b1632008-05-30 10:30:31 +0000710/// EmitDeclare - Emit local variable declaration debug info.
Chris Lattner9c85ba32008-11-10 06:08:34 +0000711void CGDebugInfo::EmitDeclare(const VarDecl *Decl, unsigned Tag,
712 llvm::Value *Storage, CGBuilderTy &Builder) {
Daniel Dunbar5273f512008-10-17 01:07:56 +0000713 assert(!RegionStack.empty() && "Region stack mismatch, stack empty!");
714
Devang Patel07739032009-03-27 23:16:32 +0000715 // Do not emit variable debug information while generating optimized code.
716 // The llvm optimizer and code generator are not yet ready to support
717 // optimized code debugging.
718 const CompileOptions &CO = M->getCompileOpts();
719 if (CO.OptimizationLevel)
720 return;
721
Chris Lattner9c85ba32008-11-10 06:08:34 +0000722 // Get location information.
Sanjiv Guptacc9b1632008-05-30 10:30:31 +0000723 SourceManager &SM = M->getContext().getSourceManager();
Devang Patel4f6fa232009-04-17 21:35:15 +0000724 PresumedLoc PLoc = SM.getPresumedLoc(Decl->getLocation());
725 unsigned Line = PLoc.isInvalid() ? 0 : PLoc.getLine();
Chris Lattner9c85ba32008-11-10 06:08:34 +0000726 llvm::DICompileUnit Unit = getOrCreateCompileUnit(Decl->getLocation());
727
728 // Create the descriptor for the variable.
729 llvm::DIVariable D =
Chris Lattnerd9d22dd2008-11-24 05:29:24 +0000730 DebugFactory.CreateVariable(Tag, RegionStack.back(),Decl->getNameAsString(),
Chris Lattner9c85ba32008-11-10 06:08:34 +0000731 Unit, Line,
732 getOrCreateType(Decl->getType(), Unit));
733 // Insert an llvm.dbg.declare into the current block.
734 DebugFactory.InsertDeclare(Storage, D, Builder.GetInsertBlock());
Sanjiv Guptacc9b1632008-05-30 10:30:31 +0000735}
736
Chris Lattner9c85ba32008-11-10 06:08:34 +0000737void CGDebugInfo::EmitDeclareOfAutoVariable(const VarDecl *Decl,
738 llvm::Value *Storage,
739 CGBuilderTy &Builder) {
740 EmitDeclare(Decl, llvm::dwarf::DW_TAG_auto_variable, Storage, Builder);
741}
742
743/// EmitDeclareOfArgVariable - Emit call to llvm.dbg.declare for an argument
744/// variable declaration.
745void CGDebugInfo::EmitDeclareOfArgVariable(const VarDecl *Decl, llvm::Value *AI,
746 CGBuilderTy &Builder) {
747 EmitDeclare(Decl, llvm::dwarf::DW_TAG_arg_variable, AI, Builder);
748}
749
750
751
Sanjiv Gupta686226b2008-06-05 08:59:10 +0000752/// EmitGlobalVariable - Emit information about a global variable.
Chris Lattner9c85ba32008-11-10 06:08:34 +0000753void CGDebugInfo::EmitGlobalVariable(llvm::GlobalVariable *Var,
754 const VarDecl *Decl) {
Devang Patel07739032009-03-27 23:16:32 +0000755
756 // Do not emit variable debug information while generating optimized code.
757 // The llvm optimizer and code generator are not yet ready to support
758 // optimized code debugging.
759 const CompileOptions &CO = M->getCompileOpts();
760 if (CO.OptimizationLevel)
761 return;
762
Sanjiv Gupta686226b2008-06-05 08:59:10 +0000763 // Create global variable debug descriptor.
Chris Lattner9c85ba32008-11-10 06:08:34 +0000764 llvm::DICompileUnit Unit = getOrCreateCompileUnit(Decl->getLocation());
Sanjiv Gupta686226b2008-06-05 08:59:10 +0000765 SourceManager &SM = M->getContext().getSourceManager();
Devang Patel4f6fa232009-04-17 21:35:15 +0000766 PresumedLoc PLoc = SM.getPresumedLoc(Decl->getLocation());
767 unsigned LineNo = PLoc.isInvalid() ? 0 : PLoc.getLine();
Chris Lattner8ec03f52008-11-24 03:54:41 +0000768
769 std::string Name = Decl->getNameAsString();
Anders Carlsson4d6e8dd2008-11-26 17:40:42 +0000770
771 QualType T = Decl->getType();
772 if (T->isIncompleteArrayType()) {
773
774 // CodeGen turns int[] into int[1] so we'll do the same here.
775 llvm::APSInt ConstVal(32);
776
777 ConstVal = 1;
778 QualType ET = M->getContext().getAsArrayType(T)->getElementType();
779
780 T = M->getContext().getConstantArrayType(ET, ConstVal,
781 ArrayType::Normal, 0);
782 }
783
Chris Lattner9c85ba32008-11-10 06:08:34 +0000784 DebugFactory.CreateGlobalVariable(Unit, Name, Name, "", Unit, LineNo,
Anders Carlsson4d6e8dd2008-11-26 17:40:42 +0000785 getOrCreateType(T, Unit),
Chris Lattner9c85ba32008-11-10 06:08:34 +0000786 Var->hasInternalLinkage(),
787 true/*definition*/, Var);
Sanjiv Gupta686226b2008-06-05 08:59:10 +0000788}
789
Devang Patel9ca36b62009-02-26 21:10:26 +0000790/// EmitGlobalVariable - Emit information about an objective-c interface.
791void CGDebugInfo::EmitGlobalVariable(llvm::GlobalVariable *Var,
792 ObjCInterfaceDecl *Decl) {
793 // Create global variable debug descriptor.
794 llvm::DICompileUnit Unit = getOrCreateCompileUnit(Decl->getLocation());
795 SourceManager &SM = M->getContext().getSourceManager();
Devang Patel4f6fa232009-04-17 21:35:15 +0000796 PresumedLoc PLoc = SM.getPresumedLoc(Decl->getLocation());
797 unsigned LineNo = PLoc.isInvalid() ? 0 : PLoc.getLine();
Devang Patel9ca36b62009-02-26 21:10:26 +0000798
799 std::string Name = Decl->getNameAsString();
800
Chris Lattner03d9f342009-04-01 06:23:52 +0000801 QualType T = M->getContext().getObjCInterfaceType(Decl);
Devang Patel9ca36b62009-02-26 21:10:26 +0000802 if (T->isIncompleteArrayType()) {
803
804 // CodeGen turns int[] into int[1] so we'll do the same here.
805 llvm::APSInt ConstVal(32);
806
807 ConstVal = 1;
808 QualType ET = M->getContext().getAsArrayType(T)->getElementType();
809
810 T = M->getContext().getConstantArrayType(ET, ConstVal,
811 ArrayType::Normal, 0);
812 }
813
814 DebugFactory.CreateGlobalVariable(Unit, Name, Name, "", Unit, LineNo,
815 getOrCreateType(T, Unit),
816 Var->hasInternalLinkage(),
817 true/*definition*/, Var);
818}
819