blob: b42e946f2ffa22941b05b97441470bc2226cf9e0 [file] [log] [blame]
Chris Lattnera45664f2008-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
15#include "llvm/Analysis/DebugInfo.h"
16#include "llvm/Constants.h"
17#include "llvm/DerivedTypes.h"
18#include "llvm/Intrinsics.h"
Torok Edwin620f2802008-12-16 09:07:36 +000019#include "llvm/IntrinsicInst.h"
Chris Lattnera45664f2008-11-10 02:56:27 +000020#include "llvm/Instructions.h"
21#include "llvm/Module.h"
22#include "llvm/Analysis/ValueTracking.h"
Devang Patele4b27562009-08-28 23:24:31 +000023#include "llvm/ADT/SmallPtrSet.h"
Dan Gohman17aa92c2010-07-21 23:38:33 +000024#include "llvm/ADT/SmallString.h"
Dan Gohman489b29b2010-08-20 22:02:26 +000025#include "llvm/ADT/STLExtras.h"
David Greene0eb5b662009-12-23 19:45:49 +000026#include "llvm/Support/Debug.h"
Argyrios Kyrtzidis77eaa682009-05-03 08:50:41 +000027#include "llvm/Support/Dwarf.h"
Chris Lattnera81d29b2009-08-23 07:33:14 +000028#include "llvm/Support/raw_ostream.h"
Chris Lattnera45664f2008-11-10 02:56:27 +000029using namespace llvm;
Argyrios Kyrtzidis77eaa682009-05-03 08:50:41 +000030using namespace llvm::dwarf;
Chris Lattnera45664f2008-11-10 02:56:27 +000031
32//===----------------------------------------------------------------------===//
33// DIDescriptor
34//===----------------------------------------------------------------------===//
35
Devang Patel5b164b52010-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
42DIDescriptor::DIDescriptor(const DILexicalBlock F) : DbgNode(F.DbgNode) {
43}
44
45DIDescriptor::DIDescriptor(const DIVariable F) : DbgNode(F.DbgNode) {
46}
47
48DIDescriptor::DIDescriptor(const DIType F) : DbgNode(F.DbgNode) {
49}
50
Jim Grosbache62b6902010-07-21 21:36:25 +000051StringRef
Devang Patel5ccdd102009-09-29 18:40:58 +000052DIDescriptor::getStringField(unsigned Elt) const {
Devang Patele4b27562009-08-28 23:24:31 +000053 if (DbgNode == 0)
Devang Patel65dbc902009-11-25 17:36:49 +000054 return StringRef();
Chris Lattnera45664f2008-11-10 02:56:27 +000055
Chris Lattner5d0cacd2009-12-31 01:22:29 +000056 if (Elt < DbgNode->getNumOperands())
57 if (MDString *MDS = dyn_cast_or_null<MDString>(DbgNode->getOperand(Elt)))
Devang Patel65dbc902009-11-25 17:36:49 +000058 return MDS->getString();
Daniel Dunbarf612ff62009-09-19 20:40:05 +000059
Devang Patel65dbc902009-11-25 17:36:49 +000060 return StringRef();
Chris Lattnera45664f2008-11-10 02:56:27 +000061}
62
63uint64_t DIDescriptor::getUInt64Field(unsigned Elt) const {
Daniel Dunbarf612ff62009-09-19 20:40:05 +000064 if (DbgNode == 0)
Chris Lattnera45664f2008-11-10 02:56:27 +000065 return 0;
Bill Wendlingdc817b62009-05-14 18:26:15 +000066
Chris Lattner5d0cacd2009-12-31 01:22:29 +000067 if (Elt < DbgNode->getNumOperands())
68 if (ConstantInt *CI = dyn_cast<ConstantInt>(DbgNode->getOperand(Elt)))
Devang Patele4b27562009-08-28 23:24:31 +000069 return CI->getZExtValue();
Daniel Dunbarf612ff62009-09-19 20:40:05 +000070
Chris Lattnera45664f2008-11-10 02:56:27 +000071 return 0;
72}
73
Chris Lattnera45664f2008-11-10 02:56:27 +000074DIDescriptor DIDescriptor::getDescriptorField(unsigned Elt) const {
Daniel Dunbarf612ff62009-09-19 20:40:05 +000075 if (DbgNode == 0)
Chris Lattnera45664f2008-11-10 02:56:27 +000076 return DIDescriptor();
Bill Wendlingdc817b62009-05-14 18:26:15 +000077
Chris Lattner7a2f3e02010-03-31 05:53:47 +000078 if (Elt < DbgNode->getNumOperands())
Jim Grosbache62b6902010-07-21 21:36:25 +000079 return
80 DIDescriptor(dyn_cast_or_null<const MDNode>(DbgNode->getOperand(Elt)));
Devang Patele4b27562009-08-28 23:24:31 +000081 return DIDescriptor();
Chris Lattnera45664f2008-11-10 02:56:27 +000082}
83
84GlobalVariable *DIDescriptor::getGlobalVariableField(unsigned Elt) const {
Daniel Dunbarf612ff62009-09-19 20:40:05 +000085 if (DbgNode == 0)
Chris Lattnera45664f2008-11-10 02:56:27 +000086 return 0;
Bill Wendlingdc817b62009-05-14 18:26:15 +000087
Chris Lattner5d0cacd2009-12-31 01:22:29 +000088 if (Elt < DbgNode->getNumOperands())
89 return dyn_cast_or_null<GlobalVariable>(DbgNode->getOperand(Elt));
Devang Patele4b27562009-08-28 23:24:31 +000090 return 0;
Chris Lattnera45664f2008-11-10 02:56:27 +000091}
92
Devang Patel27398962010-08-09 21:39:24 +000093Constant *DIDescriptor::getConstantField(unsigned Elt) const {
94 if (DbgNode == 0)
95 return 0;
96
97 if (Elt < DbgNode->getNumOperands())
98 return dyn_cast_or_null<Constant>(DbgNode->getOperand(Elt));
99 return 0;
100}
101
Stuart Hastings215aa152010-06-11 20:08:44 +0000102Function *DIDescriptor::getFunctionField(unsigned Elt) const {
103 if (DbgNode == 0)
104 return 0;
105
106 if (Elt < DbgNode->getNumOperands())
107 return dyn_cast_or_null<Function>(DbgNode->getOperand(Elt));
108 return 0;
109}
110
Chris Lattnerf0908a32009-12-31 03:02:08 +0000111unsigned DIVariable::getNumAddrElements() const {
Devang Patel3cf763d2010-09-29 23:07:21 +0000112 if (getVersion() <= llvm::LLVMDebugVersion8)
113 return DbgNode->getNumOperands()-6;
114 return DbgNode->getNumOperands()-7;
Chris Lattnerf0908a32009-12-31 03:02:08 +0000115}
116
117
Chris Lattnera45664f2008-11-10 02:56:27 +0000118//===----------------------------------------------------------------------===//
Devang Patel6ceea332009-08-31 18:49:10 +0000119// Predicates
Chris Lattnera45664f2008-11-10 02:56:27 +0000120//===----------------------------------------------------------------------===//
121
Devang Patel6ceea332009-08-31 18:49:10 +0000122/// isBasicType - Return true if the specified tag is legal for
123/// DIBasicType.
124bool DIDescriptor::isBasicType() const {
Devang Patel3c91b052010-03-08 20:52:55 +0000125 return DbgNode && getTag() == dwarf::DW_TAG_base_type;
Torok Edwinb07fbd92008-12-13 08:25:29 +0000126}
Chris Lattnera45664f2008-11-10 02:56:27 +0000127
Devang Patel6ceea332009-08-31 18:49:10 +0000128/// isDerivedType - Return true if the specified tag is legal for DIDerivedType.
129bool DIDescriptor::isDerivedType() const {
Devang Patel3c91b052010-03-08 20:52:55 +0000130 if (!DbgNode) return false;
Chris Lattner099b7792009-12-29 09:22:47 +0000131 switch (getTag()) {
Chris Lattnera45664f2008-11-10 02:56:27 +0000132 case dwarf::DW_TAG_typedef:
133 case dwarf::DW_TAG_pointer_type:
134 case dwarf::DW_TAG_reference_type:
135 case dwarf::DW_TAG_const_type:
136 case dwarf::DW_TAG_volatile_type:
137 case dwarf::DW_TAG_restrict_type:
138 case dwarf::DW_TAG_member:
139 case dwarf::DW_TAG_inheritance:
Devang Patel49d96382010-08-23 23:16:25 +0000140 case dwarf::DW_TAG_friend:
Chris Lattnera45664f2008-11-10 02:56:27 +0000141 return true;
142 default:
Devang Patele4b27562009-08-28 23:24:31 +0000143 // CompositeTypes are currently modelled as DerivedTypes.
Devang Patel6ceea332009-08-31 18:49:10 +0000144 return isCompositeType();
Chris Lattnera45664f2008-11-10 02:56:27 +0000145 }
146}
147
Chris Lattnera45664f2008-11-10 02:56:27 +0000148/// isCompositeType - Return true if the specified tag is legal for
149/// DICompositeType.
Devang Patel6ceea332009-08-31 18:49:10 +0000150bool DIDescriptor::isCompositeType() const {
Devang Patel3c91b052010-03-08 20:52:55 +0000151 if (!DbgNode) return false;
Chris Lattner099b7792009-12-29 09:22:47 +0000152 switch (getTag()) {
Chris Lattnera45664f2008-11-10 02:56:27 +0000153 case dwarf::DW_TAG_array_type:
154 case dwarf::DW_TAG_structure_type:
155 case dwarf::DW_TAG_union_type:
156 case dwarf::DW_TAG_enumeration_type:
157 case dwarf::DW_TAG_vector_type:
158 case dwarf::DW_TAG_subroutine_type:
Devang Patel25cb0d72009-03-25 03:52:06 +0000159 case dwarf::DW_TAG_class_type:
Chris Lattnera45664f2008-11-10 02:56:27 +0000160 return true;
161 default:
162 return false;
163 }
164}
165
Chris Lattnera45664f2008-11-10 02:56:27 +0000166/// isVariable - Return true if the specified tag is legal for DIVariable.
Devang Patel6ceea332009-08-31 18:49:10 +0000167bool DIDescriptor::isVariable() const {
Devang Patel3c91b052010-03-08 20:52:55 +0000168 if (!DbgNode) return false;
Chris Lattner099b7792009-12-29 09:22:47 +0000169 switch (getTag()) {
Chris Lattnera45664f2008-11-10 02:56:27 +0000170 case dwarf::DW_TAG_auto_variable:
171 case dwarf::DW_TAG_arg_variable:
172 case dwarf::DW_TAG_return_variable:
173 return true;
174 default:
175 return false;
176 }
177}
178
Devang Patelecbeb1a2009-09-30 22:34:41 +0000179/// isType - Return true if the specified tag is legal for DIType.
180bool DIDescriptor::isType() const {
181 return isBasicType() || isCompositeType() || isDerivedType();
182}
183
Devang Patel6ceea332009-08-31 18:49:10 +0000184/// isSubprogram - Return true if the specified tag is legal for
185/// DISubprogram.
186bool DIDescriptor::isSubprogram() const {
Devang Patel3c91b052010-03-08 20:52:55 +0000187 return DbgNode && getTag() == dwarf::DW_TAG_subprogram;
Devang Patel6ceea332009-08-31 18:49:10 +0000188}
189
190/// isGlobalVariable - Return true if the specified tag is legal for
191/// DIGlobalVariable.
192bool DIDescriptor::isGlobalVariable() const {
Devang Patel29368072010-08-10 07:11:13 +0000193 return DbgNode && (getTag() == dwarf::DW_TAG_variable ||
194 getTag() == dwarf::DW_TAG_constant);
Devang Patel6ceea332009-08-31 18:49:10 +0000195}
196
Devang Patelecbeb1a2009-09-30 22:34:41 +0000197/// isGlobal - Return true if the specified tag is legal for DIGlobal.
198bool DIDescriptor::isGlobal() const {
199 return isGlobalVariable();
200}
201
Devang Patel716a67f2011-02-03 00:13:47 +0000202/// isUnspecifiedParmeter - Return true if the specified tag is
Devang Pateld6747df2010-10-06 20:50:40 +0000203/// DW_TAG_unspecified_parameters.
204bool DIDescriptor::isUnspecifiedParameter() const {
205 return DbgNode && getTag() == dwarf::DW_TAG_unspecified_parameters;
206}
207
Daniel Dunbarf612ff62009-09-19 20:40:05 +0000208/// isScope - Return true if the specified tag is one of the scope
Devang Patel43d98b32009-08-31 20:44:45 +0000209/// related tag.
210bool DIDescriptor::isScope() const {
Devang Patel3c91b052010-03-08 20:52:55 +0000211 if (!DbgNode) return false;
Chris Lattner099b7792009-12-29 09:22:47 +0000212 switch (getTag()) {
213 case dwarf::DW_TAG_compile_unit:
214 case dwarf::DW_TAG_lexical_block:
215 case dwarf::DW_TAG_subprogram:
216 case dwarf::DW_TAG_namespace:
217 return true;
218 default:
219 break;
Devang Patel43d98b32009-08-31 20:44:45 +0000220 }
221 return false;
222}
Devang Patel6ceea332009-08-31 18:49:10 +0000223
Devang Patel7e2cb112011-02-02 21:38:25 +0000224/// isTemplateTypeParameter - Return true if the specified tag is
225/// DW_TAG_template_type_parameter.
226bool DIDescriptor::isTemplateTypeParameter() const {
227 return DbgNode && getTag() == dwarf::DW_TAG_template_type_parameter;
228}
229
Devang Patele7d93872011-02-02 22:35:53 +0000230/// isTemplateValueParameter - Return true if the specified tag is
231/// DW_TAG_template_value_parameter.
232bool DIDescriptor::isTemplateValueParameter() const {
233 return DbgNode && getTag() == dwarf::DW_TAG_template_value_parameter;
234}
235
Devang Patelc9f322d2009-08-31 21:34:44 +0000236/// isCompileUnit - Return true if the specified tag is DW_TAG_compile_unit.
237bool DIDescriptor::isCompileUnit() const {
Devang Patel3c91b052010-03-08 20:52:55 +0000238 return DbgNode && getTag() == dwarf::DW_TAG_compile_unit;
Devang Patelc9f322d2009-08-31 21:34:44 +0000239}
240
Devang Patel7aa81892010-03-08 22:27:22 +0000241/// isFile - Return true if the specified tag is DW_TAG_file_type.
242bool DIDescriptor::isFile() const {
243 return DbgNode && getTag() == dwarf::DW_TAG_file_type;
244}
245
Devang Patel6404e4e2009-12-15 19:16:48 +0000246/// isNameSpace - Return true if the specified tag is DW_TAG_namespace.
247bool DIDescriptor::isNameSpace() const {
Devang Patel3c91b052010-03-08 20:52:55 +0000248 return DbgNode && getTag() == dwarf::DW_TAG_namespace;
Devang Patel6404e4e2009-12-15 19:16:48 +0000249}
250
Devang Patel5e005d82009-08-31 22:00:15 +0000251/// isLexicalBlock - Return true if the specified tag is DW_TAG_lexical_block.
252bool DIDescriptor::isLexicalBlock() const {
Devang Patel3c91b052010-03-08 20:52:55 +0000253 return DbgNode && getTag() == dwarf::DW_TAG_lexical_block;
Devang Patel5e005d82009-08-31 22:00:15 +0000254}
255
Devang Patelecbeb1a2009-09-30 22:34:41 +0000256/// isSubrange - Return true if the specified tag is DW_TAG_subrange_type.
257bool DIDescriptor::isSubrange() const {
Devang Patel3c91b052010-03-08 20:52:55 +0000258 return DbgNode && getTag() == dwarf::DW_TAG_subrange_type;
Devang Patelecbeb1a2009-09-30 22:34:41 +0000259}
260
261/// isEnumerator - Return true if the specified tag is DW_TAG_enumerator.
262bool DIDescriptor::isEnumerator() const {
Devang Patel3c91b052010-03-08 20:52:55 +0000263 return DbgNode && getTag() == dwarf::DW_TAG_enumerator;
Devang Patelecbeb1a2009-09-30 22:34:41 +0000264}
265
Devang Patel6ceea332009-08-31 18:49:10 +0000266//===----------------------------------------------------------------------===//
267// Simple Descriptor Constructors and other Methods
268//===----------------------------------------------------------------------===//
269
Devang Patele9f8f5e2010-05-07 20:54:48 +0000270DIType::DIType(const MDNode *N) : DIScope(N) {
Devang Patel6ceea332009-08-31 18:49:10 +0000271 if (!N) return;
272 if (!isBasicType() && !isDerivedType() && !isCompositeType()) {
273 DbgNode = 0;
274 }
275}
276
Devang Patel68afdc32009-01-05 18:33:01 +0000277unsigned DIArray::getNumElements() const {
Devang Patel3c91b052010-03-08 20:52:55 +0000278 if (!DbgNode)
279 return 0;
Chris Lattner5d0cacd2009-12-31 01:22:29 +0000280 return DbgNode->getNumOperands();
Devang Patel68afdc32009-01-05 18:33:01 +0000281}
Chris Lattnera45664f2008-11-10 02:56:27 +0000282
Devang Patelc4999d72009-07-22 18:23:44 +0000283/// replaceAllUsesWith - Replace all uses of debug info referenced by
Dan Gohman872814a2010-07-21 18:54:18 +0000284/// this descriptor.
Dan Gohman489b29b2010-08-20 22:02:26 +0000285void DIType::replaceAllUsesWith(DIDescriptor &D) {
Devang Patel3c91b052010-03-08 20:52:55 +0000286 if (!DbgNode)
Devang Patelc4999d72009-07-22 18:23:44 +0000287 return;
288
Daniel Dunbar48a097b2009-09-22 02:03:18 +0000289 // Since we use a TrackingVH for the node, its easy for clients to manufacture
290 // legitimate situations where they want to replaceAllUsesWith() on something
291 // which, due to uniquing, has merged with the source. We shield clients from
292 // this detail by allowing a value to be replaced with replaceAllUsesWith()
293 // itself.
Devang Pateled66bf52010-05-07 18:19:32 +0000294 if (DbgNode != D) {
Devang Patele9f8f5e2010-05-07 20:54:48 +0000295 MDNode *Node = const_cast<MDNode*>(DbgNode);
296 const MDNode *DN = D;
297 const Value *V = cast_or_null<Value>(DN);
298 Node->replaceAllUsesWith(const_cast<Value*>(V));
Dan Gohman489b29b2010-08-20 22:02:26 +0000299 MDNode::deleteTemporary(Node);
Daniel Dunbar48a097b2009-09-22 02:03:18 +0000300 }
Devang Patelc4999d72009-07-22 18:23:44 +0000301}
302
Devang Patel0a2551d2010-12-08 20:18:20 +0000303/// replaceAllUsesWith - Replace all uses of debug info referenced by
304/// this descriptor.
305void DIType::replaceAllUsesWith(MDNode *D) {
306 if (!DbgNode)
307 return;
308
309 // Since we use a TrackingVH for the node, its easy for clients to manufacture
310 // legitimate situations where they want to replaceAllUsesWith() on something
311 // which, due to uniquing, has merged with the source. We shield clients from
312 // this detail by allowing a value to be replaced with replaceAllUsesWith()
313 // itself.
314 if (DbgNode != D) {
315 MDNode *Node = const_cast<MDNode*>(DbgNode);
316 const MDNode *DN = D;
317 const Value *V = cast_or_null<Value>(DN);
318 Node->replaceAllUsesWith(const_cast<Value*>(V));
319 MDNode::deleteTemporary(Node);
320 }
321}
322
Devang Patelb79b5352009-01-19 23:21:49 +0000323/// Verify - Verify that a compile unit is well formed.
324bool DICompileUnit::Verify() const {
Devang Patel3c91b052010-03-08 20:52:55 +0000325 if (!DbgNode)
Devang Patelb79b5352009-01-19 23:21:49 +0000326 return false;
Devang Patel65dbc902009-11-25 17:36:49 +0000327 StringRef N = getFilename();
328 if (N.empty())
Bill Wendling0582ae92009-03-13 04:39:26 +0000329 return false;
Devang Patelb79b5352009-01-19 23:21:49 +0000330 // It is possible that directory and produce string is empty.
Bill Wendling0582ae92009-03-13 04:39:26 +0000331 return true;
Devang Patelb79b5352009-01-19 23:21:49 +0000332}
333
334/// Verify - Verify that a type descriptor is well formed.
335bool DIType::Verify() const {
Devang Patel3c91b052010-03-08 20:52:55 +0000336 if (!DbgNode)
Devang Patelb79b5352009-01-19 23:21:49 +0000337 return false;
Devang Patel3c91b052010-03-08 20:52:55 +0000338 if (!getContext().Verify())
Devang Patelb79b5352009-01-19 23:21:49 +0000339 return false;
Devang Patelb71bbf92010-11-02 20:41:13 +0000340 unsigned Tag = getTag();
341 if (!isBasicType() && Tag != dwarf::DW_TAG_const_type &&
342 Tag != dwarf::DW_TAG_volatile_type && Tag != dwarf::DW_TAG_pointer_type &&
Devang Patelfe58f952010-12-07 23:25:47 +0000343 Tag != dwarf::DW_TAG_reference_type && Tag != dwarf::DW_TAG_restrict_type
Devang Patel43c249c2010-12-08 01:50:15 +0000344 && Tag != dwarf::DW_TAG_vector_type && Tag != dwarf::DW_TAG_array_type
345 && Tag != dwarf::DW_TAG_enumeration_type
Devang Patelfe58f952010-12-07 23:25:47 +0000346 && getFilename().empty())
Devang Patelb79b5352009-01-19 23:21:49 +0000347 return false;
348 return true;
349}
350
Devang Patel0c4720c2010-08-23 18:25:56 +0000351/// Verify - Verify that a basic type descriptor is well formed.
352bool DIBasicType::Verify() const {
353 return isBasicType();
354}
355
356/// Verify - Verify that a derived type descriptor is well formed.
357bool DIDerivedType::Verify() const {
358 return isDerivedType();
359}
360
Devang Patelb79b5352009-01-19 23:21:49 +0000361/// Verify - Verify that a composite type descriptor is well formed.
362bool DICompositeType::Verify() const {
Devang Patel3c91b052010-03-08 20:52:55 +0000363 if (!DbgNode)
Devang Patelb79b5352009-01-19 23:21:49 +0000364 return false;
Devang Patel3c91b052010-03-08 20:52:55 +0000365 if (!getContext().Verify())
Devang Patelb79b5352009-01-19 23:21:49 +0000366 return false;
367
368 DICompileUnit CU = getCompileUnit();
Devang Patel3c91b052010-03-08 20:52:55 +0000369 if (!CU.Verify())
Devang Patelb79b5352009-01-19 23:21:49 +0000370 return false;
371 return true;
372}
373
374/// Verify - Verify that a subprogram descriptor is well formed.
375bool DISubprogram::Verify() const {
Devang Patel3c91b052010-03-08 20:52:55 +0000376 if (!DbgNode)
Devang Patelb79b5352009-01-19 23:21:49 +0000377 return false;
Daniel Dunbarf612ff62009-09-19 20:40:05 +0000378
Devang Patel3c91b052010-03-08 20:52:55 +0000379 if (!getContext().Verify())
Devang Patelb79b5352009-01-19 23:21:49 +0000380 return false;
381
382 DICompileUnit CU = getCompileUnit();
Daniel Dunbarf612ff62009-09-19 20:40:05 +0000383 if (!CU.Verify())
Devang Patelb79b5352009-01-19 23:21:49 +0000384 return false;
385
386 DICompositeType Ty = getType();
Devang Patel3c91b052010-03-08 20:52:55 +0000387 if (!Ty.Verify())
Devang Patelb79b5352009-01-19 23:21:49 +0000388 return false;
389 return true;
390}
391
392/// Verify - Verify that a global variable descriptor is well formed.
393bool DIGlobalVariable::Verify() const {
Devang Patel3c91b052010-03-08 20:52:55 +0000394 if (!DbgNode)
Devang Patelb79b5352009-01-19 23:21:49 +0000395 return false;
Daniel Dunbarf612ff62009-09-19 20:40:05 +0000396
Devang Patel65dbc902009-11-25 17:36:49 +0000397 if (getDisplayName().empty())
Devang Patel84c73e92009-11-06 17:58:12 +0000398 return false;
399
Devang Patel3c91b052010-03-08 20:52:55 +0000400 if (!getContext().Verify())
Devang Patelb79b5352009-01-19 23:21:49 +0000401 return false;
402
403 DICompileUnit CU = getCompileUnit();
Devang Patel3c91b052010-03-08 20:52:55 +0000404 if (!CU.Verify())
Devang Patelb79b5352009-01-19 23:21:49 +0000405 return false;
406
407 DIType Ty = getType();
408 if (!Ty.Verify())
409 return false;
410
Devang Patel27398962010-08-09 21:39:24 +0000411 if (!getGlobal() && !getConstant())
Devang Patelb79b5352009-01-19 23:21:49 +0000412 return false;
413
414 return true;
415}
416
417/// Verify - Verify that a variable descriptor is well formed.
418bool DIVariable::Verify() const {
Devang Patel3c91b052010-03-08 20:52:55 +0000419 if (!DbgNode)
Devang Patelb79b5352009-01-19 23:21:49 +0000420 return false;
Daniel Dunbarf612ff62009-09-19 20:40:05 +0000421
Devang Patel3c91b052010-03-08 20:52:55 +0000422 if (!getContext().Verify())
Devang Patelb79b5352009-01-19 23:21:49 +0000423 return false;
424
Devang Patel62077af2010-05-07 21:42:24 +0000425 if (!getCompileUnit().Verify())
426 return false;
427
Devang Patelb79b5352009-01-19 23:21:49 +0000428 DIType Ty = getType();
429 if (!Ty.Verify())
430 return false;
431
Devang Patelb79b5352009-01-19 23:21:49 +0000432 return true;
433}
434
Devang Patel3c91b052010-03-08 20:52:55 +0000435/// Verify - Verify that a location descriptor is well formed.
436bool DILocation::Verify() const {
437 if (!DbgNode)
438 return false;
Jim Grosbache62b6902010-07-21 21:36:25 +0000439
Devang Patel3c91b052010-03-08 20:52:55 +0000440 return DbgNode->getNumOperands() == 4;
441}
442
Devang Patel47e22652010-05-07 23:04:32 +0000443/// Verify - Verify that a namespace descriptor is well formed.
444bool DINameSpace::Verify() const {
445 if (!DbgNode)
446 return false;
447 if (getName().empty())
448 return false;
449 if (!getCompileUnit().Verify())
450 return false;
451 return true;
452}
453
Devang Patel36375ee2009-02-17 21:23:59 +0000454/// getOriginalTypeSize - If this type is derived from a base type then
455/// return base type size.
456uint64_t DIDerivedType::getOriginalTypeSize() const {
Devang Patel61ecbd12009-11-04 23:48:00 +0000457 unsigned Tag = getTag();
458 if (Tag == dwarf::DW_TAG_member || Tag == dwarf::DW_TAG_typedef ||
459 Tag == dwarf::DW_TAG_const_type || Tag == dwarf::DW_TAG_volatile_type ||
460 Tag == dwarf::DW_TAG_restrict_type) {
461 DIType BaseType = getTypeDerivedFrom();
Jim Grosbache62b6902010-07-21 21:36:25 +0000462 // If this type is not derived from any type then take conservative
Devang Patel5ebfa2d2009-11-06 18:24:05 +0000463 // approach.
Devang Patel3c91b052010-03-08 20:52:55 +0000464 if (!BaseType.isValid())
Devang Patel5ebfa2d2009-11-06 18:24:05 +0000465 return getSizeInBits();
Devang Patel61ecbd12009-11-04 23:48:00 +0000466 if (BaseType.isDerivedType())
Devang Patel2db49d72010-05-07 18:11:54 +0000467 return DIDerivedType(BaseType).getOriginalTypeSize();
Devang Patel61ecbd12009-11-04 23:48:00 +0000468 else
469 return BaseType.getSizeInBits();
470 }
Jim Grosbache62b6902010-07-21 21:36:25 +0000471
Devang Patel61ecbd12009-11-04 23:48:00 +0000472 return getSizeInBits();
Devang Patel36375ee2009-02-17 21:23:59 +0000473}
Devang Patelb79b5352009-01-19 23:21:49 +0000474
Jim Grosbache62b6902010-07-21 21:36:25 +0000475/// isInlinedFnArgument - Return true if this variable provides debugging
Devang Patel719f6a92010-04-29 20:40:36 +0000476/// information for an inlined function arguments.
477bool DIVariable::isInlinedFnArgument(const Function *CurFn) {
478 assert(CurFn && "Invalid function");
479 if (!getContext().isSubprogram())
480 return false;
Jim Grosbache62b6902010-07-21 21:36:25 +0000481 // This variable is not inlined function argument if its scope
Devang Patel719f6a92010-04-29 20:40:36 +0000482 // does not describe current function.
Devang Patel2db49d72010-05-07 18:11:54 +0000483 return !(DISubprogram(getContext()).describes(CurFn));
Devang Patel719f6a92010-04-29 20:40:36 +0000484}
485
Devang Patelaf5b6bb2009-04-15 00:06:07 +0000486/// describes - Return true if this subprogram provides debugging
487/// information for the function F.
488bool DISubprogram::describes(const Function *F) {
Chris Lattner099b7792009-12-29 09:22:47 +0000489 assert(F && "Invalid function");
Devang Patelffd33cd2010-06-16 06:42:02 +0000490 if (F == getFunction())
491 return true;
Devang Patel65dbc902009-11-25 17:36:49 +0000492 StringRef Name = getLinkageName();
493 if (Name.empty())
Devang Patel5ccdd102009-09-29 18:40:58 +0000494 Name = getName();
Devang Patel65dbc902009-11-25 17:36:49 +0000495 if (F->getName() == Name)
Devang Patelaf5b6bb2009-04-15 00:06:07 +0000496 return true;
497 return false;
498}
499
Jim Grosbache62b6902010-07-21 21:36:25 +0000500unsigned DISubprogram::isOptimized() const {
Devang Patelccff8122010-04-30 19:38:23 +0000501 assert (DbgNode && "Invalid subprogram descriptor!");
502 if (DbgNode->getNumOperands() == 16)
503 return getUnsignedField(15);
504 return 0;
505}
506
Devang Patel65dbc902009-11-25 17:36:49 +0000507StringRef DIScope::getFilename() const {
Devang Patel77bf2952010-03-08 22:02:50 +0000508 if (!DbgNode)
509 return StringRef();
Jim Grosbache62b6902010-07-21 21:36:25 +0000510 if (isLexicalBlock())
Devang Patelecbeb1a2009-09-30 22:34:41 +0000511 return DILexicalBlock(DbgNode).getFilename();
Chris Lattner099b7792009-12-29 09:22:47 +0000512 if (isSubprogram())
Devang Patelecbeb1a2009-09-30 22:34:41 +0000513 return DISubprogram(DbgNode).getFilename();
Chris Lattner099b7792009-12-29 09:22:47 +0000514 if (isCompileUnit())
Devang Patelecbeb1a2009-09-30 22:34:41 +0000515 return DICompileUnit(DbgNode).getFilename();
Chris Lattner099b7792009-12-29 09:22:47 +0000516 if (isNameSpace())
Devang Patel6404e4e2009-12-15 19:16:48 +0000517 return DINameSpace(DbgNode).getFilename();
Devang Patel77bf2952010-03-08 22:02:50 +0000518 if (isType())
519 return DIType(DbgNode).getFilename();
Devang Patel7aa81892010-03-08 22:27:22 +0000520 if (isFile())
521 return DIFile(DbgNode).getFilename();
Chris Lattner099b7792009-12-29 09:22:47 +0000522 assert(0 && "Invalid DIScope!");
Devang Patel65dbc902009-11-25 17:36:49 +0000523 return StringRef();
Devang Patelecbeb1a2009-09-30 22:34:41 +0000524}
525
Devang Patel65dbc902009-11-25 17:36:49 +0000526StringRef DIScope::getDirectory() const {
Devang Patel77bf2952010-03-08 22:02:50 +0000527 if (!DbgNode)
528 return StringRef();
Jim Grosbache62b6902010-07-21 21:36:25 +0000529 if (isLexicalBlock())
Devang Patelecbeb1a2009-09-30 22:34:41 +0000530 return DILexicalBlock(DbgNode).getDirectory();
Chris Lattner099b7792009-12-29 09:22:47 +0000531 if (isSubprogram())
Devang Patelecbeb1a2009-09-30 22:34:41 +0000532 return DISubprogram(DbgNode).getDirectory();
Chris Lattner099b7792009-12-29 09:22:47 +0000533 if (isCompileUnit())
Devang Patelecbeb1a2009-09-30 22:34:41 +0000534 return DICompileUnit(DbgNode).getDirectory();
Chris Lattner099b7792009-12-29 09:22:47 +0000535 if (isNameSpace())
Devang Patel6404e4e2009-12-15 19:16:48 +0000536 return DINameSpace(DbgNode).getDirectory();
Devang Patel77bf2952010-03-08 22:02:50 +0000537 if (isType())
538 return DIType(DbgNode).getDirectory();
Devang Patel7aa81892010-03-08 22:27:22 +0000539 if (isFile())
540 return DIFile(DbgNode).getDirectory();
Chris Lattner099b7792009-12-29 09:22:47 +0000541 assert(0 && "Invalid DIScope!");
Devang Patel65dbc902009-11-25 17:36:49 +0000542 return StringRef();
Devang Patelecbeb1a2009-09-30 22:34:41 +0000543}
544
Chris Lattnera45664f2008-11-10 02:56:27 +0000545//===----------------------------------------------------------------------===//
Devang Patel7136a652009-07-01 22:10:23 +0000546// DIDescriptor: dump routines for all descriptors.
547//===----------------------------------------------------------------------===//
548
549
Dan Gohman50404362010-05-07 15:30:29 +0000550/// print - Print descriptor.
551void DIDescriptor::print(raw_ostream &OS) const {
552 OS << "[" << dwarf::TagString(getTag()) << "] ";
553 OS.write_hex((intptr_t) &*DbgNode) << ']';
Devang Patel7136a652009-07-01 22:10:23 +0000554}
555
Dan Gohman50404362010-05-07 15:30:29 +0000556/// print - Print compile unit.
557void DICompileUnit::print(raw_ostream &OS) const {
Devang Patel7136a652009-07-01 22:10:23 +0000558 if (getLanguage())
Dan Gohman50404362010-05-07 15:30:29 +0000559 OS << " [" << dwarf::LanguageString(getLanguage()) << "] ";
Devang Patel7136a652009-07-01 22:10:23 +0000560
Dan Gohmanfaa19c32010-05-10 20:07:44 +0000561 OS << " [" << getDirectory() << "/" << getFilename() << "]";
Devang Patel7136a652009-07-01 22:10:23 +0000562}
563
Dan Gohman50404362010-05-07 15:30:29 +0000564/// print - Print type.
565void DIType::print(raw_ostream &OS) const {
Devang Patel3c91b052010-03-08 20:52:55 +0000566 if (!DbgNode) return;
Devang Patel7136a652009-07-01 22:10:23 +0000567
Devang Patel65dbc902009-11-25 17:36:49 +0000568 StringRef Res = getName();
569 if (!Res.empty())
Dan Gohman50404362010-05-07 15:30:29 +0000570 OS << " [" << Res << "] ";
Devang Patel7136a652009-07-01 22:10:23 +0000571
572 unsigned Tag = getTag();
Dan Gohman50404362010-05-07 15:30:29 +0000573 OS << " [" << dwarf::TagString(Tag) << "] ";
Devang Patel7136a652009-07-01 22:10:23 +0000574
575 // TODO : Print context
Dan Gohmanc014d092010-05-07 16:17:22 +0000576 getCompileUnit().print(OS);
Dan Gohman50404362010-05-07 15:30:29 +0000577 OS << " ["
Dan Gohman9a7063e2010-05-07 16:39:27 +0000578 << "line " << getLineNumber() << ", "
579 << getSizeInBits() << " bits, "
580 << getAlignInBits() << " bit alignment, "
581 << getOffsetInBits() << " bit offset"
Chris Lattnera81d29b2009-08-23 07:33:14 +0000582 << "] ";
Devang Patel7136a652009-07-01 22:10:23 +0000583
Daniel Dunbarf612ff62009-09-19 20:40:05 +0000584 if (isPrivate())
Dan Gohman50404362010-05-07 15:30:29 +0000585 OS << " [private] ";
Devang Patel7136a652009-07-01 22:10:23 +0000586 else if (isProtected())
Dan Gohman50404362010-05-07 15:30:29 +0000587 OS << " [protected] ";
Devang Patel7136a652009-07-01 22:10:23 +0000588
589 if (isForwardDecl())
Dan Gohman50404362010-05-07 15:30:29 +0000590 OS << " [fwd] ";
Devang Patel7136a652009-07-01 22:10:23 +0000591
Devang Patel6ceea332009-08-31 18:49:10 +0000592 if (isBasicType())
Dan Gohmanc014d092010-05-07 16:17:22 +0000593 DIBasicType(DbgNode).print(OS);
Devang Patel6ceea332009-08-31 18:49:10 +0000594 else if (isDerivedType())
Dan Gohmanc014d092010-05-07 16:17:22 +0000595 DIDerivedType(DbgNode).print(OS);
Devang Patel6ceea332009-08-31 18:49:10 +0000596 else if (isCompositeType())
Dan Gohmanc014d092010-05-07 16:17:22 +0000597 DICompositeType(DbgNode).print(OS);
Devang Patel7136a652009-07-01 22:10:23 +0000598 else {
Dan Gohman50404362010-05-07 15:30:29 +0000599 OS << "Invalid DIType\n";
Devang Patel7136a652009-07-01 22:10:23 +0000600 return;
601 }
602
Dan Gohman50404362010-05-07 15:30:29 +0000603 OS << "\n";
Devang Patel7136a652009-07-01 22:10:23 +0000604}
605
Dan Gohman50404362010-05-07 15:30:29 +0000606/// print - Print basic type.
607void DIBasicType::print(raw_ostream &OS) const {
608 OS << " [" << dwarf::AttributeEncodingString(getEncoding()) << "] ";
Devang Patel7136a652009-07-01 22:10:23 +0000609}
610
Dan Gohman50404362010-05-07 15:30:29 +0000611/// print - Print derived type.
612void DIDerivedType::print(raw_ostream &OS) const {
Dan Gohmanc014d092010-05-07 16:17:22 +0000613 OS << "\n\t Derived From: "; getTypeDerivedFrom().print(OS);
Devang Patel7136a652009-07-01 22:10:23 +0000614}
615
Dan Gohman50404362010-05-07 15:30:29 +0000616/// print - Print composite type.
617void DICompositeType::print(raw_ostream &OS) const {
Devang Patel7136a652009-07-01 22:10:23 +0000618 DIArray A = getTypeArray();
Dan Gohman50404362010-05-07 15:30:29 +0000619 OS << " [" << A.getNumElements() << " elements]";
Devang Patel7136a652009-07-01 22:10:23 +0000620}
621
Dan Gohman50404362010-05-07 15:30:29 +0000622/// print - Print subprogram.
623void DISubprogram::print(raw_ostream &OS) const {
Devang Patel65dbc902009-11-25 17:36:49 +0000624 StringRef Res = getName();
625 if (!Res.empty())
Dan Gohman50404362010-05-07 15:30:29 +0000626 OS << " [" << Res << "] ";
Devang Patel82dfc0c2009-08-31 22:47:13 +0000627
628 unsigned Tag = getTag();
Dan Gohman50404362010-05-07 15:30:29 +0000629 OS << " [" << dwarf::TagString(Tag) << "] ";
Devang Patel82dfc0c2009-08-31 22:47:13 +0000630
631 // TODO : Print context
Dan Gohmanc014d092010-05-07 16:17:22 +0000632 getCompileUnit().print(OS);
Dan Gohman50404362010-05-07 15:30:29 +0000633 OS << " [" << getLineNumber() << "] ";
Devang Patel82dfc0c2009-08-31 22:47:13 +0000634
635 if (isLocalToUnit())
Dan Gohman50404362010-05-07 15:30:29 +0000636 OS << " [local] ";
Devang Patel82dfc0c2009-08-31 22:47:13 +0000637
638 if (isDefinition())
Dan Gohman50404362010-05-07 15:30:29 +0000639 OS << " [def] ";
Devang Patel82dfc0c2009-08-31 22:47:13 +0000640
Dan Gohman50404362010-05-07 15:30:29 +0000641 OS << "\n";
642}
643
644/// print - Print global variable.
645void DIGlobalVariable::print(raw_ostream &OS) const {
646 OS << " [";
Devang Patela49d8772010-05-07 23:19:07 +0000647 StringRef Res = getName();
648 if (!Res.empty())
649 OS << " [" << Res << "] ";
650
651 unsigned Tag = getTag();
652 OS << " [" << dwarf::TagString(Tag) << "] ";
653
654 // TODO : Print context
655 getCompileUnit().print(OS);
656 OS << " [" << getLineNumber() << "] ";
657
658 if (isLocalToUnit())
659 OS << " [local] ";
660
661 if (isDefinition())
662 OS << " [def] ";
663
664 if (isGlobalVariable())
665 DIGlobalVariable(DbgNode).print(OS);
666 OS << "]\n";
Dan Gohman50404362010-05-07 15:30:29 +0000667}
668
669/// print - Print variable.
670void DIVariable::print(raw_ostream &OS) const {
671 StringRef Res = getName();
672 if (!Res.empty())
673 OS << " [" << Res << "] ";
674
Dan Gohmanc014d092010-05-07 16:17:22 +0000675 getCompileUnit().print(OS);
Dan Gohman50404362010-05-07 15:30:29 +0000676 OS << " [" << getLineNumber() << "] ";
Dan Gohmanc014d092010-05-07 16:17:22 +0000677 getType().print(OS);
Dan Gohman50404362010-05-07 15:30:29 +0000678 OS << "\n";
679
680 // FIXME: Dump complex addresses
681}
682
683/// dump - Print descriptor to dbgs() with a newline.
684void DIDescriptor::dump() const {
685 print(dbgs()); dbgs() << '\n';
686}
687
688/// dump - Print compile unit to dbgs() with a newline.
689void DICompileUnit::dump() const {
690 print(dbgs()); dbgs() << '\n';
691}
692
693/// dump - Print type to dbgs() with a newline.
694void DIType::dump() const {
695 print(dbgs()); dbgs() << '\n';
696}
697
698/// dump - Print basic type to dbgs() with a newline.
699void DIBasicType::dump() const {
700 print(dbgs()); dbgs() << '\n';
701}
702
703/// dump - Print derived type to dbgs() with a newline.
704void DIDerivedType::dump() const {
705 print(dbgs()); dbgs() << '\n';
706}
707
708/// dump - Print composite type to dbgs() with a newline.
709void DICompositeType::dump() const {
710 print(dbgs()); dbgs() << '\n';
711}
712
Dan Gohman50404362010-05-07 15:30:29 +0000713/// dump - Print subprogram to dbgs() with a newline.
714void DISubprogram::dump() const {
715 print(dbgs()); dbgs() << '\n';
Devang Patel7136a652009-07-01 22:10:23 +0000716}
717
718/// dump - Print global variable.
719void DIGlobalVariable::dump() const {
Dan Gohman50404362010-05-07 15:30:29 +0000720 print(dbgs()); dbgs() << '\n';
Devang Patel7136a652009-07-01 22:10:23 +0000721}
722
723/// dump - Print variable.
724void DIVariable::dump() const {
Dan Gohman50404362010-05-07 15:30:29 +0000725 print(dbgs()); dbgs() << '\n';
Devang Patel7136a652009-07-01 22:10:23 +0000726}
727
Devang Patel62367042010-11-10 22:19:21 +0000728/// fixupObjcLikeName - Replace contains special characters used
729/// in a typical Objective-C names with '.' in a given string.
Benjamin Kramer1a81d482011-06-18 14:42:42 +0000730static void fixupObjcLikeName(StringRef Str, SmallVectorImpl<char> &Out) {
731 bool isObjCLike = false;
Devang Patel62367042010-11-10 22:19:21 +0000732 for (size_t i = 0, e = Str.size(); i < e; ++i) {
733 char C = Str[i];
Benjamin Kramer1a81d482011-06-18 14:42:42 +0000734 if (C == '[')
735 isObjCLike = true;
736
737 if (isObjCLike && (C == '[' || C == ']' || C == ' ' || C == ':' ||
738 C == '+' || C == '(' || C == ')'))
739 Out.push_back('.');
740 else
741 Out.push_back(C);
Devang Patel62367042010-11-10 22:19:21 +0000742 }
743}
744
Devang Patel62367042010-11-10 22:19:21 +0000745/// getFnSpecificMDNode - Return a NameMDNode, if available, that is
746/// suitable to hold function specific information.
747NamedMDNode *llvm::getFnSpecificMDNode(const Module &M, StringRef FuncName) {
Benjamin Kramer1a81d482011-06-18 14:42:42 +0000748 SmallString<32> Name = StringRef("llvm.dbg.lv.");
749 fixupObjcLikeName(FuncName, Name);
750
751 return M.getNamedMetadata(Name.str());
Devang Patel62367042010-11-10 22:19:21 +0000752}
753
Duncan Sands291bb702011-03-02 20:30:37 +0000754/// getOrInsertFnSpecificMDNode - Return a NameMDNode that is suitable
755/// to hold function specific information.
756NamedMDNode *llvm::getOrInsertFnSpecificMDNode(Module &M, StringRef FuncName) {
Benjamin Kramer1a81d482011-06-18 14:42:42 +0000757 SmallString<32> Name = StringRef("llvm.dbg.lv.");
758 fixupObjcLikeName(FuncName, Name);
759
760 return M.getOrInsertNamedMetadata(Name.str());
Devang Patel1a7ca032010-09-28 18:08:20 +0000761}
762
763
Devang Pateld2f79a12009-07-28 19:55:13 +0000764//===----------------------------------------------------------------------===//
Devang Patel98c65172009-07-30 18:25:15 +0000765// DebugInfoFinder implementations.
Devang Pateld2f79a12009-07-28 19:55:13 +0000766//===----------------------------------------------------------------------===//
767
Devang Patel98c65172009-07-30 18:25:15 +0000768/// processModule - Process entire module and collect debug info.
769void DebugInfoFinder::processModule(Module &M) {
Devang Pateld2f79a12009-07-28 19:55:13 +0000770 for (Module::iterator I = M.begin(), E = M.end(); I != E; ++I)
771 for (Function::iterator FI = (*I).begin(), FE = (*I).end(); FI != FE; ++FI)
772 for (BasicBlock::iterator BI = (*FI).begin(), BE = (*FI).end(); BI != BE;
773 ++BI) {
Devang Patel01c5ff62010-05-04 01:05:02 +0000774 if (DbgDeclareInst *DDI = dyn_cast<DbgDeclareInst>(BI))
Devang Patelb4d31302009-07-31 18:18:52 +0000775 processDeclare(DDI);
Jim Grosbache62b6902010-07-21 21:36:25 +0000776
Chris Lattner28a9bf62010-04-02 20:44:29 +0000777 DebugLoc Loc = BI->getDebugLoc();
778 if (Loc.isUnknown())
779 continue;
Jim Grosbache62b6902010-07-21 21:36:25 +0000780
Chris Lattner28a9bf62010-04-02 20:44:29 +0000781 LLVMContext &Ctx = BI->getContext();
782 DIDescriptor Scope(Loc.getScope(Ctx));
Jim Grosbache62b6902010-07-21 21:36:25 +0000783
Chris Lattner28a9bf62010-04-02 20:44:29 +0000784 if (Scope.isCompileUnit())
Devang Patel2db49d72010-05-07 18:11:54 +0000785 addCompileUnit(DICompileUnit(Scope));
Chris Lattner28a9bf62010-04-02 20:44:29 +0000786 else if (Scope.isSubprogram())
Devang Patel2db49d72010-05-07 18:11:54 +0000787 processSubprogram(DISubprogram(Scope));
Chris Lattner28a9bf62010-04-02 20:44:29 +0000788 else if (Scope.isLexicalBlock())
Devang Patel2db49d72010-05-07 18:11:54 +0000789 processLexicalBlock(DILexicalBlock(Scope));
Jim Grosbache62b6902010-07-21 21:36:25 +0000790
Chris Lattner28a9bf62010-04-02 20:44:29 +0000791 if (MDNode *IA = Loc.getInlinedAt(Ctx))
792 processLocation(DILocation(IA));
Devang Pateld2f79a12009-07-28 19:55:13 +0000793 }
Devang Patele4b27562009-08-28 23:24:31 +0000794
Devang Patelfd5fdc32010-06-28 05:53:08 +0000795 if (NamedMDNode *NMD = M.getNamedMetadata("llvm.dbg.gv")) {
796 for (unsigned i = 0, e = NMD->getNumOperands(); i != e; ++i) {
797 DIGlobalVariable DIG(cast<MDNode>(NMD->getOperand(i)));
798 if (addGlobalVariable(DIG)) {
799 addCompileUnit(DIG.getCompileUnit());
800 processType(DIG.getType());
801 }
Devang Pateld2f79a12009-07-28 19:55:13 +0000802 }
803 }
Devang Patelfd5fdc32010-06-28 05:53:08 +0000804
805 if (NamedMDNode *NMD = M.getNamedMetadata("llvm.dbg.sp"))
806 for (unsigned i = 0, e = NMD->getNumOperands(); i != e; ++i)
807 processSubprogram(DISubprogram(NMD->getOperand(i)));
Devang Pateld2f79a12009-07-28 19:55:13 +0000808}
Daniel Dunbarf612ff62009-09-19 20:40:05 +0000809
Devang Patel6daf99b2009-11-10 22:05:35 +0000810/// processLocation - Process DILocation.
811void DebugInfoFinder::processLocation(DILocation Loc) {
Devang Patel3c91b052010-03-08 20:52:55 +0000812 if (!Loc.Verify()) return;
Devang Patel2db49d72010-05-07 18:11:54 +0000813 DIDescriptor S(Loc.getScope());
Devang Patel6daf99b2009-11-10 22:05:35 +0000814 if (S.isCompileUnit())
Devang Patel2db49d72010-05-07 18:11:54 +0000815 addCompileUnit(DICompileUnit(S));
Devang Patel6daf99b2009-11-10 22:05:35 +0000816 else if (S.isSubprogram())
Devang Patel2db49d72010-05-07 18:11:54 +0000817 processSubprogram(DISubprogram(S));
Devang Patel6daf99b2009-11-10 22:05:35 +0000818 else if (S.isLexicalBlock())
Devang Patel2db49d72010-05-07 18:11:54 +0000819 processLexicalBlock(DILexicalBlock(S));
Devang Patel6daf99b2009-11-10 22:05:35 +0000820 processLocation(Loc.getOrigLocation());
821}
822
Devang Patel98c65172009-07-30 18:25:15 +0000823/// processType - Process DIType.
824void DebugInfoFinder::processType(DIType DT) {
Devang Patel72bcdb62009-08-10 22:09:58 +0000825 if (!addType(DT))
Devang Pateld2f79a12009-07-28 19:55:13 +0000826 return;
827
828 addCompileUnit(DT.getCompileUnit());
Devang Patel6ceea332009-08-31 18:49:10 +0000829 if (DT.isCompositeType()) {
Devang Patel2db49d72010-05-07 18:11:54 +0000830 DICompositeType DCT(DT);
Devang Patel98c65172009-07-30 18:25:15 +0000831 processType(DCT.getTypeDerivedFrom());
Devang Pateld2f79a12009-07-28 19:55:13 +0000832 DIArray DA = DCT.getTypeArray();
Devang Patel3c91b052010-03-08 20:52:55 +0000833 for (unsigned i = 0, e = DA.getNumElements(); i != e; ++i) {
834 DIDescriptor D = DA.getElement(i);
835 if (D.isType())
Devang Patel2db49d72010-05-07 18:11:54 +0000836 processType(DIType(D));
Devang Patel3c91b052010-03-08 20:52:55 +0000837 else if (D.isSubprogram())
Devang Patel2db49d72010-05-07 18:11:54 +0000838 processSubprogram(DISubprogram(D));
Devang Patel3c91b052010-03-08 20:52:55 +0000839 }
Devang Patel6ceea332009-08-31 18:49:10 +0000840 } else if (DT.isDerivedType()) {
Devang Patel2db49d72010-05-07 18:11:54 +0000841 DIDerivedType DDT(DT);
Devang Patel3c91b052010-03-08 20:52:55 +0000842 processType(DDT.getTypeDerivedFrom());
Devang Pateld2f79a12009-07-28 19:55:13 +0000843 }
844}
845
Devang Patelbeab41b2009-10-07 22:04:08 +0000846/// processLexicalBlock
847void DebugInfoFinder::processLexicalBlock(DILexicalBlock LB) {
Devang Patelbeab41b2009-10-07 22:04:08 +0000848 DIScope Context = LB.getContext();
849 if (Context.isLexicalBlock())
Devang Patel2db49d72010-05-07 18:11:54 +0000850 return processLexicalBlock(DILexicalBlock(Context));
Devang Patelbeab41b2009-10-07 22:04:08 +0000851 else
Devang Patel2db49d72010-05-07 18:11:54 +0000852 return processSubprogram(DISubprogram(Context));
Devang Patelbeab41b2009-10-07 22:04:08 +0000853}
854
Devang Patel98c65172009-07-30 18:25:15 +0000855/// processSubprogram - Process DISubprogram.
856void DebugInfoFinder::processSubprogram(DISubprogram SP) {
Devang Pateld2f79a12009-07-28 19:55:13 +0000857 if (!addSubprogram(SP))
858 return;
859 addCompileUnit(SP.getCompileUnit());
Devang Patel98c65172009-07-30 18:25:15 +0000860 processType(SP.getType());
Devang Pateld2f79a12009-07-28 19:55:13 +0000861}
862
Devang Patelb4d31302009-07-31 18:18:52 +0000863/// processDeclare - Process DbgDeclareInst.
864void DebugInfoFinder::processDeclare(DbgDeclareInst *DDI) {
Devang Patel3c91b052010-03-08 20:52:55 +0000865 MDNode *N = dyn_cast<MDNode>(DDI->getVariable());
866 if (!N) return;
867
868 DIDescriptor DV(N);
869 if (!DV.isVariable())
Devang Patelb4d31302009-07-31 18:18:52 +0000870 return;
871
Devang Patel2db49d72010-05-07 18:11:54 +0000872 if (!NodesSeen.insert(DV))
Devang Patelb4d31302009-07-31 18:18:52 +0000873 return;
874
Devang Patel3c91b052010-03-08 20:52:55 +0000875 addCompileUnit(DIVariable(N).getCompileUnit());
876 processType(DIVariable(N).getType());
Devang Patelb4d31302009-07-31 18:18:52 +0000877}
878
Devang Patel72bcdb62009-08-10 22:09:58 +0000879/// addType - Add type into Tys.
880bool DebugInfoFinder::addType(DIType DT) {
Devang Patel3c91b052010-03-08 20:52:55 +0000881 if (!DT.isValid())
Devang Patel72bcdb62009-08-10 22:09:58 +0000882 return false;
883
Devang Patel2db49d72010-05-07 18:11:54 +0000884 if (!NodesSeen.insert(DT))
Devang Patel72bcdb62009-08-10 22:09:58 +0000885 return false;
886
Devang Patel2db49d72010-05-07 18:11:54 +0000887 TYs.push_back(DT);
Devang Patel72bcdb62009-08-10 22:09:58 +0000888 return true;
889}
890
Devang Pateld2f79a12009-07-28 19:55:13 +0000891/// addCompileUnit - Add compile unit into CUs.
Devang Patel98c65172009-07-30 18:25:15 +0000892bool DebugInfoFinder::addCompileUnit(DICompileUnit CU) {
Devang Patel3c91b052010-03-08 20:52:55 +0000893 if (!CU.Verify())
Devang Pateld2f79a12009-07-28 19:55:13 +0000894 return false;
895
Devang Patel2db49d72010-05-07 18:11:54 +0000896 if (!NodesSeen.insert(CU))
Devang Pateld2f79a12009-07-28 19:55:13 +0000897 return false;
898
Devang Patel2db49d72010-05-07 18:11:54 +0000899 CUs.push_back(CU);
Devang Pateld2f79a12009-07-28 19:55:13 +0000900 return true;
901}
Daniel Dunbarf612ff62009-09-19 20:40:05 +0000902
Devang Pateld2f79a12009-07-28 19:55:13 +0000903/// addGlobalVariable - Add global variable into GVs.
Devang Patel98c65172009-07-30 18:25:15 +0000904bool DebugInfoFinder::addGlobalVariable(DIGlobalVariable DIG) {
Devang Patel2db49d72010-05-07 18:11:54 +0000905 if (!DIDescriptor(DIG).isGlobalVariable())
Devang Pateld2f79a12009-07-28 19:55:13 +0000906 return false;
907
Devang Patel2db49d72010-05-07 18:11:54 +0000908 if (!NodesSeen.insert(DIG))
Devang Pateld2f79a12009-07-28 19:55:13 +0000909 return false;
910
Devang Patel2db49d72010-05-07 18:11:54 +0000911 GVs.push_back(DIG);
Devang Pateld2f79a12009-07-28 19:55:13 +0000912 return true;
913}
914
915// addSubprogram - Add subprgoram into SPs.
Devang Patel98c65172009-07-30 18:25:15 +0000916bool DebugInfoFinder::addSubprogram(DISubprogram SP) {
Devang Patel2db49d72010-05-07 18:11:54 +0000917 if (!DIDescriptor(SP).isSubprogram())
Devang Pateld2f79a12009-07-28 19:55:13 +0000918 return false;
Daniel Dunbarf612ff62009-09-19 20:40:05 +0000919
Devang Patel2db49d72010-05-07 18:11:54 +0000920 if (!NodesSeen.insert(SP))
Devang Pateld2f79a12009-07-28 19:55:13 +0000921 return false;
922
Devang Patel2db49d72010-05-07 18:11:54 +0000923 SPs.push_back(SP);
Devang Pateld2f79a12009-07-28 19:55:13 +0000924 return true;
925}
926
Chris Lattner099b7792009-12-29 09:22:47 +0000927/// getDISubprogram - Find subprogram that is enclosing this scope.
Devang Patele9f8f5e2010-05-07 20:54:48 +0000928DISubprogram llvm::getDISubprogram(const MDNode *Scope) {
Chris Lattner099b7792009-12-29 09:22:47 +0000929 DIDescriptor D(Scope);
Chris Lattner099b7792009-12-29 09:22:47 +0000930 if (D.isSubprogram())
931 return DISubprogram(Scope);
Jim Grosbache62b6902010-07-21 21:36:25 +0000932
Chris Lattner099b7792009-12-29 09:22:47 +0000933 if (D.isLexicalBlock())
Devang Patel2db49d72010-05-07 18:11:54 +0000934 return getDISubprogram(DILexicalBlock(Scope).getContext());
Jim Grosbache62b6902010-07-21 21:36:25 +0000935
Chris Lattner099b7792009-12-29 09:22:47 +0000936 return DISubprogram();
937}
Devang Patel193f7202009-11-24 01:14:22 +0000938
Chris Lattner099b7792009-12-29 09:22:47 +0000939/// getDICompositeType - Find underlying composite type.
940DICompositeType llvm::getDICompositeType(DIType T) {
Chris Lattner099b7792009-12-29 09:22:47 +0000941 if (T.isCompositeType())
Devang Patel2db49d72010-05-07 18:11:54 +0000942 return DICompositeType(T);
Jim Grosbache62b6902010-07-21 21:36:25 +0000943
Chris Lattner099b7792009-12-29 09:22:47 +0000944 if (T.isDerivedType())
Devang Patel2db49d72010-05-07 18:11:54 +0000945 return getDICompositeType(DIDerivedType(T).getTypeDerivedFrom());
Jim Grosbache62b6902010-07-21 21:36:25 +0000946
Chris Lattner099b7792009-12-29 09:22:47 +0000947 return DICompositeType();
Torok Edwin620f2802008-12-16 09:07:36 +0000948}