blob: a4fd705e30c18bcdf39a369736b750314f23a972 [file] [log] [blame]
Chris Lattner6d389332008-11-10 02:56:27 +00001//===--- DebugInfo.cpp - Debug Information Helper Classes -----------------===//
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 file implements the helper classes used to build and interpret debug
11// information in LLVM IR form.
12//
13//===----------------------------------------------------------------------===//
14
Bill Wendlinge38859d2012-06-28 00:05:13 +000015#include "llvm/DebugInfo.h"
Chris Lattner6d389332008-11-10 02:56:27 +000016#include "llvm/Constants.h"
17#include "llvm/DerivedTypes.h"
18#include "llvm/Intrinsics.h"
Torok Edwin89261392008-12-16 09:07:36 +000019#include "llvm/IntrinsicInst.h"
Chris Lattner6d389332008-11-10 02:56:27 +000020#include "llvm/Instructions.h"
21#include "llvm/Module.h"
22#include "llvm/Analysis/ValueTracking.h"
Devang Patel80ae3492009-08-28 23:24:31 +000023#include "llvm/ADT/SmallPtrSet.h"
Dan Gohman2637cc12010-07-21 23:38:33 +000024#include "llvm/ADT/SmallString.h"
Dan Gohman16a5d982010-08-20 22:02:26 +000025#include "llvm/ADT/STLExtras.h"
David Greene83d4781452009-12-23 19:45:49 +000026#include "llvm/Support/Debug.h"
Argyrios Kyrtzidis97324ce2009-05-03 08:50:41 +000027#include "llvm/Support/Dwarf.h"
Chris Lattner91922252009-08-23 07:33:14 +000028#include "llvm/Support/raw_ostream.h"
Chris Lattner6d389332008-11-10 02:56:27 +000029using namespace llvm;
Argyrios Kyrtzidis97324ce2009-05-03 08:50:41 +000030using namespace llvm::dwarf;
Chris Lattner6d389332008-11-10 02:56:27 +000031
32//===----------------------------------------------------------------------===//
33// DIDescriptor
34//===----------------------------------------------------------------------===//
35
Devang Patel33a2cdf2010-08-02 22:51:46 +000036DIDescriptor::DIDescriptor(const DIFile F) : DbgNode(F.DbgNode) {
37}
38
39DIDescriptor::DIDescriptor(const DISubprogram F) : DbgNode(F.DbgNode) {
40}
41
Eric Christopher6647b832011-10-11 22:59:11 +000042DIDescriptor::DIDescriptor(const DILexicalBlockFile F) : DbgNode(F.DbgNode) {
43}
44
Devang Patel33a2cdf2010-08-02 22:51:46 +000045DIDescriptor::DIDescriptor(const DILexicalBlock F) : DbgNode(F.DbgNode) {
46}
47
48DIDescriptor::DIDescriptor(const DIVariable F) : DbgNode(F.DbgNode) {
49}
50
51DIDescriptor::DIDescriptor(const DIType F) : DbgNode(F.DbgNode) {
52}
53
Jim Grosbach6cd0deb2010-07-21 21:36:25 +000054StringRef
Devang Patelb2969422009-09-29 18:40:58 +000055DIDescriptor::getStringField(unsigned Elt) const {
Devang Patel80ae3492009-08-28 23:24:31 +000056 if (DbgNode == 0)
Devang Patel2d9caf92009-11-25 17:36:49 +000057 return StringRef();
Chris Lattner6d389332008-11-10 02:56:27 +000058
Chris Lattner9b493022009-12-31 01:22:29 +000059 if (Elt < DbgNode->getNumOperands())
60 if (MDString *MDS = dyn_cast_or_null<MDString>(DbgNode->getOperand(Elt)))
Devang Patel2d9caf92009-11-25 17:36:49 +000061 return MDS->getString();
Daniel Dunbarc418d6b2009-09-19 20:40:05 +000062
Devang Patel2d9caf92009-11-25 17:36:49 +000063 return StringRef();
Chris Lattner6d389332008-11-10 02:56:27 +000064}
65
66uint64_t DIDescriptor::getUInt64Field(unsigned Elt) const {
Daniel Dunbarc418d6b2009-09-19 20:40:05 +000067 if (DbgNode == 0)
Chris Lattner6d389332008-11-10 02:56:27 +000068 return 0;
Bill Wendling6ddc11a2009-05-14 18:26:15 +000069
Chris Lattner9b493022009-12-31 01:22:29 +000070 if (Elt < DbgNode->getNumOperands())
Eric Christopherb1d992f2012-11-20 00:15:36 +000071 if (ConstantInt *CI
72 = dyn_cast_or_null<ConstantInt>(DbgNode->getOperand(Elt)))
Devang Patel80ae3492009-08-28 23:24:31 +000073 return CI->getZExtValue();
Daniel Dunbarc418d6b2009-09-19 20:40:05 +000074
Chris Lattner6d389332008-11-10 02:56:27 +000075 return 0;
76}
77
Chris Lattner6d389332008-11-10 02:56:27 +000078DIDescriptor DIDescriptor::getDescriptorField(unsigned Elt) const {
Daniel Dunbarc418d6b2009-09-19 20:40:05 +000079 if (DbgNode == 0)
Chris Lattner6d389332008-11-10 02:56:27 +000080 return DIDescriptor();
Bill Wendling6ddc11a2009-05-14 18:26:15 +000081
Chris Lattner743bdca2010-03-31 05:53:47 +000082 if (Elt < DbgNode->getNumOperands())
Jim Grosbach6cd0deb2010-07-21 21:36:25 +000083 return
84 DIDescriptor(dyn_cast_or_null<const MDNode>(DbgNode->getOperand(Elt)));
Devang Patel80ae3492009-08-28 23:24:31 +000085 return DIDescriptor();
Chris Lattner6d389332008-11-10 02:56:27 +000086}
87
88GlobalVariable *DIDescriptor::getGlobalVariableField(unsigned Elt) const {
Daniel Dunbarc418d6b2009-09-19 20:40:05 +000089 if (DbgNode == 0)
Chris Lattner6d389332008-11-10 02:56:27 +000090 return 0;
Bill Wendling6ddc11a2009-05-14 18:26:15 +000091
Chris Lattner9b493022009-12-31 01:22:29 +000092 if (Elt < DbgNode->getNumOperands())
93 return dyn_cast_or_null<GlobalVariable>(DbgNode->getOperand(Elt));
Devang Patel80ae3492009-08-28 23:24:31 +000094 return 0;
Chris Lattner6d389332008-11-10 02:56:27 +000095}
96
Devang Patelc7cf14f2010-08-09 21:39:24 +000097Constant *DIDescriptor::getConstantField(unsigned Elt) const {
98 if (DbgNode == 0)
99 return 0;
100
101 if (Elt < DbgNode->getNumOperands())
102 return dyn_cast_or_null<Constant>(DbgNode->getOperand(Elt));
103 return 0;
104}
105
Stuart Hastingsafe54f12010-06-11 20:08:44 +0000106Function *DIDescriptor::getFunctionField(unsigned Elt) const {
107 if (DbgNode == 0)
108 return 0;
109
110 if (Elt < DbgNode->getNumOperands())
111 return dyn_cast_or_null<Function>(DbgNode->getOperand(Elt));
112 return 0;
113}
114
Alexey Samsonov3b861ec2012-10-09 08:13:15 +0000115void DIDescriptor::replaceFunctionField(unsigned Elt, Function *F) {
116 if (DbgNode == 0)
117 return;
118
119 if (Elt < DbgNode->getNumOperands()) {
120 MDNode *Node = const_cast<MDNode*>(DbgNode);
121 Node->replaceOperandWith(Elt, F);
122 }
123}
124
Chris Lattner6a0ca6a2009-12-31 03:02:08 +0000125unsigned DIVariable::getNumAddrElements() const {
Bill Wendling16d944c2012-07-06 17:46:28 +0000126 if (getVersion() <= LLVMDebugVersion8)
Devang Patelbea08d12010-09-29 23:07:21 +0000127 return DbgNode->getNumOperands()-6;
Bill Wendling16d944c2012-07-06 17:46:28 +0000128 if (getVersion() == LLVMDebugVersion9)
Devang Patelcfa82a32011-07-19 19:41:54 +0000129 return DbgNode->getNumOperands()-7;
130 return DbgNode->getNumOperands()-8;
Chris Lattner6a0ca6a2009-12-31 03:02:08 +0000131}
132
Devang Patel8fb9fd62011-07-20 22:18:50 +0000133/// getInlinedAt - If this variable is inlined then return inline location.
Devang Patel3d6e3892011-08-09 01:03:14 +0000134MDNode *DIVariable::getInlinedAt() const {
Bill Wendling16d944c2012-07-06 17:46:28 +0000135 if (getVersion() <= LLVMDebugVersion9)
Devang Patel8fb9fd62011-07-20 22:18:50 +0000136 return NULL;
137 return dyn_cast_or_null<MDNode>(DbgNode->getOperand(7));
138}
Chris Lattner6a0ca6a2009-12-31 03:02:08 +0000139
Chris Lattner6d389332008-11-10 02:56:27 +0000140//===----------------------------------------------------------------------===//
Devang Patel9fda4bd2009-08-31 18:49:10 +0000141// Predicates
Chris Lattner6d389332008-11-10 02:56:27 +0000142//===----------------------------------------------------------------------===//
143
Devang Patel9fda4bd2009-08-31 18:49:10 +0000144/// isBasicType - Return true if the specified tag is legal for
145/// DIBasicType.
146bool DIDescriptor::isBasicType() const {
Devang Patel04d6d472011-09-14 23:13:28 +0000147 if (!DbgNode) return false;
148 switch (getTag()) {
149 case dwarf::DW_TAG_base_type:
150 case dwarf::DW_TAG_unspecified_type:
151 return true;
152 default:
153 return false;
154 }
Torok Edwin040688f2008-12-13 08:25:29 +0000155}
Chris Lattner6d389332008-11-10 02:56:27 +0000156
Devang Patel9fda4bd2009-08-31 18:49:10 +0000157/// isDerivedType - Return true if the specified tag is legal for DIDerivedType.
158bool DIDescriptor::isDerivedType() const {
Devang Patel3b548aa2010-03-08 20:52:55 +0000159 if (!DbgNode) return false;
Chris Lattner047fd2f2009-12-29 09:22:47 +0000160 switch (getTag()) {
Chris Lattner6d389332008-11-10 02:56:27 +0000161 case dwarf::DW_TAG_typedef:
162 case dwarf::DW_TAG_pointer_type:
163 case dwarf::DW_TAG_reference_type:
Eric Christopherb5cf66c2012-05-19 01:36:37 +0000164 case dwarf::DW_TAG_rvalue_reference_type:
Chris Lattner6d389332008-11-10 02:56:27 +0000165 case dwarf::DW_TAG_const_type:
166 case dwarf::DW_TAG_volatile_type:
167 case dwarf::DW_TAG_restrict_type:
168 case dwarf::DW_TAG_member:
169 case dwarf::DW_TAG_inheritance:
Devang Pateldd719f72010-08-23 23:16:25 +0000170 case dwarf::DW_TAG_friend:
Chris Lattner6d389332008-11-10 02:56:27 +0000171 return true;
172 default:
Devang Patel80ae3492009-08-28 23:24:31 +0000173 // CompositeTypes are currently modelled as DerivedTypes.
Devang Patel9fda4bd2009-08-31 18:49:10 +0000174 return isCompositeType();
Chris Lattner6d389332008-11-10 02:56:27 +0000175 }
176}
177
Chris Lattner6d389332008-11-10 02:56:27 +0000178/// isCompositeType - Return true if the specified tag is legal for
179/// DICompositeType.
Devang Patel9fda4bd2009-08-31 18:49:10 +0000180bool DIDescriptor::isCompositeType() const {
Devang Patel3b548aa2010-03-08 20:52:55 +0000181 if (!DbgNode) return false;
Chris Lattner047fd2f2009-12-29 09:22:47 +0000182 switch (getTag()) {
Chris Lattner6d389332008-11-10 02:56:27 +0000183 case dwarf::DW_TAG_array_type:
184 case dwarf::DW_TAG_structure_type:
185 case dwarf::DW_TAG_union_type:
186 case dwarf::DW_TAG_enumeration_type:
187 case dwarf::DW_TAG_vector_type:
188 case dwarf::DW_TAG_subroutine_type:
Devang Patel68b08812009-03-25 03:52:06 +0000189 case dwarf::DW_TAG_class_type:
Chris Lattner6d389332008-11-10 02:56:27 +0000190 return true;
191 default:
192 return false;
193 }
194}
195
Chris Lattner6d389332008-11-10 02:56:27 +0000196/// isVariable - Return true if the specified tag is legal for DIVariable.
Devang Patel9fda4bd2009-08-31 18:49:10 +0000197bool DIDescriptor::isVariable() const {
Devang Patel3b548aa2010-03-08 20:52:55 +0000198 if (!DbgNode) return false;
Chris Lattner047fd2f2009-12-29 09:22:47 +0000199 switch (getTag()) {
Chris Lattner6d389332008-11-10 02:56:27 +0000200 case dwarf::DW_TAG_auto_variable:
201 case dwarf::DW_TAG_arg_variable:
202 case dwarf::DW_TAG_return_variable:
203 return true;
204 default:
205 return false;
206 }
207}
208
Devang Patela49be762009-09-30 22:34:41 +0000209/// isType - Return true if the specified tag is legal for DIType.
210bool DIDescriptor::isType() const {
211 return isBasicType() || isCompositeType() || isDerivedType();
212}
213
Devang Patel9fda4bd2009-08-31 18:49:10 +0000214/// isSubprogram - Return true if the specified tag is legal for
215/// DISubprogram.
216bool DIDescriptor::isSubprogram() const {
Devang Patel3b548aa2010-03-08 20:52:55 +0000217 return DbgNode && getTag() == dwarf::DW_TAG_subprogram;
Devang Patel9fda4bd2009-08-31 18:49:10 +0000218}
219
220/// isGlobalVariable - Return true if the specified tag is legal for
221/// DIGlobalVariable.
222bool DIDescriptor::isGlobalVariable() const {
Devang Patelb2197462010-08-10 07:11:13 +0000223 return DbgNode && (getTag() == dwarf::DW_TAG_variable ||
224 getTag() == dwarf::DW_TAG_constant);
Devang Patel9fda4bd2009-08-31 18:49:10 +0000225}
226
Devang Patela49be762009-09-30 22:34:41 +0000227/// isGlobal - Return true if the specified tag is legal for DIGlobal.
228bool DIDescriptor::isGlobal() const {
229 return isGlobalVariable();
230}
231
Devang Pateldf0dd7d2011-02-03 00:13:47 +0000232/// isUnspecifiedParmeter - Return true if the specified tag is
Devang Patel9a33ec22010-10-06 20:50:40 +0000233/// DW_TAG_unspecified_parameters.
234bool DIDescriptor::isUnspecifiedParameter() const {
235 return DbgNode && getTag() == dwarf::DW_TAG_unspecified_parameters;
236}
237
Daniel Dunbarc418d6b2009-09-19 20:40:05 +0000238/// isScope - Return true if the specified tag is one of the scope
Devang Pateld9b61152009-08-31 20:44:45 +0000239/// related tag.
240bool DIDescriptor::isScope() const {
Devang Patel3b548aa2010-03-08 20:52:55 +0000241 if (!DbgNode) return false;
Chris Lattner047fd2f2009-12-29 09:22:47 +0000242 switch (getTag()) {
243 case dwarf::DW_TAG_compile_unit:
244 case dwarf::DW_TAG_lexical_block:
245 case dwarf::DW_TAG_subprogram:
246 case dwarf::DW_TAG_namespace:
247 return true;
248 default:
249 break;
Devang Pateld9b61152009-08-31 20:44:45 +0000250 }
251 return false;
252}
Devang Patel9fda4bd2009-08-31 18:49:10 +0000253
Devang Patel3a9e65e2011-02-02 21:38:25 +0000254/// isTemplateTypeParameter - Return true if the specified tag is
255/// DW_TAG_template_type_parameter.
256bool DIDescriptor::isTemplateTypeParameter() const {
257 return DbgNode && getTag() == dwarf::DW_TAG_template_type_parameter;
258}
259
Devang Patelbe933b42011-02-02 22:35:53 +0000260/// isTemplateValueParameter - Return true if the specified tag is
261/// DW_TAG_template_value_parameter.
262bool DIDescriptor::isTemplateValueParameter() const {
263 return DbgNode && getTag() == dwarf::DW_TAG_template_value_parameter;
264}
265
Devang Patele4cfcc32009-08-31 21:34:44 +0000266/// isCompileUnit - Return true if the specified tag is DW_TAG_compile_unit.
267bool DIDescriptor::isCompileUnit() const {
Devang Patel3b548aa2010-03-08 20:52:55 +0000268 return DbgNode && getTag() == dwarf::DW_TAG_compile_unit;
Devang Patele4cfcc32009-08-31 21:34:44 +0000269}
270
Devang Patel2e520f62010-03-08 22:27:22 +0000271/// isFile - Return true if the specified tag is DW_TAG_file_type.
272bool DIDescriptor::isFile() const {
273 return DbgNode && getTag() == dwarf::DW_TAG_file_type;
274}
275
Devang Patel1f4690c2009-12-15 19:16:48 +0000276/// isNameSpace - Return true if the specified tag is DW_TAG_namespace.
277bool DIDescriptor::isNameSpace() const {
Devang Patel3b548aa2010-03-08 20:52:55 +0000278 return DbgNode && getTag() == dwarf::DW_TAG_namespace;
Devang Patel1f4690c2009-12-15 19:16:48 +0000279}
280
Eric Christopher6647b832011-10-11 22:59:11 +0000281/// isLexicalBlockFile - Return true if the specified descriptor is a
282/// lexical block with an extra file.
283bool DIDescriptor::isLexicalBlockFile() const {
284 return DbgNode && getTag() == dwarf::DW_TAG_lexical_block &&
285 (DbgNode->getNumOperands() == 3);
286}
287
Devang Patel869529c2009-08-31 22:00:15 +0000288/// isLexicalBlock - Return true if the specified tag is DW_TAG_lexical_block.
289bool DIDescriptor::isLexicalBlock() const {
Eric Christopher6647b832011-10-11 22:59:11 +0000290 return DbgNode && getTag() == dwarf::DW_TAG_lexical_block &&
291 (DbgNode->getNumOperands() > 3);
Devang Patel869529c2009-08-31 22:00:15 +0000292}
293
Devang Patela49be762009-09-30 22:34:41 +0000294/// isSubrange - Return true if the specified tag is DW_TAG_subrange_type.
295bool DIDescriptor::isSubrange() const {
Devang Patel3b548aa2010-03-08 20:52:55 +0000296 return DbgNode && getTag() == dwarf::DW_TAG_subrange_type;
Devang Patela49be762009-09-30 22:34:41 +0000297}
298
299/// isEnumerator - Return true if the specified tag is DW_TAG_enumerator.
300bool DIDescriptor::isEnumerator() const {
Devang Patel3b548aa2010-03-08 20:52:55 +0000301 return DbgNode && getTag() == dwarf::DW_TAG_enumerator;
Devang Patela49be762009-09-30 22:34:41 +0000302}
303
Devang Patelcc481592012-02-04 00:59:25 +0000304/// isObjCProperty - Return true if the specified tag is DW_TAG
305bool DIDescriptor::isObjCProperty() const {
Eric Christopherc13fd6d2012-03-29 21:35:05 +0000306 return DbgNode && getTag() == dwarf::DW_TAG_APPLE_property;
Devang Patelcc481592012-02-04 00:59:25 +0000307}
Devang Patel9fda4bd2009-08-31 18:49:10 +0000308//===----------------------------------------------------------------------===//
309// Simple Descriptor Constructors and other Methods
310//===----------------------------------------------------------------------===//
311
Devang Patel32cc43c2010-05-07 20:54:48 +0000312DIType::DIType(const MDNode *N) : DIScope(N) {
Devang Patel9fda4bd2009-08-31 18:49:10 +0000313 if (!N) return;
314 if (!isBasicType() && !isDerivedType() && !isCompositeType()) {
315 DbgNode = 0;
316 }
317}
318
Devang Patel758e7d72009-01-05 18:33:01 +0000319unsigned DIArray::getNumElements() const {
Devang Patel3b548aa2010-03-08 20:52:55 +0000320 if (!DbgNode)
321 return 0;
Chris Lattner9b493022009-12-31 01:22:29 +0000322 return DbgNode->getNumOperands();
Devang Patel758e7d72009-01-05 18:33:01 +0000323}
Chris Lattner6d389332008-11-10 02:56:27 +0000324
Devang Patelf03c9be2009-07-22 18:23:44 +0000325/// replaceAllUsesWith - Replace all uses of debug info referenced by
Dan Gohman093cb792010-07-21 18:54:18 +0000326/// this descriptor.
Dan Gohman16a5d982010-08-20 22:02:26 +0000327void DIType::replaceAllUsesWith(DIDescriptor &D) {
Devang Patel3b548aa2010-03-08 20:52:55 +0000328 if (!DbgNode)
Devang Patelf03c9be2009-07-22 18:23:44 +0000329 return;
330
Daniel Dunbar03564832009-09-22 02:03:18 +0000331 // Since we use a TrackingVH for the node, its easy for clients to manufacture
332 // legitimate situations where they want to replaceAllUsesWith() on something
333 // which, due to uniquing, has merged with the source. We shield clients from
334 // this detail by allowing a value to be replaced with replaceAllUsesWith()
335 // itself.
Devang Patel4423abd2010-05-07 18:19:32 +0000336 if (DbgNode != D) {
Devang Patel32cc43c2010-05-07 20:54:48 +0000337 MDNode *Node = const_cast<MDNode*>(DbgNode);
338 const MDNode *DN = D;
339 const Value *V = cast_or_null<Value>(DN);
340 Node->replaceAllUsesWith(const_cast<Value*>(V));
Dan Gohman16a5d982010-08-20 22:02:26 +0000341 MDNode::deleteTemporary(Node);
Daniel Dunbar03564832009-09-22 02:03:18 +0000342 }
Devang Patelf03c9be2009-07-22 18:23:44 +0000343}
344
Devang Patel81c3c872010-12-08 20:18:20 +0000345/// replaceAllUsesWith - Replace all uses of debug info referenced by
346/// this descriptor.
347void DIType::replaceAllUsesWith(MDNode *D) {
348 if (!DbgNode)
349 return;
350
351 // Since we use a TrackingVH for the node, its easy for clients to manufacture
352 // legitimate situations where they want to replaceAllUsesWith() on something
353 // which, due to uniquing, has merged with the source. We shield clients from
354 // this detail by allowing a value to be replaced with replaceAllUsesWith()
355 // itself.
356 if (DbgNode != D) {
357 MDNode *Node = const_cast<MDNode*>(DbgNode);
358 const MDNode *DN = D;
359 const Value *V = cast_or_null<Value>(DN);
360 Node->replaceAllUsesWith(const_cast<Value*>(V));
361 MDNode::deleteTemporary(Node);
362 }
363}
364
Devang Pateldfd6ec32011-08-15 17:57:41 +0000365/// isUnsignedDIType - Return true if type encoding is unsigned.
366bool DIType::isUnsignedDIType() {
367 DIDerivedType DTy(DbgNode);
368 if (DTy.Verify())
369 return DTy.getTypeDerivedFrom().isUnsignedDIType();
370
371 DIBasicType BTy(DbgNode);
372 if (BTy.Verify()) {
373 unsigned Encoding = BTy.getEncoding();
374 if (Encoding == dwarf::DW_ATE_unsigned ||
375 Encoding == dwarf::DW_ATE_unsigned_char)
376 return true;
377 }
378 return false;
379}
380
Devang Patel44afc822009-01-19 23:21:49 +0000381/// Verify - Verify that a compile unit is well formed.
382bool DICompileUnit::Verify() const {
Devang Patel3b548aa2010-03-08 20:52:55 +0000383 if (!DbgNode)
Devang Patel44afc822009-01-19 23:21:49 +0000384 return false;
Devang Patel2d9caf92009-11-25 17:36:49 +0000385 StringRef N = getFilename();
386 if (N.empty())
Bill Wendlingfa54bc22009-03-13 04:39:26 +0000387 return false;
Devang Patel44afc822009-01-19 23:21:49 +0000388 // It is possible that directory and produce string is empty.
Bill Wendlingfa54bc22009-03-13 04:39:26 +0000389 return true;
Devang Patel44afc822009-01-19 23:21:49 +0000390}
391
Eric Christopher70e1bd82012-03-29 08:42:56 +0000392/// Verify - Verify that an ObjC property is well formed.
393bool DIObjCProperty::Verify() const {
394 if (!DbgNode)
395 return false;
396 unsigned Tag = getTag();
Eric Christopherc13fd6d2012-03-29 21:35:05 +0000397 if (Tag != dwarf::DW_TAG_APPLE_property) return false;
Eric Christopher70e1bd82012-03-29 08:42:56 +0000398 DIType Ty = getType();
399 if (!Ty.Verify()) return false;
400
401 // Don't worry about the rest of the strings for now.
402 return true;
403}
404
Devang Patel44afc822009-01-19 23:21:49 +0000405/// Verify - Verify that a type descriptor is well formed.
406bool DIType::Verify() const {
Devang Patel3b548aa2010-03-08 20:52:55 +0000407 if (!DbgNode)
Devang Patel44afc822009-01-19 23:21:49 +0000408 return false;
Devang Pateleb1bb4e2011-08-16 22:09:43 +0000409 if (getContext() && !getContext().Verify())
Devang Patel44afc822009-01-19 23:21:49 +0000410 return false;
Devang Patel415c5512010-11-02 20:41:13 +0000411 unsigned Tag = getTag();
412 if (!isBasicType() && Tag != dwarf::DW_TAG_const_type &&
413 Tag != dwarf::DW_TAG_volatile_type && Tag != dwarf::DW_TAG_pointer_type &&
Eric Christopherb5cf66c2012-05-19 01:36:37 +0000414 Tag != dwarf::DW_TAG_reference_type &&
415 Tag != dwarf::DW_TAG_rvalue_reference_type &&
416 Tag != dwarf::DW_TAG_restrict_type && Tag != dwarf::DW_TAG_vector_type &&
417 Tag != dwarf::DW_TAG_array_type &&
418 Tag != dwarf::DW_TAG_enumeration_type &&
419 Tag != dwarf::DW_TAG_subroutine_type &&
420 getFilename().empty())
Devang Patel44afc822009-01-19 23:21:49 +0000421 return false;
422 return true;
423}
424
Devang Patela8652672010-08-23 18:25:56 +0000425/// Verify - Verify that a basic type descriptor is well formed.
426bool DIBasicType::Verify() const {
427 return isBasicType();
428}
429
430/// Verify - Verify that a derived type descriptor is well formed.
431bool DIDerivedType::Verify() const {
432 return isDerivedType();
433}
434
Devang Patel44afc822009-01-19 23:21:49 +0000435/// Verify - Verify that a composite type descriptor is well formed.
436bool DICompositeType::Verify() const {
Devang Patel3b548aa2010-03-08 20:52:55 +0000437 if (!DbgNode)
Devang Patel44afc822009-01-19 23:21:49 +0000438 return false;
Devang Pateleb1bb4e2011-08-16 22:09:43 +0000439 if (getContext() && !getContext().Verify())
Devang Patel44afc822009-01-19 23:21:49 +0000440 return false;
441
Devang Patel44afc822009-01-19 23:21:49 +0000442 return true;
443}
444
445/// Verify - Verify that a subprogram descriptor is well formed.
446bool DISubprogram::Verify() const {
Devang Patel3b548aa2010-03-08 20:52:55 +0000447 if (!DbgNode)
Devang Patel44afc822009-01-19 23:21:49 +0000448 return false;
Daniel Dunbarc418d6b2009-09-19 20:40:05 +0000449
Devang Pateleb1bb4e2011-08-16 22:09:43 +0000450 if (getContext() && !getContext().Verify())
Devang Patel44afc822009-01-19 23:21:49 +0000451 return false;
452
453 DICompositeType Ty = getType();
Devang Patel3b548aa2010-03-08 20:52:55 +0000454 if (!Ty.Verify())
Devang Patel44afc822009-01-19 23:21:49 +0000455 return false;
456 return true;
457}
458
459/// Verify - Verify that a global variable descriptor is well formed.
460bool DIGlobalVariable::Verify() const {
Devang Patel3b548aa2010-03-08 20:52:55 +0000461 if (!DbgNode)
Devang Patel44afc822009-01-19 23:21:49 +0000462 return false;
Daniel Dunbarc418d6b2009-09-19 20:40:05 +0000463
Devang Patel2d9caf92009-11-25 17:36:49 +0000464 if (getDisplayName().empty())
Devang Patelcc11371b2009-11-06 17:58:12 +0000465 return false;
466
Devang Pateleb1bb4e2011-08-16 22:09:43 +0000467 if (getContext() && !getContext().Verify())
Devang Patel44afc822009-01-19 23:21:49 +0000468 return false;
469
470 DIType Ty = getType();
471 if (!Ty.Verify())
472 return false;
473
Devang Patelc7cf14f2010-08-09 21:39:24 +0000474 if (!getGlobal() && !getConstant())
Devang Patel44afc822009-01-19 23:21:49 +0000475 return false;
476
477 return true;
478}
479
480/// Verify - Verify that a variable descriptor is well formed.
481bool DIVariable::Verify() const {
Devang Patel3b548aa2010-03-08 20:52:55 +0000482 if (!DbgNode)
Devang Patel44afc822009-01-19 23:21:49 +0000483 return false;
Daniel Dunbarc418d6b2009-09-19 20:40:05 +0000484
Devang Pateleb1bb4e2011-08-16 22:09:43 +0000485 if (getContext() && !getContext().Verify())
Devang Patel2c4d69d2010-05-07 21:42:24 +0000486 return false;
487
Devang Patel44afc822009-01-19 23:21:49 +0000488 DIType Ty = getType();
489 if (!Ty.Verify())
490 return false;
491
Devang Patel44afc822009-01-19 23:21:49 +0000492 return true;
493}
494
Devang Patel3b548aa2010-03-08 20:52:55 +0000495/// Verify - Verify that a location descriptor is well formed.
496bool DILocation::Verify() const {
497 if (!DbgNode)
498 return false;
Jim Grosbach6cd0deb2010-07-21 21:36:25 +0000499
Devang Patel3b548aa2010-03-08 20:52:55 +0000500 return DbgNode->getNumOperands() == 4;
501}
502
Devang Patel54c59312010-05-07 23:04:32 +0000503/// Verify - Verify that a namespace descriptor is well formed.
504bool DINameSpace::Verify() const {
505 if (!DbgNode)
506 return false;
507 if (getName().empty())
508 return false;
Devang Patel54c59312010-05-07 23:04:32 +0000509 return true;
510}
511
Devang Patel528987a2009-02-17 21:23:59 +0000512/// getOriginalTypeSize - If this type is derived from a base type then
513/// return base type size.
514uint64_t DIDerivedType::getOriginalTypeSize() const {
Devang Patelf05d5722009-11-04 23:48:00 +0000515 unsigned Tag = getTag();
Eric Christopher27886c62011-12-16 23:42:45 +0000516
Bill Wendling3b70d782012-06-26 23:22:18 +0000517 if (Tag != dwarf::DW_TAG_member && Tag != dwarf::DW_TAG_typedef &&
518 Tag != dwarf::DW_TAG_const_type && Tag != dwarf::DW_TAG_volatile_type &&
519 Tag != dwarf::DW_TAG_restrict_type)
520 return getSizeInBits();
Jim Grosbach6cd0deb2010-07-21 21:36:25 +0000521
Bill Wendling3b70d782012-06-26 23:22:18 +0000522 DIType BaseType = getTypeDerivedFrom();
523
524 // If this type is not derived from any type then take conservative approach.
525 if (!BaseType.isValid())
526 return getSizeInBits();
527
528 // If this is a derived type, go ahead and get the base type, unless it's a
529 // reference then it's just the size of the field. Pointer types have no need
530 // of this since they're a different type of qualification on the type.
531 if (BaseType.getTag() == dwarf::DW_TAG_reference_type ||
532 BaseType.getTag() == dwarf::DW_TAG_rvalue_reference_type)
533 return getSizeInBits();
534
535 if (BaseType.isDerivedType())
536 return DIDerivedType(BaseType).getOriginalTypeSize();
537
538 return BaseType.getSizeInBits();
Devang Patel528987a2009-02-17 21:23:59 +0000539}
Devang Patel44afc822009-01-19 23:21:49 +0000540
Devang Patel44882172012-02-06 17:49:43 +0000541/// getObjCProperty - Return property node, if this ivar is associated with one.
542MDNode *DIDerivedType::getObjCProperty() const {
543 if (getVersion() <= LLVMDebugVersion11 || DbgNode->getNumOperands() <= 10)
544 return NULL;
545 return dyn_cast_or_null<MDNode>(DbgNode->getOperand(10));
546}
547
Jim Grosbach6cd0deb2010-07-21 21:36:25 +0000548/// isInlinedFnArgument - Return true if this variable provides debugging
Devang Patel03955532010-04-29 20:40:36 +0000549/// information for an inlined function arguments.
550bool DIVariable::isInlinedFnArgument(const Function *CurFn) {
551 assert(CurFn && "Invalid function");
552 if (!getContext().isSubprogram())
553 return false;
Jim Grosbach6cd0deb2010-07-21 21:36:25 +0000554 // This variable is not inlined function argument if its scope
Devang Patel03955532010-04-29 20:40:36 +0000555 // does not describe current function.
Bill Wendling3b70d782012-06-26 23:22:18 +0000556 return !DISubprogram(getContext()).describes(CurFn);
Devang Patel03955532010-04-29 20:40:36 +0000557}
558
Devang Patel1be3b532009-04-15 00:06:07 +0000559/// describes - Return true if this subprogram provides debugging
560/// information for the function F.
561bool DISubprogram::describes(const Function *F) {
Chris Lattner047fd2f2009-12-29 09:22:47 +0000562 assert(F && "Invalid function");
Devang Pateld119da52010-06-16 06:42:02 +0000563 if (F == getFunction())
564 return true;
Devang Patel2d9caf92009-11-25 17:36:49 +0000565 StringRef Name = getLinkageName();
566 if (Name.empty())
Devang Patelb2969422009-09-29 18:40:58 +0000567 Name = getName();
Devang Patel2d9caf92009-11-25 17:36:49 +0000568 if (F->getName() == Name)
Devang Patel1be3b532009-04-15 00:06:07 +0000569 return true;
570 return false;
571}
572
Jim Grosbach6cd0deb2010-07-21 21:36:25 +0000573unsigned DISubprogram::isOptimized() const {
Devang Patelb4e3b902010-04-30 19:38:23 +0000574 assert (DbgNode && "Invalid subprogram descriptor!");
575 if (DbgNode->getNumOperands() == 16)
576 return getUnsignedField(15);
577 return 0;
578}
579
Devang Patel59e27c52011-08-19 23:28:12 +0000580MDNode *DISubprogram::getVariablesNodes() const {
581 if (!DbgNode || DbgNode->getNumOperands() <= 19)
582 return NULL;
583 if (MDNode *Temp = dyn_cast_or_null<MDNode>(DbgNode->getOperand(19)))
584 return dyn_cast_or_null<MDNode>(Temp->getOperand(0));
585 return NULL;
586}
587
588DIArray DISubprogram::getVariables() const {
589 if (!DbgNode || DbgNode->getNumOperands() <= 19)
590 return DIArray();
591 if (MDNode *T = dyn_cast_or_null<MDNode>(DbgNode->getOperand(19)))
592 if (MDNode *A = dyn_cast_or_null<MDNode>(T->getOperand(0)))
593 return DIArray(A);
594 return DIArray();
595}
596
Devang Patel2d9caf92009-11-25 17:36:49 +0000597StringRef DIScope::getFilename() const {
Devang Patel8119fe872010-03-08 22:02:50 +0000598 if (!DbgNode)
599 return StringRef();
Eric Christopher6647b832011-10-11 22:59:11 +0000600 if (isLexicalBlockFile())
601 return DILexicalBlockFile(DbgNode).getFilename();
Jim Grosbach6cd0deb2010-07-21 21:36:25 +0000602 if (isLexicalBlock())
Devang Patela49be762009-09-30 22:34:41 +0000603 return DILexicalBlock(DbgNode).getFilename();
Chris Lattner047fd2f2009-12-29 09:22:47 +0000604 if (isSubprogram())
Devang Patela49be762009-09-30 22:34:41 +0000605 return DISubprogram(DbgNode).getFilename();
Chris Lattner047fd2f2009-12-29 09:22:47 +0000606 if (isCompileUnit())
Devang Patela49be762009-09-30 22:34:41 +0000607 return DICompileUnit(DbgNode).getFilename();
Chris Lattner047fd2f2009-12-29 09:22:47 +0000608 if (isNameSpace())
Devang Patel1f4690c2009-12-15 19:16:48 +0000609 return DINameSpace(DbgNode).getFilename();
Devang Patel8119fe872010-03-08 22:02:50 +0000610 if (isType())
611 return DIType(DbgNode).getFilename();
Devang Patel2e520f62010-03-08 22:27:22 +0000612 if (isFile())
613 return DIFile(DbgNode).getFilename();
Craig Toppera2886c22012-02-07 05:05:23 +0000614 llvm_unreachable("Invalid DIScope!");
Devang Patela49be762009-09-30 22:34:41 +0000615}
616
Devang Patel2d9caf92009-11-25 17:36:49 +0000617StringRef DIScope::getDirectory() const {
Devang Patel8119fe872010-03-08 22:02:50 +0000618 if (!DbgNode)
619 return StringRef();
Eric Christopher6647b832011-10-11 22:59:11 +0000620 if (isLexicalBlockFile())
621 return DILexicalBlockFile(DbgNode).getDirectory();
Jim Grosbach6cd0deb2010-07-21 21:36:25 +0000622 if (isLexicalBlock())
Devang Patela49be762009-09-30 22:34:41 +0000623 return DILexicalBlock(DbgNode).getDirectory();
Chris Lattner047fd2f2009-12-29 09:22:47 +0000624 if (isSubprogram())
Devang Patela49be762009-09-30 22:34:41 +0000625 return DISubprogram(DbgNode).getDirectory();
Chris Lattner047fd2f2009-12-29 09:22:47 +0000626 if (isCompileUnit())
Devang Patela49be762009-09-30 22:34:41 +0000627 return DICompileUnit(DbgNode).getDirectory();
Chris Lattner047fd2f2009-12-29 09:22:47 +0000628 if (isNameSpace())
Devang Patel1f4690c2009-12-15 19:16:48 +0000629 return DINameSpace(DbgNode).getDirectory();
Devang Patel8119fe872010-03-08 22:02:50 +0000630 if (isType())
631 return DIType(DbgNode).getDirectory();
Devang Patel2e520f62010-03-08 22:27:22 +0000632 if (isFile())
633 return DIFile(DbgNode).getDirectory();
Craig Toppera2886c22012-02-07 05:05:23 +0000634 llvm_unreachable("Invalid DIScope!");
Devang Patela49be762009-09-30 22:34:41 +0000635}
636
Devang Pateleb1bb4e2011-08-16 22:09:43 +0000637DIArray DICompileUnit::getEnumTypes() const {
638 if (!DbgNode || DbgNode->getNumOperands() < 14)
639 return DIArray();
640
641 if (MDNode *N = dyn_cast_or_null<MDNode>(DbgNode->getOperand(10)))
642 if (MDNode *A = dyn_cast_or_null<MDNode>(N->getOperand(0)))
643 return DIArray(A);
644 return DIArray();
645}
646
647DIArray DICompileUnit::getRetainedTypes() const {
648 if (!DbgNode || DbgNode->getNumOperands() < 14)
649 return DIArray();
650
651 if (MDNode *N = dyn_cast_or_null<MDNode>(DbgNode->getOperand(11)))
652 if (MDNode *A = dyn_cast_or_null<MDNode>(N->getOperand(0)))
653 return DIArray(A);
654 return DIArray();
655}
656
657DIArray DICompileUnit::getSubprograms() const {
658 if (!DbgNode || DbgNode->getNumOperands() < 14)
659 return DIArray();
660
661 if (MDNode *N = dyn_cast_or_null<MDNode>(DbgNode->getOperand(12)))
NAKAMURA Takumi7fe8a402012-12-01 02:23:45 +0000662 if (N->getNumOperands() > 0)
663 if (MDNode *A = dyn_cast_or_null<MDNode>(N->getOperand(0)))
664 return DIArray(A);
Devang Pateleb1bb4e2011-08-16 22:09:43 +0000665 return DIArray();
666}
667
668
669DIArray DICompileUnit::getGlobalVariables() const {
670 if (!DbgNode || DbgNode->getNumOperands() < 14)
671 return DIArray();
672
673 if (MDNode *N = dyn_cast_or_null<MDNode>(DbgNode->getOperand(13)))
674 if (MDNode *A = dyn_cast_or_null<MDNode>(N->getOperand(0)))
675 return DIArray(A);
676 return DIArray();
677}
678
Devang Patel364bf042010-11-10 22:19:21 +0000679/// fixupObjcLikeName - Replace contains special characters used
680/// in a typical Objective-C names with '.' in a given string.
Benjamin Kramer9319e9c2011-06-18 14:42:42 +0000681static void fixupObjcLikeName(StringRef Str, SmallVectorImpl<char> &Out) {
682 bool isObjCLike = false;
Devang Patel364bf042010-11-10 22:19:21 +0000683 for (size_t i = 0, e = Str.size(); i < e; ++i) {
684 char C = Str[i];
Benjamin Kramer9319e9c2011-06-18 14:42:42 +0000685 if (C == '[')
686 isObjCLike = true;
687
688 if (isObjCLike && (C == '[' || C == ']' || C == ' ' || C == ':' ||
689 C == '+' || C == '(' || C == ')'))
690 Out.push_back('.');
691 else
692 Out.push_back(C);
Devang Patel364bf042010-11-10 22:19:21 +0000693 }
694}
695
Eric Christopherb1d992f2012-11-20 00:15:36 +0000696/// getFnSpecificMDNode - Return a NameMDNode, if available, that is
Devang Patel364bf042010-11-10 22:19:21 +0000697/// suitable to hold function specific information.
Devang Patel59e27c52011-08-19 23:28:12 +0000698NamedMDNode *llvm::getFnSpecificMDNode(const Module &M, DISubprogram Fn) {
Benjamin Kramer9319e9c2011-06-18 14:42:42 +0000699 SmallString<32> Name = StringRef("llvm.dbg.lv.");
Devang Patel59e27c52011-08-19 23:28:12 +0000700 StringRef FName = "fn";
701 if (Fn.getFunction())
702 FName = Fn.getFunction()->getName();
703 else
704 FName = Fn.getName();
705 char One = '\1';
706 if (FName.startswith(StringRef(&One, 1)))
707 FName = FName.substr(1);
708 fixupObjcLikeName(FName, Name);
Benjamin Kramer9319e9c2011-06-18 14:42:42 +0000709 return M.getNamedMetadata(Name.str());
Devang Patel364bf042010-11-10 22:19:21 +0000710}
711
Duncan Sandsbf577d62011-03-02 20:30:37 +0000712/// getOrInsertFnSpecificMDNode - Return a NameMDNode that is suitable
713/// to hold function specific information.
Devang Patel59e27c52011-08-19 23:28:12 +0000714NamedMDNode *llvm::getOrInsertFnSpecificMDNode(Module &M, DISubprogram Fn) {
Benjamin Kramer9319e9c2011-06-18 14:42:42 +0000715 SmallString<32> Name = StringRef("llvm.dbg.lv.");
Devang Patel59e27c52011-08-19 23:28:12 +0000716 StringRef FName = "fn";
717 if (Fn.getFunction())
718 FName = Fn.getFunction()->getName();
719 else
720 FName = Fn.getName();
721 char One = '\1';
722 if (FName.startswith(StringRef(&One, 1)))
723 FName = FName.substr(1);
724 fixupObjcLikeName(FName, Name);
Eric Christopherb1d992f2012-11-20 00:15:36 +0000725
Benjamin Kramer9319e9c2011-06-18 14:42:42 +0000726 return M.getOrInsertNamedMetadata(Name.str());
Devang Patel7a554812010-09-28 18:08:20 +0000727}
728
Devang Patelcfa82a32011-07-19 19:41:54 +0000729/// createInlinedVariable - Create a new inlined variable based on current
730/// variable.
731/// @param DV Current Variable.
732/// @param InlinedScope Location at current variable is inlined.
733DIVariable llvm::createInlinedVariable(MDNode *DV, MDNode *InlinedScope,
734 LLVMContext &VMContext) {
735 SmallVector<Value *, 16> Elts;
736 // Insert inlined scope as 7th element.
737 for (unsigned i = 0, e = DV->getNumOperands(); i != e; ++i)
738 i == 7 ? Elts.push_back(InlinedScope) :
739 Elts.push_back(DV->getOperand(i));
740 return DIVariable(MDNode::get(VMContext, Elts));
741}
Devang Patel7a554812010-09-28 18:08:20 +0000742
Devang Patelbb23a4a2011-08-10 21:50:54 +0000743/// cleanseInlinedVariable - Remove inlined scope from the variable.
744DIVariable llvm::cleanseInlinedVariable(MDNode *DV, LLVMContext &VMContext) {
745 SmallVector<Value *, 16> Elts;
746 // Insert inlined scope as 7th element.
747 for (unsigned i = 0, e = DV->getNumOperands(); i != e; ++i)
Eric Christopherb1d992f2012-11-20 00:15:36 +0000748 i == 7 ?
Bill Wendling16d944c2012-07-06 17:46:28 +0000749 Elts.push_back(Constant::getNullValue(Type::getInt32Ty(VMContext))):
Devang Patelbb23a4a2011-08-10 21:50:54 +0000750 Elts.push_back(DV->getOperand(i));
751 return DIVariable(MDNode::get(VMContext, Elts));
752}
753
Bill Wendlinge38859d2012-06-28 00:05:13 +0000754/// getDISubprogram - Find subprogram that is enclosing this scope.
755DISubprogram llvm::getDISubprogram(const MDNode *Scope) {
756 DIDescriptor D(Scope);
757 if (D.isSubprogram())
758 return DISubprogram(Scope);
759
760 if (D.isLexicalBlockFile())
761 return getDISubprogram(DILexicalBlockFile(Scope).getContext());
Eric Christopherb1d992f2012-11-20 00:15:36 +0000762
Bill Wendlinge38859d2012-06-28 00:05:13 +0000763 if (D.isLexicalBlock())
764 return getDISubprogram(DILexicalBlock(Scope).getContext());
765
766 return DISubprogram();
767}
768
769/// getDICompositeType - Find underlying composite type.
770DICompositeType llvm::getDICompositeType(DIType T) {
771 if (T.isCompositeType())
772 return DICompositeType(T);
773
774 if (T.isDerivedType())
775 return getDICompositeType(DIDerivedType(T).getTypeDerivedFrom());
776
777 return DICompositeType();
778}
779
780/// isSubprogramContext - Return true if Context is either a subprogram
781/// or another context nested inside a subprogram.
782bool llvm::isSubprogramContext(const MDNode *Context) {
783 if (!Context)
784 return false;
785 DIDescriptor D(Context);
786 if (D.isSubprogram())
787 return true;
788 if (D.isType())
789 return isSubprogramContext(DIType(Context).getContext());
790 return false;
791}
792
Devang Patel1da75552009-07-28 19:55:13 +0000793//===----------------------------------------------------------------------===//
Devang Patel3b4e8272009-07-30 18:25:15 +0000794// DebugInfoFinder implementations.
Devang Patel1da75552009-07-28 19:55:13 +0000795//===----------------------------------------------------------------------===//
796
Devang Patel3b4e8272009-07-30 18:25:15 +0000797/// processModule - Process entire module and collect debug info.
Eric Christopher58f41952012-11-19 22:42:15 +0000798void DebugInfoFinder::processModule(const Module &M) {
Devang Patel7973e782011-10-17 22:30:34 +0000799 if (NamedMDNode *CU_Nodes = M.getNamedMetadata("llvm.dbg.cu")) {
800 for (unsigned i = 0, e = CU_Nodes->getNumOperands(); i != e; ++i) {
801 DICompileUnit CU(CU_Nodes->getOperand(i));
802 addCompileUnit(CU);
803 if (CU.getVersion() > LLVMDebugVersion10) {
Devang Patela93cc252012-02-08 00:17:07 +0000804 DIArray GVs = CU.getGlobalVariables();
805 for (unsigned i = 0, e = GVs.getNumElements(); i != e; ++i) {
806 DIGlobalVariable DIG(GVs.getElement(i));
807 if (addGlobalVariable(DIG))
808 processType(DIG.getType());
809 }
810 DIArray SPs = CU.getSubprograms();
811 for (unsigned i = 0, e = SPs.getNumElements(); i != e; ++i)
812 processSubprogram(DISubprogram(SPs.getElement(i)));
813 DIArray EnumTypes = CU.getEnumTypes();
814 for (unsigned i = 0, e = EnumTypes.getNumElements(); i != e; ++i)
815 processType(DIType(EnumTypes.getElement(i)));
816 DIArray RetainedTypes = CU.getRetainedTypes();
817 for (unsigned i = 0, e = RetainedTypes.getNumElements(); i != e; ++i)
818 processType(DIType(RetainedTypes.getElement(i)));
819 return;
Devang Patel7973e782011-10-17 22:30:34 +0000820 }
821 }
822 }
Devang Patel5ea5d792011-09-06 17:40:08 +0000823
Eric Christopher58f41952012-11-19 22:42:15 +0000824 for (Module::const_iterator I = M.begin(), E = M.end(); I != E; ++I)
Eric Christopherb1d992f2012-11-20 00:15:36 +0000825 for (Function::const_iterator FI = (*I).begin(), FE = (*I).end();
826 FI != FE; ++FI)
827 for (BasicBlock::const_iterator BI = (*FI).begin(), BE = (*FI).end();
828 BI != BE; ++BI) {
Eric Christopher58f41952012-11-19 22:42:15 +0000829 if (const DbgDeclareInst *DDI = dyn_cast<DbgDeclareInst>(BI))
Devang Patel9d7de2a2009-07-31 18:18:52 +0000830 processDeclare(DDI);
Jim Grosbach6cd0deb2010-07-21 21:36:25 +0000831
Chris Lattner44714c92010-04-02 20:44:29 +0000832 DebugLoc Loc = BI->getDebugLoc();
833 if (Loc.isUnknown())
834 continue;
Jim Grosbach6cd0deb2010-07-21 21:36:25 +0000835
Chris Lattner44714c92010-04-02 20:44:29 +0000836 LLVMContext &Ctx = BI->getContext();
837 DIDescriptor Scope(Loc.getScope(Ctx));
Jim Grosbach6cd0deb2010-07-21 21:36:25 +0000838
Chris Lattner44714c92010-04-02 20:44:29 +0000839 if (Scope.isCompileUnit())
Devang Patelcfa8e9d2010-05-07 18:11:54 +0000840 addCompileUnit(DICompileUnit(Scope));
Chris Lattner44714c92010-04-02 20:44:29 +0000841 else if (Scope.isSubprogram())
Devang Patelcfa8e9d2010-05-07 18:11:54 +0000842 processSubprogram(DISubprogram(Scope));
Eric Christopher6647b832011-10-11 22:59:11 +0000843 else if (Scope.isLexicalBlockFile()) {
844 DILexicalBlockFile DBF = DILexicalBlockFile(Scope);
845 processLexicalBlock(DILexicalBlock(DBF.getScope()));
846 }
Chris Lattner44714c92010-04-02 20:44:29 +0000847 else if (Scope.isLexicalBlock())
Devang Patelcfa8e9d2010-05-07 18:11:54 +0000848 processLexicalBlock(DILexicalBlock(Scope));
Jim Grosbach6cd0deb2010-07-21 21:36:25 +0000849
Chris Lattner44714c92010-04-02 20:44:29 +0000850 if (MDNode *IA = Loc.getInlinedAt(Ctx))
851 processLocation(DILocation(IA));
Devang Patel1da75552009-07-28 19:55:13 +0000852 }
Devang Patel80ae3492009-08-28 23:24:31 +0000853
Devang Patelf7869a42010-06-28 05:53:08 +0000854 if (NamedMDNode *NMD = M.getNamedMetadata("llvm.dbg.gv")) {
855 for (unsigned i = 0, e = NMD->getNumOperands(); i != e; ++i) {
856 DIGlobalVariable DIG(cast<MDNode>(NMD->getOperand(i)));
857 if (addGlobalVariable(DIG)) {
Devang Patel5ea5d792011-09-06 17:40:08 +0000858 if (DIG.getVersion() <= LLVMDebugVersion10)
859 addCompileUnit(DIG.getCompileUnit());
Devang Patelf7869a42010-06-28 05:53:08 +0000860 processType(DIG.getType());
861 }
Devang Patel1da75552009-07-28 19:55:13 +0000862 }
863 }
Devang Patelf7869a42010-06-28 05:53:08 +0000864
865 if (NamedMDNode *NMD = M.getNamedMetadata("llvm.dbg.sp"))
866 for (unsigned i = 0, e = NMD->getNumOperands(); i != e; ++i)
867 processSubprogram(DISubprogram(NMD->getOperand(i)));
Devang Patel1da75552009-07-28 19:55:13 +0000868}
Daniel Dunbarc418d6b2009-09-19 20:40:05 +0000869
Devang Patel42c1d172009-11-10 22:05:35 +0000870/// processLocation - Process DILocation.
871void DebugInfoFinder::processLocation(DILocation Loc) {
Devang Patel3b548aa2010-03-08 20:52:55 +0000872 if (!Loc.Verify()) return;
Devang Patelcfa8e9d2010-05-07 18:11:54 +0000873 DIDescriptor S(Loc.getScope());
Devang Patel42c1d172009-11-10 22:05:35 +0000874 if (S.isCompileUnit())
Devang Patelcfa8e9d2010-05-07 18:11:54 +0000875 addCompileUnit(DICompileUnit(S));
Devang Patel42c1d172009-11-10 22:05:35 +0000876 else if (S.isSubprogram())
Devang Patelcfa8e9d2010-05-07 18:11:54 +0000877 processSubprogram(DISubprogram(S));
Devang Patel42c1d172009-11-10 22:05:35 +0000878 else if (S.isLexicalBlock())
Devang Patelcfa8e9d2010-05-07 18:11:54 +0000879 processLexicalBlock(DILexicalBlock(S));
Eric Christopher6647b832011-10-11 22:59:11 +0000880 else if (S.isLexicalBlockFile()) {
881 DILexicalBlockFile DBF = DILexicalBlockFile(S);
882 processLexicalBlock(DILexicalBlock(DBF.getScope()));
883 }
Devang Patel42c1d172009-11-10 22:05:35 +0000884 processLocation(Loc.getOrigLocation());
885}
886
Devang Patel3b4e8272009-07-30 18:25:15 +0000887/// processType - Process DIType.
888void DebugInfoFinder::processType(DIType DT) {
Devang Pateld0658ef2009-08-10 22:09:58 +0000889 if (!addType(DT))
Devang Patel1da75552009-07-28 19:55:13 +0000890 return;
Devang Patel5ea5d792011-09-06 17:40:08 +0000891 if (DT.getVersion() <= LLVMDebugVersion10)
892 addCompileUnit(DT.getCompileUnit());
Devang Patel9fda4bd2009-08-31 18:49:10 +0000893 if (DT.isCompositeType()) {
Devang Patelcfa8e9d2010-05-07 18:11:54 +0000894 DICompositeType DCT(DT);
Devang Patel3b4e8272009-07-30 18:25:15 +0000895 processType(DCT.getTypeDerivedFrom());
Devang Patel1da75552009-07-28 19:55:13 +0000896 DIArray DA = DCT.getTypeArray();
Devang Patel3b548aa2010-03-08 20:52:55 +0000897 for (unsigned i = 0, e = DA.getNumElements(); i != e; ++i) {
898 DIDescriptor D = DA.getElement(i);
899 if (D.isType())
Devang Patelcfa8e9d2010-05-07 18:11:54 +0000900 processType(DIType(D));
Devang Patel3b548aa2010-03-08 20:52:55 +0000901 else if (D.isSubprogram())
Devang Patelcfa8e9d2010-05-07 18:11:54 +0000902 processSubprogram(DISubprogram(D));
Devang Patel3b548aa2010-03-08 20:52:55 +0000903 }
Devang Patel9fda4bd2009-08-31 18:49:10 +0000904 } else if (DT.isDerivedType()) {
Devang Patelcfa8e9d2010-05-07 18:11:54 +0000905 DIDerivedType DDT(DT);
Devang Patel3b548aa2010-03-08 20:52:55 +0000906 processType(DDT.getTypeDerivedFrom());
Devang Patel1da75552009-07-28 19:55:13 +0000907 }
908}
909
Devang Patel55571bd2009-10-07 22:04:08 +0000910/// processLexicalBlock
911void DebugInfoFinder::processLexicalBlock(DILexicalBlock LB) {
Devang Patel55571bd2009-10-07 22:04:08 +0000912 DIScope Context = LB.getContext();
913 if (Context.isLexicalBlock())
Devang Patelcfa8e9d2010-05-07 18:11:54 +0000914 return processLexicalBlock(DILexicalBlock(Context));
Eric Christopher6647b832011-10-11 22:59:11 +0000915 else if (Context.isLexicalBlockFile()) {
916 DILexicalBlockFile DBF = DILexicalBlockFile(Context);
917 return processLexicalBlock(DILexicalBlock(DBF.getScope()));
918 }
Devang Patel55571bd2009-10-07 22:04:08 +0000919 else
Devang Patelcfa8e9d2010-05-07 18:11:54 +0000920 return processSubprogram(DISubprogram(Context));
Devang Patel55571bd2009-10-07 22:04:08 +0000921}
922
Devang Patel3b4e8272009-07-30 18:25:15 +0000923/// processSubprogram - Process DISubprogram.
924void DebugInfoFinder::processSubprogram(DISubprogram SP) {
Devang Patel1da75552009-07-28 19:55:13 +0000925 if (!addSubprogram(SP))
926 return;
Devang Patel5ea5d792011-09-06 17:40:08 +0000927 if (SP.getVersion() <= LLVMDebugVersion10)
928 addCompileUnit(SP.getCompileUnit());
Devang Patel3b4e8272009-07-30 18:25:15 +0000929 processType(SP.getType());
Devang Patel1da75552009-07-28 19:55:13 +0000930}
931
Devang Patel9d7de2a2009-07-31 18:18:52 +0000932/// processDeclare - Process DbgDeclareInst.
Eric Christopher58f41952012-11-19 22:42:15 +0000933void DebugInfoFinder::processDeclare(const DbgDeclareInst *DDI) {
Devang Patel3b548aa2010-03-08 20:52:55 +0000934 MDNode *N = dyn_cast<MDNode>(DDI->getVariable());
935 if (!N) return;
936
937 DIDescriptor DV(N);
938 if (!DV.isVariable())
Devang Patel9d7de2a2009-07-31 18:18:52 +0000939 return;
940
Devang Patelcfa8e9d2010-05-07 18:11:54 +0000941 if (!NodesSeen.insert(DV))
Devang Patel9d7de2a2009-07-31 18:18:52 +0000942 return;
Devang Patel5ea5d792011-09-06 17:40:08 +0000943 if (DIVariable(N).getVersion() <= LLVMDebugVersion10)
944 addCompileUnit(DIVariable(N).getCompileUnit());
Devang Patel3b548aa2010-03-08 20:52:55 +0000945 processType(DIVariable(N).getType());
Devang Patel9d7de2a2009-07-31 18:18:52 +0000946}
947
Devang Pateld0658ef2009-08-10 22:09:58 +0000948/// addType - Add type into Tys.
949bool DebugInfoFinder::addType(DIType DT) {
Devang Patel3b548aa2010-03-08 20:52:55 +0000950 if (!DT.isValid())
Devang Pateld0658ef2009-08-10 22:09:58 +0000951 return false;
952
Devang Patelcfa8e9d2010-05-07 18:11:54 +0000953 if (!NodesSeen.insert(DT))
Devang Pateld0658ef2009-08-10 22:09:58 +0000954 return false;
955
Devang Patelcfa8e9d2010-05-07 18:11:54 +0000956 TYs.push_back(DT);
Devang Pateld0658ef2009-08-10 22:09:58 +0000957 return true;
958}
959
Devang Patel1da75552009-07-28 19:55:13 +0000960/// addCompileUnit - Add compile unit into CUs.
Devang Patel3b4e8272009-07-30 18:25:15 +0000961bool DebugInfoFinder::addCompileUnit(DICompileUnit CU) {
Devang Patel3b548aa2010-03-08 20:52:55 +0000962 if (!CU.Verify())
Devang Patel1da75552009-07-28 19:55:13 +0000963 return false;
964
Devang Patelcfa8e9d2010-05-07 18:11:54 +0000965 if (!NodesSeen.insert(CU))
Devang Patel1da75552009-07-28 19:55:13 +0000966 return false;
967
Devang Patelcfa8e9d2010-05-07 18:11:54 +0000968 CUs.push_back(CU);
Devang Patel1da75552009-07-28 19:55:13 +0000969 return true;
970}
Daniel Dunbarc418d6b2009-09-19 20:40:05 +0000971
Devang Patel1da75552009-07-28 19:55:13 +0000972/// addGlobalVariable - Add global variable into GVs.
Devang Patel3b4e8272009-07-30 18:25:15 +0000973bool DebugInfoFinder::addGlobalVariable(DIGlobalVariable DIG) {
Devang Patelcfa8e9d2010-05-07 18:11:54 +0000974 if (!DIDescriptor(DIG).isGlobalVariable())
Devang Patel1da75552009-07-28 19:55:13 +0000975 return false;
976
Devang Patelcfa8e9d2010-05-07 18:11:54 +0000977 if (!NodesSeen.insert(DIG))
Devang Patel1da75552009-07-28 19:55:13 +0000978 return false;
979
Devang Patelcfa8e9d2010-05-07 18:11:54 +0000980 GVs.push_back(DIG);
Devang Patel1da75552009-07-28 19:55:13 +0000981 return true;
982}
983
984// addSubprogram - Add subprgoram into SPs.
Devang Patel3b4e8272009-07-30 18:25:15 +0000985bool DebugInfoFinder::addSubprogram(DISubprogram SP) {
Devang Patelcfa8e9d2010-05-07 18:11:54 +0000986 if (!DIDescriptor(SP).isSubprogram())
Devang Patel1da75552009-07-28 19:55:13 +0000987 return false;
Daniel Dunbarc418d6b2009-09-19 20:40:05 +0000988
Devang Patelcfa8e9d2010-05-07 18:11:54 +0000989 if (!NodesSeen.insert(SP))
Devang Patel1da75552009-07-28 19:55:13 +0000990 return false;
991
Devang Patelcfa8e9d2010-05-07 18:11:54 +0000992 SPs.push_back(SP);
Devang Patel1da75552009-07-28 19:55:13 +0000993 return true;
994}
995
Bill Wendling3b70d782012-06-26 23:22:18 +0000996//===----------------------------------------------------------------------===//
997// DIDescriptor: dump routines for all descriptors.
998//===----------------------------------------------------------------------===//
999
1000/// dump - Print descriptor to dbgs() with a newline.
1001void DIDescriptor::dump() const {
1002 print(dbgs()); dbgs() << '\n';
1003}
1004
1005/// print - Print descriptor.
1006void DIDescriptor::print(raw_ostream &OS) const {
1007 if (!DbgNode) return;
1008
Bill Wendlinga2ccbf02012-06-28 02:17:58 +00001009 if (const char *Tag = dwarf::TagString(getTag()))
1010 OS << "[ " << Tag << " ]";
Bill Wendling3b70d782012-06-26 23:22:18 +00001011
1012 if (this->isSubrange()) {
1013 DISubrange(DbgNode).printInternal(OS);
Bill Wendling3b70d782012-06-26 23:22:18 +00001014 } else if (this->isCompileUnit()) {
1015 DICompileUnit(DbgNode).printInternal(OS);
1016 } else if (this->isFile()) {
1017 DIFile(DbgNode).printInternal(OS);
1018 } else if (this->isEnumerator()) {
1019 DIEnumerator(DbgNode).printInternal(OS);
1020 } else if (this->isBasicType()) {
1021 DIType(DbgNode).printInternal(OS);
1022 } else if (this->isDerivedType()) {
1023 DIDerivedType(DbgNode).printInternal(OS);
1024 } else if (this->isCompositeType()) {
1025 DICompositeType(DbgNode).printInternal(OS);
1026 } else if (this->isSubprogram()) {
1027 DISubprogram(DbgNode).printInternal(OS);
1028 } else if (this->isGlobalVariable()) {
1029 DIGlobalVariable(DbgNode).printInternal(OS);
1030 } else if (this->isVariable()) {
1031 DIVariable(DbgNode).printInternal(OS);
Bill Wendlingaa02e362012-07-06 19:12:31 +00001032 } else if (this->isObjCProperty()) {
1033 DIObjCProperty(DbgNode).printInternal(OS);
Bill Wendling32705822012-07-06 23:06:16 +00001034 } else if (this->isScope()) {
1035 DIScope(DbgNode).printInternal(OS);
Bill Wendling3b70d782012-06-26 23:22:18 +00001036 }
1037}
1038
1039void DISubrange::printInternal(raw_ostream &OS) const {
1040 OS << " [" << getLo() << ", " << getHi() << ']';
1041}
1042
1043void DIScope::printInternal(raw_ostream &OS) const {
1044 OS << " [" << getDirectory() << "/" << getFilename() << ']';
1045}
1046
1047void DICompileUnit::printInternal(raw_ostream &OS) const {
1048 DIScope::printInternal(OS);
1049 if (unsigned Lang = getLanguage())
1050 OS << " [" << dwarf::LanguageString(Lang) << ']';
1051}
1052
1053void DIEnumerator::printInternal(raw_ostream &OS) const {
1054 OS << " [" << getName() << " :: " << getEnumValue() << ']';
1055}
1056
1057void DIType::printInternal(raw_ostream &OS) const {
1058 if (!DbgNode) return;
1059
1060 StringRef Res = getName();
1061 if (!Res.empty())
1062 OS << " [" << Res << "]";
1063
1064 // TODO: Print context?
1065
1066 OS << " [line " << getLineNumber()
1067 << ", size " << getSizeInBits()
1068 << ", align " << getAlignInBits()
1069 << ", offset " << getOffsetInBits();
1070 if (isBasicType())
Eric Christopherb1d992f2012-11-20 00:15:36 +00001071 if (const char *Enc =
Bill Wendling74ac0232012-06-28 02:12:20 +00001072 dwarf::AttributeEncodingString(DIBasicType(DbgNode).getEncoding()))
1073 OS << ", enc " << Enc;
Bill Wendling3b70d782012-06-26 23:22:18 +00001074 OS << "]";
1075
1076 if (isPrivate())
1077 OS << " [private]";
1078 else if (isProtected())
1079 OS << " [protected]";
1080
1081 if (isForwardDecl())
1082 OS << " [fwd]";
1083}
1084
1085void DIDerivedType::printInternal(raw_ostream &OS) const {
1086 DIType::printInternal(OS);
1087 OS << " [from " << getTypeDerivedFrom().getName() << ']';
1088}
1089
1090void DICompositeType::printInternal(raw_ostream &OS) const {
1091 DIType::printInternal(OS);
1092 DIArray A = getTypeArray();
1093 OS << " [" << A.getNumElements() << " elements]";
1094}
1095
1096void DISubprogram::printInternal(raw_ostream &OS) const {
Bill Wendling3b70d782012-06-26 23:22:18 +00001097 // TODO : Print context
Bill Wendling3b70d782012-06-26 23:22:18 +00001098 OS << " [line " << getLineNumber() << ']';
1099
1100 if (isLocalToUnit())
1101 OS << " [local]";
1102
1103 if (isDefinition())
1104 OS << " [def]";
1105
1106 if (getScopeLineNumber() != getLineNumber())
1107 OS << " [scope " << getScopeLineNumber() << "]";
Bill Wendling56543732012-07-06 23:43:12 +00001108
1109 StringRef Res = getName();
1110 if (!Res.empty())
1111 OS << " [" << Res << ']';
Bill Wendling3b70d782012-06-26 23:22:18 +00001112}
1113
1114void DIGlobalVariable::printInternal(raw_ostream &OS) const {
1115 StringRef Res = getName();
1116 if (!Res.empty())
1117 OS << " [" << Res << ']';
1118
1119 OS << " [line " << getLineNumber() << ']';
1120
1121 // TODO : Print context
1122
1123 if (isLocalToUnit())
1124 OS << " [local]";
1125
1126 if (isDefinition())
1127 OS << " [def]";
1128}
1129
1130void DIVariable::printInternal(raw_ostream &OS) const {
1131 StringRef Res = getName();
1132 if (!Res.empty())
1133 OS << " [" << Res << ']';
1134
1135 OS << " [line " << getLineNumber() << ']';
1136}
1137
Bill Wendlingaa02e362012-07-06 19:12:31 +00001138void DIObjCProperty::printInternal(raw_ostream &OS) const {
1139 StringRef Name = getObjCPropertyName();
1140 if (!Name.empty())
1141 OS << " [" << Name << ']';
1142
1143 OS << " [line " << getLineNumber()
1144 << ", properties " << getUnsignedField(6) << ']';
1145}
1146
Bill Wendling3b70d782012-06-26 23:22:18 +00001147static void printDebugLoc(DebugLoc DL, raw_ostream &CommentOS,
1148 const LLVMContext &Ctx) {
1149 if (!DL.isUnknown()) { // Print source line info.
1150 DIScope Scope(DL.getScope(Ctx));
1151 // Omit the directory, because it's likely to be long and uninteresting.
1152 if (Scope.Verify())
1153 CommentOS << Scope.getFilename();
1154 else
1155 CommentOS << "<unknown>";
1156 CommentOS << ':' << DL.getLine();
1157 if (DL.getCol() != 0)
1158 CommentOS << ':' << DL.getCol();
1159 DebugLoc InlinedAtDL = DebugLoc::getFromDILocation(DL.getInlinedAt(Ctx));
1160 if (!InlinedAtDL.isUnknown()) {
1161 CommentOS << " @[ ";
1162 printDebugLoc(InlinedAtDL, CommentOS, Ctx);
1163 CommentOS << " ]";
1164 }
1165 }
1166}
1167
1168void DIVariable::printExtendedName(raw_ostream &OS) const {
1169 const LLVMContext &Ctx = DbgNode->getContext();
1170 StringRef Res = getName();
1171 if (!Res.empty())
1172 OS << Res << "," << getLineNumber();
1173 if (MDNode *InlinedAt = getInlinedAt()) {
1174 DebugLoc InlinedAtDL = DebugLoc::getFromDILocation(InlinedAt);
1175 if (!InlinedAtDL.isUnknown()) {
1176 OS << " @[";
1177 printDebugLoc(InlinedAtDL, OS, Ctx);
1178 OS << "]";
1179 }
1180 }
1181}