blob: 76836fd95517870f27aeef1c65d132fc68ae19dc [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
Duncan P. N. Exon Smith5bf8fef2014-12-09 18:38:53 +000055static Metadata *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 {
Duncan P. N. Exon Smith5bf8fef2014-12-09 18:38:53 +000076 if (auto *C = getConstantField(Elt))
77 if (ConstantInt *CI = dyn_cast<ConstantInt>(C))
Bill Wendling523bea82013-11-08 08:13:15 +000078 return CI->getZExtValue();
79
80 return 0;
81}
82
83int64_t DIDescriptor::getInt64Field(unsigned Elt) const {
Duncan P. N. Exon Smith5bf8fef2014-12-09 18:38:53 +000084 if (auto *C = getConstantField(Elt))
85 if (ConstantInt *CI = dyn_cast<ConstantInt>(C))
86 return CI->getZExtValue();
Bill Wendling523bea82013-11-08 08:13:15 +000087
88 return 0;
89}
90
91DIDescriptor DIDescriptor::getDescriptorField(unsigned Elt) const {
92 MDNode *Field = getNodeField(DbgNode, Elt);
93 return DIDescriptor(Field);
94}
95
96GlobalVariable *DIDescriptor::getGlobalVariableField(unsigned Elt) const {
Duncan P. N. Exon Smith5bf8fef2014-12-09 18:38:53 +000097 return dyn_cast_or_null<GlobalVariable>(getConstantField(Elt));
Bill Wendling523bea82013-11-08 08:13:15 +000098}
99
100Constant *DIDescriptor::getConstantField(unsigned Elt) const {
Craig Topperc6207612014-04-09 06:08:46 +0000101 if (!DbgNode)
102 return nullptr;
Bill Wendling523bea82013-11-08 08:13:15 +0000103
104 if (Elt < DbgNode->getNumOperands())
Duncan P. N. Exon Smith5bf8fef2014-12-09 18:38:53 +0000105 if (auto *C =
106 dyn_cast_or_null<ConstantAsMetadata>(DbgNode->getOperand(Elt)))
107 return C->getValue();
Craig Topperc6207612014-04-09 06:08:46 +0000108 return nullptr;
Bill Wendling523bea82013-11-08 08:13:15 +0000109}
110
111Function *DIDescriptor::getFunctionField(unsigned Elt) const {
Duncan P. N. Exon Smith5bf8fef2014-12-09 18:38:53 +0000112 return dyn_cast_or_null<Function>(getConstantField(Elt));
Bill Wendling523bea82013-11-08 08:13:15 +0000113}
114
115void DIDescriptor::replaceFunctionField(unsigned Elt, Function *F) {
Craig Topperc6207612014-04-09 06:08:46 +0000116 if (!DbgNode)
Bill Wendling523bea82013-11-08 08:13:15 +0000117 return;
118
119 if (Elt < DbgNode->getNumOperands()) {
120 MDNode *Node = const_cast<MDNode *>(DbgNode);
Duncan P. N. Exon Smith5bf8fef2014-12-09 18:38:53 +0000121 Node->replaceOperandWith(Elt, F ? ConstantAsMetadata::get(F) : nullptr);
Bill Wendling523bea82013-11-08 08:13:15 +0000122 }
123}
124
Duncan P. N. Exon Smith176b6912014-10-03 20:01:09 +0000125static unsigned DIVariableInlinedAtIndex = 4;
126MDNode *DIVariable::getInlinedAt() const {
127 return getNodeField(DbgNode, DIVariableInlinedAtIndex);
128}
Bill Wendling523bea82013-11-08 08:13:15 +0000129
Duncan P. N. Exon Smith7f637a92014-10-15 17:01:28 +0000130/// \brief Return the size reported by the variable's type.
Adrian Prantlb1416832014-08-01 22:11:58 +0000131unsigned DIVariable::getSizeInBits(const DITypeIdentifierMap &Map) {
132 DIType Ty = getType().resolve(Map);
133 // Follow derived types until we reach a type that
134 // reports back a size.
135 while (Ty.isDerivedType() && !Ty.getSizeInBits()) {
136 DIDerivedType DT(&*Ty);
137 Ty = DT.getTypeDerivedFrom().resolve(Map);
138 }
139 assert(Ty.getSizeInBits() && "type with size 0");
140 return Ty.getSizeInBits();
141}
142
Adrian Prantl87b7eb92014-10-01 18:55:02 +0000143uint64_t DIExpression::getElement(unsigned Idx) const {
144 unsigned I = Idx + 1;
Duncan P. N. Exon Smith176b6912014-10-03 20:01:09 +0000145 assert(I < getNumHeaderFields() &&
146 "non-existing complex address element requested");
147 return getHeaderFieldAs<int64_t>(I);
Adrian Prantl87b7eb92014-10-01 18:55:02 +0000148}
Adrian Prantlb1416832014-08-01 22:11:58 +0000149
Adrian Prantl87b7eb92014-10-01 18:55:02 +0000150bool DIExpression::isVariablePiece() const {
Adrian Prantl34bcbee2015-01-21 00:59:20 +0000151 unsigned N = getNumElements();
152 return N >=3 && getElement(N-3) == dwarf::DW_OP_piece;
Adrian Prantl87b7eb92014-10-01 18:55:02 +0000153}
154
155uint64_t DIExpression::getPieceOffset() const {
Adrian Prantl34bcbee2015-01-21 00:59:20 +0000156 assert(isVariablePiece() && "not a piece");
157 return getElement(getNumElements()-2);
Adrian Prantl87b7eb92014-10-01 18:55:02 +0000158}
159
160uint64_t DIExpression::getPieceSize() const {
Adrian Prantl34bcbee2015-01-21 00:59:20 +0000161 assert(isVariablePiece() && "not a piece");
162 return getElement(getNumElements()-1);
Adrian Prantl87b7eb92014-10-01 18:55:02 +0000163}
Adrian Prantlb1416832014-08-01 22:11:58 +0000164
Bill Wendling523bea82013-11-08 08:13:15 +0000165//===----------------------------------------------------------------------===//
166// Predicates
167//===----------------------------------------------------------------------===//
168
Manman Renf8a19672014-07-28 22:24:06 +0000169bool DIDescriptor::isSubroutineType() const {
Yaron Kerene8270222014-12-19 22:15:09 +0000170 return DbgNode && getTag() == dwarf::DW_TAG_subroutine_type;
Manman Renf8a19672014-07-28 22:24:06 +0000171}
172
Bill Wendling523bea82013-11-08 08:13:15 +0000173bool DIDescriptor::isBasicType() const {
174 if (!DbgNode)
175 return false;
176 switch (getTag()) {
177 case dwarf::DW_TAG_base_type:
178 case dwarf::DW_TAG_unspecified_type:
179 return true;
180 default:
181 return false;
182 }
183}
184
Bill Wendling523bea82013-11-08 08:13:15 +0000185bool DIDescriptor::isDerivedType() const {
186 if (!DbgNode)
187 return false;
188 switch (getTag()) {
189 case dwarf::DW_TAG_typedef:
190 case dwarf::DW_TAG_pointer_type:
191 case dwarf::DW_TAG_ptr_to_member_type:
192 case dwarf::DW_TAG_reference_type:
193 case dwarf::DW_TAG_rvalue_reference_type:
194 case dwarf::DW_TAG_const_type:
195 case dwarf::DW_TAG_volatile_type:
196 case dwarf::DW_TAG_restrict_type:
197 case dwarf::DW_TAG_member:
198 case dwarf::DW_TAG_inheritance:
199 case dwarf::DW_TAG_friend:
200 return true;
201 default:
202 // CompositeTypes are currently modelled as DerivedTypes.
203 return isCompositeType();
204 }
205}
206
Bill Wendling523bea82013-11-08 08:13:15 +0000207bool DIDescriptor::isCompositeType() const {
208 if (!DbgNode)
209 return false;
210 switch (getTag()) {
211 case dwarf::DW_TAG_array_type:
212 case dwarf::DW_TAG_structure_type:
213 case dwarf::DW_TAG_union_type:
214 case dwarf::DW_TAG_enumeration_type:
215 case dwarf::DW_TAG_subroutine_type:
216 case dwarf::DW_TAG_class_type:
217 return true;
218 default:
219 return false;
220 }
221}
222
Bill Wendling523bea82013-11-08 08:13:15 +0000223bool DIDescriptor::isVariable() const {
224 if (!DbgNode)
225 return false;
226 switch (getTag()) {
227 case dwarf::DW_TAG_auto_variable:
228 case dwarf::DW_TAG_arg_variable:
229 return true;
230 default:
231 return false;
232 }
233}
234
Bill Wendling523bea82013-11-08 08:13:15 +0000235bool DIDescriptor::isType() const {
Manman Renf93ac4b2014-07-29 18:20:39 +0000236 return isBasicType() || isCompositeType() || isDerivedType();
Bill Wendling523bea82013-11-08 08:13:15 +0000237}
238
Bill Wendling523bea82013-11-08 08:13:15 +0000239bool DIDescriptor::isSubprogram() const {
240 return DbgNode && getTag() == dwarf::DW_TAG_subprogram;
241}
242
Bill Wendling523bea82013-11-08 08:13:15 +0000243bool DIDescriptor::isGlobalVariable() const {
244 return DbgNode && (getTag() == dwarf::DW_TAG_variable ||
245 getTag() == dwarf::DW_TAG_constant);
246}
247
Bill Wendling523bea82013-11-08 08:13:15 +0000248bool DIDescriptor::isScope() const {
249 if (!DbgNode)
250 return false;
251 switch (getTag()) {
252 case dwarf::DW_TAG_compile_unit:
253 case dwarf::DW_TAG_lexical_block:
254 case dwarf::DW_TAG_subprogram:
255 case dwarf::DW_TAG_namespace:
256 case dwarf::DW_TAG_file_type:
257 return true;
258 default:
259 break;
260 }
261 return isType();
262}
263
Bill Wendling523bea82013-11-08 08:13:15 +0000264bool DIDescriptor::isTemplateTypeParameter() const {
265 return DbgNode && getTag() == dwarf::DW_TAG_template_type_parameter;
266}
267
Bill Wendling523bea82013-11-08 08:13:15 +0000268bool DIDescriptor::isTemplateValueParameter() const {
269 return DbgNode && (getTag() == dwarf::DW_TAG_template_value_parameter ||
270 getTag() == dwarf::DW_TAG_GNU_template_template_param ||
271 getTag() == dwarf::DW_TAG_GNU_template_parameter_pack);
272}
273
Bill Wendling523bea82013-11-08 08:13:15 +0000274bool DIDescriptor::isCompileUnit() const {
275 return DbgNode && getTag() == dwarf::DW_TAG_compile_unit;
276}
277
Bill Wendling523bea82013-11-08 08:13:15 +0000278bool DIDescriptor::isFile() const {
279 return DbgNode && getTag() == dwarf::DW_TAG_file_type;
280}
281
Bill Wendling523bea82013-11-08 08:13:15 +0000282bool DIDescriptor::isNameSpace() const {
283 return DbgNode && getTag() == dwarf::DW_TAG_namespace;
284}
285
Bill Wendling523bea82013-11-08 08:13:15 +0000286bool DIDescriptor::isLexicalBlockFile() const {
287 return DbgNode && getTag() == dwarf::DW_TAG_lexical_block &&
Duncan P. N. Exon Smith176b6912014-10-03 20:01:09 +0000288 DbgNode->getNumOperands() == 3 && getNumHeaderFields() == 2;
Bill Wendling523bea82013-11-08 08:13:15 +0000289}
290
Bill Wendling523bea82013-11-08 08:13:15 +0000291bool DIDescriptor::isLexicalBlock() const {
Duncan P. N. Exon Smith176b6912014-10-03 20:01:09 +0000292 // FIXME: There are always exactly 4 header fields in DILexicalBlock, but
293 // something relies on this returning true for DILexicalBlockFile.
Bill Wendling523bea82013-11-08 08:13:15 +0000294 return DbgNode && getTag() == dwarf::DW_TAG_lexical_block &&
Duncan P. N. Exon Smith176b6912014-10-03 20:01:09 +0000295 DbgNode->getNumOperands() == 3 &&
296 (getNumHeaderFields() == 2 || getNumHeaderFields() == 4);
Bill Wendling523bea82013-11-08 08:13:15 +0000297}
298
Bill Wendling523bea82013-11-08 08:13:15 +0000299bool DIDescriptor::isSubrange() const {
300 return DbgNode && getTag() == dwarf::DW_TAG_subrange_type;
301}
302
Bill Wendling523bea82013-11-08 08:13:15 +0000303bool DIDescriptor::isEnumerator() const {
304 return DbgNode && getTag() == dwarf::DW_TAG_enumerator;
305}
306
Bill Wendling523bea82013-11-08 08:13:15 +0000307bool DIDescriptor::isObjCProperty() const {
308 return DbgNode && getTag() == dwarf::DW_TAG_APPLE_property;
309}
310
Bill Wendling523bea82013-11-08 08:13:15 +0000311bool DIDescriptor::isImportedEntity() const {
312 return DbgNode && (getTag() == dwarf::DW_TAG_imported_module ||
313 getTag() == dwarf::DW_TAG_imported_declaration);
314}
315
Adrian Prantl87b7eb92014-10-01 18:55:02 +0000316bool DIDescriptor::isExpression() const {
317 return DbgNode && (getTag() == dwarf::DW_TAG_expression);
318}
319
Bill Wendling523bea82013-11-08 08:13:15 +0000320//===----------------------------------------------------------------------===//
321// Simple Descriptor Constructors and other Methods
322//===----------------------------------------------------------------------===//
323
Frederic Riss36acf0f2014-09-15 07:50:36 +0000324void DIDescriptor::replaceAllUsesWith(LLVMContext &VMContext, DIDescriptor D) {
Bill Wendling523bea82013-11-08 08:13:15 +0000325
326 assert(DbgNode && "Trying to replace an unverified type!");
327
328 // Since we use a TrackingVH for the node, its easy for clients to manufacture
329 // legitimate situations where they want to replaceAllUsesWith() on something
330 // which, due to uniquing, has merged with the source. We shield clients from
331 // this detail by allowing a value to be replaced with replaceAllUsesWith()
332 // itself.
David Blaikied3f094a2014-05-06 03:41:57 +0000333 const MDNode *DN = D;
334 if (DbgNode == DN) {
Duncan P. N. Exon Smith5bf8fef2014-12-09 18:38:53 +0000335 SmallVector<Metadata *, 10> Ops(DbgNode->getNumOperands());
David Blaikied3f094a2014-05-06 03:41:57 +0000336 for (size_t i = 0; i != Ops.size(); ++i)
337 Ops[i] = DbgNode->getOperand(i);
338 DN = MDNode::get(VMContext, Ops);
Bill Wendling523bea82013-11-08 08:13:15 +0000339 }
David Blaikied3f094a2014-05-06 03:41:57 +0000340
Duncan P. N. Exon Smith946fdcc2015-01-19 20:36:39 +0000341 assert(DbgNode->isTemporary() && "Expected temporary node");
342 auto *Node = const_cast<MDNode *>(DbgNode);
Duncan P. N. Exon Smith5bf8fef2014-12-09 18:38:53 +0000343 Node->replaceAllUsesWith(const_cast<MDNode *>(DN));
David Blaikied3f094a2014-05-06 03:41:57 +0000344 MDNode::deleteTemporary(Node);
Frederic Rissdd7aec52014-09-15 07:50:42 +0000345 DbgNode = DN;
Bill Wendling523bea82013-11-08 08:13:15 +0000346}
347
Frederic Riss36acf0f2014-09-15 07:50:36 +0000348void DIDescriptor::replaceAllUsesWith(MDNode *D) {
Bill Wendling523bea82013-11-08 08:13:15 +0000349 assert(DbgNode && "Trying to replace an unverified type!");
David Blaikied3f094a2014-05-06 03:41:57 +0000350 assert(DbgNode != D && "This replacement should always happen");
Duncan P. N. Exon Smith946fdcc2015-01-19 20:36:39 +0000351 assert(DbgNode->isTemporary() && "Expected temporary node");
352 auto *Node = const_cast<MDNode *>(DbgNode);
Duncan P. N. Exon Smith5bf8fef2014-12-09 18:38:53 +0000353 Node->replaceAllUsesWith(D);
David Blaikied3f094a2014-05-06 03:41:57 +0000354 MDNode::deleteTemporary(Node);
Bill Wendling523bea82013-11-08 08:13:15 +0000355}
356
Bill Wendling523bea82013-11-08 08:13:15 +0000357bool DICompileUnit::Verify() const {
358 if (!isCompileUnit())
359 return false;
360
361 // Don't bother verifying the compilation directory or producer string
362 // as those could be empty.
363 if (getFilename().empty())
364 return false;
365
Duncan P. N. Exon Smith176b6912014-10-03 20:01:09 +0000366 return DbgNode->getNumOperands() == 7 && getNumHeaderFields() == 8;
Bill Wendling523bea82013-11-08 08:13:15 +0000367}
368
Bill Wendling523bea82013-11-08 08:13:15 +0000369bool DIObjCProperty::Verify() const {
370 if (!isObjCProperty())
371 return false;
372
373 // Don't worry about the rest of the strings for now.
Duncan P. N. Exon Smith176b6912014-10-03 20:01:09 +0000374 return DbgNode->getNumOperands() == 3 && getNumHeaderFields() == 6;
Bill Wendling523bea82013-11-08 08:13:15 +0000375}
376
Duncan P. N. Exon Smith7f637a92014-10-15 17:01:28 +0000377/// \brief Check if a field at position Elt of a MDNode is a MDNode.
378///
Bill Wendling523bea82013-11-08 08:13:15 +0000379/// We currently allow an empty string and an integer.
380/// But we don't allow a non-empty string in a MDNode field.
381static bool fieldIsMDNode(const MDNode *DbgNode, unsigned Elt) {
382 // FIXME: This function should return true, if the field is null or the field
383 // is indeed a MDNode: return !Fld || isa<MDNode>(Fld).
Duncan P. N. Exon Smith5bf8fef2014-12-09 18:38:53 +0000384 Metadata *Fld = getField(DbgNode, Elt);
Bill Wendling523bea82013-11-08 08:13:15 +0000385 if (Fld && isa<MDString>(Fld) && !cast<MDString>(Fld)->getString().empty())
386 return false;
387 return true;
388}
389
Duncan P. N. Exon Smith7f637a92014-10-15 17:01:28 +0000390/// \brief Check if a field at position Elt of a MDNode is a MDString.
Bill Wendling523bea82013-11-08 08:13:15 +0000391static bool fieldIsMDString(const MDNode *DbgNode, unsigned Elt) {
Duncan P. N. Exon Smith5bf8fef2014-12-09 18:38:53 +0000392 Metadata *Fld = getField(DbgNode, Elt);
Bill Wendling523bea82013-11-08 08:13:15 +0000393 return !Fld || isa<MDString>(Fld);
394}
395
Duncan P. N. Exon Smith7f637a92014-10-15 17:01:28 +0000396/// \brief Check if a value can be a reference to a type.
Duncan P. N. Exon Smithc81307a2014-11-14 23:55:03 +0000397static bool isTypeRef(const Metadata *MD) {
398 if (!MD)
399 return true;
400 if (auto *S = dyn_cast<MDString>(MD))
401 return !S->getString().empty();
402 if (auto *N = dyn_cast<MDNode>(MD))
403 return DIType(N).isType();
404 return false;
Bill Wendling523bea82013-11-08 08:13:15 +0000405}
406
Duncan P. N. Exon Smith7f637a92014-10-15 17:01:28 +0000407/// \brief Check if referenced field might be a type.
Bill Wendling523bea82013-11-08 08:13:15 +0000408static bool fieldIsTypeRef(const MDNode *DbgNode, unsigned Elt) {
Duncan P. N. Exon Smithc81307a2014-11-14 23:55:03 +0000409 return isTypeRef(dyn_cast_or_null<Metadata>(getField(DbgNode, Elt)));
Bill Wendling523bea82013-11-08 08:13:15 +0000410}
411
Duncan P. N. Exon Smith7f637a92014-10-15 17:01:28 +0000412/// \brief Check if a value can be a ScopeRef.
Duncan P. N. Exon Smithc81307a2014-11-14 23:55:03 +0000413static bool isScopeRef(const Metadata *MD) {
414 if (!MD)
415 return true;
416 if (auto *S = dyn_cast<MDString>(MD))
417 return !S->getString().empty();
418 return isa<MDNode>(MD);
Bill Wendling523bea82013-11-08 08:13:15 +0000419}
420
Duncan P. N. Exon Smith7f637a92014-10-15 17:01:28 +0000421/// \brief Check if a field at position Elt of a MDNode can be a ScopeRef.
Bill Wendling523bea82013-11-08 08:13:15 +0000422static bool fieldIsScopeRef(const MDNode *DbgNode, unsigned Elt) {
Duncan P. N. Exon Smithc81307a2014-11-14 23:55:03 +0000423 return isScopeRef(dyn_cast_or_null<Metadata>(getField(DbgNode, Elt)));
Bill Wendling523bea82013-11-08 08:13:15 +0000424}
425
Bill Wendling523bea82013-11-08 08:13:15 +0000426bool DIType::Verify() const {
427 if (!isType())
428 return false;
429 // Make sure Context @ field 2 is MDNode.
430 if (!fieldIsScopeRef(DbgNode, 2))
431 return false;
432
433 // FIXME: Sink this into the various subclass verifies.
434 uint16_t Tag = getTag();
Manman Renf93ac4b2014-07-29 18:20:39 +0000435 if (!isBasicType() && Tag != dwarf::DW_TAG_const_type &&
Bill Wendling523bea82013-11-08 08:13:15 +0000436 Tag != dwarf::DW_TAG_volatile_type && Tag != dwarf::DW_TAG_pointer_type &&
437 Tag != dwarf::DW_TAG_ptr_to_member_type &&
438 Tag != dwarf::DW_TAG_reference_type &&
439 Tag != dwarf::DW_TAG_rvalue_reference_type &&
440 Tag != dwarf::DW_TAG_restrict_type && Tag != dwarf::DW_TAG_array_type &&
441 Tag != dwarf::DW_TAG_enumeration_type &&
442 Tag != dwarf::DW_TAG_subroutine_type &&
443 Tag != dwarf::DW_TAG_inheritance && Tag != dwarf::DW_TAG_friend &&
444 getFilename().empty())
445 return false;
Adrian Prantldaedfda2014-08-29 22:44:07 +0000446
Bill Wendling523bea82013-11-08 08:13:15 +0000447 // DIType is abstract, it should be a BasicType, a DerivedType or
448 // a CompositeType.
449 if (isBasicType())
Renato Golin47f46fd2013-11-26 16:47:00 +0000450 return DIBasicType(DbgNode).Verify();
Bill Wendling523bea82013-11-08 08:13:15 +0000451 else if (isCompositeType())
Renato Golin47f46fd2013-11-26 16:47:00 +0000452 return DICompositeType(DbgNode).Verify();
Bill Wendling523bea82013-11-08 08:13:15 +0000453 else if (isDerivedType())
Renato Golin47f46fd2013-11-26 16:47:00 +0000454 return DIDerivedType(DbgNode).Verify();
Bill Wendling523bea82013-11-08 08:13:15 +0000455 else
456 return false;
Bill Wendling523bea82013-11-08 08:13:15 +0000457}
458
Bill Wendling523bea82013-11-08 08:13:15 +0000459bool DIBasicType::Verify() const {
Duncan P. N. Exon Smith176b6912014-10-03 20:01:09 +0000460 return isBasicType() && DbgNode->getNumOperands() == 3 &&
461 getNumHeaderFields() == 8;
Bill Wendling523bea82013-11-08 08:13:15 +0000462}
463
Bill Wendling523bea82013-11-08 08:13:15 +0000464bool DIDerivedType::Verify() const {
Duncan P. N. Exon Smith176b6912014-10-03 20:01:09 +0000465 // Make sure DerivedFrom @ field 3 is TypeRef.
466 if (!fieldIsTypeRef(DbgNode, 3))
Bill Wendling523bea82013-11-08 08:13:15 +0000467 return false;
468 if (getTag() == dwarf::DW_TAG_ptr_to_member_type)
Duncan P. N. Exon Smith176b6912014-10-03 20:01:09 +0000469 // Make sure ClassType @ field 4 is a TypeRef.
470 if (!fieldIsTypeRef(DbgNode, 4))
Bill Wendling523bea82013-11-08 08:13:15 +0000471 return false;
472
Duncan P. N. Exon Smith176b6912014-10-03 20:01:09 +0000473 return isDerivedType() && DbgNode->getNumOperands() >= 4 &&
474 DbgNode->getNumOperands() <= 8 && getNumHeaderFields() >= 7 &&
475 getNumHeaderFields() <= 8;
Bill Wendling523bea82013-11-08 08:13:15 +0000476}
477
Bill Wendling523bea82013-11-08 08:13:15 +0000478bool DICompositeType::Verify() const {
479 if (!isCompositeType())
480 return false;
481
Duncan P. N. Exon Smith176b6912014-10-03 20:01:09 +0000482 // Make sure DerivedFrom @ field 3 and ContainingType @ field 5 are TypeRef.
483 if (!fieldIsTypeRef(DbgNode, 3))
Bill Wendling523bea82013-11-08 08:13:15 +0000484 return false;
Duncan P. N. Exon Smith176b6912014-10-03 20:01:09 +0000485 if (!fieldIsTypeRef(DbgNode, 5))
Bill Wendling523bea82013-11-08 08:13:15 +0000486 return false;
487
Duncan P. N. Exon Smith176b6912014-10-03 20:01:09 +0000488 // Make sure the type identifier at field 7 is MDString, it can be null.
489 if (!fieldIsMDString(DbgNode, 7))
Bill Wendling523bea82013-11-08 08:13:15 +0000490 return false;
491
Adrian Prantl99c7af22013-12-18 21:48:19 +0000492 // A subroutine type can't be both & and &&.
493 if (isLValueReference() && isRValueReference())
494 return false;
495
Duncan P. N. Exon Smith176b6912014-10-03 20:01:09 +0000496 return DbgNode->getNumOperands() == 8 && getNumHeaderFields() == 8;
Bill Wendling523bea82013-11-08 08:13:15 +0000497}
498
Bill Wendling523bea82013-11-08 08:13:15 +0000499bool DISubprogram::Verify() const {
500 if (!isSubprogram())
501 return false;
502
Duncan P. N. Exon Smith176b6912014-10-03 20:01:09 +0000503 // Make sure context @ field 2 is a ScopeRef and type @ field 3 is a MDNode.
Bill Wendling523bea82013-11-08 08:13:15 +0000504 if (!fieldIsScopeRef(DbgNode, 2))
505 return false;
Duncan P. N. Exon Smith176b6912014-10-03 20:01:09 +0000506 if (!fieldIsMDNode(DbgNode, 3))
Bill Wendling523bea82013-11-08 08:13:15 +0000507 return false;
Duncan P. N. Exon Smith176b6912014-10-03 20:01:09 +0000508 // Containing type @ field 4.
509 if (!fieldIsTypeRef(DbgNode, 4))
Bill Wendling523bea82013-11-08 08:13:15 +0000510 return false;
Adrian Prantl99c7af22013-12-18 21:48:19 +0000511
512 // A subprogram can't be both & and &&.
513 if (isLValueReference() && isRValueReference())
514 return false;
515
David Blaikie3dfe4782014-10-14 18:22:52 +0000516 // If a DISubprogram has an llvm::Function*, then scope chains from all
517 // instructions within the function should lead to this DISubprogram.
518 if (auto *F = getFunction()) {
David Blaikie3dfe4782014-10-14 18:22:52 +0000519 for (auto &BB : *F) {
520 for (auto &I : BB) {
521 DebugLoc DL = I.getDebugLoc();
522 if (DL.isUnknown())
523 continue;
524
525 MDNode *Scope = nullptr;
526 MDNode *IA = nullptr;
527 // walk the inlined-at scopes
Duncan P. N. Exon Smith5bf8fef2014-12-09 18:38:53 +0000528 while ((IA = DL.getInlinedAt()))
David Blaikie3dfe4782014-10-14 18:22:52 +0000529 DL = DebugLoc::getFromDILocation(IA);
Duncan P. N. Exon Smith5bf8fef2014-12-09 18:38:53 +0000530 DL.getScopeAndInlinedAt(Scope, IA);
Adrian Prantlde200df2015-01-20 22:37:25 +0000531 if (!Scope)
532 return false;
David Blaikie3dfe4782014-10-14 18:22:52 +0000533 assert(!IA);
534 while (!DIDescriptor(Scope).isSubprogram()) {
535 DILexicalBlockFile D(Scope);
536 Scope = D.isLexicalBlockFile()
537 ? D.getScope()
Duncan P. N. Exon Smith5bf8fef2014-12-09 18:38:53 +0000538 : DebugLoc::getFromDILexicalBlock(Scope).getScope();
Adrian Prantl1292e242015-01-21 18:32:56 +0000539 if (!Scope)
540 return false;
David Blaikie3dfe4782014-10-14 18:22:52 +0000541 }
542 if (!DISubprogram(Scope).describes(F))
543 return false;
544 }
545 }
546 }
Duncan P. N. Exon Smith176b6912014-10-03 20:01:09 +0000547 return DbgNode->getNumOperands() == 9 && getNumHeaderFields() == 12;
Bill Wendling523bea82013-11-08 08:13:15 +0000548}
549
Bill Wendling523bea82013-11-08 08:13:15 +0000550bool DIGlobalVariable::Verify() const {
551 if (!isGlobalVariable())
552 return false;
553
554 if (getDisplayName().empty())
555 return false;
Manman Renf0a582b2014-11-21 19:55:23 +0000556 // Make sure context @ field 1 is an MDNode.
557 if (!fieldIsMDNode(DbgNode, 1))
Bill Wendling523bea82013-11-08 08:13:15 +0000558 return false;
Duncan P. N. Exon Smith176b6912014-10-03 20:01:09 +0000559 // Make sure that type @ field 3 is a DITypeRef.
560 if (!fieldIsTypeRef(DbgNode, 3))
Bill Wendling523bea82013-11-08 08:13:15 +0000561 return false;
Duncan P. N. Exon Smith176b6912014-10-03 20:01:09 +0000562 // Make sure StaticDataMemberDeclaration @ field 5 is MDNode.
563 if (!fieldIsMDNode(DbgNode, 5))
Bill Wendling523bea82013-11-08 08:13:15 +0000564 return false;
565
Duncan P. N. Exon Smith176b6912014-10-03 20:01:09 +0000566 return DbgNode->getNumOperands() == 6 && getNumHeaderFields() == 7;
Bill Wendling523bea82013-11-08 08:13:15 +0000567}
568
Bill Wendling523bea82013-11-08 08:13:15 +0000569bool DIVariable::Verify() const {
570 if (!isVariable())
571 return false;
572
Adrian Prantl1a1647c2014-03-18 02:34:58 +0000573 // Make sure context @ field 1 is an MDNode.
Bill Wendling523bea82013-11-08 08:13:15 +0000574 if (!fieldIsMDNode(DbgNode, 1))
575 return false;
Duncan P. N. Exon Smith176b6912014-10-03 20:01:09 +0000576 // Make sure that type @ field 3 is a DITypeRef.
577 if (!fieldIsTypeRef(DbgNode, 3))
578 return false;
579
580 // Check the number of header fields, which is common between complex and
581 // simple variables.
582 if (getNumHeaderFields() != 4)
Bill Wendling523bea82013-11-08 08:13:15 +0000583 return false;
Adrian Prantlda7d92e2014-06-30 17:17:35 +0000584
Adrian Prantl87b7eb92014-10-01 18:55:02 +0000585 // Variable without an inline location.
Duncan P. N. Exon Smith176b6912014-10-03 20:01:09 +0000586 if (DbgNode->getNumOperands() == 4)
Adrian Prantlda7d92e2014-06-30 17:17:35 +0000587 return true;
588
Duncan P. N. Exon Smith176b6912014-10-03 20:01:09 +0000589 // Variable with an inline location.
590 return getInlinedAt() != nullptr && DbgNode->getNumOperands() == 5;
Adrian Prantl87b7eb92014-10-01 18:55:02 +0000591}
592
Adrian Prantl87b7eb92014-10-01 18:55:02 +0000593bool DIExpression::Verify() const {
594 // Empty DIExpressions may be represented as a nullptr.
595 if (!DbgNode)
596 return true;
597
Adrian Prantl34bcbee2015-01-21 00:59:20 +0000598 unsigned N = getNumElements();
599 for (unsigned I = 0; I < N; ++I)
600 switch (getElement(I)) {
601 case DW_OP_piece:
602 // DW_OP_piece has to be the last element in the expression and take two
603 // arguments.
604 if (getElement(I) == DW_OP_piece && !isVariablePiece())
605 return false;
606 I += 2;
607 break;
608 case DW_OP_plus:
609 // Takes one argument.
610 if (I+1 == N)
611 return false;
612 I += 1;
613 break;
614 default: break;
615 }
Duncan P. N. Exon Smith176b6912014-10-03 20:01:09 +0000616 return isExpression() && DbgNode->getNumOperands() == 1;
Bill Wendling523bea82013-11-08 08:13:15 +0000617}
618
Bill Wendling523bea82013-11-08 08:13:15 +0000619bool DILocation::Verify() const {
Duncan P. N. Exon Smith98854692015-01-14 22:27:36 +0000620 return DbgNode && isa<MDLocation>(DbgNode);
Bill Wendling523bea82013-11-08 08:13:15 +0000621}
622
Bill Wendling523bea82013-11-08 08:13:15 +0000623bool DINameSpace::Verify() const {
624 if (!isNameSpace())
625 return false;
Duncan P. N. Exon Smith176b6912014-10-03 20:01:09 +0000626 return DbgNode->getNumOperands() == 3 && getNumHeaderFields() == 3;
Bill Wendling523bea82013-11-08 08:13:15 +0000627}
628
Bill Wendling523bea82013-11-08 08:13:15 +0000629MDNode *DIFile::getFileNode() const { return getNodeField(DbgNode, 1); }
630
Bill Wendling523bea82013-11-08 08:13:15 +0000631bool DIFile::Verify() const {
632 return isFile() && DbgNode->getNumOperands() == 2;
633}
634
Bill Wendling523bea82013-11-08 08:13:15 +0000635bool DIEnumerator::Verify() const {
Duncan P. N. Exon Smith176b6912014-10-03 20:01:09 +0000636 return isEnumerator() && DbgNode->getNumOperands() == 1 &&
637 getNumHeaderFields() == 3;
Bill Wendling523bea82013-11-08 08:13:15 +0000638}
639
Bill Wendling523bea82013-11-08 08:13:15 +0000640bool DISubrange::Verify() const {
Duncan P. N. Exon Smith176b6912014-10-03 20:01:09 +0000641 return isSubrange() && DbgNode->getNumOperands() == 1 &&
642 getNumHeaderFields() == 3;
Bill Wendling523bea82013-11-08 08:13:15 +0000643}
644
Bill Wendling523bea82013-11-08 08:13:15 +0000645bool DILexicalBlock::Verify() const {
Duncan P. N. Exon Smith176b6912014-10-03 20:01:09 +0000646 return isLexicalBlock() && DbgNode->getNumOperands() == 3 &&
647 getNumHeaderFields() == 4;
Bill Wendling523bea82013-11-08 08:13:15 +0000648}
649
Bill Wendling523bea82013-11-08 08:13:15 +0000650bool DILexicalBlockFile::Verify() const {
Duncan P. N. Exon Smith176b6912014-10-03 20:01:09 +0000651 return isLexicalBlockFile() && DbgNode->getNumOperands() == 3 &&
652 getNumHeaderFields() == 2;
Bill Wendling523bea82013-11-08 08:13:15 +0000653}
654
Bill Wendling523bea82013-11-08 08:13:15 +0000655bool DITemplateTypeParameter::Verify() const {
Duncan P. N. Exon Smith176b6912014-10-03 20:01:09 +0000656 return isTemplateTypeParameter() && DbgNode->getNumOperands() == 4 &&
657 getNumHeaderFields() == 4;
Bill Wendling523bea82013-11-08 08:13:15 +0000658}
659
Bill Wendling523bea82013-11-08 08:13:15 +0000660bool DITemplateValueParameter::Verify() const {
Duncan P. N. Exon Smith176b6912014-10-03 20:01:09 +0000661 return isTemplateValueParameter() && DbgNode->getNumOperands() == 5 &&
662 getNumHeaderFields() == 4;
Bill Wendling523bea82013-11-08 08:13:15 +0000663}
664
Bill Wendling523bea82013-11-08 08:13:15 +0000665bool DIImportedEntity::Verify() const {
Duncan P. N. Exon Smith176b6912014-10-03 20:01:09 +0000666 return isImportedEntity() && DbgNode->getNumOperands() == 3 &&
667 getNumHeaderFields() == 3;
Bill Wendling523bea82013-11-08 08:13:15 +0000668}
669
Bill Wendling523bea82013-11-08 08:13:15 +0000670MDNode *DIDerivedType::getObjCProperty() const {
Duncan P. N. Exon Smith176b6912014-10-03 20:01:09 +0000671 return getNodeField(DbgNode, 4);
Bill Wendling523bea82013-11-08 08:13:15 +0000672}
673
674MDString *DICompositeType::getIdentifier() const {
Duncan P. N. Exon Smith176b6912014-10-03 20:01:09 +0000675 return cast_or_null<MDString>(getField(DbgNode, 7));
Bill Wendling523bea82013-11-08 08:13:15 +0000676}
677
678#ifndef NDEBUG
679static void VerifySubsetOf(const MDNode *LHS, const MDNode *RHS) {
680 for (unsigned i = 0; i != LHS->getNumOperands(); ++i) {
681 // Skip the 'empty' list (that's a single i32 0, rather than truly empty).
Duncan P. N. Exon Smith5bf8fef2014-12-09 18:38:53 +0000682 if (i == 0 && mdconst::hasa<ConstantInt>(LHS->getOperand(i)))
Bill Wendling523bea82013-11-08 08:13:15 +0000683 continue;
684 const MDNode *E = cast<MDNode>(LHS->getOperand(i));
685 bool found = false;
686 for (unsigned j = 0; !found && j != RHS->getNumOperands(); ++j)
Hans Wennborge242e8b2014-12-09 20:39:15 +0000687 found = (E == cast<MDNode>(RHS->getOperand(j)));
Bill Wendling523bea82013-11-08 08:13:15 +0000688 assert(found && "Losing a member during member list replacement");
689 }
690}
691#endif
692
Manman Ren1a125c92014-07-28 19:33:20 +0000693void DICompositeType::setArraysHelper(MDNode *Elements, MDNode *TParams) {
Duncan P. N. Exon Smith5bf8fef2014-12-09 18:38:53 +0000694 TrackingMDNodeRef N(*this);
Bill Wendling523bea82013-11-08 08:13:15 +0000695 if (Elements) {
696#ifndef NDEBUG
697 // Check that the new list of members contains all the old members as well.
Duncan P. N. Exon Smith176b6912014-10-03 20:01:09 +0000698 if (const MDNode *El = cast_or_null<MDNode>(N->getOperand(4)))
Bill Wendling523bea82013-11-08 08:13:15 +0000699 VerifySubsetOf(El, Elements);
700#endif
Duncan P. N. Exon Smith176b6912014-10-03 20:01:09 +0000701 N->replaceOperandWith(4, Elements);
Bill Wendling523bea82013-11-08 08:13:15 +0000702 }
703 if (TParams)
Duncan P. N. Exon Smith176b6912014-10-03 20:01:09 +0000704 N->replaceOperandWith(6, TParams);
Bill Wendling523bea82013-11-08 08:13:15 +0000705 DbgNode = N;
706}
707
Bill Wendling523bea82013-11-08 08:13:15 +0000708DIScopeRef DIScope::getRef() const {
709 if (!isCompositeType())
710 return DIScopeRef(*this);
711 DICompositeType DTy(DbgNode);
712 if (!DTy.getIdentifier())
713 return DIScopeRef(*this);
714 return DIScopeRef(DTy.getIdentifier());
715}
716
Bill Wendling523bea82013-11-08 08:13:15 +0000717void DICompositeType::setContainingType(DICompositeType ContainingType) {
Duncan P. N. Exon Smith5bf8fef2014-12-09 18:38:53 +0000718 TrackingMDNodeRef N(*this);
Duncan P. N. Exon Smith176b6912014-10-03 20:01:09 +0000719 N->replaceOperandWith(5, ContainingType.getRef());
Bill Wendling523bea82013-11-08 08:13:15 +0000720 DbgNode = N;
721}
722
Bill Wendling523bea82013-11-08 08:13:15 +0000723bool DIVariable::isInlinedFnArgument(const Function *CurFn) {
724 assert(CurFn && "Invalid function");
725 if (!getContext().isSubprogram())
726 return false;
727 // This variable is not inlined function argument if its scope
728 // does not describe current function.
729 return !DISubprogram(getContext()).describes(CurFn);
730}
731
Bill Wendling523bea82013-11-08 08:13:15 +0000732bool DISubprogram::describes(const Function *F) {
733 assert(F && "Invalid function");
734 if (F == getFunction())
735 return true;
736 StringRef Name = getLinkageName();
737 if (Name.empty())
738 Name = getName();
739 if (F->getName() == Name)
740 return true;
741 return false;
742}
743
Bill Wendling523bea82013-11-08 08:13:15 +0000744MDNode *DISubprogram::getVariablesNodes() const {
Duncan P. N. Exon Smith176b6912014-10-03 20:01:09 +0000745 return getNodeField(DbgNode, 8);
Bill Wendling523bea82013-11-08 08:13:15 +0000746}
747
748DIArray DISubprogram::getVariables() const {
Duncan P. N. Exon Smith176b6912014-10-03 20:01:09 +0000749 return DIArray(getNodeField(DbgNode, 8));
Bill Wendling523bea82013-11-08 08:13:15 +0000750}
751
Duncan P. N. Exon Smith5bf8fef2014-12-09 18:38:53 +0000752Metadata *DITemplateValueParameter::getValue() const {
753 return DbgNode->getOperand(3);
Bill Wendling523bea82013-11-08 08:13:15 +0000754}
755
Bill Wendling523bea82013-11-08 08:13:15 +0000756DIScopeRef DIScope::getContext() const {
757
758 if (isType())
759 return DIType(DbgNode).getContext();
760
761 if (isSubprogram())
762 return DIScopeRef(DISubprogram(DbgNode).getContext());
763
764 if (isLexicalBlock())
765 return DIScopeRef(DILexicalBlock(DbgNode).getContext());
766
767 if (isLexicalBlockFile())
768 return DIScopeRef(DILexicalBlockFile(DbgNode).getContext());
769
770 if (isNameSpace())
771 return DIScopeRef(DINameSpace(DbgNode).getContext());
772
773 assert((isFile() || isCompileUnit()) && "Unhandled type of scope.");
Craig Topperc6207612014-04-09 06:08:46 +0000774 return DIScopeRef(nullptr);
Bill Wendling523bea82013-11-08 08:13:15 +0000775}
776
Bill Wendling523bea82013-11-08 08:13:15 +0000777StringRef DIScope::getName() const {
778 if (isType())
779 return DIType(DbgNode).getName();
780 if (isSubprogram())
781 return DISubprogram(DbgNode).getName();
782 if (isNameSpace())
783 return DINameSpace(DbgNode).getName();
784 assert((isLexicalBlock() || isLexicalBlockFile() || isFile() ||
785 isCompileUnit()) &&
786 "Unhandled type of scope.");
787 return StringRef();
788}
789
790StringRef DIScope::getFilename() const {
791 if (!DbgNode)
792 return StringRef();
793 return ::getStringField(getNodeField(DbgNode, 1), 0);
794}
795
796StringRef DIScope::getDirectory() const {
797 if (!DbgNode)
798 return StringRef();
799 return ::getStringField(getNodeField(DbgNode, 1), 1);
800}
801
802DIArray DICompileUnit::getEnumTypes() const {
Duncan P. N. Exon Smith176b6912014-10-03 20:01:09 +0000803 if (!DbgNode || DbgNode->getNumOperands() < 7)
Bill Wendling523bea82013-11-08 08:13:15 +0000804 return DIArray();
805
Duncan P. N. Exon Smith176b6912014-10-03 20:01:09 +0000806 return DIArray(getNodeField(DbgNode, 2));
Bill Wendling523bea82013-11-08 08:13:15 +0000807}
808
809DIArray DICompileUnit::getRetainedTypes() const {
Duncan P. N. Exon Smith176b6912014-10-03 20:01:09 +0000810 if (!DbgNode || DbgNode->getNumOperands() < 7)
Bill Wendling523bea82013-11-08 08:13:15 +0000811 return DIArray();
812
Duncan P. N. Exon Smith176b6912014-10-03 20:01:09 +0000813 return DIArray(getNodeField(DbgNode, 3));
Bill Wendling523bea82013-11-08 08:13:15 +0000814}
815
816DIArray DICompileUnit::getSubprograms() const {
Duncan P. N. Exon Smith176b6912014-10-03 20:01:09 +0000817 if (!DbgNode || DbgNode->getNumOperands() < 7)
Bill Wendling523bea82013-11-08 08:13:15 +0000818 return DIArray();
819
Duncan P. N. Exon Smith176b6912014-10-03 20:01:09 +0000820 return DIArray(getNodeField(DbgNode, 4));
Bill Wendling523bea82013-11-08 08:13:15 +0000821}
822
823DIArray DICompileUnit::getGlobalVariables() const {
Duncan P. N. Exon Smith176b6912014-10-03 20:01:09 +0000824 if (!DbgNode || DbgNode->getNumOperands() < 7)
Bill Wendling523bea82013-11-08 08:13:15 +0000825 return DIArray();
826
Duncan P. N. Exon Smith176b6912014-10-03 20:01:09 +0000827 return DIArray(getNodeField(DbgNode, 5));
Bill Wendling523bea82013-11-08 08:13:15 +0000828}
829
830DIArray DICompileUnit::getImportedEntities() const {
Duncan P. N. Exon Smith176b6912014-10-03 20:01:09 +0000831 if (!DbgNode || DbgNode->getNumOperands() < 7)
Bill Wendling523bea82013-11-08 08:13:15 +0000832 return DIArray();
833
Duncan P. N. Exon Smith176b6912014-10-03 20:01:09 +0000834 return DIArray(getNodeField(DbgNode, 6));
835}
836
837void DICompileUnit::replaceSubprograms(DIArray Subprograms) {
838 assert(Verify() && "Expected compile unit");
839 if (Subprograms == getSubprograms())
840 return;
841
842 const_cast<MDNode *>(DbgNode)->replaceOperandWith(4, Subprograms);
843}
844
845void DICompileUnit::replaceGlobalVariables(DIArray GlobalVariables) {
846 assert(Verify() && "Expected compile unit");
847 if (GlobalVariables == getGlobalVariables())
848 return;
849
850 const_cast<MDNode *>(DbgNode)->replaceOperandWith(5, GlobalVariables);
Bill Wendling523bea82013-11-08 08:13:15 +0000851}
852
Diego Novillof5041ce2014-03-03 20:06:11 +0000853DILocation DILocation::copyWithNewScope(LLVMContext &Ctx,
David Blaikie2f3f76f2014-08-21 22:45:21 +0000854 DILexicalBlockFile NewScope) {
Diego Novillof5041ce2014-03-03 20:06:11 +0000855 assert(Verify());
Duncan P. N. Exon Smith98854692015-01-14 22:27:36 +0000856 assert(NewScope && "Expected valid scope");
857
858 const auto *Old = cast<MDLocation>(DbgNode);
859 return DILocation(MDLocation::get(Ctx, Old->getLine(), Old->getColumn(),
860 NewScope, Old->getInlinedAt()));
Diego Novillof5041ce2014-03-03 20:06:11 +0000861}
862
Diego Novillof5041ce2014-03-03 20:06:11 +0000863unsigned DILocation::computeNewDiscriminator(LLVMContext &Ctx) {
864 std::pair<const char *, unsigned> Key(getFilename().data(), getLineNumber());
865 return ++Ctx.pImpl->DiscriminatorTable[Key];
866}
867
Bill Wendling523bea82013-11-08 08:13:15 +0000868DIVariable llvm::createInlinedVariable(MDNode *DV, MDNode *InlinedScope,
869 LLVMContext &VMContext) {
Duncan P. N. Exon Smith176b6912014-10-03 20:01:09 +0000870 assert(DIVariable(DV).Verify() && "Expected a DIVariable");
871 if (!InlinedScope)
872 return cleanseInlinedVariable(DV, VMContext);
873
874 // Insert inlined scope.
Duncan P. N. Exon Smith5bf8fef2014-12-09 18:38:53 +0000875 SmallVector<Metadata *, 8> Elts;
Duncan P. N. Exon Smith176b6912014-10-03 20:01:09 +0000876 for (unsigned I = 0, E = DIVariableInlinedAtIndex; I != E; ++I)
877 Elts.push_back(DV->getOperand(I));
878 Elts.push_back(InlinedScope);
879
880 DIVariable Inlined(MDNode::get(VMContext, Elts));
881 assert(Inlined.Verify() && "Expected to create a DIVariable");
882 return Inlined;
Bill Wendling523bea82013-11-08 08:13:15 +0000883}
884
Bill Wendling523bea82013-11-08 08:13:15 +0000885DIVariable llvm::cleanseInlinedVariable(MDNode *DV, LLVMContext &VMContext) {
Duncan P. N. Exon Smith176b6912014-10-03 20:01:09 +0000886 assert(DIVariable(DV).Verify() && "Expected a DIVariable");
887 if (!DIVariable(DV).getInlinedAt())
888 return DIVariable(DV);
889
890 // Remove inlined scope.
Duncan P. N. Exon Smith5bf8fef2014-12-09 18:38:53 +0000891 SmallVector<Metadata *, 8> Elts;
Duncan P. N. Exon Smith176b6912014-10-03 20:01:09 +0000892 for (unsigned I = 0, E = DIVariableInlinedAtIndex; I != E; ++I)
893 Elts.push_back(DV->getOperand(I));
894
895 DIVariable Cleansed(MDNode::get(VMContext, Elts));
896 assert(Cleansed.Verify() && "Expected to create a DIVariable");
897 return Cleansed;
Bill Wendling523bea82013-11-08 08:13:15 +0000898}
899
Bill Wendling523bea82013-11-08 08:13:15 +0000900DISubprogram llvm::getDISubprogram(const MDNode *Scope) {
901 DIDescriptor D(Scope);
902 if (D.isSubprogram())
903 return DISubprogram(Scope);
904
905 if (D.isLexicalBlockFile())
906 return getDISubprogram(DILexicalBlockFile(Scope).getContext());
907
908 if (D.isLexicalBlock())
909 return getDISubprogram(DILexicalBlock(Scope).getContext());
910
911 return DISubprogram();
912}
913
Timur Iskhodzhanoveb229ca2014-10-23 23:46:28 +0000914DISubprogram llvm::getDISubprogram(const Function *F) {
915 // We look for the first instr that has a debug annotation leading back to F.
Timur Iskhodzhanoveb229ca2014-10-23 23:46:28 +0000916 for (auto &BB : *F) {
David Majnemerc758df42014-11-01 07:57:14 +0000917 auto Inst = std::find_if(BB.begin(), BB.end(), [](const Instruction &Inst) {
918 return !Inst.getDebugLoc().isUnknown();
919 });
920 if (Inst == BB.end())
921 continue;
922 DebugLoc DLoc = Inst->getDebugLoc();
Duncan P. N. Exon Smith5bf8fef2014-12-09 18:38:53 +0000923 const MDNode *Scope = DLoc.getScopeNode();
David Majnemerc758df42014-11-01 07:57:14 +0000924 DISubprogram Subprogram = getDISubprogram(Scope);
925 return Subprogram.describes(F) ? Subprogram : DISubprogram();
Timur Iskhodzhanoveb229ca2014-10-23 23:46:28 +0000926 }
927
928 return DISubprogram();
929}
930
Bill Wendling523bea82013-11-08 08:13:15 +0000931DICompositeType llvm::getDICompositeType(DIType T) {
932 if (T.isCompositeType())
933 return DICompositeType(T);
934
935 if (T.isDerivedType()) {
936 // This function is currently used by dragonegg and dragonegg does
937 // not generate identifier for types, so using an empty map to resolve
938 // DerivedFrom should be fine.
939 DITypeIdentifierMap EmptyMap;
940 return getDICompositeType(
941 DIDerivedType(T).getTypeDerivedFrom().resolve(EmptyMap));
942 }
943
944 return DICompositeType();
945}
946
Bill Wendling523bea82013-11-08 08:13:15 +0000947DITypeIdentifierMap
948llvm::generateDITypeIdentifierMap(const NamedMDNode *CU_Nodes) {
949 DITypeIdentifierMap Map;
950 for (unsigned CUi = 0, CUe = CU_Nodes->getNumOperands(); CUi != CUe; ++CUi) {
Duncan P. N. Exon Smithde36e802014-11-11 21:30:22 +0000951 DICompileUnit CU(CU_Nodes->getOperand(CUi));
Bill Wendling523bea82013-11-08 08:13:15 +0000952 DIArray Retain = CU.getRetainedTypes();
953 for (unsigned Ti = 0, Te = Retain.getNumElements(); Ti != Te; ++Ti) {
954 if (!Retain.getElement(Ti).isCompositeType())
955 continue;
956 DICompositeType Ty(Retain.getElement(Ti));
957 if (MDString *TypeId = Ty.getIdentifier()) {
958 // Definition has priority over declaration.
959 // Try to insert (TypeId, Ty) to Map.
960 std::pair<DITypeIdentifierMap::iterator, bool> P =
961 Map.insert(std::make_pair(TypeId, Ty));
962 // If TypeId already exists in Map and this is a definition, replace
963 // whatever we had (declaration or definition) with the definition.
964 if (!P.second && !Ty.isForwardDecl())
965 P.first->second = Ty;
966 }
967 }
968 }
969 return Map;
970}
971
972//===----------------------------------------------------------------------===//
973// DebugInfoFinder implementations.
974//===----------------------------------------------------------------------===//
975
976void DebugInfoFinder::reset() {
977 CUs.clear();
978 SPs.clear();
979 GVs.clear();
980 TYs.clear();
981 Scopes.clear();
982 NodesSeen.clear();
983 TypeIdentifierMap.clear();
Manman Ren2085ccc2013-11-17 18:42:37 +0000984 TypeMapInitialized = false;
985}
986
Manman Renb46e5502013-11-17 19:35:03 +0000987void DebugInfoFinder::InitializeTypeMap(const Module &M) {
Manman Ren2085ccc2013-11-17 18:42:37 +0000988 if (!TypeMapInitialized)
989 if (NamedMDNode *CU_Nodes = M.getNamedMetadata("llvm.dbg.cu")) {
990 TypeIdentifierMap = generateDITypeIdentifierMap(CU_Nodes);
991 TypeMapInitialized = true;
992 }
Bill Wendling523bea82013-11-08 08:13:15 +0000993}
994
Bill Wendling523bea82013-11-08 08:13:15 +0000995void DebugInfoFinder::processModule(const Module &M) {
Manman Renb46e5502013-11-17 19:35:03 +0000996 InitializeTypeMap(M);
Bill Wendling523bea82013-11-08 08:13:15 +0000997 if (NamedMDNode *CU_Nodes = M.getNamedMetadata("llvm.dbg.cu")) {
Bill Wendling523bea82013-11-08 08:13:15 +0000998 for (unsigned i = 0, e = CU_Nodes->getNumOperands(); i != e; ++i) {
Duncan P. N. Exon Smithde36e802014-11-11 21:30:22 +0000999 DICompileUnit CU(CU_Nodes->getOperand(i));
Bill Wendling523bea82013-11-08 08:13:15 +00001000 addCompileUnit(CU);
1001 DIArray GVs = CU.getGlobalVariables();
1002 for (unsigned i = 0, e = GVs.getNumElements(); i != e; ++i) {
1003 DIGlobalVariable DIG(GVs.getElement(i));
1004 if (addGlobalVariable(DIG)) {
Manman Renf0a582b2014-11-21 19:55:23 +00001005 processScope(DIG.getContext());
Adrian Prantl1a1647c2014-03-18 02:34:58 +00001006 processType(DIG.getType().resolve(TypeIdentifierMap));
Bill Wendling523bea82013-11-08 08:13:15 +00001007 }
1008 }
1009 DIArray SPs = CU.getSubprograms();
1010 for (unsigned i = 0, e = SPs.getNumElements(); i != e; ++i)
1011 processSubprogram(DISubprogram(SPs.getElement(i)));
1012 DIArray EnumTypes = CU.getEnumTypes();
1013 for (unsigned i = 0, e = EnumTypes.getNumElements(); i != e; ++i)
1014 processType(DIType(EnumTypes.getElement(i)));
1015 DIArray RetainedTypes = CU.getRetainedTypes();
1016 for (unsigned i = 0, e = RetainedTypes.getNumElements(); i != e; ++i)
1017 processType(DIType(RetainedTypes.getElement(i)));
1018 DIArray Imports = CU.getImportedEntities();
1019 for (unsigned i = 0, e = Imports.getNumElements(); i != e; ++i) {
1020 DIImportedEntity Import = DIImportedEntity(Imports.getElement(i));
Adrian Prantld09ba232014-04-01 03:41:04 +00001021 DIDescriptor Entity = Import.getEntity().resolve(TypeIdentifierMap);
Bill Wendling523bea82013-11-08 08:13:15 +00001022 if (Entity.isType())
1023 processType(DIType(Entity));
1024 else if (Entity.isSubprogram())
1025 processSubprogram(DISubprogram(Entity));
1026 else if (Entity.isNameSpace())
1027 processScope(DINameSpace(Entity).getContext());
1028 }
1029 }
1030 }
1031}
1032
Manman Ren2085ccc2013-11-17 18:42:37 +00001033void DebugInfoFinder::processLocation(const Module &M, DILocation Loc) {
Bill Wendling523bea82013-11-08 08:13:15 +00001034 if (!Loc)
1035 return;
Manman Renb46e5502013-11-17 19:35:03 +00001036 InitializeTypeMap(M);
Bill Wendling523bea82013-11-08 08:13:15 +00001037 processScope(Loc.getScope());
Manman Ren2085ccc2013-11-17 18:42:37 +00001038 processLocation(M, Loc.getOrigLocation());
Bill Wendling523bea82013-11-08 08:13:15 +00001039}
1040
Bill Wendling523bea82013-11-08 08:13:15 +00001041void DebugInfoFinder::processType(DIType DT) {
1042 if (!addType(DT))
1043 return;
1044 processScope(DT.getContext().resolve(TypeIdentifierMap));
1045 if (DT.isCompositeType()) {
1046 DICompositeType DCT(DT);
1047 processType(DCT.getTypeDerivedFrom().resolve(TypeIdentifierMap));
Manman Renf8a19672014-07-28 22:24:06 +00001048 if (DT.isSubroutineType()) {
1049 DITypeArray DTA = DISubroutineType(DT).getTypeArray();
1050 for (unsigned i = 0, e = DTA.getNumElements(); i != e; ++i)
1051 processType(DTA.getElement(i).resolve(TypeIdentifierMap));
1052 return;
1053 }
Manman Renab8ffba2014-07-28 19:14:13 +00001054 DIArray DA = DCT.getElements();
Bill Wendling523bea82013-11-08 08:13:15 +00001055 for (unsigned i = 0, e = DA.getNumElements(); i != e; ++i) {
1056 DIDescriptor D = DA.getElement(i);
1057 if (D.isType())
1058 processType(DIType(D));
1059 else if (D.isSubprogram())
1060 processSubprogram(DISubprogram(D));
1061 }
1062 } else if (DT.isDerivedType()) {
1063 DIDerivedType DDT(DT);
1064 processType(DDT.getTypeDerivedFrom().resolve(TypeIdentifierMap));
1065 }
1066}
1067
1068void DebugInfoFinder::processScope(DIScope Scope) {
1069 if (Scope.isType()) {
1070 DIType Ty(Scope);
1071 processType(Ty);
1072 return;
1073 }
1074 if (Scope.isCompileUnit()) {
1075 addCompileUnit(DICompileUnit(Scope));
1076 return;
1077 }
1078 if (Scope.isSubprogram()) {
1079 processSubprogram(DISubprogram(Scope));
1080 return;
1081 }
1082 if (!addScope(Scope))
1083 return;
1084 if (Scope.isLexicalBlock()) {
1085 DILexicalBlock LB(Scope);
1086 processScope(LB.getContext());
1087 } else if (Scope.isLexicalBlockFile()) {
1088 DILexicalBlockFile LBF = DILexicalBlockFile(Scope);
1089 processScope(LBF.getScope());
1090 } else if (Scope.isNameSpace()) {
1091 DINameSpace NS(Scope);
1092 processScope(NS.getContext());
1093 }
1094}
1095
Bill Wendling523bea82013-11-08 08:13:15 +00001096void DebugInfoFinder::processSubprogram(DISubprogram SP) {
1097 if (!addSubprogram(SP))
1098 return;
1099 processScope(SP.getContext().resolve(TypeIdentifierMap));
1100 processType(SP.getType());
1101 DIArray TParams = SP.getTemplateParams();
1102 for (unsigned I = 0, E = TParams.getNumElements(); I != E; ++I) {
1103 DIDescriptor Element = TParams.getElement(I);
1104 if (Element.isTemplateTypeParameter()) {
1105 DITemplateTypeParameter TType(Element);
1106 processScope(TType.getContext().resolve(TypeIdentifierMap));
1107 processType(TType.getType().resolve(TypeIdentifierMap));
1108 } else if (Element.isTemplateValueParameter()) {
1109 DITemplateValueParameter TVal(Element);
1110 processScope(TVal.getContext().resolve(TypeIdentifierMap));
1111 processType(TVal.getType().resolve(TypeIdentifierMap));
1112 }
1113 }
1114}
1115
Manman Ren2085ccc2013-11-17 18:42:37 +00001116void DebugInfoFinder::processDeclare(const Module &M,
1117 const DbgDeclareInst *DDI) {
Bill Wendling523bea82013-11-08 08:13:15 +00001118 MDNode *N = dyn_cast<MDNode>(DDI->getVariable());
1119 if (!N)
1120 return;
Manman Renb46e5502013-11-17 19:35:03 +00001121 InitializeTypeMap(M);
Bill Wendling523bea82013-11-08 08:13:15 +00001122
1123 DIDescriptor DV(N);
1124 if (!DV.isVariable())
1125 return;
1126
David Blaikie70573dc2014-11-19 07:49:26 +00001127 if (!NodesSeen.insert(DV).second)
Bill Wendling523bea82013-11-08 08:13:15 +00001128 return;
1129 processScope(DIVariable(N).getContext());
Adrian Prantl1a1647c2014-03-18 02:34:58 +00001130 processType(DIVariable(N).getType().resolve(TypeIdentifierMap));
Bill Wendling523bea82013-11-08 08:13:15 +00001131}
1132
Manman Ren2085ccc2013-11-17 18:42:37 +00001133void DebugInfoFinder::processValue(const Module &M, const DbgValueInst *DVI) {
Bill Wendling523bea82013-11-08 08:13:15 +00001134 MDNode *N = dyn_cast<MDNode>(DVI->getVariable());
1135 if (!N)
1136 return;
Manman Renb46e5502013-11-17 19:35:03 +00001137 InitializeTypeMap(M);
Bill Wendling523bea82013-11-08 08:13:15 +00001138
1139 DIDescriptor DV(N);
1140 if (!DV.isVariable())
1141 return;
1142
David Blaikie70573dc2014-11-19 07:49:26 +00001143 if (!NodesSeen.insert(DV).second)
Bill Wendling523bea82013-11-08 08:13:15 +00001144 return;
1145 processScope(DIVariable(N).getContext());
Adrian Prantl1a1647c2014-03-18 02:34:58 +00001146 processType(DIVariable(N).getType().resolve(TypeIdentifierMap));
Bill Wendling523bea82013-11-08 08:13:15 +00001147}
1148
Bill Wendling523bea82013-11-08 08:13:15 +00001149bool DebugInfoFinder::addType(DIType DT) {
1150 if (!DT)
1151 return false;
1152
David Blaikie70573dc2014-11-19 07:49:26 +00001153 if (!NodesSeen.insert(DT).second)
Bill Wendling523bea82013-11-08 08:13:15 +00001154 return false;
1155
1156 TYs.push_back(DT);
1157 return true;
1158}
1159
Bill Wendling523bea82013-11-08 08:13:15 +00001160bool DebugInfoFinder::addCompileUnit(DICompileUnit CU) {
1161 if (!CU)
1162 return false;
David Blaikie70573dc2014-11-19 07:49:26 +00001163 if (!NodesSeen.insert(CU).second)
Bill Wendling523bea82013-11-08 08:13:15 +00001164 return false;
1165
1166 CUs.push_back(CU);
1167 return true;
1168}
1169
Bill Wendling523bea82013-11-08 08:13:15 +00001170bool DebugInfoFinder::addGlobalVariable(DIGlobalVariable DIG) {
1171 if (!DIG)
1172 return false;
1173
David Blaikie70573dc2014-11-19 07:49:26 +00001174 if (!NodesSeen.insert(DIG).second)
Bill Wendling523bea82013-11-08 08:13:15 +00001175 return false;
1176
1177 GVs.push_back(DIG);
1178 return true;
1179}
1180
Bill Wendling523bea82013-11-08 08:13:15 +00001181bool DebugInfoFinder::addSubprogram(DISubprogram SP) {
1182 if (!SP)
1183 return false;
1184
David Blaikie70573dc2014-11-19 07:49:26 +00001185 if (!NodesSeen.insert(SP).second)
Bill Wendling523bea82013-11-08 08:13:15 +00001186 return false;
1187
1188 SPs.push_back(SP);
1189 return true;
1190}
1191
1192bool DebugInfoFinder::addScope(DIScope Scope) {
1193 if (!Scope)
1194 return false;
1195 // FIXME: Ocaml binding generates a scope with no content, we treat it
1196 // as null for now.
1197 if (Scope->getNumOperands() == 0)
1198 return false;
David Blaikie70573dc2014-11-19 07:49:26 +00001199 if (!NodesSeen.insert(Scope).second)
Bill Wendling523bea82013-11-08 08:13:15 +00001200 return false;
1201 Scopes.push_back(Scope);
1202 return true;
1203}
1204
1205//===----------------------------------------------------------------------===//
1206// DIDescriptor: dump routines for all descriptors.
1207//===----------------------------------------------------------------------===//
1208
Bill Wendling523bea82013-11-08 08:13:15 +00001209void DIDescriptor::dump() const {
1210 print(dbgs());
1211 dbgs() << '\n';
1212}
1213
Bill Wendling523bea82013-11-08 08:13:15 +00001214void DIDescriptor::print(raw_ostream &OS) const {
1215 if (!DbgNode)
1216 return;
1217
1218 if (const char *Tag = dwarf::TagString(getTag()))
1219 OS << "[ " << Tag << " ]";
1220
1221 if (this->isSubrange()) {
1222 DISubrange(DbgNode).printInternal(OS);
1223 } else if (this->isCompileUnit()) {
1224 DICompileUnit(DbgNode).printInternal(OS);
1225 } else if (this->isFile()) {
1226 DIFile(DbgNode).printInternal(OS);
1227 } else if (this->isEnumerator()) {
1228 DIEnumerator(DbgNode).printInternal(OS);
1229 } else if (this->isBasicType()) {
1230 DIType(DbgNode).printInternal(OS);
1231 } else if (this->isDerivedType()) {
1232 DIDerivedType(DbgNode).printInternal(OS);
1233 } else if (this->isCompositeType()) {
1234 DICompositeType(DbgNode).printInternal(OS);
1235 } else if (this->isSubprogram()) {
1236 DISubprogram(DbgNode).printInternal(OS);
1237 } else if (this->isGlobalVariable()) {
1238 DIGlobalVariable(DbgNode).printInternal(OS);
1239 } else if (this->isVariable()) {
1240 DIVariable(DbgNode).printInternal(OS);
1241 } else if (this->isObjCProperty()) {
1242 DIObjCProperty(DbgNode).printInternal(OS);
1243 } else if (this->isNameSpace()) {
1244 DINameSpace(DbgNode).printInternal(OS);
1245 } else if (this->isScope()) {
1246 DIScope(DbgNode).printInternal(OS);
Adrian Prantl87b7eb92014-10-01 18:55:02 +00001247 } else if (this->isExpression()) {
1248 DIExpression(DbgNode).printInternal(OS);
Bill Wendling523bea82013-11-08 08:13:15 +00001249 }
1250}
1251
1252void DISubrange::printInternal(raw_ostream &OS) const {
1253 int64_t Count = getCount();
1254 if (Count != -1)
1255 OS << " [" << getLo() << ", " << Count - 1 << ']';
1256 else
1257 OS << " [unbounded]";
1258}
1259
1260void DIScope::printInternal(raw_ostream &OS) const {
1261 OS << " [" << getDirectory() << "/" << getFilename() << ']';
1262}
1263
1264void DICompileUnit::printInternal(raw_ostream &OS) const {
1265 DIScope::printInternal(OS);
1266 OS << " [";
1267 unsigned Lang = getLanguage();
1268 if (const char *LangStr = dwarf::LanguageString(Lang))
1269 OS << LangStr;
1270 else
1271 (OS << "lang 0x").write_hex(Lang);
1272 OS << ']';
1273}
1274
1275void DIEnumerator::printInternal(raw_ostream &OS) const {
1276 OS << " [" << getName() << " :: " << getEnumValue() << ']';
1277}
1278
1279void DIType::printInternal(raw_ostream &OS) const {
Manman Renf93ac4b2014-07-29 18:20:39 +00001280 if (!DbgNode)
Bill Wendling523bea82013-11-08 08:13:15 +00001281 return;
1282
1283 StringRef Res = getName();
1284 if (!Res.empty())
1285 OS << " [" << Res << "]";
1286
1287 // TODO: Print context?
1288
1289 OS << " [line " << getLineNumber() << ", size " << getSizeInBits()
1290 << ", align " << getAlignInBits() << ", offset " << getOffsetInBits();
1291 if (isBasicType())
1292 if (const char *Enc =
1293 dwarf::AttributeEncodingString(DIBasicType(DbgNode).getEncoding()))
1294 OS << ", enc " << Enc;
1295 OS << "]";
1296
1297 if (isPrivate())
1298 OS << " [private]";
1299 else if (isProtected())
1300 OS << " [protected]";
Adrian Prantldaedfda2014-08-29 22:44:07 +00001301 else if (isPublic())
1302 OS << " [public]";
Bill Wendling523bea82013-11-08 08:13:15 +00001303
1304 if (isArtificial())
1305 OS << " [artificial]";
1306
1307 if (isForwardDecl())
1308 OS << " [decl]";
1309 else if (getTag() == dwarf::DW_TAG_structure_type ||
1310 getTag() == dwarf::DW_TAG_union_type ||
1311 getTag() == dwarf::DW_TAG_enumeration_type ||
1312 getTag() == dwarf::DW_TAG_class_type)
1313 OS << " [def]";
1314 if (isVector())
1315 OS << " [vector]";
1316 if (isStaticMember())
1317 OS << " [static]";
Adrian Prantl99c7af22013-12-18 21:48:19 +00001318
1319 if (isLValueReference())
1320 OS << " [reference]";
1321
1322 if (isRValueReference())
1323 OS << " [rvalue reference]";
Bill Wendling523bea82013-11-08 08:13:15 +00001324}
1325
1326void DIDerivedType::printInternal(raw_ostream &OS) const {
1327 DIType::printInternal(OS);
1328 OS << " [from " << getTypeDerivedFrom().getName() << ']';
1329}
1330
1331void DICompositeType::printInternal(raw_ostream &OS) const {
1332 DIType::printInternal(OS);
Manman Renab8ffba2014-07-28 19:14:13 +00001333 DIArray A = getElements();
Bill Wendling523bea82013-11-08 08:13:15 +00001334 OS << " [" << A.getNumElements() << " elements]";
1335}
1336
1337void DINameSpace::printInternal(raw_ostream &OS) const {
1338 StringRef Name = getName();
1339 if (!Name.empty())
1340 OS << " [" << Name << ']';
1341
1342 OS << " [line " << getLineNumber() << ']';
1343}
1344
1345void DISubprogram::printInternal(raw_ostream &OS) const {
1346 // TODO : Print context
1347 OS << " [line " << getLineNumber() << ']';
1348
1349 if (isLocalToUnit())
1350 OS << " [local]";
1351
1352 if (isDefinition())
1353 OS << " [def]";
1354
1355 if (getScopeLineNumber() != getLineNumber())
1356 OS << " [scope " << getScopeLineNumber() << "]";
1357
1358 if (isPrivate())
1359 OS << " [private]";
1360 else if (isProtected())
1361 OS << " [protected]";
Adrian Prantldaedfda2014-08-29 22:44:07 +00001362 else if (isPublic())
1363 OS << " [public]";
Bill Wendling523bea82013-11-08 08:13:15 +00001364
Adrian Prantl99c7af22013-12-18 21:48:19 +00001365 if (isLValueReference())
1366 OS << " [reference]";
1367
1368 if (isRValueReference())
1369 OS << " [rvalue reference]";
1370
Bill Wendling523bea82013-11-08 08:13:15 +00001371 StringRef Res = getName();
1372 if (!Res.empty())
1373 OS << " [" << Res << ']';
1374}
1375
1376void DIGlobalVariable::printInternal(raw_ostream &OS) const {
1377 StringRef Res = getName();
1378 if (!Res.empty())
1379 OS << " [" << Res << ']';
1380
1381 OS << " [line " << getLineNumber() << ']';
1382
1383 // TODO : Print context
1384
1385 if (isLocalToUnit())
1386 OS << " [local]";
1387
1388 if (isDefinition())
1389 OS << " [def]";
1390}
1391
1392void DIVariable::printInternal(raw_ostream &OS) const {
1393 StringRef Res = getName();
1394 if (!Res.empty())
1395 OS << " [" << Res << ']';
1396
1397 OS << " [line " << getLineNumber() << ']';
Adrian Prantl87b7eb92014-10-01 18:55:02 +00001398}
Adrian Prantlb1416832014-08-01 22:11:58 +00001399
Adrian Prantl87b7eb92014-10-01 18:55:02 +00001400void DIExpression::printInternal(raw_ostream &OS) const {
1401 for (unsigned I = 0; I < getNumElements(); ++I) {
1402 uint64_t OpCode = getElement(I);
1403 OS << " [" << OperationEncodingString(OpCode);
1404 switch (OpCode) {
1405 case DW_OP_plus: {
1406 OS << " " << getElement(++I);
1407 break;
1408 }
1409 case DW_OP_piece: {
1410 unsigned Offset = getElement(++I);
1411 unsigned Size = getElement(++I);
Adrian Prantl38666f12014-10-02 16:42:15 +00001412 OS << " offset=" << Offset << ", size=" << Size;
Adrian Prantl87b7eb92014-10-01 18:55:02 +00001413 break;
1414 }
Adrian Prantlb9a88e22014-12-05 18:19:38 +00001415 case DW_OP_deref:
1416 // No arguments.
1417 break;
Adrian Prantl87b7eb92014-10-01 18:55:02 +00001418 default:
Adrian Prantl75a0dac2014-10-02 16:42:13 +00001419 // Else bail out early. This may be a line table entry.
1420 OS << "Unknown]";
1421 return;
Adrian Prantl87b7eb92014-10-01 18:55:02 +00001422 }
1423 OS << "]";
1424 }
Bill Wendling523bea82013-11-08 08:13:15 +00001425}
1426
1427void DIObjCProperty::printInternal(raw_ostream &OS) const {
1428 StringRef Name = getObjCPropertyName();
1429 if (!Name.empty())
1430 OS << " [" << Name << ']';
1431
1432 OS << " [line " << getLineNumber() << ", properties " << getUnsignedField(6)
1433 << ']';
1434}
1435
1436static void printDebugLoc(DebugLoc DL, raw_ostream &CommentOS,
1437 const LLVMContext &Ctx) {
1438 if (!DL.isUnknown()) { // Print source line info.
1439 DIScope Scope(DL.getScope(Ctx));
1440 assert(Scope.isScope() && "Scope of a DebugLoc should be a DIScope.");
1441 // Omit the directory, because it's likely to be long and uninteresting.
1442 CommentOS << Scope.getFilename();
1443 CommentOS << ':' << DL.getLine();
1444 if (DL.getCol() != 0)
1445 CommentOS << ':' << DL.getCol();
1446 DebugLoc InlinedAtDL = DebugLoc::getFromDILocation(DL.getInlinedAt(Ctx));
1447 if (!InlinedAtDL.isUnknown()) {
1448 CommentOS << " @[ ";
1449 printDebugLoc(InlinedAtDL, CommentOS, Ctx);
1450 CommentOS << " ]";
1451 }
1452 }
1453}
1454
1455void DIVariable::printExtendedName(raw_ostream &OS) const {
1456 const LLVMContext &Ctx = DbgNode->getContext();
1457 StringRef Res = getName();
1458 if (!Res.empty())
1459 OS << Res << "," << getLineNumber();
1460 if (MDNode *InlinedAt = getInlinedAt()) {
1461 DebugLoc InlinedAtDL = DebugLoc::getFromDILocation(InlinedAt);
1462 if (!InlinedAtDL.isUnknown()) {
1463 OS << " @[";
1464 printDebugLoc(InlinedAtDL, OS, Ctx);
1465 OS << "]";
1466 }
1467 }
1468}
1469
Duncan P. N. Exon Smithc81307a2014-11-14 23:55:03 +00001470template <> DIRef<DIScope>::DIRef(const Metadata *V) : Val(V) {
Bill Wendling523bea82013-11-08 08:13:15 +00001471 assert(isScopeRef(V) && "DIScopeRef should be a MDString or MDNode");
1472}
Duncan P. N. Exon Smithc81307a2014-11-14 23:55:03 +00001473template <> DIRef<DIType>::DIRef(const Metadata *V) : Val(V) {
Bill Wendling523bea82013-11-08 08:13:15 +00001474 assert(isTypeRef(V) && "DITypeRef should be a MDString or MDNode");
1475}
1476
Bill Wendling523bea82013-11-08 08:13:15 +00001477template <>
1478DIScopeRef DIDescriptor::getFieldAs<DIScopeRef>(unsigned Elt) const {
Duncan P. N. Exon Smithc81307a2014-11-14 23:55:03 +00001479 return DIScopeRef(cast_or_null<Metadata>(getField(DbgNode, Elt)));
Bill Wendling523bea82013-11-08 08:13:15 +00001480}
Bill Wendling523bea82013-11-08 08:13:15 +00001481template <> DITypeRef DIDescriptor::getFieldAs<DITypeRef>(unsigned Elt) const {
Duncan P. N. Exon Smithc81307a2014-11-14 23:55:03 +00001482 return DITypeRef(cast_or_null<Metadata>(getField(DbgNode, Elt)));
Bill Wendling523bea82013-11-08 08:13:15 +00001483}
Manman Rencb14bbc2013-11-22 22:06:31 +00001484
Manman Rencb14bbc2013-11-22 22:06:31 +00001485bool llvm::StripDebugInfo(Module &M) {
Manman Rencb14bbc2013-11-22 22:06:31 +00001486 bool Changed = false;
1487
1488 // Remove all of the calls to the debugger intrinsics, and remove them from
1489 // the module.
1490 if (Function *Declare = M.getFunction("llvm.dbg.declare")) {
1491 while (!Declare->use_empty()) {
Chandler Carruthcdf47882014-03-09 03:16:01 +00001492 CallInst *CI = cast<CallInst>(Declare->user_back());
Manman Rencb14bbc2013-11-22 22:06:31 +00001493 CI->eraseFromParent();
1494 }
1495 Declare->eraseFromParent();
1496 Changed = true;
1497 }
1498
1499 if (Function *DbgVal = M.getFunction("llvm.dbg.value")) {
1500 while (!DbgVal->use_empty()) {
Chandler Carruthcdf47882014-03-09 03:16:01 +00001501 CallInst *CI = cast<CallInst>(DbgVal->user_back());
Manman Rencb14bbc2013-11-22 22:06:31 +00001502 CI->eraseFromParent();
1503 }
1504 DbgVal->eraseFromParent();
1505 Changed = true;
1506 }
1507
1508 for (Module::named_metadata_iterator NMI = M.named_metadata_begin(),
1509 NME = M.named_metadata_end(); NMI != NME;) {
1510 NamedMDNode *NMD = NMI;
1511 ++NMI;
1512 if (NMD->getName().startswith("llvm.dbg.")) {
1513 NMD->eraseFromParent();
1514 Changed = true;
1515 }
1516 }
1517
1518 for (Module::iterator MI = M.begin(), ME = M.end(); MI != ME; ++MI)
1519 for (Function::iterator FI = MI->begin(), FE = MI->end(); FI != FE;
1520 ++FI)
1521 for (BasicBlock::iterator BI = FI->begin(), BE = FI->end(); BI != BE;
1522 ++BI) {
1523 if (!BI->getDebugLoc().isUnknown()) {
1524 Changed = true;
1525 BI->setDebugLoc(DebugLoc());
1526 }
1527 }
1528
1529 return Changed;
1530}
Manman Ren8b4306c2013-12-02 21:29:56 +00001531
Manman Renbd4daf82013-12-03 00:12:14 +00001532unsigned llvm::getDebugMetadataVersionFromModule(const Module &M) {
Duncan P. N. Exon Smith5bf8fef2014-12-09 18:38:53 +00001533 if (auto *Val = mdconst::extract_or_null<ConstantInt>(
1534 M.getModuleFlag("Debug Info Version")))
1535 return Val->getZExtValue();
1536 return 0;
Manman Ren8b4306c2013-12-02 21:29:56 +00001537}
David Blaikie6876b3b2014-07-01 20:05:26 +00001538
David Blaikiea8c35092014-07-02 18:30:05 +00001539llvm::DenseMap<const llvm::Function *, llvm::DISubprogram>
1540llvm::makeSubprogramMap(const Module &M) {
1541 DenseMap<const Function *, DISubprogram> R;
David Blaikie6876b3b2014-07-01 20:05:26 +00001542
1543 NamedMDNode *CU_Nodes = M.getNamedMetadata("llvm.dbg.cu");
1544 if (!CU_Nodes)
1545 return R;
1546
Duncan P. N. Exon Smithde36e802014-11-11 21:30:22 +00001547 for (MDNode *N : CU_Nodes->operands()) {
1548 DICompileUnit CUNode(N);
David Blaikie6876b3b2014-07-01 20:05:26 +00001549 DIArray SPs = CUNode.getSubprograms();
1550 for (unsigned i = 0, e = SPs.getNumElements(); i != e; ++i) {
1551 DISubprogram SP(SPs.getElement(i));
1552 if (Function *F = SP.getFunction())
1553 R.insert(std::make_pair(F, SP));
1554 }
1555 }
1556 return R;
1557}