blob: 56b06230e22b1309436441105ac20d2ac928d151 [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
Duncan P. N. Exon Smith7f637a92014-10-15 17:01:28 +0000146/// \brief Return the size reported by the variable's type.
Adrian Prantlb1416832014-08-01 22:11:58 +0000147unsigned 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 +0000188bool DIDescriptor::isBasicType() const {
189 if (!DbgNode)
190 return false;
191 switch (getTag()) {
192 case dwarf::DW_TAG_base_type:
193 case dwarf::DW_TAG_unspecified_type:
194 return true;
195 default:
196 return false;
197 }
198}
199
Bill Wendling523bea82013-11-08 08:13:15 +0000200bool DIDescriptor::isDerivedType() const {
201 if (!DbgNode)
202 return false;
203 switch (getTag()) {
204 case dwarf::DW_TAG_typedef:
205 case dwarf::DW_TAG_pointer_type:
206 case dwarf::DW_TAG_ptr_to_member_type:
207 case dwarf::DW_TAG_reference_type:
208 case dwarf::DW_TAG_rvalue_reference_type:
209 case dwarf::DW_TAG_const_type:
210 case dwarf::DW_TAG_volatile_type:
211 case dwarf::DW_TAG_restrict_type:
212 case dwarf::DW_TAG_member:
213 case dwarf::DW_TAG_inheritance:
214 case dwarf::DW_TAG_friend:
215 return true;
216 default:
217 // CompositeTypes are currently modelled as DerivedTypes.
218 return isCompositeType();
219 }
220}
221
Bill Wendling523bea82013-11-08 08:13:15 +0000222bool DIDescriptor::isCompositeType() const {
223 if (!DbgNode)
224 return false;
225 switch (getTag()) {
226 case dwarf::DW_TAG_array_type:
227 case dwarf::DW_TAG_structure_type:
228 case dwarf::DW_TAG_union_type:
229 case dwarf::DW_TAG_enumeration_type:
230 case dwarf::DW_TAG_subroutine_type:
231 case dwarf::DW_TAG_class_type:
232 return true;
233 default:
234 return false;
235 }
236}
237
Bill Wendling523bea82013-11-08 08:13:15 +0000238bool DIDescriptor::isVariable() const {
239 if (!DbgNode)
240 return false;
241 switch (getTag()) {
242 case dwarf::DW_TAG_auto_variable:
243 case dwarf::DW_TAG_arg_variable:
244 return true;
245 default:
246 return false;
247 }
248}
249
Bill Wendling523bea82013-11-08 08:13:15 +0000250bool DIDescriptor::isType() const {
Manman Renf93ac4b2014-07-29 18:20:39 +0000251 return isBasicType() || isCompositeType() || isDerivedType();
Bill Wendling523bea82013-11-08 08:13:15 +0000252}
253
Bill Wendling523bea82013-11-08 08:13:15 +0000254bool DIDescriptor::isSubprogram() const {
255 return DbgNode && getTag() == dwarf::DW_TAG_subprogram;
256}
257
Bill Wendling523bea82013-11-08 08:13:15 +0000258bool DIDescriptor::isGlobalVariable() const {
259 return DbgNode && (getTag() == dwarf::DW_TAG_variable ||
260 getTag() == dwarf::DW_TAG_constant);
261}
262
Bill Wendling523bea82013-11-08 08:13:15 +0000263bool DIDescriptor::isScope() const {
264 if (!DbgNode)
265 return false;
266 switch (getTag()) {
267 case dwarf::DW_TAG_compile_unit:
268 case dwarf::DW_TAG_lexical_block:
269 case dwarf::DW_TAG_subprogram:
270 case dwarf::DW_TAG_namespace:
271 case dwarf::DW_TAG_file_type:
272 return true;
273 default:
274 break;
275 }
276 return isType();
277}
278
Bill Wendling523bea82013-11-08 08:13:15 +0000279bool DIDescriptor::isTemplateTypeParameter() const {
280 return DbgNode && getTag() == dwarf::DW_TAG_template_type_parameter;
281}
282
Bill Wendling523bea82013-11-08 08:13:15 +0000283bool DIDescriptor::isTemplateValueParameter() const {
284 return DbgNode && (getTag() == dwarf::DW_TAG_template_value_parameter ||
285 getTag() == dwarf::DW_TAG_GNU_template_template_param ||
286 getTag() == dwarf::DW_TAG_GNU_template_parameter_pack);
287}
288
Bill Wendling523bea82013-11-08 08:13:15 +0000289bool DIDescriptor::isCompileUnit() const {
290 return DbgNode && getTag() == dwarf::DW_TAG_compile_unit;
291}
292
Bill Wendling523bea82013-11-08 08:13:15 +0000293bool DIDescriptor::isFile() const {
294 return DbgNode && getTag() == dwarf::DW_TAG_file_type;
295}
296
Bill Wendling523bea82013-11-08 08:13:15 +0000297bool DIDescriptor::isNameSpace() const {
298 return DbgNode && getTag() == dwarf::DW_TAG_namespace;
299}
300
Bill Wendling523bea82013-11-08 08:13:15 +0000301bool DIDescriptor::isLexicalBlockFile() const {
302 return DbgNode && getTag() == dwarf::DW_TAG_lexical_block &&
Duncan P. N. Exon Smith176b6912014-10-03 20:01:09 +0000303 DbgNode->getNumOperands() == 3 && getNumHeaderFields() == 2;
Bill Wendling523bea82013-11-08 08:13:15 +0000304}
305
Bill Wendling523bea82013-11-08 08:13:15 +0000306bool DIDescriptor::isLexicalBlock() const {
Duncan P. N. Exon Smith176b6912014-10-03 20:01:09 +0000307 // FIXME: There are always exactly 4 header fields in DILexicalBlock, but
308 // something relies on this returning true for DILexicalBlockFile.
Bill Wendling523bea82013-11-08 08:13:15 +0000309 return DbgNode && getTag() == dwarf::DW_TAG_lexical_block &&
Duncan P. N. Exon Smith176b6912014-10-03 20:01:09 +0000310 DbgNode->getNumOperands() == 3 &&
311 (getNumHeaderFields() == 2 || getNumHeaderFields() == 4);
Bill Wendling523bea82013-11-08 08:13:15 +0000312}
313
Bill Wendling523bea82013-11-08 08:13:15 +0000314bool DIDescriptor::isSubrange() const {
315 return DbgNode && getTag() == dwarf::DW_TAG_subrange_type;
316}
317
Bill Wendling523bea82013-11-08 08:13:15 +0000318bool DIDescriptor::isEnumerator() const {
319 return DbgNode && getTag() == dwarf::DW_TAG_enumerator;
320}
321
Bill Wendling523bea82013-11-08 08:13:15 +0000322bool DIDescriptor::isObjCProperty() const {
323 return DbgNode && getTag() == dwarf::DW_TAG_APPLE_property;
324}
325
Bill Wendling523bea82013-11-08 08:13:15 +0000326bool DIDescriptor::isImportedEntity() const {
327 return DbgNode && (getTag() == dwarf::DW_TAG_imported_module ||
328 getTag() == dwarf::DW_TAG_imported_declaration);
329}
330
Adrian Prantl87b7eb92014-10-01 18:55:02 +0000331bool DIDescriptor::isExpression() const {
332 return DbgNode && (getTag() == dwarf::DW_TAG_expression);
333}
334
Bill Wendling523bea82013-11-08 08:13:15 +0000335//===----------------------------------------------------------------------===//
336// Simple Descriptor Constructors and other Methods
337//===----------------------------------------------------------------------===//
338
Frederic Riss36acf0f2014-09-15 07:50:36 +0000339void DIDescriptor::replaceAllUsesWith(LLVMContext &VMContext, DIDescriptor D) {
Bill Wendling523bea82013-11-08 08:13:15 +0000340
341 assert(DbgNode && "Trying to replace an unverified type!");
342
343 // Since we use a TrackingVH for the node, its easy for clients to manufacture
344 // legitimate situations where they want to replaceAllUsesWith() on something
345 // which, due to uniquing, has merged with the source. We shield clients from
346 // this detail by allowing a value to be replaced with replaceAllUsesWith()
347 // itself.
David Blaikied3f094a2014-05-06 03:41:57 +0000348 const MDNode *DN = D;
349 if (DbgNode == DN) {
350 SmallVector<Value*, 10> Ops(DbgNode->getNumOperands());
351 for (size_t i = 0; i != Ops.size(); ++i)
352 Ops[i] = DbgNode->getOperand(i);
353 DN = MDNode::get(VMContext, Ops);
Bill Wendling523bea82013-11-08 08:13:15 +0000354 }
David Blaikied3f094a2014-05-06 03:41:57 +0000355
356 MDNode *Node = const_cast<MDNode *>(DbgNode);
357 const Value *V = cast_or_null<Value>(DN);
358 Node->replaceAllUsesWith(const_cast<Value *>(V));
359 MDNode::deleteTemporary(Node);
Frederic Rissdd7aec52014-09-15 07:50:42 +0000360 DbgNode = DN;
Bill Wendling523bea82013-11-08 08:13:15 +0000361}
362
Frederic Riss36acf0f2014-09-15 07:50:36 +0000363void DIDescriptor::replaceAllUsesWith(MDNode *D) {
Bill Wendling523bea82013-11-08 08:13:15 +0000364
365 assert(DbgNode && "Trying to replace an unverified type!");
David Blaikied3f094a2014-05-06 03:41:57 +0000366 assert(DbgNode != D && "This replacement should always happen");
367 MDNode *Node = const_cast<MDNode *>(DbgNode);
368 const MDNode *DN = D;
369 const Value *V = cast_or_null<Value>(DN);
370 Node->replaceAllUsesWith(const_cast<Value *>(V));
371 MDNode::deleteTemporary(Node);
Bill Wendling523bea82013-11-08 08:13:15 +0000372}
373
Bill Wendling523bea82013-11-08 08:13:15 +0000374bool DICompileUnit::Verify() const {
375 if (!isCompileUnit())
376 return false;
377
378 // Don't bother verifying the compilation directory or producer string
379 // as those could be empty.
380 if (getFilename().empty())
381 return false;
382
Duncan P. N. Exon Smith176b6912014-10-03 20:01:09 +0000383 return DbgNode->getNumOperands() == 7 && getNumHeaderFields() == 8;
Bill Wendling523bea82013-11-08 08:13:15 +0000384}
385
Bill Wendling523bea82013-11-08 08:13:15 +0000386bool DIObjCProperty::Verify() const {
387 if (!isObjCProperty())
388 return false;
389
390 // Don't worry about the rest of the strings for now.
Duncan P. N. Exon Smith176b6912014-10-03 20:01:09 +0000391 return DbgNode->getNumOperands() == 3 && getNumHeaderFields() == 6;
Bill Wendling523bea82013-11-08 08:13:15 +0000392}
393
Duncan P. N. Exon Smith7f637a92014-10-15 17:01:28 +0000394/// \brief Check if a field at position Elt of a MDNode is a MDNode.
395///
Bill Wendling523bea82013-11-08 08:13:15 +0000396/// We currently allow an empty string and an integer.
397/// But we don't allow a non-empty string in a MDNode field.
398static bool fieldIsMDNode(const MDNode *DbgNode, unsigned Elt) {
399 // FIXME: This function should return true, if the field is null or the field
400 // is indeed a MDNode: return !Fld || isa<MDNode>(Fld).
401 Value *Fld = getField(DbgNode, Elt);
402 if (Fld && isa<MDString>(Fld) && !cast<MDString>(Fld)->getString().empty())
403 return false;
404 return true;
405}
406
Duncan P. N. Exon Smith7f637a92014-10-15 17:01:28 +0000407/// \brief Check if a field at position Elt of a MDNode is a MDString.
Bill Wendling523bea82013-11-08 08:13:15 +0000408static bool fieldIsMDString(const MDNode *DbgNode, unsigned Elt) {
409 Value *Fld = getField(DbgNode, Elt);
410 return !Fld || isa<MDString>(Fld);
411}
412
Duncan P. N. Exon Smith7f637a92014-10-15 17:01:28 +0000413/// \brief Check if a value can be a reference to a type.
Bill Wendling523bea82013-11-08 08:13:15 +0000414static bool isTypeRef(const Value *Val) {
415 return !Val ||
416 (isa<MDString>(Val) && !cast<MDString>(Val)->getString().empty()) ||
417 (isa<MDNode>(Val) && DIType(cast<MDNode>(Val)).isType());
418}
419
Duncan P. N. Exon Smith7f637a92014-10-15 17:01:28 +0000420/// \brief Check if referenced field might be a type.
Bill Wendling523bea82013-11-08 08:13:15 +0000421static bool fieldIsTypeRef(const MDNode *DbgNode, unsigned Elt) {
422 Value *Fld = getField(DbgNode, Elt);
423 return isTypeRef(Fld);
424}
425
Duncan P. N. Exon Smith7f637a92014-10-15 17:01:28 +0000426/// \brief Check if a value can be a ScopeRef.
Bill Wendling523bea82013-11-08 08:13:15 +0000427static bool isScopeRef(const Value *Val) {
428 return !Val ||
Adrian Prantl6b444c52014-04-01 21:04:24 +0000429 (isa<MDString>(Val) && !cast<MDString>(Val)->getString().empty()) ||
430 // Not checking for Val->isScope() here, because it would work
431 // only for lexical scopes and not all subclasses of DIScope.
432 isa<MDNode>(Val);
Bill Wendling523bea82013-11-08 08:13:15 +0000433}
434
Duncan P. N. Exon Smith7f637a92014-10-15 17:01:28 +0000435/// \brief Check if a field at position Elt of a MDNode can be a ScopeRef.
Bill Wendling523bea82013-11-08 08:13:15 +0000436static bool fieldIsScopeRef(const MDNode *DbgNode, unsigned Elt) {
437 Value *Fld = getField(DbgNode, Elt);
438 return isScopeRef(Fld);
439}
440
Bill Wendling523bea82013-11-08 08:13:15 +0000441bool DIType::Verify() const {
442 if (!isType())
443 return false;
444 // Make sure Context @ field 2 is MDNode.
445 if (!fieldIsScopeRef(DbgNode, 2))
446 return false;
447
448 // FIXME: Sink this into the various subclass verifies.
449 uint16_t Tag = getTag();
Manman Renf93ac4b2014-07-29 18:20:39 +0000450 if (!isBasicType() && Tag != dwarf::DW_TAG_const_type &&
Bill Wendling523bea82013-11-08 08:13:15 +0000451 Tag != dwarf::DW_TAG_volatile_type && Tag != dwarf::DW_TAG_pointer_type &&
452 Tag != dwarf::DW_TAG_ptr_to_member_type &&
453 Tag != dwarf::DW_TAG_reference_type &&
454 Tag != dwarf::DW_TAG_rvalue_reference_type &&
455 Tag != dwarf::DW_TAG_restrict_type && Tag != dwarf::DW_TAG_array_type &&
456 Tag != dwarf::DW_TAG_enumeration_type &&
457 Tag != dwarf::DW_TAG_subroutine_type &&
458 Tag != dwarf::DW_TAG_inheritance && Tag != dwarf::DW_TAG_friend &&
459 getFilename().empty())
460 return false;
Adrian Prantldaedfda2014-08-29 22:44:07 +0000461
Bill Wendling523bea82013-11-08 08:13:15 +0000462 // DIType is abstract, it should be a BasicType, a DerivedType or
463 // a CompositeType.
464 if (isBasicType())
Renato Golin47f46fd2013-11-26 16:47:00 +0000465 return DIBasicType(DbgNode).Verify();
Bill Wendling523bea82013-11-08 08:13:15 +0000466 else if (isCompositeType())
Renato Golin47f46fd2013-11-26 16:47:00 +0000467 return DICompositeType(DbgNode).Verify();
Bill Wendling523bea82013-11-08 08:13:15 +0000468 else if (isDerivedType())
Renato Golin47f46fd2013-11-26 16:47:00 +0000469 return DIDerivedType(DbgNode).Verify();
Bill Wendling523bea82013-11-08 08:13:15 +0000470 else
471 return false;
Bill Wendling523bea82013-11-08 08:13:15 +0000472}
473
Bill Wendling523bea82013-11-08 08:13:15 +0000474bool DIBasicType::Verify() const {
Duncan P. N. Exon Smith176b6912014-10-03 20:01:09 +0000475 return isBasicType() && DbgNode->getNumOperands() == 3 &&
476 getNumHeaderFields() == 8;
Bill Wendling523bea82013-11-08 08:13:15 +0000477}
478
Bill Wendling523bea82013-11-08 08:13:15 +0000479bool DIDerivedType::Verify() const {
Duncan P. N. Exon Smith176b6912014-10-03 20:01:09 +0000480 // Make sure DerivedFrom @ field 3 is TypeRef.
481 if (!fieldIsTypeRef(DbgNode, 3))
Bill Wendling523bea82013-11-08 08:13:15 +0000482 return false;
483 if (getTag() == dwarf::DW_TAG_ptr_to_member_type)
Duncan P. N. Exon Smith176b6912014-10-03 20:01:09 +0000484 // Make sure ClassType @ field 4 is a TypeRef.
485 if (!fieldIsTypeRef(DbgNode, 4))
Bill Wendling523bea82013-11-08 08:13:15 +0000486 return false;
487
Duncan P. N. Exon Smith176b6912014-10-03 20:01:09 +0000488 return isDerivedType() && DbgNode->getNumOperands() >= 4 &&
489 DbgNode->getNumOperands() <= 8 && getNumHeaderFields() >= 7 &&
490 getNumHeaderFields() <= 8;
Bill Wendling523bea82013-11-08 08:13:15 +0000491}
492
Bill Wendling523bea82013-11-08 08:13:15 +0000493bool DICompositeType::Verify() const {
494 if (!isCompositeType())
495 return false;
496
Duncan P. N. Exon Smith176b6912014-10-03 20:01:09 +0000497 // Make sure DerivedFrom @ field 3 and ContainingType @ field 5 are TypeRef.
498 if (!fieldIsTypeRef(DbgNode, 3))
Bill Wendling523bea82013-11-08 08:13:15 +0000499 return false;
Duncan P. N. Exon Smith176b6912014-10-03 20:01:09 +0000500 if (!fieldIsTypeRef(DbgNode, 5))
Bill Wendling523bea82013-11-08 08:13:15 +0000501 return false;
502
Duncan P. N. Exon Smith176b6912014-10-03 20:01:09 +0000503 // Make sure the type identifier at field 7 is MDString, it can be null.
504 if (!fieldIsMDString(DbgNode, 7))
Bill Wendling523bea82013-11-08 08:13:15 +0000505 return false;
506
Adrian Prantl99c7af22013-12-18 21:48:19 +0000507 // A subroutine type can't be both & and &&.
508 if (isLValueReference() && isRValueReference())
509 return false;
510
Duncan P. N. Exon Smith176b6912014-10-03 20:01:09 +0000511 return DbgNode->getNumOperands() == 8 && getNumHeaderFields() == 8;
Bill Wendling523bea82013-11-08 08:13:15 +0000512}
513
Bill Wendling523bea82013-11-08 08:13:15 +0000514bool DISubprogram::Verify() const {
515 if (!isSubprogram())
516 return false;
517
Duncan P. N. Exon Smith176b6912014-10-03 20:01:09 +0000518 // Make sure context @ field 2 is a ScopeRef and type @ field 3 is a MDNode.
Bill Wendling523bea82013-11-08 08:13:15 +0000519 if (!fieldIsScopeRef(DbgNode, 2))
520 return false;
Duncan P. N. Exon Smith176b6912014-10-03 20:01:09 +0000521 if (!fieldIsMDNode(DbgNode, 3))
Bill Wendling523bea82013-11-08 08:13:15 +0000522 return false;
Duncan P. N. Exon Smith176b6912014-10-03 20:01:09 +0000523 // Containing type @ field 4.
524 if (!fieldIsTypeRef(DbgNode, 4))
Bill Wendling523bea82013-11-08 08:13:15 +0000525 return false;
Adrian Prantl99c7af22013-12-18 21:48:19 +0000526
527 // A subprogram can't be both & and &&.
528 if (isLValueReference() && isRValueReference())
529 return false;
530
David Blaikie3dfe4782014-10-14 18:22:52 +0000531 // If a DISubprogram has an llvm::Function*, then scope chains from all
532 // instructions within the function should lead to this DISubprogram.
533 if (auto *F = getFunction()) {
534 LLVMContext &Ctxt = F->getContext();
535 for (auto &BB : *F) {
536 for (auto &I : BB) {
537 DebugLoc DL = I.getDebugLoc();
538 if (DL.isUnknown())
539 continue;
540
541 MDNode *Scope = nullptr;
542 MDNode *IA = nullptr;
543 // walk the inlined-at scopes
544 while (DL.getScopeAndInlinedAt(Scope, IA, F->getContext()), IA)
545 DL = DebugLoc::getFromDILocation(IA);
546 DL.getScopeAndInlinedAt(Scope, IA, Ctxt);
547 assert(!IA);
548 while (!DIDescriptor(Scope).isSubprogram()) {
549 DILexicalBlockFile D(Scope);
550 Scope = D.isLexicalBlockFile()
551 ? D.getScope()
552 : DebugLoc::getFromDILexicalBlock(Scope).getScope(Ctxt);
553 }
554 if (!DISubprogram(Scope).describes(F))
555 return false;
556 }
557 }
558 }
Duncan P. N. Exon Smith176b6912014-10-03 20:01:09 +0000559 return DbgNode->getNumOperands() == 9 && getNumHeaderFields() == 12;
Bill Wendling523bea82013-11-08 08:13:15 +0000560}
561
Bill Wendling523bea82013-11-08 08:13:15 +0000562bool DIGlobalVariable::Verify() const {
563 if (!isGlobalVariable())
564 return false;
565
566 if (getDisplayName().empty())
567 return false;
Duncan P. N. Exon Smith176b6912014-10-03 20:01:09 +0000568 // Make sure context @ field 1 is an MDNode.
569 if (!fieldIsMDNode(DbgNode, 1))
Bill Wendling523bea82013-11-08 08:13:15 +0000570 return false;
Duncan P. N. Exon Smith176b6912014-10-03 20:01:09 +0000571 // Make sure that type @ field 3 is a DITypeRef.
572 if (!fieldIsTypeRef(DbgNode, 3))
Bill Wendling523bea82013-11-08 08:13:15 +0000573 return false;
Duncan P. N. Exon Smith176b6912014-10-03 20:01:09 +0000574 // Make sure StaticDataMemberDeclaration @ field 5 is MDNode.
575 if (!fieldIsMDNode(DbgNode, 5))
Bill Wendling523bea82013-11-08 08:13:15 +0000576 return false;
577
Duncan P. N. Exon Smith176b6912014-10-03 20:01:09 +0000578 return DbgNode->getNumOperands() == 6 && getNumHeaderFields() == 7;
Bill Wendling523bea82013-11-08 08:13:15 +0000579}
580
Bill Wendling523bea82013-11-08 08:13:15 +0000581bool DIVariable::Verify() const {
582 if (!isVariable())
583 return false;
584
Adrian Prantl1a1647c2014-03-18 02:34:58 +0000585 // Make sure context @ field 1 is an MDNode.
Bill Wendling523bea82013-11-08 08:13:15 +0000586 if (!fieldIsMDNode(DbgNode, 1))
587 return false;
Duncan P. N. Exon Smith176b6912014-10-03 20:01:09 +0000588 // Make sure that type @ field 3 is a DITypeRef.
589 if (!fieldIsTypeRef(DbgNode, 3))
590 return false;
591
592 // Check the number of header fields, which is common between complex and
593 // simple variables.
594 if (getNumHeaderFields() != 4)
Bill Wendling523bea82013-11-08 08:13:15 +0000595 return false;
Adrian Prantlda7d92e2014-06-30 17:17:35 +0000596
Adrian Prantl87b7eb92014-10-01 18:55:02 +0000597 // Variable without an inline location.
Duncan P. N. Exon Smith176b6912014-10-03 20:01:09 +0000598 if (DbgNode->getNumOperands() == 4)
Adrian Prantlda7d92e2014-06-30 17:17:35 +0000599 return true;
600
Duncan P. N. Exon Smith176b6912014-10-03 20:01:09 +0000601 // Variable with an inline location.
602 return getInlinedAt() != nullptr && DbgNode->getNumOperands() == 5;
Adrian Prantl87b7eb92014-10-01 18:55:02 +0000603}
604
Adrian Prantl87b7eb92014-10-01 18:55:02 +0000605bool DIExpression::Verify() const {
606 // Empty DIExpressions may be represented as a nullptr.
607 if (!DbgNode)
608 return true;
609
Duncan P. N. Exon Smith176b6912014-10-03 20:01:09 +0000610 return isExpression() && DbgNode->getNumOperands() == 1;
Bill Wendling523bea82013-11-08 08:13:15 +0000611}
612
Bill Wendling523bea82013-11-08 08:13:15 +0000613bool DILocation::Verify() const {
614 if (!DbgNode)
615 return false;
616
617 return DbgNode->getNumOperands() == 4;
618}
619
Bill Wendling523bea82013-11-08 08:13:15 +0000620bool DINameSpace::Verify() const {
621 if (!isNameSpace())
622 return false;
Duncan P. N. Exon Smith176b6912014-10-03 20:01:09 +0000623 return DbgNode->getNumOperands() == 3 && getNumHeaderFields() == 3;
Bill Wendling523bea82013-11-08 08:13:15 +0000624}
625
Bill Wendling523bea82013-11-08 08:13:15 +0000626MDNode *DIFile::getFileNode() const { return getNodeField(DbgNode, 1); }
627
Bill Wendling523bea82013-11-08 08:13:15 +0000628bool DIFile::Verify() const {
629 return isFile() && DbgNode->getNumOperands() == 2;
630}
631
Bill Wendling523bea82013-11-08 08:13:15 +0000632bool DIEnumerator::Verify() const {
Duncan P. N. Exon Smith176b6912014-10-03 20:01:09 +0000633 return isEnumerator() && DbgNode->getNumOperands() == 1 &&
634 getNumHeaderFields() == 3;
Bill Wendling523bea82013-11-08 08:13:15 +0000635}
636
Bill Wendling523bea82013-11-08 08:13:15 +0000637bool DISubrange::Verify() const {
Duncan P. N. Exon Smith176b6912014-10-03 20:01:09 +0000638 return isSubrange() && DbgNode->getNumOperands() == 1 &&
639 getNumHeaderFields() == 3;
Bill Wendling523bea82013-11-08 08:13:15 +0000640}
641
Bill Wendling523bea82013-11-08 08:13:15 +0000642bool DILexicalBlock::Verify() const {
Duncan P. N. Exon Smith176b6912014-10-03 20:01:09 +0000643 return isLexicalBlock() && DbgNode->getNumOperands() == 3 &&
644 getNumHeaderFields() == 4;
Bill Wendling523bea82013-11-08 08:13:15 +0000645}
646
Bill Wendling523bea82013-11-08 08:13:15 +0000647bool DILexicalBlockFile::Verify() const {
Duncan P. N. Exon Smith176b6912014-10-03 20:01:09 +0000648 return isLexicalBlockFile() && DbgNode->getNumOperands() == 3 &&
649 getNumHeaderFields() == 2;
Bill Wendling523bea82013-11-08 08:13:15 +0000650}
651
Bill Wendling523bea82013-11-08 08:13:15 +0000652bool DITemplateTypeParameter::Verify() const {
Duncan P. N. Exon Smith176b6912014-10-03 20:01:09 +0000653 return isTemplateTypeParameter() && DbgNode->getNumOperands() == 4 &&
654 getNumHeaderFields() == 4;
Bill Wendling523bea82013-11-08 08:13:15 +0000655}
656
Bill Wendling523bea82013-11-08 08:13:15 +0000657bool DITemplateValueParameter::Verify() const {
Duncan P. N. Exon Smith176b6912014-10-03 20:01:09 +0000658 return isTemplateValueParameter() && DbgNode->getNumOperands() == 5 &&
659 getNumHeaderFields() == 4;
Bill Wendling523bea82013-11-08 08:13:15 +0000660}
661
Bill Wendling523bea82013-11-08 08:13:15 +0000662bool DIImportedEntity::Verify() const {
Duncan P. N. Exon Smith176b6912014-10-03 20:01:09 +0000663 return isImportedEntity() && DbgNode->getNumOperands() == 3 &&
664 getNumHeaderFields() == 3;
Bill Wendling523bea82013-11-08 08:13:15 +0000665}
666
Bill Wendling523bea82013-11-08 08:13:15 +0000667MDNode *DIDerivedType::getObjCProperty() const {
Duncan P. N. Exon Smith176b6912014-10-03 20:01:09 +0000668 return getNodeField(DbgNode, 4);
Bill Wendling523bea82013-11-08 08:13:15 +0000669}
670
671MDString *DICompositeType::getIdentifier() const {
Duncan P. N. Exon Smith176b6912014-10-03 20:01:09 +0000672 return cast_or_null<MDString>(getField(DbgNode, 7));
Bill Wendling523bea82013-11-08 08:13:15 +0000673}
674
675#ifndef NDEBUG
676static void VerifySubsetOf(const MDNode *LHS, const MDNode *RHS) {
677 for (unsigned i = 0; i != LHS->getNumOperands(); ++i) {
678 // Skip the 'empty' list (that's a single i32 0, rather than truly empty).
679 if (i == 0 && isa<ConstantInt>(LHS->getOperand(i)))
680 continue;
681 const MDNode *E = cast<MDNode>(LHS->getOperand(i));
682 bool found = false;
683 for (unsigned j = 0; !found && j != RHS->getNumOperands(); ++j)
684 found = E == RHS->getOperand(j);
685 assert(found && "Losing a member during member list replacement");
686 }
687}
688#endif
689
Manman Ren1a125c92014-07-28 19:33:20 +0000690void DICompositeType::setArraysHelper(MDNode *Elements, MDNode *TParams) {
Bill Wendling523bea82013-11-08 08:13:15 +0000691 TrackingVH<MDNode> N(*this);
692 if (Elements) {
693#ifndef NDEBUG
694 // Check that the new list of members contains all the old members as well.
Duncan P. N. Exon Smith176b6912014-10-03 20:01:09 +0000695 if (const MDNode *El = cast_or_null<MDNode>(N->getOperand(4)))
Bill Wendling523bea82013-11-08 08:13:15 +0000696 VerifySubsetOf(El, Elements);
697#endif
Duncan P. N. Exon Smith176b6912014-10-03 20:01:09 +0000698 N->replaceOperandWith(4, Elements);
Bill Wendling523bea82013-11-08 08:13:15 +0000699 }
700 if (TParams)
Duncan P. N. Exon Smith176b6912014-10-03 20:01:09 +0000701 N->replaceOperandWith(6, TParams);
Bill Wendling523bea82013-11-08 08:13:15 +0000702 DbgNode = N;
703}
704
Bill Wendling523bea82013-11-08 08:13:15 +0000705DIScopeRef DIScope::getRef() const {
706 if (!isCompositeType())
707 return DIScopeRef(*this);
708 DICompositeType DTy(DbgNode);
709 if (!DTy.getIdentifier())
710 return DIScopeRef(*this);
711 return DIScopeRef(DTy.getIdentifier());
712}
713
Bill Wendling523bea82013-11-08 08:13:15 +0000714void DICompositeType::setContainingType(DICompositeType ContainingType) {
715 TrackingVH<MDNode> N(*this);
Duncan P. N. Exon Smith176b6912014-10-03 20:01:09 +0000716 N->replaceOperandWith(5, ContainingType.getRef());
Bill Wendling523bea82013-11-08 08:13:15 +0000717 DbgNode = N;
718}
719
Bill Wendling523bea82013-11-08 08:13:15 +0000720bool DIVariable::isInlinedFnArgument(const Function *CurFn) {
721 assert(CurFn && "Invalid function");
722 if (!getContext().isSubprogram())
723 return false;
724 // This variable is not inlined function argument if its scope
725 // does not describe current function.
726 return !DISubprogram(getContext()).describes(CurFn);
727}
728
Bill Wendling523bea82013-11-08 08:13:15 +0000729bool DISubprogram::describes(const Function *F) {
730 assert(F && "Invalid function");
731 if (F == getFunction())
732 return true;
733 StringRef Name = getLinkageName();
734 if (Name.empty())
735 Name = getName();
736 if (F->getName() == Name)
737 return true;
738 return false;
739}
740
Bill Wendling523bea82013-11-08 08:13:15 +0000741MDNode *DISubprogram::getVariablesNodes() const {
Duncan P. N. Exon Smith176b6912014-10-03 20:01:09 +0000742 return getNodeField(DbgNode, 8);
Bill Wendling523bea82013-11-08 08:13:15 +0000743}
744
745DIArray DISubprogram::getVariables() const {
Duncan P. N. Exon Smith176b6912014-10-03 20:01:09 +0000746 return DIArray(getNodeField(DbgNode, 8));
Bill Wendling523bea82013-11-08 08:13:15 +0000747}
748
749Value *DITemplateValueParameter::getValue() const {
Duncan P. N. Exon Smith176b6912014-10-03 20:01:09 +0000750 return getField(DbgNode, 3);
Bill Wendling523bea82013-11-08 08:13:15 +0000751}
752
Bill Wendling523bea82013-11-08 08:13:15 +0000753DIScopeRef DIScope::getContext() const {
754
755 if (isType())
756 return DIType(DbgNode).getContext();
757
758 if (isSubprogram())
759 return DIScopeRef(DISubprogram(DbgNode).getContext());
760
761 if (isLexicalBlock())
762 return DIScopeRef(DILexicalBlock(DbgNode).getContext());
763
764 if (isLexicalBlockFile())
765 return DIScopeRef(DILexicalBlockFile(DbgNode).getContext());
766
767 if (isNameSpace())
768 return DIScopeRef(DINameSpace(DbgNode).getContext());
769
770 assert((isFile() || isCompileUnit()) && "Unhandled type of scope.");
Craig Topperc6207612014-04-09 06:08:46 +0000771 return DIScopeRef(nullptr);
Bill Wendling523bea82013-11-08 08:13:15 +0000772}
773
Bill Wendling523bea82013-11-08 08:13:15 +0000774StringRef DIScope::getName() const {
775 if (isType())
776 return DIType(DbgNode).getName();
777 if (isSubprogram())
778 return DISubprogram(DbgNode).getName();
779 if (isNameSpace())
780 return DINameSpace(DbgNode).getName();
781 assert((isLexicalBlock() || isLexicalBlockFile() || isFile() ||
782 isCompileUnit()) &&
783 "Unhandled type of scope.");
784 return StringRef();
785}
786
787StringRef DIScope::getFilename() const {
788 if (!DbgNode)
789 return StringRef();
790 return ::getStringField(getNodeField(DbgNode, 1), 0);
791}
792
793StringRef DIScope::getDirectory() const {
794 if (!DbgNode)
795 return StringRef();
796 return ::getStringField(getNodeField(DbgNode, 1), 1);
797}
798
799DIArray DICompileUnit::getEnumTypes() const {
Duncan P. N. Exon Smith176b6912014-10-03 20:01:09 +0000800 if (!DbgNode || DbgNode->getNumOperands() < 7)
Bill Wendling523bea82013-11-08 08:13:15 +0000801 return DIArray();
802
Duncan P. N. Exon Smith176b6912014-10-03 20:01:09 +0000803 return DIArray(getNodeField(DbgNode, 2));
Bill Wendling523bea82013-11-08 08:13:15 +0000804}
805
806DIArray DICompileUnit::getRetainedTypes() const {
Duncan P. N. Exon Smith176b6912014-10-03 20:01:09 +0000807 if (!DbgNode || DbgNode->getNumOperands() < 7)
Bill Wendling523bea82013-11-08 08:13:15 +0000808 return DIArray();
809
Duncan P. N. Exon Smith176b6912014-10-03 20:01:09 +0000810 return DIArray(getNodeField(DbgNode, 3));
Bill Wendling523bea82013-11-08 08:13:15 +0000811}
812
813DIArray DICompileUnit::getSubprograms() const {
Duncan P. N. Exon Smith176b6912014-10-03 20:01:09 +0000814 if (!DbgNode || DbgNode->getNumOperands() < 7)
Bill Wendling523bea82013-11-08 08:13:15 +0000815 return DIArray();
816
Duncan P. N. Exon Smith176b6912014-10-03 20:01:09 +0000817 return DIArray(getNodeField(DbgNode, 4));
Bill Wendling523bea82013-11-08 08:13:15 +0000818}
819
820DIArray DICompileUnit::getGlobalVariables() const {
Duncan P. N. Exon Smith176b6912014-10-03 20:01:09 +0000821 if (!DbgNode || DbgNode->getNumOperands() < 7)
Bill Wendling523bea82013-11-08 08:13:15 +0000822 return DIArray();
823
Duncan P. N. Exon Smith176b6912014-10-03 20:01:09 +0000824 return DIArray(getNodeField(DbgNode, 5));
Bill Wendling523bea82013-11-08 08:13:15 +0000825}
826
827DIArray DICompileUnit::getImportedEntities() const {
Duncan P. N. Exon Smith176b6912014-10-03 20:01:09 +0000828 if (!DbgNode || DbgNode->getNumOperands() < 7)
Bill Wendling523bea82013-11-08 08:13:15 +0000829 return DIArray();
830
Duncan P. N. Exon Smith176b6912014-10-03 20:01:09 +0000831 return DIArray(getNodeField(DbgNode, 6));
832}
833
834void DICompileUnit::replaceSubprograms(DIArray Subprograms) {
835 assert(Verify() && "Expected compile unit");
836 if (Subprograms == getSubprograms())
837 return;
838
839 const_cast<MDNode *>(DbgNode)->replaceOperandWith(4, Subprograms);
840}
841
842void DICompileUnit::replaceGlobalVariables(DIArray GlobalVariables) {
843 assert(Verify() && "Expected compile unit");
844 if (GlobalVariables == getGlobalVariables())
845 return;
846
847 const_cast<MDNode *>(DbgNode)->replaceOperandWith(5, GlobalVariables);
Bill Wendling523bea82013-11-08 08:13:15 +0000848}
849
Diego Novillof5041ce2014-03-03 20:06:11 +0000850DILocation DILocation::copyWithNewScope(LLVMContext &Ctx,
David Blaikie2f3f76f2014-08-21 22:45:21 +0000851 DILexicalBlockFile NewScope) {
Diego Novillof5041ce2014-03-03 20:06:11 +0000852 SmallVector<Value *, 10> Elts;
853 assert(Verify());
854 for (unsigned I = 0; I < DbgNode->getNumOperands(); ++I) {
855 if (I != 2)
856 Elts.push_back(DbgNode->getOperand(I));
857 else
858 Elts.push_back(NewScope);
859 }
860 MDNode *NewDIL = MDNode::get(Ctx, Elts);
861 return DILocation(NewDIL);
862}
863
Diego Novillof5041ce2014-03-03 20:06:11 +0000864unsigned DILocation::computeNewDiscriminator(LLVMContext &Ctx) {
865 std::pair<const char *, unsigned> Key(getFilename().data(), getLineNumber());
866 return ++Ctx.pImpl->DiscriminatorTable[Key];
867}
868
Bill Wendling523bea82013-11-08 08:13:15 +0000869DIVariable llvm::createInlinedVariable(MDNode *DV, MDNode *InlinedScope,
870 LLVMContext &VMContext) {
Duncan P. N. Exon Smith176b6912014-10-03 20:01:09 +0000871 assert(DIVariable(DV).Verify() && "Expected a DIVariable");
872 if (!InlinedScope)
873 return cleanseInlinedVariable(DV, VMContext);
874
875 // Insert inlined scope.
876 SmallVector<Value *, 8> Elts;
877 for (unsigned I = 0, E = DIVariableInlinedAtIndex; I != E; ++I)
878 Elts.push_back(DV->getOperand(I));
879 Elts.push_back(InlinedScope);
880
881 DIVariable Inlined(MDNode::get(VMContext, Elts));
882 assert(Inlined.Verify() && "Expected to create a DIVariable");
883 return Inlined;
Bill Wendling523bea82013-11-08 08:13:15 +0000884}
885
Bill Wendling523bea82013-11-08 08:13:15 +0000886DIVariable llvm::cleanseInlinedVariable(MDNode *DV, LLVMContext &VMContext) {
Duncan P. N. Exon Smith176b6912014-10-03 20:01:09 +0000887 assert(DIVariable(DV).Verify() && "Expected a DIVariable");
888 if (!DIVariable(DV).getInlinedAt())
889 return DIVariable(DV);
890
891 // Remove inlined scope.
892 SmallVector<Value *, 8> Elts;
893 for (unsigned I = 0, E = DIVariableInlinedAtIndex; I != E; ++I)
894 Elts.push_back(DV->getOperand(I));
895
896 DIVariable Cleansed(MDNode::get(VMContext, Elts));
897 assert(Cleansed.Verify() && "Expected to create a DIVariable");
898 return Cleansed;
Bill Wendling523bea82013-11-08 08:13:15 +0000899}
900
Bill Wendling523bea82013-11-08 08:13:15 +0000901DISubprogram llvm::getDISubprogram(const MDNode *Scope) {
902 DIDescriptor D(Scope);
903 if (D.isSubprogram())
904 return DISubprogram(Scope);
905
906 if (D.isLexicalBlockFile())
907 return getDISubprogram(DILexicalBlockFile(Scope).getContext());
908
909 if (D.isLexicalBlock())
910 return getDISubprogram(DILexicalBlock(Scope).getContext());
911
912 return DISubprogram();
913}
914
Timur Iskhodzhanoveb229ca2014-10-23 23:46:28 +0000915DISubprogram llvm::getDISubprogram(const Function *F) {
916 // We look for the first instr that has a debug annotation leading back to F.
917 const LLVMContext &Ctx = F->getParent()->getContext();
918 for (auto &BB : *F) {
919 for (auto &Inst : BB.getInstList()) {
920 DebugLoc DLoc = Inst.getDebugLoc();
921 if (DLoc.isUnknown())
922 continue;
923 const MDNode *Scope = DLoc.getScopeNode(Ctx);
924 DISubprogram Subprogram = getDISubprogram(Scope);
925 if (Subprogram.describes(F))
926 return Subprogram;
927 }
928 }
929
930 return DISubprogram();
931}
932
Bill Wendling523bea82013-11-08 08:13:15 +0000933DICompositeType llvm::getDICompositeType(DIType T) {
934 if (T.isCompositeType())
935 return DICompositeType(T);
936
937 if (T.isDerivedType()) {
938 // This function is currently used by dragonegg and dragonegg does
939 // not generate identifier for types, so using an empty map to resolve
940 // DerivedFrom should be fine.
941 DITypeIdentifierMap EmptyMap;
942 return getDICompositeType(
943 DIDerivedType(T).getTypeDerivedFrom().resolve(EmptyMap));
944 }
945
946 return DICompositeType();
947}
948
Bill Wendling523bea82013-11-08 08:13:15 +0000949DITypeIdentifierMap
950llvm::generateDITypeIdentifierMap(const NamedMDNode *CU_Nodes) {
951 DITypeIdentifierMap Map;
952 for (unsigned CUi = 0, CUe = CU_Nodes->getNumOperands(); CUi != CUe; ++CUi) {
953 DICompileUnit CU(CU_Nodes->getOperand(CUi));
954 DIArray Retain = CU.getRetainedTypes();
955 for (unsigned Ti = 0, Te = Retain.getNumElements(); Ti != Te; ++Ti) {
956 if (!Retain.getElement(Ti).isCompositeType())
957 continue;
958 DICompositeType Ty(Retain.getElement(Ti));
959 if (MDString *TypeId = Ty.getIdentifier()) {
960 // Definition has priority over declaration.
961 // Try to insert (TypeId, Ty) to Map.
962 std::pair<DITypeIdentifierMap::iterator, bool> P =
963 Map.insert(std::make_pair(TypeId, Ty));
964 // If TypeId already exists in Map and this is a definition, replace
965 // whatever we had (declaration or definition) with the definition.
966 if (!P.second && !Ty.isForwardDecl())
967 P.first->second = Ty;
968 }
969 }
970 }
971 return Map;
972}
973
974//===----------------------------------------------------------------------===//
975// DebugInfoFinder implementations.
976//===----------------------------------------------------------------------===//
977
978void DebugInfoFinder::reset() {
979 CUs.clear();
980 SPs.clear();
981 GVs.clear();
982 TYs.clear();
983 Scopes.clear();
984 NodesSeen.clear();
985 TypeIdentifierMap.clear();
Manman Ren2085ccc2013-11-17 18:42:37 +0000986 TypeMapInitialized = false;
987}
988
Manman Renb46e5502013-11-17 19:35:03 +0000989void DebugInfoFinder::InitializeTypeMap(const Module &M) {
Manman Ren2085ccc2013-11-17 18:42:37 +0000990 if (!TypeMapInitialized)
991 if (NamedMDNode *CU_Nodes = M.getNamedMetadata("llvm.dbg.cu")) {
992 TypeIdentifierMap = generateDITypeIdentifierMap(CU_Nodes);
993 TypeMapInitialized = true;
994 }
Bill Wendling523bea82013-11-08 08:13:15 +0000995}
996
Bill Wendling523bea82013-11-08 08:13:15 +0000997void DebugInfoFinder::processModule(const Module &M) {
Manman Renb46e5502013-11-17 19:35:03 +0000998 InitializeTypeMap(M);
Bill Wendling523bea82013-11-08 08:13:15 +0000999 if (NamedMDNode *CU_Nodes = M.getNamedMetadata("llvm.dbg.cu")) {
Bill Wendling523bea82013-11-08 08:13:15 +00001000 for (unsigned i = 0, e = CU_Nodes->getNumOperands(); i != e; ++i) {
1001 DICompileUnit CU(CU_Nodes->getOperand(i));
1002 addCompileUnit(CU);
1003 DIArray GVs = CU.getGlobalVariables();
1004 for (unsigned i = 0, e = GVs.getNumElements(); i != e; ++i) {
1005 DIGlobalVariable DIG(GVs.getElement(i));
1006 if (addGlobalVariable(DIG)) {
1007 processScope(DIG.getContext());
Adrian Prantl1a1647c2014-03-18 02:34:58 +00001008 processType(DIG.getType().resolve(TypeIdentifierMap));
Bill Wendling523bea82013-11-08 08:13:15 +00001009 }
1010 }
1011 DIArray SPs = CU.getSubprograms();
1012 for (unsigned i = 0, e = SPs.getNumElements(); i != e; ++i)
1013 processSubprogram(DISubprogram(SPs.getElement(i)));
1014 DIArray EnumTypes = CU.getEnumTypes();
1015 for (unsigned i = 0, e = EnumTypes.getNumElements(); i != e; ++i)
1016 processType(DIType(EnumTypes.getElement(i)));
1017 DIArray RetainedTypes = CU.getRetainedTypes();
1018 for (unsigned i = 0, e = RetainedTypes.getNumElements(); i != e; ++i)
1019 processType(DIType(RetainedTypes.getElement(i)));
1020 DIArray Imports = CU.getImportedEntities();
1021 for (unsigned i = 0, e = Imports.getNumElements(); i != e; ++i) {
1022 DIImportedEntity Import = DIImportedEntity(Imports.getElement(i));
Adrian Prantld09ba232014-04-01 03:41:04 +00001023 DIDescriptor Entity = Import.getEntity().resolve(TypeIdentifierMap);
Bill Wendling523bea82013-11-08 08:13:15 +00001024 if (Entity.isType())
1025 processType(DIType(Entity));
1026 else if (Entity.isSubprogram())
1027 processSubprogram(DISubprogram(Entity));
1028 else if (Entity.isNameSpace())
1029 processScope(DINameSpace(Entity).getContext());
1030 }
1031 }
1032 }
1033}
1034
Manman Ren2085ccc2013-11-17 18:42:37 +00001035void DebugInfoFinder::processLocation(const Module &M, DILocation Loc) {
Bill Wendling523bea82013-11-08 08:13:15 +00001036 if (!Loc)
1037 return;
Manman Renb46e5502013-11-17 19:35:03 +00001038 InitializeTypeMap(M);
Bill Wendling523bea82013-11-08 08:13:15 +00001039 processScope(Loc.getScope());
Manman Ren2085ccc2013-11-17 18:42:37 +00001040 processLocation(M, Loc.getOrigLocation());
Bill Wendling523bea82013-11-08 08:13:15 +00001041}
1042
Bill Wendling523bea82013-11-08 08:13:15 +00001043void DebugInfoFinder::processType(DIType DT) {
1044 if (!addType(DT))
1045 return;
1046 processScope(DT.getContext().resolve(TypeIdentifierMap));
1047 if (DT.isCompositeType()) {
1048 DICompositeType DCT(DT);
1049 processType(DCT.getTypeDerivedFrom().resolve(TypeIdentifierMap));
Manman Renf8a19672014-07-28 22:24:06 +00001050 if (DT.isSubroutineType()) {
1051 DITypeArray DTA = DISubroutineType(DT).getTypeArray();
1052 for (unsigned i = 0, e = DTA.getNumElements(); i != e; ++i)
1053 processType(DTA.getElement(i).resolve(TypeIdentifierMap));
1054 return;
1055 }
Manman Renab8ffba2014-07-28 19:14:13 +00001056 DIArray DA = DCT.getElements();
Bill Wendling523bea82013-11-08 08:13:15 +00001057 for (unsigned i = 0, e = DA.getNumElements(); i != e; ++i) {
1058 DIDescriptor D = DA.getElement(i);
1059 if (D.isType())
1060 processType(DIType(D));
1061 else if (D.isSubprogram())
1062 processSubprogram(DISubprogram(D));
1063 }
1064 } else if (DT.isDerivedType()) {
1065 DIDerivedType DDT(DT);
1066 processType(DDT.getTypeDerivedFrom().resolve(TypeIdentifierMap));
1067 }
1068}
1069
1070void DebugInfoFinder::processScope(DIScope Scope) {
1071 if (Scope.isType()) {
1072 DIType Ty(Scope);
1073 processType(Ty);
1074 return;
1075 }
1076 if (Scope.isCompileUnit()) {
1077 addCompileUnit(DICompileUnit(Scope));
1078 return;
1079 }
1080 if (Scope.isSubprogram()) {
1081 processSubprogram(DISubprogram(Scope));
1082 return;
1083 }
1084 if (!addScope(Scope))
1085 return;
1086 if (Scope.isLexicalBlock()) {
1087 DILexicalBlock LB(Scope);
1088 processScope(LB.getContext());
1089 } else if (Scope.isLexicalBlockFile()) {
1090 DILexicalBlockFile LBF = DILexicalBlockFile(Scope);
1091 processScope(LBF.getScope());
1092 } else if (Scope.isNameSpace()) {
1093 DINameSpace NS(Scope);
1094 processScope(NS.getContext());
1095 }
1096}
1097
Bill Wendling523bea82013-11-08 08:13:15 +00001098void DebugInfoFinder::processSubprogram(DISubprogram SP) {
1099 if (!addSubprogram(SP))
1100 return;
1101 processScope(SP.getContext().resolve(TypeIdentifierMap));
1102 processType(SP.getType());
1103 DIArray TParams = SP.getTemplateParams();
1104 for (unsigned I = 0, E = TParams.getNumElements(); I != E; ++I) {
1105 DIDescriptor Element = TParams.getElement(I);
1106 if (Element.isTemplateTypeParameter()) {
1107 DITemplateTypeParameter TType(Element);
1108 processScope(TType.getContext().resolve(TypeIdentifierMap));
1109 processType(TType.getType().resolve(TypeIdentifierMap));
1110 } else if (Element.isTemplateValueParameter()) {
1111 DITemplateValueParameter TVal(Element);
1112 processScope(TVal.getContext().resolve(TypeIdentifierMap));
1113 processType(TVal.getType().resolve(TypeIdentifierMap));
1114 }
1115 }
1116}
1117
Manman Ren2085ccc2013-11-17 18:42:37 +00001118void DebugInfoFinder::processDeclare(const Module &M,
1119 const DbgDeclareInst *DDI) {
Bill Wendling523bea82013-11-08 08:13:15 +00001120 MDNode *N = dyn_cast<MDNode>(DDI->getVariable());
1121 if (!N)
1122 return;
Manman Renb46e5502013-11-17 19:35:03 +00001123 InitializeTypeMap(M);
Bill Wendling523bea82013-11-08 08:13:15 +00001124
1125 DIDescriptor DV(N);
1126 if (!DV.isVariable())
1127 return;
1128
1129 if (!NodesSeen.insert(DV))
1130 return;
1131 processScope(DIVariable(N).getContext());
Adrian Prantl1a1647c2014-03-18 02:34:58 +00001132 processType(DIVariable(N).getType().resolve(TypeIdentifierMap));
Bill Wendling523bea82013-11-08 08:13:15 +00001133}
1134
Manman Ren2085ccc2013-11-17 18:42:37 +00001135void DebugInfoFinder::processValue(const Module &M, const DbgValueInst *DVI) {
Bill Wendling523bea82013-11-08 08:13:15 +00001136 MDNode *N = dyn_cast<MDNode>(DVI->getVariable());
1137 if (!N)
1138 return;
Manman Renb46e5502013-11-17 19:35:03 +00001139 InitializeTypeMap(M);
Bill Wendling523bea82013-11-08 08:13:15 +00001140
1141 DIDescriptor DV(N);
1142 if (!DV.isVariable())
1143 return;
1144
1145 if (!NodesSeen.insert(DV))
1146 return;
1147 processScope(DIVariable(N).getContext());
Adrian Prantl1a1647c2014-03-18 02:34:58 +00001148 processType(DIVariable(N).getType().resolve(TypeIdentifierMap));
Bill Wendling523bea82013-11-08 08:13:15 +00001149}
1150
Bill Wendling523bea82013-11-08 08:13:15 +00001151bool DebugInfoFinder::addType(DIType DT) {
1152 if (!DT)
1153 return false;
1154
1155 if (!NodesSeen.insert(DT))
1156 return false;
1157
1158 TYs.push_back(DT);
1159 return true;
1160}
1161
Bill Wendling523bea82013-11-08 08:13:15 +00001162bool DebugInfoFinder::addCompileUnit(DICompileUnit CU) {
1163 if (!CU)
1164 return false;
1165 if (!NodesSeen.insert(CU))
1166 return false;
1167
1168 CUs.push_back(CU);
1169 return true;
1170}
1171
Bill Wendling523bea82013-11-08 08:13:15 +00001172bool DebugInfoFinder::addGlobalVariable(DIGlobalVariable DIG) {
1173 if (!DIG)
1174 return false;
1175
1176 if (!NodesSeen.insert(DIG))
1177 return false;
1178
1179 GVs.push_back(DIG);
1180 return true;
1181}
1182
Bill Wendling523bea82013-11-08 08:13:15 +00001183bool DebugInfoFinder::addSubprogram(DISubprogram SP) {
1184 if (!SP)
1185 return false;
1186
1187 if (!NodesSeen.insert(SP))
1188 return false;
1189
1190 SPs.push_back(SP);
1191 return true;
1192}
1193
1194bool DebugInfoFinder::addScope(DIScope Scope) {
1195 if (!Scope)
1196 return false;
1197 // FIXME: Ocaml binding generates a scope with no content, we treat it
1198 // as null for now.
1199 if (Scope->getNumOperands() == 0)
1200 return false;
1201 if (!NodesSeen.insert(Scope))
1202 return false;
1203 Scopes.push_back(Scope);
1204 return true;
1205}
1206
1207//===----------------------------------------------------------------------===//
1208// DIDescriptor: dump routines for all descriptors.
1209//===----------------------------------------------------------------------===//
1210
Bill Wendling523bea82013-11-08 08:13:15 +00001211void DIDescriptor::dump() const {
1212 print(dbgs());
1213 dbgs() << '\n';
1214}
1215
Bill Wendling523bea82013-11-08 08:13:15 +00001216void DIDescriptor::print(raw_ostream &OS) const {
1217 if (!DbgNode)
1218 return;
1219
1220 if (const char *Tag = dwarf::TagString(getTag()))
1221 OS << "[ " << Tag << " ]";
1222
1223 if (this->isSubrange()) {
1224 DISubrange(DbgNode).printInternal(OS);
1225 } else if (this->isCompileUnit()) {
1226 DICompileUnit(DbgNode).printInternal(OS);
1227 } else if (this->isFile()) {
1228 DIFile(DbgNode).printInternal(OS);
1229 } else if (this->isEnumerator()) {
1230 DIEnumerator(DbgNode).printInternal(OS);
1231 } else if (this->isBasicType()) {
1232 DIType(DbgNode).printInternal(OS);
1233 } else if (this->isDerivedType()) {
1234 DIDerivedType(DbgNode).printInternal(OS);
1235 } else if (this->isCompositeType()) {
1236 DICompositeType(DbgNode).printInternal(OS);
1237 } else if (this->isSubprogram()) {
1238 DISubprogram(DbgNode).printInternal(OS);
1239 } else if (this->isGlobalVariable()) {
1240 DIGlobalVariable(DbgNode).printInternal(OS);
1241 } else if (this->isVariable()) {
1242 DIVariable(DbgNode).printInternal(OS);
1243 } else if (this->isObjCProperty()) {
1244 DIObjCProperty(DbgNode).printInternal(OS);
1245 } else if (this->isNameSpace()) {
1246 DINameSpace(DbgNode).printInternal(OS);
1247 } else if (this->isScope()) {
1248 DIScope(DbgNode).printInternal(OS);
Adrian Prantl87b7eb92014-10-01 18:55:02 +00001249 } else if (this->isExpression()) {
1250 DIExpression(DbgNode).printInternal(OS);
Bill Wendling523bea82013-11-08 08:13:15 +00001251 }
1252}
1253
1254void DISubrange::printInternal(raw_ostream &OS) const {
1255 int64_t Count = getCount();
1256 if (Count != -1)
1257 OS << " [" << getLo() << ", " << Count - 1 << ']';
1258 else
1259 OS << " [unbounded]";
1260}
1261
1262void DIScope::printInternal(raw_ostream &OS) const {
1263 OS << " [" << getDirectory() << "/" << getFilename() << ']';
1264}
1265
1266void DICompileUnit::printInternal(raw_ostream &OS) const {
1267 DIScope::printInternal(OS);
1268 OS << " [";
1269 unsigned Lang = getLanguage();
1270 if (const char *LangStr = dwarf::LanguageString(Lang))
1271 OS << LangStr;
1272 else
1273 (OS << "lang 0x").write_hex(Lang);
1274 OS << ']';
1275}
1276
1277void DIEnumerator::printInternal(raw_ostream &OS) const {
1278 OS << " [" << getName() << " :: " << getEnumValue() << ']';
1279}
1280
1281void DIType::printInternal(raw_ostream &OS) const {
Manman Renf93ac4b2014-07-29 18:20:39 +00001282 if (!DbgNode)
Bill Wendling523bea82013-11-08 08:13:15 +00001283 return;
1284
1285 StringRef Res = getName();
1286 if (!Res.empty())
1287 OS << " [" << Res << "]";
1288
1289 // TODO: Print context?
1290
1291 OS << " [line " << getLineNumber() << ", size " << getSizeInBits()
1292 << ", align " << getAlignInBits() << ", offset " << getOffsetInBits();
1293 if (isBasicType())
1294 if (const char *Enc =
1295 dwarf::AttributeEncodingString(DIBasicType(DbgNode).getEncoding()))
1296 OS << ", enc " << Enc;
1297 OS << "]";
1298
1299 if (isPrivate())
1300 OS << " [private]";
1301 else if (isProtected())
1302 OS << " [protected]";
Adrian Prantldaedfda2014-08-29 22:44:07 +00001303 else if (isPublic())
1304 OS << " [public]";
Bill Wendling523bea82013-11-08 08:13:15 +00001305
1306 if (isArtificial())
1307 OS << " [artificial]";
1308
1309 if (isForwardDecl())
1310 OS << " [decl]";
1311 else if (getTag() == dwarf::DW_TAG_structure_type ||
1312 getTag() == dwarf::DW_TAG_union_type ||
1313 getTag() == dwarf::DW_TAG_enumeration_type ||
1314 getTag() == dwarf::DW_TAG_class_type)
1315 OS << " [def]";
1316 if (isVector())
1317 OS << " [vector]";
1318 if (isStaticMember())
1319 OS << " [static]";
Adrian Prantl99c7af22013-12-18 21:48:19 +00001320
1321 if (isLValueReference())
1322 OS << " [reference]";
1323
1324 if (isRValueReference())
1325 OS << " [rvalue reference]";
Bill Wendling523bea82013-11-08 08:13:15 +00001326}
1327
1328void DIDerivedType::printInternal(raw_ostream &OS) const {
1329 DIType::printInternal(OS);
1330 OS << " [from " << getTypeDerivedFrom().getName() << ']';
1331}
1332
1333void DICompositeType::printInternal(raw_ostream &OS) const {
1334 DIType::printInternal(OS);
Manman Renab8ffba2014-07-28 19:14:13 +00001335 DIArray A = getElements();
Bill Wendling523bea82013-11-08 08:13:15 +00001336 OS << " [" << A.getNumElements() << " elements]";
1337}
1338
1339void DINameSpace::printInternal(raw_ostream &OS) const {
1340 StringRef Name = getName();
1341 if (!Name.empty())
1342 OS << " [" << Name << ']';
1343
1344 OS << " [line " << getLineNumber() << ']';
1345}
1346
1347void DISubprogram::printInternal(raw_ostream &OS) const {
1348 // TODO : Print context
1349 OS << " [line " << getLineNumber() << ']';
1350
1351 if (isLocalToUnit())
1352 OS << " [local]";
1353
1354 if (isDefinition())
1355 OS << " [def]";
1356
1357 if (getScopeLineNumber() != getLineNumber())
1358 OS << " [scope " << getScopeLineNumber() << "]";
1359
1360 if (isPrivate())
1361 OS << " [private]";
1362 else if (isProtected())
1363 OS << " [protected]";
Adrian Prantldaedfda2014-08-29 22:44:07 +00001364 else if (isPublic())
1365 OS << " [public]";
Bill Wendling523bea82013-11-08 08:13:15 +00001366
Adrian Prantl99c7af22013-12-18 21:48:19 +00001367 if (isLValueReference())
1368 OS << " [reference]";
1369
1370 if (isRValueReference())
1371 OS << " [rvalue reference]";
1372
Bill Wendling523bea82013-11-08 08:13:15 +00001373 StringRef Res = getName();
1374 if (!Res.empty())
1375 OS << " [" << Res << ']';
1376}
1377
1378void DIGlobalVariable::printInternal(raw_ostream &OS) const {
1379 StringRef Res = getName();
1380 if (!Res.empty())
1381 OS << " [" << Res << ']';
1382
1383 OS << " [line " << getLineNumber() << ']';
1384
1385 // TODO : Print context
1386
1387 if (isLocalToUnit())
1388 OS << " [local]";
1389
1390 if (isDefinition())
1391 OS << " [def]";
1392}
1393
1394void DIVariable::printInternal(raw_ostream &OS) const {
1395 StringRef Res = getName();
1396 if (!Res.empty())
1397 OS << " [" << Res << ']';
1398
1399 OS << " [line " << getLineNumber() << ']';
Adrian Prantl87b7eb92014-10-01 18:55:02 +00001400}
Adrian Prantlb1416832014-08-01 22:11:58 +00001401
Adrian Prantl87b7eb92014-10-01 18:55:02 +00001402void DIExpression::printInternal(raw_ostream &OS) const {
1403 for (unsigned I = 0; I < getNumElements(); ++I) {
1404 uint64_t OpCode = getElement(I);
1405 OS << " [" << OperationEncodingString(OpCode);
1406 switch (OpCode) {
1407 case DW_OP_plus: {
1408 OS << " " << getElement(++I);
1409 break;
1410 }
1411 case DW_OP_piece: {
1412 unsigned Offset = getElement(++I);
1413 unsigned Size = getElement(++I);
Adrian Prantl38666f12014-10-02 16:42:15 +00001414 OS << " offset=" << Offset << ", size=" << Size;
Adrian Prantl87b7eb92014-10-01 18:55:02 +00001415 break;
1416 }
1417 default:
Adrian Prantl75a0dac2014-10-02 16:42:13 +00001418 // Else bail out early. This may be a line table entry.
1419 OS << "Unknown]";
1420 return;
Adrian Prantl87b7eb92014-10-01 18:55:02 +00001421 }
1422 OS << "]";
1423 }
Bill Wendling523bea82013-11-08 08:13:15 +00001424}
1425
1426void DIObjCProperty::printInternal(raw_ostream &OS) const {
1427 StringRef Name = getObjCPropertyName();
1428 if (!Name.empty())
1429 OS << " [" << Name << ']';
1430
1431 OS << " [line " << getLineNumber() << ", properties " << getUnsignedField(6)
1432 << ']';
1433}
1434
1435static void printDebugLoc(DebugLoc DL, raw_ostream &CommentOS,
1436 const LLVMContext &Ctx) {
1437 if (!DL.isUnknown()) { // Print source line info.
1438 DIScope Scope(DL.getScope(Ctx));
1439 assert(Scope.isScope() && "Scope of a DebugLoc should be a DIScope.");
1440 // Omit the directory, because it's likely to be long and uninteresting.
1441 CommentOS << Scope.getFilename();
1442 CommentOS << ':' << DL.getLine();
1443 if (DL.getCol() != 0)
1444 CommentOS << ':' << DL.getCol();
1445 DebugLoc InlinedAtDL = DebugLoc::getFromDILocation(DL.getInlinedAt(Ctx));
1446 if (!InlinedAtDL.isUnknown()) {
1447 CommentOS << " @[ ";
1448 printDebugLoc(InlinedAtDL, CommentOS, Ctx);
1449 CommentOS << " ]";
1450 }
1451 }
1452}
1453
1454void DIVariable::printExtendedName(raw_ostream &OS) const {
1455 const LLVMContext &Ctx = DbgNode->getContext();
1456 StringRef Res = getName();
1457 if (!Res.empty())
1458 OS << Res << "," << getLineNumber();
1459 if (MDNode *InlinedAt = getInlinedAt()) {
1460 DebugLoc InlinedAtDL = DebugLoc::getFromDILocation(InlinedAt);
1461 if (!InlinedAtDL.isUnknown()) {
1462 OS << " @[";
1463 printDebugLoc(InlinedAtDL, OS, Ctx);
1464 OS << "]";
1465 }
1466 }
1467}
1468
Bill Wendling523bea82013-11-08 08:13:15 +00001469template <> DIRef<DIScope>::DIRef(const Value *V) : Val(V) {
1470 assert(isScopeRef(V) && "DIScopeRef should be a MDString or MDNode");
1471}
1472template <> DIRef<DIType>::DIRef(const Value *V) : Val(V) {
1473 assert(isTypeRef(V) && "DITypeRef should be a MDString or MDNode");
1474}
1475
Bill Wendling523bea82013-11-08 08:13:15 +00001476template <>
1477DIScopeRef DIDescriptor::getFieldAs<DIScopeRef>(unsigned Elt) const {
1478 return DIScopeRef(getField(DbgNode, Elt));
1479}
Bill Wendling523bea82013-11-08 08:13:15 +00001480template <> DITypeRef DIDescriptor::getFieldAs<DITypeRef>(unsigned Elt) const {
1481 return DITypeRef(getField(DbgNode, Elt));
1482}
Manman Rencb14bbc2013-11-22 22:06:31 +00001483
Manman Rencb14bbc2013-11-22 22:06:31 +00001484bool llvm::StripDebugInfo(Module &M) {
Manman Rencb14bbc2013-11-22 22:06:31 +00001485 bool Changed = false;
1486
1487 // Remove all of the calls to the debugger intrinsics, and remove them from
1488 // the module.
1489 if (Function *Declare = M.getFunction("llvm.dbg.declare")) {
1490 while (!Declare->use_empty()) {
Chandler Carruthcdf47882014-03-09 03:16:01 +00001491 CallInst *CI = cast<CallInst>(Declare->user_back());
Manman Rencb14bbc2013-11-22 22:06:31 +00001492 CI->eraseFromParent();
1493 }
1494 Declare->eraseFromParent();
1495 Changed = true;
1496 }
1497
1498 if (Function *DbgVal = M.getFunction("llvm.dbg.value")) {
1499 while (!DbgVal->use_empty()) {
Chandler Carruthcdf47882014-03-09 03:16:01 +00001500 CallInst *CI = cast<CallInst>(DbgVal->user_back());
Manman Rencb14bbc2013-11-22 22:06:31 +00001501 CI->eraseFromParent();
1502 }
1503 DbgVal->eraseFromParent();
1504 Changed = true;
1505 }
1506
1507 for (Module::named_metadata_iterator NMI = M.named_metadata_begin(),
1508 NME = M.named_metadata_end(); NMI != NME;) {
1509 NamedMDNode *NMD = NMI;
1510 ++NMI;
1511 if (NMD->getName().startswith("llvm.dbg.")) {
1512 NMD->eraseFromParent();
1513 Changed = true;
1514 }
1515 }
1516
1517 for (Module::iterator MI = M.begin(), ME = M.end(); MI != ME; ++MI)
1518 for (Function::iterator FI = MI->begin(), FE = MI->end(); FI != FE;
1519 ++FI)
1520 for (BasicBlock::iterator BI = FI->begin(), BE = FI->end(); BI != BE;
1521 ++BI) {
1522 if (!BI->getDebugLoc().isUnknown()) {
1523 Changed = true;
1524 BI->setDebugLoc(DebugLoc());
1525 }
1526 }
1527
1528 return Changed;
1529}
Manman Ren8b4306c2013-12-02 21:29:56 +00001530
Manman Renbd4daf82013-12-03 00:12:14 +00001531unsigned llvm::getDebugMetadataVersionFromModule(const Module &M) {
Manman Ren8b4306c2013-12-02 21:29:56 +00001532 Value *Val = M.getModuleFlag("Debug Info Version");
1533 if (!Val)
1534 return 0;
1535 return cast<ConstantInt>(Val)->getZExtValue();
1536}
David Blaikie6876b3b2014-07-01 20:05:26 +00001537
David Blaikiea8c35092014-07-02 18:30:05 +00001538llvm::DenseMap<const llvm::Function *, llvm::DISubprogram>
1539llvm::makeSubprogramMap(const Module &M) {
1540 DenseMap<const Function *, DISubprogram> R;
David Blaikie6876b3b2014-07-01 20:05:26 +00001541
1542 NamedMDNode *CU_Nodes = M.getNamedMetadata("llvm.dbg.cu");
1543 if (!CU_Nodes)
1544 return R;
1545
1546 for (MDNode *N : CU_Nodes->operands()) {
1547 DICompileUnit CUNode(N);
1548 DIArray SPs = CUNode.getSubprograms();
1549 for (unsigned i = 0, e = SPs.getNumElements(); i != e; ++i) {
1550 DISubprogram SP(SPs.getElement(i));
1551 if (Function *F = SP.getFunction())
1552 R.insert(std::make_pair(F, SP));
1553 }
1554 }
1555 return R;
1556}