blob: b5c27933f16087fbe08ff9055bf136b16f9ccbeb [file] [log] [blame]
Bill Wendling523bea82013-11-08 08:13:15 +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
Chandler Carruth9a4c9e52014-03-06 00:46:21 +000015#include "llvm/IR/DebugInfo.h"
Chandler Carruth442f7842014-03-04 10:07:28 +000016#include "LLVMContextImpl.h"
Bill Wendling523bea82013-11-08 08:13:15 +000017#include "llvm/ADT/STLExtras.h"
18#include "llvm/ADT/SmallPtrSet.h"
19#include "llvm/ADT/SmallString.h"
20#include "llvm/Analysis/ValueTracking.h"
21#include "llvm/IR/Constants.h"
Adrian Prantlb1416832014-08-01 22:11:58 +000022#include "llvm/IR/DIBuilder.h"
Bill Wendling523bea82013-11-08 08:13:15 +000023#include "llvm/IR/DerivedTypes.h"
24#include "llvm/IR/Instructions.h"
25#include "llvm/IR/IntrinsicInst.h"
26#include "llvm/IR/Intrinsics.h"
27#include "llvm/IR/Module.h"
Chandler Carruth4220e9c2014-03-04 11:17:44 +000028#include "llvm/IR/ValueHandle.h"
Bill Wendling523bea82013-11-08 08:13:15 +000029#include "llvm/Support/Debug.h"
30#include "llvm/Support/Dwarf.h"
Bill Wendling523bea82013-11-08 08:13:15 +000031#include "llvm/Support/raw_ostream.h"
32using namespace llvm;
33using namespace llvm::dwarf;
34
35//===----------------------------------------------------------------------===//
36// DIDescriptor
37//===----------------------------------------------------------------------===//
38
39bool DIDescriptor::Verify() const {
40 return DbgNode &&
41 (DIDerivedType(DbgNode).Verify() ||
42 DICompositeType(DbgNode).Verify() || DIBasicType(DbgNode).Verify() ||
43 DIVariable(DbgNode).Verify() || DISubprogram(DbgNode).Verify() ||
44 DIGlobalVariable(DbgNode).Verify() || DIFile(DbgNode).Verify() ||
45 DICompileUnit(DbgNode).Verify() || DINameSpace(DbgNode).Verify() ||
46 DILexicalBlock(DbgNode).Verify() ||
47 DILexicalBlockFile(DbgNode).Verify() ||
48 DISubrange(DbgNode).Verify() || DIEnumerator(DbgNode).Verify() ||
49 DIObjCProperty(DbgNode).Verify() ||
50 DITemplateTypeParameter(DbgNode).Verify() ||
51 DITemplateValueParameter(DbgNode).Verify() ||
Adrian Prantl87b7eb92014-10-01 18:55:02 +000052 DIImportedEntity(DbgNode).Verify() || DIExpression(DbgNode).Verify());
Bill Wendling523bea82013-11-08 08:13:15 +000053}
54
55static Value *getField(const MDNode *DbgNode, unsigned Elt) {
Craig Topperc6207612014-04-09 06:08:46 +000056 if (!DbgNode || Elt >= DbgNode->getNumOperands())
57 return nullptr;
Bill Wendling523bea82013-11-08 08:13:15 +000058 return DbgNode->getOperand(Elt);
59}
60
61static MDNode *getNodeField(const MDNode *DbgNode, unsigned Elt) {
62 return dyn_cast_or_null<MDNode>(getField(DbgNode, Elt));
63}
64
65static StringRef getStringField(const MDNode *DbgNode, unsigned Elt) {
66 if (MDString *MDS = dyn_cast_or_null<MDString>(getField(DbgNode, Elt)))
67 return MDS->getString();
68 return StringRef();
69}
70
71StringRef DIDescriptor::getStringField(unsigned Elt) const {
72 return ::getStringField(DbgNode, Elt);
73}
74
75uint64_t DIDescriptor::getUInt64Field(unsigned Elt) const {
Craig Topperc6207612014-04-09 06:08:46 +000076 if (!DbgNode)
Bill Wendling523bea82013-11-08 08:13:15 +000077 return 0;
78
79 if (Elt < DbgNode->getNumOperands())
80 if (ConstantInt *CI =
81 dyn_cast_or_null<ConstantInt>(DbgNode->getOperand(Elt)))
82 return CI->getZExtValue();
83
84 return 0;
85}
86
87int64_t DIDescriptor::getInt64Field(unsigned Elt) const {
Craig Topperc6207612014-04-09 06:08:46 +000088 if (!DbgNode)
Bill Wendling523bea82013-11-08 08:13:15 +000089 return 0;
90
91 if (Elt < DbgNode->getNumOperands())
92 if (ConstantInt *CI =
93 dyn_cast_or_null<ConstantInt>(DbgNode->getOperand(Elt)))
94 return CI->getSExtValue();
95
96 return 0;
97}
98
99DIDescriptor DIDescriptor::getDescriptorField(unsigned Elt) const {
100 MDNode *Field = getNodeField(DbgNode, Elt);
101 return DIDescriptor(Field);
102}
103
104GlobalVariable *DIDescriptor::getGlobalVariableField(unsigned Elt) const {
Craig Topperc6207612014-04-09 06:08:46 +0000105 if (!DbgNode)
106 return nullptr;
Bill Wendling523bea82013-11-08 08:13:15 +0000107
108 if (Elt < DbgNode->getNumOperands())
109 return dyn_cast_or_null<GlobalVariable>(DbgNode->getOperand(Elt));
Craig Topperc6207612014-04-09 06:08:46 +0000110 return nullptr;
Bill Wendling523bea82013-11-08 08:13:15 +0000111}
112
113Constant *DIDescriptor::getConstantField(unsigned Elt) const {
Craig Topperc6207612014-04-09 06:08:46 +0000114 if (!DbgNode)
115 return nullptr;
Bill Wendling523bea82013-11-08 08:13:15 +0000116
117 if (Elt < DbgNode->getNumOperands())
118 return dyn_cast_or_null<Constant>(DbgNode->getOperand(Elt));
Craig Topperc6207612014-04-09 06:08:46 +0000119 return nullptr;
Bill Wendling523bea82013-11-08 08:13:15 +0000120}
121
122Function *DIDescriptor::getFunctionField(unsigned Elt) const {
Craig Topperc6207612014-04-09 06:08:46 +0000123 if (!DbgNode)
124 return nullptr;
Bill Wendling523bea82013-11-08 08:13:15 +0000125
126 if (Elt < DbgNode->getNumOperands())
127 return dyn_cast_or_null<Function>(DbgNode->getOperand(Elt));
Craig Topperc6207612014-04-09 06:08:46 +0000128 return nullptr;
Bill Wendling523bea82013-11-08 08:13:15 +0000129}
130
131void DIDescriptor::replaceFunctionField(unsigned Elt, Function *F) {
Craig Topperc6207612014-04-09 06:08:46 +0000132 if (!DbgNode)
Bill Wendling523bea82013-11-08 08:13:15 +0000133 return;
134
135 if (Elt < DbgNode->getNumOperands()) {
136 MDNode *Node = const_cast<MDNode *>(DbgNode);
137 Node->replaceOperandWith(Elt, F);
138 }
139}
140
Duncan P. N. Exon Smith176b6912014-10-03 20:01:09 +0000141static unsigned DIVariableInlinedAtIndex = 4;
142MDNode *DIVariable::getInlinedAt() const {
143 return getNodeField(DbgNode, DIVariableInlinedAtIndex);
144}
Bill Wendling523bea82013-11-08 08:13:15 +0000145
Adrian Prantlb1416832014-08-01 22:11:58 +0000146/// Return the size reported by the variable's type.
147unsigned DIVariable::getSizeInBits(const DITypeIdentifierMap &Map) {
148 DIType Ty = getType().resolve(Map);
149 // Follow derived types until we reach a type that
150 // reports back a size.
151 while (Ty.isDerivedType() && !Ty.getSizeInBits()) {
152 DIDerivedType DT(&*Ty);
153 Ty = DT.getTypeDerivedFrom().resolve(Map);
154 }
155 assert(Ty.getSizeInBits() && "type with size 0");
156 return Ty.getSizeInBits();
157}
158
Adrian Prantl87b7eb92014-10-01 18:55:02 +0000159uint64_t DIExpression::getElement(unsigned Idx) const {
160 unsigned I = Idx + 1;
Duncan P. N. Exon Smith176b6912014-10-03 20:01:09 +0000161 assert(I < getNumHeaderFields() &&
162 "non-existing complex address element requested");
163 return getHeaderFieldAs<int64_t>(I);
Adrian Prantl87b7eb92014-10-01 18:55:02 +0000164}
Adrian Prantlb1416832014-08-01 22:11:58 +0000165
Adrian Prantl87b7eb92014-10-01 18:55:02 +0000166bool DIExpression::isVariablePiece() const {
167 return getNumElements() && getElement(0) == dwarf::DW_OP_piece;
168}
169
170uint64_t DIExpression::getPieceOffset() const {
171 assert(isVariablePiece());
172 return getElement(1);
173}
174
175uint64_t DIExpression::getPieceSize() const {
176 assert(isVariablePiece());
177 return getElement(2);
178}
Adrian Prantlb1416832014-08-01 22:11:58 +0000179
Bill Wendling523bea82013-11-08 08:13:15 +0000180//===----------------------------------------------------------------------===//
181// Predicates
182//===----------------------------------------------------------------------===//
183
Manman Renf8a19672014-07-28 22:24:06 +0000184bool DIDescriptor::isSubroutineType() const {
185 return isCompositeType() && getTag() == dwarf::DW_TAG_subroutine_type;
186}
187
Bill Wendling523bea82013-11-08 08:13:15 +0000188/// isBasicType - Return true if the specified tag is legal for
189/// DIBasicType.
190bool DIDescriptor::isBasicType() const {
191 if (!DbgNode)
192 return false;
193 switch (getTag()) {
194 case dwarf::DW_TAG_base_type:
195 case dwarf::DW_TAG_unspecified_type:
196 return true;
197 default:
198 return false;
199 }
200}
201
202/// isDerivedType - Return true if the specified tag is legal for DIDerivedType.
203bool DIDescriptor::isDerivedType() const {
204 if (!DbgNode)
205 return false;
206 switch (getTag()) {
207 case dwarf::DW_TAG_typedef:
208 case dwarf::DW_TAG_pointer_type:
209 case dwarf::DW_TAG_ptr_to_member_type:
210 case dwarf::DW_TAG_reference_type:
211 case dwarf::DW_TAG_rvalue_reference_type:
212 case dwarf::DW_TAG_const_type:
213 case dwarf::DW_TAG_volatile_type:
214 case dwarf::DW_TAG_restrict_type:
215 case dwarf::DW_TAG_member:
216 case dwarf::DW_TAG_inheritance:
217 case dwarf::DW_TAG_friend:
218 return true;
219 default:
220 // CompositeTypes are currently modelled as DerivedTypes.
221 return isCompositeType();
222 }
223}
224
225/// isCompositeType - Return true if the specified tag is legal for
226/// DICompositeType.
227bool DIDescriptor::isCompositeType() const {
228 if (!DbgNode)
229 return false;
230 switch (getTag()) {
231 case dwarf::DW_TAG_array_type:
232 case dwarf::DW_TAG_structure_type:
233 case dwarf::DW_TAG_union_type:
234 case dwarf::DW_TAG_enumeration_type:
235 case dwarf::DW_TAG_subroutine_type:
236 case dwarf::DW_TAG_class_type:
237 return true;
238 default:
239 return false;
240 }
241}
242
243/// isVariable - Return true if the specified tag is legal for DIVariable.
244bool DIDescriptor::isVariable() const {
245 if (!DbgNode)
246 return false;
247 switch (getTag()) {
248 case dwarf::DW_TAG_auto_variable:
249 case dwarf::DW_TAG_arg_variable:
250 return true;
251 default:
252 return false;
253 }
254}
255
256/// isType - Return true if the specified tag is legal for DIType.
257bool DIDescriptor::isType() const {
Manman Renf93ac4b2014-07-29 18:20:39 +0000258 return isBasicType() || isCompositeType() || isDerivedType();
Bill Wendling523bea82013-11-08 08:13:15 +0000259}
260
261/// isSubprogram - Return true if the specified tag is legal for
262/// DISubprogram.
263bool DIDescriptor::isSubprogram() const {
264 return DbgNode && getTag() == dwarf::DW_TAG_subprogram;
265}
266
267/// isGlobalVariable - Return true if the specified tag is legal for
268/// DIGlobalVariable.
269bool DIDescriptor::isGlobalVariable() const {
270 return DbgNode && (getTag() == dwarf::DW_TAG_variable ||
271 getTag() == dwarf::DW_TAG_constant);
272}
273
Bill Wendling523bea82013-11-08 08:13:15 +0000274/// isScope - Return true if the specified tag is one of the scope
275/// related tag.
276bool DIDescriptor::isScope() const {
277 if (!DbgNode)
278 return false;
279 switch (getTag()) {
280 case dwarf::DW_TAG_compile_unit:
281 case dwarf::DW_TAG_lexical_block:
282 case dwarf::DW_TAG_subprogram:
283 case dwarf::DW_TAG_namespace:
284 case dwarf::DW_TAG_file_type:
285 return true;
286 default:
287 break;
288 }
289 return isType();
290}
291
292/// isTemplateTypeParameter - Return true if the specified tag is
293/// DW_TAG_template_type_parameter.
294bool DIDescriptor::isTemplateTypeParameter() const {
295 return DbgNode && getTag() == dwarf::DW_TAG_template_type_parameter;
296}
297
298/// isTemplateValueParameter - Return true if the specified tag is
299/// DW_TAG_template_value_parameter.
300bool DIDescriptor::isTemplateValueParameter() const {
301 return DbgNode && (getTag() == dwarf::DW_TAG_template_value_parameter ||
302 getTag() == dwarf::DW_TAG_GNU_template_template_param ||
303 getTag() == dwarf::DW_TAG_GNU_template_parameter_pack);
304}
305
306/// isCompileUnit - Return true if the specified tag is DW_TAG_compile_unit.
307bool DIDescriptor::isCompileUnit() const {
308 return DbgNode && getTag() == dwarf::DW_TAG_compile_unit;
309}
310
311/// isFile - Return true if the specified tag is DW_TAG_file_type.
312bool DIDescriptor::isFile() const {
313 return DbgNode && getTag() == dwarf::DW_TAG_file_type;
314}
315
316/// isNameSpace - Return true if the specified tag is DW_TAG_namespace.
317bool DIDescriptor::isNameSpace() const {
318 return DbgNode && getTag() == dwarf::DW_TAG_namespace;
319}
320
321/// isLexicalBlockFile - Return true if the specified descriptor is a
322/// lexical block with an extra file.
323bool DIDescriptor::isLexicalBlockFile() const {
324 return DbgNode && getTag() == dwarf::DW_TAG_lexical_block &&
Duncan P. N. Exon Smith176b6912014-10-03 20:01:09 +0000325 DbgNode->getNumOperands() == 3 && getNumHeaderFields() == 2;
Bill Wendling523bea82013-11-08 08:13:15 +0000326}
327
328/// isLexicalBlock - Return true if the specified tag is DW_TAG_lexical_block.
329bool DIDescriptor::isLexicalBlock() const {
Duncan P. N. Exon Smith176b6912014-10-03 20:01:09 +0000330 // FIXME: There are always exactly 4 header fields in DILexicalBlock, but
331 // something relies on this returning true for DILexicalBlockFile.
Bill Wendling523bea82013-11-08 08:13:15 +0000332 return DbgNode && getTag() == dwarf::DW_TAG_lexical_block &&
Duncan P. N. Exon Smith176b6912014-10-03 20:01:09 +0000333 DbgNode->getNumOperands() == 3 &&
334 (getNumHeaderFields() == 2 || getNumHeaderFields() == 4);
Bill Wendling523bea82013-11-08 08:13:15 +0000335}
336
337/// isSubrange - Return true if the specified tag is DW_TAG_subrange_type.
338bool DIDescriptor::isSubrange() const {
339 return DbgNode && getTag() == dwarf::DW_TAG_subrange_type;
340}
341
342/// isEnumerator - Return true if the specified tag is DW_TAG_enumerator.
343bool DIDescriptor::isEnumerator() const {
344 return DbgNode && getTag() == dwarf::DW_TAG_enumerator;
345}
346
347/// isObjCProperty - Return true if the specified tag is DW_TAG_APPLE_property.
348bool DIDescriptor::isObjCProperty() const {
349 return DbgNode && getTag() == dwarf::DW_TAG_APPLE_property;
350}
351
352/// \brief Return true if the specified tag is DW_TAG_imported_module or
353/// DW_TAG_imported_declaration.
354bool DIDescriptor::isImportedEntity() const {
355 return DbgNode && (getTag() == dwarf::DW_TAG_imported_module ||
356 getTag() == dwarf::DW_TAG_imported_declaration);
357}
358
Adrian Prantl87b7eb92014-10-01 18:55:02 +0000359/// \brief Return true if the specified tag is DW_TAG_imported_module or
360/// DW_TAG_imported_declaration.
361bool DIDescriptor::isExpression() const {
362 return DbgNode && (getTag() == dwarf::DW_TAG_expression);
363}
364
Bill Wendling523bea82013-11-08 08:13:15 +0000365//===----------------------------------------------------------------------===//
366// Simple Descriptor Constructors and other Methods
367//===----------------------------------------------------------------------===//
368
Bill Wendling523bea82013-11-08 08:13:15 +0000369/// replaceAllUsesWith - Replace all uses of the MDNode used by this
370/// type with the one in the passed descriptor.
Frederic Riss36acf0f2014-09-15 07:50:36 +0000371void DIDescriptor::replaceAllUsesWith(LLVMContext &VMContext, DIDescriptor D) {
Bill Wendling523bea82013-11-08 08:13:15 +0000372
373 assert(DbgNode && "Trying to replace an unverified type!");
374
375 // Since we use a TrackingVH for the node, its easy for clients to manufacture
376 // legitimate situations where they want to replaceAllUsesWith() on something
377 // which, due to uniquing, has merged with the source. We shield clients from
378 // this detail by allowing a value to be replaced with replaceAllUsesWith()
379 // itself.
David Blaikied3f094a2014-05-06 03:41:57 +0000380 const MDNode *DN = D;
381 if (DbgNode == DN) {
382 SmallVector<Value*, 10> Ops(DbgNode->getNumOperands());
383 for (size_t i = 0; i != Ops.size(); ++i)
384 Ops[i] = DbgNode->getOperand(i);
385 DN = MDNode::get(VMContext, Ops);
Bill Wendling523bea82013-11-08 08:13:15 +0000386 }
David Blaikied3f094a2014-05-06 03:41:57 +0000387
388 MDNode *Node = const_cast<MDNode *>(DbgNode);
389 const Value *V = cast_or_null<Value>(DN);
390 Node->replaceAllUsesWith(const_cast<Value *>(V));
391 MDNode::deleteTemporary(Node);
Frederic Rissdd7aec52014-09-15 07:50:42 +0000392 DbgNode = DN;
Bill Wendling523bea82013-11-08 08:13:15 +0000393}
394
395/// replaceAllUsesWith - Replace all uses of the MDNode used by this
396/// type with the one in D.
Frederic Riss36acf0f2014-09-15 07:50:36 +0000397void DIDescriptor::replaceAllUsesWith(MDNode *D) {
Bill Wendling523bea82013-11-08 08:13:15 +0000398
399 assert(DbgNode && "Trying to replace an unverified type!");
David Blaikied3f094a2014-05-06 03:41:57 +0000400 assert(DbgNode != D && "This replacement should always happen");
401 MDNode *Node = const_cast<MDNode *>(DbgNode);
402 const MDNode *DN = D;
403 const Value *V = cast_or_null<Value>(DN);
404 Node->replaceAllUsesWith(const_cast<Value *>(V));
405 MDNode::deleteTemporary(Node);
Bill Wendling523bea82013-11-08 08:13:15 +0000406}
407
408/// Verify - Verify that a compile unit is well formed.
409bool DICompileUnit::Verify() const {
410 if (!isCompileUnit())
411 return false;
412
413 // Don't bother verifying the compilation directory or producer string
414 // as those could be empty.
415 if (getFilename().empty())
416 return false;
417
Duncan P. N. Exon Smith176b6912014-10-03 20:01:09 +0000418 return DbgNode->getNumOperands() == 7 && getNumHeaderFields() == 8;
Bill Wendling523bea82013-11-08 08:13:15 +0000419}
420
421/// Verify - Verify that an ObjC property is well formed.
422bool DIObjCProperty::Verify() const {
423 if (!isObjCProperty())
424 return false;
425
426 // Don't worry about the rest of the strings for now.
Duncan P. N. Exon Smith176b6912014-10-03 20:01:09 +0000427 return DbgNode->getNumOperands() == 3 && getNumHeaderFields() == 6;
Bill Wendling523bea82013-11-08 08:13:15 +0000428}
429
430/// Check if a field at position Elt of a MDNode is a MDNode.
431/// We currently allow an empty string and an integer.
432/// But we don't allow a non-empty string in a MDNode field.
433static bool fieldIsMDNode(const MDNode *DbgNode, unsigned Elt) {
434 // FIXME: This function should return true, if the field is null or the field
435 // is indeed a MDNode: return !Fld || isa<MDNode>(Fld).
436 Value *Fld = getField(DbgNode, Elt);
437 if (Fld && isa<MDString>(Fld) && !cast<MDString>(Fld)->getString().empty())
438 return false;
439 return true;
440}
441
442/// Check if a field at position Elt of a MDNode is a MDString.
443static bool fieldIsMDString(const MDNode *DbgNode, unsigned Elt) {
444 Value *Fld = getField(DbgNode, Elt);
445 return !Fld || isa<MDString>(Fld);
446}
447
448/// Check if a value can be a reference to a type.
449static bool isTypeRef(const Value *Val) {
450 return !Val ||
451 (isa<MDString>(Val) && !cast<MDString>(Val)->getString().empty()) ||
452 (isa<MDNode>(Val) && DIType(cast<MDNode>(Val)).isType());
453}
454
455/// Check if a field at position Elt of a MDNode can be a reference to a type.
456static bool fieldIsTypeRef(const MDNode *DbgNode, unsigned Elt) {
457 Value *Fld = getField(DbgNode, Elt);
458 return isTypeRef(Fld);
459}
460
461/// Check if a value can be a ScopeRef.
462static bool isScopeRef(const Value *Val) {
463 return !Val ||
Adrian Prantl6b444c52014-04-01 21:04:24 +0000464 (isa<MDString>(Val) && !cast<MDString>(Val)->getString().empty()) ||
465 // Not checking for Val->isScope() here, because it would work
466 // only for lexical scopes and not all subclasses of DIScope.
467 isa<MDNode>(Val);
Bill Wendling523bea82013-11-08 08:13:15 +0000468}
469
470/// Check if a field at position Elt of a MDNode can be a ScopeRef.
471static bool fieldIsScopeRef(const MDNode *DbgNode, unsigned Elt) {
472 Value *Fld = getField(DbgNode, Elt);
473 return isScopeRef(Fld);
474}
475
476/// Verify - Verify that a type descriptor is well formed.
477bool DIType::Verify() const {
478 if (!isType())
479 return false;
480 // Make sure Context @ field 2 is MDNode.
481 if (!fieldIsScopeRef(DbgNode, 2))
482 return false;
483
484 // FIXME: Sink this into the various subclass verifies.
485 uint16_t Tag = getTag();
Manman Renf93ac4b2014-07-29 18:20:39 +0000486 if (!isBasicType() && Tag != dwarf::DW_TAG_const_type &&
Bill Wendling523bea82013-11-08 08:13:15 +0000487 Tag != dwarf::DW_TAG_volatile_type && Tag != dwarf::DW_TAG_pointer_type &&
488 Tag != dwarf::DW_TAG_ptr_to_member_type &&
489 Tag != dwarf::DW_TAG_reference_type &&
490 Tag != dwarf::DW_TAG_rvalue_reference_type &&
491 Tag != dwarf::DW_TAG_restrict_type && Tag != dwarf::DW_TAG_array_type &&
492 Tag != dwarf::DW_TAG_enumeration_type &&
493 Tag != dwarf::DW_TAG_subroutine_type &&
494 Tag != dwarf::DW_TAG_inheritance && Tag != dwarf::DW_TAG_friend &&
495 getFilename().empty())
496 return false;
Adrian Prantldaedfda2014-08-29 22:44:07 +0000497
Bill Wendling523bea82013-11-08 08:13:15 +0000498 // DIType is abstract, it should be a BasicType, a DerivedType or
499 // a CompositeType.
500 if (isBasicType())
Renato Golin47f46fd2013-11-26 16:47:00 +0000501 return DIBasicType(DbgNode).Verify();
Bill Wendling523bea82013-11-08 08:13:15 +0000502 else if (isCompositeType())
Renato Golin47f46fd2013-11-26 16:47:00 +0000503 return DICompositeType(DbgNode).Verify();
Bill Wendling523bea82013-11-08 08:13:15 +0000504 else if (isDerivedType())
Renato Golin47f46fd2013-11-26 16:47:00 +0000505 return DIDerivedType(DbgNode).Verify();
Bill Wendling523bea82013-11-08 08:13:15 +0000506 else
507 return false;
Bill Wendling523bea82013-11-08 08:13:15 +0000508}
509
510/// Verify - Verify that a basic type descriptor is well formed.
511bool DIBasicType::Verify() const {
Duncan P. N. Exon Smith176b6912014-10-03 20:01:09 +0000512 return isBasicType() && DbgNode->getNumOperands() == 3 &&
513 getNumHeaderFields() == 8;
Bill Wendling523bea82013-11-08 08:13:15 +0000514}
515
516/// Verify - Verify that a derived type descriptor is well formed.
517bool DIDerivedType::Verify() const {
Duncan P. N. Exon Smith176b6912014-10-03 20:01:09 +0000518 // Make sure DerivedFrom @ field 3 is TypeRef.
519 if (!fieldIsTypeRef(DbgNode, 3))
Bill Wendling523bea82013-11-08 08:13:15 +0000520 return false;
521 if (getTag() == dwarf::DW_TAG_ptr_to_member_type)
Duncan P. N. Exon Smith176b6912014-10-03 20:01:09 +0000522 // Make sure ClassType @ field 4 is a TypeRef.
523 if (!fieldIsTypeRef(DbgNode, 4))
Bill Wendling523bea82013-11-08 08:13:15 +0000524 return false;
525
Duncan P. N. Exon Smith176b6912014-10-03 20:01:09 +0000526 return isDerivedType() && DbgNode->getNumOperands() >= 4 &&
527 DbgNode->getNumOperands() <= 8 && getNumHeaderFields() >= 7 &&
528 getNumHeaderFields() <= 8;
Bill Wendling523bea82013-11-08 08:13:15 +0000529}
530
531/// Verify - Verify that a composite type descriptor is well formed.
532bool DICompositeType::Verify() const {
533 if (!isCompositeType())
534 return false;
535
Duncan P. N. Exon Smith176b6912014-10-03 20:01:09 +0000536 // Make sure DerivedFrom @ field 3 and ContainingType @ field 5 are TypeRef.
537 if (!fieldIsTypeRef(DbgNode, 3))
Bill Wendling523bea82013-11-08 08:13:15 +0000538 return false;
Duncan P. N. Exon Smith176b6912014-10-03 20:01:09 +0000539 if (!fieldIsTypeRef(DbgNode, 5))
Bill Wendling523bea82013-11-08 08:13:15 +0000540 return false;
541
Duncan P. N. Exon Smith176b6912014-10-03 20:01:09 +0000542 // Make sure the type identifier at field 7 is MDString, it can be null.
543 if (!fieldIsMDString(DbgNode, 7))
Bill Wendling523bea82013-11-08 08:13:15 +0000544 return false;
545
Adrian Prantl99c7af22013-12-18 21:48:19 +0000546 // A subroutine type can't be both & and &&.
547 if (isLValueReference() && isRValueReference())
548 return false;
549
Duncan P. N. Exon Smith176b6912014-10-03 20:01:09 +0000550 return DbgNode->getNumOperands() == 8 && getNumHeaderFields() == 8;
Bill Wendling523bea82013-11-08 08:13:15 +0000551}
552
553/// Verify - Verify that a subprogram descriptor is well formed.
554bool DISubprogram::Verify() const {
555 if (!isSubprogram())
556 return false;
557
Duncan P. N. Exon Smith176b6912014-10-03 20:01:09 +0000558 // Make sure context @ field 2 is a ScopeRef and type @ field 3 is a MDNode.
Bill Wendling523bea82013-11-08 08:13:15 +0000559 if (!fieldIsScopeRef(DbgNode, 2))
560 return false;
Duncan P. N. Exon Smith176b6912014-10-03 20:01:09 +0000561 if (!fieldIsMDNode(DbgNode, 3))
Bill Wendling523bea82013-11-08 08:13:15 +0000562 return false;
Duncan P. N. Exon Smith176b6912014-10-03 20:01:09 +0000563 // Containing type @ field 4.
564 if (!fieldIsTypeRef(DbgNode, 4))
Bill Wendling523bea82013-11-08 08:13:15 +0000565 return false;
Adrian Prantl99c7af22013-12-18 21:48:19 +0000566
567 // A subprogram can't be both & and &&.
568 if (isLValueReference() && isRValueReference())
569 return false;
570
Duncan P. N. Exon Smith176b6912014-10-03 20:01:09 +0000571 return DbgNode->getNumOperands() == 9 && getNumHeaderFields() == 12;
Bill Wendling523bea82013-11-08 08:13:15 +0000572}
573
574/// Verify - Verify that a global variable descriptor is well formed.
575bool DIGlobalVariable::Verify() const {
576 if (!isGlobalVariable())
577 return false;
578
579 if (getDisplayName().empty())
580 return false;
Duncan P. N. Exon Smith176b6912014-10-03 20:01:09 +0000581 // Make sure context @ field 1 is an MDNode.
582 if (!fieldIsMDNode(DbgNode, 1))
Bill Wendling523bea82013-11-08 08:13:15 +0000583 return false;
Duncan P. N. Exon Smith176b6912014-10-03 20:01:09 +0000584 // Make sure that type @ field 3 is a DITypeRef.
585 if (!fieldIsTypeRef(DbgNode, 3))
Bill Wendling523bea82013-11-08 08:13:15 +0000586 return false;
Duncan P. N. Exon Smith176b6912014-10-03 20:01:09 +0000587 // Make sure StaticDataMemberDeclaration @ field 5 is MDNode.
588 if (!fieldIsMDNode(DbgNode, 5))
Bill Wendling523bea82013-11-08 08:13:15 +0000589 return false;
590
Duncan P. N. Exon Smith176b6912014-10-03 20:01:09 +0000591 return DbgNode->getNumOperands() == 6 && getNumHeaderFields() == 7;
Bill Wendling523bea82013-11-08 08:13:15 +0000592}
593
594/// Verify - Verify that a variable descriptor is well formed.
595bool DIVariable::Verify() const {
596 if (!isVariable())
597 return false;
598
Adrian Prantl1a1647c2014-03-18 02:34:58 +0000599 // Make sure context @ field 1 is an MDNode.
Bill Wendling523bea82013-11-08 08:13:15 +0000600 if (!fieldIsMDNode(DbgNode, 1))
601 return false;
Duncan P. N. Exon Smith176b6912014-10-03 20:01:09 +0000602 // Make sure that type @ field 3 is a DITypeRef.
603 if (!fieldIsTypeRef(DbgNode, 3))
604 return false;
605
606 // Check the number of header fields, which is common between complex and
607 // simple variables.
608 if (getNumHeaderFields() != 4)
Bill Wendling523bea82013-11-08 08:13:15 +0000609 return false;
Adrian Prantlda7d92e2014-06-30 17:17:35 +0000610
Adrian Prantl87b7eb92014-10-01 18:55:02 +0000611 // Variable without an inline location.
Duncan P. N. Exon Smith176b6912014-10-03 20:01:09 +0000612 if (DbgNode->getNumOperands() == 4)
Adrian Prantlda7d92e2014-06-30 17:17:35 +0000613 return true;
614
Duncan P. N. Exon Smith176b6912014-10-03 20:01:09 +0000615 // Variable with an inline location.
616 return getInlinedAt() != nullptr && DbgNode->getNumOperands() == 5;
Adrian Prantl87b7eb92014-10-01 18:55:02 +0000617}
618
619/// Verify - Verify that a variable descriptor is well formed.
620bool DIExpression::Verify() const {
621 // Empty DIExpressions may be represented as a nullptr.
622 if (!DbgNode)
623 return true;
624
Duncan P. N. Exon Smith176b6912014-10-03 20:01:09 +0000625 return isExpression() && DbgNode->getNumOperands() == 1;
Bill Wendling523bea82013-11-08 08:13:15 +0000626}
627
628/// Verify - Verify that a location descriptor is well formed.
629bool DILocation::Verify() const {
630 if (!DbgNode)
631 return false;
632
633 return DbgNode->getNumOperands() == 4;
634}
635
636/// Verify - Verify that a namespace descriptor is well formed.
637bool DINameSpace::Verify() const {
638 if (!isNameSpace())
639 return false;
Duncan P. N. Exon Smith176b6912014-10-03 20:01:09 +0000640 return DbgNode->getNumOperands() == 3 && getNumHeaderFields() == 3;
Bill Wendling523bea82013-11-08 08:13:15 +0000641}
642
643/// \brief Retrieve the MDNode for the directory/file pair.
644MDNode *DIFile::getFileNode() const { return getNodeField(DbgNode, 1); }
645
646/// \brief Verify that the file descriptor is well formed.
647bool DIFile::Verify() const {
648 return isFile() && DbgNode->getNumOperands() == 2;
649}
650
651/// \brief Verify that the enumerator descriptor is well formed.
652bool DIEnumerator::Verify() const {
Duncan P. N. Exon Smith176b6912014-10-03 20:01:09 +0000653 return isEnumerator() && DbgNode->getNumOperands() == 1 &&
654 getNumHeaderFields() == 3;
Bill Wendling523bea82013-11-08 08:13:15 +0000655}
656
657/// \brief Verify that the subrange descriptor is well formed.
658bool DISubrange::Verify() const {
Duncan P. N. Exon Smith176b6912014-10-03 20:01:09 +0000659 return isSubrange() && DbgNode->getNumOperands() == 1 &&
660 getNumHeaderFields() == 3;
Bill Wendling523bea82013-11-08 08:13:15 +0000661}
662
663/// \brief Verify that the lexical block descriptor is well formed.
664bool DILexicalBlock::Verify() const {
Duncan P. N. Exon Smith176b6912014-10-03 20:01:09 +0000665 return isLexicalBlock() && DbgNode->getNumOperands() == 3 &&
666 getNumHeaderFields() == 4;
Bill Wendling523bea82013-11-08 08:13:15 +0000667}
668
669/// \brief Verify that the file-scoped lexical block descriptor is well formed.
670bool DILexicalBlockFile::Verify() const {
Duncan P. N. Exon Smith176b6912014-10-03 20:01:09 +0000671 return isLexicalBlockFile() && DbgNode->getNumOperands() == 3 &&
672 getNumHeaderFields() == 2;
Bill Wendling523bea82013-11-08 08:13:15 +0000673}
674
675/// \brief Verify that the template type parameter descriptor is well formed.
676bool DITemplateTypeParameter::Verify() const {
Duncan P. N. Exon Smith176b6912014-10-03 20:01:09 +0000677 return isTemplateTypeParameter() && DbgNode->getNumOperands() == 4 &&
678 getNumHeaderFields() == 4;
Bill Wendling523bea82013-11-08 08:13:15 +0000679}
680
681/// \brief Verify that the template value parameter descriptor is well formed.
682bool DITemplateValueParameter::Verify() const {
Duncan P. N. Exon Smith176b6912014-10-03 20:01:09 +0000683 return isTemplateValueParameter() && DbgNode->getNumOperands() == 5 &&
684 getNumHeaderFields() == 4;
Bill Wendling523bea82013-11-08 08:13:15 +0000685}
686
687/// \brief Verify that the imported module descriptor is well formed.
688bool DIImportedEntity::Verify() const {
Duncan P. N. Exon Smith176b6912014-10-03 20:01:09 +0000689 return isImportedEntity() && DbgNode->getNumOperands() == 3 &&
690 getNumHeaderFields() == 3;
Bill Wendling523bea82013-11-08 08:13:15 +0000691}
692
693/// getObjCProperty - Return property node, if this ivar is associated with one.
694MDNode *DIDerivedType::getObjCProperty() const {
Duncan P. N. Exon Smith176b6912014-10-03 20:01:09 +0000695 return getNodeField(DbgNode, 4);
Bill Wendling523bea82013-11-08 08:13:15 +0000696}
697
698MDString *DICompositeType::getIdentifier() const {
Duncan P. N. Exon Smith176b6912014-10-03 20:01:09 +0000699 return cast_or_null<MDString>(getField(DbgNode, 7));
Bill Wendling523bea82013-11-08 08:13:15 +0000700}
701
702#ifndef NDEBUG
703static void VerifySubsetOf(const MDNode *LHS, const MDNode *RHS) {
704 for (unsigned i = 0; i != LHS->getNumOperands(); ++i) {
705 // Skip the 'empty' list (that's a single i32 0, rather than truly empty).
706 if (i == 0 && isa<ConstantInt>(LHS->getOperand(i)))
707 continue;
708 const MDNode *E = cast<MDNode>(LHS->getOperand(i));
709 bool found = false;
710 for (unsigned j = 0; !found && j != RHS->getNumOperands(); ++j)
711 found = E == RHS->getOperand(j);
712 assert(found && "Losing a member during member list replacement");
713 }
714}
715#endif
716
717/// \brief Set the array of member DITypes.
Manman Ren1a125c92014-07-28 19:33:20 +0000718void DICompositeType::setArraysHelper(MDNode *Elements, MDNode *TParams) {
Bill Wendling523bea82013-11-08 08:13:15 +0000719 TrackingVH<MDNode> N(*this);
720 if (Elements) {
721#ifndef NDEBUG
722 // Check that the new list of members contains all the old members as well.
Duncan P. N. Exon Smith176b6912014-10-03 20:01:09 +0000723 if (const MDNode *El = cast_or_null<MDNode>(N->getOperand(4)))
Bill Wendling523bea82013-11-08 08:13:15 +0000724 VerifySubsetOf(El, Elements);
725#endif
Duncan P. N. Exon Smith176b6912014-10-03 20:01:09 +0000726 N->replaceOperandWith(4, Elements);
Bill Wendling523bea82013-11-08 08:13:15 +0000727 }
728 if (TParams)
Duncan P. N. Exon Smith176b6912014-10-03 20:01:09 +0000729 N->replaceOperandWith(6, TParams);
Bill Wendling523bea82013-11-08 08:13:15 +0000730 DbgNode = N;
731}
732
Bill Wendling523bea82013-11-08 08:13:15 +0000733/// Generate a reference to this DIType. Uses the type identifier instead
734/// of the actual MDNode if possible, to help type uniquing.
735DIScopeRef DIScope::getRef() const {
736 if (!isCompositeType())
737 return DIScopeRef(*this);
738 DICompositeType DTy(DbgNode);
739 if (!DTy.getIdentifier())
740 return DIScopeRef(*this);
741 return DIScopeRef(DTy.getIdentifier());
742}
743
744/// \brief Set the containing type.
745void DICompositeType::setContainingType(DICompositeType ContainingType) {
746 TrackingVH<MDNode> N(*this);
Duncan P. N. Exon Smith176b6912014-10-03 20:01:09 +0000747 N->replaceOperandWith(5, ContainingType.getRef());
Bill Wendling523bea82013-11-08 08:13:15 +0000748 DbgNode = N;
749}
750
751/// isInlinedFnArgument - Return true if this variable provides debugging
752/// information for an inlined function arguments.
753bool DIVariable::isInlinedFnArgument(const Function *CurFn) {
754 assert(CurFn && "Invalid function");
755 if (!getContext().isSubprogram())
756 return false;
757 // This variable is not inlined function argument if its scope
758 // does not describe current function.
759 return !DISubprogram(getContext()).describes(CurFn);
760}
761
762/// describes - Return true if this subprogram provides debugging
763/// information for the function F.
764bool DISubprogram::describes(const Function *F) {
765 assert(F && "Invalid function");
766 if (F == getFunction())
767 return true;
768 StringRef Name = getLinkageName();
769 if (Name.empty())
770 Name = getName();
771 if (F->getName() == Name)
772 return true;
773 return false;
774}
775
Bill Wendling523bea82013-11-08 08:13:15 +0000776MDNode *DISubprogram::getVariablesNodes() const {
Duncan P. N. Exon Smith176b6912014-10-03 20:01:09 +0000777 return getNodeField(DbgNode, 8);
Bill Wendling523bea82013-11-08 08:13:15 +0000778}
779
780DIArray DISubprogram::getVariables() const {
Duncan P. N. Exon Smith176b6912014-10-03 20:01:09 +0000781 return DIArray(getNodeField(DbgNode, 8));
Bill Wendling523bea82013-11-08 08:13:15 +0000782}
783
784Value *DITemplateValueParameter::getValue() const {
Duncan P. N. Exon Smith176b6912014-10-03 20:01:09 +0000785 return getField(DbgNode, 3);
Bill Wendling523bea82013-11-08 08:13:15 +0000786}
787
788// If the current node has a parent scope then return that,
789// else return an empty scope.
790DIScopeRef DIScope::getContext() const {
791
792 if (isType())
793 return DIType(DbgNode).getContext();
794
795 if (isSubprogram())
796 return DIScopeRef(DISubprogram(DbgNode).getContext());
797
798 if (isLexicalBlock())
799 return DIScopeRef(DILexicalBlock(DbgNode).getContext());
800
801 if (isLexicalBlockFile())
802 return DIScopeRef(DILexicalBlockFile(DbgNode).getContext());
803
804 if (isNameSpace())
805 return DIScopeRef(DINameSpace(DbgNode).getContext());
806
807 assert((isFile() || isCompileUnit()) && "Unhandled type of scope.");
Craig Topperc6207612014-04-09 06:08:46 +0000808 return DIScopeRef(nullptr);
Bill Wendling523bea82013-11-08 08:13:15 +0000809}
810
811// If the scope node has a name, return that, else return an empty string.
812StringRef DIScope::getName() const {
813 if (isType())
814 return DIType(DbgNode).getName();
815 if (isSubprogram())
816 return DISubprogram(DbgNode).getName();
817 if (isNameSpace())
818 return DINameSpace(DbgNode).getName();
819 assert((isLexicalBlock() || isLexicalBlockFile() || isFile() ||
820 isCompileUnit()) &&
821 "Unhandled type of scope.");
822 return StringRef();
823}
824
825StringRef DIScope::getFilename() const {
826 if (!DbgNode)
827 return StringRef();
828 return ::getStringField(getNodeField(DbgNode, 1), 0);
829}
830
831StringRef DIScope::getDirectory() const {
832 if (!DbgNode)
833 return StringRef();
834 return ::getStringField(getNodeField(DbgNode, 1), 1);
835}
836
837DIArray DICompileUnit::getEnumTypes() const {
Duncan P. N. Exon Smith176b6912014-10-03 20:01:09 +0000838 if (!DbgNode || DbgNode->getNumOperands() < 7)
Bill Wendling523bea82013-11-08 08:13:15 +0000839 return DIArray();
840
Duncan P. N. Exon Smith176b6912014-10-03 20:01:09 +0000841 return DIArray(getNodeField(DbgNode, 2));
Bill Wendling523bea82013-11-08 08:13:15 +0000842}
843
844DIArray DICompileUnit::getRetainedTypes() const {
Duncan P. N. Exon Smith176b6912014-10-03 20:01:09 +0000845 if (!DbgNode || DbgNode->getNumOperands() < 7)
Bill Wendling523bea82013-11-08 08:13:15 +0000846 return DIArray();
847
Duncan P. N. Exon Smith176b6912014-10-03 20:01:09 +0000848 return DIArray(getNodeField(DbgNode, 3));
Bill Wendling523bea82013-11-08 08:13:15 +0000849}
850
851DIArray DICompileUnit::getSubprograms() const {
Duncan P. N. Exon Smith176b6912014-10-03 20:01:09 +0000852 if (!DbgNode || DbgNode->getNumOperands() < 7)
Bill Wendling523bea82013-11-08 08:13:15 +0000853 return DIArray();
854
Duncan P. N. Exon Smith176b6912014-10-03 20:01:09 +0000855 return DIArray(getNodeField(DbgNode, 4));
Bill Wendling523bea82013-11-08 08:13:15 +0000856}
857
858DIArray DICompileUnit::getGlobalVariables() const {
Duncan P. N. Exon Smith176b6912014-10-03 20:01:09 +0000859 if (!DbgNode || DbgNode->getNumOperands() < 7)
Bill Wendling523bea82013-11-08 08:13:15 +0000860 return DIArray();
861
Duncan P. N. Exon Smith176b6912014-10-03 20:01:09 +0000862 return DIArray(getNodeField(DbgNode, 5));
Bill Wendling523bea82013-11-08 08:13:15 +0000863}
864
865DIArray DICompileUnit::getImportedEntities() const {
Duncan P. N. Exon Smith176b6912014-10-03 20:01:09 +0000866 if (!DbgNode || DbgNode->getNumOperands() < 7)
Bill Wendling523bea82013-11-08 08:13:15 +0000867 return DIArray();
868
Duncan P. N. Exon Smith176b6912014-10-03 20:01:09 +0000869 return DIArray(getNodeField(DbgNode, 6));
870}
871
872void DICompileUnit::replaceSubprograms(DIArray Subprograms) {
873 assert(Verify() && "Expected compile unit");
874 if (Subprograms == getSubprograms())
875 return;
876
877 const_cast<MDNode *>(DbgNode)->replaceOperandWith(4, Subprograms);
878}
879
880void DICompileUnit::replaceGlobalVariables(DIArray GlobalVariables) {
881 assert(Verify() && "Expected compile unit");
882 if (GlobalVariables == getGlobalVariables())
883 return;
884
885 const_cast<MDNode *>(DbgNode)->replaceOperandWith(5, GlobalVariables);
Bill Wendling523bea82013-11-08 08:13:15 +0000886}
887
Diego Novillof5041ce2014-03-03 20:06:11 +0000888/// copyWithNewScope - Return a copy of this location, replacing the
889/// current scope with the given one.
890DILocation DILocation::copyWithNewScope(LLVMContext &Ctx,
David Blaikie2f3f76f2014-08-21 22:45:21 +0000891 DILexicalBlockFile NewScope) {
Diego Novillof5041ce2014-03-03 20:06:11 +0000892 SmallVector<Value *, 10> Elts;
893 assert(Verify());
894 for (unsigned I = 0; I < DbgNode->getNumOperands(); ++I) {
895 if (I != 2)
896 Elts.push_back(DbgNode->getOperand(I));
897 else
898 Elts.push_back(NewScope);
899 }
900 MDNode *NewDIL = MDNode::get(Ctx, Elts);
901 return DILocation(NewDIL);
902}
903
904/// computeNewDiscriminator - Generate a new discriminator value for this
905/// file and line location.
906unsigned DILocation::computeNewDiscriminator(LLVMContext &Ctx) {
907 std::pair<const char *, unsigned> Key(getFilename().data(), getLineNumber());
908 return ++Ctx.pImpl->DiscriminatorTable[Key];
909}
910
Bill Wendling523bea82013-11-08 08:13:15 +0000911/// fixupSubprogramName - Replace contains special characters used
912/// in a typical Objective-C names with '.' in a given string.
913static void fixupSubprogramName(DISubprogram Fn, SmallVectorImpl<char> &Out) {
914 StringRef FName =
915 Fn.getFunction() ? Fn.getFunction()->getName() : Fn.getName();
916 FName = Function::getRealLinkageName(FName);
917
918 StringRef Prefix("llvm.dbg.lv.");
919 Out.reserve(FName.size() + Prefix.size());
920 Out.append(Prefix.begin(), Prefix.end());
921
922 bool isObjCLike = false;
923 for (size_t i = 0, e = FName.size(); i < e; ++i) {
924 char C = FName[i];
925 if (C == '[')
926 isObjCLike = true;
927
928 if (isObjCLike && (C == '[' || C == ']' || C == ' ' || C == ':' ||
929 C == '+' || C == '(' || C == ')'))
930 Out.push_back('.');
931 else
932 Out.push_back(C);
933 }
934}
935
936/// getFnSpecificMDNode - Return a NameMDNode, if available, that is
937/// suitable to hold function specific information.
938NamedMDNode *llvm::getFnSpecificMDNode(const Module &M, DISubprogram Fn) {
939 SmallString<32> Name;
940 fixupSubprogramName(Fn, Name);
941 return M.getNamedMetadata(Name.str());
942}
943
944/// getOrInsertFnSpecificMDNode - Return a NameMDNode that is suitable
945/// to hold function specific information.
946NamedMDNode *llvm::getOrInsertFnSpecificMDNode(Module &M, DISubprogram Fn) {
947 SmallString<32> Name;
948 fixupSubprogramName(Fn, Name);
949 return M.getOrInsertNamedMetadata(Name.str());
950}
951
952/// createInlinedVariable - Create a new inlined variable based on current
953/// variable.
954/// @param DV Current Variable.
955/// @param InlinedScope Location at current variable is inlined.
956DIVariable llvm::createInlinedVariable(MDNode *DV, MDNode *InlinedScope,
957 LLVMContext &VMContext) {
Duncan P. N. Exon Smith176b6912014-10-03 20:01:09 +0000958 assert(DIVariable(DV).Verify() && "Expected a DIVariable");
959 if (!InlinedScope)
960 return cleanseInlinedVariable(DV, VMContext);
961
962 // Insert inlined scope.
963 SmallVector<Value *, 8> Elts;
964 for (unsigned I = 0, E = DIVariableInlinedAtIndex; I != E; ++I)
965 Elts.push_back(DV->getOperand(I));
966 Elts.push_back(InlinedScope);
967
968 DIVariable Inlined(MDNode::get(VMContext, Elts));
969 assert(Inlined.Verify() && "Expected to create a DIVariable");
970 return Inlined;
Bill Wendling523bea82013-11-08 08:13:15 +0000971}
972
973/// cleanseInlinedVariable - Remove inlined scope from the variable.
974DIVariable llvm::cleanseInlinedVariable(MDNode *DV, LLVMContext &VMContext) {
Duncan P. N. Exon Smith176b6912014-10-03 20:01:09 +0000975 assert(DIVariable(DV).Verify() && "Expected a DIVariable");
976 if (!DIVariable(DV).getInlinedAt())
977 return DIVariable(DV);
978
979 // Remove inlined scope.
980 SmallVector<Value *, 8> Elts;
981 for (unsigned I = 0, E = DIVariableInlinedAtIndex; I != E; ++I)
982 Elts.push_back(DV->getOperand(I));
983
984 DIVariable Cleansed(MDNode::get(VMContext, Elts));
985 assert(Cleansed.Verify() && "Expected to create a DIVariable");
986 return Cleansed;
Bill Wendling523bea82013-11-08 08:13:15 +0000987}
988
989/// getDISubprogram - Find subprogram that is enclosing this scope.
990DISubprogram llvm::getDISubprogram(const MDNode *Scope) {
991 DIDescriptor D(Scope);
992 if (D.isSubprogram())
993 return DISubprogram(Scope);
994
995 if (D.isLexicalBlockFile())
996 return getDISubprogram(DILexicalBlockFile(Scope).getContext());
997
998 if (D.isLexicalBlock())
999 return getDISubprogram(DILexicalBlock(Scope).getContext());
1000
1001 return DISubprogram();
1002}
1003
1004/// getDICompositeType - Find underlying composite type.
1005DICompositeType llvm::getDICompositeType(DIType T) {
1006 if (T.isCompositeType())
1007 return DICompositeType(T);
1008
1009 if (T.isDerivedType()) {
1010 // This function is currently used by dragonegg and dragonegg does
1011 // not generate identifier for types, so using an empty map to resolve
1012 // DerivedFrom should be fine.
1013 DITypeIdentifierMap EmptyMap;
1014 return getDICompositeType(
1015 DIDerivedType(T).getTypeDerivedFrom().resolve(EmptyMap));
1016 }
1017
1018 return DICompositeType();
1019}
1020
1021/// Update DITypeIdentifierMap by going through retained types of each CU.
1022DITypeIdentifierMap
1023llvm::generateDITypeIdentifierMap(const NamedMDNode *CU_Nodes) {
1024 DITypeIdentifierMap Map;
1025 for (unsigned CUi = 0, CUe = CU_Nodes->getNumOperands(); CUi != CUe; ++CUi) {
1026 DICompileUnit CU(CU_Nodes->getOperand(CUi));
1027 DIArray Retain = CU.getRetainedTypes();
1028 for (unsigned Ti = 0, Te = Retain.getNumElements(); Ti != Te; ++Ti) {
1029 if (!Retain.getElement(Ti).isCompositeType())
1030 continue;
1031 DICompositeType Ty(Retain.getElement(Ti));
1032 if (MDString *TypeId = Ty.getIdentifier()) {
1033 // Definition has priority over declaration.
1034 // Try to insert (TypeId, Ty) to Map.
1035 std::pair<DITypeIdentifierMap::iterator, bool> P =
1036 Map.insert(std::make_pair(TypeId, Ty));
1037 // If TypeId already exists in Map and this is a definition, replace
1038 // whatever we had (declaration or definition) with the definition.
1039 if (!P.second && !Ty.isForwardDecl())
1040 P.first->second = Ty;
1041 }
1042 }
1043 }
1044 return Map;
1045}
1046
1047//===----------------------------------------------------------------------===//
1048// DebugInfoFinder implementations.
1049//===----------------------------------------------------------------------===//
1050
1051void DebugInfoFinder::reset() {
1052 CUs.clear();
1053 SPs.clear();
1054 GVs.clear();
1055 TYs.clear();
1056 Scopes.clear();
1057 NodesSeen.clear();
1058 TypeIdentifierMap.clear();
Manman Ren2085ccc2013-11-17 18:42:37 +00001059 TypeMapInitialized = false;
1060}
1061
Manman Renb46e5502013-11-17 19:35:03 +00001062void DebugInfoFinder::InitializeTypeMap(const Module &M) {
Manman Ren2085ccc2013-11-17 18:42:37 +00001063 if (!TypeMapInitialized)
1064 if (NamedMDNode *CU_Nodes = M.getNamedMetadata("llvm.dbg.cu")) {
1065 TypeIdentifierMap = generateDITypeIdentifierMap(CU_Nodes);
1066 TypeMapInitialized = true;
1067 }
Bill Wendling523bea82013-11-08 08:13:15 +00001068}
1069
1070/// processModule - Process entire module and collect debug info.
1071void DebugInfoFinder::processModule(const Module &M) {
Manman Renb46e5502013-11-17 19:35:03 +00001072 InitializeTypeMap(M);
Bill Wendling523bea82013-11-08 08:13:15 +00001073 if (NamedMDNode *CU_Nodes = M.getNamedMetadata("llvm.dbg.cu")) {
Bill Wendling523bea82013-11-08 08:13:15 +00001074 for (unsigned i = 0, e = CU_Nodes->getNumOperands(); i != e; ++i) {
1075 DICompileUnit CU(CU_Nodes->getOperand(i));
1076 addCompileUnit(CU);
1077 DIArray GVs = CU.getGlobalVariables();
1078 for (unsigned i = 0, e = GVs.getNumElements(); i != e; ++i) {
1079 DIGlobalVariable DIG(GVs.getElement(i));
1080 if (addGlobalVariable(DIG)) {
1081 processScope(DIG.getContext());
Adrian Prantl1a1647c2014-03-18 02:34:58 +00001082 processType(DIG.getType().resolve(TypeIdentifierMap));
Bill Wendling523bea82013-11-08 08:13:15 +00001083 }
1084 }
1085 DIArray SPs = CU.getSubprograms();
1086 for (unsigned i = 0, e = SPs.getNumElements(); i != e; ++i)
1087 processSubprogram(DISubprogram(SPs.getElement(i)));
1088 DIArray EnumTypes = CU.getEnumTypes();
1089 for (unsigned i = 0, e = EnumTypes.getNumElements(); i != e; ++i)
1090 processType(DIType(EnumTypes.getElement(i)));
1091 DIArray RetainedTypes = CU.getRetainedTypes();
1092 for (unsigned i = 0, e = RetainedTypes.getNumElements(); i != e; ++i)
1093 processType(DIType(RetainedTypes.getElement(i)));
1094 DIArray Imports = CU.getImportedEntities();
1095 for (unsigned i = 0, e = Imports.getNumElements(); i != e; ++i) {
1096 DIImportedEntity Import = DIImportedEntity(Imports.getElement(i));
Adrian Prantld09ba232014-04-01 03:41:04 +00001097 DIDescriptor Entity = Import.getEntity().resolve(TypeIdentifierMap);
Bill Wendling523bea82013-11-08 08:13:15 +00001098 if (Entity.isType())
1099 processType(DIType(Entity));
1100 else if (Entity.isSubprogram())
1101 processSubprogram(DISubprogram(Entity));
1102 else if (Entity.isNameSpace())
1103 processScope(DINameSpace(Entity).getContext());
1104 }
1105 }
1106 }
1107}
1108
1109/// processLocation - Process DILocation.
Manman Ren2085ccc2013-11-17 18:42:37 +00001110void DebugInfoFinder::processLocation(const Module &M, DILocation Loc) {
Bill Wendling523bea82013-11-08 08:13:15 +00001111 if (!Loc)
1112 return;
Manman Renb46e5502013-11-17 19:35:03 +00001113 InitializeTypeMap(M);
Bill Wendling523bea82013-11-08 08:13:15 +00001114 processScope(Loc.getScope());
Manman Ren2085ccc2013-11-17 18:42:37 +00001115 processLocation(M, Loc.getOrigLocation());
Bill Wendling523bea82013-11-08 08:13:15 +00001116}
1117
1118/// processType - Process DIType.
1119void DebugInfoFinder::processType(DIType DT) {
1120 if (!addType(DT))
1121 return;
1122 processScope(DT.getContext().resolve(TypeIdentifierMap));
1123 if (DT.isCompositeType()) {
1124 DICompositeType DCT(DT);
1125 processType(DCT.getTypeDerivedFrom().resolve(TypeIdentifierMap));
Manman Renf8a19672014-07-28 22:24:06 +00001126 if (DT.isSubroutineType()) {
1127 DITypeArray DTA = DISubroutineType(DT).getTypeArray();
1128 for (unsigned i = 0, e = DTA.getNumElements(); i != e; ++i)
1129 processType(DTA.getElement(i).resolve(TypeIdentifierMap));
1130 return;
1131 }
Manman Renab8ffba2014-07-28 19:14:13 +00001132 DIArray DA = DCT.getElements();
Bill Wendling523bea82013-11-08 08:13:15 +00001133 for (unsigned i = 0, e = DA.getNumElements(); i != e; ++i) {
1134 DIDescriptor D = DA.getElement(i);
1135 if (D.isType())
1136 processType(DIType(D));
1137 else if (D.isSubprogram())
1138 processSubprogram(DISubprogram(D));
1139 }
1140 } else if (DT.isDerivedType()) {
1141 DIDerivedType DDT(DT);
1142 processType(DDT.getTypeDerivedFrom().resolve(TypeIdentifierMap));
1143 }
1144}
1145
1146void DebugInfoFinder::processScope(DIScope Scope) {
1147 if (Scope.isType()) {
1148 DIType Ty(Scope);
1149 processType(Ty);
1150 return;
1151 }
1152 if (Scope.isCompileUnit()) {
1153 addCompileUnit(DICompileUnit(Scope));
1154 return;
1155 }
1156 if (Scope.isSubprogram()) {
1157 processSubprogram(DISubprogram(Scope));
1158 return;
1159 }
1160 if (!addScope(Scope))
1161 return;
1162 if (Scope.isLexicalBlock()) {
1163 DILexicalBlock LB(Scope);
1164 processScope(LB.getContext());
1165 } else if (Scope.isLexicalBlockFile()) {
1166 DILexicalBlockFile LBF = DILexicalBlockFile(Scope);
1167 processScope(LBF.getScope());
1168 } else if (Scope.isNameSpace()) {
1169 DINameSpace NS(Scope);
1170 processScope(NS.getContext());
1171 }
1172}
1173
Bill Wendling523bea82013-11-08 08:13:15 +00001174/// processSubprogram - Process DISubprogram.
1175void DebugInfoFinder::processSubprogram(DISubprogram SP) {
1176 if (!addSubprogram(SP))
1177 return;
1178 processScope(SP.getContext().resolve(TypeIdentifierMap));
1179 processType(SP.getType());
1180 DIArray TParams = SP.getTemplateParams();
1181 for (unsigned I = 0, E = TParams.getNumElements(); I != E; ++I) {
1182 DIDescriptor Element = TParams.getElement(I);
1183 if (Element.isTemplateTypeParameter()) {
1184 DITemplateTypeParameter TType(Element);
1185 processScope(TType.getContext().resolve(TypeIdentifierMap));
1186 processType(TType.getType().resolve(TypeIdentifierMap));
1187 } else if (Element.isTemplateValueParameter()) {
1188 DITemplateValueParameter TVal(Element);
1189 processScope(TVal.getContext().resolve(TypeIdentifierMap));
1190 processType(TVal.getType().resolve(TypeIdentifierMap));
1191 }
1192 }
1193}
1194
1195/// processDeclare - Process DbgDeclareInst.
Manman Ren2085ccc2013-11-17 18:42:37 +00001196void DebugInfoFinder::processDeclare(const Module &M,
1197 const DbgDeclareInst *DDI) {
Bill Wendling523bea82013-11-08 08:13:15 +00001198 MDNode *N = dyn_cast<MDNode>(DDI->getVariable());
1199 if (!N)
1200 return;
Manman Renb46e5502013-11-17 19:35:03 +00001201 InitializeTypeMap(M);
Bill Wendling523bea82013-11-08 08:13:15 +00001202
1203 DIDescriptor DV(N);
1204 if (!DV.isVariable())
1205 return;
1206
1207 if (!NodesSeen.insert(DV))
1208 return;
1209 processScope(DIVariable(N).getContext());
Adrian Prantl1a1647c2014-03-18 02:34:58 +00001210 processType(DIVariable(N).getType().resolve(TypeIdentifierMap));
Bill Wendling523bea82013-11-08 08:13:15 +00001211}
1212
Manman Ren2085ccc2013-11-17 18:42:37 +00001213void DebugInfoFinder::processValue(const Module &M, const DbgValueInst *DVI) {
Bill Wendling523bea82013-11-08 08:13:15 +00001214 MDNode *N = dyn_cast<MDNode>(DVI->getVariable());
1215 if (!N)
1216 return;
Manman Renb46e5502013-11-17 19:35:03 +00001217 InitializeTypeMap(M);
Bill Wendling523bea82013-11-08 08:13:15 +00001218
1219 DIDescriptor DV(N);
1220 if (!DV.isVariable())
1221 return;
1222
1223 if (!NodesSeen.insert(DV))
1224 return;
1225 processScope(DIVariable(N).getContext());
Adrian Prantl1a1647c2014-03-18 02:34:58 +00001226 processType(DIVariable(N).getType().resolve(TypeIdentifierMap));
Bill Wendling523bea82013-11-08 08:13:15 +00001227}
1228
1229/// addType - Add type into Tys.
1230bool DebugInfoFinder::addType(DIType DT) {
1231 if (!DT)
1232 return false;
1233
1234 if (!NodesSeen.insert(DT))
1235 return false;
1236
1237 TYs.push_back(DT);
1238 return true;
1239}
1240
1241/// addCompileUnit - Add compile unit into CUs.
1242bool DebugInfoFinder::addCompileUnit(DICompileUnit CU) {
1243 if (!CU)
1244 return false;
1245 if (!NodesSeen.insert(CU))
1246 return false;
1247
1248 CUs.push_back(CU);
1249 return true;
1250}
1251
1252/// addGlobalVariable - Add global variable into GVs.
1253bool DebugInfoFinder::addGlobalVariable(DIGlobalVariable DIG) {
1254 if (!DIG)
1255 return false;
1256
1257 if (!NodesSeen.insert(DIG))
1258 return false;
1259
1260 GVs.push_back(DIG);
1261 return true;
1262}
1263
1264// addSubprogram - Add subprgoram into SPs.
1265bool DebugInfoFinder::addSubprogram(DISubprogram SP) {
1266 if (!SP)
1267 return false;
1268
1269 if (!NodesSeen.insert(SP))
1270 return false;
1271
1272 SPs.push_back(SP);
1273 return true;
1274}
1275
1276bool DebugInfoFinder::addScope(DIScope Scope) {
1277 if (!Scope)
1278 return false;
1279 // FIXME: Ocaml binding generates a scope with no content, we treat it
1280 // as null for now.
1281 if (Scope->getNumOperands() == 0)
1282 return false;
1283 if (!NodesSeen.insert(Scope))
1284 return false;
1285 Scopes.push_back(Scope);
1286 return true;
1287}
1288
1289//===----------------------------------------------------------------------===//
1290// DIDescriptor: dump routines for all descriptors.
1291//===----------------------------------------------------------------------===//
1292
1293/// dump - Print descriptor to dbgs() with a newline.
1294void DIDescriptor::dump() const {
1295 print(dbgs());
1296 dbgs() << '\n';
1297}
1298
1299/// print - Print descriptor.
1300void DIDescriptor::print(raw_ostream &OS) const {
1301 if (!DbgNode)
1302 return;
1303
1304 if (const char *Tag = dwarf::TagString(getTag()))
1305 OS << "[ " << Tag << " ]";
1306
1307 if (this->isSubrange()) {
1308 DISubrange(DbgNode).printInternal(OS);
1309 } else if (this->isCompileUnit()) {
1310 DICompileUnit(DbgNode).printInternal(OS);
1311 } else if (this->isFile()) {
1312 DIFile(DbgNode).printInternal(OS);
1313 } else if (this->isEnumerator()) {
1314 DIEnumerator(DbgNode).printInternal(OS);
1315 } else if (this->isBasicType()) {
1316 DIType(DbgNode).printInternal(OS);
1317 } else if (this->isDerivedType()) {
1318 DIDerivedType(DbgNode).printInternal(OS);
1319 } else if (this->isCompositeType()) {
1320 DICompositeType(DbgNode).printInternal(OS);
1321 } else if (this->isSubprogram()) {
1322 DISubprogram(DbgNode).printInternal(OS);
1323 } else if (this->isGlobalVariable()) {
1324 DIGlobalVariable(DbgNode).printInternal(OS);
1325 } else if (this->isVariable()) {
1326 DIVariable(DbgNode).printInternal(OS);
1327 } else if (this->isObjCProperty()) {
1328 DIObjCProperty(DbgNode).printInternal(OS);
1329 } else if (this->isNameSpace()) {
1330 DINameSpace(DbgNode).printInternal(OS);
1331 } else if (this->isScope()) {
1332 DIScope(DbgNode).printInternal(OS);
Adrian Prantl87b7eb92014-10-01 18:55:02 +00001333 } else if (this->isExpression()) {
1334 DIExpression(DbgNode).printInternal(OS);
Bill Wendling523bea82013-11-08 08:13:15 +00001335 }
1336}
1337
1338void DISubrange::printInternal(raw_ostream &OS) const {
1339 int64_t Count = getCount();
1340 if (Count != -1)
1341 OS << " [" << getLo() << ", " << Count - 1 << ']';
1342 else
1343 OS << " [unbounded]";
1344}
1345
1346void DIScope::printInternal(raw_ostream &OS) const {
1347 OS << " [" << getDirectory() << "/" << getFilename() << ']';
1348}
1349
1350void DICompileUnit::printInternal(raw_ostream &OS) const {
1351 DIScope::printInternal(OS);
1352 OS << " [";
1353 unsigned Lang = getLanguage();
1354 if (const char *LangStr = dwarf::LanguageString(Lang))
1355 OS << LangStr;
1356 else
1357 (OS << "lang 0x").write_hex(Lang);
1358 OS << ']';
1359}
1360
1361void DIEnumerator::printInternal(raw_ostream &OS) const {
1362 OS << " [" << getName() << " :: " << getEnumValue() << ']';
1363}
1364
1365void DIType::printInternal(raw_ostream &OS) const {
Manman Renf93ac4b2014-07-29 18:20:39 +00001366 if (!DbgNode)
Bill Wendling523bea82013-11-08 08:13:15 +00001367 return;
1368
1369 StringRef Res = getName();
1370 if (!Res.empty())
1371 OS << " [" << Res << "]";
1372
1373 // TODO: Print context?
1374
1375 OS << " [line " << getLineNumber() << ", size " << getSizeInBits()
1376 << ", align " << getAlignInBits() << ", offset " << getOffsetInBits();
1377 if (isBasicType())
1378 if (const char *Enc =
1379 dwarf::AttributeEncodingString(DIBasicType(DbgNode).getEncoding()))
1380 OS << ", enc " << Enc;
1381 OS << "]";
1382
1383 if (isPrivate())
1384 OS << " [private]";
1385 else if (isProtected())
1386 OS << " [protected]";
Adrian Prantldaedfda2014-08-29 22:44:07 +00001387 else if (isPublic())
1388 OS << " [public]";
Bill Wendling523bea82013-11-08 08:13:15 +00001389
1390 if (isArtificial())
1391 OS << " [artificial]";
1392
1393 if (isForwardDecl())
1394 OS << " [decl]";
1395 else if (getTag() == dwarf::DW_TAG_structure_type ||
1396 getTag() == dwarf::DW_TAG_union_type ||
1397 getTag() == dwarf::DW_TAG_enumeration_type ||
1398 getTag() == dwarf::DW_TAG_class_type)
1399 OS << " [def]";
1400 if (isVector())
1401 OS << " [vector]";
1402 if (isStaticMember())
1403 OS << " [static]";
Adrian Prantl99c7af22013-12-18 21:48:19 +00001404
1405 if (isLValueReference())
1406 OS << " [reference]";
1407
1408 if (isRValueReference())
1409 OS << " [rvalue reference]";
Bill Wendling523bea82013-11-08 08:13:15 +00001410}
1411
1412void DIDerivedType::printInternal(raw_ostream &OS) const {
1413 DIType::printInternal(OS);
1414 OS << " [from " << getTypeDerivedFrom().getName() << ']';
1415}
1416
1417void DICompositeType::printInternal(raw_ostream &OS) const {
1418 DIType::printInternal(OS);
Manman Renab8ffba2014-07-28 19:14:13 +00001419 DIArray A = getElements();
Bill Wendling523bea82013-11-08 08:13:15 +00001420 OS << " [" << A.getNumElements() << " elements]";
1421}
1422
1423void DINameSpace::printInternal(raw_ostream &OS) const {
1424 StringRef Name = getName();
1425 if (!Name.empty())
1426 OS << " [" << Name << ']';
1427
1428 OS << " [line " << getLineNumber() << ']';
1429}
1430
1431void DISubprogram::printInternal(raw_ostream &OS) const {
1432 // TODO : Print context
1433 OS << " [line " << getLineNumber() << ']';
1434
1435 if (isLocalToUnit())
1436 OS << " [local]";
1437
1438 if (isDefinition())
1439 OS << " [def]";
1440
1441 if (getScopeLineNumber() != getLineNumber())
1442 OS << " [scope " << getScopeLineNumber() << "]";
1443
1444 if (isPrivate())
1445 OS << " [private]";
1446 else if (isProtected())
1447 OS << " [protected]";
Adrian Prantldaedfda2014-08-29 22:44:07 +00001448 else if (isPublic())
1449 OS << " [public]";
Bill Wendling523bea82013-11-08 08:13:15 +00001450
Adrian Prantl99c7af22013-12-18 21:48:19 +00001451 if (isLValueReference())
1452 OS << " [reference]";
1453
1454 if (isRValueReference())
1455 OS << " [rvalue reference]";
1456
Bill Wendling523bea82013-11-08 08:13:15 +00001457 StringRef Res = getName();
1458 if (!Res.empty())
1459 OS << " [" << Res << ']';
1460}
1461
1462void DIGlobalVariable::printInternal(raw_ostream &OS) const {
1463 StringRef Res = getName();
1464 if (!Res.empty())
1465 OS << " [" << Res << ']';
1466
1467 OS << " [line " << getLineNumber() << ']';
1468
1469 // TODO : Print context
1470
1471 if (isLocalToUnit())
1472 OS << " [local]";
1473
1474 if (isDefinition())
1475 OS << " [def]";
1476}
1477
1478void DIVariable::printInternal(raw_ostream &OS) const {
1479 StringRef Res = getName();
1480 if (!Res.empty())
1481 OS << " [" << Res << ']';
1482
1483 OS << " [line " << getLineNumber() << ']';
Adrian Prantl87b7eb92014-10-01 18:55:02 +00001484}
Adrian Prantlb1416832014-08-01 22:11:58 +00001485
Adrian Prantl87b7eb92014-10-01 18:55:02 +00001486void DIExpression::printInternal(raw_ostream &OS) const {
1487 for (unsigned I = 0; I < getNumElements(); ++I) {
1488 uint64_t OpCode = getElement(I);
1489 OS << " [" << OperationEncodingString(OpCode);
1490 switch (OpCode) {
1491 case DW_OP_plus: {
1492 OS << " " << getElement(++I);
1493 break;
1494 }
1495 case DW_OP_piece: {
1496 unsigned Offset = getElement(++I);
1497 unsigned Size = getElement(++I);
Adrian Prantl38666f12014-10-02 16:42:15 +00001498 OS << " offset=" << Offset << ", size=" << Size;
Adrian Prantl87b7eb92014-10-01 18:55:02 +00001499 break;
1500 }
1501 default:
Adrian Prantl75a0dac2014-10-02 16:42:13 +00001502 // Else bail out early. This may be a line table entry.
1503 OS << "Unknown]";
1504 return;
Adrian Prantl87b7eb92014-10-01 18:55:02 +00001505 }
1506 OS << "]";
1507 }
Bill Wendling523bea82013-11-08 08:13:15 +00001508}
1509
1510void DIObjCProperty::printInternal(raw_ostream &OS) const {
1511 StringRef Name = getObjCPropertyName();
1512 if (!Name.empty())
1513 OS << " [" << Name << ']';
1514
1515 OS << " [line " << getLineNumber() << ", properties " << getUnsignedField(6)
1516 << ']';
1517}
1518
1519static void printDebugLoc(DebugLoc DL, raw_ostream &CommentOS,
1520 const LLVMContext &Ctx) {
1521 if (!DL.isUnknown()) { // Print source line info.
1522 DIScope Scope(DL.getScope(Ctx));
1523 assert(Scope.isScope() && "Scope of a DebugLoc should be a DIScope.");
1524 // Omit the directory, because it's likely to be long and uninteresting.
1525 CommentOS << Scope.getFilename();
1526 CommentOS << ':' << DL.getLine();
1527 if (DL.getCol() != 0)
1528 CommentOS << ':' << DL.getCol();
1529 DebugLoc InlinedAtDL = DebugLoc::getFromDILocation(DL.getInlinedAt(Ctx));
1530 if (!InlinedAtDL.isUnknown()) {
1531 CommentOS << " @[ ";
1532 printDebugLoc(InlinedAtDL, CommentOS, Ctx);
1533 CommentOS << " ]";
1534 }
1535 }
1536}
1537
1538void DIVariable::printExtendedName(raw_ostream &OS) const {
1539 const LLVMContext &Ctx = DbgNode->getContext();
1540 StringRef Res = getName();
1541 if (!Res.empty())
1542 OS << Res << "," << getLineNumber();
1543 if (MDNode *InlinedAt = getInlinedAt()) {
1544 DebugLoc InlinedAtDL = DebugLoc::getFromDILocation(InlinedAt);
1545 if (!InlinedAtDL.isUnknown()) {
1546 OS << " @[";
1547 printDebugLoc(InlinedAtDL, OS, Ctx);
1548 OS << "]";
1549 }
1550 }
1551}
1552
1553/// Specialize constructor to make sure it has the correct type.
1554template <> DIRef<DIScope>::DIRef(const Value *V) : Val(V) {
1555 assert(isScopeRef(V) && "DIScopeRef should be a MDString or MDNode");
1556}
1557template <> DIRef<DIType>::DIRef(const Value *V) : Val(V) {
1558 assert(isTypeRef(V) && "DITypeRef should be a MDString or MDNode");
1559}
1560
1561/// Specialize getFieldAs to handle fields that are references to DIScopes.
1562template <>
1563DIScopeRef DIDescriptor::getFieldAs<DIScopeRef>(unsigned Elt) const {
1564 return DIScopeRef(getField(DbgNode, Elt));
1565}
1566/// Specialize getFieldAs to handle fields that are references to DITypes.
1567template <> DITypeRef DIDescriptor::getFieldAs<DITypeRef>(unsigned Elt) const {
1568 return DITypeRef(getField(DbgNode, Elt));
1569}
Manman Rencb14bbc2013-11-22 22:06:31 +00001570
1571/// Strip debug info in the module if it exists.
1572/// To do this, we remove all calls to the debugger intrinsics and any named
1573/// metadata for debugging. We also remove debug locations for instructions.
1574/// Return true if module is modified.
1575bool llvm::StripDebugInfo(Module &M) {
1576
1577 bool Changed = false;
1578
1579 // Remove all of the calls to the debugger intrinsics, and remove them from
1580 // the module.
1581 if (Function *Declare = M.getFunction("llvm.dbg.declare")) {
1582 while (!Declare->use_empty()) {
Chandler Carruthcdf47882014-03-09 03:16:01 +00001583 CallInst *CI = cast<CallInst>(Declare->user_back());
Manman Rencb14bbc2013-11-22 22:06:31 +00001584 CI->eraseFromParent();
1585 }
1586 Declare->eraseFromParent();
1587 Changed = true;
1588 }
1589
1590 if (Function *DbgVal = M.getFunction("llvm.dbg.value")) {
1591 while (!DbgVal->use_empty()) {
Chandler Carruthcdf47882014-03-09 03:16:01 +00001592 CallInst *CI = cast<CallInst>(DbgVal->user_back());
Manman Rencb14bbc2013-11-22 22:06:31 +00001593 CI->eraseFromParent();
1594 }
1595 DbgVal->eraseFromParent();
1596 Changed = true;
1597 }
1598
1599 for (Module::named_metadata_iterator NMI = M.named_metadata_begin(),
1600 NME = M.named_metadata_end(); NMI != NME;) {
1601 NamedMDNode *NMD = NMI;
1602 ++NMI;
1603 if (NMD->getName().startswith("llvm.dbg.")) {
1604 NMD->eraseFromParent();
1605 Changed = true;
1606 }
1607 }
1608
1609 for (Module::iterator MI = M.begin(), ME = M.end(); MI != ME; ++MI)
1610 for (Function::iterator FI = MI->begin(), FE = MI->end(); FI != FE;
1611 ++FI)
1612 for (BasicBlock::iterator BI = FI->begin(), BE = FI->end(); BI != BE;
1613 ++BI) {
1614 if (!BI->getDebugLoc().isUnknown()) {
1615 Changed = true;
1616 BI->setDebugLoc(DebugLoc());
1617 }
1618 }
1619
1620 return Changed;
1621}
Manman Ren8b4306c2013-12-02 21:29:56 +00001622
Manman Renbd4daf82013-12-03 00:12:14 +00001623/// Return Debug Info Metadata Version by checking module flags.
1624unsigned llvm::getDebugMetadataVersionFromModule(const Module &M) {
Manman Ren8b4306c2013-12-02 21:29:56 +00001625 Value *Val = M.getModuleFlag("Debug Info Version");
1626 if (!Val)
1627 return 0;
1628 return cast<ConstantInt>(Val)->getZExtValue();
1629}
David Blaikie6876b3b2014-07-01 20:05:26 +00001630
David Blaikiea8c35092014-07-02 18:30:05 +00001631llvm::DenseMap<const llvm::Function *, llvm::DISubprogram>
1632llvm::makeSubprogramMap(const Module &M) {
1633 DenseMap<const Function *, DISubprogram> R;
David Blaikie6876b3b2014-07-01 20:05:26 +00001634
1635 NamedMDNode *CU_Nodes = M.getNamedMetadata("llvm.dbg.cu");
1636 if (!CU_Nodes)
1637 return R;
1638
1639 for (MDNode *N : CU_Nodes->operands()) {
1640 DICompileUnit CUNode(N);
1641 DIArray SPs = CUNode.getSubprograms();
1642 for (unsigned i = 0, e = SPs.getNumElements(); i != e; ++i) {
1643 DISubprogram SP(SPs.getElement(i));
1644 if (Function *F = SP.getFunction())
1645 R.insert(std::make_pair(F, SP));
1646 }
1647 }
1648 return R;
1649}