blob: aeb039ce71c19f57c4e1ea66cb8caf7d675b4023 [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 Patel734a67c2011-09-14 23:13:28 +0000133 if (!DbgNode) return false;
134 switch (getTag()) {
135 case dwarf::DW_TAG_base_type:
136 case dwarf::DW_TAG_unspecified_type:
137 return true;
138 default:
139 return false;
140 }
Torok Edwinb07fbd92008-12-13 08:25:29 +0000141}
Chris Lattnera45664f2008-11-10 02:56:27 +0000142
Devang Patel6ceea332009-08-31 18:49:10 +0000143/// isDerivedType - Return true if the specified tag is legal for DIDerivedType.
144bool DIDescriptor::isDerivedType() const {
Devang Patel3c91b052010-03-08 20:52:55 +0000145 if (!DbgNode) return false;
Chris Lattner099b7792009-12-29 09:22:47 +0000146 switch (getTag()) {
Chris Lattnera45664f2008-11-10 02:56:27 +0000147 case dwarf::DW_TAG_typedef:
148 case dwarf::DW_TAG_pointer_type:
149 case dwarf::DW_TAG_reference_type:
150 case dwarf::DW_TAG_const_type:
151 case dwarf::DW_TAG_volatile_type:
152 case dwarf::DW_TAG_restrict_type:
153 case dwarf::DW_TAG_member:
154 case dwarf::DW_TAG_inheritance:
Devang Patel49d96382010-08-23 23:16:25 +0000155 case dwarf::DW_TAG_friend:
Chris Lattnera45664f2008-11-10 02:56:27 +0000156 return true;
157 default:
Devang Patele4b27562009-08-28 23:24:31 +0000158 // CompositeTypes are currently modelled as DerivedTypes.
Devang Patel6ceea332009-08-31 18:49:10 +0000159 return isCompositeType();
Chris Lattnera45664f2008-11-10 02:56:27 +0000160 }
161}
162
Chris Lattnera45664f2008-11-10 02:56:27 +0000163/// isCompositeType - Return true if the specified tag is legal for
164/// DICompositeType.
Devang Patel6ceea332009-08-31 18:49:10 +0000165bool DIDescriptor::isCompositeType() const {
Devang Patel3c91b052010-03-08 20:52:55 +0000166 if (!DbgNode) return false;
Chris Lattner099b7792009-12-29 09:22:47 +0000167 switch (getTag()) {
Chris Lattnera45664f2008-11-10 02:56:27 +0000168 case dwarf::DW_TAG_array_type:
169 case dwarf::DW_TAG_structure_type:
170 case dwarf::DW_TAG_union_type:
171 case dwarf::DW_TAG_enumeration_type:
172 case dwarf::DW_TAG_vector_type:
173 case dwarf::DW_TAG_subroutine_type:
Devang Patel25cb0d72009-03-25 03:52:06 +0000174 case dwarf::DW_TAG_class_type:
Chris Lattnera45664f2008-11-10 02:56:27 +0000175 return true;
176 default:
177 return false;
178 }
179}
180
Chris Lattnera45664f2008-11-10 02:56:27 +0000181/// isVariable - Return true if the specified tag is legal for DIVariable.
Devang Patel6ceea332009-08-31 18:49:10 +0000182bool DIDescriptor::isVariable() const {
Devang Patel3c91b052010-03-08 20:52:55 +0000183 if (!DbgNode) return false;
Chris Lattner099b7792009-12-29 09:22:47 +0000184 switch (getTag()) {
Chris Lattnera45664f2008-11-10 02:56:27 +0000185 case dwarf::DW_TAG_auto_variable:
186 case dwarf::DW_TAG_arg_variable:
187 case dwarf::DW_TAG_return_variable:
188 return true;
189 default:
190 return false;
191 }
192}
193
Devang Patelecbeb1a2009-09-30 22:34:41 +0000194/// isType - Return true if the specified tag is legal for DIType.
195bool DIDescriptor::isType() const {
196 return isBasicType() || isCompositeType() || isDerivedType();
197}
198
Devang Patel6ceea332009-08-31 18:49:10 +0000199/// isSubprogram - Return true if the specified tag is legal for
200/// DISubprogram.
201bool DIDescriptor::isSubprogram() const {
Devang Patel3c91b052010-03-08 20:52:55 +0000202 return DbgNode && getTag() == dwarf::DW_TAG_subprogram;
Devang Patel6ceea332009-08-31 18:49:10 +0000203}
204
205/// isGlobalVariable - Return true if the specified tag is legal for
206/// DIGlobalVariable.
207bool DIDescriptor::isGlobalVariable() const {
Devang Patel29368072010-08-10 07:11:13 +0000208 return DbgNode && (getTag() == dwarf::DW_TAG_variable ||
209 getTag() == dwarf::DW_TAG_constant);
Devang Patel6ceea332009-08-31 18:49:10 +0000210}
211
Devang Patelecbeb1a2009-09-30 22:34:41 +0000212/// isGlobal - Return true if the specified tag is legal for DIGlobal.
213bool DIDescriptor::isGlobal() const {
214 return isGlobalVariable();
215}
216
Devang Patel716a67f2011-02-03 00:13:47 +0000217/// isUnspecifiedParmeter - Return true if the specified tag is
Devang Pateld6747df2010-10-06 20:50:40 +0000218/// DW_TAG_unspecified_parameters.
219bool DIDescriptor::isUnspecifiedParameter() const {
220 return DbgNode && getTag() == dwarf::DW_TAG_unspecified_parameters;
221}
222
Daniel Dunbarf612ff62009-09-19 20:40:05 +0000223/// isScope - Return true if the specified tag is one of the scope
Devang Patel43d98b32009-08-31 20:44:45 +0000224/// related tag.
225bool DIDescriptor::isScope() const {
Devang Patel3c91b052010-03-08 20:52:55 +0000226 if (!DbgNode) return false;
Chris Lattner099b7792009-12-29 09:22:47 +0000227 switch (getTag()) {
228 case dwarf::DW_TAG_compile_unit:
229 case dwarf::DW_TAG_lexical_block:
230 case dwarf::DW_TAG_subprogram:
231 case dwarf::DW_TAG_namespace:
232 return true;
233 default:
234 break;
Devang Patel43d98b32009-08-31 20:44:45 +0000235 }
236 return false;
237}
Devang Patel6ceea332009-08-31 18:49:10 +0000238
Devang Patel7e2cb112011-02-02 21:38:25 +0000239/// isTemplateTypeParameter - Return true if the specified tag is
240/// DW_TAG_template_type_parameter.
241bool DIDescriptor::isTemplateTypeParameter() const {
242 return DbgNode && getTag() == dwarf::DW_TAG_template_type_parameter;
243}
244
Devang Patele7d93872011-02-02 22:35:53 +0000245/// isTemplateValueParameter - Return true if the specified tag is
246/// DW_TAG_template_value_parameter.
247bool DIDescriptor::isTemplateValueParameter() const {
248 return DbgNode && getTag() == dwarf::DW_TAG_template_value_parameter;
249}
250
Devang Patelc9f322d2009-08-31 21:34:44 +0000251/// isCompileUnit - Return true if the specified tag is DW_TAG_compile_unit.
252bool DIDescriptor::isCompileUnit() const {
Devang Patel3c91b052010-03-08 20:52:55 +0000253 return DbgNode && getTag() == dwarf::DW_TAG_compile_unit;
Devang Patelc9f322d2009-08-31 21:34:44 +0000254}
255
Devang Patel7aa81892010-03-08 22:27:22 +0000256/// isFile - Return true if the specified tag is DW_TAG_file_type.
257bool DIDescriptor::isFile() const {
258 return DbgNode && getTag() == dwarf::DW_TAG_file_type;
259}
260
Devang Patel6404e4e2009-12-15 19:16:48 +0000261/// isNameSpace - Return true if the specified tag is DW_TAG_namespace.
262bool DIDescriptor::isNameSpace() const {
Devang Patel3c91b052010-03-08 20:52:55 +0000263 return DbgNode && getTag() == dwarf::DW_TAG_namespace;
Devang Patel6404e4e2009-12-15 19:16:48 +0000264}
265
Devang Patel5e005d82009-08-31 22:00:15 +0000266/// isLexicalBlock - Return true if the specified tag is DW_TAG_lexical_block.
267bool DIDescriptor::isLexicalBlock() const {
Devang Patel3c91b052010-03-08 20:52:55 +0000268 return DbgNode && getTag() == dwarf::DW_TAG_lexical_block;
Devang Patel5e005d82009-08-31 22:00:15 +0000269}
270
Devang Patelecbeb1a2009-09-30 22:34:41 +0000271/// isSubrange - Return true if the specified tag is DW_TAG_subrange_type.
272bool DIDescriptor::isSubrange() const {
Devang Patel3c91b052010-03-08 20:52:55 +0000273 return DbgNode && getTag() == dwarf::DW_TAG_subrange_type;
Devang Patelecbeb1a2009-09-30 22:34:41 +0000274}
275
276/// isEnumerator - Return true if the specified tag is DW_TAG_enumerator.
277bool DIDescriptor::isEnumerator() const {
Devang Patel3c91b052010-03-08 20:52:55 +0000278 return DbgNode && getTag() == dwarf::DW_TAG_enumerator;
Devang Patelecbeb1a2009-09-30 22:34:41 +0000279}
280
Devang Patel6ceea332009-08-31 18:49:10 +0000281//===----------------------------------------------------------------------===//
282// Simple Descriptor Constructors and other Methods
283//===----------------------------------------------------------------------===//
284
Devang Patele9f8f5e2010-05-07 20:54:48 +0000285DIType::DIType(const MDNode *N) : DIScope(N) {
Devang Patel6ceea332009-08-31 18:49:10 +0000286 if (!N) return;
287 if (!isBasicType() && !isDerivedType() && !isCompositeType()) {
288 DbgNode = 0;
289 }
290}
291
Devang Patel68afdc32009-01-05 18:33:01 +0000292unsigned DIArray::getNumElements() const {
Devang Patel3c91b052010-03-08 20:52:55 +0000293 if (!DbgNode)
294 return 0;
Chris Lattner5d0cacd2009-12-31 01:22:29 +0000295 return DbgNode->getNumOperands();
Devang Patel68afdc32009-01-05 18:33:01 +0000296}
Chris Lattnera45664f2008-11-10 02:56:27 +0000297
Devang Patelc4999d72009-07-22 18:23:44 +0000298/// replaceAllUsesWith - Replace all uses of debug info referenced by
Dan Gohman872814a2010-07-21 18:54:18 +0000299/// this descriptor.
Dan Gohman489b29b2010-08-20 22:02:26 +0000300void DIType::replaceAllUsesWith(DIDescriptor &D) {
Devang Patel3c91b052010-03-08 20:52:55 +0000301 if (!DbgNode)
Devang Patelc4999d72009-07-22 18:23:44 +0000302 return;
303
Daniel Dunbar48a097b2009-09-22 02:03:18 +0000304 // Since we use a TrackingVH for the node, its easy for clients to manufacture
305 // legitimate situations where they want to replaceAllUsesWith() on something
306 // which, due to uniquing, has merged with the source. We shield clients from
307 // this detail by allowing a value to be replaced with replaceAllUsesWith()
308 // itself.
Devang Pateled66bf52010-05-07 18:19:32 +0000309 if (DbgNode != D) {
Devang Patele9f8f5e2010-05-07 20:54:48 +0000310 MDNode *Node = const_cast<MDNode*>(DbgNode);
311 const MDNode *DN = D;
312 const Value *V = cast_or_null<Value>(DN);
313 Node->replaceAllUsesWith(const_cast<Value*>(V));
Dan Gohman489b29b2010-08-20 22:02:26 +0000314 MDNode::deleteTemporary(Node);
Daniel Dunbar48a097b2009-09-22 02:03:18 +0000315 }
Devang Patelc4999d72009-07-22 18:23:44 +0000316}
317
Devang Patel0a2551d2010-12-08 20:18:20 +0000318/// replaceAllUsesWith - Replace all uses of debug info referenced by
319/// this descriptor.
320void DIType::replaceAllUsesWith(MDNode *D) {
321 if (!DbgNode)
322 return;
323
324 // Since we use a TrackingVH for the node, its easy for clients to manufacture
325 // legitimate situations where they want to replaceAllUsesWith() on something
326 // which, due to uniquing, has merged with the source. We shield clients from
327 // this detail by allowing a value to be replaced with replaceAllUsesWith()
328 // itself.
329 if (DbgNode != D) {
330 MDNode *Node = const_cast<MDNode*>(DbgNode);
331 const MDNode *DN = D;
332 const Value *V = cast_or_null<Value>(DN);
333 Node->replaceAllUsesWith(const_cast<Value*>(V));
334 MDNode::deleteTemporary(Node);
335 }
336}
337
Devang Patel6f9d8ff2011-08-15 17:57:41 +0000338/// isUnsignedDIType - Return true if type encoding is unsigned.
339bool DIType::isUnsignedDIType() {
340 DIDerivedType DTy(DbgNode);
341 if (DTy.Verify())
342 return DTy.getTypeDerivedFrom().isUnsignedDIType();
343
344 DIBasicType BTy(DbgNode);
345 if (BTy.Verify()) {
346 unsigned Encoding = BTy.getEncoding();
347 if (Encoding == dwarf::DW_ATE_unsigned ||
348 Encoding == dwarf::DW_ATE_unsigned_char)
349 return true;
350 }
351 return false;
352}
353
Devang Patelb79b5352009-01-19 23:21:49 +0000354/// Verify - Verify that a compile unit is well formed.
355bool DICompileUnit::Verify() const {
Devang Patel3c91b052010-03-08 20:52:55 +0000356 if (!DbgNode)
Devang Patelb79b5352009-01-19 23:21:49 +0000357 return false;
Devang Patel65dbc902009-11-25 17:36:49 +0000358 StringRef N = getFilename();
359 if (N.empty())
Bill Wendling0582ae92009-03-13 04:39:26 +0000360 return false;
Devang Patelb79b5352009-01-19 23:21:49 +0000361 // It is possible that directory and produce string is empty.
Bill Wendling0582ae92009-03-13 04:39:26 +0000362 return true;
Devang Patelb79b5352009-01-19 23:21:49 +0000363}
364
365/// Verify - Verify that a type descriptor is well formed.
366bool DIType::Verify() const {
Devang Patel3c91b052010-03-08 20:52:55 +0000367 if (!DbgNode)
Devang Patelb79b5352009-01-19 23:21:49 +0000368 return false;
Devang Patel94c7ddb2011-08-16 22:09:43 +0000369 if (getContext() && !getContext().Verify())
Devang Patelb79b5352009-01-19 23:21:49 +0000370 return false;
Devang Patelb71bbf92010-11-02 20:41:13 +0000371 unsigned Tag = getTag();
372 if (!isBasicType() && Tag != dwarf::DW_TAG_const_type &&
373 Tag != dwarf::DW_TAG_volatile_type && Tag != dwarf::DW_TAG_pointer_type &&
Devang Patelfe58f952010-12-07 23:25:47 +0000374 Tag != dwarf::DW_TAG_reference_type && Tag != dwarf::DW_TAG_restrict_type
Devang Patel43c249c2010-12-08 01:50:15 +0000375 && Tag != dwarf::DW_TAG_vector_type && Tag != dwarf::DW_TAG_array_type
376 && Tag != dwarf::DW_TAG_enumeration_type
Devang Patelb71541a2011-08-31 18:04:31 +0000377 && Tag != dwarf::DW_TAG_subroutine_type
Devang Patelfe58f952010-12-07 23:25:47 +0000378 && getFilename().empty())
Devang Patelb79b5352009-01-19 23:21:49 +0000379 return false;
380 return true;
381}
382
Devang Patel0c4720c2010-08-23 18:25:56 +0000383/// Verify - Verify that a basic type descriptor is well formed.
384bool DIBasicType::Verify() const {
385 return isBasicType();
386}
387
388/// Verify - Verify that a derived type descriptor is well formed.
389bool DIDerivedType::Verify() const {
390 return isDerivedType();
391}
392
Devang Patelb79b5352009-01-19 23:21:49 +0000393/// Verify - Verify that a composite type descriptor is well formed.
394bool DICompositeType::Verify() const {
Devang Patel3c91b052010-03-08 20:52:55 +0000395 if (!DbgNode)
Devang Patelb79b5352009-01-19 23:21:49 +0000396 return false;
Devang Patel94c7ddb2011-08-16 22:09:43 +0000397 if (getContext() && !getContext().Verify())
Devang Patelb79b5352009-01-19 23:21:49 +0000398 return false;
399
Devang Patelb79b5352009-01-19 23:21:49 +0000400 return true;
401}
402
403/// Verify - Verify that a subprogram descriptor is well formed.
404bool DISubprogram::Verify() const {
Devang Patel3c91b052010-03-08 20:52:55 +0000405 if (!DbgNode)
Devang Patelb79b5352009-01-19 23:21:49 +0000406 return false;
Daniel Dunbarf612ff62009-09-19 20:40:05 +0000407
Devang Patel94c7ddb2011-08-16 22:09:43 +0000408 if (getContext() && !getContext().Verify())
Devang Patelb79b5352009-01-19 23:21:49 +0000409 return false;
410
411 DICompositeType Ty = getType();
Devang Patel3c91b052010-03-08 20:52:55 +0000412 if (!Ty.Verify())
Devang Patelb79b5352009-01-19 23:21:49 +0000413 return false;
414 return true;
415}
416
417/// Verify - Verify that a global variable descriptor is well formed.
418bool DIGlobalVariable::Verify() const {
Devang Patel3c91b052010-03-08 20:52:55 +0000419 if (!DbgNode)
Devang Patelb79b5352009-01-19 23:21:49 +0000420 return false;
Daniel Dunbarf612ff62009-09-19 20:40:05 +0000421
Devang Patel65dbc902009-11-25 17:36:49 +0000422 if (getDisplayName().empty())
Devang Patel84c73e92009-11-06 17:58:12 +0000423 return false;
424
Devang Patel94c7ddb2011-08-16 22:09:43 +0000425 if (getContext() && !getContext().Verify())
Devang Patelb79b5352009-01-19 23:21:49 +0000426 return false;
427
428 DIType Ty = getType();
429 if (!Ty.Verify())
430 return false;
431
Devang Patel27398962010-08-09 21:39:24 +0000432 if (!getGlobal() && !getConstant())
Devang Patelb79b5352009-01-19 23:21:49 +0000433 return false;
434
435 return true;
436}
437
438/// Verify - Verify that a variable descriptor is well formed.
439bool DIVariable::Verify() const {
Devang Patel3c91b052010-03-08 20:52:55 +0000440 if (!DbgNode)
Devang Patelb79b5352009-01-19 23:21:49 +0000441 return false;
Daniel Dunbarf612ff62009-09-19 20:40:05 +0000442
Devang Patel94c7ddb2011-08-16 22:09:43 +0000443 if (getContext() && !getContext().Verify())
Devang Patel62077af2010-05-07 21:42:24 +0000444 return false;
445
Devang Patelb79b5352009-01-19 23:21:49 +0000446 DIType Ty = getType();
447 if (!Ty.Verify())
448 return false;
449
Devang Patelb79b5352009-01-19 23:21:49 +0000450 return true;
451}
452
Devang Patel3c91b052010-03-08 20:52:55 +0000453/// Verify - Verify that a location descriptor is well formed.
454bool DILocation::Verify() const {
455 if (!DbgNode)
456 return false;
Jim Grosbache62b6902010-07-21 21:36:25 +0000457
Devang Patel3c91b052010-03-08 20:52:55 +0000458 return DbgNode->getNumOperands() == 4;
459}
460
Devang Patel47e22652010-05-07 23:04:32 +0000461/// Verify - Verify that a namespace descriptor is well formed.
462bool DINameSpace::Verify() const {
463 if (!DbgNode)
464 return false;
465 if (getName().empty())
466 return false;
Devang Patel47e22652010-05-07 23:04:32 +0000467 return true;
468}
469
Devang Patel36375ee2009-02-17 21:23:59 +0000470/// getOriginalTypeSize - If this type is derived from a base type then
471/// return base type size.
472uint64_t DIDerivedType::getOriginalTypeSize() const {
Devang Patel61ecbd12009-11-04 23:48:00 +0000473 unsigned Tag = getTag();
474 if (Tag == dwarf::DW_TAG_member || Tag == dwarf::DW_TAG_typedef ||
475 Tag == dwarf::DW_TAG_const_type || Tag == dwarf::DW_TAG_volatile_type ||
476 Tag == dwarf::DW_TAG_restrict_type) {
477 DIType BaseType = getTypeDerivedFrom();
Jim Grosbache62b6902010-07-21 21:36:25 +0000478 // If this type is not derived from any type then take conservative
Devang Patel5ebfa2d2009-11-06 18:24:05 +0000479 // approach.
Devang Patel3c91b052010-03-08 20:52:55 +0000480 if (!BaseType.isValid())
Devang Patel5ebfa2d2009-11-06 18:24:05 +0000481 return getSizeInBits();
Devang Patel61ecbd12009-11-04 23:48:00 +0000482 if (BaseType.isDerivedType())
Devang Patel2db49d72010-05-07 18:11:54 +0000483 return DIDerivedType(BaseType).getOriginalTypeSize();
Devang Patel61ecbd12009-11-04 23:48:00 +0000484 else
485 return BaseType.getSizeInBits();
486 }
Jim Grosbache62b6902010-07-21 21:36:25 +0000487
Devang Patel61ecbd12009-11-04 23:48:00 +0000488 return getSizeInBits();
Devang Patel36375ee2009-02-17 21:23:59 +0000489}
Devang Patelb79b5352009-01-19 23:21:49 +0000490
Jim Grosbache62b6902010-07-21 21:36:25 +0000491/// isInlinedFnArgument - Return true if this variable provides debugging
Devang Patel719f6a92010-04-29 20:40:36 +0000492/// information for an inlined function arguments.
493bool DIVariable::isInlinedFnArgument(const Function *CurFn) {
494 assert(CurFn && "Invalid function");
495 if (!getContext().isSubprogram())
496 return false;
Jim Grosbache62b6902010-07-21 21:36:25 +0000497 // This variable is not inlined function argument if its scope
Devang Patel719f6a92010-04-29 20:40:36 +0000498 // does not describe current function.
Devang Patel2db49d72010-05-07 18:11:54 +0000499 return !(DISubprogram(getContext()).describes(CurFn));
Devang Patel719f6a92010-04-29 20:40:36 +0000500}
501
Devang Patelaf5b6bb2009-04-15 00:06:07 +0000502/// describes - Return true if this subprogram provides debugging
503/// information for the function F.
504bool DISubprogram::describes(const Function *F) {
Chris Lattner099b7792009-12-29 09:22:47 +0000505 assert(F && "Invalid function");
Devang Patelffd33cd2010-06-16 06:42:02 +0000506 if (F == getFunction())
507 return true;
Devang Patel65dbc902009-11-25 17:36:49 +0000508 StringRef Name = getLinkageName();
509 if (Name.empty())
Devang Patel5ccdd102009-09-29 18:40:58 +0000510 Name = getName();
Devang Patel65dbc902009-11-25 17:36:49 +0000511 if (F->getName() == Name)
Devang Patelaf5b6bb2009-04-15 00:06:07 +0000512 return true;
513 return false;
514}
515
Jim Grosbache62b6902010-07-21 21:36:25 +0000516unsigned DISubprogram::isOptimized() const {
Devang Patelccff8122010-04-30 19:38:23 +0000517 assert (DbgNode && "Invalid subprogram descriptor!");
518 if (DbgNode->getNumOperands() == 16)
519 return getUnsignedField(15);
520 return 0;
521}
522
Devang Patel93d39be2011-08-19 23:28:12 +0000523MDNode *DISubprogram::getVariablesNodes() const {
524 if (!DbgNode || DbgNode->getNumOperands() <= 19)
525 return NULL;
526 if (MDNode *Temp = dyn_cast_or_null<MDNode>(DbgNode->getOperand(19)))
527 return dyn_cast_or_null<MDNode>(Temp->getOperand(0));
528 return NULL;
529}
530
531DIArray DISubprogram::getVariables() const {
532 if (!DbgNode || DbgNode->getNumOperands() <= 19)
533 return DIArray();
534 if (MDNode *T = dyn_cast_or_null<MDNode>(DbgNode->getOperand(19)))
535 if (MDNode *A = dyn_cast_or_null<MDNode>(T->getOperand(0)))
536 return DIArray(A);
537 return DIArray();
538}
539
Devang Patel65dbc902009-11-25 17:36:49 +0000540StringRef DIScope::getFilename() const {
Devang Patel77bf2952010-03-08 22:02:50 +0000541 if (!DbgNode)
542 return StringRef();
Jim Grosbache62b6902010-07-21 21:36:25 +0000543 if (isLexicalBlock())
Devang Patelecbeb1a2009-09-30 22:34:41 +0000544 return DILexicalBlock(DbgNode).getFilename();
Chris Lattner099b7792009-12-29 09:22:47 +0000545 if (isSubprogram())
Devang Patelecbeb1a2009-09-30 22:34:41 +0000546 return DISubprogram(DbgNode).getFilename();
Chris Lattner099b7792009-12-29 09:22:47 +0000547 if (isCompileUnit())
Devang Patelecbeb1a2009-09-30 22:34:41 +0000548 return DICompileUnit(DbgNode).getFilename();
Chris Lattner099b7792009-12-29 09:22:47 +0000549 if (isNameSpace())
Devang Patel6404e4e2009-12-15 19:16:48 +0000550 return DINameSpace(DbgNode).getFilename();
Devang Patel77bf2952010-03-08 22:02:50 +0000551 if (isType())
552 return DIType(DbgNode).getFilename();
Devang Patel7aa81892010-03-08 22:27:22 +0000553 if (isFile())
554 return DIFile(DbgNode).getFilename();
Chris Lattner099b7792009-12-29 09:22:47 +0000555 assert(0 && "Invalid DIScope!");
Devang Patel65dbc902009-11-25 17:36:49 +0000556 return StringRef();
Devang Patelecbeb1a2009-09-30 22:34:41 +0000557}
558
Devang Patel65dbc902009-11-25 17:36:49 +0000559StringRef DIScope::getDirectory() const {
Devang Patel77bf2952010-03-08 22:02:50 +0000560 if (!DbgNode)
561 return StringRef();
Jim Grosbache62b6902010-07-21 21:36:25 +0000562 if (isLexicalBlock())
Devang Patelecbeb1a2009-09-30 22:34:41 +0000563 return DILexicalBlock(DbgNode).getDirectory();
Chris Lattner099b7792009-12-29 09:22:47 +0000564 if (isSubprogram())
Devang Patelecbeb1a2009-09-30 22:34:41 +0000565 return DISubprogram(DbgNode).getDirectory();
Chris Lattner099b7792009-12-29 09:22:47 +0000566 if (isCompileUnit())
Devang Patelecbeb1a2009-09-30 22:34:41 +0000567 return DICompileUnit(DbgNode).getDirectory();
Chris Lattner099b7792009-12-29 09:22:47 +0000568 if (isNameSpace())
Devang Patel6404e4e2009-12-15 19:16:48 +0000569 return DINameSpace(DbgNode).getDirectory();
Devang Patel77bf2952010-03-08 22:02:50 +0000570 if (isType())
571 return DIType(DbgNode).getDirectory();
Devang Patel7aa81892010-03-08 22:27:22 +0000572 if (isFile())
573 return DIFile(DbgNode).getDirectory();
Chris Lattner099b7792009-12-29 09:22:47 +0000574 assert(0 && "Invalid DIScope!");
Devang Patel65dbc902009-11-25 17:36:49 +0000575 return StringRef();
Devang Patelecbeb1a2009-09-30 22:34:41 +0000576}
577
Devang Patel94c7ddb2011-08-16 22:09:43 +0000578DIArray DICompileUnit::getEnumTypes() const {
579 if (!DbgNode || DbgNode->getNumOperands() < 14)
580 return DIArray();
581
582 if (MDNode *N = dyn_cast_or_null<MDNode>(DbgNode->getOperand(10)))
583 if (MDNode *A = dyn_cast_or_null<MDNode>(N->getOperand(0)))
584 return DIArray(A);
585 return DIArray();
586}
587
588DIArray DICompileUnit::getRetainedTypes() const {
589 if (!DbgNode || DbgNode->getNumOperands() < 14)
590 return DIArray();
591
592 if (MDNode *N = dyn_cast_or_null<MDNode>(DbgNode->getOperand(11)))
593 if (MDNode *A = dyn_cast_or_null<MDNode>(N->getOperand(0)))
594 return DIArray(A);
595 return DIArray();
596}
597
598DIArray DICompileUnit::getSubprograms() const {
599 if (!DbgNode || DbgNode->getNumOperands() < 14)
600 return DIArray();
601
602 if (MDNode *N = dyn_cast_or_null<MDNode>(DbgNode->getOperand(12)))
603 if (MDNode *A = dyn_cast_or_null<MDNode>(N->getOperand(0)))
604 return DIArray(A);
605 return DIArray();
606}
607
608
609DIArray DICompileUnit::getGlobalVariables() const {
610 if (!DbgNode || DbgNode->getNumOperands() < 14)
611 return DIArray();
612
613 if (MDNode *N = dyn_cast_or_null<MDNode>(DbgNode->getOperand(13)))
614 if (MDNode *A = dyn_cast_or_null<MDNode>(N->getOperand(0)))
615 return DIArray(A);
616 return DIArray();
617}
618
Chris Lattnera45664f2008-11-10 02:56:27 +0000619//===----------------------------------------------------------------------===//
Devang Patel7136a652009-07-01 22:10:23 +0000620// DIDescriptor: dump routines for all descriptors.
621//===----------------------------------------------------------------------===//
622
623
Dan Gohman50404362010-05-07 15:30:29 +0000624/// print - Print descriptor.
625void DIDescriptor::print(raw_ostream &OS) const {
626 OS << "[" << dwarf::TagString(getTag()) << "] ";
627 OS.write_hex((intptr_t) &*DbgNode) << ']';
Devang Patel7136a652009-07-01 22:10:23 +0000628}
629
Dan Gohman50404362010-05-07 15:30:29 +0000630/// print - Print compile unit.
631void DICompileUnit::print(raw_ostream &OS) const {
Devang Patel7136a652009-07-01 22:10:23 +0000632 if (getLanguage())
Dan Gohman50404362010-05-07 15:30:29 +0000633 OS << " [" << dwarf::LanguageString(getLanguage()) << "] ";
Devang Patel7136a652009-07-01 22:10:23 +0000634
Dan Gohmanfaa19c32010-05-10 20:07:44 +0000635 OS << " [" << getDirectory() << "/" << getFilename() << "]";
Devang Patel7136a652009-07-01 22:10:23 +0000636}
637
Dan Gohman50404362010-05-07 15:30:29 +0000638/// print - Print type.
639void DIType::print(raw_ostream &OS) const {
Devang Patel3c91b052010-03-08 20:52:55 +0000640 if (!DbgNode) return;
Devang Patel7136a652009-07-01 22:10:23 +0000641
Devang Patel65dbc902009-11-25 17:36:49 +0000642 StringRef Res = getName();
643 if (!Res.empty())
Dan Gohman50404362010-05-07 15:30:29 +0000644 OS << " [" << Res << "] ";
Devang Patel7136a652009-07-01 22:10:23 +0000645
646 unsigned Tag = getTag();
Dan Gohman50404362010-05-07 15:30:29 +0000647 OS << " [" << dwarf::TagString(Tag) << "] ";
Devang Patel7136a652009-07-01 22:10:23 +0000648
649 // TODO : Print context
Dan Gohman50404362010-05-07 15:30:29 +0000650 OS << " ["
Dan Gohman9a7063e2010-05-07 16:39:27 +0000651 << "line " << getLineNumber() << ", "
652 << getSizeInBits() << " bits, "
653 << getAlignInBits() << " bit alignment, "
654 << getOffsetInBits() << " bit offset"
Chris Lattnera81d29b2009-08-23 07:33:14 +0000655 << "] ";
Devang Patel7136a652009-07-01 22:10:23 +0000656
Daniel Dunbarf612ff62009-09-19 20:40:05 +0000657 if (isPrivate())
Dan Gohman50404362010-05-07 15:30:29 +0000658 OS << " [private] ";
Devang Patel7136a652009-07-01 22:10:23 +0000659 else if (isProtected())
Dan Gohman50404362010-05-07 15:30:29 +0000660 OS << " [protected] ";
Devang Patel7136a652009-07-01 22:10:23 +0000661
662 if (isForwardDecl())
Dan Gohman50404362010-05-07 15:30:29 +0000663 OS << " [fwd] ";
Devang Patel7136a652009-07-01 22:10:23 +0000664
Devang Patel6ceea332009-08-31 18:49:10 +0000665 if (isBasicType())
Dan Gohmanc014d092010-05-07 16:17:22 +0000666 DIBasicType(DbgNode).print(OS);
Devang Patel6ceea332009-08-31 18:49:10 +0000667 else if (isDerivedType())
Dan Gohmanc014d092010-05-07 16:17:22 +0000668 DIDerivedType(DbgNode).print(OS);
Devang Patel6ceea332009-08-31 18:49:10 +0000669 else if (isCompositeType())
Dan Gohmanc014d092010-05-07 16:17:22 +0000670 DICompositeType(DbgNode).print(OS);
Devang Patel7136a652009-07-01 22:10:23 +0000671 else {
Dan Gohman50404362010-05-07 15:30:29 +0000672 OS << "Invalid DIType\n";
Devang Patel7136a652009-07-01 22:10:23 +0000673 return;
674 }
675
Dan Gohman50404362010-05-07 15:30:29 +0000676 OS << "\n";
Devang Patel7136a652009-07-01 22:10:23 +0000677}
678
Dan Gohman50404362010-05-07 15:30:29 +0000679/// print - Print basic type.
680void DIBasicType::print(raw_ostream &OS) const {
681 OS << " [" << dwarf::AttributeEncodingString(getEncoding()) << "] ";
Devang Patel7136a652009-07-01 22:10:23 +0000682}
683
Dan Gohman50404362010-05-07 15:30:29 +0000684/// print - Print derived type.
685void DIDerivedType::print(raw_ostream &OS) const {
Dan Gohmanc014d092010-05-07 16:17:22 +0000686 OS << "\n\t Derived From: "; getTypeDerivedFrom().print(OS);
Devang Patel7136a652009-07-01 22:10:23 +0000687}
688
Dan Gohman50404362010-05-07 15:30:29 +0000689/// print - Print composite type.
690void DICompositeType::print(raw_ostream &OS) const {
Devang Patel7136a652009-07-01 22:10:23 +0000691 DIArray A = getTypeArray();
Dan Gohman50404362010-05-07 15:30:29 +0000692 OS << " [" << A.getNumElements() << " elements]";
Devang Patel7136a652009-07-01 22:10:23 +0000693}
694
Dan Gohman50404362010-05-07 15:30:29 +0000695/// print - Print subprogram.
696void DISubprogram::print(raw_ostream &OS) const {
Devang Patel65dbc902009-11-25 17:36:49 +0000697 StringRef Res = getName();
698 if (!Res.empty())
Dan Gohman50404362010-05-07 15:30:29 +0000699 OS << " [" << Res << "] ";
Devang Patel82dfc0c2009-08-31 22:47:13 +0000700
701 unsigned Tag = getTag();
Dan Gohman50404362010-05-07 15:30:29 +0000702 OS << " [" << dwarf::TagString(Tag) << "] ";
Devang Patel82dfc0c2009-08-31 22:47:13 +0000703
704 // TODO : Print context
Dan Gohman50404362010-05-07 15:30:29 +0000705 OS << " [" << getLineNumber() << "] ";
Devang Patel82dfc0c2009-08-31 22:47:13 +0000706
707 if (isLocalToUnit())
Dan Gohman50404362010-05-07 15:30:29 +0000708 OS << " [local] ";
Devang Patel82dfc0c2009-08-31 22:47:13 +0000709
710 if (isDefinition())
Dan Gohman50404362010-05-07 15:30:29 +0000711 OS << " [def] ";
Devang Patel82dfc0c2009-08-31 22:47:13 +0000712
Dan Gohman50404362010-05-07 15:30:29 +0000713 OS << "\n";
714}
715
716/// print - Print global variable.
717void DIGlobalVariable::print(raw_ostream &OS) const {
718 OS << " [";
Devang Patela49d8772010-05-07 23:19:07 +0000719 StringRef Res = getName();
720 if (!Res.empty())
721 OS << " [" << Res << "] ";
722
723 unsigned Tag = getTag();
724 OS << " [" << dwarf::TagString(Tag) << "] ";
725
726 // TODO : Print context
Devang Patela49d8772010-05-07 23:19:07 +0000727 OS << " [" << getLineNumber() << "] ";
728
729 if (isLocalToUnit())
730 OS << " [local] ";
731
732 if (isDefinition())
733 OS << " [def] ";
734
735 if (isGlobalVariable())
736 DIGlobalVariable(DbgNode).print(OS);
737 OS << "]\n";
Dan Gohman50404362010-05-07 15:30:29 +0000738}
739
Devang Patel48d726f2011-08-09 01:03:14 +0000740static void printDebugLoc(DebugLoc DL, raw_ostream &CommentOS,
741 const LLVMContext &Ctx) {
742 if (!DL.isUnknown()) { // Print source line info.
743 DIScope Scope(DL.getScope(Ctx));
744 // Omit the directory, because it's likely to be long and uninteresting.
745 if (Scope.Verify())
746 CommentOS << Scope.getFilename();
747 else
748 CommentOS << "<unknown>";
749 CommentOS << ':' << DL.getLine();
750 if (DL.getCol() != 0)
751 CommentOS << ':' << DL.getCol();
752 DebugLoc InlinedAtDL = DebugLoc::getFromDILocation(DL.getInlinedAt(Ctx));
753 if (!InlinedAtDL.isUnknown()) {
754 CommentOS << " @[ ";
755 printDebugLoc(InlinedAtDL, CommentOS, Ctx);
756 CommentOS << " ]";
757 }
758 }
759}
760
761void DIVariable::printExtendedName(raw_ostream &OS) const {
762 const LLVMContext &Ctx = DbgNode->getContext();
763 StringRef Res = getName();
764 if (!Res.empty())
765 OS << Res << "," << getLineNumber();
766 if (MDNode *InlinedAt = getInlinedAt()) {
767 DebugLoc InlinedAtDL = DebugLoc::getFromDILocation(InlinedAt);
768 if (!InlinedAtDL.isUnknown()) {
769 OS << " @[";
770 printDebugLoc(InlinedAtDL, OS, Ctx);
771 OS << "]";
772 }
773 }
774}
775
Dan Gohman50404362010-05-07 15:30:29 +0000776/// print - Print variable.
777void DIVariable::print(raw_ostream &OS) const {
778 StringRef Res = getName();
779 if (!Res.empty())
780 OS << " [" << Res << "] ";
781
Dan Gohman50404362010-05-07 15:30:29 +0000782 OS << " [" << getLineNumber() << "] ";
Dan Gohmanc014d092010-05-07 16:17:22 +0000783 getType().print(OS);
Dan Gohman50404362010-05-07 15:30:29 +0000784 OS << "\n";
785
786 // FIXME: Dump complex addresses
787}
788
789/// dump - Print descriptor to dbgs() with a newline.
790void DIDescriptor::dump() const {
791 print(dbgs()); dbgs() << '\n';
792}
793
794/// dump - Print compile unit to dbgs() with a newline.
795void DICompileUnit::dump() const {
796 print(dbgs()); dbgs() << '\n';
797}
798
799/// dump - Print type to dbgs() with a newline.
800void DIType::dump() const {
801 print(dbgs()); dbgs() << '\n';
802}
803
804/// dump - Print basic type to dbgs() with a newline.
805void DIBasicType::dump() const {
806 print(dbgs()); dbgs() << '\n';
807}
808
809/// dump - Print derived type to dbgs() with a newline.
810void DIDerivedType::dump() const {
811 print(dbgs()); dbgs() << '\n';
812}
813
814/// dump - Print composite type to dbgs() with a newline.
815void DICompositeType::dump() const {
816 print(dbgs()); dbgs() << '\n';
817}
818
Dan Gohman50404362010-05-07 15:30:29 +0000819/// dump - Print subprogram to dbgs() with a newline.
820void DISubprogram::dump() const {
821 print(dbgs()); dbgs() << '\n';
Devang Patel7136a652009-07-01 22:10:23 +0000822}
823
824/// dump - Print global variable.
825void DIGlobalVariable::dump() const {
Dan Gohman50404362010-05-07 15:30:29 +0000826 print(dbgs()); dbgs() << '\n';
Devang Patel7136a652009-07-01 22:10:23 +0000827}
828
829/// dump - Print variable.
830void DIVariable::dump() const {
Dan Gohman50404362010-05-07 15:30:29 +0000831 print(dbgs()); dbgs() << '\n';
Devang Patel7136a652009-07-01 22:10:23 +0000832}
833
Devang Patel62367042010-11-10 22:19:21 +0000834/// fixupObjcLikeName - Replace contains special characters used
835/// in a typical Objective-C names with '.' in a given string.
Benjamin Kramer1a81d482011-06-18 14:42:42 +0000836static void fixupObjcLikeName(StringRef Str, SmallVectorImpl<char> &Out) {
837 bool isObjCLike = false;
Devang Patel62367042010-11-10 22:19:21 +0000838 for (size_t i = 0, e = Str.size(); i < e; ++i) {
839 char C = Str[i];
Benjamin Kramer1a81d482011-06-18 14:42:42 +0000840 if (C == '[')
841 isObjCLike = true;
842
843 if (isObjCLike && (C == '[' || C == ']' || C == ' ' || C == ':' ||
844 C == '+' || C == '(' || C == ')'))
845 Out.push_back('.');
846 else
847 Out.push_back(C);
Devang Patel62367042010-11-10 22:19:21 +0000848 }
849}
850
Devang Patel62367042010-11-10 22:19:21 +0000851/// getFnSpecificMDNode - Return a NameMDNode, if available, that is
852/// suitable to hold function specific information.
Devang Patel93d39be2011-08-19 23:28:12 +0000853NamedMDNode *llvm::getFnSpecificMDNode(const Module &M, DISubprogram Fn) {
Benjamin Kramer1a81d482011-06-18 14:42:42 +0000854 SmallString<32> Name = StringRef("llvm.dbg.lv.");
Devang Patel93d39be2011-08-19 23:28:12 +0000855 StringRef FName = "fn";
856 if (Fn.getFunction())
857 FName = Fn.getFunction()->getName();
858 else
859 FName = Fn.getName();
860 char One = '\1';
861 if (FName.startswith(StringRef(&One, 1)))
862 FName = FName.substr(1);
863 fixupObjcLikeName(FName, Name);
Benjamin Kramer1a81d482011-06-18 14:42:42 +0000864 return M.getNamedMetadata(Name.str());
Devang Patel62367042010-11-10 22:19:21 +0000865}
866
Duncan Sands291bb702011-03-02 20:30:37 +0000867/// getOrInsertFnSpecificMDNode - Return a NameMDNode that is suitable
868/// to hold function specific information.
Devang Patel93d39be2011-08-19 23:28:12 +0000869NamedMDNode *llvm::getOrInsertFnSpecificMDNode(Module &M, DISubprogram Fn) {
Benjamin Kramer1a81d482011-06-18 14:42:42 +0000870 SmallString<32> Name = StringRef("llvm.dbg.lv.");
Devang Patel93d39be2011-08-19 23:28:12 +0000871 StringRef FName = "fn";
872 if (Fn.getFunction())
873 FName = Fn.getFunction()->getName();
874 else
875 FName = Fn.getName();
876 char One = '\1';
877 if (FName.startswith(StringRef(&One, 1)))
878 FName = FName.substr(1);
879 fixupObjcLikeName(FName, Name);
880
Benjamin Kramer1a81d482011-06-18 14:42:42 +0000881 return M.getOrInsertNamedMetadata(Name.str());
Devang Patel1a7ca032010-09-28 18:08:20 +0000882}
883
Devang Patel23336b42011-07-19 19:41:54 +0000884/// createInlinedVariable - Create a new inlined variable based on current
885/// variable.
886/// @param DV Current Variable.
887/// @param InlinedScope Location at current variable is inlined.
888DIVariable llvm::createInlinedVariable(MDNode *DV, MDNode *InlinedScope,
889 LLVMContext &VMContext) {
890 SmallVector<Value *, 16> Elts;
891 // Insert inlined scope as 7th element.
892 for (unsigned i = 0, e = DV->getNumOperands(); i != e; ++i)
893 i == 7 ? Elts.push_back(InlinedScope) :
894 Elts.push_back(DV->getOperand(i));
895 return DIVariable(MDNode::get(VMContext, Elts));
896}
Devang Patel1a7ca032010-09-28 18:08:20 +0000897
Devang Patelb549bcf2011-08-10 21:50:54 +0000898/// cleanseInlinedVariable - Remove inlined scope from the variable.
899DIVariable llvm::cleanseInlinedVariable(MDNode *DV, LLVMContext &VMContext) {
900 SmallVector<Value *, 16> Elts;
901 // Insert inlined scope as 7th element.
902 for (unsigned i = 0, e = DV->getNumOperands(); i != e; ++i)
903 i == 7 ?
904 Elts.push_back(llvm::Constant::getNullValue(Type::getInt32Ty(VMContext))):
905 Elts.push_back(DV->getOperand(i));
906 return DIVariable(MDNode::get(VMContext, Elts));
907}
908
Devang Pateld2f79a12009-07-28 19:55:13 +0000909//===----------------------------------------------------------------------===//
Devang Patel98c65172009-07-30 18:25:15 +0000910// DebugInfoFinder implementations.
Devang Pateld2f79a12009-07-28 19:55:13 +0000911//===----------------------------------------------------------------------===//
912
Devang Patel98c65172009-07-30 18:25:15 +0000913/// processModule - Process entire module and collect debug info.
914void DebugInfoFinder::processModule(Module &M) {
Devang Patelfa515ec2011-09-06 17:40:08 +0000915 if (NamedMDNode *CU_Nodes = M.getNamedMetadata("llvm.dbg.cu"))
916 for (unsigned i = 0, e = CU_Nodes->getNumOperands(); i != e; ++i)
917 addCompileUnit(DICompileUnit(CU_Nodes->getOperand(i)));
918
Devang Pateld2f79a12009-07-28 19:55:13 +0000919 for (Module::iterator I = M.begin(), E = M.end(); I != E; ++I)
920 for (Function::iterator FI = (*I).begin(), FE = (*I).end(); FI != FE; ++FI)
921 for (BasicBlock::iterator BI = (*FI).begin(), BE = (*FI).end(); BI != BE;
922 ++BI) {
Devang Patel01c5ff62010-05-04 01:05:02 +0000923 if (DbgDeclareInst *DDI = dyn_cast<DbgDeclareInst>(BI))
Devang Patelb4d31302009-07-31 18:18:52 +0000924 processDeclare(DDI);
Jim Grosbache62b6902010-07-21 21:36:25 +0000925
Chris Lattner28a9bf62010-04-02 20:44:29 +0000926 DebugLoc Loc = BI->getDebugLoc();
927 if (Loc.isUnknown())
928 continue;
Jim Grosbache62b6902010-07-21 21:36:25 +0000929
Chris Lattner28a9bf62010-04-02 20:44:29 +0000930 LLVMContext &Ctx = BI->getContext();
931 DIDescriptor Scope(Loc.getScope(Ctx));
Jim Grosbache62b6902010-07-21 21:36:25 +0000932
Chris Lattner28a9bf62010-04-02 20:44:29 +0000933 if (Scope.isCompileUnit())
Devang Patel2db49d72010-05-07 18:11:54 +0000934 addCompileUnit(DICompileUnit(Scope));
Chris Lattner28a9bf62010-04-02 20:44:29 +0000935 else if (Scope.isSubprogram())
Devang Patel2db49d72010-05-07 18:11:54 +0000936 processSubprogram(DISubprogram(Scope));
Chris Lattner28a9bf62010-04-02 20:44:29 +0000937 else if (Scope.isLexicalBlock())
Devang Patel2db49d72010-05-07 18:11:54 +0000938 processLexicalBlock(DILexicalBlock(Scope));
Jim Grosbache62b6902010-07-21 21:36:25 +0000939
Chris Lattner28a9bf62010-04-02 20:44:29 +0000940 if (MDNode *IA = Loc.getInlinedAt(Ctx))
941 processLocation(DILocation(IA));
Devang Pateld2f79a12009-07-28 19:55:13 +0000942 }
Devang Patele4b27562009-08-28 23:24:31 +0000943
Devang Patelfd5fdc32010-06-28 05:53:08 +0000944 if (NamedMDNode *NMD = M.getNamedMetadata("llvm.dbg.gv")) {
945 for (unsigned i = 0, e = NMD->getNumOperands(); i != e; ++i) {
946 DIGlobalVariable DIG(cast<MDNode>(NMD->getOperand(i)));
947 if (addGlobalVariable(DIG)) {
Devang Patelfa515ec2011-09-06 17:40:08 +0000948 if (DIG.getVersion() <= LLVMDebugVersion10)
949 addCompileUnit(DIG.getCompileUnit());
Devang Patelfd5fdc32010-06-28 05:53:08 +0000950 processType(DIG.getType());
951 }
Devang Pateld2f79a12009-07-28 19:55:13 +0000952 }
953 }
Devang Patelfd5fdc32010-06-28 05:53:08 +0000954
955 if (NamedMDNode *NMD = M.getNamedMetadata("llvm.dbg.sp"))
956 for (unsigned i = 0, e = NMD->getNumOperands(); i != e; ++i)
957 processSubprogram(DISubprogram(NMD->getOperand(i)));
Devang Pateld2f79a12009-07-28 19:55:13 +0000958}
Daniel Dunbarf612ff62009-09-19 20:40:05 +0000959
Devang Patel6daf99b2009-11-10 22:05:35 +0000960/// processLocation - Process DILocation.
961void DebugInfoFinder::processLocation(DILocation Loc) {
Devang Patel3c91b052010-03-08 20:52:55 +0000962 if (!Loc.Verify()) return;
Devang Patel2db49d72010-05-07 18:11:54 +0000963 DIDescriptor S(Loc.getScope());
Devang Patel6daf99b2009-11-10 22:05:35 +0000964 if (S.isCompileUnit())
Devang Patel2db49d72010-05-07 18:11:54 +0000965 addCompileUnit(DICompileUnit(S));
Devang Patel6daf99b2009-11-10 22:05:35 +0000966 else if (S.isSubprogram())
Devang Patel2db49d72010-05-07 18:11:54 +0000967 processSubprogram(DISubprogram(S));
Devang Patel6daf99b2009-11-10 22:05:35 +0000968 else if (S.isLexicalBlock())
Devang Patel2db49d72010-05-07 18:11:54 +0000969 processLexicalBlock(DILexicalBlock(S));
Devang Patel6daf99b2009-11-10 22:05:35 +0000970 processLocation(Loc.getOrigLocation());
971}
972
Devang Patel98c65172009-07-30 18:25:15 +0000973/// processType - Process DIType.
974void DebugInfoFinder::processType(DIType DT) {
Devang Patel72bcdb62009-08-10 22:09:58 +0000975 if (!addType(DT))
Devang Pateld2f79a12009-07-28 19:55:13 +0000976 return;
Devang Patelfa515ec2011-09-06 17:40:08 +0000977 if (DT.getVersion() <= LLVMDebugVersion10)
978 addCompileUnit(DT.getCompileUnit());
Devang Patel6ceea332009-08-31 18:49:10 +0000979 if (DT.isCompositeType()) {
Devang Patel2db49d72010-05-07 18:11:54 +0000980 DICompositeType DCT(DT);
Devang Patel98c65172009-07-30 18:25:15 +0000981 processType(DCT.getTypeDerivedFrom());
Devang Pateld2f79a12009-07-28 19:55:13 +0000982 DIArray DA = DCT.getTypeArray();
Devang Patel3c91b052010-03-08 20:52:55 +0000983 for (unsigned i = 0, e = DA.getNumElements(); i != e; ++i) {
984 DIDescriptor D = DA.getElement(i);
985 if (D.isType())
Devang Patel2db49d72010-05-07 18:11:54 +0000986 processType(DIType(D));
Devang Patel3c91b052010-03-08 20:52:55 +0000987 else if (D.isSubprogram())
Devang Patel2db49d72010-05-07 18:11:54 +0000988 processSubprogram(DISubprogram(D));
Devang Patel3c91b052010-03-08 20:52:55 +0000989 }
Devang Patel6ceea332009-08-31 18:49:10 +0000990 } else if (DT.isDerivedType()) {
Devang Patel2db49d72010-05-07 18:11:54 +0000991 DIDerivedType DDT(DT);
Devang Patel3c91b052010-03-08 20:52:55 +0000992 processType(DDT.getTypeDerivedFrom());
Devang Pateld2f79a12009-07-28 19:55:13 +0000993 }
994}
995
Devang Patelbeab41b2009-10-07 22:04:08 +0000996/// processLexicalBlock
997void DebugInfoFinder::processLexicalBlock(DILexicalBlock LB) {
Devang Patelbeab41b2009-10-07 22:04:08 +0000998 DIScope Context = LB.getContext();
999 if (Context.isLexicalBlock())
Devang Patel2db49d72010-05-07 18:11:54 +00001000 return processLexicalBlock(DILexicalBlock(Context));
Devang Patelbeab41b2009-10-07 22:04:08 +00001001 else
Devang Patel2db49d72010-05-07 18:11:54 +00001002 return processSubprogram(DISubprogram(Context));
Devang Patelbeab41b2009-10-07 22:04:08 +00001003}
1004
Devang Patel98c65172009-07-30 18:25:15 +00001005/// processSubprogram - Process DISubprogram.
1006void DebugInfoFinder::processSubprogram(DISubprogram SP) {
Devang Pateld2f79a12009-07-28 19:55:13 +00001007 if (!addSubprogram(SP))
1008 return;
Devang Patelfa515ec2011-09-06 17:40:08 +00001009 if (SP.getVersion() <= LLVMDebugVersion10)
1010 addCompileUnit(SP.getCompileUnit());
Devang Patel98c65172009-07-30 18:25:15 +00001011 processType(SP.getType());
Devang Pateld2f79a12009-07-28 19:55:13 +00001012}
1013
Devang Patelb4d31302009-07-31 18:18:52 +00001014/// processDeclare - Process DbgDeclareInst.
1015void DebugInfoFinder::processDeclare(DbgDeclareInst *DDI) {
Devang Patel3c91b052010-03-08 20:52:55 +00001016 MDNode *N = dyn_cast<MDNode>(DDI->getVariable());
1017 if (!N) return;
1018
1019 DIDescriptor DV(N);
1020 if (!DV.isVariable())
Devang Patelb4d31302009-07-31 18:18:52 +00001021 return;
1022
Devang Patel2db49d72010-05-07 18:11:54 +00001023 if (!NodesSeen.insert(DV))
Devang Patelb4d31302009-07-31 18:18:52 +00001024 return;
Devang Patelfa515ec2011-09-06 17:40:08 +00001025 if (DIVariable(N).getVersion() <= LLVMDebugVersion10)
1026 addCompileUnit(DIVariable(N).getCompileUnit());
Devang Patel3c91b052010-03-08 20:52:55 +00001027 processType(DIVariable(N).getType());
Devang Patelb4d31302009-07-31 18:18:52 +00001028}
1029
Devang Patel72bcdb62009-08-10 22:09:58 +00001030/// addType - Add type into Tys.
1031bool DebugInfoFinder::addType(DIType DT) {
Devang Patel3c91b052010-03-08 20:52:55 +00001032 if (!DT.isValid())
Devang Patel72bcdb62009-08-10 22:09:58 +00001033 return false;
1034
Devang Patel2db49d72010-05-07 18:11:54 +00001035 if (!NodesSeen.insert(DT))
Devang Patel72bcdb62009-08-10 22:09:58 +00001036 return false;
1037
Devang Patel2db49d72010-05-07 18:11:54 +00001038 TYs.push_back(DT);
Devang Patel72bcdb62009-08-10 22:09:58 +00001039 return true;
1040}
1041
Devang Pateld2f79a12009-07-28 19:55:13 +00001042/// addCompileUnit - Add compile unit into CUs.
Devang Patel98c65172009-07-30 18:25:15 +00001043bool DebugInfoFinder::addCompileUnit(DICompileUnit CU) {
Devang Patel3c91b052010-03-08 20:52:55 +00001044 if (!CU.Verify())
Devang Pateld2f79a12009-07-28 19:55:13 +00001045 return false;
1046
Devang Patel2db49d72010-05-07 18:11:54 +00001047 if (!NodesSeen.insert(CU))
Devang Pateld2f79a12009-07-28 19:55:13 +00001048 return false;
1049
Devang Patel2db49d72010-05-07 18:11:54 +00001050 CUs.push_back(CU);
Devang Pateld2f79a12009-07-28 19:55:13 +00001051 return true;
1052}
Daniel Dunbarf612ff62009-09-19 20:40:05 +00001053
Devang Pateld2f79a12009-07-28 19:55:13 +00001054/// addGlobalVariable - Add global variable into GVs.
Devang Patel98c65172009-07-30 18:25:15 +00001055bool DebugInfoFinder::addGlobalVariable(DIGlobalVariable DIG) {
Devang Patel2db49d72010-05-07 18:11:54 +00001056 if (!DIDescriptor(DIG).isGlobalVariable())
Devang Pateld2f79a12009-07-28 19:55:13 +00001057 return false;
1058
Devang Patel2db49d72010-05-07 18:11:54 +00001059 if (!NodesSeen.insert(DIG))
Devang Pateld2f79a12009-07-28 19:55:13 +00001060 return false;
1061
Devang Patel2db49d72010-05-07 18:11:54 +00001062 GVs.push_back(DIG);
Devang Pateld2f79a12009-07-28 19:55:13 +00001063 return true;
1064}
1065
1066// addSubprogram - Add subprgoram into SPs.
Devang Patel98c65172009-07-30 18:25:15 +00001067bool DebugInfoFinder::addSubprogram(DISubprogram SP) {
Devang Patel2db49d72010-05-07 18:11:54 +00001068 if (!DIDescriptor(SP).isSubprogram())
Devang Pateld2f79a12009-07-28 19:55:13 +00001069 return false;
Daniel Dunbarf612ff62009-09-19 20:40:05 +00001070
Devang Patel2db49d72010-05-07 18:11:54 +00001071 if (!NodesSeen.insert(SP))
Devang Pateld2f79a12009-07-28 19:55:13 +00001072 return false;
1073
Devang Patel2db49d72010-05-07 18:11:54 +00001074 SPs.push_back(SP);
Devang Pateld2f79a12009-07-28 19:55:13 +00001075 return true;
1076}
1077
Chris Lattner099b7792009-12-29 09:22:47 +00001078/// getDISubprogram - Find subprogram that is enclosing this scope.
Devang Patele9f8f5e2010-05-07 20:54:48 +00001079DISubprogram llvm::getDISubprogram(const MDNode *Scope) {
Chris Lattner099b7792009-12-29 09:22:47 +00001080 DIDescriptor D(Scope);
Chris Lattner099b7792009-12-29 09:22:47 +00001081 if (D.isSubprogram())
1082 return DISubprogram(Scope);
Jim Grosbache62b6902010-07-21 21:36:25 +00001083
Chris Lattner099b7792009-12-29 09:22:47 +00001084 if (D.isLexicalBlock())
Devang Patel2db49d72010-05-07 18:11:54 +00001085 return getDISubprogram(DILexicalBlock(Scope).getContext());
Jim Grosbache62b6902010-07-21 21:36:25 +00001086
Chris Lattner099b7792009-12-29 09:22:47 +00001087 return DISubprogram();
1088}
Devang Patel193f7202009-11-24 01:14:22 +00001089
Chris Lattner099b7792009-12-29 09:22:47 +00001090/// getDICompositeType - Find underlying composite type.
1091DICompositeType llvm::getDICompositeType(DIType T) {
Chris Lattner099b7792009-12-29 09:22:47 +00001092 if (T.isCompositeType())
Devang Patel2db49d72010-05-07 18:11:54 +00001093 return DICompositeType(T);
Jim Grosbache62b6902010-07-21 21:36:25 +00001094
Chris Lattner099b7792009-12-29 09:22:47 +00001095 if (T.isDerivedType())
Devang Patel2db49d72010-05-07 18:11:54 +00001096 return getDICompositeType(DIDerivedType(T).getTypeDerivedFrom());
Jim Grosbache62b6902010-07-21 21:36:25 +00001097
Chris Lattner099b7792009-12-29 09:22:47 +00001098 return DICompositeType();
Torok Edwin620f2802008-12-16 09:07:36 +00001099}
Devang Patel6f9d8ff2011-08-15 17:57:41 +00001100
1101/// isSubprogramContext - Return true if Context is either a subprogram
1102/// or another context nested inside a subprogram.
1103bool llvm::isSubprogramContext(const MDNode *Context) {
1104 if (!Context)
1105 return false;
1106 DIDescriptor D(Context);
1107 if (D.isSubprogram())
1108 return true;
1109 if (D.isType())
1110 return isSubprogramContext(DIType(Context).getContext());
1111 return false;
1112}
1113