blob: 8fac3fb7218a9efeffa593a958a03529589d36ce [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;
Devang Patel23336b42011-07-19 19:41:54 +0000114 if (getVersion() == llvm::LLVMDebugVersion9)
115 return DbgNode->getNumOperands()-7;
116 return DbgNode->getNumOperands()-8;
Chris Lattnerf0908a32009-12-31 03:02:08 +0000117}
118
119
Chris Lattnera45664f2008-11-10 02:56:27 +0000120//===----------------------------------------------------------------------===//
Devang Patel6ceea332009-08-31 18:49:10 +0000121// Predicates
Chris Lattnera45664f2008-11-10 02:56:27 +0000122//===----------------------------------------------------------------------===//
123
Devang Patel6ceea332009-08-31 18:49:10 +0000124/// isBasicType - Return true if the specified tag is legal for
125/// DIBasicType.
126bool DIDescriptor::isBasicType() const {
Devang Patel3c91b052010-03-08 20:52:55 +0000127 return DbgNode && getTag() == dwarf::DW_TAG_base_type;
Torok Edwinb07fbd92008-12-13 08:25:29 +0000128}
Chris Lattnera45664f2008-11-10 02:56:27 +0000129
Devang Patel6ceea332009-08-31 18:49:10 +0000130/// isDerivedType - Return true if the specified tag is legal for DIDerivedType.
131bool DIDescriptor::isDerivedType() const {
Devang Patel3c91b052010-03-08 20:52:55 +0000132 if (!DbgNode) return false;
Chris Lattner099b7792009-12-29 09:22:47 +0000133 switch (getTag()) {
Chris Lattnera45664f2008-11-10 02:56:27 +0000134 case dwarf::DW_TAG_typedef:
135 case dwarf::DW_TAG_pointer_type:
136 case dwarf::DW_TAG_reference_type:
137 case dwarf::DW_TAG_const_type:
138 case dwarf::DW_TAG_volatile_type:
139 case dwarf::DW_TAG_restrict_type:
140 case dwarf::DW_TAG_member:
141 case dwarf::DW_TAG_inheritance:
Devang Patel49d96382010-08-23 23:16:25 +0000142 case dwarf::DW_TAG_friend:
Chris Lattnera45664f2008-11-10 02:56:27 +0000143 return true;
144 default:
Devang Patele4b27562009-08-28 23:24:31 +0000145 // CompositeTypes are currently modelled as DerivedTypes.
Devang Patel6ceea332009-08-31 18:49:10 +0000146 return isCompositeType();
Chris Lattnera45664f2008-11-10 02:56:27 +0000147 }
148}
149
Chris Lattnera45664f2008-11-10 02:56:27 +0000150/// isCompositeType - Return true if the specified tag is legal for
151/// DICompositeType.
Devang Patel6ceea332009-08-31 18:49:10 +0000152bool DIDescriptor::isCompositeType() const {
Devang Patel3c91b052010-03-08 20:52:55 +0000153 if (!DbgNode) return false;
Chris Lattner099b7792009-12-29 09:22:47 +0000154 switch (getTag()) {
Chris Lattnera45664f2008-11-10 02:56:27 +0000155 case dwarf::DW_TAG_array_type:
156 case dwarf::DW_TAG_structure_type:
157 case dwarf::DW_TAG_union_type:
158 case dwarf::DW_TAG_enumeration_type:
159 case dwarf::DW_TAG_vector_type:
160 case dwarf::DW_TAG_subroutine_type:
Devang Patel25cb0d72009-03-25 03:52:06 +0000161 case dwarf::DW_TAG_class_type:
Chris Lattnera45664f2008-11-10 02:56:27 +0000162 return true;
163 default:
164 return false;
165 }
166}
167
Chris Lattnera45664f2008-11-10 02:56:27 +0000168/// isVariable - Return true if the specified tag is legal for DIVariable.
Devang Patel6ceea332009-08-31 18:49:10 +0000169bool DIDescriptor::isVariable() const {
Devang Patel3c91b052010-03-08 20:52:55 +0000170 if (!DbgNode) return false;
Chris Lattner099b7792009-12-29 09:22:47 +0000171 switch (getTag()) {
Chris Lattnera45664f2008-11-10 02:56:27 +0000172 case dwarf::DW_TAG_auto_variable:
173 case dwarf::DW_TAG_arg_variable:
174 case dwarf::DW_TAG_return_variable:
175 return true;
176 default:
177 return false;
178 }
179}
180
Devang Patelecbeb1a2009-09-30 22:34:41 +0000181/// isType - Return true if the specified tag is legal for DIType.
182bool DIDescriptor::isType() const {
183 return isBasicType() || isCompositeType() || isDerivedType();
184}
185
Devang Patel6ceea332009-08-31 18:49:10 +0000186/// isSubprogram - Return true if the specified tag is legal for
187/// DISubprogram.
188bool DIDescriptor::isSubprogram() const {
Devang Patel3c91b052010-03-08 20:52:55 +0000189 return DbgNode && getTag() == dwarf::DW_TAG_subprogram;
Devang Patel6ceea332009-08-31 18:49:10 +0000190}
191
192/// isGlobalVariable - Return true if the specified tag is legal for
193/// DIGlobalVariable.
194bool DIDescriptor::isGlobalVariable() const {
Devang Patel29368072010-08-10 07:11:13 +0000195 return DbgNode && (getTag() == dwarf::DW_TAG_variable ||
196 getTag() == dwarf::DW_TAG_constant);
Devang Patel6ceea332009-08-31 18:49:10 +0000197}
198
Devang Patelecbeb1a2009-09-30 22:34:41 +0000199/// isGlobal - Return true if the specified tag is legal for DIGlobal.
200bool DIDescriptor::isGlobal() const {
201 return isGlobalVariable();
202}
203
Devang Patel716a67f2011-02-03 00:13:47 +0000204/// isUnspecifiedParmeter - Return true if the specified tag is
Devang Pateld6747df2010-10-06 20:50:40 +0000205/// DW_TAG_unspecified_parameters.
206bool DIDescriptor::isUnspecifiedParameter() const {
207 return DbgNode && getTag() == dwarf::DW_TAG_unspecified_parameters;
208}
209
Daniel Dunbarf612ff62009-09-19 20:40:05 +0000210/// isScope - Return true if the specified tag is one of the scope
Devang Patel43d98b32009-08-31 20:44:45 +0000211/// related tag.
212bool DIDescriptor::isScope() const {
Devang Patel3c91b052010-03-08 20:52:55 +0000213 if (!DbgNode) return false;
Chris Lattner099b7792009-12-29 09:22:47 +0000214 switch (getTag()) {
215 case dwarf::DW_TAG_compile_unit:
216 case dwarf::DW_TAG_lexical_block:
217 case dwarf::DW_TAG_subprogram:
218 case dwarf::DW_TAG_namespace:
219 return true;
220 default:
221 break;
Devang Patel43d98b32009-08-31 20:44:45 +0000222 }
223 return false;
224}
Devang Patel6ceea332009-08-31 18:49:10 +0000225
Devang Patel7e2cb112011-02-02 21:38:25 +0000226/// isTemplateTypeParameter - Return true if the specified tag is
227/// DW_TAG_template_type_parameter.
228bool DIDescriptor::isTemplateTypeParameter() const {
229 return DbgNode && getTag() == dwarf::DW_TAG_template_type_parameter;
230}
231
Devang Patele7d93872011-02-02 22:35:53 +0000232/// isTemplateValueParameter - Return true if the specified tag is
233/// DW_TAG_template_value_parameter.
234bool DIDescriptor::isTemplateValueParameter() const {
235 return DbgNode && getTag() == dwarf::DW_TAG_template_value_parameter;
236}
237
Devang Patelc9f322d2009-08-31 21:34:44 +0000238/// isCompileUnit - Return true if the specified tag is DW_TAG_compile_unit.
239bool DIDescriptor::isCompileUnit() const {
Devang Patel3c91b052010-03-08 20:52:55 +0000240 return DbgNode && getTag() == dwarf::DW_TAG_compile_unit;
Devang Patelc9f322d2009-08-31 21:34:44 +0000241}
242
Devang Patel7aa81892010-03-08 22:27:22 +0000243/// isFile - Return true if the specified tag is DW_TAG_file_type.
244bool DIDescriptor::isFile() const {
245 return DbgNode && getTag() == dwarf::DW_TAG_file_type;
246}
247
Devang Patel6404e4e2009-12-15 19:16:48 +0000248/// isNameSpace - Return true if the specified tag is DW_TAG_namespace.
249bool DIDescriptor::isNameSpace() const {
Devang Patel3c91b052010-03-08 20:52:55 +0000250 return DbgNode && getTag() == dwarf::DW_TAG_namespace;
Devang Patel6404e4e2009-12-15 19:16:48 +0000251}
252
Devang Patel5e005d82009-08-31 22:00:15 +0000253/// isLexicalBlock - Return true if the specified tag is DW_TAG_lexical_block.
254bool DIDescriptor::isLexicalBlock() const {
Devang Patel3c91b052010-03-08 20:52:55 +0000255 return DbgNode && getTag() == dwarf::DW_TAG_lexical_block;
Devang Patel5e005d82009-08-31 22:00:15 +0000256}
257
Devang Patelecbeb1a2009-09-30 22:34:41 +0000258/// isSubrange - Return true if the specified tag is DW_TAG_subrange_type.
259bool DIDescriptor::isSubrange() const {
Devang Patel3c91b052010-03-08 20:52:55 +0000260 return DbgNode && getTag() == dwarf::DW_TAG_subrange_type;
Devang Patelecbeb1a2009-09-30 22:34:41 +0000261}
262
263/// isEnumerator - Return true if the specified tag is DW_TAG_enumerator.
264bool DIDescriptor::isEnumerator() const {
Devang Patel3c91b052010-03-08 20:52:55 +0000265 return DbgNode && getTag() == dwarf::DW_TAG_enumerator;
Devang Patelecbeb1a2009-09-30 22:34:41 +0000266}
267
Devang Patel6ceea332009-08-31 18:49:10 +0000268//===----------------------------------------------------------------------===//
269// Simple Descriptor Constructors and other Methods
270//===----------------------------------------------------------------------===//
271
Devang Patele9f8f5e2010-05-07 20:54:48 +0000272DIType::DIType(const MDNode *N) : DIScope(N) {
Devang Patel6ceea332009-08-31 18:49:10 +0000273 if (!N) return;
274 if (!isBasicType() && !isDerivedType() && !isCompositeType()) {
275 DbgNode = 0;
276 }
277}
278
Devang Patel68afdc32009-01-05 18:33:01 +0000279unsigned DIArray::getNumElements() const {
Devang Patel3c91b052010-03-08 20:52:55 +0000280 if (!DbgNode)
281 return 0;
Chris Lattner5d0cacd2009-12-31 01:22:29 +0000282 return DbgNode->getNumOperands();
Devang Patel68afdc32009-01-05 18:33:01 +0000283}
Chris Lattnera45664f2008-11-10 02:56:27 +0000284
Devang Patelc4999d72009-07-22 18:23:44 +0000285/// replaceAllUsesWith - Replace all uses of debug info referenced by
Dan Gohman872814a2010-07-21 18:54:18 +0000286/// this descriptor.
Dan Gohman489b29b2010-08-20 22:02:26 +0000287void DIType::replaceAllUsesWith(DIDescriptor &D) {
Devang Patel3c91b052010-03-08 20:52:55 +0000288 if (!DbgNode)
Devang Patelc4999d72009-07-22 18:23:44 +0000289 return;
290
Daniel Dunbar48a097b2009-09-22 02:03:18 +0000291 // Since we use a TrackingVH for the node, its easy for clients to manufacture
292 // legitimate situations where they want to replaceAllUsesWith() on something
293 // which, due to uniquing, has merged with the source. We shield clients from
294 // this detail by allowing a value to be replaced with replaceAllUsesWith()
295 // itself.
Devang Pateled66bf52010-05-07 18:19:32 +0000296 if (DbgNode != D) {
Devang Patele9f8f5e2010-05-07 20:54:48 +0000297 MDNode *Node = const_cast<MDNode*>(DbgNode);
298 const MDNode *DN = D;
299 const Value *V = cast_or_null<Value>(DN);
300 Node->replaceAllUsesWith(const_cast<Value*>(V));
Dan Gohman489b29b2010-08-20 22:02:26 +0000301 MDNode::deleteTemporary(Node);
Daniel Dunbar48a097b2009-09-22 02:03:18 +0000302 }
Devang Patelc4999d72009-07-22 18:23:44 +0000303}
304
Devang Patel0a2551d2010-12-08 20:18:20 +0000305/// replaceAllUsesWith - Replace all uses of debug info referenced by
306/// this descriptor.
307void DIType::replaceAllUsesWith(MDNode *D) {
308 if (!DbgNode)
309 return;
310
311 // Since we use a TrackingVH for the node, its easy for clients to manufacture
312 // legitimate situations where they want to replaceAllUsesWith() on something
313 // which, due to uniquing, has merged with the source. We shield clients from
314 // this detail by allowing a value to be replaced with replaceAllUsesWith()
315 // itself.
316 if (DbgNode != D) {
317 MDNode *Node = const_cast<MDNode*>(DbgNode);
318 const MDNode *DN = D;
319 const Value *V = cast_or_null<Value>(DN);
320 Node->replaceAllUsesWith(const_cast<Value*>(V));
321 MDNode::deleteTemporary(Node);
322 }
323}
324
Devang Patelb79b5352009-01-19 23:21:49 +0000325/// Verify - Verify that a compile unit is well formed.
326bool DICompileUnit::Verify() const {
Devang Patel3c91b052010-03-08 20:52:55 +0000327 if (!DbgNode)
Devang Patelb79b5352009-01-19 23:21:49 +0000328 return false;
Devang Patel65dbc902009-11-25 17:36:49 +0000329 StringRef N = getFilename();
330 if (N.empty())
Bill Wendling0582ae92009-03-13 04:39:26 +0000331 return false;
Devang Patelb79b5352009-01-19 23:21:49 +0000332 // It is possible that directory and produce string is empty.
Bill Wendling0582ae92009-03-13 04:39:26 +0000333 return true;
Devang Patelb79b5352009-01-19 23:21:49 +0000334}
335
336/// Verify - Verify that a type descriptor is well formed.
337bool DIType::Verify() const {
Devang Patel3c91b052010-03-08 20:52:55 +0000338 if (!DbgNode)
Devang Patelb79b5352009-01-19 23:21:49 +0000339 return false;
Devang Patel3c91b052010-03-08 20:52:55 +0000340 if (!getContext().Verify())
Devang Patelb79b5352009-01-19 23:21:49 +0000341 return false;
Devang Patelb71bbf92010-11-02 20:41:13 +0000342 unsigned Tag = getTag();
343 if (!isBasicType() && Tag != dwarf::DW_TAG_const_type &&
344 Tag != dwarf::DW_TAG_volatile_type && Tag != dwarf::DW_TAG_pointer_type &&
Devang Patelfe58f952010-12-07 23:25:47 +0000345 Tag != dwarf::DW_TAG_reference_type && Tag != dwarf::DW_TAG_restrict_type
Devang Patel43c249c2010-12-08 01:50:15 +0000346 && Tag != dwarf::DW_TAG_vector_type && Tag != dwarf::DW_TAG_array_type
347 && Tag != dwarf::DW_TAG_enumeration_type
Devang Patelfe58f952010-12-07 23:25:47 +0000348 && getFilename().empty())
Devang Patelb79b5352009-01-19 23:21:49 +0000349 return false;
350 return true;
351}
352
Devang Patel0c4720c2010-08-23 18:25:56 +0000353/// Verify - Verify that a basic type descriptor is well formed.
354bool DIBasicType::Verify() const {
355 return isBasicType();
356}
357
358/// Verify - Verify that a derived type descriptor is well formed.
359bool DIDerivedType::Verify() const {
360 return isDerivedType();
361}
362
Devang Patelb79b5352009-01-19 23:21:49 +0000363/// Verify - Verify that a composite type descriptor is well formed.
364bool DICompositeType::Verify() const {
Devang Patel3c91b052010-03-08 20:52:55 +0000365 if (!DbgNode)
Devang Patelb79b5352009-01-19 23:21:49 +0000366 return false;
Devang Patel3c91b052010-03-08 20:52:55 +0000367 if (!getContext().Verify())
Devang Patelb79b5352009-01-19 23:21:49 +0000368 return false;
369
370 DICompileUnit CU = getCompileUnit();
Devang Patel3c91b052010-03-08 20:52:55 +0000371 if (!CU.Verify())
Devang Patelb79b5352009-01-19 23:21:49 +0000372 return false;
373 return true;
374}
375
376/// Verify - Verify that a subprogram descriptor is well formed.
377bool DISubprogram::Verify() const {
Devang Patel3c91b052010-03-08 20:52:55 +0000378 if (!DbgNode)
Devang Patelb79b5352009-01-19 23:21:49 +0000379 return false;
Daniel Dunbarf612ff62009-09-19 20:40:05 +0000380
Devang Patel3c91b052010-03-08 20:52:55 +0000381 if (!getContext().Verify())
Devang Patelb79b5352009-01-19 23:21:49 +0000382 return false;
383
384 DICompileUnit CU = getCompileUnit();
Daniel Dunbarf612ff62009-09-19 20:40:05 +0000385 if (!CU.Verify())
Devang Patelb79b5352009-01-19 23:21:49 +0000386 return false;
387
388 DICompositeType Ty = getType();
Devang Patel3c91b052010-03-08 20:52:55 +0000389 if (!Ty.Verify())
Devang Patelb79b5352009-01-19 23:21:49 +0000390 return false;
391 return true;
392}
393
394/// Verify - Verify that a global variable descriptor is well formed.
395bool DIGlobalVariable::Verify() const {
Devang Patel3c91b052010-03-08 20:52:55 +0000396 if (!DbgNode)
Devang Patelb79b5352009-01-19 23:21:49 +0000397 return false;
Daniel Dunbarf612ff62009-09-19 20:40:05 +0000398
Devang Patel65dbc902009-11-25 17:36:49 +0000399 if (getDisplayName().empty())
Devang Patel84c73e92009-11-06 17:58:12 +0000400 return false;
401
Devang Patel3c91b052010-03-08 20:52:55 +0000402 if (!getContext().Verify())
Devang Patelb79b5352009-01-19 23:21:49 +0000403 return false;
404
405 DICompileUnit CU = getCompileUnit();
Devang Patel3c91b052010-03-08 20:52:55 +0000406 if (!CU.Verify())
Devang Patelb79b5352009-01-19 23:21:49 +0000407 return false;
408
409 DIType Ty = getType();
410 if (!Ty.Verify())
411 return false;
412
Devang Patel27398962010-08-09 21:39:24 +0000413 if (!getGlobal() && !getConstant())
Devang Patelb79b5352009-01-19 23:21:49 +0000414 return false;
415
416 return true;
417}
418
419/// Verify - Verify that a variable descriptor is well formed.
420bool DIVariable::Verify() const {
Devang Patel3c91b052010-03-08 20:52:55 +0000421 if (!DbgNode)
Devang Patelb79b5352009-01-19 23:21:49 +0000422 return false;
Daniel Dunbarf612ff62009-09-19 20:40:05 +0000423
Devang Patel3c91b052010-03-08 20:52:55 +0000424 if (!getContext().Verify())
Devang Patelb79b5352009-01-19 23:21:49 +0000425 return false;
426
Devang Patel62077af2010-05-07 21:42:24 +0000427 if (!getCompileUnit().Verify())
428 return false;
429
Devang Patelb79b5352009-01-19 23:21:49 +0000430 DIType Ty = getType();
431 if (!Ty.Verify())
432 return false;
433
Devang Patelb79b5352009-01-19 23:21:49 +0000434 return true;
435}
436
Devang Patel3c91b052010-03-08 20:52:55 +0000437/// Verify - Verify that a location descriptor is well formed.
438bool DILocation::Verify() const {
439 if (!DbgNode)
440 return false;
Jim Grosbache62b6902010-07-21 21:36:25 +0000441
Devang Patel3c91b052010-03-08 20:52:55 +0000442 return DbgNode->getNumOperands() == 4;
443}
444
Devang Patel47e22652010-05-07 23:04:32 +0000445/// Verify - Verify that a namespace descriptor is well formed.
446bool DINameSpace::Verify() const {
447 if (!DbgNode)
448 return false;
449 if (getName().empty())
450 return false;
451 if (!getCompileUnit().Verify())
452 return false;
453 return true;
454}
455
Devang Patel36375ee2009-02-17 21:23:59 +0000456/// getOriginalTypeSize - If this type is derived from a base type then
457/// return base type size.
458uint64_t DIDerivedType::getOriginalTypeSize() const {
Devang Patel61ecbd12009-11-04 23:48:00 +0000459 unsigned Tag = getTag();
460 if (Tag == dwarf::DW_TAG_member || Tag == dwarf::DW_TAG_typedef ||
461 Tag == dwarf::DW_TAG_const_type || Tag == dwarf::DW_TAG_volatile_type ||
462 Tag == dwarf::DW_TAG_restrict_type) {
463 DIType BaseType = getTypeDerivedFrom();
Jim Grosbache62b6902010-07-21 21:36:25 +0000464 // If this type is not derived from any type then take conservative
Devang Patel5ebfa2d2009-11-06 18:24:05 +0000465 // approach.
Devang Patel3c91b052010-03-08 20:52:55 +0000466 if (!BaseType.isValid())
Devang Patel5ebfa2d2009-11-06 18:24:05 +0000467 return getSizeInBits();
Devang Patel61ecbd12009-11-04 23:48:00 +0000468 if (BaseType.isDerivedType())
Devang Patel2db49d72010-05-07 18:11:54 +0000469 return DIDerivedType(BaseType).getOriginalTypeSize();
Devang Patel61ecbd12009-11-04 23:48:00 +0000470 else
471 return BaseType.getSizeInBits();
472 }
Jim Grosbache62b6902010-07-21 21:36:25 +0000473
Devang Patel61ecbd12009-11-04 23:48:00 +0000474 return getSizeInBits();
Devang Patel36375ee2009-02-17 21:23:59 +0000475}
Devang Patelb79b5352009-01-19 23:21:49 +0000476
Jim Grosbache62b6902010-07-21 21:36:25 +0000477/// isInlinedFnArgument - Return true if this variable provides debugging
Devang Patel719f6a92010-04-29 20:40:36 +0000478/// information for an inlined function arguments.
479bool DIVariable::isInlinedFnArgument(const Function *CurFn) {
480 assert(CurFn && "Invalid function");
481 if (!getContext().isSubprogram())
482 return false;
Jim Grosbache62b6902010-07-21 21:36:25 +0000483 // This variable is not inlined function argument if its scope
Devang Patel719f6a92010-04-29 20:40:36 +0000484 // does not describe current function.
Devang Patel2db49d72010-05-07 18:11:54 +0000485 return !(DISubprogram(getContext()).describes(CurFn));
Devang Patel719f6a92010-04-29 20:40:36 +0000486}
487
Devang Patelaf5b6bb2009-04-15 00:06:07 +0000488/// describes - Return true if this subprogram provides debugging
489/// information for the function F.
490bool DISubprogram::describes(const Function *F) {
Chris Lattner099b7792009-12-29 09:22:47 +0000491 assert(F && "Invalid function");
Devang Patelffd33cd2010-06-16 06:42:02 +0000492 if (F == getFunction())
493 return true;
Devang Patel65dbc902009-11-25 17:36:49 +0000494 StringRef Name = getLinkageName();
495 if (Name.empty())
Devang Patel5ccdd102009-09-29 18:40:58 +0000496 Name = getName();
Devang Patel65dbc902009-11-25 17:36:49 +0000497 if (F->getName() == Name)
Devang Patelaf5b6bb2009-04-15 00:06:07 +0000498 return true;
499 return false;
500}
501
Jim Grosbache62b6902010-07-21 21:36:25 +0000502unsigned DISubprogram::isOptimized() const {
Devang Patelccff8122010-04-30 19:38:23 +0000503 assert (DbgNode && "Invalid subprogram descriptor!");
504 if (DbgNode->getNumOperands() == 16)
505 return getUnsignedField(15);
506 return 0;
507}
508
Devang Patel65dbc902009-11-25 17:36:49 +0000509StringRef DIScope::getFilename() const {
Devang Patel77bf2952010-03-08 22:02:50 +0000510 if (!DbgNode)
511 return StringRef();
Jim Grosbache62b6902010-07-21 21:36:25 +0000512 if (isLexicalBlock())
Devang Patelecbeb1a2009-09-30 22:34:41 +0000513 return DILexicalBlock(DbgNode).getFilename();
Chris Lattner099b7792009-12-29 09:22:47 +0000514 if (isSubprogram())
Devang Patelecbeb1a2009-09-30 22:34:41 +0000515 return DISubprogram(DbgNode).getFilename();
Chris Lattner099b7792009-12-29 09:22:47 +0000516 if (isCompileUnit())
Devang Patelecbeb1a2009-09-30 22:34:41 +0000517 return DICompileUnit(DbgNode).getFilename();
Chris Lattner099b7792009-12-29 09:22:47 +0000518 if (isNameSpace())
Devang Patel6404e4e2009-12-15 19:16:48 +0000519 return DINameSpace(DbgNode).getFilename();
Devang Patel77bf2952010-03-08 22:02:50 +0000520 if (isType())
521 return DIType(DbgNode).getFilename();
Devang Patel7aa81892010-03-08 22:27:22 +0000522 if (isFile())
523 return DIFile(DbgNode).getFilename();
Chris Lattner099b7792009-12-29 09:22:47 +0000524 assert(0 && "Invalid DIScope!");
Devang Patel65dbc902009-11-25 17:36:49 +0000525 return StringRef();
Devang Patelecbeb1a2009-09-30 22:34:41 +0000526}
527
Devang Patel65dbc902009-11-25 17:36:49 +0000528StringRef DIScope::getDirectory() const {
Devang Patel77bf2952010-03-08 22:02:50 +0000529 if (!DbgNode)
530 return StringRef();
Jim Grosbache62b6902010-07-21 21:36:25 +0000531 if (isLexicalBlock())
Devang Patelecbeb1a2009-09-30 22:34:41 +0000532 return DILexicalBlock(DbgNode).getDirectory();
Chris Lattner099b7792009-12-29 09:22:47 +0000533 if (isSubprogram())
Devang Patelecbeb1a2009-09-30 22:34:41 +0000534 return DISubprogram(DbgNode).getDirectory();
Chris Lattner099b7792009-12-29 09:22:47 +0000535 if (isCompileUnit())
Devang Patelecbeb1a2009-09-30 22:34:41 +0000536 return DICompileUnit(DbgNode).getDirectory();
Chris Lattner099b7792009-12-29 09:22:47 +0000537 if (isNameSpace())
Devang Patel6404e4e2009-12-15 19:16:48 +0000538 return DINameSpace(DbgNode).getDirectory();
Devang Patel77bf2952010-03-08 22:02:50 +0000539 if (isType())
540 return DIType(DbgNode).getDirectory();
Devang Patel7aa81892010-03-08 22:27:22 +0000541 if (isFile())
542 return DIFile(DbgNode).getDirectory();
Chris Lattner099b7792009-12-29 09:22:47 +0000543 assert(0 && "Invalid DIScope!");
Devang Patel65dbc902009-11-25 17:36:49 +0000544 return StringRef();
Devang Patelecbeb1a2009-09-30 22:34:41 +0000545}
546
Chris Lattnera45664f2008-11-10 02:56:27 +0000547//===----------------------------------------------------------------------===//
Devang Patel7136a652009-07-01 22:10:23 +0000548// DIDescriptor: dump routines for all descriptors.
549//===----------------------------------------------------------------------===//
550
551
Dan Gohman50404362010-05-07 15:30:29 +0000552/// print - Print descriptor.
553void DIDescriptor::print(raw_ostream &OS) const {
554 OS << "[" << dwarf::TagString(getTag()) << "] ";
555 OS.write_hex((intptr_t) &*DbgNode) << ']';
Devang Patel7136a652009-07-01 22:10:23 +0000556}
557
Dan Gohman50404362010-05-07 15:30:29 +0000558/// print - Print compile unit.
559void DICompileUnit::print(raw_ostream &OS) const {
Devang Patel7136a652009-07-01 22:10:23 +0000560 if (getLanguage())
Dan Gohman50404362010-05-07 15:30:29 +0000561 OS << " [" << dwarf::LanguageString(getLanguage()) << "] ";
Devang Patel7136a652009-07-01 22:10:23 +0000562
Dan Gohmanfaa19c32010-05-10 20:07:44 +0000563 OS << " [" << getDirectory() << "/" << getFilename() << "]";
Devang Patel7136a652009-07-01 22:10:23 +0000564}
565
Dan Gohman50404362010-05-07 15:30:29 +0000566/// print - Print type.
567void DIType::print(raw_ostream &OS) const {
Devang Patel3c91b052010-03-08 20:52:55 +0000568 if (!DbgNode) return;
Devang Patel7136a652009-07-01 22:10:23 +0000569
Devang Patel65dbc902009-11-25 17:36:49 +0000570 StringRef Res = getName();
571 if (!Res.empty())
Dan Gohman50404362010-05-07 15:30:29 +0000572 OS << " [" << Res << "] ";
Devang Patel7136a652009-07-01 22:10:23 +0000573
574 unsigned Tag = getTag();
Dan Gohman50404362010-05-07 15:30:29 +0000575 OS << " [" << dwarf::TagString(Tag) << "] ";
Devang Patel7136a652009-07-01 22:10:23 +0000576
577 // TODO : Print context
Dan Gohmanc014d092010-05-07 16:17:22 +0000578 getCompileUnit().print(OS);
Dan Gohman50404362010-05-07 15:30:29 +0000579 OS << " ["
Dan Gohman9a7063e2010-05-07 16:39:27 +0000580 << "line " << getLineNumber() << ", "
581 << getSizeInBits() << " bits, "
582 << getAlignInBits() << " bit alignment, "
583 << getOffsetInBits() << " bit offset"
Chris Lattnera81d29b2009-08-23 07:33:14 +0000584 << "] ";
Devang Patel7136a652009-07-01 22:10:23 +0000585
Daniel Dunbarf612ff62009-09-19 20:40:05 +0000586 if (isPrivate())
Dan Gohman50404362010-05-07 15:30:29 +0000587 OS << " [private] ";
Devang Patel7136a652009-07-01 22:10:23 +0000588 else if (isProtected())
Dan Gohman50404362010-05-07 15:30:29 +0000589 OS << " [protected] ";
Devang Patel7136a652009-07-01 22:10:23 +0000590
591 if (isForwardDecl())
Dan Gohman50404362010-05-07 15:30:29 +0000592 OS << " [fwd] ";
Devang Patel7136a652009-07-01 22:10:23 +0000593
Devang Patel6ceea332009-08-31 18:49:10 +0000594 if (isBasicType())
Dan Gohmanc014d092010-05-07 16:17:22 +0000595 DIBasicType(DbgNode).print(OS);
Devang Patel6ceea332009-08-31 18:49:10 +0000596 else if (isDerivedType())
Dan Gohmanc014d092010-05-07 16:17:22 +0000597 DIDerivedType(DbgNode).print(OS);
Devang Patel6ceea332009-08-31 18:49:10 +0000598 else if (isCompositeType())
Dan Gohmanc014d092010-05-07 16:17:22 +0000599 DICompositeType(DbgNode).print(OS);
Devang Patel7136a652009-07-01 22:10:23 +0000600 else {
Dan Gohman50404362010-05-07 15:30:29 +0000601 OS << "Invalid DIType\n";
Devang Patel7136a652009-07-01 22:10:23 +0000602 return;
603 }
604
Dan Gohman50404362010-05-07 15:30:29 +0000605 OS << "\n";
Devang Patel7136a652009-07-01 22:10:23 +0000606}
607
Dan Gohman50404362010-05-07 15:30:29 +0000608/// print - Print basic type.
609void DIBasicType::print(raw_ostream &OS) const {
610 OS << " [" << dwarf::AttributeEncodingString(getEncoding()) << "] ";
Devang Patel7136a652009-07-01 22:10:23 +0000611}
612
Dan Gohman50404362010-05-07 15:30:29 +0000613/// print - Print derived type.
614void DIDerivedType::print(raw_ostream &OS) const {
Dan Gohmanc014d092010-05-07 16:17:22 +0000615 OS << "\n\t Derived From: "; getTypeDerivedFrom().print(OS);
Devang Patel7136a652009-07-01 22:10:23 +0000616}
617
Dan Gohman50404362010-05-07 15:30:29 +0000618/// print - Print composite type.
619void DICompositeType::print(raw_ostream &OS) const {
Devang Patel7136a652009-07-01 22:10:23 +0000620 DIArray A = getTypeArray();
Dan Gohman50404362010-05-07 15:30:29 +0000621 OS << " [" << A.getNumElements() << " elements]";
Devang Patel7136a652009-07-01 22:10:23 +0000622}
623
Dan Gohman50404362010-05-07 15:30:29 +0000624/// print - Print subprogram.
625void DISubprogram::print(raw_ostream &OS) const {
Devang Patel65dbc902009-11-25 17:36:49 +0000626 StringRef Res = getName();
627 if (!Res.empty())
Dan Gohman50404362010-05-07 15:30:29 +0000628 OS << " [" << Res << "] ";
Devang Patel82dfc0c2009-08-31 22:47:13 +0000629
630 unsigned Tag = getTag();
Dan Gohman50404362010-05-07 15:30:29 +0000631 OS << " [" << dwarf::TagString(Tag) << "] ";
Devang Patel82dfc0c2009-08-31 22:47:13 +0000632
633 // TODO : Print context
Dan Gohmanc014d092010-05-07 16:17:22 +0000634 getCompileUnit().print(OS);
Dan Gohman50404362010-05-07 15:30:29 +0000635 OS << " [" << getLineNumber() << "] ";
Devang Patel82dfc0c2009-08-31 22:47:13 +0000636
637 if (isLocalToUnit())
Dan Gohman50404362010-05-07 15:30:29 +0000638 OS << " [local] ";
Devang Patel82dfc0c2009-08-31 22:47:13 +0000639
640 if (isDefinition())
Dan Gohman50404362010-05-07 15:30:29 +0000641 OS << " [def] ";
Devang Patel82dfc0c2009-08-31 22:47:13 +0000642
Dan Gohman50404362010-05-07 15:30:29 +0000643 OS << "\n";
644}
645
646/// print - Print global variable.
647void DIGlobalVariable::print(raw_ostream &OS) const {
648 OS << " [";
Devang Patela49d8772010-05-07 23:19:07 +0000649 StringRef Res = getName();
650 if (!Res.empty())
651 OS << " [" << Res << "] ";
652
653 unsigned Tag = getTag();
654 OS << " [" << dwarf::TagString(Tag) << "] ";
655
656 // TODO : Print context
657 getCompileUnit().print(OS);
658 OS << " [" << getLineNumber() << "] ";
659
660 if (isLocalToUnit())
661 OS << " [local] ";
662
663 if (isDefinition())
664 OS << " [def] ";
665
666 if (isGlobalVariable())
667 DIGlobalVariable(DbgNode).print(OS);
668 OS << "]\n";
Dan Gohman50404362010-05-07 15:30:29 +0000669}
670
671/// print - Print variable.
672void DIVariable::print(raw_ostream &OS) const {
673 StringRef Res = getName();
674 if (!Res.empty())
675 OS << " [" << Res << "] ";
676
Dan Gohmanc014d092010-05-07 16:17:22 +0000677 getCompileUnit().print(OS);
Dan Gohman50404362010-05-07 15:30:29 +0000678 OS << " [" << getLineNumber() << "] ";
Dan Gohmanc014d092010-05-07 16:17:22 +0000679 getType().print(OS);
Dan Gohman50404362010-05-07 15:30:29 +0000680 OS << "\n";
681
682 // FIXME: Dump complex addresses
683}
684
685/// dump - Print descriptor to dbgs() with a newline.
686void DIDescriptor::dump() const {
687 print(dbgs()); dbgs() << '\n';
688}
689
690/// dump - Print compile unit to dbgs() with a newline.
691void DICompileUnit::dump() const {
692 print(dbgs()); dbgs() << '\n';
693}
694
695/// dump - Print type to dbgs() with a newline.
696void DIType::dump() const {
697 print(dbgs()); dbgs() << '\n';
698}
699
700/// dump - Print basic type to dbgs() with a newline.
701void DIBasicType::dump() const {
702 print(dbgs()); dbgs() << '\n';
703}
704
705/// dump - Print derived type to dbgs() with a newline.
706void DIDerivedType::dump() const {
707 print(dbgs()); dbgs() << '\n';
708}
709
710/// dump - Print composite type to dbgs() with a newline.
711void DICompositeType::dump() const {
712 print(dbgs()); dbgs() << '\n';
713}
714
Dan Gohman50404362010-05-07 15:30:29 +0000715/// dump - Print subprogram to dbgs() with a newline.
716void DISubprogram::dump() const {
717 print(dbgs()); dbgs() << '\n';
Devang Patel7136a652009-07-01 22:10:23 +0000718}
719
720/// dump - Print global variable.
721void DIGlobalVariable::dump() const {
Dan Gohman50404362010-05-07 15:30:29 +0000722 print(dbgs()); dbgs() << '\n';
Devang Patel7136a652009-07-01 22:10:23 +0000723}
724
725/// dump - Print variable.
726void DIVariable::dump() const {
Dan Gohman50404362010-05-07 15:30:29 +0000727 print(dbgs()); dbgs() << '\n';
Devang Patel7136a652009-07-01 22:10:23 +0000728}
729
Devang Patel62367042010-11-10 22:19:21 +0000730/// fixupObjcLikeName - Replace contains special characters used
731/// in a typical Objective-C names with '.' in a given string.
Benjamin Kramer1a81d482011-06-18 14:42:42 +0000732static void fixupObjcLikeName(StringRef Str, SmallVectorImpl<char> &Out) {
733 bool isObjCLike = false;
Devang Patel62367042010-11-10 22:19:21 +0000734 for (size_t i = 0, e = Str.size(); i < e; ++i) {
735 char C = Str[i];
Benjamin Kramer1a81d482011-06-18 14:42:42 +0000736 if (C == '[')
737 isObjCLike = true;
738
739 if (isObjCLike && (C == '[' || C == ']' || C == ' ' || C == ':' ||
740 C == '+' || C == '(' || C == ')'))
741 Out.push_back('.');
742 else
743 Out.push_back(C);
Devang Patel62367042010-11-10 22:19:21 +0000744 }
745}
746
Devang Patel62367042010-11-10 22:19:21 +0000747/// getFnSpecificMDNode - Return a NameMDNode, if available, that is
748/// suitable to hold function specific information.
749NamedMDNode *llvm::getFnSpecificMDNode(const Module &M, StringRef FuncName) {
Benjamin Kramer1a81d482011-06-18 14:42:42 +0000750 SmallString<32> Name = StringRef("llvm.dbg.lv.");
751 fixupObjcLikeName(FuncName, Name);
752
753 return M.getNamedMetadata(Name.str());
Devang Patel62367042010-11-10 22:19:21 +0000754}
755
Duncan Sands291bb702011-03-02 20:30:37 +0000756/// getOrInsertFnSpecificMDNode - Return a NameMDNode that is suitable
757/// to hold function specific information.
758NamedMDNode *llvm::getOrInsertFnSpecificMDNode(Module &M, StringRef FuncName) {
Benjamin Kramer1a81d482011-06-18 14:42:42 +0000759 SmallString<32> Name = StringRef("llvm.dbg.lv.");
760 fixupObjcLikeName(FuncName, Name);
761
762 return M.getOrInsertNamedMetadata(Name.str());
Devang Patel1a7ca032010-09-28 18:08:20 +0000763}
764
Devang Patel23336b42011-07-19 19:41:54 +0000765/// createInlinedVariable - Create a new inlined variable based on current
766/// variable.
767/// @param DV Current Variable.
768/// @param InlinedScope Location at current variable is inlined.
769DIVariable llvm::createInlinedVariable(MDNode *DV, MDNode *InlinedScope,
770 LLVMContext &VMContext) {
771 SmallVector<Value *, 16> Elts;
772 // Insert inlined scope as 7th element.
773 for (unsigned i = 0, e = DV->getNumOperands(); i != e; ++i)
774 i == 7 ? Elts.push_back(InlinedScope) :
775 Elts.push_back(DV->getOperand(i));
776 return DIVariable(MDNode::get(VMContext, Elts));
777}
Devang Patel1a7ca032010-09-28 18:08:20 +0000778
Devang Pateld2f79a12009-07-28 19:55:13 +0000779//===----------------------------------------------------------------------===//
Devang Patel98c65172009-07-30 18:25:15 +0000780// DebugInfoFinder implementations.
Devang Pateld2f79a12009-07-28 19:55:13 +0000781//===----------------------------------------------------------------------===//
782
Devang Patel98c65172009-07-30 18:25:15 +0000783/// processModule - Process entire module and collect debug info.
784void DebugInfoFinder::processModule(Module &M) {
Devang Pateld2f79a12009-07-28 19:55:13 +0000785 for (Module::iterator I = M.begin(), E = M.end(); I != E; ++I)
786 for (Function::iterator FI = (*I).begin(), FE = (*I).end(); FI != FE; ++FI)
787 for (BasicBlock::iterator BI = (*FI).begin(), BE = (*FI).end(); BI != BE;
788 ++BI) {
Devang Patel01c5ff62010-05-04 01:05:02 +0000789 if (DbgDeclareInst *DDI = dyn_cast<DbgDeclareInst>(BI))
Devang Patelb4d31302009-07-31 18:18:52 +0000790 processDeclare(DDI);
Jim Grosbache62b6902010-07-21 21:36:25 +0000791
Chris Lattner28a9bf62010-04-02 20:44:29 +0000792 DebugLoc Loc = BI->getDebugLoc();
793 if (Loc.isUnknown())
794 continue;
Jim Grosbache62b6902010-07-21 21:36:25 +0000795
Chris Lattner28a9bf62010-04-02 20:44:29 +0000796 LLVMContext &Ctx = BI->getContext();
797 DIDescriptor Scope(Loc.getScope(Ctx));
Jim Grosbache62b6902010-07-21 21:36:25 +0000798
Chris Lattner28a9bf62010-04-02 20:44:29 +0000799 if (Scope.isCompileUnit())
Devang Patel2db49d72010-05-07 18:11:54 +0000800 addCompileUnit(DICompileUnit(Scope));
Chris Lattner28a9bf62010-04-02 20:44:29 +0000801 else if (Scope.isSubprogram())
Devang Patel2db49d72010-05-07 18:11:54 +0000802 processSubprogram(DISubprogram(Scope));
Chris Lattner28a9bf62010-04-02 20:44:29 +0000803 else if (Scope.isLexicalBlock())
Devang Patel2db49d72010-05-07 18:11:54 +0000804 processLexicalBlock(DILexicalBlock(Scope));
Jim Grosbache62b6902010-07-21 21:36:25 +0000805
Chris Lattner28a9bf62010-04-02 20:44:29 +0000806 if (MDNode *IA = Loc.getInlinedAt(Ctx))
807 processLocation(DILocation(IA));
Devang Pateld2f79a12009-07-28 19:55:13 +0000808 }
Devang Patele4b27562009-08-28 23:24:31 +0000809
Devang Patelfd5fdc32010-06-28 05:53:08 +0000810 if (NamedMDNode *NMD = M.getNamedMetadata("llvm.dbg.gv")) {
811 for (unsigned i = 0, e = NMD->getNumOperands(); i != e; ++i) {
812 DIGlobalVariable DIG(cast<MDNode>(NMD->getOperand(i)));
813 if (addGlobalVariable(DIG)) {
814 addCompileUnit(DIG.getCompileUnit());
815 processType(DIG.getType());
816 }
Devang Pateld2f79a12009-07-28 19:55:13 +0000817 }
818 }
Devang Patelfd5fdc32010-06-28 05:53:08 +0000819
820 if (NamedMDNode *NMD = M.getNamedMetadata("llvm.dbg.sp"))
821 for (unsigned i = 0, e = NMD->getNumOperands(); i != e; ++i)
822 processSubprogram(DISubprogram(NMD->getOperand(i)));
Devang Pateld2f79a12009-07-28 19:55:13 +0000823}
Daniel Dunbarf612ff62009-09-19 20:40:05 +0000824
Devang Patel6daf99b2009-11-10 22:05:35 +0000825/// processLocation - Process DILocation.
826void DebugInfoFinder::processLocation(DILocation Loc) {
Devang Patel3c91b052010-03-08 20:52:55 +0000827 if (!Loc.Verify()) return;
Devang Patel2db49d72010-05-07 18:11:54 +0000828 DIDescriptor S(Loc.getScope());
Devang Patel6daf99b2009-11-10 22:05:35 +0000829 if (S.isCompileUnit())
Devang Patel2db49d72010-05-07 18:11:54 +0000830 addCompileUnit(DICompileUnit(S));
Devang Patel6daf99b2009-11-10 22:05:35 +0000831 else if (S.isSubprogram())
Devang Patel2db49d72010-05-07 18:11:54 +0000832 processSubprogram(DISubprogram(S));
Devang Patel6daf99b2009-11-10 22:05:35 +0000833 else if (S.isLexicalBlock())
Devang Patel2db49d72010-05-07 18:11:54 +0000834 processLexicalBlock(DILexicalBlock(S));
Devang Patel6daf99b2009-11-10 22:05:35 +0000835 processLocation(Loc.getOrigLocation());
836}
837
Devang Patel98c65172009-07-30 18:25:15 +0000838/// processType - Process DIType.
839void DebugInfoFinder::processType(DIType DT) {
Devang Patel72bcdb62009-08-10 22:09:58 +0000840 if (!addType(DT))
Devang Pateld2f79a12009-07-28 19:55:13 +0000841 return;
842
843 addCompileUnit(DT.getCompileUnit());
Devang Patel6ceea332009-08-31 18:49:10 +0000844 if (DT.isCompositeType()) {
Devang Patel2db49d72010-05-07 18:11:54 +0000845 DICompositeType DCT(DT);
Devang Patel98c65172009-07-30 18:25:15 +0000846 processType(DCT.getTypeDerivedFrom());
Devang Pateld2f79a12009-07-28 19:55:13 +0000847 DIArray DA = DCT.getTypeArray();
Devang Patel3c91b052010-03-08 20:52:55 +0000848 for (unsigned i = 0, e = DA.getNumElements(); i != e; ++i) {
849 DIDescriptor D = DA.getElement(i);
850 if (D.isType())
Devang Patel2db49d72010-05-07 18:11:54 +0000851 processType(DIType(D));
Devang Patel3c91b052010-03-08 20:52:55 +0000852 else if (D.isSubprogram())
Devang Patel2db49d72010-05-07 18:11:54 +0000853 processSubprogram(DISubprogram(D));
Devang Patel3c91b052010-03-08 20:52:55 +0000854 }
Devang Patel6ceea332009-08-31 18:49:10 +0000855 } else if (DT.isDerivedType()) {
Devang Patel2db49d72010-05-07 18:11:54 +0000856 DIDerivedType DDT(DT);
Devang Patel3c91b052010-03-08 20:52:55 +0000857 processType(DDT.getTypeDerivedFrom());
Devang Pateld2f79a12009-07-28 19:55:13 +0000858 }
859}
860
Devang Patelbeab41b2009-10-07 22:04:08 +0000861/// processLexicalBlock
862void DebugInfoFinder::processLexicalBlock(DILexicalBlock LB) {
Devang Patelbeab41b2009-10-07 22:04:08 +0000863 DIScope Context = LB.getContext();
864 if (Context.isLexicalBlock())
Devang Patel2db49d72010-05-07 18:11:54 +0000865 return processLexicalBlock(DILexicalBlock(Context));
Devang Patelbeab41b2009-10-07 22:04:08 +0000866 else
Devang Patel2db49d72010-05-07 18:11:54 +0000867 return processSubprogram(DISubprogram(Context));
Devang Patelbeab41b2009-10-07 22:04:08 +0000868}
869
Devang Patel98c65172009-07-30 18:25:15 +0000870/// processSubprogram - Process DISubprogram.
871void DebugInfoFinder::processSubprogram(DISubprogram SP) {
Devang Pateld2f79a12009-07-28 19:55:13 +0000872 if (!addSubprogram(SP))
873 return;
874 addCompileUnit(SP.getCompileUnit());
Devang Patel98c65172009-07-30 18:25:15 +0000875 processType(SP.getType());
Devang Pateld2f79a12009-07-28 19:55:13 +0000876}
877
Devang Patelb4d31302009-07-31 18:18:52 +0000878/// processDeclare - Process DbgDeclareInst.
879void DebugInfoFinder::processDeclare(DbgDeclareInst *DDI) {
Devang Patel3c91b052010-03-08 20:52:55 +0000880 MDNode *N = dyn_cast<MDNode>(DDI->getVariable());
881 if (!N) return;
882
883 DIDescriptor DV(N);
884 if (!DV.isVariable())
Devang Patelb4d31302009-07-31 18:18:52 +0000885 return;
886
Devang Patel2db49d72010-05-07 18:11:54 +0000887 if (!NodesSeen.insert(DV))
Devang Patelb4d31302009-07-31 18:18:52 +0000888 return;
889
Devang Patel3c91b052010-03-08 20:52:55 +0000890 addCompileUnit(DIVariable(N).getCompileUnit());
891 processType(DIVariable(N).getType());
Devang Patelb4d31302009-07-31 18:18:52 +0000892}
893
Devang Patel72bcdb62009-08-10 22:09:58 +0000894/// addType - Add type into Tys.
895bool DebugInfoFinder::addType(DIType DT) {
Devang Patel3c91b052010-03-08 20:52:55 +0000896 if (!DT.isValid())
Devang Patel72bcdb62009-08-10 22:09:58 +0000897 return false;
898
Devang Patel2db49d72010-05-07 18:11:54 +0000899 if (!NodesSeen.insert(DT))
Devang Patel72bcdb62009-08-10 22:09:58 +0000900 return false;
901
Devang Patel2db49d72010-05-07 18:11:54 +0000902 TYs.push_back(DT);
Devang Patel72bcdb62009-08-10 22:09:58 +0000903 return true;
904}
905
Devang Pateld2f79a12009-07-28 19:55:13 +0000906/// addCompileUnit - Add compile unit into CUs.
Devang Patel98c65172009-07-30 18:25:15 +0000907bool DebugInfoFinder::addCompileUnit(DICompileUnit CU) {
Devang Patel3c91b052010-03-08 20:52:55 +0000908 if (!CU.Verify())
Devang Pateld2f79a12009-07-28 19:55:13 +0000909 return false;
910
Devang Patel2db49d72010-05-07 18:11:54 +0000911 if (!NodesSeen.insert(CU))
Devang Pateld2f79a12009-07-28 19:55:13 +0000912 return false;
913
Devang Patel2db49d72010-05-07 18:11:54 +0000914 CUs.push_back(CU);
Devang Pateld2f79a12009-07-28 19:55:13 +0000915 return true;
916}
Daniel Dunbarf612ff62009-09-19 20:40:05 +0000917
Devang Pateld2f79a12009-07-28 19:55:13 +0000918/// addGlobalVariable - Add global variable into GVs.
Devang Patel98c65172009-07-30 18:25:15 +0000919bool DebugInfoFinder::addGlobalVariable(DIGlobalVariable DIG) {
Devang Patel2db49d72010-05-07 18:11:54 +0000920 if (!DIDescriptor(DIG).isGlobalVariable())
Devang Pateld2f79a12009-07-28 19:55:13 +0000921 return false;
922
Devang Patel2db49d72010-05-07 18:11:54 +0000923 if (!NodesSeen.insert(DIG))
Devang Pateld2f79a12009-07-28 19:55:13 +0000924 return false;
925
Devang Patel2db49d72010-05-07 18:11:54 +0000926 GVs.push_back(DIG);
Devang Pateld2f79a12009-07-28 19:55:13 +0000927 return true;
928}
929
930// addSubprogram - Add subprgoram into SPs.
Devang Patel98c65172009-07-30 18:25:15 +0000931bool DebugInfoFinder::addSubprogram(DISubprogram SP) {
Devang Patel2db49d72010-05-07 18:11:54 +0000932 if (!DIDescriptor(SP).isSubprogram())
Devang Pateld2f79a12009-07-28 19:55:13 +0000933 return false;
Daniel Dunbarf612ff62009-09-19 20:40:05 +0000934
Devang Patel2db49d72010-05-07 18:11:54 +0000935 if (!NodesSeen.insert(SP))
Devang Pateld2f79a12009-07-28 19:55:13 +0000936 return false;
937
Devang Patel2db49d72010-05-07 18:11:54 +0000938 SPs.push_back(SP);
Devang Pateld2f79a12009-07-28 19:55:13 +0000939 return true;
940}
941
Chris Lattner099b7792009-12-29 09:22:47 +0000942/// getDISubprogram - Find subprogram that is enclosing this scope.
Devang Patele9f8f5e2010-05-07 20:54:48 +0000943DISubprogram llvm::getDISubprogram(const MDNode *Scope) {
Chris Lattner099b7792009-12-29 09:22:47 +0000944 DIDescriptor D(Scope);
Chris Lattner099b7792009-12-29 09:22:47 +0000945 if (D.isSubprogram())
946 return DISubprogram(Scope);
Jim Grosbache62b6902010-07-21 21:36:25 +0000947
Chris Lattner099b7792009-12-29 09:22:47 +0000948 if (D.isLexicalBlock())
Devang Patel2db49d72010-05-07 18:11:54 +0000949 return getDISubprogram(DILexicalBlock(Scope).getContext());
Jim Grosbache62b6902010-07-21 21:36:25 +0000950
Chris Lattner099b7792009-12-29 09:22:47 +0000951 return DISubprogram();
952}
Devang Patel193f7202009-11-24 01:14:22 +0000953
Chris Lattner099b7792009-12-29 09:22:47 +0000954/// getDICompositeType - Find underlying composite type.
955DICompositeType llvm::getDICompositeType(DIType T) {
Chris Lattner099b7792009-12-29 09:22:47 +0000956 if (T.isCompositeType())
Devang Patel2db49d72010-05-07 18:11:54 +0000957 return DICompositeType(T);
Jim Grosbache62b6902010-07-21 21:36:25 +0000958
Chris Lattner099b7792009-12-29 09:22:47 +0000959 if (T.isDerivedType())
Devang Patel2db49d72010-05-07 18:11:54 +0000960 return getDICompositeType(DIDerivedType(T).getTypeDerivedFrom());
Jim Grosbache62b6902010-07-21 21:36:25 +0000961
Chris Lattner099b7792009-12-29 09:22:47 +0000962 return DICompositeType();
Torok Edwin620f2802008-12-16 09:07:36 +0000963}