blob: 7935fadb2fa88e3a61c753380d29f9681792dddc [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
Devang Patel40c7e412011-07-20 22:18:50 +0000119/// getInlinedAt - If this variable is inlined then return inline location.
Devang Patel48d726f2011-08-09 01:03:14 +0000120MDNode *DIVariable::getInlinedAt() const {
Devang Patel40c7e412011-07-20 22:18:50 +0000121 if (getVersion() <= llvm::LLVMDebugVersion9)
122 return NULL;
123 return dyn_cast_or_null<MDNode>(DbgNode->getOperand(7));
124}
Chris Lattnerf0908a32009-12-31 03:02:08 +0000125
Chris Lattnera45664f2008-11-10 02:56:27 +0000126//===----------------------------------------------------------------------===//
Devang Patel6ceea332009-08-31 18:49:10 +0000127// Predicates
Chris Lattnera45664f2008-11-10 02:56:27 +0000128//===----------------------------------------------------------------------===//
129
Devang Patel6ceea332009-08-31 18:49:10 +0000130/// isBasicType - Return true if the specified tag is legal for
131/// DIBasicType.
132bool DIDescriptor::isBasicType() const {
Devang Patel3c91b052010-03-08 20:52:55 +0000133 return DbgNode && getTag() == dwarf::DW_TAG_base_type;
Torok Edwinb07fbd92008-12-13 08:25:29 +0000134}
Chris Lattnera45664f2008-11-10 02:56:27 +0000135
Devang Patel6ceea332009-08-31 18:49:10 +0000136/// isDerivedType - Return true if the specified tag is legal for DIDerivedType.
137bool DIDescriptor::isDerivedType() const {
Devang Patel3c91b052010-03-08 20:52:55 +0000138 if (!DbgNode) return false;
Chris Lattner099b7792009-12-29 09:22:47 +0000139 switch (getTag()) {
Chris Lattnera45664f2008-11-10 02:56:27 +0000140 case dwarf::DW_TAG_typedef:
141 case dwarf::DW_TAG_pointer_type:
142 case dwarf::DW_TAG_reference_type:
143 case dwarf::DW_TAG_const_type:
144 case dwarf::DW_TAG_volatile_type:
145 case dwarf::DW_TAG_restrict_type:
146 case dwarf::DW_TAG_member:
147 case dwarf::DW_TAG_inheritance:
Devang Patel49d96382010-08-23 23:16:25 +0000148 case dwarf::DW_TAG_friend:
Chris Lattnera45664f2008-11-10 02:56:27 +0000149 return true;
150 default:
Devang Patele4b27562009-08-28 23:24:31 +0000151 // CompositeTypes are currently modelled as DerivedTypes.
Devang Patel6ceea332009-08-31 18:49:10 +0000152 return isCompositeType();
Chris Lattnera45664f2008-11-10 02:56:27 +0000153 }
154}
155
Chris Lattnera45664f2008-11-10 02:56:27 +0000156/// isCompositeType - Return true if the specified tag is legal for
157/// DICompositeType.
Devang Patel6ceea332009-08-31 18:49:10 +0000158bool DIDescriptor::isCompositeType() const {
Devang Patel3c91b052010-03-08 20:52:55 +0000159 if (!DbgNode) return false;
Chris Lattner099b7792009-12-29 09:22:47 +0000160 switch (getTag()) {
Chris Lattnera45664f2008-11-10 02:56:27 +0000161 case dwarf::DW_TAG_array_type:
162 case dwarf::DW_TAG_structure_type:
163 case dwarf::DW_TAG_union_type:
164 case dwarf::DW_TAG_enumeration_type:
165 case dwarf::DW_TAG_vector_type:
166 case dwarf::DW_TAG_subroutine_type:
Devang Patel25cb0d72009-03-25 03:52:06 +0000167 case dwarf::DW_TAG_class_type:
Chris Lattnera45664f2008-11-10 02:56:27 +0000168 return true;
169 default:
170 return false;
171 }
172}
173
Chris Lattnera45664f2008-11-10 02:56:27 +0000174/// isVariable - Return true if the specified tag is legal for DIVariable.
Devang Patel6ceea332009-08-31 18:49:10 +0000175bool DIDescriptor::isVariable() const {
Devang Patel3c91b052010-03-08 20:52:55 +0000176 if (!DbgNode) return false;
Chris Lattner099b7792009-12-29 09:22:47 +0000177 switch (getTag()) {
Chris Lattnera45664f2008-11-10 02:56:27 +0000178 case dwarf::DW_TAG_auto_variable:
179 case dwarf::DW_TAG_arg_variable:
180 case dwarf::DW_TAG_return_variable:
181 return true;
182 default:
183 return false;
184 }
185}
186
Devang Patelecbeb1a2009-09-30 22:34:41 +0000187/// isType - Return true if the specified tag is legal for DIType.
188bool DIDescriptor::isType() const {
189 return isBasicType() || isCompositeType() || isDerivedType();
190}
191
Devang Patel6ceea332009-08-31 18:49:10 +0000192/// isSubprogram - Return true if the specified tag is legal for
193/// DISubprogram.
194bool DIDescriptor::isSubprogram() const {
Devang Patel3c91b052010-03-08 20:52:55 +0000195 return DbgNode && getTag() == dwarf::DW_TAG_subprogram;
Devang Patel6ceea332009-08-31 18:49:10 +0000196}
197
198/// isGlobalVariable - Return true if the specified tag is legal for
199/// DIGlobalVariable.
200bool DIDescriptor::isGlobalVariable() const {
Devang Patel29368072010-08-10 07:11:13 +0000201 return DbgNode && (getTag() == dwarf::DW_TAG_variable ||
202 getTag() == dwarf::DW_TAG_constant);
Devang Patel6ceea332009-08-31 18:49:10 +0000203}
204
Devang Patelecbeb1a2009-09-30 22:34:41 +0000205/// isGlobal - Return true if the specified tag is legal for DIGlobal.
206bool DIDescriptor::isGlobal() const {
207 return isGlobalVariable();
208}
209
Devang Patel716a67f2011-02-03 00:13:47 +0000210/// isUnspecifiedParmeter - Return true if the specified tag is
Devang Pateld6747df2010-10-06 20:50:40 +0000211/// DW_TAG_unspecified_parameters.
212bool DIDescriptor::isUnspecifiedParameter() const {
213 return DbgNode && getTag() == dwarf::DW_TAG_unspecified_parameters;
214}
215
Daniel Dunbarf612ff62009-09-19 20:40:05 +0000216/// isScope - Return true if the specified tag is one of the scope
Devang Patel43d98b32009-08-31 20:44:45 +0000217/// related tag.
218bool DIDescriptor::isScope() const {
Devang Patel3c91b052010-03-08 20:52:55 +0000219 if (!DbgNode) return false;
Chris Lattner099b7792009-12-29 09:22:47 +0000220 switch (getTag()) {
221 case dwarf::DW_TAG_compile_unit:
222 case dwarf::DW_TAG_lexical_block:
223 case dwarf::DW_TAG_subprogram:
224 case dwarf::DW_TAG_namespace:
225 return true;
226 default:
227 break;
Devang Patel43d98b32009-08-31 20:44:45 +0000228 }
229 return false;
230}
Devang Patel6ceea332009-08-31 18:49:10 +0000231
Devang Patel7e2cb112011-02-02 21:38:25 +0000232/// isTemplateTypeParameter - Return true if the specified tag is
233/// DW_TAG_template_type_parameter.
234bool DIDescriptor::isTemplateTypeParameter() const {
235 return DbgNode && getTag() == dwarf::DW_TAG_template_type_parameter;
236}
237
Devang Patele7d93872011-02-02 22:35:53 +0000238/// isTemplateValueParameter - Return true if the specified tag is
239/// DW_TAG_template_value_parameter.
240bool DIDescriptor::isTemplateValueParameter() const {
241 return DbgNode && getTag() == dwarf::DW_TAG_template_value_parameter;
242}
243
Devang Patelc9f322d2009-08-31 21:34:44 +0000244/// isCompileUnit - Return true if the specified tag is DW_TAG_compile_unit.
245bool DIDescriptor::isCompileUnit() const {
Devang Patel3c91b052010-03-08 20:52:55 +0000246 return DbgNode && getTag() == dwarf::DW_TAG_compile_unit;
Devang Patelc9f322d2009-08-31 21:34:44 +0000247}
248
Devang Patel7aa81892010-03-08 22:27:22 +0000249/// isFile - Return true if the specified tag is DW_TAG_file_type.
250bool DIDescriptor::isFile() const {
251 return DbgNode && getTag() == dwarf::DW_TAG_file_type;
252}
253
Devang Patel6404e4e2009-12-15 19:16:48 +0000254/// isNameSpace - Return true if the specified tag is DW_TAG_namespace.
255bool DIDescriptor::isNameSpace() const {
Devang Patel3c91b052010-03-08 20:52:55 +0000256 return DbgNode && getTag() == dwarf::DW_TAG_namespace;
Devang Patel6404e4e2009-12-15 19:16:48 +0000257}
258
Devang Patel5e005d82009-08-31 22:00:15 +0000259/// isLexicalBlock - Return true if the specified tag is DW_TAG_lexical_block.
260bool DIDescriptor::isLexicalBlock() const {
Devang Patel3c91b052010-03-08 20:52:55 +0000261 return DbgNode && getTag() == dwarf::DW_TAG_lexical_block;
Devang Patel5e005d82009-08-31 22:00:15 +0000262}
263
Devang Patelecbeb1a2009-09-30 22:34:41 +0000264/// isSubrange - Return true if the specified tag is DW_TAG_subrange_type.
265bool DIDescriptor::isSubrange() const {
Devang Patel3c91b052010-03-08 20:52:55 +0000266 return DbgNode && getTag() == dwarf::DW_TAG_subrange_type;
Devang Patelecbeb1a2009-09-30 22:34:41 +0000267}
268
269/// isEnumerator - Return true if the specified tag is DW_TAG_enumerator.
270bool DIDescriptor::isEnumerator() const {
Devang Patel3c91b052010-03-08 20:52:55 +0000271 return DbgNode && getTag() == dwarf::DW_TAG_enumerator;
Devang Patelecbeb1a2009-09-30 22:34:41 +0000272}
273
Devang Patel6ceea332009-08-31 18:49:10 +0000274//===----------------------------------------------------------------------===//
275// Simple Descriptor Constructors and other Methods
276//===----------------------------------------------------------------------===//
277
Devang Patele9f8f5e2010-05-07 20:54:48 +0000278DIType::DIType(const MDNode *N) : DIScope(N) {
Devang Patel6ceea332009-08-31 18:49:10 +0000279 if (!N) return;
280 if (!isBasicType() && !isDerivedType() && !isCompositeType()) {
281 DbgNode = 0;
282 }
283}
284
Devang Patel68afdc32009-01-05 18:33:01 +0000285unsigned DIArray::getNumElements() const {
Devang Patel3c91b052010-03-08 20:52:55 +0000286 if (!DbgNode)
287 return 0;
Chris Lattner5d0cacd2009-12-31 01:22:29 +0000288 return DbgNode->getNumOperands();
Devang Patel68afdc32009-01-05 18:33:01 +0000289}
Chris Lattnera45664f2008-11-10 02:56:27 +0000290
Devang Patelc4999d72009-07-22 18:23:44 +0000291/// replaceAllUsesWith - Replace all uses of debug info referenced by
Dan Gohman872814a2010-07-21 18:54:18 +0000292/// this descriptor.
Dan Gohman489b29b2010-08-20 22:02:26 +0000293void DIType::replaceAllUsesWith(DIDescriptor &D) {
Devang Patel3c91b052010-03-08 20:52:55 +0000294 if (!DbgNode)
Devang Patelc4999d72009-07-22 18:23:44 +0000295 return;
296
Daniel Dunbar48a097b2009-09-22 02:03:18 +0000297 // Since we use a TrackingVH for the node, its easy for clients to manufacture
298 // legitimate situations where they want to replaceAllUsesWith() on something
299 // which, due to uniquing, has merged with the source. We shield clients from
300 // this detail by allowing a value to be replaced with replaceAllUsesWith()
301 // itself.
Devang Pateled66bf52010-05-07 18:19:32 +0000302 if (DbgNode != D) {
Devang Patele9f8f5e2010-05-07 20:54:48 +0000303 MDNode *Node = const_cast<MDNode*>(DbgNode);
304 const MDNode *DN = D;
305 const Value *V = cast_or_null<Value>(DN);
306 Node->replaceAllUsesWith(const_cast<Value*>(V));
Dan Gohman489b29b2010-08-20 22:02:26 +0000307 MDNode::deleteTemporary(Node);
Daniel Dunbar48a097b2009-09-22 02:03:18 +0000308 }
Devang Patelc4999d72009-07-22 18:23:44 +0000309}
310
Devang Patel0a2551d2010-12-08 20:18:20 +0000311/// replaceAllUsesWith - Replace all uses of debug info referenced by
312/// this descriptor.
313void DIType::replaceAllUsesWith(MDNode *D) {
314 if (!DbgNode)
315 return;
316
317 // Since we use a TrackingVH for the node, its easy for clients to manufacture
318 // legitimate situations where they want to replaceAllUsesWith() on something
319 // which, due to uniquing, has merged with the source. We shield clients from
320 // this detail by allowing a value to be replaced with replaceAllUsesWith()
321 // itself.
322 if (DbgNode != D) {
323 MDNode *Node = const_cast<MDNode*>(DbgNode);
324 const MDNode *DN = D;
325 const Value *V = cast_or_null<Value>(DN);
326 Node->replaceAllUsesWith(const_cast<Value*>(V));
327 MDNode::deleteTemporary(Node);
328 }
329}
330
Devang Patel6f9d8ff2011-08-15 17:57:41 +0000331/// isUnsignedDIType - Return true if type encoding is unsigned.
332bool DIType::isUnsignedDIType() {
333 DIDerivedType DTy(DbgNode);
334 if (DTy.Verify())
335 return DTy.getTypeDerivedFrom().isUnsignedDIType();
336
337 DIBasicType BTy(DbgNode);
338 if (BTy.Verify()) {
339 unsigned Encoding = BTy.getEncoding();
340 if (Encoding == dwarf::DW_ATE_unsigned ||
341 Encoding == dwarf::DW_ATE_unsigned_char)
342 return true;
343 }
344 return false;
345}
346
Devang Patelb79b5352009-01-19 23:21:49 +0000347/// Verify - Verify that a compile unit is well formed.
348bool DICompileUnit::Verify() const {
Devang Patel3c91b052010-03-08 20:52:55 +0000349 if (!DbgNode)
Devang Patelb79b5352009-01-19 23:21:49 +0000350 return false;
Devang Patel65dbc902009-11-25 17:36:49 +0000351 StringRef N = getFilename();
352 if (N.empty())
Bill Wendling0582ae92009-03-13 04:39:26 +0000353 return false;
Devang Patelb79b5352009-01-19 23:21:49 +0000354 // It is possible that directory and produce string is empty.
Bill Wendling0582ae92009-03-13 04:39:26 +0000355 return true;
Devang Patelb79b5352009-01-19 23:21:49 +0000356}
357
358/// Verify - Verify that a type descriptor is well formed.
359bool DIType::Verify() const {
Devang Patel3c91b052010-03-08 20:52:55 +0000360 if (!DbgNode)
Devang Patelb79b5352009-01-19 23:21:49 +0000361 return false;
Devang Patel94c7ddb2011-08-16 22:09:43 +0000362 if (getContext() && !getContext().Verify())
Devang Patelb79b5352009-01-19 23:21:49 +0000363 return false;
Devang Patelb71bbf92010-11-02 20:41:13 +0000364 unsigned Tag = getTag();
365 if (!isBasicType() && Tag != dwarf::DW_TAG_const_type &&
366 Tag != dwarf::DW_TAG_volatile_type && Tag != dwarf::DW_TAG_pointer_type &&
Devang Patelfe58f952010-12-07 23:25:47 +0000367 Tag != dwarf::DW_TAG_reference_type && Tag != dwarf::DW_TAG_restrict_type
Devang Patel43c249c2010-12-08 01:50:15 +0000368 && Tag != dwarf::DW_TAG_vector_type && Tag != dwarf::DW_TAG_array_type
369 && Tag != dwarf::DW_TAG_enumeration_type
Devang Patelfe58f952010-12-07 23:25:47 +0000370 && getFilename().empty())
Devang Patelb79b5352009-01-19 23:21:49 +0000371 return false;
372 return true;
373}
374
Devang Patel0c4720c2010-08-23 18:25:56 +0000375/// Verify - Verify that a basic type descriptor is well formed.
376bool DIBasicType::Verify() const {
377 return isBasicType();
378}
379
380/// Verify - Verify that a derived type descriptor is well formed.
381bool DIDerivedType::Verify() const {
382 return isDerivedType();
383}
384
Devang Patelb79b5352009-01-19 23:21:49 +0000385/// Verify - Verify that a composite type descriptor is well formed.
386bool DICompositeType::Verify() const {
Devang Patel3c91b052010-03-08 20:52:55 +0000387 if (!DbgNode)
Devang Patelb79b5352009-01-19 23:21:49 +0000388 return false;
Devang Patel94c7ddb2011-08-16 22:09:43 +0000389 if (getContext() && !getContext().Verify())
Devang Patelb79b5352009-01-19 23:21:49 +0000390 return false;
391
Devang Patelb79b5352009-01-19 23:21:49 +0000392 return true;
393}
394
395/// Verify - Verify that a subprogram descriptor is well formed.
396bool DISubprogram::Verify() const {
Devang Patel3c91b052010-03-08 20:52:55 +0000397 if (!DbgNode)
Devang Patelb79b5352009-01-19 23:21:49 +0000398 return false;
Daniel Dunbarf612ff62009-09-19 20:40:05 +0000399
Devang Patel94c7ddb2011-08-16 22:09:43 +0000400 if (getContext() && !getContext().Verify())
Devang Patelb79b5352009-01-19 23:21:49 +0000401 return false;
402
403 DICompositeType Ty = getType();
Devang Patel3c91b052010-03-08 20:52:55 +0000404 if (!Ty.Verify())
Devang Patelb79b5352009-01-19 23:21:49 +0000405 return false;
406 return true;
407}
408
409/// Verify - Verify that a global variable descriptor is well formed.
410bool DIGlobalVariable::Verify() const {
Devang Patel3c91b052010-03-08 20:52:55 +0000411 if (!DbgNode)
Devang Patelb79b5352009-01-19 23:21:49 +0000412 return false;
Daniel Dunbarf612ff62009-09-19 20:40:05 +0000413
Devang Patel65dbc902009-11-25 17:36:49 +0000414 if (getDisplayName().empty())
Devang Patel84c73e92009-11-06 17:58:12 +0000415 return false;
416
Devang Patel94c7ddb2011-08-16 22:09:43 +0000417 if (getContext() && !getContext().Verify())
Devang Patelb79b5352009-01-19 23:21:49 +0000418 return false;
419
420 DIType Ty = getType();
421 if (!Ty.Verify())
422 return false;
423
Devang Patel27398962010-08-09 21:39:24 +0000424 if (!getGlobal() && !getConstant())
Devang Patelb79b5352009-01-19 23:21:49 +0000425 return false;
426
427 return true;
428}
429
430/// Verify - Verify that a variable descriptor is well formed.
431bool DIVariable::Verify() const {
Devang Patel3c91b052010-03-08 20:52:55 +0000432 if (!DbgNode)
Devang Patelb79b5352009-01-19 23:21:49 +0000433 return false;
Daniel Dunbarf612ff62009-09-19 20:40:05 +0000434
Devang Patel94c7ddb2011-08-16 22:09:43 +0000435 if (getContext() && !getContext().Verify())
Devang Patel62077af2010-05-07 21:42:24 +0000436 return false;
437
Devang Patelb79b5352009-01-19 23:21:49 +0000438 DIType Ty = getType();
439 if (!Ty.Verify())
440 return false;
441
Devang Patelb79b5352009-01-19 23:21:49 +0000442 return true;
443}
444
Devang Patel3c91b052010-03-08 20:52:55 +0000445/// Verify - Verify that a location descriptor is well formed.
446bool DILocation::Verify() const {
447 if (!DbgNode)
448 return false;
Jim Grosbache62b6902010-07-21 21:36:25 +0000449
Devang Patel3c91b052010-03-08 20:52:55 +0000450 return DbgNode->getNumOperands() == 4;
451}
452
Devang Patel47e22652010-05-07 23:04:32 +0000453/// Verify - Verify that a namespace descriptor is well formed.
454bool DINameSpace::Verify() const {
455 if (!DbgNode)
456 return false;
457 if (getName().empty())
458 return false;
Devang Patel47e22652010-05-07 23:04:32 +0000459 return true;
460}
461
Devang Patel36375ee2009-02-17 21:23:59 +0000462/// getOriginalTypeSize - If this type is derived from a base type then
463/// return base type size.
464uint64_t DIDerivedType::getOriginalTypeSize() const {
Devang Patel61ecbd12009-11-04 23:48:00 +0000465 unsigned Tag = getTag();
466 if (Tag == dwarf::DW_TAG_member || Tag == dwarf::DW_TAG_typedef ||
467 Tag == dwarf::DW_TAG_const_type || Tag == dwarf::DW_TAG_volatile_type ||
468 Tag == dwarf::DW_TAG_restrict_type) {
469 DIType BaseType = getTypeDerivedFrom();
Jim Grosbache62b6902010-07-21 21:36:25 +0000470 // If this type is not derived from any type then take conservative
Devang Patel5ebfa2d2009-11-06 18:24:05 +0000471 // approach.
Devang Patel3c91b052010-03-08 20:52:55 +0000472 if (!BaseType.isValid())
Devang Patel5ebfa2d2009-11-06 18:24:05 +0000473 return getSizeInBits();
Devang Patel61ecbd12009-11-04 23:48:00 +0000474 if (BaseType.isDerivedType())
Devang Patel2db49d72010-05-07 18:11:54 +0000475 return DIDerivedType(BaseType).getOriginalTypeSize();
Devang Patel61ecbd12009-11-04 23:48:00 +0000476 else
477 return BaseType.getSizeInBits();
478 }
Jim Grosbache62b6902010-07-21 21:36:25 +0000479
Devang Patel61ecbd12009-11-04 23:48:00 +0000480 return getSizeInBits();
Devang Patel36375ee2009-02-17 21:23:59 +0000481}
Devang Patelb79b5352009-01-19 23:21:49 +0000482
Jim Grosbache62b6902010-07-21 21:36:25 +0000483/// isInlinedFnArgument - Return true if this variable provides debugging
Devang Patel719f6a92010-04-29 20:40:36 +0000484/// information for an inlined function arguments.
485bool DIVariable::isInlinedFnArgument(const Function *CurFn) {
486 assert(CurFn && "Invalid function");
487 if (!getContext().isSubprogram())
488 return false;
Jim Grosbache62b6902010-07-21 21:36:25 +0000489 // This variable is not inlined function argument if its scope
Devang Patel719f6a92010-04-29 20:40:36 +0000490 // does not describe current function.
Devang Patel2db49d72010-05-07 18:11:54 +0000491 return !(DISubprogram(getContext()).describes(CurFn));
Devang Patel719f6a92010-04-29 20:40:36 +0000492}
493
Devang Patelaf5b6bb2009-04-15 00:06:07 +0000494/// describes - Return true if this subprogram provides debugging
495/// information for the function F.
496bool DISubprogram::describes(const Function *F) {
Chris Lattner099b7792009-12-29 09:22:47 +0000497 assert(F && "Invalid function");
Devang Patelffd33cd2010-06-16 06:42:02 +0000498 if (F == getFunction())
499 return true;
Devang Patel65dbc902009-11-25 17:36:49 +0000500 StringRef Name = getLinkageName();
501 if (Name.empty())
Devang Patel5ccdd102009-09-29 18:40:58 +0000502 Name = getName();
Devang Patel65dbc902009-11-25 17:36:49 +0000503 if (F->getName() == Name)
Devang Patelaf5b6bb2009-04-15 00:06:07 +0000504 return true;
505 return false;
506}
507
Jim Grosbache62b6902010-07-21 21:36:25 +0000508unsigned DISubprogram::isOptimized() const {
Devang Patelccff8122010-04-30 19:38:23 +0000509 assert (DbgNode && "Invalid subprogram descriptor!");
510 if (DbgNode->getNumOperands() == 16)
511 return getUnsignedField(15);
512 return 0;
513}
514
Devang Patel93d39be2011-08-19 23:28:12 +0000515MDNode *DISubprogram::getVariablesNodes() const {
516 if (!DbgNode || DbgNode->getNumOperands() <= 19)
517 return NULL;
518 if (MDNode *Temp = dyn_cast_or_null<MDNode>(DbgNode->getOperand(19)))
519 return dyn_cast_or_null<MDNode>(Temp->getOperand(0));
520 return NULL;
521}
522
523DIArray DISubprogram::getVariables() const {
524 if (!DbgNode || DbgNode->getNumOperands() <= 19)
525 return DIArray();
526 if (MDNode *T = dyn_cast_or_null<MDNode>(DbgNode->getOperand(19)))
527 if (MDNode *A = dyn_cast_or_null<MDNode>(T->getOperand(0)))
528 return DIArray(A);
529 return DIArray();
530}
531
Devang Patel65dbc902009-11-25 17:36:49 +0000532StringRef DIScope::getFilename() const {
Devang Patel77bf2952010-03-08 22:02:50 +0000533 if (!DbgNode)
534 return StringRef();
Jim Grosbache62b6902010-07-21 21:36:25 +0000535 if (isLexicalBlock())
Devang Patelecbeb1a2009-09-30 22:34:41 +0000536 return DILexicalBlock(DbgNode).getFilename();
Chris Lattner099b7792009-12-29 09:22:47 +0000537 if (isSubprogram())
Devang Patelecbeb1a2009-09-30 22:34:41 +0000538 return DISubprogram(DbgNode).getFilename();
Chris Lattner099b7792009-12-29 09:22:47 +0000539 if (isCompileUnit())
Devang Patelecbeb1a2009-09-30 22:34:41 +0000540 return DICompileUnit(DbgNode).getFilename();
Chris Lattner099b7792009-12-29 09:22:47 +0000541 if (isNameSpace())
Devang Patel6404e4e2009-12-15 19:16:48 +0000542 return DINameSpace(DbgNode).getFilename();
Devang Patel77bf2952010-03-08 22:02:50 +0000543 if (isType())
544 return DIType(DbgNode).getFilename();
Devang Patel7aa81892010-03-08 22:27:22 +0000545 if (isFile())
546 return DIFile(DbgNode).getFilename();
Chris Lattner099b7792009-12-29 09:22:47 +0000547 assert(0 && "Invalid DIScope!");
Devang Patel65dbc902009-11-25 17:36:49 +0000548 return StringRef();
Devang Patelecbeb1a2009-09-30 22:34:41 +0000549}
550
Devang Patel65dbc902009-11-25 17:36:49 +0000551StringRef DIScope::getDirectory() const {
Devang Patel77bf2952010-03-08 22:02:50 +0000552 if (!DbgNode)
553 return StringRef();
Jim Grosbache62b6902010-07-21 21:36:25 +0000554 if (isLexicalBlock())
Devang Patelecbeb1a2009-09-30 22:34:41 +0000555 return DILexicalBlock(DbgNode).getDirectory();
Chris Lattner099b7792009-12-29 09:22:47 +0000556 if (isSubprogram())
Devang Patelecbeb1a2009-09-30 22:34:41 +0000557 return DISubprogram(DbgNode).getDirectory();
Chris Lattner099b7792009-12-29 09:22:47 +0000558 if (isCompileUnit())
Devang Patelecbeb1a2009-09-30 22:34:41 +0000559 return DICompileUnit(DbgNode).getDirectory();
Chris Lattner099b7792009-12-29 09:22:47 +0000560 if (isNameSpace())
Devang Patel6404e4e2009-12-15 19:16:48 +0000561 return DINameSpace(DbgNode).getDirectory();
Devang Patel77bf2952010-03-08 22:02:50 +0000562 if (isType())
563 return DIType(DbgNode).getDirectory();
Devang Patel7aa81892010-03-08 22:27:22 +0000564 if (isFile())
565 return DIFile(DbgNode).getDirectory();
Chris Lattner099b7792009-12-29 09:22:47 +0000566 assert(0 && "Invalid DIScope!");
Devang Patel65dbc902009-11-25 17:36:49 +0000567 return StringRef();
Devang Patelecbeb1a2009-09-30 22:34:41 +0000568}
569
Devang Patel94c7ddb2011-08-16 22:09:43 +0000570DIArray DICompileUnit::getEnumTypes() const {
571 if (!DbgNode || DbgNode->getNumOperands() < 14)
572 return DIArray();
573
574 if (MDNode *N = dyn_cast_or_null<MDNode>(DbgNode->getOperand(10)))
575 if (MDNode *A = dyn_cast_or_null<MDNode>(N->getOperand(0)))
576 return DIArray(A);
577 return DIArray();
578}
579
580DIArray DICompileUnit::getRetainedTypes() const {
581 if (!DbgNode || DbgNode->getNumOperands() < 14)
582 return DIArray();
583
584 if (MDNode *N = dyn_cast_or_null<MDNode>(DbgNode->getOperand(11)))
585 if (MDNode *A = dyn_cast_or_null<MDNode>(N->getOperand(0)))
586 return DIArray(A);
587 return DIArray();
588}
589
590DIArray DICompileUnit::getSubprograms() const {
591 if (!DbgNode || DbgNode->getNumOperands() < 14)
592 return DIArray();
593
594 if (MDNode *N = dyn_cast_or_null<MDNode>(DbgNode->getOperand(12)))
595 if (MDNode *A = dyn_cast_or_null<MDNode>(N->getOperand(0)))
596 return DIArray(A);
597 return DIArray();
598}
599
600
601DIArray DICompileUnit::getGlobalVariables() const {
602 if (!DbgNode || DbgNode->getNumOperands() < 14)
603 return DIArray();
604
605 if (MDNode *N = dyn_cast_or_null<MDNode>(DbgNode->getOperand(13)))
606 if (MDNode *A = dyn_cast_or_null<MDNode>(N->getOperand(0)))
607 return DIArray(A);
608 return DIArray();
609}
610
Chris Lattnera45664f2008-11-10 02:56:27 +0000611//===----------------------------------------------------------------------===//
Devang Patel7136a652009-07-01 22:10:23 +0000612// DIDescriptor: dump routines for all descriptors.
613//===----------------------------------------------------------------------===//
614
615
Dan Gohman50404362010-05-07 15:30:29 +0000616/// print - Print descriptor.
617void DIDescriptor::print(raw_ostream &OS) const {
618 OS << "[" << dwarf::TagString(getTag()) << "] ";
619 OS.write_hex((intptr_t) &*DbgNode) << ']';
Devang Patel7136a652009-07-01 22:10:23 +0000620}
621
Dan Gohman50404362010-05-07 15:30:29 +0000622/// print - Print compile unit.
623void DICompileUnit::print(raw_ostream &OS) const {
Devang Patel7136a652009-07-01 22:10:23 +0000624 if (getLanguage())
Dan Gohman50404362010-05-07 15:30:29 +0000625 OS << " [" << dwarf::LanguageString(getLanguage()) << "] ";
Devang Patel7136a652009-07-01 22:10:23 +0000626
Dan Gohmanfaa19c32010-05-10 20:07:44 +0000627 OS << " [" << getDirectory() << "/" << getFilename() << "]";
Devang Patel7136a652009-07-01 22:10:23 +0000628}
629
Dan Gohman50404362010-05-07 15:30:29 +0000630/// print - Print type.
631void DIType::print(raw_ostream &OS) const {
Devang Patel3c91b052010-03-08 20:52:55 +0000632 if (!DbgNode) return;
Devang Patel7136a652009-07-01 22:10:23 +0000633
Devang Patel65dbc902009-11-25 17:36:49 +0000634 StringRef Res = getName();
635 if (!Res.empty())
Dan Gohman50404362010-05-07 15:30:29 +0000636 OS << " [" << Res << "] ";
Devang Patel7136a652009-07-01 22:10:23 +0000637
638 unsigned Tag = getTag();
Dan Gohman50404362010-05-07 15:30:29 +0000639 OS << " [" << dwarf::TagString(Tag) << "] ";
Devang Patel7136a652009-07-01 22:10:23 +0000640
641 // TODO : Print context
Dan Gohman50404362010-05-07 15:30:29 +0000642 OS << " ["
Dan Gohman9a7063e2010-05-07 16:39:27 +0000643 << "line " << getLineNumber() << ", "
644 << getSizeInBits() << " bits, "
645 << getAlignInBits() << " bit alignment, "
646 << getOffsetInBits() << " bit offset"
Chris Lattnera81d29b2009-08-23 07:33:14 +0000647 << "] ";
Devang Patel7136a652009-07-01 22:10:23 +0000648
Daniel Dunbarf612ff62009-09-19 20:40:05 +0000649 if (isPrivate())
Dan Gohman50404362010-05-07 15:30:29 +0000650 OS << " [private] ";
Devang Patel7136a652009-07-01 22:10:23 +0000651 else if (isProtected())
Dan Gohman50404362010-05-07 15:30:29 +0000652 OS << " [protected] ";
Devang Patel7136a652009-07-01 22:10:23 +0000653
654 if (isForwardDecl())
Dan Gohman50404362010-05-07 15:30:29 +0000655 OS << " [fwd] ";
Devang Patel7136a652009-07-01 22:10:23 +0000656
Devang Patel6ceea332009-08-31 18:49:10 +0000657 if (isBasicType())
Dan Gohmanc014d092010-05-07 16:17:22 +0000658 DIBasicType(DbgNode).print(OS);
Devang Patel6ceea332009-08-31 18:49:10 +0000659 else if (isDerivedType())
Dan Gohmanc014d092010-05-07 16:17:22 +0000660 DIDerivedType(DbgNode).print(OS);
Devang Patel6ceea332009-08-31 18:49:10 +0000661 else if (isCompositeType())
Dan Gohmanc014d092010-05-07 16:17:22 +0000662 DICompositeType(DbgNode).print(OS);
Devang Patel7136a652009-07-01 22:10:23 +0000663 else {
Dan Gohman50404362010-05-07 15:30:29 +0000664 OS << "Invalid DIType\n";
Devang Patel7136a652009-07-01 22:10:23 +0000665 return;
666 }
667
Dan Gohman50404362010-05-07 15:30:29 +0000668 OS << "\n";
Devang Patel7136a652009-07-01 22:10:23 +0000669}
670
Dan Gohman50404362010-05-07 15:30:29 +0000671/// print - Print basic type.
672void DIBasicType::print(raw_ostream &OS) const {
673 OS << " [" << dwarf::AttributeEncodingString(getEncoding()) << "] ";
Devang Patel7136a652009-07-01 22:10:23 +0000674}
675
Dan Gohman50404362010-05-07 15:30:29 +0000676/// print - Print derived type.
677void DIDerivedType::print(raw_ostream &OS) const {
Dan Gohmanc014d092010-05-07 16:17:22 +0000678 OS << "\n\t Derived From: "; getTypeDerivedFrom().print(OS);
Devang Patel7136a652009-07-01 22:10:23 +0000679}
680
Dan Gohman50404362010-05-07 15:30:29 +0000681/// print - Print composite type.
682void DICompositeType::print(raw_ostream &OS) const {
Devang Patel7136a652009-07-01 22:10:23 +0000683 DIArray A = getTypeArray();
Dan Gohman50404362010-05-07 15:30:29 +0000684 OS << " [" << A.getNumElements() << " elements]";
Devang Patel7136a652009-07-01 22:10:23 +0000685}
686
Dan Gohman50404362010-05-07 15:30:29 +0000687/// print - Print subprogram.
688void DISubprogram::print(raw_ostream &OS) const {
Devang Patel65dbc902009-11-25 17:36:49 +0000689 StringRef Res = getName();
690 if (!Res.empty())
Dan Gohman50404362010-05-07 15:30:29 +0000691 OS << " [" << Res << "] ";
Devang Patel82dfc0c2009-08-31 22:47:13 +0000692
693 unsigned Tag = getTag();
Dan Gohman50404362010-05-07 15:30:29 +0000694 OS << " [" << dwarf::TagString(Tag) << "] ";
Devang Patel82dfc0c2009-08-31 22:47:13 +0000695
696 // TODO : Print context
Dan Gohman50404362010-05-07 15:30:29 +0000697 OS << " [" << getLineNumber() << "] ";
Devang Patel82dfc0c2009-08-31 22:47:13 +0000698
699 if (isLocalToUnit())
Dan Gohman50404362010-05-07 15:30:29 +0000700 OS << " [local] ";
Devang Patel82dfc0c2009-08-31 22:47:13 +0000701
702 if (isDefinition())
Dan Gohman50404362010-05-07 15:30:29 +0000703 OS << " [def] ";
Devang Patel82dfc0c2009-08-31 22:47:13 +0000704
Dan Gohman50404362010-05-07 15:30:29 +0000705 OS << "\n";
706}
707
708/// print - Print global variable.
709void DIGlobalVariable::print(raw_ostream &OS) const {
710 OS << " [";
Devang Patela49d8772010-05-07 23:19:07 +0000711 StringRef Res = getName();
712 if (!Res.empty())
713 OS << " [" << Res << "] ";
714
715 unsigned Tag = getTag();
716 OS << " [" << dwarf::TagString(Tag) << "] ";
717
718 // TODO : Print context
Devang Patela49d8772010-05-07 23:19:07 +0000719 OS << " [" << getLineNumber() << "] ";
720
721 if (isLocalToUnit())
722 OS << " [local] ";
723
724 if (isDefinition())
725 OS << " [def] ";
726
727 if (isGlobalVariable())
728 DIGlobalVariable(DbgNode).print(OS);
729 OS << "]\n";
Dan Gohman50404362010-05-07 15:30:29 +0000730}
731
Devang Patel48d726f2011-08-09 01:03:14 +0000732static void printDebugLoc(DebugLoc DL, raw_ostream &CommentOS,
733 const LLVMContext &Ctx) {
734 if (!DL.isUnknown()) { // Print source line info.
735 DIScope Scope(DL.getScope(Ctx));
736 // Omit the directory, because it's likely to be long and uninteresting.
737 if (Scope.Verify())
738 CommentOS << Scope.getFilename();
739 else
740 CommentOS << "<unknown>";
741 CommentOS << ':' << DL.getLine();
742 if (DL.getCol() != 0)
743 CommentOS << ':' << DL.getCol();
744 DebugLoc InlinedAtDL = DebugLoc::getFromDILocation(DL.getInlinedAt(Ctx));
745 if (!InlinedAtDL.isUnknown()) {
746 CommentOS << " @[ ";
747 printDebugLoc(InlinedAtDL, CommentOS, Ctx);
748 CommentOS << " ]";
749 }
750 }
751}
752
753void DIVariable::printExtendedName(raw_ostream &OS) const {
754 const LLVMContext &Ctx = DbgNode->getContext();
755 StringRef Res = getName();
756 if (!Res.empty())
757 OS << Res << "," << getLineNumber();
758 if (MDNode *InlinedAt = getInlinedAt()) {
759 DebugLoc InlinedAtDL = DebugLoc::getFromDILocation(InlinedAt);
760 if (!InlinedAtDL.isUnknown()) {
761 OS << " @[";
762 printDebugLoc(InlinedAtDL, OS, Ctx);
763 OS << "]";
764 }
765 }
766}
767
Dan Gohman50404362010-05-07 15:30:29 +0000768/// print - Print variable.
769void DIVariable::print(raw_ostream &OS) const {
770 StringRef Res = getName();
771 if (!Res.empty())
772 OS << " [" << Res << "] ";
773
Dan Gohman50404362010-05-07 15:30:29 +0000774 OS << " [" << getLineNumber() << "] ";
Dan Gohmanc014d092010-05-07 16:17:22 +0000775 getType().print(OS);
Dan Gohman50404362010-05-07 15:30:29 +0000776 OS << "\n";
777
778 // FIXME: Dump complex addresses
779}
780
781/// dump - Print descriptor to dbgs() with a newline.
782void DIDescriptor::dump() const {
783 print(dbgs()); dbgs() << '\n';
784}
785
786/// dump - Print compile unit to dbgs() with a newline.
787void DICompileUnit::dump() const {
788 print(dbgs()); dbgs() << '\n';
789}
790
791/// dump - Print type to dbgs() with a newline.
792void DIType::dump() const {
793 print(dbgs()); dbgs() << '\n';
794}
795
796/// dump - Print basic type to dbgs() with a newline.
797void DIBasicType::dump() const {
798 print(dbgs()); dbgs() << '\n';
799}
800
801/// dump - Print derived type to dbgs() with a newline.
802void DIDerivedType::dump() const {
803 print(dbgs()); dbgs() << '\n';
804}
805
806/// dump - Print composite type to dbgs() with a newline.
807void DICompositeType::dump() const {
808 print(dbgs()); dbgs() << '\n';
809}
810
Dan Gohman50404362010-05-07 15:30:29 +0000811/// dump - Print subprogram to dbgs() with a newline.
812void DISubprogram::dump() const {
813 print(dbgs()); dbgs() << '\n';
Devang Patel7136a652009-07-01 22:10:23 +0000814}
815
816/// dump - Print global variable.
817void DIGlobalVariable::dump() const {
Dan Gohman50404362010-05-07 15:30:29 +0000818 print(dbgs()); dbgs() << '\n';
Devang Patel7136a652009-07-01 22:10:23 +0000819}
820
821/// dump - Print variable.
822void DIVariable::dump() const {
Dan Gohman50404362010-05-07 15:30:29 +0000823 print(dbgs()); dbgs() << '\n';
Devang Patel7136a652009-07-01 22:10:23 +0000824}
825
Devang Patel62367042010-11-10 22:19:21 +0000826/// fixupObjcLikeName - Replace contains special characters used
827/// in a typical Objective-C names with '.' in a given string.
Benjamin Kramer1a81d482011-06-18 14:42:42 +0000828static void fixupObjcLikeName(StringRef Str, SmallVectorImpl<char> &Out) {
829 bool isObjCLike = false;
Devang Patel62367042010-11-10 22:19:21 +0000830 for (size_t i = 0, e = Str.size(); i < e; ++i) {
831 char C = Str[i];
Benjamin Kramer1a81d482011-06-18 14:42:42 +0000832 if (C == '[')
833 isObjCLike = true;
834
835 if (isObjCLike && (C == '[' || C == ']' || C == ' ' || C == ':' ||
836 C == '+' || C == '(' || C == ')'))
837 Out.push_back('.');
838 else
839 Out.push_back(C);
Devang Patel62367042010-11-10 22:19:21 +0000840 }
841}
842
Devang Patel62367042010-11-10 22:19:21 +0000843/// getFnSpecificMDNode - Return a NameMDNode, if available, that is
844/// suitable to hold function specific information.
Devang Patel93d39be2011-08-19 23:28:12 +0000845NamedMDNode *llvm::getFnSpecificMDNode(const Module &M, DISubprogram Fn) {
Benjamin Kramer1a81d482011-06-18 14:42:42 +0000846 SmallString<32> Name = StringRef("llvm.dbg.lv.");
Devang Patel93d39be2011-08-19 23:28:12 +0000847 StringRef FName = "fn";
848 if (Fn.getFunction())
849 FName = Fn.getFunction()->getName();
850 else
851 FName = Fn.getName();
852 char One = '\1';
853 if (FName.startswith(StringRef(&One, 1)))
854 FName = FName.substr(1);
855 fixupObjcLikeName(FName, Name);
Benjamin Kramer1a81d482011-06-18 14:42:42 +0000856 return M.getNamedMetadata(Name.str());
Devang Patel62367042010-11-10 22:19:21 +0000857}
858
Duncan Sands291bb702011-03-02 20:30:37 +0000859/// getOrInsertFnSpecificMDNode - Return a NameMDNode that is suitable
860/// to hold function specific information.
Devang Patel93d39be2011-08-19 23:28:12 +0000861NamedMDNode *llvm::getOrInsertFnSpecificMDNode(Module &M, DISubprogram Fn) {
Benjamin Kramer1a81d482011-06-18 14:42:42 +0000862 SmallString<32> Name = StringRef("llvm.dbg.lv.");
Devang Patel93d39be2011-08-19 23:28:12 +0000863 StringRef FName = "fn";
864 if (Fn.getFunction())
865 FName = Fn.getFunction()->getName();
866 else
867 FName = Fn.getName();
868 char One = '\1';
869 if (FName.startswith(StringRef(&One, 1)))
870 FName = FName.substr(1);
871 fixupObjcLikeName(FName, Name);
872
Benjamin Kramer1a81d482011-06-18 14:42:42 +0000873 return M.getOrInsertNamedMetadata(Name.str());
Devang Patel1a7ca032010-09-28 18:08:20 +0000874}
875
Devang Patel23336b42011-07-19 19:41:54 +0000876/// createInlinedVariable - Create a new inlined variable based on current
877/// variable.
878/// @param DV Current Variable.
879/// @param InlinedScope Location at current variable is inlined.
880DIVariable llvm::createInlinedVariable(MDNode *DV, MDNode *InlinedScope,
881 LLVMContext &VMContext) {
882 SmallVector<Value *, 16> Elts;
883 // Insert inlined scope as 7th element.
884 for (unsigned i = 0, e = DV->getNumOperands(); i != e; ++i)
885 i == 7 ? Elts.push_back(InlinedScope) :
886 Elts.push_back(DV->getOperand(i));
887 return DIVariable(MDNode::get(VMContext, Elts));
888}
Devang Patel1a7ca032010-09-28 18:08:20 +0000889
Devang Patelb549bcf2011-08-10 21:50:54 +0000890/// cleanseInlinedVariable - Remove inlined scope from the variable.
891DIVariable llvm::cleanseInlinedVariable(MDNode *DV, LLVMContext &VMContext) {
892 SmallVector<Value *, 16> Elts;
893 // Insert inlined scope as 7th element.
894 for (unsigned i = 0, e = DV->getNumOperands(); i != e; ++i)
895 i == 7 ?
896 Elts.push_back(llvm::Constant::getNullValue(Type::getInt32Ty(VMContext))):
897 Elts.push_back(DV->getOperand(i));
898 return DIVariable(MDNode::get(VMContext, Elts));
899}
900
Devang Pateld2f79a12009-07-28 19:55:13 +0000901//===----------------------------------------------------------------------===//
Devang Patel98c65172009-07-30 18:25:15 +0000902// DebugInfoFinder implementations.
Devang Pateld2f79a12009-07-28 19:55:13 +0000903//===----------------------------------------------------------------------===//
904
Devang Patel98c65172009-07-30 18:25:15 +0000905/// processModule - Process entire module and collect debug info.
906void DebugInfoFinder::processModule(Module &M) {
Devang Pateld2f79a12009-07-28 19:55:13 +0000907 for (Module::iterator I = M.begin(), E = M.end(); I != E; ++I)
908 for (Function::iterator FI = (*I).begin(), FE = (*I).end(); FI != FE; ++FI)
909 for (BasicBlock::iterator BI = (*FI).begin(), BE = (*FI).end(); BI != BE;
910 ++BI) {
Devang Patel01c5ff62010-05-04 01:05:02 +0000911 if (DbgDeclareInst *DDI = dyn_cast<DbgDeclareInst>(BI))
Devang Patelb4d31302009-07-31 18:18:52 +0000912 processDeclare(DDI);
Jim Grosbache62b6902010-07-21 21:36:25 +0000913
Chris Lattner28a9bf62010-04-02 20:44:29 +0000914 DebugLoc Loc = BI->getDebugLoc();
915 if (Loc.isUnknown())
916 continue;
Jim Grosbache62b6902010-07-21 21:36:25 +0000917
Chris Lattner28a9bf62010-04-02 20:44:29 +0000918 LLVMContext &Ctx = BI->getContext();
919 DIDescriptor Scope(Loc.getScope(Ctx));
Jim Grosbache62b6902010-07-21 21:36:25 +0000920
Chris Lattner28a9bf62010-04-02 20:44:29 +0000921 if (Scope.isCompileUnit())
Devang Patel2db49d72010-05-07 18:11:54 +0000922 addCompileUnit(DICompileUnit(Scope));
Chris Lattner28a9bf62010-04-02 20:44:29 +0000923 else if (Scope.isSubprogram())
Devang Patel2db49d72010-05-07 18:11:54 +0000924 processSubprogram(DISubprogram(Scope));
Chris Lattner28a9bf62010-04-02 20:44:29 +0000925 else if (Scope.isLexicalBlock())
Devang Patel2db49d72010-05-07 18:11:54 +0000926 processLexicalBlock(DILexicalBlock(Scope));
Jim Grosbache62b6902010-07-21 21:36:25 +0000927
Chris Lattner28a9bf62010-04-02 20:44:29 +0000928 if (MDNode *IA = Loc.getInlinedAt(Ctx))
929 processLocation(DILocation(IA));
Devang Pateld2f79a12009-07-28 19:55:13 +0000930 }
Devang Patele4b27562009-08-28 23:24:31 +0000931
Devang Patelfd5fdc32010-06-28 05:53:08 +0000932 if (NamedMDNode *NMD = M.getNamedMetadata("llvm.dbg.gv")) {
933 for (unsigned i = 0, e = NMD->getNumOperands(); i != e; ++i) {
934 DIGlobalVariable DIG(cast<MDNode>(NMD->getOperand(i)));
935 if (addGlobalVariable(DIG)) {
936 addCompileUnit(DIG.getCompileUnit());
937 processType(DIG.getType());
938 }
Devang Pateld2f79a12009-07-28 19:55:13 +0000939 }
940 }
Devang Patelfd5fdc32010-06-28 05:53:08 +0000941
942 if (NamedMDNode *NMD = M.getNamedMetadata("llvm.dbg.sp"))
943 for (unsigned i = 0, e = NMD->getNumOperands(); i != e; ++i)
944 processSubprogram(DISubprogram(NMD->getOperand(i)));
Devang Pateld2f79a12009-07-28 19:55:13 +0000945}
Daniel Dunbarf612ff62009-09-19 20:40:05 +0000946
Devang Patel6daf99b2009-11-10 22:05:35 +0000947/// processLocation - Process DILocation.
948void DebugInfoFinder::processLocation(DILocation Loc) {
Devang Patel3c91b052010-03-08 20:52:55 +0000949 if (!Loc.Verify()) return;
Devang Patel2db49d72010-05-07 18:11:54 +0000950 DIDescriptor S(Loc.getScope());
Devang Patel6daf99b2009-11-10 22:05:35 +0000951 if (S.isCompileUnit())
Devang Patel2db49d72010-05-07 18:11:54 +0000952 addCompileUnit(DICompileUnit(S));
Devang Patel6daf99b2009-11-10 22:05:35 +0000953 else if (S.isSubprogram())
Devang Patel2db49d72010-05-07 18:11:54 +0000954 processSubprogram(DISubprogram(S));
Devang Patel6daf99b2009-11-10 22:05:35 +0000955 else if (S.isLexicalBlock())
Devang Patel2db49d72010-05-07 18:11:54 +0000956 processLexicalBlock(DILexicalBlock(S));
Devang Patel6daf99b2009-11-10 22:05:35 +0000957 processLocation(Loc.getOrigLocation());
958}
959
Devang Patel98c65172009-07-30 18:25:15 +0000960/// processType - Process DIType.
961void DebugInfoFinder::processType(DIType DT) {
Devang Patel72bcdb62009-08-10 22:09:58 +0000962 if (!addType(DT))
Devang Pateld2f79a12009-07-28 19:55:13 +0000963 return;
964
965 addCompileUnit(DT.getCompileUnit());
Devang Patel6ceea332009-08-31 18:49:10 +0000966 if (DT.isCompositeType()) {
Devang Patel2db49d72010-05-07 18:11:54 +0000967 DICompositeType DCT(DT);
Devang Patel98c65172009-07-30 18:25:15 +0000968 processType(DCT.getTypeDerivedFrom());
Devang Pateld2f79a12009-07-28 19:55:13 +0000969 DIArray DA = DCT.getTypeArray();
Devang Patel3c91b052010-03-08 20:52:55 +0000970 for (unsigned i = 0, e = DA.getNumElements(); i != e; ++i) {
971 DIDescriptor D = DA.getElement(i);
972 if (D.isType())
Devang Patel2db49d72010-05-07 18:11:54 +0000973 processType(DIType(D));
Devang Patel3c91b052010-03-08 20:52:55 +0000974 else if (D.isSubprogram())
Devang Patel2db49d72010-05-07 18:11:54 +0000975 processSubprogram(DISubprogram(D));
Devang Patel3c91b052010-03-08 20:52:55 +0000976 }
Devang Patel6ceea332009-08-31 18:49:10 +0000977 } else if (DT.isDerivedType()) {
Devang Patel2db49d72010-05-07 18:11:54 +0000978 DIDerivedType DDT(DT);
Devang Patel3c91b052010-03-08 20:52:55 +0000979 processType(DDT.getTypeDerivedFrom());
Devang Pateld2f79a12009-07-28 19:55:13 +0000980 }
981}
982
Devang Patelbeab41b2009-10-07 22:04:08 +0000983/// processLexicalBlock
984void DebugInfoFinder::processLexicalBlock(DILexicalBlock LB) {
Devang Patelbeab41b2009-10-07 22:04:08 +0000985 DIScope Context = LB.getContext();
986 if (Context.isLexicalBlock())
Devang Patel2db49d72010-05-07 18:11:54 +0000987 return processLexicalBlock(DILexicalBlock(Context));
Devang Patelbeab41b2009-10-07 22:04:08 +0000988 else
Devang Patel2db49d72010-05-07 18:11:54 +0000989 return processSubprogram(DISubprogram(Context));
Devang Patelbeab41b2009-10-07 22:04:08 +0000990}
991
Devang Patel98c65172009-07-30 18:25:15 +0000992/// processSubprogram - Process DISubprogram.
993void DebugInfoFinder::processSubprogram(DISubprogram SP) {
Devang Pateld2f79a12009-07-28 19:55:13 +0000994 if (!addSubprogram(SP))
995 return;
996 addCompileUnit(SP.getCompileUnit());
Devang Patel98c65172009-07-30 18:25:15 +0000997 processType(SP.getType());
Devang Pateld2f79a12009-07-28 19:55:13 +0000998}
999
Devang Patelb4d31302009-07-31 18:18:52 +00001000/// processDeclare - Process DbgDeclareInst.
1001void DebugInfoFinder::processDeclare(DbgDeclareInst *DDI) {
Devang Patel3c91b052010-03-08 20:52:55 +00001002 MDNode *N = dyn_cast<MDNode>(DDI->getVariable());
1003 if (!N) return;
1004
1005 DIDescriptor DV(N);
1006 if (!DV.isVariable())
Devang Patelb4d31302009-07-31 18:18:52 +00001007 return;
1008
Devang Patel2db49d72010-05-07 18:11:54 +00001009 if (!NodesSeen.insert(DV))
Devang Patelb4d31302009-07-31 18:18:52 +00001010 return;
1011
Devang Patel3c91b052010-03-08 20:52:55 +00001012 addCompileUnit(DIVariable(N).getCompileUnit());
1013 processType(DIVariable(N).getType());
Devang Patelb4d31302009-07-31 18:18:52 +00001014}
1015
Devang Patel72bcdb62009-08-10 22:09:58 +00001016/// addType - Add type into Tys.
1017bool DebugInfoFinder::addType(DIType DT) {
Devang Patel3c91b052010-03-08 20:52:55 +00001018 if (!DT.isValid())
Devang Patel72bcdb62009-08-10 22:09:58 +00001019 return false;
1020
Devang Patel2db49d72010-05-07 18:11:54 +00001021 if (!NodesSeen.insert(DT))
Devang Patel72bcdb62009-08-10 22:09:58 +00001022 return false;
1023
Devang Patel2db49d72010-05-07 18:11:54 +00001024 TYs.push_back(DT);
Devang Patel72bcdb62009-08-10 22:09:58 +00001025 return true;
1026}
1027
Devang Pateld2f79a12009-07-28 19:55:13 +00001028/// addCompileUnit - Add compile unit into CUs.
Devang Patel98c65172009-07-30 18:25:15 +00001029bool DebugInfoFinder::addCompileUnit(DICompileUnit CU) {
Devang Patel3c91b052010-03-08 20:52:55 +00001030 if (!CU.Verify())
Devang Pateld2f79a12009-07-28 19:55:13 +00001031 return false;
1032
Devang Patel2db49d72010-05-07 18:11:54 +00001033 if (!NodesSeen.insert(CU))
Devang Pateld2f79a12009-07-28 19:55:13 +00001034 return false;
1035
Devang Patel2db49d72010-05-07 18:11:54 +00001036 CUs.push_back(CU);
Devang Pateld2f79a12009-07-28 19:55:13 +00001037 return true;
1038}
Daniel Dunbarf612ff62009-09-19 20:40:05 +00001039
Devang Pateld2f79a12009-07-28 19:55:13 +00001040/// addGlobalVariable - Add global variable into GVs.
Devang Patel98c65172009-07-30 18:25:15 +00001041bool DebugInfoFinder::addGlobalVariable(DIGlobalVariable DIG) {
Devang Patel2db49d72010-05-07 18:11:54 +00001042 if (!DIDescriptor(DIG).isGlobalVariable())
Devang Pateld2f79a12009-07-28 19:55:13 +00001043 return false;
1044
Devang Patel2db49d72010-05-07 18:11:54 +00001045 if (!NodesSeen.insert(DIG))
Devang Pateld2f79a12009-07-28 19:55:13 +00001046 return false;
1047
Devang Patel2db49d72010-05-07 18:11:54 +00001048 GVs.push_back(DIG);
Devang Pateld2f79a12009-07-28 19:55:13 +00001049 return true;
1050}
1051
1052// addSubprogram - Add subprgoram into SPs.
Devang Patel98c65172009-07-30 18:25:15 +00001053bool DebugInfoFinder::addSubprogram(DISubprogram SP) {
Devang Patel2db49d72010-05-07 18:11:54 +00001054 if (!DIDescriptor(SP).isSubprogram())
Devang Pateld2f79a12009-07-28 19:55:13 +00001055 return false;
Daniel Dunbarf612ff62009-09-19 20:40:05 +00001056
Devang Patel2db49d72010-05-07 18:11:54 +00001057 if (!NodesSeen.insert(SP))
Devang Pateld2f79a12009-07-28 19:55:13 +00001058 return false;
1059
Devang Patel2db49d72010-05-07 18:11:54 +00001060 SPs.push_back(SP);
Devang Pateld2f79a12009-07-28 19:55:13 +00001061 return true;
1062}
1063
Chris Lattner099b7792009-12-29 09:22:47 +00001064/// getDISubprogram - Find subprogram that is enclosing this scope.
Devang Patele9f8f5e2010-05-07 20:54:48 +00001065DISubprogram llvm::getDISubprogram(const MDNode *Scope) {
Chris Lattner099b7792009-12-29 09:22:47 +00001066 DIDescriptor D(Scope);
Chris Lattner099b7792009-12-29 09:22:47 +00001067 if (D.isSubprogram())
1068 return DISubprogram(Scope);
Jim Grosbache62b6902010-07-21 21:36:25 +00001069
Chris Lattner099b7792009-12-29 09:22:47 +00001070 if (D.isLexicalBlock())
Devang Patel2db49d72010-05-07 18:11:54 +00001071 return getDISubprogram(DILexicalBlock(Scope).getContext());
Jim Grosbache62b6902010-07-21 21:36:25 +00001072
Chris Lattner099b7792009-12-29 09:22:47 +00001073 return DISubprogram();
1074}
Devang Patel193f7202009-11-24 01:14:22 +00001075
Chris Lattner099b7792009-12-29 09:22:47 +00001076/// getDICompositeType - Find underlying composite type.
1077DICompositeType llvm::getDICompositeType(DIType T) {
Chris Lattner099b7792009-12-29 09:22:47 +00001078 if (T.isCompositeType())
Devang Patel2db49d72010-05-07 18:11:54 +00001079 return DICompositeType(T);
Jim Grosbache62b6902010-07-21 21:36:25 +00001080
Chris Lattner099b7792009-12-29 09:22:47 +00001081 if (T.isDerivedType())
Devang Patel2db49d72010-05-07 18:11:54 +00001082 return getDICompositeType(DIDerivedType(T).getTypeDerivedFrom());
Jim Grosbache62b6902010-07-21 21:36:25 +00001083
Chris Lattner099b7792009-12-29 09:22:47 +00001084 return DICompositeType();
Torok Edwin620f2802008-12-16 09:07:36 +00001085}
Devang Patel6f9d8ff2011-08-15 17:57:41 +00001086
1087/// isSubprogramContext - Return true if Context is either a subprogram
1088/// or another context nested inside a subprogram.
1089bool llvm::isSubprogramContext(const MDNode *Context) {
1090 if (!Context)
1091 return false;
1092 DIDescriptor D(Context);
1093 if (D.isSubprogram())
1094 return true;
1095 if (D.isType())
1096 return isSubprogramContext(DIType(Context).getContext());
1097 return false;
1098}
1099