blob: 4a7bc67ab58f93ef2e6e6ac9b06516e4603d7c1d [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
Adrian Prantl2585a982015-01-22 16:55:20 +0000165DIExpression::iterator DIExpression::begin() const {
166 return DIExpression::iterator(*this);
Adrian Prantl9260cca2015-01-22 00:00:52 +0000167}
168
Adrian Prantl2585a982015-01-22 16:55:20 +0000169DIExpression::iterator DIExpression::end() const {
170 return DIExpression::iterator();
Adrian Prantl9260cca2015-01-22 00:00:52 +0000171}
172
Bill Wendling523bea82013-11-08 08:13:15 +0000173//===----------------------------------------------------------------------===//
174// Predicates
175//===----------------------------------------------------------------------===//
176
Manman Renf8a19672014-07-28 22:24:06 +0000177bool DIDescriptor::isSubroutineType() const {
Yaron Kerene8270222014-12-19 22:15:09 +0000178 return DbgNode && getTag() == dwarf::DW_TAG_subroutine_type;
Manman Renf8a19672014-07-28 22:24:06 +0000179}
180
Bill Wendling523bea82013-11-08 08:13:15 +0000181bool DIDescriptor::isBasicType() const {
182 if (!DbgNode)
183 return false;
184 switch (getTag()) {
185 case dwarf::DW_TAG_base_type:
186 case dwarf::DW_TAG_unspecified_type:
187 return true;
188 default:
189 return false;
190 }
191}
192
Bill Wendling523bea82013-11-08 08:13:15 +0000193bool DIDescriptor::isDerivedType() const {
194 if (!DbgNode)
195 return false;
196 switch (getTag()) {
197 case dwarf::DW_TAG_typedef:
198 case dwarf::DW_TAG_pointer_type:
199 case dwarf::DW_TAG_ptr_to_member_type:
200 case dwarf::DW_TAG_reference_type:
201 case dwarf::DW_TAG_rvalue_reference_type:
202 case dwarf::DW_TAG_const_type:
203 case dwarf::DW_TAG_volatile_type:
204 case dwarf::DW_TAG_restrict_type:
205 case dwarf::DW_TAG_member:
206 case dwarf::DW_TAG_inheritance:
207 case dwarf::DW_TAG_friend:
208 return true;
209 default:
210 // CompositeTypes are currently modelled as DerivedTypes.
211 return isCompositeType();
212 }
213}
214
Bill Wendling523bea82013-11-08 08:13:15 +0000215bool DIDescriptor::isCompositeType() const {
216 if (!DbgNode)
217 return false;
218 switch (getTag()) {
219 case dwarf::DW_TAG_array_type:
220 case dwarf::DW_TAG_structure_type:
221 case dwarf::DW_TAG_union_type:
222 case dwarf::DW_TAG_enumeration_type:
223 case dwarf::DW_TAG_subroutine_type:
224 case dwarf::DW_TAG_class_type:
225 return true;
226 default:
227 return false;
228 }
229}
230
Bill Wendling523bea82013-11-08 08:13:15 +0000231bool DIDescriptor::isVariable() const {
232 if (!DbgNode)
233 return false;
234 switch (getTag()) {
235 case dwarf::DW_TAG_auto_variable:
236 case dwarf::DW_TAG_arg_variable:
237 return true;
238 default:
239 return false;
240 }
241}
242
Bill Wendling523bea82013-11-08 08:13:15 +0000243bool DIDescriptor::isType() const {
Manman Renf93ac4b2014-07-29 18:20:39 +0000244 return isBasicType() || isCompositeType() || isDerivedType();
Bill Wendling523bea82013-11-08 08:13:15 +0000245}
246
Bill Wendling523bea82013-11-08 08:13:15 +0000247bool DIDescriptor::isSubprogram() const {
248 return DbgNode && getTag() == dwarf::DW_TAG_subprogram;
249}
250
Bill Wendling523bea82013-11-08 08:13:15 +0000251bool DIDescriptor::isGlobalVariable() const {
252 return DbgNode && (getTag() == dwarf::DW_TAG_variable ||
253 getTag() == dwarf::DW_TAG_constant);
254}
255
Bill Wendling523bea82013-11-08 08:13:15 +0000256bool DIDescriptor::isScope() const {
257 if (!DbgNode)
258 return false;
259 switch (getTag()) {
260 case dwarf::DW_TAG_compile_unit:
261 case dwarf::DW_TAG_lexical_block:
262 case dwarf::DW_TAG_subprogram:
263 case dwarf::DW_TAG_namespace:
264 case dwarf::DW_TAG_file_type:
265 return true;
266 default:
267 break;
268 }
269 return isType();
270}
271
Bill Wendling523bea82013-11-08 08:13:15 +0000272bool DIDescriptor::isTemplateTypeParameter() const {
273 return DbgNode && getTag() == dwarf::DW_TAG_template_type_parameter;
274}
275
Bill Wendling523bea82013-11-08 08:13:15 +0000276bool DIDescriptor::isTemplateValueParameter() const {
277 return DbgNode && (getTag() == dwarf::DW_TAG_template_value_parameter ||
278 getTag() == dwarf::DW_TAG_GNU_template_template_param ||
279 getTag() == dwarf::DW_TAG_GNU_template_parameter_pack);
280}
281
Bill Wendling523bea82013-11-08 08:13:15 +0000282bool DIDescriptor::isCompileUnit() const {
283 return DbgNode && getTag() == dwarf::DW_TAG_compile_unit;
284}
285
Bill Wendling523bea82013-11-08 08:13:15 +0000286bool DIDescriptor::isFile() const {
287 return DbgNode && getTag() == dwarf::DW_TAG_file_type;
288}
289
Bill Wendling523bea82013-11-08 08:13:15 +0000290bool DIDescriptor::isNameSpace() const {
291 return DbgNode && getTag() == dwarf::DW_TAG_namespace;
292}
293
Bill Wendling523bea82013-11-08 08:13:15 +0000294bool DIDescriptor::isLexicalBlockFile() const {
295 return DbgNode && getTag() == dwarf::DW_TAG_lexical_block &&
Duncan P. N. Exon Smith176b6912014-10-03 20:01:09 +0000296 DbgNode->getNumOperands() == 3 && getNumHeaderFields() == 2;
Bill Wendling523bea82013-11-08 08:13:15 +0000297}
298
Bill Wendling523bea82013-11-08 08:13:15 +0000299bool DIDescriptor::isLexicalBlock() const {
Duncan P. N. Exon Smith176b6912014-10-03 20:01:09 +0000300 // FIXME: There are always exactly 4 header fields in DILexicalBlock, but
301 // something relies on this returning true for DILexicalBlockFile.
Bill Wendling523bea82013-11-08 08:13:15 +0000302 return DbgNode && getTag() == dwarf::DW_TAG_lexical_block &&
Duncan P. N. Exon Smith176b6912014-10-03 20:01:09 +0000303 DbgNode->getNumOperands() == 3 &&
304 (getNumHeaderFields() == 2 || getNumHeaderFields() == 4);
Bill Wendling523bea82013-11-08 08:13:15 +0000305}
306
Bill Wendling523bea82013-11-08 08:13:15 +0000307bool DIDescriptor::isSubrange() const {
308 return DbgNode && getTag() == dwarf::DW_TAG_subrange_type;
309}
310
Bill Wendling523bea82013-11-08 08:13:15 +0000311bool DIDescriptor::isEnumerator() const {
312 return DbgNode && getTag() == dwarf::DW_TAG_enumerator;
313}
314
Bill Wendling523bea82013-11-08 08:13:15 +0000315bool DIDescriptor::isObjCProperty() const {
316 return DbgNode && getTag() == dwarf::DW_TAG_APPLE_property;
317}
318
Bill Wendling523bea82013-11-08 08:13:15 +0000319bool DIDescriptor::isImportedEntity() const {
320 return DbgNode && (getTag() == dwarf::DW_TAG_imported_module ||
321 getTag() == dwarf::DW_TAG_imported_declaration);
322}
323
Adrian Prantl87b7eb92014-10-01 18:55:02 +0000324bool DIDescriptor::isExpression() const {
325 return DbgNode && (getTag() == dwarf::DW_TAG_expression);
326}
327
Bill Wendling523bea82013-11-08 08:13:15 +0000328//===----------------------------------------------------------------------===//
329// Simple Descriptor Constructors and other Methods
330//===----------------------------------------------------------------------===//
331
Frederic Riss36acf0f2014-09-15 07:50:36 +0000332void DIDescriptor::replaceAllUsesWith(LLVMContext &VMContext, DIDescriptor D) {
Bill Wendling523bea82013-11-08 08:13:15 +0000333
334 assert(DbgNode && "Trying to replace an unverified type!");
335
336 // Since we use a TrackingVH for the node, its easy for clients to manufacture
337 // legitimate situations where they want to replaceAllUsesWith() on something
338 // which, due to uniquing, has merged with the source. We shield clients from
339 // this detail by allowing a value to be replaced with replaceAllUsesWith()
340 // itself.
David Blaikied3f094a2014-05-06 03:41:57 +0000341 const MDNode *DN = D;
342 if (DbgNode == DN) {
Duncan P. N. Exon Smith5bf8fef2014-12-09 18:38:53 +0000343 SmallVector<Metadata *, 10> Ops(DbgNode->getNumOperands());
David Blaikied3f094a2014-05-06 03:41:57 +0000344 for (size_t i = 0; i != Ops.size(); ++i)
345 Ops[i] = DbgNode->getOperand(i);
346 DN = MDNode::get(VMContext, Ops);
Bill Wendling523bea82013-11-08 08:13:15 +0000347 }
David Blaikied3f094a2014-05-06 03:41:57 +0000348
Duncan P. N. Exon Smith946fdcc2015-01-19 20:36:39 +0000349 assert(DbgNode->isTemporary() && "Expected temporary node");
350 auto *Node = const_cast<MDNode *>(DbgNode);
Duncan P. N. Exon Smith5bf8fef2014-12-09 18:38:53 +0000351 Node->replaceAllUsesWith(const_cast<MDNode *>(DN));
David Blaikied3f094a2014-05-06 03:41:57 +0000352 MDNode::deleteTemporary(Node);
Frederic Rissdd7aec52014-09-15 07:50:42 +0000353 DbgNode = DN;
Bill Wendling523bea82013-11-08 08:13:15 +0000354}
355
Frederic Riss36acf0f2014-09-15 07:50:36 +0000356void DIDescriptor::replaceAllUsesWith(MDNode *D) {
Bill Wendling523bea82013-11-08 08:13:15 +0000357 assert(DbgNode && "Trying to replace an unverified type!");
David Blaikied3f094a2014-05-06 03:41:57 +0000358 assert(DbgNode != D && "This replacement should always happen");
Duncan P. N. Exon Smith946fdcc2015-01-19 20:36:39 +0000359 assert(DbgNode->isTemporary() && "Expected temporary node");
360 auto *Node = const_cast<MDNode *>(DbgNode);
Duncan P. N. Exon Smith5bf8fef2014-12-09 18:38:53 +0000361 Node->replaceAllUsesWith(D);
David Blaikied3f094a2014-05-06 03:41:57 +0000362 MDNode::deleteTemporary(Node);
Bill Wendling523bea82013-11-08 08:13:15 +0000363}
364
Bill Wendling523bea82013-11-08 08:13:15 +0000365bool DICompileUnit::Verify() const {
366 if (!isCompileUnit())
367 return false;
368
369 // Don't bother verifying the compilation directory or producer string
370 // as those could be empty.
371 if (getFilename().empty())
372 return false;
373
Duncan P. N. Exon Smith176b6912014-10-03 20:01:09 +0000374 return DbgNode->getNumOperands() == 7 && getNumHeaderFields() == 8;
Bill Wendling523bea82013-11-08 08:13:15 +0000375}
376
Bill Wendling523bea82013-11-08 08:13:15 +0000377bool DIObjCProperty::Verify() const {
378 if (!isObjCProperty())
379 return false;
380
381 // Don't worry about the rest of the strings for now.
Duncan P. N. Exon Smith176b6912014-10-03 20:01:09 +0000382 return DbgNode->getNumOperands() == 3 && getNumHeaderFields() == 6;
Bill Wendling523bea82013-11-08 08:13:15 +0000383}
384
Duncan P. N. Exon Smith7f637a92014-10-15 17:01:28 +0000385/// \brief Check if a field at position Elt of a MDNode is a MDNode.
386///
Bill Wendling523bea82013-11-08 08:13:15 +0000387/// We currently allow an empty string and an integer.
388/// But we don't allow a non-empty string in a MDNode field.
389static bool fieldIsMDNode(const MDNode *DbgNode, unsigned Elt) {
390 // FIXME: This function should return true, if the field is null or the field
391 // is indeed a MDNode: return !Fld || isa<MDNode>(Fld).
Duncan P. N. Exon Smith5bf8fef2014-12-09 18:38:53 +0000392 Metadata *Fld = getField(DbgNode, Elt);
Bill Wendling523bea82013-11-08 08:13:15 +0000393 if (Fld && isa<MDString>(Fld) && !cast<MDString>(Fld)->getString().empty())
394 return false;
395 return true;
396}
397
Duncan P. N. Exon Smith7f637a92014-10-15 17:01:28 +0000398/// \brief Check if a field at position Elt of a MDNode is a MDString.
Bill Wendling523bea82013-11-08 08:13:15 +0000399static bool fieldIsMDString(const MDNode *DbgNode, unsigned Elt) {
Duncan P. N. Exon Smith5bf8fef2014-12-09 18:38:53 +0000400 Metadata *Fld = getField(DbgNode, Elt);
Bill Wendling523bea82013-11-08 08:13:15 +0000401 return !Fld || isa<MDString>(Fld);
402}
403
Duncan P. N. Exon Smith7f637a92014-10-15 17:01:28 +0000404/// \brief Check if a value can be a reference to a type.
Duncan P. N. Exon Smithc81307a2014-11-14 23:55:03 +0000405static bool isTypeRef(const Metadata *MD) {
406 if (!MD)
407 return true;
408 if (auto *S = dyn_cast<MDString>(MD))
409 return !S->getString().empty();
410 if (auto *N = dyn_cast<MDNode>(MD))
411 return DIType(N).isType();
412 return false;
Bill Wendling523bea82013-11-08 08:13:15 +0000413}
414
Duncan P. N. Exon Smith7f637a92014-10-15 17:01:28 +0000415/// \brief Check if referenced field might be a type.
Bill Wendling523bea82013-11-08 08:13:15 +0000416static bool fieldIsTypeRef(const MDNode *DbgNode, unsigned Elt) {
Duncan P. N. Exon Smithc81307a2014-11-14 23:55:03 +0000417 return isTypeRef(dyn_cast_or_null<Metadata>(getField(DbgNode, Elt)));
Bill Wendling523bea82013-11-08 08:13:15 +0000418}
419
Duncan P. N. Exon Smith7f637a92014-10-15 17:01:28 +0000420/// \brief Check if a value can be a ScopeRef.
Duncan P. N. Exon Smithc81307a2014-11-14 23:55:03 +0000421static bool isScopeRef(const Metadata *MD) {
422 if (!MD)
423 return true;
424 if (auto *S = dyn_cast<MDString>(MD))
425 return !S->getString().empty();
426 return isa<MDNode>(MD);
Bill Wendling523bea82013-11-08 08:13:15 +0000427}
428
Duncan P. N. Exon Smith7f637a92014-10-15 17:01:28 +0000429/// \brief Check if a field at position Elt of a MDNode can be a ScopeRef.
Bill Wendling523bea82013-11-08 08:13:15 +0000430static bool fieldIsScopeRef(const MDNode *DbgNode, unsigned Elt) {
Duncan P. N. Exon Smithc81307a2014-11-14 23:55:03 +0000431 return isScopeRef(dyn_cast_or_null<Metadata>(getField(DbgNode, Elt)));
Bill Wendling523bea82013-11-08 08:13:15 +0000432}
433
Bill Wendling523bea82013-11-08 08:13:15 +0000434bool DIType::Verify() const {
435 if (!isType())
436 return false;
437 // Make sure Context @ field 2 is MDNode.
438 if (!fieldIsScopeRef(DbgNode, 2))
439 return false;
440
441 // FIXME: Sink this into the various subclass verifies.
442 uint16_t Tag = getTag();
Manman Renf93ac4b2014-07-29 18:20:39 +0000443 if (!isBasicType() && Tag != dwarf::DW_TAG_const_type &&
Bill Wendling523bea82013-11-08 08:13:15 +0000444 Tag != dwarf::DW_TAG_volatile_type && Tag != dwarf::DW_TAG_pointer_type &&
445 Tag != dwarf::DW_TAG_ptr_to_member_type &&
446 Tag != dwarf::DW_TAG_reference_type &&
447 Tag != dwarf::DW_TAG_rvalue_reference_type &&
448 Tag != dwarf::DW_TAG_restrict_type && Tag != dwarf::DW_TAG_array_type &&
449 Tag != dwarf::DW_TAG_enumeration_type &&
450 Tag != dwarf::DW_TAG_subroutine_type &&
451 Tag != dwarf::DW_TAG_inheritance && Tag != dwarf::DW_TAG_friend &&
452 getFilename().empty())
453 return false;
Adrian Prantldaedfda2014-08-29 22:44:07 +0000454
Bill Wendling523bea82013-11-08 08:13:15 +0000455 // DIType is abstract, it should be a BasicType, a DerivedType or
456 // a CompositeType.
457 if (isBasicType())
Renato Golin47f46fd2013-11-26 16:47:00 +0000458 return DIBasicType(DbgNode).Verify();
Bill Wendling523bea82013-11-08 08:13:15 +0000459 else if (isCompositeType())
Renato Golin47f46fd2013-11-26 16:47:00 +0000460 return DICompositeType(DbgNode).Verify();
Bill Wendling523bea82013-11-08 08:13:15 +0000461 else if (isDerivedType())
Renato Golin47f46fd2013-11-26 16:47:00 +0000462 return DIDerivedType(DbgNode).Verify();
Bill Wendling523bea82013-11-08 08:13:15 +0000463 else
464 return false;
Bill Wendling523bea82013-11-08 08:13:15 +0000465}
466
Bill Wendling523bea82013-11-08 08:13:15 +0000467bool DIBasicType::Verify() const {
Duncan P. N. Exon Smith176b6912014-10-03 20:01:09 +0000468 return isBasicType() && DbgNode->getNumOperands() == 3 &&
469 getNumHeaderFields() == 8;
Bill Wendling523bea82013-11-08 08:13:15 +0000470}
471
Bill Wendling523bea82013-11-08 08:13:15 +0000472bool DIDerivedType::Verify() const {
Duncan P. N. Exon Smith176b6912014-10-03 20:01:09 +0000473 // Make sure DerivedFrom @ field 3 is TypeRef.
474 if (!fieldIsTypeRef(DbgNode, 3))
Bill Wendling523bea82013-11-08 08:13:15 +0000475 return false;
476 if (getTag() == dwarf::DW_TAG_ptr_to_member_type)
Duncan P. N. Exon Smith176b6912014-10-03 20:01:09 +0000477 // Make sure ClassType @ field 4 is a TypeRef.
478 if (!fieldIsTypeRef(DbgNode, 4))
Bill Wendling523bea82013-11-08 08:13:15 +0000479 return false;
480
Duncan P. N. Exon Smith176b6912014-10-03 20:01:09 +0000481 return isDerivedType() && DbgNode->getNumOperands() >= 4 &&
482 DbgNode->getNumOperands() <= 8 && getNumHeaderFields() >= 7 &&
483 getNumHeaderFields() <= 8;
Bill Wendling523bea82013-11-08 08:13:15 +0000484}
485
Bill Wendling523bea82013-11-08 08:13:15 +0000486bool DICompositeType::Verify() const {
487 if (!isCompositeType())
488 return false;
489
Duncan P. N. Exon Smith176b6912014-10-03 20:01:09 +0000490 // Make sure DerivedFrom @ field 3 and ContainingType @ field 5 are TypeRef.
491 if (!fieldIsTypeRef(DbgNode, 3))
Bill Wendling523bea82013-11-08 08:13:15 +0000492 return false;
Duncan P. N. Exon Smith176b6912014-10-03 20:01:09 +0000493 if (!fieldIsTypeRef(DbgNode, 5))
Bill Wendling523bea82013-11-08 08:13:15 +0000494 return false;
495
Duncan P. N. Exon Smith176b6912014-10-03 20:01:09 +0000496 // Make sure the type identifier at field 7 is MDString, it can be null.
497 if (!fieldIsMDString(DbgNode, 7))
Bill Wendling523bea82013-11-08 08:13:15 +0000498 return false;
499
Adrian Prantl99c7af22013-12-18 21:48:19 +0000500 // A subroutine type can't be both & and &&.
501 if (isLValueReference() && isRValueReference())
502 return false;
503
Duncan P. N. Exon Smith176b6912014-10-03 20:01:09 +0000504 return DbgNode->getNumOperands() == 8 && getNumHeaderFields() == 8;
Bill Wendling523bea82013-11-08 08:13:15 +0000505}
506
Bill Wendling523bea82013-11-08 08:13:15 +0000507bool DISubprogram::Verify() const {
508 if (!isSubprogram())
509 return false;
510
Duncan P. N. Exon Smith176b6912014-10-03 20:01:09 +0000511 // Make sure context @ field 2 is a ScopeRef and type @ field 3 is a MDNode.
Bill Wendling523bea82013-11-08 08:13:15 +0000512 if (!fieldIsScopeRef(DbgNode, 2))
513 return false;
Duncan P. N. Exon Smith176b6912014-10-03 20:01:09 +0000514 if (!fieldIsMDNode(DbgNode, 3))
Bill Wendling523bea82013-11-08 08:13:15 +0000515 return false;
Duncan P. N. Exon Smith176b6912014-10-03 20:01:09 +0000516 // Containing type @ field 4.
517 if (!fieldIsTypeRef(DbgNode, 4))
Bill Wendling523bea82013-11-08 08:13:15 +0000518 return false;
Adrian Prantl99c7af22013-12-18 21:48:19 +0000519
520 // A subprogram can't be both & and &&.
521 if (isLValueReference() && isRValueReference())
522 return false;
523
David Blaikie3dfe4782014-10-14 18:22:52 +0000524 // If a DISubprogram has an llvm::Function*, then scope chains from all
525 // instructions within the function should lead to this DISubprogram.
526 if (auto *F = getFunction()) {
David Blaikie3dfe4782014-10-14 18:22:52 +0000527 for (auto &BB : *F) {
528 for (auto &I : BB) {
529 DebugLoc DL = I.getDebugLoc();
530 if (DL.isUnknown())
531 continue;
532
533 MDNode *Scope = nullptr;
534 MDNode *IA = nullptr;
535 // walk the inlined-at scopes
Duncan P. N. Exon Smith5bf8fef2014-12-09 18:38:53 +0000536 while ((IA = DL.getInlinedAt()))
David Blaikie3dfe4782014-10-14 18:22:52 +0000537 DL = DebugLoc::getFromDILocation(IA);
Duncan P. N. Exon Smith5bf8fef2014-12-09 18:38:53 +0000538 DL.getScopeAndInlinedAt(Scope, IA);
Adrian Prantlde200df2015-01-20 22:37:25 +0000539 if (!Scope)
540 return false;
David Blaikie3dfe4782014-10-14 18:22:52 +0000541 assert(!IA);
542 while (!DIDescriptor(Scope).isSubprogram()) {
543 DILexicalBlockFile D(Scope);
544 Scope = D.isLexicalBlockFile()
545 ? D.getScope()
Duncan P. N. Exon Smith5bf8fef2014-12-09 18:38:53 +0000546 : DebugLoc::getFromDILexicalBlock(Scope).getScope();
Adrian Prantl1292e242015-01-21 18:32:56 +0000547 if (!Scope)
548 return false;
David Blaikie3dfe4782014-10-14 18:22:52 +0000549 }
550 if (!DISubprogram(Scope).describes(F))
551 return false;
552 }
553 }
554 }
Duncan P. N. Exon Smith176b6912014-10-03 20:01:09 +0000555 return DbgNode->getNumOperands() == 9 && getNumHeaderFields() == 12;
Bill Wendling523bea82013-11-08 08:13:15 +0000556}
557
Bill Wendling523bea82013-11-08 08:13:15 +0000558bool DIGlobalVariable::Verify() const {
559 if (!isGlobalVariable())
560 return false;
561
562 if (getDisplayName().empty())
563 return false;
Manman Renf0a582b2014-11-21 19:55:23 +0000564 // Make sure context @ field 1 is an MDNode.
565 if (!fieldIsMDNode(DbgNode, 1))
Bill Wendling523bea82013-11-08 08:13:15 +0000566 return false;
Duncan P. N. Exon Smith176b6912014-10-03 20:01:09 +0000567 // Make sure that type @ field 3 is a DITypeRef.
568 if (!fieldIsTypeRef(DbgNode, 3))
Bill Wendling523bea82013-11-08 08:13:15 +0000569 return false;
Duncan P. N. Exon Smith176b6912014-10-03 20:01:09 +0000570 // Make sure StaticDataMemberDeclaration @ field 5 is MDNode.
571 if (!fieldIsMDNode(DbgNode, 5))
Bill Wendling523bea82013-11-08 08:13:15 +0000572 return false;
573
Duncan P. N. Exon Smith176b6912014-10-03 20:01:09 +0000574 return DbgNode->getNumOperands() == 6 && getNumHeaderFields() == 7;
Bill Wendling523bea82013-11-08 08:13:15 +0000575}
576
Bill Wendling523bea82013-11-08 08:13:15 +0000577bool DIVariable::Verify() const {
578 if (!isVariable())
579 return false;
580
Adrian Prantl1a1647c2014-03-18 02:34:58 +0000581 // Make sure context @ field 1 is an MDNode.
Bill Wendling523bea82013-11-08 08:13:15 +0000582 if (!fieldIsMDNode(DbgNode, 1))
583 return false;
Duncan P. N. Exon Smith176b6912014-10-03 20:01:09 +0000584 // Make sure that type @ field 3 is a DITypeRef.
585 if (!fieldIsTypeRef(DbgNode, 3))
586 return false;
587
588 // Check the number of header fields, which is common between complex and
589 // simple variables.
590 if (getNumHeaderFields() != 4)
Bill Wendling523bea82013-11-08 08:13:15 +0000591 return false;
Adrian Prantlda7d92e2014-06-30 17:17:35 +0000592
Adrian Prantl87b7eb92014-10-01 18:55:02 +0000593 // Variable without an inline location.
Duncan P. N. Exon Smith176b6912014-10-03 20:01:09 +0000594 if (DbgNode->getNumOperands() == 4)
Adrian Prantlda7d92e2014-06-30 17:17:35 +0000595 return true;
596
Duncan P. N. Exon Smith176b6912014-10-03 20:01:09 +0000597 // Variable with an inline location.
598 return getInlinedAt() != nullptr && DbgNode->getNumOperands() == 5;
Adrian Prantl87b7eb92014-10-01 18:55:02 +0000599}
600
Adrian Prantl87b7eb92014-10-01 18:55:02 +0000601bool DIExpression::Verify() const {
602 // Empty DIExpressions may be represented as a nullptr.
603 if (!DbgNode)
604 return true;
605
Adrian Prantl9260cca2015-01-22 00:00:52 +0000606 if (!(isExpression() && DbgNode->getNumOperands() == 1))
607 return false;
608
609 for (auto E = end(), I = begin(); I != E; ++I)
610 switch (*I) {
Adrian Prantl34bcbee2015-01-21 00:59:20 +0000611 case DW_OP_piece:
Adrian Prantl9260cca2015-01-22 00:00:52 +0000612 // Must be the last element of the expression.
613 return std::distance(I.getBase(), DIHeaderFieldIterator()) == 3;
Adrian Prantl34bcbee2015-01-21 00:59:20 +0000614 case DW_OP_plus:
Adrian Prantl9260cca2015-01-22 00:00:52 +0000615 if (std::distance(I.getBase(), DIHeaderFieldIterator()) < 2)
Adrian Prantl34bcbee2015-01-21 00:59:20 +0000616 return false;
Adrian Prantl34bcbee2015-01-21 00:59:20 +0000617 break;
Adrian Prantl9260cca2015-01-22 00:00:52 +0000618 case DW_OP_deref:
619 break;
620 default:
621 // Other operators are not yet supported by the backend.
622 return false;
623 }
624 return true;
Bill Wendling523bea82013-11-08 08:13:15 +0000625}
626
Bill Wendling523bea82013-11-08 08:13:15 +0000627bool DILocation::Verify() const {
Duncan P. N. Exon Smith98854692015-01-14 22:27:36 +0000628 return DbgNode && isa<MDLocation>(DbgNode);
Bill Wendling523bea82013-11-08 08:13:15 +0000629}
630
Bill Wendling523bea82013-11-08 08:13:15 +0000631bool DINameSpace::Verify() const {
632 if (!isNameSpace())
633 return false;
Duncan P. N. Exon Smith176b6912014-10-03 20:01:09 +0000634 return DbgNode->getNumOperands() == 3 && getNumHeaderFields() == 3;
Bill Wendling523bea82013-11-08 08:13:15 +0000635}
636
Bill Wendling523bea82013-11-08 08:13:15 +0000637MDNode *DIFile::getFileNode() const { return getNodeField(DbgNode, 1); }
638
Bill Wendling523bea82013-11-08 08:13:15 +0000639bool DIFile::Verify() const {
640 return isFile() && DbgNode->getNumOperands() == 2;
641}
642
Bill Wendling523bea82013-11-08 08:13:15 +0000643bool DIEnumerator::Verify() const {
Duncan P. N. Exon Smith176b6912014-10-03 20:01:09 +0000644 return isEnumerator() && DbgNode->getNumOperands() == 1 &&
645 getNumHeaderFields() == 3;
Bill Wendling523bea82013-11-08 08:13:15 +0000646}
647
Bill Wendling523bea82013-11-08 08:13:15 +0000648bool DISubrange::Verify() const {
Duncan P. N. Exon Smith176b6912014-10-03 20:01:09 +0000649 return isSubrange() && DbgNode->getNumOperands() == 1 &&
650 getNumHeaderFields() == 3;
Bill Wendling523bea82013-11-08 08:13:15 +0000651}
652
Bill Wendling523bea82013-11-08 08:13:15 +0000653bool DILexicalBlock::Verify() const {
Duncan P. N. Exon Smith176b6912014-10-03 20:01:09 +0000654 return isLexicalBlock() && DbgNode->getNumOperands() == 3 &&
655 getNumHeaderFields() == 4;
Bill Wendling523bea82013-11-08 08:13:15 +0000656}
657
Bill Wendling523bea82013-11-08 08:13:15 +0000658bool DILexicalBlockFile::Verify() const {
Duncan P. N. Exon Smith176b6912014-10-03 20:01:09 +0000659 return isLexicalBlockFile() && DbgNode->getNumOperands() == 3 &&
660 getNumHeaderFields() == 2;
Bill Wendling523bea82013-11-08 08:13:15 +0000661}
662
Bill Wendling523bea82013-11-08 08:13:15 +0000663bool DITemplateTypeParameter::Verify() const {
Duncan P. N. Exon Smith176b6912014-10-03 20:01:09 +0000664 return isTemplateTypeParameter() && DbgNode->getNumOperands() == 4 &&
665 getNumHeaderFields() == 4;
Bill Wendling523bea82013-11-08 08:13:15 +0000666}
667
Bill Wendling523bea82013-11-08 08:13:15 +0000668bool DITemplateValueParameter::Verify() const {
Duncan P. N. Exon Smith176b6912014-10-03 20:01:09 +0000669 return isTemplateValueParameter() && DbgNode->getNumOperands() == 5 &&
670 getNumHeaderFields() == 4;
Bill Wendling523bea82013-11-08 08:13:15 +0000671}
672
Bill Wendling523bea82013-11-08 08:13:15 +0000673bool DIImportedEntity::Verify() const {
Duncan P. N. Exon Smith176b6912014-10-03 20:01:09 +0000674 return isImportedEntity() && DbgNode->getNumOperands() == 3 &&
675 getNumHeaderFields() == 3;
Bill Wendling523bea82013-11-08 08:13:15 +0000676}
677
Bill Wendling523bea82013-11-08 08:13:15 +0000678MDNode *DIDerivedType::getObjCProperty() const {
Duncan P. N. Exon Smith176b6912014-10-03 20:01:09 +0000679 return getNodeField(DbgNode, 4);
Bill Wendling523bea82013-11-08 08:13:15 +0000680}
681
682MDString *DICompositeType::getIdentifier() const {
Duncan P. N. Exon Smith176b6912014-10-03 20:01:09 +0000683 return cast_or_null<MDString>(getField(DbgNode, 7));
Bill Wendling523bea82013-11-08 08:13:15 +0000684}
685
686#ifndef NDEBUG
687static void VerifySubsetOf(const MDNode *LHS, const MDNode *RHS) {
688 for (unsigned i = 0; i != LHS->getNumOperands(); ++i) {
689 // Skip the 'empty' list (that's a single i32 0, rather than truly empty).
Duncan P. N. Exon Smith5bf8fef2014-12-09 18:38:53 +0000690 if (i == 0 && mdconst::hasa<ConstantInt>(LHS->getOperand(i)))
Bill Wendling523bea82013-11-08 08:13:15 +0000691 continue;
692 const MDNode *E = cast<MDNode>(LHS->getOperand(i));
693 bool found = false;
694 for (unsigned j = 0; !found && j != RHS->getNumOperands(); ++j)
Hans Wennborge242e8b2014-12-09 20:39:15 +0000695 found = (E == cast<MDNode>(RHS->getOperand(j)));
Bill Wendling523bea82013-11-08 08:13:15 +0000696 assert(found && "Losing a member during member list replacement");
697 }
698}
699#endif
700
Manman Ren1a125c92014-07-28 19:33:20 +0000701void DICompositeType::setArraysHelper(MDNode *Elements, MDNode *TParams) {
Duncan P. N. Exon Smith5bf8fef2014-12-09 18:38:53 +0000702 TrackingMDNodeRef N(*this);
Bill Wendling523bea82013-11-08 08:13:15 +0000703 if (Elements) {
704#ifndef NDEBUG
705 // Check that the new list of members contains all the old members as well.
Duncan P. N. Exon Smith176b6912014-10-03 20:01:09 +0000706 if (const MDNode *El = cast_or_null<MDNode>(N->getOperand(4)))
Bill Wendling523bea82013-11-08 08:13:15 +0000707 VerifySubsetOf(El, Elements);
708#endif
Duncan P. N. Exon Smith176b6912014-10-03 20:01:09 +0000709 N->replaceOperandWith(4, Elements);
Bill Wendling523bea82013-11-08 08:13:15 +0000710 }
711 if (TParams)
Duncan P. N. Exon Smith176b6912014-10-03 20:01:09 +0000712 N->replaceOperandWith(6, TParams);
Bill Wendling523bea82013-11-08 08:13:15 +0000713 DbgNode = N;
714}
715
Bill Wendling523bea82013-11-08 08:13:15 +0000716DIScopeRef DIScope::getRef() const {
717 if (!isCompositeType())
718 return DIScopeRef(*this);
719 DICompositeType DTy(DbgNode);
720 if (!DTy.getIdentifier())
721 return DIScopeRef(*this);
722 return DIScopeRef(DTy.getIdentifier());
723}
724
Bill Wendling523bea82013-11-08 08:13:15 +0000725void DICompositeType::setContainingType(DICompositeType ContainingType) {
Duncan P. N. Exon Smith5bf8fef2014-12-09 18:38:53 +0000726 TrackingMDNodeRef N(*this);
Duncan P. N. Exon Smith176b6912014-10-03 20:01:09 +0000727 N->replaceOperandWith(5, ContainingType.getRef());
Bill Wendling523bea82013-11-08 08:13:15 +0000728 DbgNode = N;
729}
730
Bill Wendling523bea82013-11-08 08:13:15 +0000731bool DIVariable::isInlinedFnArgument(const Function *CurFn) {
732 assert(CurFn && "Invalid function");
733 if (!getContext().isSubprogram())
734 return false;
735 // This variable is not inlined function argument if its scope
736 // does not describe current function.
737 return !DISubprogram(getContext()).describes(CurFn);
738}
739
Bill Wendling523bea82013-11-08 08:13:15 +0000740bool DISubprogram::describes(const Function *F) {
741 assert(F && "Invalid function");
742 if (F == getFunction())
743 return true;
744 StringRef Name = getLinkageName();
745 if (Name.empty())
746 Name = getName();
747 if (F->getName() == Name)
748 return true;
749 return false;
750}
751
Bill Wendling523bea82013-11-08 08:13:15 +0000752MDNode *DISubprogram::getVariablesNodes() const {
Duncan P. N. Exon Smith176b6912014-10-03 20:01:09 +0000753 return getNodeField(DbgNode, 8);
Bill Wendling523bea82013-11-08 08:13:15 +0000754}
755
756DIArray DISubprogram::getVariables() const {
Duncan P. N. Exon Smith176b6912014-10-03 20:01:09 +0000757 return DIArray(getNodeField(DbgNode, 8));
Bill Wendling523bea82013-11-08 08:13:15 +0000758}
759
Duncan P. N. Exon Smith5bf8fef2014-12-09 18:38:53 +0000760Metadata *DITemplateValueParameter::getValue() const {
761 return DbgNode->getOperand(3);
Bill Wendling523bea82013-11-08 08:13:15 +0000762}
763
Bill Wendling523bea82013-11-08 08:13:15 +0000764DIScopeRef DIScope::getContext() const {
765
766 if (isType())
767 return DIType(DbgNode).getContext();
768
769 if (isSubprogram())
770 return DIScopeRef(DISubprogram(DbgNode).getContext());
771
772 if (isLexicalBlock())
773 return DIScopeRef(DILexicalBlock(DbgNode).getContext());
774
775 if (isLexicalBlockFile())
776 return DIScopeRef(DILexicalBlockFile(DbgNode).getContext());
777
778 if (isNameSpace())
779 return DIScopeRef(DINameSpace(DbgNode).getContext());
780
781 assert((isFile() || isCompileUnit()) && "Unhandled type of scope.");
Craig Topperc6207612014-04-09 06:08:46 +0000782 return DIScopeRef(nullptr);
Bill Wendling523bea82013-11-08 08:13:15 +0000783}
784
Bill Wendling523bea82013-11-08 08:13:15 +0000785StringRef DIScope::getName() const {
786 if (isType())
787 return DIType(DbgNode).getName();
788 if (isSubprogram())
789 return DISubprogram(DbgNode).getName();
790 if (isNameSpace())
791 return DINameSpace(DbgNode).getName();
792 assert((isLexicalBlock() || isLexicalBlockFile() || isFile() ||
793 isCompileUnit()) &&
794 "Unhandled type of scope.");
795 return StringRef();
796}
797
798StringRef DIScope::getFilename() const {
799 if (!DbgNode)
800 return StringRef();
801 return ::getStringField(getNodeField(DbgNode, 1), 0);
802}
803
804StringRef DIScope::getDirectory() const {
805 if (!DbgNode)
806 return StringRef();
807 return ::getStringField(getNodeField(DbgNode, 1), 1);
808}
809
810DIArray DICompileUnit::getEnumTypes() const {
Duncan P. N. Exon Smith176b6912014-10-03 20:01:09 +0000811 if (!DbgNode || DbgNode->getNumOperands() < 7)
Bill Wendling523bea82013-11-08 08:13:15 +0000812 return DIArray();
813
Duncan P. N. Exon Smith176b6912014-10-03 20:01:09 +0000814 return DIArray(getNodeField(DbgNode, 2));
Bill Wendling523bea82013-11-08 08:13:15 +0000815}
816
817DIArray DICompileUnit::getRetainedTypes() const {
Duncan P. N. Exon Smith176b6912014-10-03 20:01:09 +0000818 if (!DbgNode || DbgNode->getNumOperands() < 7)
Bill Wendling523bea82013-11-08 08:13:15 +0000819 return DIArray();
820
Duncan P. N. Exon Smith176b6912014-10-03 20:01:09 +0000821 return DIArray(getNodeField(DbgNode, 3));
Bill Wendling523bea82013-11-08 08:13:15 +0000822}
823
824DIArray DICompileUnit::getSubprograms() const {
Duncan P. N. Exon Smith176b6912014-10-03 20:01:09 +0000825 if (!DbgNode || DbgNode->getNumOperands() < 7)
Bill Wendling523bea82013-11-08 08:13:15 +0000826 return DIArray();
827
Duncan P. N. Exon Smith176b6912014-10-03 20:01:09 +0000828 return DIArray(getNodeField(DbgNode, 4));
Bill Wendling523bea82013-11-08 08:13:15 +0000829}
830
831DIArray DICompileUnit::getGlobalVariables() const {
Duncan P. N. Exon Smith176b6912014-10-03 20:01:09 +0000832 if (!DbgNode || DbgNode->getNumOperands() < 7)
Bill Wendling523bea82013-11-08 08:13:15 +0000833 return DIArray();
834
Duncan P. N. Exon Smith176b6912014-10-03 20:01:09 +0000835 return DIArray(getNodeField(DbgNode, 5));
Bill Wendling523bea82013-11-08 08:13:15 +0000836}
837
838DIArray DICompileUnit::getImportedEntities() const {
Duncan P. N. Exon Smith176b6912014-10-03 20:01:09 +0000839 if (!DbgNode || DbgNode->getNumOperands() < 7)
Bill Wendling523bea82013-11-08 08:13:15 +0000840 return DIArray();
841
Duncan P. N. Exon Smith176b6912014-10-03 20:01:09 +0000842 return DIArray(getNodeField(DbgNode, 6));
843}
844
845void DICompileUnit::replaceSubprograms(DIArray Subprograms) {
846 assert(Verify() && "Expected compile unit");
847 if (Subprograms == getSubprograms())
848 return;
849
850 const_cast<MDNode *>(DbgNode)->replaceOperandWith(4, Subprograms);
851}
852
853void DICompileUnit::replaceGlobalVariables(DIArray GlobalVariables) {
854 assert(Verify() && "Expected compile unit");
855 if (GlobalVariables == getGlobalVariables())
856 return;
857
858 const_cast<MDNode *>(DbgNode)->replaceOperandWith(5, GlobalVariables);
Bill Wendling523bea82013-11-08 08:13:15 +0000859}
860
Diego Novillof5041ce2014-03-03 20:06:11 +0000861DILocation DILocation::copyWithNewScope(LLVMContext &Ctx,
David Blaikie2f3f76f2014-08-21 22:45:21 +0000862 DILexicalBlockFile NewScope) {
Diego Novillof5041ce2014-03-03 20:06:11 +0000863 assert(Verify());
Duncan P. N. Exon Smith98854692015-01-14 22:27:36 +0000864 assert(NewScope && "Expected valid scope");
865
866 const auto *Old = cast<MDLocation>(DbgNode);
867 return DILocation(MDLocation::get(Ctx, Old->getLine(), Old->getColumn(),
868 NewScope, Old->getInlinedAt()));
Diego Novillof5041ce2014-03-03 20:06:11 +0000869}
870
Diego Novillof5041ce2014-03-03 20:06:11 +0000871unsigned DILocation::computeNewDiscriminator(LLVMContext &Ctx) {
872 std::pair<const char *, unsigned> Key(getFilename().data(), getLineNumber());
873 return ++Ctx.pImpl->DiscriminatorTable[Key];
874}
875
Bill Wendling523bea82013-11-08 08:13:15 +0000876DIVariable llvm::createInlinedVariable(MDNode *DV, MDNode *InlinedScope,
877 LLVMContext &VMContext) {
Duncan P. N. Exon Smith176b6912014-10-03 20:01:09 +0000878 assert(DIVariable(DV).Verify() && "Expected a DIVariable");
879 if (!InlinedScope)
880 return cleanseInlinedVariable(DV, VMContext);
881
882 // Insert inlined scope.
Duncan P. N. Exon Smith5bf8fef2014-12-09 18:38:53 +0000883 SmallVector<Metadata *, 8> Elts;
Duncan P. N. Exon Smith176b6912014-10-03 20:01:09 +0000884 for (unsigned I = 0, E = DIVariableInlinedAtIndex; I != E; ++I)
885 Elts.push_back(DV->getOperand(I));
886 Elts.push_back(InlinedScope);
887
888 DIVariable Inlined(MDNode::get(VMContext, Elts));
889 assert(Inlined.Verify() && "Expected to create a DIVariable");
890 return Inlined;
Bill Wendling523bea82013-11-08 08:13:15 +0000891}
892
Bill Wendling523bea82013-11-08 08:13:15 +0000893DIVariable llvm::cleanseInlinedVariable(MDNode *DV, LLVMContext &VMContext) {
Duncan P. N. Exon Smith176b6912014-10-03 20:01:09 +0000894 assert(DIVariable(DV).Verify() && "Expected a DIVariable");
895 if (!DIVariable(DV).getInlinedAt())
896 return DIVariable(DV);
897
898 // Remove inlined scope.
Duncan P. N. Exon Smith5bf8fef2014-12-09 18:38:53 +0000899 SmallVector<Metadata *, 8> Elts;
Duncan P. N. Exon Smith176b6912014-10-03 20:01:09 +0000900 for (unsigned I = 0, E = DIVariableInlinedAtIndex; I != E; ++I)
901 Elts.push_back(DV->getOperand(I));
902
903 DIVariable Cleansed(MDNode::get(VMContext, Elts));
904 assert(Cleansed.Verify() && "Expected to create a DIVariable");
905 return Cleansed;
Bill Wendling523bea82013-11-08 08:13:15 +0000906}
907
Bill Wendling523bea82013-11-08 08:13:15 +0000908DISubprogram llvm::getDISubprogram(const MDNode *Scope) {
909 DIDescriptor D(Scope);
910 if (D.isSubprogram())
911 return DISubprogram(Scope);
912
913 if (D.isLexicalBlockFile())
914 return getDISubprogram(DILexicalBlockFile(Scope).getContext());
915
916 if (D.isLexicalBlock())
917 return getDISubprogram(DILexicalBlock(Scope).getContext());
918
919 return DISubprogram();
920}
921
Timur Iskhodzhanoveb229ca2014-10-23 23:46:28 +0000922DISubprogram llvm::getDISubprogram(const Function *F) {
923 // We look for the first instr that has a debug annotation leading back to F.
Timur Iskhodzhanoveb229ca2014-10-23 23:46:28 +0000924 for (auto &BB : *F) {
David Majnemerc758df42014-11-01 07:57:14 +0000925 auto Inst = std::find_if(BB.begin(), BB.end(), [](const Instruction &Inst) {
926 return !Inst.getDebugLoc().isUnknown();
927 });
928 if (Inst == BB.end())
929 continue;
930 DebugLoc DLoc = Inst->getDebugLoc();
Duncan P. N. Exon Smith5bf8fef2014-12-09 18:38:53 +0000931 const MDNode *Scope = DLoc.getScopeNode();
David Majnemerc758df42014-11-01 07:57:14 +0000932 DISubprogram Subprogram = getDISubprogram(Scope);
933 return Subprogram.describes(F) ? Subprogram : DISubprogram();
Timur Iskhodzhanoveb229ca2014-10-23 23:46:28 +0000934 }
935
936 return DISubprogram();
937}
938
Bill Wendling523bea82013-11-08 08:13:15 +0000939DICompositeType llvm::getDICompositeType(DIType T) {
940 if (T.isCompositeType())
941 return DICompositeType(T);
942
943 if (T.isDerivedType()) {
944 // This function is currently used by dragonegg and dragonegg does
945 // not generate identifier for types, so using an empty map to resolve
946 // DerivedFrom should be fine.
947 DITypeIdentifierMap EmptyMap;
948 return getDICompositeType(
949 DIDerivedType(T).getTypeDerivedFrom().resolve(EmptyMap));
950 }
951
952 return DICompositeType();
953}
954
Bill Wendling523bea82013-11-08 08:13:15 +0000955DITypeIdentifierMap
956llvm::generateDITypeIdentifierMap(const NamedMDNode *CU_Nodes) {
957 DITypeIdentifierMap Map;
958 for (unsigned CUi = 0, CUe = CU_Nodes->getNumOperands(); CUi != CUe; ++CUi) {
Duncan P. N. Exon Smithde36e802014-11-11 21:30:22 +0000959 DICompileUnit CU(CU_Nodes->getOperand(CUi));
Bill Wendling523bea82013-11-08 08:13:15 +0000960 DIArray Retain = CU.getRetainedTypes();
961 for (unsigned Ti = 0, Te = Retain.getNumElements(); Ti != Te; ++Ti) {
962 if (!Retain.getElement(Ti).isCompositeType())
963 continue;
964 DICompositeType Ty(Retain.getElement(Ti));
965 if (MDString *TypeId = Ty.getIdentifier()) {
966 // Definition has priority over declaration.
967 // Try to insert (TypeId, Ty) to Map.
968 std::pair<DITypeIdentifierMap::iterator, bool> P =
969 Map.insert(std::make_pair(TypeId, Ty));
970 // If TypeId already exists in Map and this is a definition, replace
971 // whatever we had (declaration or definition) with the definition.
972 if (!P.second && !Ty.isForwardDecl())
973 P.first->second = Ty;
974 }
975 }
976 }
977 return Map;
978}
979
980//===----------------------------------------------------------------------===//
981// DebugInfoFinder implementations.
982//===----------------------------------------------------------------------===//
983
984void DebugInfoFinder::reset() {
985 CUs.clear();
986 SPs.clear();
987 GVs.clear();
988 TYs.clear();
989 Scopes.clear();
990 NodesSeen.clear();
991 TypeIdentifierMap.clear();
Manman Ren2085ccc2013-11-17 18:42:37 +0000992 TypeMapInitialized = false;
993}
994
Manman Renb46e5502013-11-17 19:35:03 +0000995void DebugInfoFinder::InitializeTypeMap(const Module &M) {
Manman Ren2085ccc2013-11-17 18:42:37 +0000996 if (!TypeMapInitialized)
997 if (NamedMDNode *CU_Nodes = M.getNamedMetadata("llvm.dbg.cu")) {
998 TypeIdentifierMap = generateDITypeIdentifierMap(CU_Nodes);
999 TypeMapInitialized = true;
1000 }
Bill Wendling523bea82013-11-08 08:13:15 +00001001}
1002
Bill Wendling523bea82013-11-08 08:13:15 +00001003void DebugInfoFinder::processModule(const Module &M) {
Manman Renb46e5502013-11-17 19:35:03 +00001004 InitializeTypeMap(M);
Bill Wendling523bea82013-11-08 08:13:15 +00001005 if (NamedMDNode *CU_Nodes = M.getNamedMetadata("llvm.dbg.cu")) {
Bill Wendling523bea82013-11-08 08:13:15 +00001006 for (unsigned i = 0, e = CU_Nodes->getNumOperands(); i != e; ++i) {
Duncan P. N. Exon Smithde36e802014-11-11 21:30:22 +00001007 DICompileUnit CU(CU_Nodes->getOperand(i));
Bill Wendling523bea82013-11-08 08:13:15 +00001008 addCompileUnit(CU);
1009 DIArray GVs = CU.getGlobalVariables();
1010 for (unsigned i = 0, e = GVs.getNumElements(); i != e; ++i) {
1011 DIGlobalVariable DIG(GVs.getElement(i));
1012 if (addGlobalVariable(DIG)) {
Manman Renf0a582b2014-11-21 19:55:23 +00001013 processScope(DIG.getContext());
Adrian Prantl1a1647c2014-03-18 02:34:58 +00001014 processType(DIG.getType().resolve(TypeIdentifierMap));
Bill Wendling523bea82013-11-08 08:13:15 +00001015 }
1016 }
1017 DIArray SPs = CU.getSubprograms();
1018 for (unsigned i = 0, e = SPs.getNumElements(); i != e; ++i)
1019 processSubprogram(DISubprogram(SPs.getElement(i)));
1020 DIArray EnumTypes = CU.getEnumTypes();
1021 for (unsigned i = 0, e = EnumTypes.getNumElements(); i != e; ++i)
1022 processType(DIType(EnumTypes.getElement(i)));
1023 DIArray RetainedTypes = CU.getRetainedTypes();
1024 for (unsigned i = 0, e = RetainedTypes.getNumElements(); i != e; ++i)
1025 processType(DIType(RetainedTypes.getElement(i)));
1026 DIArray Imports = CU.getImportedEntities();
1027 for (unsigned i = 0, e = Imports.getNumElements(); i != e; ++i) {
1028 DIImportedEntity Import = DIImportedEntity(Imports.getElement(i));
Adrian Prantld09ba232014-04-01 03:41:04 +00001029 DIDescriptor Entity = Import.getEntity().resolve(TypeIdentifierMap);
Bill Wendling523bea82013-11-08 08:13:15 +00001030 if (Entity.isType())
1031 processType(DIType(Entity));
1032 else if (Entity.isSubprogram())
1033 processSubprogram(DISubprogram(Entity));
1034 else if (Entity.isNameSpace())
1035 processScope(DINameSpace(Entity).getContext());
1036 }
1037 }
1038 }
1039}
1040
Manman Ren2085ccc2013-11-17 18:42:37 +00001041void DebugInfoFinder::processLocation(const Module &M, DILocation Loc) {
Bill Wendling523bea82013-11-08 08:13:15 +00001042 if (!Loc)
1043 return;
Manman Renb46e5502013-11-17 19:35:03 +00001044 InitializeTypeMap(M);
Bill Wendling523bea82013-11-08 08:13:15 +00001045 processScope(Loc.getScope());
Manman Ren2085ccc2013-11-17 18:42:37 +00001046 processLocation(M, Loc.getOrigLocation());
Bill Wendling523bea82013-11-08 08:13:15 +00001047}
1048
Bill Wendling523bea82013-11-08 08:13:15 +00001049void DebugInfoFinder::processType(DIType DT) {
1050 if (!addType(DT))
1051 return;
1052 processScope(DT.getContext().resolve(TypeIdentifierMap));
1053 if (DT.isCompositeType()) {
1054 DICompositeType DCT(DT);
1055 processType(DCT.getTypeDerivedFrom().resolve(TypeIdentifierMap));
Manman Renf8a19672014-07-28 22:24:06 +00001056 if (DT.isSubroutineType()) {
1057 DITypeArray DTA = DISubroutineType(DT).getTypeArray();
1058 for (unsigned i = 0, e = DTA.getNumElements(); i != e; ++i)
1059 processType(DTA.getElement(i).resolve(TypeIdentifierMap));
1060 return;
1061 }
Manman Renab8ffba2014-07-28 19:14:13 +00001062 DIArray DA = DCT.getElements();
Bill Wendling523bea82013-11-08 08:13:15 +00001063 for (unsigned i = 0, e = DA.getNumElements(); i != e; ++i) {
1064 DIDescriptor D = DA.getElement(i);
1065 if (D.isType())
1066 processType(DIType(D));
1067 else if (D.isSubprogram())
1068 processSubprogram(DISubprogram(D));
1069 }
1070 } else if (DT.isDerivedType()) {
1071 DIDerivedType DDT(DT);
1072 processType(DDT.getTypeDerivedFrom().resolve(TypeIdentifierMap));
1073 }
1074}
1075
1076void DebugInfoFinder::processScope(DIScope Scope) {
1077 if (Scope.isType()) {
1078 DIType Ty(Scope);
1079 processType(Ty);
1080 return;
1081 }
1082 if (Scope.isCompileUnit()) {
1083 addCompileUnit(DICompileUnit(Scope));
1084 return;
1085 }
1086 if (Scope.isSubprogram()) {
1087 processSubprogram(DISubprogram(Scope));
1088 return;
1089 }
1090 if (!addScope(Scope))
1091 return;
1092 if (Scope.isLexicalBlock()) {
1093 DILexicalBlock LB(Scope);
1094 processScope(LB.getContext());
1095 } else if (Scope.isLexicalBlockFile()) {
1096 DILexicalBlockFile LBF = DILexicalBlockFile(Scope);
1097 processScope(LBF.getScope());
1098 } else if (Scope.isNameSpace()) {
1099 DINameSpace NS(Scope);
1100 processScope(NS.getContext());
1101 }
1102}
1103
Bill Wendling523bea82013-11-08 08:13:15 +00001104void DebugInfoFinder::processSubprogram(DISubprogram SP) {
1105 if (!addSubprogram(SP))
1106 return;
1107 processScope(SP.getContext().resolve(TypeIdentifierMap));
1108 processType(SP.getType());
1109 DIArray TParams = SP.getTemplateParams();
1110 for (unsigned I = 0, E = TParams.getNumElements(); I != E; ++I) {
1111 DIDescriptor Element = TParams.getElement(I);
1112 if (Element.isTemplateTypeParameter()) {
1113 DITemplateTypeParameter TType(Element);
1114 processScope(TType.getContext().resolve(TypeIdentifierMap));
1115 processType(TType.getType().resolve(TypeIdentifierMap));
1116 } else if (Element.isTemplateValueParameter()) {
1117 DITemplateValueParameter TVal(Element);
1118 processScope(TVal.getContext().resolve(TypeIdentifierMap));
1119 processType(TVal.getType().resolve(TypeIdentifierMap));
1120 }
1121 }
1122}
1123
Manman Ren2085ccc2013-11-17 18:42:37 +00001124void DebugInfoFinder::processDeclare(const Module &M,
1125 const DbgDeclareInst *DDI) {
Bill Wendling523bea82013-11-08 08:13:15 +00001126 MDNode *N = dyn_cast<MDNode>(DDI->getVariable());
1127 if (!N)
1128 return;
Manman Renb46e5502013-11-17 19:35:03 +00001129 InitializeTypeMap(M);
Bill Wendling523bea82013-11-08 08:13:15 +00001130
1131 DIDescriptor DV(N);
1132 if (!DV.isVariable())
1133 return;
1134
David Blaikie70573dc2014-11-19 07:49:26 +00001135 if (!NodesSeen.insert(DV).second)
Bill Wendling523bea82013-11-08 08:13:15 +00001136 return;
1137 processScope(DIVariable(N).getContext());
Adrian Prantl1a1647c2014-03-18 02:34:58 +00001138 processType(DIVariable(N).getType().resolve(TypeIdentifierMap));
Bill Wendling523bea82013-11-08 08:13:15 +00001139}
1140
Manman Ren2085ccc2013-11-17 18:42:37 +00001141void DebugInfoFinder::processValue(const Module &M, const DbgValueInst *DVI) {
Bill Wendling523bea82013-11-08 08:13:15 +00001142 MDNode *N = dyn_cast<MDNode>(DVI->getVariable());
1143 if (!N)
1144 return;
Manman Renb46e5502013-11-17 19:35:03 +00001145 InitializeTypeMap(M);
Bill Wendling523bea82013-11-08 08:13:15 +00001146
1147 DIDescriptor DV(N);
1148 if (!DV.isVariable())
1149 return;
1150
David Blaikie70573dc2014-11-19 07:49:26 +00001151 if (!NodesSeen.insert(DV).second)
Bill Wendling523bea82013-11-08 08:13:15 +00001152 return;
1153 processScope(DIVariable(N).getContext());
Adrian Prantl1a1647c2014-03-18 02:34:58 +00001154 processType(DIVariable(N).getType().resolve(TypeIdentifierMap));
Bill Wendling523bea82013-11-08 08:13:15 +00001155}
1156
Bill Wendling523bea82013-11-08 08:13:15 +00001157bool DebugInfoFinder::addType(DIType DT) {
1158 if (!DT)
1159 return false;
1160
David Blaikie70573dc2014-11-19 07:49:26 +00001161 if (!NodesSeen.insert(DT).second)
Bill Wendling523bea82013-11-08 08:13:15 +00001162 return false;
1163
1164 TYs.push_back(DT);
1165 return true;
1166}
1167
Bill Wendling523bea82013-11-08 08:13:15 +00001168bool DebugInfoFinder::addCompileUnit(DICompileUnit CU) {
1169 if (!CU)
1170 return false;
David Blaikie70573dc2014-11-19 07:49:26 +00001171 if (!NodesSeen.insert(CU).second)
Bill Wendling523bea82013-11-08 08:13:15 +00001172 return false;
1173
1174 CUs.push_back(CU);
1175 return true;
1176}
1177
Bill Wendling523bea82013-11-08 08:13:15 +00001178bool DebugInfoFinder::addGlobalVariable(DIGlobalVariable DIG) {
1179 if (!DIG)
1180 return false;
1181
David Blaikie70573dc2014-11-19 07:49:26 +00001182 if (!NodesSeen.insert(DIG).second)
Bill Wendling523bea82013-11-08 08:13:15 +00001183 return false;
1184
1185 GVs.push_back(DIG);
1186 return true;
1187}
1188
Bill Wendling523bea82013-11-08 08:13:15 +00001189bool DebugInfoFinder::addSubprogram(DISubprogram SP) {
1190 if (!SP)
1191 return false;
1192
David Blaikie70573dc2014-11-19 07:49:26 +00001193 if (!NodesSeen.insert(SP).second)
Bill Wendling523bea82013-11-08 08:13:15 +00001194 return false;
1195
1196 SPs.push_back(SP);
1197 return true;
1198}
1199
1200bool DebugInfoFinder::addScope(DIScope Scope) {
1201 if (!Scope)
1202 return false;
1203 // FIXME: Ocaml binding generates a scope with no content, we treat it
1204 // as null for now.
1205 if (Scope->getNumOperands() == 0)
1206 return false;
David Blaikie70573dc2014-11-19 07:49:26 +00001207 if (!NodesSeen.insert(Scope).second)
Bill Wendling523bea82013-11-08 08:13:15 +00001208 return false;
1209 Scopes.push_back(Scope);
1210 return true;
1211}
1212
1213//===----------------------------------------------------------------------===//
1214// DIDescriptor: dump routines for all descriptors.
1215//===----------------------------------------------------------------------===//
1216
Bill Wendling523bea82013-11-08 08:13:15 +00001217void DIDescriptor::dump() const {
1218 print(dbgs());
1219 dbgs() << '\n';
1220}
1221
Bill Wendling523bea82013-11-08 08:13:15 +00001222void DIDescriptor::print(raw_ostream &OS) const {
1223 if (!DbgNode)
1224 return;
1225
1226 if (const char *Tag = dwarf::TagString(getTag()))
1227 OS << "[ " << Tag << " ]";
1228
1229 if (this->isSubrange()) {
1230 DISubrange(DbgNode).printInternal(OS);
1231 } else if (this->isCompileUnit()) {
1232 DICompileUnit(DbgNode).printInternal(OS);
1233 } else if (this->isFile()) {
1234 DIFile(DbgNode).printInternal(OS);
1235 } else if (this->isEnumerator()) {
1236 DIEnumerator(DbgNode).printInternal(OS);
1237 } else if (this->isBasicType()) {
1238 DIType(DbgNode).printInternal(OS);
1239 } else if (this->isDerivedType()) {
1240 DIDerivedType(DbgNode).printInternal(OS);
1241 } else if (this->isCompositeType()) {
1242 DICompositeType(DbgNode).printInternal(OS);
1243 } else if (this->isSubprogram()) {
1244 DISubprogram(DbgNode).printInternal(OS);
1245 } else if (this->isGlobalVariable()) {
1246 DIGlobalVariable(DbgNode).printInternal(OS);
1247 } else if (this->isVariable()) {
1248 DIVariable(DbgNode).printInternal(OS);
1249 } else if (this->isObjCProperty()) {
1250 DIObjCProperty(DbgNode).printInternal(OS);
1251 } else if (this->isNameSpace()) {
1252 DINameSpace(DbgNode).printInternal(OS);
1253 } else if (this->isScope()) {
1254 DIScope(DbgNode).printInternal(OS);
Adrian Prantl87b7eb92014-10-01 18:55:02 +00001255 } else if (this->isExpression()) {
1256 DIExpression(DbgNode).printInternal(OS);
Bill Wendling523bea82013-11-08 08:13:15 +00001257 }
1258}
1259
1260void DISubrange::printInternal(raw_ostream &OS) const {
1261 int64_t Count = getCount();
1262 if (Count != -1)
1263 OS << " [" << getLo() << ", " << Count - 1 << ']';
1264 else
1265 OS << " [unbounded]";
1266}
1267
1268void DIScope::printInternal(raw_ostream &OS) const {
1269 OS << " [" << getDirectory() << "/" << getFilename() << ']';
1270}
1271
1272void DICompileUnit::printInternal(raw_ostream &OS) const {
1273 DIScope::printInternal(OS);
1274 OS << " [";
1275 unsigned Lang = getLanguage();
1276 if (const char *LangStr = dwarf::LanguageString(Lang))
1277 OS << LangStr;
1278 else
1279 (OS << "lang 0x").write_hex(Lang);
1280 OS << ']';
1281}
1282
1283void DIEnumerator::printInternal(raw_ostream &OS) const {
1284 OS << " [" << getName() << " :: " << getEnumValue() << ']';
1285}
1286
1287void DIType::printInternal(raw_ostream &OS) const {
Manman Renf93ac4b2014-07-29 18:20:39 +00001288 if (!DbgNode)
Bill Wendling523bea82013-11-08 08:13:15 +00001289 return;
1290
1291 StringRef Res = getName();
1292 if (!Res.empty())
1293 OS << " [" << Res << "]";
1294
1295 // TODO: Print context?
1296
1297 OS << " [line " << getLineNumber() << ", size " << getSizeInBits()
1298 << ", align " << getAlignInBits() << ", offset " << getOffsetInBits();
1299 if (isBasicType())
1300 if (const char *Enc =
1301 dwarf::AttributeEncodingString(DIBasicType(DbgNode).getEncoding()))
1302 OS << ", enc " << Enc;
1303 OS << "]";
1304
1305 if (isPrivate())
1306 OS << " [private]";
1307 else if (isProtected())
1308 OS << " [protected]";
Adrian Prantldaedfda2014-08-29 22:44:07 +00001309 else if (isPublic())
1310 OS << " [public]";
Bill Wendling523bea82013-11-08 08:13:15 +00001311
1312 if (isArtificial())
1313 OS << " [artificial]";
1314
1315 if (isForwardDecl())
1316 OS << " [decl]";
1317 else if (getTag() == dwarf::DW_TAG_structure_type ||
1318 getTag() == dwarf::DW_TAG_union_type ||
1319 getTag() == dwarf::DW_TAG_enumeration_type ||
1320 getTag() == dwarf::DW_TAG_class_type)
1321 OS << " [def]";
1322 if (isVector())
1323 OS << " [vector]";
1324 if (isStaticMember())
1325 OS << " [static]";
Adrian Prantl99c7af22013-12-18 21:48:19 +00001326
1327 if (isLValueReference())
1328 OS << " [reference]";
1329
1330 if (isRValueReference())
1331 OS << " [rvalue reference]";
Bill Wendling523bea82013-11-08 08:13:15 +00001332}
1333
1334void DIDerivedType::printInternal(raw_ostream &OS) const {
1335 DIType::printInternal(OS);
1336 OS << " [from " << getTypeDerivedFrom().getName() << ']';
1337}
1338
1339void DICompositeType::printInternal(raw_ostream &OS) const {
1340 DIType::printInternal(OS);
Manman Renab8ffba2014-07-28 19:14:13 +00001341 DIArray A = getElements();
Bill Wendling523bea82013-11-08 08:13:15 +00001342 OS << " [" << A.getNumElements() << " elements]";
1343}
1344
1345void DINameSpace::printInternal(raw_ostream &OS) const {
1346 StringRef Name = getName();
1347 if (!Name.empty())
1348 OS << " [" << Name << ']';
1349
1350 OS << " [line " << getLineNumber() << ']';
1351}
1352
1353void DISubprogram::printInternal(raw_ostream &OS) const {
1354 // TODO : Print context
1355 OS << " [line " << getLineNumber() << ']';
1356
1357 if (isLocalToUnit())
1358 OS << " [local]";
1359
1360 if (isDefinition())
1361 OS << " [def]";
1362
1363 if (getScopeLineNumber() != getLineNumber())
1364 OS << " [scope " << getScopeLineNumber() << "]";
1365
1366 if (isPrivate())
1367 OS << " [private]";
1368 else if (isProtected())
1369 OS << " [protected]";
Adrian Prantldaedfda2014-08-29 22:44:07 +00001370 else if (isPublic())
1371 OS << " [public]";
Bill Wendling523bea82013-11-08 08:13:15 +00001372
Adrian Prantl99c7af22013-12-18 21:48:19 +00001373 if (isLValueReference())
1374 OS << " [reference]";
1375
1376 if (isRValueReference())
1377 OS << " [rvalue reference]";
1378
Bill Wendling523bea82013-11-08 08:13:15 +00001379 StringRef Res = getName();
1380 if (!Res.empty())
1381 OS << " [" << Res << ']';
1382}
1383
1384void DIGlobalVariable::printInternal(raw_ostream &OS) const {
1385 StringRef Res = getName();
1386 if (!Res.empty())
1387 OS << " [" << Res << ']';
1388
1389 OS << " [line " << getLineNumber() << ']';
1390
1391 // TODO : Print context
1392
1393 if (isLocalToUnit())
1394 OS << " [local]";
1395
1396 if (isDefinition())
1397 OS << " [def]";
1398}
1399
1400void DIVariable::printInternal(raw_ostream &OS) const {
1401 StringRef Res = getName();
1402 if (!Res.empty())
1403 OS << " [" << Res << ']';
1404
1405 OS << " [line " << getLineNumber() << ']';
Adrian Prantl87b7eb92014-10-01 18:55:02 +00001406}
Adrian Prantlb1416832014-08-01 22:11:58 +00001407
Adrian Prantl87b7eb92014-10-01 18:55:02 +00001408void DIExpression::printInternal(raw_ostream &OS) const {
Adrian Prantl0d7d8e42015-01-22 16:55:22 +00001409 for (auto E = end(), I = begin(); I != E; ++I) {
1410 uint64_t OpCode = *I;
Adrian Prantl87b7eb92014-10-01 18:55:02 +00001411 OS << " [" << OperationEncodingString(OpCode);
1412 switch (OpCode) {
1413 case DW_OP_plus: {
Adrian Prantl0d7d8e42015-01-22 16:55:22 +00001414 OS << " " << I.getArg(1);
Adrian Prantl87b7eb92014-10-01 18:55:02 +00001415 break;
1416 }
1417 case DW_OP_piece: {
Adrian Prantl0d7d8e42015-01-22 16:55:22 +00001418 OS << " offset=" << I.getArg(1) << ", size=" << I.getArg(2);
Adrian Prantl87b7eb92014-10-01 18:55:02 +00001419 break;
1420 }
Adrian Prantlb9a88e22014-12-05 18:19:38 +00001421 case DW_OP_deref:
1422 // No arguments.
1423 break;
Adrian Prantl87b7eb92014-10-01 18:55:02 +00001424 default:
Adrian Prantl0d7d8e42015-01-22 16:55:22 +00001425 llvm_unreachable("unhandled operation");
Adrian Prantl87b7eb92014-10-01 18:55:02 +00001426 }
1427 OS << "]";
1428 }
Bill Wendling523bea82013-11-08 08:13:15 +00001429}
1430
1431void DIObjCProperty::printInternal(raw_ostream &OS) const {
1432 StringRef Name = getObjCPropertyName();
1433 if (!Name.empty())
1434 OS << " [" << Name << ']';
1435
1436 OS << " [line " << getLineNumber() << ", properties " << getUnsignedField(6)
1437 << ']';
1438}
1439
1440static void printDebugLoc(DebugLoc DL, raw_ostream &CommentOS,
1441 const LLVMContext &Ctx) {
1442 if (!DL.isUnknown()) { // Print source line info.
1443 DIScope Scope(DL.getScope(Ctx));
1444 assert(Scope.isScope() && "Scope of a DebugLoc should be a DIScope.");
1445 // Omit the directory, because it's likely to be long and uninteresting.
1446 CommentOS << Scope.getFilename();
1447 CommentOS << ':' << DL.getLine();
1448 if (DL.getCol() != 0)
1449 CommentOS << ':' << DL.getCol();
1450 DebugLoc InlinedAtDL = DebugLoc::getFromDILocation(DL.getInlinedAt(Ctx));
1451 if (!InlinedAtDL.isUnknown()) {
1452 CommentOS << " @[ ";
1453 printDebugLoc(InlinedAtDL, CommentOS, Ctx);
1454 CommentOS << " ]";
1455 }
1456 }
1457}
1458
1459void DIVariable::printExtendedName(raw_ostream &OS) const {
1460 const LLVMContext &Ctx = DbgNode->getContext();
1461 StringRef Res = getName();
1462 if (!Res.empty())
1463 OS << Res << "," << getLineNumber();
1464 if (MDNode *InlinedAt = getInlinedAt()) {
1465 DebugLoc InlinedAtDL = DebugLoc::getFromDILocation(InlinedAt);
1466 if (!InlinedAtDL.isUnknown()) {
1467 OS << " @[";
1468 printDebugLoc(InlinedAtDL, OS, Ctx);
1469 OS << "]";
1470 }
1471 }
1472}
1473
Duncan P. N. Exon Smithc81307a2014-11-14 23:55:03 +00001474template <> DIRef<DIScope>::DIRef(const Metadata *V) : Val(V) {
Bill Wendling523bea82013-11-08 08:13:15 +00001475 assert(isScopeRef(V) && "DIScopeRef should be a MDString or MDNode");
1476}
Duncan P. N. Exon Smithc81307a2014-11-14 23:55:03 +00001477template <> DIRef<DIType>::DIRef(const Metadata *V) : Val(V) {
Bill Wendling523bea82013-11-08 08:13:15 +00001478 assert(isTypeRef(V) && "DITypeRef should be a MDString or MDNode");
1479}
1480
Bill Wendling523bea82013-11-08 08:13:15 +00001481template <>
1482DIScopeRef DIDescriptor::getFieldAs<DIScopeRef>(unsigned Elt) const {
Duncan P. N. Exon Smithc81307a2014-11-14 23:55:03 +00001483 return DIScopeRef(cast_or_null<Metadata>(getField(DbgNode, Elt)));
Bill Wendling523bea82013-11-08 08:13:15 +00001484}
Bill Wendling523bea82013-11-08 08:13:15 +00001485template <> DITypeRef DIDescriptor::getFieldAs<DITypeRef>(unsigned Elt) const {
Duncan P. N. Exon Smithc81307a2014-11-14 23:55:03 +00001486 return DITypeRef(cast_or_null<Metadata>(getField(DbgNode, Elt)));
Bill Wendling523bea82013-11-08 08:13:15 +00001487}
Manman Rencb14bbc2013-11-22 22:06:31 +00001488
Manman Rencb14bbc2013-11-22 22:06:31 +00001489bool llvm::StripDebugInfo(Module &M) {
Manman Rencb14bbc2013-11-22 22:06:31 +00001490 bool Changed = false;
1491
1492 // Remove all of the calls to the debugger intrinsics, and remove them from
1493 // the module.
1494 if (Function *Declare = M.getFunction("llvm.dbg.declare")) {
1495 while (!Declare->use_empty()) {
Chandler Carruthcdf47882014-03-09 03:16:01 +00001496 CallInst *CI = cast<CallInst>(Declare->user_back());
Manman Rencb14bbc2013-11-22 22:06:31 +00001497 CI->eraseFromParent();
1498 }
1499 Declare->eraseFromParent();
1500 Changed = true;
1501 }
1502
1503 if (Function *DbgVal = M.getFunction("llvm.dbg.value")) {
1504 while (!DbgVal->use_empty()) {
Chandler Carruthcdf47882014-03-09 03:16:01 +00001505 CallInst *CI = cast<CallInst>(DbgVal->user_back());
Manman Rencb14bbc2013-11-22 22:06:31 +00001506 CI->eraseFromParent();
1507 }
1508 DbgVal->eraseFromParent();
1509 Changed = true;
1510 }
1511
1512 for (Module::named_metadata_iterator NMI = M.named_metadata_begin(),
1513 NME = M.named_metadata_end(); NMI != NME;) {
1514 NamedMDNode *NMD = NMI;
1515 ++NMI;
1516 if (NMD->getName().startswith("llvm.dbg.")) {
1517 NMD->eraseFromParent();
1518 Changed = true;
1519 }
1520 }
1521
1522 for (Module::iterator MI = M.begin(), ME = M.end(); MI != ME; ++MI)
1523 for (Function::iterator FI = MI->begin(), FE = MI->end(); FI != FE;
1524 ++FI)
1525 for (BasicBlock::iterator BI = FI->begin(), BE = FI->end(); BI != BE;
1526 ++BI) {
1527 if (!BI->getDebugLoc().isUnknown()) {
1528 Changed = true;
1529 BI->setDebugLoc(DebugLoc());
1530 }
1531 }
1532
1533 return Changed;
1534}
Manman Ren8b4306c2013-12-02 21:29:56 +00001535
Manman Renbd4daf82013-12-03 00:12:14 +00001536unsigned llvm::getDebugMetadataVersionFromModule(const Module &M) {
Duncan P. N. Exon Smith5bf8fef2014-12-09 18:38:53 +00001537 if (auto *Val = mdconst::extract_or_null<ConstantInt>(
1538 M.getModuleFlag("Debug Info Version")))
1539 return Val->getZExtValue();
1540 return 0;
Manman Ren8b4306c2013-12-02 21:29:56 +00001541}
David Blaikie6876b3b2014-07-01 20:05:26 +00001542
David Blaikiea8c35092014-07-02 18:30:05 +00001543llvm::DenseMap<const llvm::Function *, llvm::DISubprogram>
1544llvm::makeSubprogramMap(const Module &M) {
1545 DenseMap<const Function *, DISubprogram> R;
David Blaikie6876b3b2014-07-01 20:05:26 +00001546
1547 NamedMDNode *CU_Nodes = M.getNamedMetadata("llvm.dbg.cu");
1548 if (!CU_Nodes)
1549 return R;
1550
Duncan P. N. Exon Smithde36e802014-11-11 21:30:22 +00001551 for (MDNode *N : CU_Nodes->operands()) {
1552 DICompileUnit CUNode(N);
David Blaikie6876b3b2014-07-01 20:05:26 +00001553 DIArray SPs = CUNode.getSubprograms();
1554 for (unsigned i = 0, e = SPs.getNumElements(); i != e; ++i) {
1555 DISubprogram SP(SPs.getElement(i));
1556 if (Function *F = SP.getFunction())
1557 R.insert(std::make_pair(F, SP));
1558 }
1559 }
1560 return R;
1561}