blob: 8e996148955c2c60aefad14602fd6d1fdb6e8dc4 [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"
Duncan P. N. Exon Smithc22a5c22015-02-21 00:43:09 +000020#include "llvm/ADT/StringSwitch.h"
Bill Wendling523bea82013-11-08 08:13:15 +000021#include "llvm/Analysis/ValueTracking.h"
22#include "llvm/IR/Constants.h"
Adrian Prantlb1416832014-08-01 22:11:58 +000023#include "llvm/IR/DIBuilder.h"
Bill Wendling523bea82013-11-08 08:13:15 +000024#include "llvm/IR/DerivedTypes.h"
25#include "llvm/IR/Instructions.h"
26#include "llvm/IR/IntrinsicInst.h"
27#include "llvm/IR/Intrinsics.h"
28#include "llvm/IR/Module.h"
Chandler Carruth4220e9c2014-03-04 11:17:44 +000029#include "llvm/IR/ValueHandle.h"
Bill Wendling523bea82013-11-08 08:13:15 +000030#include "llvm/Support/Debug.h"
31#include "llvm/Support/Dwarf.h"
Bill Wendling523bea82013-11-08 08:13:15 +000032#include "llvm/Support/raw_ostream.h"
33using namespace llvm;
34using namespace llvm::dwarf;
35
36//===----------------------------------------------------------------------===//
37// DIDescriptor
38//===----------------------------------------------------------------------===//
39
Duncan P. N. Exon Smithc22a5c22015-02-21 00:43:09 +000040unsigned DIDescriptor::getFlag(StringRef Flag) {
41 return StringSwitch<unsigned>(Flag)
42#define HANDLE_DI_FLAG(ID, NAME) .Case("DIFlag" #NAME, Flag##NAME)
43#include "llvm/IR/DebugInfoFlags.def"
44 .Default(0);
45}
46
47const char *DIDescriptor::getFlagString(unsigned Flag) {
48 switch (Flag) {
49 default:
50 return "";
51#define HANDLE_DI_FLAG(ID, NAME) \
52 case Flag##NAME: \
53 return "DIFlag" #NAME;
54#include "llvm/IR/DebugInfoFlags.def"
55 }
56}
57
Duncan P. N. Exon Smith269e38d2015-02-21 00:45:26 +000058unsigned DIDescriptor::splitFlags(unsigned Flags,
59 SmallVectorImpl<unsigned> &SplitFlags) {
60 // Accessibility flags need to be specially handled, since they're packed
61 // together.
62 if (unsigned A = Flags & FlagAccessibility) {
63 if (A == FlagPrivate)
64 SplitFlags.push_back(FlagPrivate);
65 else if (A == FlagProtected)
66 SplitFlags.push_back(FlagProtected);
67 else
68 SplitFlags.push_back(FlagPublic);
69 Flags &= ~A;
70 }
71
72#define HANDLE_DI_FLAG(ID, NAME) \
73 if (unsigned Bit = Flags & ID) { \
74 SplitFlags.push_back(Bit); \
75 Flags &= ~Bit; \
76 }
77#include "llvm/IR/DebugInfoFlags.def"
78
79 return Flags;
80}
81
Bill Wendling523bea82013-11-08 08:13:15 +000082bool DIDescriptor::Verify() const {
83 return DbgNode &&
84 (DIDerivedType(DbgNode).Verify() ||
85 DICompositeType(DbgNode).Verify() || DIBasicType(DbgNode).Verify() ||
86 DIVariable(DbgNode).Verify() || DISubprogram(DbgNode).Verify() ||
87 DIGlobalVariable(DbgNode).Verify() || DIFile(DbgNode).Verify() ||
88 DICompileUnit(DbgNode).Verify() || DINameSpace(DbgNode).Verify() ||
89 DILexicalBlock(DbgNode).Verify() ||
90 DILexicalBlockFile(DbgNode).Verify() ||
91 DISubrange(DbgNode).Verify() || DIEnumerator(DbgNode).Verify() ||
92 DIObjCProperty(DbgNode).Verify() ||
93 DITemplateTypeParameter(DbgNode).Verify() ||
94 DITemplateValueParameter(DbgNode).Verify() ||
Adrian Prantl87b7eb92014-10-01 18:55:02 +000095 DIImportedEntity(DbgNode).Verify() || DIExpression(DbgNode).Verify());
Bill Wendling523bea82013-11-08 08:13:15 +000096}
97
Duncan P. N. Exon Smith5bf8fef2014-12-09 18:38:53 +000098static Metadata *getField(const MDNode *DbgNode, unsigned Elt) {
Craig Topperc6207612014-04-09 06:08:46 +000099 if (!DbgNode || Elt >= DbgNode->getNumOperands())
100 return nullptr;
Bill Wendling523bea82013-11-08 08:13:15 +0000101 return DbgNode->getOperand(Elt);
102}
103
104static MDNode *getNodeField(const MDNode *DbgNode, unsigned Elt) {
105 return dyn_cast_or_null<MDNode>(getField(DbgNode, Elt));
106}
107
108static StringRef getStringField(const MDNode *DbgNode, unsigned Elt) {
109 if (MDString *MDS = dyn_cast_or_null<MDString>(getField(DbgNode, Elt)))
110 return MDS->getString();
111 return StringRef();
112}
113
114StringRef DIDescriptor::getStringField(unsigned Elt) const {
115 return ::getStringField(DbgNode, Elt);
116}
117
118uint64_t DIDescriptor::getUInt64Field(unsigned Elt) const {
Duncan P. N. Exon Smith5bf8fef2014-12-09 18:38:53 +0000119 if (auto *C = getConstantField(Elt))
120 if (ConstantInt *CI = dyn_cast<ConstantInt>(C))
Bill Wendling523bea82013-11-08 08:13:15 +0000121 return CI->getZExtValue();
122
123 return 0;
124}
125
126int64_t DIDescriptor::getInt64Field(unsigned Elt) const {
Duncan P. N. Exon Smith5bf8fef2014-12-09 18:38:53 +0000127 if (auto *C = getConstantField(Elt))
128 if (ConstantInt *CI = dyn_cast<ConstantInt>(C))
129 return CI->getZExtValue();
Bill Wendling523bea82013-11-08 08:13:15 +0000130
131 return 0;
132}
133
134DIDescriptor DIDescriptor::getDescriptorField(unsigned Elt) const {
135 MDNode *Field = getNodeField(DbgNode, Elt);
136 return DIDescriptor(Field);
137}
138
139GlobalVariable *DIDescriptor::getGlobalVariableField(unsigned Elt) const {
Duncan P. N. Exon Smith5bf8fef2014-12-09 18:38:53 +0000140 return dyn_cast_or_null<GlobalVariable>(getConstantField(Elt));
Bill Wendling523bea82013-11-08 08:13:15 +0000141}
142
143Constant *DIDescriptor::getConstantField(unsigned Elt) const {
Craig Topperc6207612014-04-09 06:08:46 +0000144 if (!DbgNode)
145 return nullptr;
Bill Wendling523bea82013-11-08 08:13:15 +0000146
147 if (Elt < DbgNode->getNumOperands())
Duncan P. N. Exon Smith5bf8fef2014-12-09 18:38:53 +0000148 if (auto *C =
149 dyn_cast_or_null<ConstantAsMetadata>(DbgNode->getOperand(Elt)))
150 return C->getValue();
Craig Topperc6207612014-04-09 06:08:46 +0000151 return nullptr;
Bill Wendling523bea82013-11-08 08:13:15 +0000152}
153
154Function *DIDescriptor::getFunctionField(unsigned Elt) const {
Duncan P. N. Exon Smith5bf8fef2014-12-09 18:38:53 +0000155 return dyn_cast_or_null<Function>(getConstantField(Elt));
Bill Wendling523bea82013-11-08 08:13:15 +0000156}
157
158void DIDescriptor::replaceFunctionField(unsigned Elt, Function *F) {
Craig Topperc6207612014-04-09 06:08:46 +0000159 if (!DbgNode)
Bill Wendling523bea82013-11-08 08:13:15 +0000160 return;
161
162 if (Elt < DbgNode->getNumOperands()) {
163 MDNode *Node = const_cast<MDNode *>(DbgNode);
Duncan P. N. Exon Smith5bf8fef2014-12-09 18:38:53 +0000164 Node->replaceOperandWith(Elt, F ? ConstantAsMetadata::get(F) : nullptr);
Bill Wendling523bea82013-11-08 08:13:15 +0000165 }
166}
167
Duncan P. N. Exon Smith176b6912014-10-03 20:01:09 +0000168static unsigned DIVariableInlinedAtIndex = 4;
169MDNode *DIVariable::getInlinedAt() const {
170 return getNodeField(DbgNode, DIVariableInlinedAtIndex);
171}
Bill Wendling523bea82013-11-08 08:13:15 +0000172
Duncan P. N. Exon Smith7f637a92014-10-15 17:01:28 +0000173/// \brief Return the size reported by the variable's type.
Adrian Prantlb1416832014-08-01 22:11:58 +0000174unsigned DIVariable::getSizeInBits(const DITypeIdentifierMap &Map) {
175 DIType Ty = getType().resolve(Map);
176 // Follow derived types until we reach a type that
177 // reports back a size.
178 while (Ty.isDerivedType() && !Ty.getSizeInBits()) {
179 DIDerivedType DT(&*Ty);
180 Ty = DT.getTypeDerivedFrom().resolve(Map);
181 }
182 assert(Ty.getSizeInBits() && "type with size 0");
183 return Ty.getSizeInBits();
184}
185
Adrian Prantl87b7eb92014-10-01 18:55:02 +0000186uint64_t DIExpression::getElement(unsigned Idx) const {
187 unsigned I = Idx + 1;
Duncan P. N. Exon Smith176b6912014-10-03 20:01:09 +0000188 assert(I < getNumHeaderFields() &&
189 "non-existing complex address element requested");
190 return getHeaderFieldAs<int64_t>(I);
Adrian Prantl87b7eb92014-10-01 18:55:02 +0000191}
Adrian Prantlb1416832014-08-01 22:11:58 +0000192
Adrian Prantl27bd01f2015-02-09 23:57:15 +0000193bool DIExpression::isBitPiece() const {
Adrian Prantl34bcbee2015-01-21 00:59:20 +0000194 unsigned N = getNumElements();
Adrian Prantl27bd01f2015-02-09 23:57:15 +0000195 return N >=3 && getElement(N-3) == dwarf::DW_OP_bit_piece;
Adrian Prantl87b7eb92014-10-01 18:55:02 +0000196}
197
Adrian Prantl27bd01f2015-02-09 23:57:15 +0000198uint64_t DIExpression::getBitPieceOffset() const {
199 assert(isBitPiece() && "not a piece");
Adrian Prantl34bcbee2015-01-21 00:59:20 +0000200 return getElement(getNumElements()-2);
Adrian Prantl87b7eb92014-10-01 18:55:02 +0000201}
202
Adrian Prantl27bd01f2015-02-09 23:57:15 +0000203uint64_t DIExpression::getBitPieceSize() const {
204 assert(isBitPiece() && "not a piece");
Adrian Prantl34bcbee2015-01-21 00:59:20 +0000205 return getElement(getNumElements()-1);
Adrian Prantl87b7eb92014-10-01 18:55:02 +0000206}
Adrian Prantlb1416832014-08-01 22:11:58 +0000207
Adrian Prantl2585a982015-01-22 16:55:20 +0000208DIExpression::iterator DIExpression::begin() const {
209 return DIExpression::iterator(*this);
Adrian Prantl9260cca2015-01-22 00:00:52 +0000210}
211
Adrian Prantl2585a982015-01-22 16:55:20 +0000212DIExpression::iterator DIExpression::end() const {
213 return DIExpression::iterator();
Adrian Prantl9260cca2015-01-22 00:00:52 +0000214}
215
Benjamin Kramer4d50b6c2015-01-24 19:55:23 +0000216DIExpression::Operand DIExpression::Operand::getNext() const {
Adrian Prantl70f2a732015-01-23 23:40:47 +0000217 iterator it(I);
218 return *(++it);
219}
220
Bill Wendling523bea82013-11-08 08:13:15 +0000221//===----------------------------------------------------------------------===//
222// Predicates
223//===----------------------------------------------------------------------===//
224
Manman Renf8a19672014-07-28 22:24:06 +0000225bool DIDescriptor::isSubroutineType() const {
Yaron Kerene8270222014-12-19 22:15:09 +0000226 return DbgNode && getTag() == dwarf::DW_TAG_subroutine_type;
Manman Renf8a19672014-07-28 22:24:06 +0000227}
228
Bill Wendling523bea82013-11-08 08:13:15 +0000229bool DIDescriptor::isBasicType() const {
230 if (!DbgNode)
231 return false;
232 switch (getTag()) {
233 case dwarf::DW_TAG_base_type:
234 case dwarf::DW_TAG_unspecified_type:
235 return true;
236 default:
237 return false;
238 }
239}
240
Bill Wendling523bea82013-11-08 08:13:15 +0000241bool DIDescriptor::isDerivedType() const {
242 if (!DbgNode)
243 return false;
244 switch (getTag()) {
245 case dwarf::DW_TAG_typedef:
246 case dwarf::DW_TAG_pointer_type:
247 case dwarf::DW_TAG_ptr_to_member_type:
248 case dwarf::DW_TAG_reference_type:
249 case dwarf::DW_TAG_rvalue_reference_type:
250 case dwarf::DW_TAG_const_type:
251 case dwarf::DW_TAG_volatile_type:
252 case dwarf::DW_TAG_restrict_type:
253 case dwarf::DW_TAG_member:
254 case dwarf::DW_TAG_inheritance:
255 case dwarf::DW_TAG_friend:
256 return true;
257 default:
258 // CompositeTypes are currently modelled as DerivedTypes.
259 return isCompositeType();
260 }
261}
262
Bill Wendling523bea82013-11-08 08:13:15 +0000263bool DIDescriptor::isCompositeType() const {
264 if (!DbgNode)
265 return false;
266 switch (getTag()) {
267 case dwarf::DW_TAG_array_type:
268 case dwarf::DW_TAG_structure_type:
269 case dwarf::DW_TAG_union_type:
270 case dwarf::DW_TAG_enumeration_type:
271 case dwarf::DW_TAG_subroutine_type:
272 case dwarf::DW_TAG_class_type:
273 return true;
274 default:
275 return false;
276 }
277}
278
Bill Wendling523bea82013-11-08 08:13:15 +0000279bool DIDescriptor::isVariable() const {
280 if (!DbgNode)
281 return false;
282 switch (getTag()) {
283 case dwarf::DW_TAG_auto_variable:
284 case dwarf::DW_TAG_arg_variable:
285 return true;
286 default:
287 return false;
288 }
289}
290
Bill Wendling523bea82013-11-08 08:13:15 +0000291bool DIDescriptor::isType() const {
Manman Renf93ac4b2014-07-29 18:20:39 +0000292 return isBasicType() || isCompositeType() || isDerivedType();
Bill Wendling523bea82013-11-08 08:13:15 +0000293}
294
Bill Wendling523bea82013-11-08 08:13:15 +0000295bool DIDescriptor::isSubprogram() const {
296 return DbgNode && getTag() == dwarf::DW_TAG_subprogram;
297}
298
Bill Wendling523bea82013-11-08 08:13:15 +0000299bool DIDescriptor::isGlobalVariable() const {
Duncan P. N. Exon Smithb407bb22015-02-09 22:48:04 +0000300 return DbgNode && getTag() == dwarf::DW_TAG_variable;
Bill Wendling523bea82013-11-08 08:13:15 +0000301}
302
Bill Wendling523bea82013-11-08 08:13:15 +0000303bool DIDescriptor::isScope() const {
304 if (!DbgNode)
305 return false;
306 switch (getTag()) {
307 case dwarf::DW_TAG_compile_unit:
308 case dwarf::DW_TAG_lexical_block:
309 case dwarf::DW_TAG_subprogram:
310 case dwarf::DW_TAG_namespace:
311 case dwarf::DW_TAG_file_type:
312 return true;
313 default:
314 break;
315 }
316 return isType();
317}
318
Bill Wendling523bea82013-11-08 08:13:15 +0000319bool DIDescriptor::isTemplateTypeParameter() const {
320 return DbgNode && getTag() == dwarf::DW_TAG_template_type_parameter;
321}
322
Bill Wendling523bea82013-11-08 08:13:15 +0000323bool DIDescriptor::isTemplateValueParameter() const {
324 return DbgNode && (getTag() == dwarf::DW_TAG_template_value_parameter ||
325 getTag() == dwarf::DW_TAG_GNU_template_template_param ||
326 getTag() == dwarf::DW_TAG_GNU_template_parameter_pack);
327}
328
Bill Wendling523bea82013-11-08 08:13:15 +0000329bool DIDescriptor::isCompileUnit() const {
330 return DbgNode && getTag() == dwarf::DW_TAG_compile_unit;
331}
332
Bill Wendling523bea82013-11-08 08:13:15 +0000333bool DIDescriptor::isFile() const {
334 return DbgNode && getTag() == dwarf::DW_TAG_file_type;
335}
336
Bill Wendling523bea82013-11-08 08:13:15 +0000337bool DIDescriptor::isNameSpace() const {
338 return DbgNode && getTag() == dwarf::DW_TAG_namespace;
339}
340
Bill Wendling523bea82013-11-08 08:13:15 +0000341bool DIDescriptor::isLexicalBlockFile() const {
342 return DbgNode && getTag() == dwarf::DW_TAG_lexical_block &&
Duncan P. N. Exon Smith176b6912014-10-03 20:01:09 +0000343 DbgNode->getNumOperands() == 3 && getNumHeaderFields() == 2;
Bill Wendling523bea82013-11-08 08:13:15 +0000344}
345
Bill Wendling523bea82013-11-08 08:13:15 +0000346bool DIDescriptor::isLexicalBlock() const {
Duncan P. N. Exon Smith176b6912014-10-03 20:01:09 +0000347 // FIXME: There are always exactly 4 header fields in DILexicalBlock, but
348 // something relies on this returning true for DILexicalBlockFile.
Bill Wendling523bea82013-11-08 08:13:15 +0000349 return DbgNode && getTag() == dwarf::DW_TAG_lexical_block &&
Duncan P. N. Exon Smith176b6912014-10-03 20:01:09 +0000350 DbgNode->getNumOperands() == 3 &&
351 (getNumHeaderFields() == 2 || getNumHeaderFields() == 4);
Bill Wendling523bea82013-11-08 08:13:15 +0000352}
353
Bill Wendling523bea82013-11-08 08:13:15 +0000354bool DIDescriptor::isSubrange() const {
355 return DbgNode && getTag() == dwarf::DW_TAG_subrange_type;
356}
357
Bill Wendling523bea82013-11-08 08:13:15 +0000358bool DIDescriptor::isEnumerator() const {
359 return DbgNode && getTag() == dwarf::DW_TAG_enumerator;
360}
361
Bill Wendling523bea82013-11-08 08:13:15 +0000362bool DIDescriptor::isObjCProperty() const {
363 return DbgNode && getTag() == dwarf::DW_TAG_APPLE_property;
364}
365
Bill Wendling523bea82013-11-08 08:13:15 +0000366bool DIDescriptor::isImportedEntity() const {
367 return DbgNode && (getTag() == dwarf::DW_TAG_imported_module ||
368 getTag() == dwarf::DW_TAG_imported_declaration);
369}
370
Adrian Prantl87b7eb92014-10-01 18:55:02 +0000371bool DIDescriptor::isExpression() const {
372 return DbgNode && (getTag() == dwarf::DW_TAG_expression);
373}
374
Bill Wendling523bea82013-11-08 08:13:15 +0000375//===----------------------------------------------------------------------===//
376// Simple Descriptor Constructors and other Methods
377//===----------------------------------------------------------------------===//
378
Duncan P. N. Exon Smith9c3b8942015-02-28 23:48:02 +0000379void DIDescriptor::replaceAllUsesWith(LLVMContext &, DIDescriptor D) {
Bill Wendling523bea82013-11-08 08:13:15 +0000380 assert(DbgNode && "Trying to replace an unverified type!");
Duncan P. N. Exon Smith9c3b8942015-02-28 23:48:02 +0000381 assert(DbgNode->isTemporary() && "Expected temporary node");
382 TempMDNode Temp(get());
Bill Wendling523bea82013-11-08 08:13:15 +0000383
384 // Since we use a TrackingVH for the node, its easy for clients to manufacture
385 // legitimate situations where they want to replaceAllUsesWith() on something
386 // which, due to uniquing, has merged with the source. We shield clients from
387 // this detail by allowing a value to be replaced with replaceAllUsesWith()
388 // itself.
Duncan P. N. Exon Smith9c3b8942015-02-28 23:48:02 +0000389 if (Temp.get() == D.get()) {
390 DbgNode = MDNode::replaceWithUniqued(std::move(Temp));
391 return;
Bill Wendling523bea82013-11-08 08:13:15 +0000392 }
David Blaikied3f094a2014-05-06 03:41:57 +0000393
Duncan P. N. Exon Smith9c3b8942015-02-28 23:48:02 +0000394 Temp->replaceAllUsesWith(D.get());
395 DbgNode = D.get();
Bill Wendling523bea82013-11-08 08:13:15 +0000396}
397
Frederic Riss36acf0f2014-09-15 07:50:36 +0000398void DIDescriptor::replaceAllUsesWith(MDNode *D) {
Bill Wendling523bea82013-11-08 08:13:15 +0000399 assert(DbgNode && "Trying to replace an unverified type!");
David Blaikied3f094a2014-05-06 03:41:57 +0000400 assert(DbgNode != D && "This replacement should always happen");
Duncan P. N. Exon Smith946fdcc2015-01-19 20:36:39 +0000401 assert(DbgNode->isTemporary() && "Expected temporary node");
Duncan P. N. Exon Smith9c3b8942015-02-28 23:48:02 +0000402 TempMDNode Node(get());
Duncan P. N. Exon Smith5bf8fef2014-12-09 18:38:53 +0000403 Node->replaceAllUsesWith(D);
Bill Wendling523bea82013-11-08 08:13:15 +0000404}
405
Bill Wendling523bea82013-11-08 08:13:15 +0000406bool DICompileUnit::Verify() const {
407 if (!isCompileUnit())
408 return false;
409
410 // Don't bother verifying the compilation directory or producer string
411 // as those could be empty.
412 if (getFilename().empty())
413 return false;
414
Duncan P. N. Exon Smith176b6912014-10-03 20:01:09 +0000415 return DbgNode->getNumOperands() == 7 && getNumHeaderFields() == 8;
Bill Wendling523bea82013-11-08 08:13:15 +0000416}
417
Bill Wendling523bea82013-11-08 08:13:15 +0000418bool DIObjCProperty::Verify() const {
419 if (!isObjCProperty())
420 return false;
421
422 // Don't worry about the rest of the strings for now.
Duncan P. N. Exon Smith176b6912014-10-03 20:01:09 +0000423 return DbgNode->getNumOperands() == 3 && getNumHeaderFields() == 6;
Bill Wendling523bea82013-11-08 08:13:15 +0000424}
425
Duncan P. N. Exon Smith7f637a92014-10-15 17:01:28 +0000426/// \brief Check if a field at position Elt of a MDNode is a MDNode.
Bill Wendling523bea82013-11-08 08:13:15 +0000427static bool fieldIsMDNode(const MDNode *DbgNode, unsigned Elt) {
Duncan P. N. Exon Smith5bf8fef2014-12-09 18:38:53 +0000428 Metadata *Fld = getField(DbgNode, Elt);
Duncan P. N. Exon Smitha55dcaf2015-02-17 22:34:15 +0000429 return !Fld || isa<MDNode>(Fld);
Bill Wendling523bea82013-11-08 08:13:15 +0000430}
431
Duncan P. N. Exon Smith7f637a92014-10-15 17:01:28 +0000432/// \brief Check if a field at position Elt of a MDNode is a MDString.
Bill Wendling523bea82013-11-08 08:13:15 +0000433static bool fieldIsMDString(const MDNode *DbgNode, unsigned Elt) {
Duncan P. N. Exon Smith5bf8fef2014-12-09 18:38:53 +0000434 Metadata *Fld = getField(DbgNode, Elt);
Bill Wendling523bea82013-11-08 08:13:15 +0000435 return !Fld || isa<MDString>(Fld);
436}
437
Duncan P. N. Exon Smith7f637a92014-10-15 17:01:28 +0000438/// \brief Check if a value can be a reference to a type.
Duncan P. N. Exon Smithc81307a2014-11-14 23:55:03 +0000439static bool isTypeRef(const Metadata *MD) {
440 if (!MD)
441 return true;
442 if (auto *S = dyn_cast<MDString>(MD))
443 return !S->getString().empty();
444 if (auto *N = dyn_cast<MDNode>(MD))
445 return DIType(N).isType();
446 return false;
Bill Wendling523bea82013-11-08 08:13:15 +0000447}
448
Duncan P. N. Exon Smith7f637a92014-10-15 17:01:28 +0000449/// \brief Check if referenced field might be a type.
Bill Wendling523bea82013-11-08 08:13:15 +0000450static bool fieldIsTypeRef(const MDNode *DbgNode, unsigned Elt) {
Duncan P. N. Exon Smithc81307a2014-11-14 23:55:03 +0000451 return isTypeRef(dyn_cast_or_null<Metadata>(getField(DbgNode, Elt)));
Bill Wendling523bea82013-11-08 08:13:15 +0000452}
453
Duncan P. N. Exon Smith7f637a92014-10-15 17:01:28 +0000454/// \brief Check if a value can be a ScopeRef.
Duncan P. N. Exon Smithc81307a2014-11-14 23:55:03 +0000455static bool isScopeRef(const Metadata *MD) {
456 if (!MD)
457 return true;
458 if (auto *S = dyn_cast<MDString>(MD))
459 return !S->getString().empty();
Duncan P. N. Exon Smith8551d252015-02-18 19:46:02 +0000460 if (auto *N = dyn_cast<MDNode>(MD))
461 return DIScope(N).isScope();
462 return false;
Bill Wendling523bea82013-11-08 08:13:15 +0000463}
464
Duncan P. N. Exon Smith7f637a92014-10-15 17:01:28 +0000465/// \brief Check if a field at position Elt of a MDNode can be a ScopeRef.
Bill Wendling523bea82013-11-08 08:13:15 +0000466static bool fieldIsScopeRef(const MDNode *DbgNode, unsigned Elt) {
Duncan P. N. Exon Smithc81307a2014-11-14 23:55:03 +0000467 return isScopeRef(dyn_cast_or_null<Metadata>(getField(DbgNode, Elt)));
Bill Wendling523bea82013-11-08 08:13:15 +0000468}
469
Duncan P. N. Exon Smithe4450142015-02-18 19:56:50 +0000470#ifndef NDEBUG
Duncan P. N. Exon Smith2a78e9b2015-02-18 19:39:36 +0000471/// \brief Check if a value can be a DescriptorRef.
472static bool isDescriptorRef(const Metadata *MD) {
473 if (!MD)
474 return true;
475 if (auto *S = dyn_cast<MDString>(MD))
476 return !S->getString().empty();
477 return isa<MDNode>(MD);
478}
Duncan P. N. Exon Smithe4450142015-02-18 19:56:50 +0000479#endif
Duncan P. N. Exon Smith2a78e9b2015-02-18 19:39:36 +0000480
Bill Wendling523bea82013-11-08 08:13:15 +0000481bool DIType::Verify() const {
482 if (!isType())
483 return false;
484 // Make sure Context @ field 2 is MDNode.
485 if (!fieldIsScopeRef(DbgNode, 2))
486 return false;
487
488 // FIXME: Sink this into the various subclass verifies.
489 uint16_t Tag = getTag();
Manman Renf93ac4b2014-07-29 18:20:39 +0000490 if (!isBasicType() && Tag != dwarf::DW_TAG_const_type &&
Bill Wendling523bea82013-11-08 08:13:15 +0000491 Tag != dwarf::DW_TAG_volatile_type && Tag != dwarf::DW_TAG_pointer_type &&
492 Tag != dwarf::DW_TAG_ptr_to_member_type &&
493 Tag != dwarf::DW_TAG_reference_type &&
494 Tag != dwarf::DW_TAG_rvalue_reference_type &&
495 Tag != dwarf::DW_TAG_restrict_type && Tag != dwarf::DW_TAG_array_type &&
496 Tag != dwarf::DW_TAG_enumeration_type &&
497 Tag != dwarf::DW_TAG_subroutine_type &&
498 Tag != dwarf::DW_TAG_inheritance && Tag != dwarf::DW_TAG_friend &&
499 getFilename().empty())
500 return false;
Adrian Prantldaedfda2014-08-29 22:44:07 +0000501
Bill Wendling523bea82013-11-08 08:13:15 +0000502 // DIType is abstract, it should be a BasicType, a DerivedType or
503 // a CompositeType.
504 if (isBasicType())
Renato Golin47f46fd2013-11-26 16:47:00 +0000505 return DIBasicType(DbgNode).Verify();
Bill Wendling523bea82013-11-08 08:13:15 +0000506 else if (isCompositeType())
Renato Golin47f46fd2013-11-26 16:47:00 +0000507 return DICompositeType(DbgNode).Verify();
Bill Wendling523bea82013-11-08 08:13:15 +0000508 else if (isDerivedType())
Renato Golin47f46fd2013-11-26 16:47:00 +0000509 return DIDerivedType(DbgNode).Verify();
Bill Wendling523bea82013-11-08 08:13:15 +0000510 else
511 return false;
Bill Wendling523bea82013-11-08 08:13:15 +0000512}
513
Bill Wendling523bea82013-11-08 08:13:15 +0000514bool DIBasicType::Verify() const {
Duncan P. N. Exon Smith176b6912014-10-03 20:01:09 +0000515 return isBasicType() && DbgNode->getNumOperands() == 3 &&
516 getNumHeaderFields() == 8;
Bill Wendling523bea82013-11-08 08:13:15 +0000517}
518
Bill Wendling523bea82013-11-08 08:13:15 +0000519bool DIDerivedType::Verify() const {
Duncan P. N. Exon Smith176b6912014-10-03 20:01:09 +0000520 // Make sure DerivedFrom @ field 3 is TypeRef.
521 if (!fieldIsTypeRef(DbgNode, 3))
Bill Wendling523bea82013-11-08 08:13:15 +0000522 return false;
523 if (getTag() == dwarf::DW_TAG_ptr_to_member_type)
Duncan P. N. Exon Smith176b6912014-10-03 20:01:09 +0000524 // Make sure ClassType @ field 4 is a TypeRef.
525 if (!fieldIsTypeRef(DbgNode, 4))
Bill Wendling523bea82013-11-08 08:13:15 +0000526 return false;
527
Duncan P. N. Exon Smith176b6912014-10-03 20:01:09 +0000528 return isDerivedType() && DbgNode->getNumOperands() >= 4 &&
529 DbgNode->getNumOperands() <= 8 && getNumHeaderFields() >= 7 &&
530 getNumHeaderFields() <= 8;
Bill Wendling523bea82013-11-08 08:13:15 +0000531}
532
Bill Wendling523bea82013-11-08 08:13:15 +0000533bool DICompositeType::Verify() const {
534 if (!isCompositeType())
535 return false;
536
Duncan P. N. Exon Smith176b6912014-10-03 20:01:09 +0000537 // Make sure DerivedFrom @ field 3 and ContainingType @ field 5 are TypeRef.
538 if (!fieldIsTypeRef(DbgNode, 3))
Bill Wendling523bea82013-11-08 08:13:15 +0000539 return false;
Duncan P. N. Exon Smith176b6912014-10-03 20:01:09 +0000540 if (!fieldIsTypeRef(DbgNode, 5))
Bill Wendling523bea82013-11-08 08:13:15 +0000541 return false;
542
Duncan P. N. Exon Smith176b6912014-10-03 20:01:09 +0000543 // Make sure the type identifier at field 7 is MDString, it can be null.
544 if (!fieldIsMDString(DbgNode, 7))
Bill Wendling523bea82013-11-08 08:13:15 +0000545 return false;
546
Adrian Prantl99c7af22013-12-18 21:48:19 +0000547 // A subroutine type can't be both & and &&.
548 if (isLValueReference() && isRValueReference())
549 return false;
550
Duncan P. N. Exon Smith176b6912014-10-03 20:01:09 +0000551 return DbgNode->getNumOperands() == 8 && getNumHeaderFields() == 8;
Bill Wendling523bea82013-11-08 08:13:15 +0000552}
553
Bill Wendling523bea82013-11-08 08:13:15 +0000554bool DISubprogram::Verify() const {
555 if (!isSubprogram())
556 return false;
557
Duncan P. N. Exon Smith176b6912014-10-03 20:01:09 +0000558 // Make sure context @ field 2 is a ScopeRef and type @ field 3 is a MDNode.
Bill Wendling523bea82013-11-08 08:13:15 +0000559 if (!fieldIsScopeRef(DbgNode, 2))
560 return false;
Duncan P. N. Exon Smith176b6912014-10-03 20:01:09 +0000561 if (!fieldIsMDNode(DbgNode, 3))
Bill Wendling523bea82013-11-08 08:13:15 +0000562 return false;
Duncan P. N. Exon Smith176b6912014-10-03 20:01:09 +0000563 // Containing type @ field 4.
564 if (!fieldIsTypeRef(DbgNode, 4))
Bill Wendling523bea82013-11-08 08:13:15 +0000565 return false;
Adrian Prantl99c7af22013-12-18 21:48:19 +0000566
567 // A subprogram can't be both & and &&.
568 if (isLValueReference() && isRValueReference())
569 return false;
570
David Blaikie3dfe4782014-10-14 18:22:52 +0000571 // If a DISubprogram has an llvm::Function*, then scope chains from all
572 // instructions within the function should lead to this DISubprogram.
573 if (auto *F = getFunction()) {
David Blaikie3dfe4782014-10-14 18:22:52 +0000574 for (auto &BB : *F) {
575 for (auto &I : BB) {
576 DebugLoc DL = I.getDebugLoc();
577 if (DL.isUnknown())
578 continue;
579
580 MDNode *Scope = nullptr;
581 MDNode *IA = nullptr;
582 // walk the inlined-at scopes
Duncan P. N. Exon Smith5bf8fef2014-12-09 18:38:53 +0000583 while ((IA = DL.getInlinedAt()))
David Blaikie3dfe4782014-10-14 18:22:52 +0000584 DL = DebugLoc::getFromDILocation(IA);
Duncan P. N. Exon Smith5bf8fef2014-12-09 18:38:53 +0000585 DL.getScopeAndInlinedAt(Scope, IA);
Adrian Prantlde200df2015-01-20 22:37:25 +0000586 if (!Scope)
587 return false;
David Blaikie3dfe4782014-10-14 18:22:52 +0000588 assert(!IA);
589 while (!DIDescriptor(Scope).isSubprogram()) {
590 DILexicalBlockFile D(Scope);
591 Scope = D.isLexicalBlockFile()
592 ? D.getScope()
Duncan P. N. Exon Smith5bf8fef2014-12-09 18:38:53 +0000593 : DebugLoc::getFromDILexicalBlock(Scope).getScope();
Adrian Prantl1292e242015-01-21 18:32:56 +0000594 if (!Scope)
595 return false;
David Blaikie3dfe4782014-10-14 18:22:52 +0000596 }
597 if (!DISubprogram(Scope).describes(F))
598 return false;
599 }
600 }
601 }
Duncan P. N. Exon Smith176b6912014-10-03 20:01:09 +0000602 return DbgNode->getNumOperands() == 9 && getNumHeaderFields() == 12;
Bill Wendling523bea82013-11-08 08:13:15 +0000603}
604
Bill Wendling523bea82013-11-08 08:13:15 +0000605bool DIGlobalVariable::Verify() const {
606 if (!isGlobalVariable())
607 return false;
608
609 if (getDisplayName().empty())
610 return false;
Manman Renf0a582b2014-11-21 19:55:23 +0000611 // Make sure context @ field 1 is an MDNode.
612 if (!fieldIsMDNode(DbgNode, 1))
Bill Wendling523bea82013-11-08 08:13:15 +0000613 return false;
Duncan P. N. Exon Smith176b6912014-10-03 20:01:09 +0000614 // Make sure that type @ field 3 is a DITypeRef.
615 if (!fieldIsTypeRef(DbgNode, 3))
Bill Wendling523bea82013-11-08 08:13:15 +0000616 return false;
Duncan P. N. Exon Smith176b6912014-10-03 20:01:09 +0000617 // Make sure StaticDataMemberDeclaration @ field 5 is MDNode.
618 if (!fieldIsMDNode(DbgNode, 5))
Bill Wendling523bea82013-11-08 08:13:15 +0000619 return false;
620
Duncan P. N. Exon Smith176b6912014-10-03 20:01:09 +0000621 return DbgNode->getNumOperands() == 6 && getNumHeaderFields() == 7;
Bill Wendling523bea82013-11-08 08:13:15 +0000622}
623
Bill Wendling523bea82013-11-08 08:13:15 +0000624bool DIVariable::Verify() const {
625 if (!isVariable())
626 return false;
627
Adrian Prantl1a1647c2014-03-18 02:34:58 +0000628 // Make sure context @ field 1 is an MDNode.
Bill Wendling523bea82013-11-08 08:13:15 +0000629 if (!fieldIsMDNode(DbgNode, 1))
630 return false;
Duncan P. N. Exon Smith176b6912014-10-03 20:01:09 +0000631 // Make sure that type @ field 3 is a DITypeRef.
632 if (!fieldIsTypeRef(DbgNode, 3))
633 return false;
634
635 // Check the number of header fields, which is common between complex and
636 // simple variables.
637 if (getNumHeaderFields() != 4)
Bill Wendling523bea82013-11-08 08:13:15 +0000638 return false;
Adrian Prantlda7d92e2014-06-30 17:17:35 +0000639
Adrian Prantl87b7eb92014-10-01 18:55:02 +0000640 // Variable without an inline location.
Duncan P. N. Exon Smith176b6912014-10-03 20:01:09 +0000641 if (DbgNode->getNumOperands() == 4)
Adrian Prantlda7d92e2014-06-30 17:17:35 +0000642 return true;
643
Duncan P. N. Exon Smith176b6912014-10-03 20:01:09 +0000644 // Variable with an inline location.
645 return getInlinedAt() != nullptr && DbgNode->getNumOperands() == 5;
Adrian Prantl87b7eb92014-10-01 18:55:02 +0000646}
647
Adrian Prantl87b7eb92014-10-01 18:55:02 +0000648bool DIExpression::Verify() const {
649 // Empty DIExpressions may be represented as a nullptr.
650 if (!DbgNode)
651 return true;
652
Adrian Prantl9260cca2015-01-22 00:00:52 +0000653 if (!(isExpression() && DbgNode->getNumOperands() == 1))
654 return false;
655
Adrian Prantl70f2a732015-01-23 23:40:47 +0000656 for (auto Op : *this)
657 switch (Op) {
Adrian Prantl27bd01f2015-02-09 23:57:15 +0000658 case DW_OP_bit_piece:
Adrian Prantl9260cca2015-01-22 00:00:52 +0000659 // Must be the last element of the expression.
Adrian Prantl70f2a732015-01-23 23:40:47 +0000660 return std::distance(Op.getBase(), DIHeaderFieldIterator()) == 3;
Adrian Prantl34bcbee2015-01-21 00:59:20 +0000661 case DW_OP_plus:
Adrian Prantl70f2a732015-01-23 23:40:47 +0000662 if (std::distance(Op.getBase(), DIHeaderFieldIterator()) < 2)
Adrian Prantl34bcbee2015-01-21 00:59:20 +0000663 return false;
Adrian Prantl34bcbee2015-01-21 00:59:20 +0000664 break;
Adrian Prantl9260cca2015-01-22 00:00:52 +0000665 case DW_OP_deref:
666 break;
667 default:
668 // Other operators are not yet supported by the backend.
669 return false;
670 }
671 return true;
Bill Wendling523bea82013-11-08 08:13:15 +0000672}
673
Bill Wendling523bea82013-11-08 08:13:15 +0000674bool DILocation::Verify() const {
Duncan P. N. Exon Smith98854692015-01-14 22:27:36 +0000675 return DbgNode && isa<MDLocation>(DbgNode);
Bill Wendling523bea82013-11-08 08:13:15 +0000676}
677
Bill Wendling523bea82013-11-08 08:13:15 +0000678bool DINameSpace::Verify() const {
679 if (!isNameSpace())
680 return false;
Duncan P. N. Exon Smith176b6912014-10-03 20:01:09 +0000681 return DbgNode->getNumOperands() == 3 && getNumHeaderFields() == 3;
Bill Wendling523bea82013-11-08 08:13:15 +0000682}
683
Bill Wendling523bea82013-11-08 08:13:15 +0000684MDNode *DIFile::getFileNode() const { return getNodeField(DbgNode, 1); }
685
Bill Wendling523bea82013-11-08 08:13:15 +0000686bool DIFile::Verify() const {
687 return isFile() && DbgNode->getNumOperands() == 2;
688}
689
Bill Wendling523bea82013-11-08 08:13:15 +0000690bool DIEnumerator::Verify() const {
Duncan P. N. Exon Smith176b6912014-10-03 20:01:09 +0000691 return isEnumerator() && DbgNode->getNumOperands() == 1 &&
692 getNumHeaderFields() == 3;
Bill Wendling523bea82013-11-08 08:13:15 +0000693}
694
Bill Wendling523bea82013-11-08 08:13:15 +0000695bool DISubrange::Verify() const {
Duncan P. N. Exon Smith176b6912014-10-03 20:01:09 +0000696 return isSubrange() && DbgNode->getNumOperands() == 1 &&
697 getNumHeaderFields() == 3;
Bill Wendling523bea82013-11-08 08:13:15 +0000698}
699
Bill Wendling523bea82013-11-08 08:13:15 +0000700bool DILexicalBlock::Verify() const {
Duncan P. N. Exon Smith176b6912014-10-03 20:01:09 +0000701 return isLexicalBlock() && DbgNode->getNumOperands() == 3 &&
702 getNumHeaderFields() == 4;
Bill Wendling523bea82013-11-08 08:13:15 +0000703}
704
Bill Wendling523bea82013-11-08 08:13:15 +0000705bool DILexicalBlockFile::Verify() const {
Duncan P. N. Exon Smith176b6912014-10-03 20:01:09 +0000706 return isLexicalBlockFile() && DbgNode->getNumOperands() == 3 &&
707 getNumHeaderFields() == 2;
Bill Wendling523bea82013-11-08 08:13:15 +0000708}
709
Bill Wendling523bea82013-11-08 08:13:15 +0000710bool DITemplateTypeParameter::Verify() const {
Duncan P. N. Exon Smith176b6912014-10-03 20:01:09 +0000711 return isTemplateTypeParameter() && DbgNode->getNumOperands() == 4 &&
712 getNumHeaderFields() == 4;
Bill Wendling523bea82013-11-08 08:13:15 +0000713}
714
Bill Wendling523bea82013-11-08 08:13:15 +0000715bool DITemplateValueParameter::Verify() const {
Duncan P. N. Exon Smith176b6912014-10-03 20:01:09 +0000716 return isTemplateValueParameter() && DbgNode->getNumOperands() == 5 &&
717 getNumHeaderFields() == 4;
Bill Wendling523bea82013-11-08 08:13:15 +0000718}
719
Bill Wendling523bea82013-11-08 08:13:15 +0000720bool DIImportedEntity::Verify() const {
Duncan P. N. Exon Smith176b6912014-10-03 20:01:09 +0000721 return isImportedEntity() && DbgNode->getNumOperands() == 3 &&
722 getNumHeaderFields() == 3;
Bill Wendling523bea82013-11-08 08:13:15 +0000723}
724
Bill Wendling523bea82013-11-08 08:13:15 +0000725MDNode *DIDerivedType::getObjCProperty() const {
Duncan P. N. Exon Smith176b6912014-10-03 20:01:09 +0000726 return getNodeField(DbgNode, 4);
Bill Wendling523bea82013-11-08 08:13:15 +0000727}
728
729MDString *DICompositeType::getIdentifier() const {
Duncan P. N. Exon Smith176b6912014-10-03 20:01:09 +0000730 return cast_or_null<MDString>(getField(DbgNode, 7));
Bill Wendling523bea82013-11-08 08:13:15 +0000731}
732
733#ifndef NDEBUG
734static void VerifySubsetOf(const MDNode *LHS, const MDNode *RHS) {
735 for (unsigned i = 0; i != LHS->getNumOperands(); ++i) {
736 // Skip the 'empty' list (that's a single i32 0, rather than truly empty).
Duncan P. N. Exon Smith5bf8fef2014-12-09 18:38:53 +0000737 if (i == 0 && mdconst::hasa<ConstantInt>(LHS->getOperand(i)))
Bill Wendling523bea82013-11-08 08:13:15 +0000738 continue;
739 const MDNode *E = cast<MDNode>(LHS->getOperand(i));
740 bool found = false;
741 for (unsigned j = 0; !found && j != RHS->getNumOperands(); ++j)
Hans Wennborge242e8b2014-12-09 20:39:15 +0000742 found = (E == cast<MDNode>(RHS->getOperand(j)));
Bill Wendling523bea82013-11-08 08:13:15 +0000743 assert(found && "Losing a member during member list replacement");
744 }
745}
746#endif
747
Manman Ren1a125c92014-07-28 19:33:20 +0000748void DICompositeType::setArraysHelper(MDNode *Elements, MDNode *TParams) {
Duncan P. N. Exon Smith5bf8fef2014-12-09 18:38:53 +0000749 TrackingMDNodeRef N(*this);
Bill Wendling523bea82013-11-08 08:13:15 +0000750 if (Elements) {
751#ifndef NDEBUG
752 // Check that the new list of members contains all the old members as well.
Duncan P. N. Exon Smith176b6912014-10-03 20:01:09 +0000753 if (const MDNode *El = cast_or_null<MDNode>(N->getOperand(4)))
Bill Wendling523bea82013-11-08 08:13:15 +0000754 VerifySubsetOf(El, Elements);
755#endif
Duncan P. N. Exon Smith176b6912014-10-03 20:01:09 +0000756 N->replaceOperandWith(4, Elements);
Bill Wendling523bea82013-11-08 08:13:15 +0000757 }
758 if (TParams)
Duncan P. N. Exon Smith176b6912014-10-03 20:01:09 +0000759 N->replaceOperandWith(6, TParams);
Bill Wendling523bea82013-11-08 08:13:15 +0000760 DbgNode = N;
761}
762
Bill Wendling523bea82013-11-08 08:13:15 +0000763DIScopeRef DIScope::getRef() const {
764 if (!isCompositeType())
765 return DIScopeRef(*this);
766 DICompositeType DTy(DbgNode);
767 if (!DTy.getIdentifier())
768 return DIScopeRef(*this);
769 return DIScopeRef(DTy.getIdentifier());
770}
771
Bill Wendling523bea82013-11-08 08:13:15 +0000772void DICompositeType::setContainingType(DICompositeType ContainingType) {
Duncan P. N. Exon Smith5bf8fef2014-12-09 18:38:53 +0000773 TrackingMDNodeRef N(*this);
Duncan P. N. Exon Smith176b6912014-10-03 20:01:09 +0000774 N->replaceOperandWith(5, ContainingType.getRef());
Bill Wendling523bea82013-11-08 08:13:15 +0000775 DbgNode = N;
776}
777
Bill Wendling523bea82013-11-08 08:13:15 +0000778bool DIVariable::isInlinedFnArgument(const Function *CurFn) {
779 assert(CurFn && "Invalid function");
780 if (!getContext().isSubprogram())
781 return false;
782 // This variable is not inlined function argument if its scope
783 // does not describe current function.
784 return !DISubprogram(getContext()).describes(CurFn);
785}
786
Bill Wendling523bea82013-11-08 08:13:15 +0000787bool DISubprogram::describes(const Function *F) {
788 assert(F && "Invalid function");
789 if (F == getFunction())
790 return true;
791 StringRef Name = getLinkageName();
792 if (Name.empty())
793 Name = getName();
794 if (F->getName() == Name)
795 return true;
796 return false;
797}
798
Bill Wendling523bea82013-11-08 08:13:15 +0000799MDNode *DISubprogram::getVariablesNodes() const {
Duncan P. N. Exon Smith176b6912014-10-03 20:01:09 +0000800 return getNodeField(DbgNode, 8);
Bill Wendling523bea82013-11-08 08:13:15 +0000801}
802
803DIArray DISubprogram::getVariables() const {
Duncan P. N. Exon Smith176b6912014-10-03 20:01:09 +0000804 return DIArray(getNodeField(DbgNode, 8));
Bill Wendling523bea82013-11-08 08:13:15 +0000805}
806
Duncan P. N. Exon Smith5bf8fef2014-12-09 18:38:53 +0000807Metadata *DITemplateValueParameter::getValue() const {
808 return DbgNode->getOperand(3);
Bill Wendling523bea82013-11-08 08:13:15 +0000809}
810
Bill Wendling523bea82013-11-08 08:13:15 +0000811DIScopeRef DIScope::getContext() const {
812
813 if (isType())
814 return DIType(DbgNode).getContext();
815
816 if (isSubprogram())
817 return DIScopeRef(DISubprogram(DbgNode).getContext());
818
819 if (isLexicalBlock())
820 return DIScopeRef(DILexicalBlock(DbgNode).getContext());
821
822 if (isLexicalBlockFile())
823 return DIScopeRef(DILexicalBlockFile(DbgNode).getContext());
824
825 if (isNameSpace())
826 return DIScopeRef(DINameSpace(DbgNode).getContext());
827
828 assert((isFile() || isCompileUnit()) && "Unhandled type of scope.");
Craig Topperc6207612014-04-09 06:08:46 +0000829 return DIScopeRef(nullptr);
Bill Wendling523bea82013-11-08 08:13:15 +0000830}
831
Bill Wendling523bea82013-11-08 08:13:15 +0000832StringRef DIScope::getName() const {
833 if (isType())
834 return DIType(DbgNode).getName();
835 if (isSubprogram())
836 return DISubprogram(DbgNode).getName();
837 if (isNameSpace())
838 return DINameSpace(DbgNode).getName();
839 assert((isLexicalBlock() || isLexicalBlockFile() || isFile() ||
840 isCompileUnit()) &&
841 "Unhandled type of scope.");
842 return StringRef();
843}
844
845StringRef DIScope::getFilename() const {
846 if (!DbgNode)
847 return StringRef();
848 return ::getStringField(getNodeField(DbgNode, 1), 0);
849}
850
851StringRef DIScope::getDirectory() const {
852 if (!DbgNode)
853 return StringRef();
854 return ::getStringField(getNodeField(DbgNode, 1), 1);
855}
856
857DIArray DICompileUnit::getEnumTypes() const {
Duncan P. N. Exon Smith176b6912014-10-03 20:01:09 +0000858 if (!DbgNode || DbgNode->getNumOperands() < 7)
Bill Wendling523bea82013-11-08 08:13:15 +0000859 return DIArray();
860
Duncan P. N. Exon Smith176b6912014-10-03 20:01:09 +0000861 return DIArray(getNodeField(DbgNode, 2));
Bill Wendling523bea82013-11-08 08:13:15 +0000862}
863
864DIArray DICompileUnit::getRetainedTypes() const {
Duncan P. N. Exon Smith176b6912014-10-03 20:01:09 +0000865 if (!DbgNode || DbgNode->getNumOperands() < 7)
Bill Wendling523bea82013-11-08 08:13:15 +0000866 return DIArray();
867
Duncan P. N. Exon Smith176b6912014-10-03 20:01:09 +0000868 return DIArray(getNodeField(DbgNode, 3));
Bill Wendling523bea82013-11-08 08:13:15 +0000869}
870
871DIArray DICompileUnit::getSubprograms() const {
Duncan P. N. Exon Smith176b6912014-10-03 20:01:09 +0000872 if (!DbgNode || DbgNode->getNumOperands() < 7)
Bill Wendling523bea82013-11-08 08:13:15 +0000873 return DIArray();
874
Duncan P. N. Exon Smith176b6912014-10-03 20:01:09 +0000875 return DIArray(getNodeField(DbgNode, 4));
Bill Wendling523bea82013-11-08 08:13:15 +0000876}
877
878DIArray DICompileUnit::getGlobalVariables() const {
Duncan P. N. Exon Smith176b6912014-10-03 20:01:09 +0000879 if (!DbgNode || DbgNode->getNumOperands() < 7)
Bill Wendling523bea82013-11-08 08:13:15 +0000880 return DIArray();
881
Duncan P. N. Exon Smith176b6912014-10-03 20:01:09 +0000882 return DIArray(getNodeField(DbgNode, 5));
Bill Wendling523bea82013-11-08 08:13:15 +0000883}
884
885DIArray DICompileUnit::getImportedEntities() const {
Duncan P. N. Exon Smith176b6912014-10-03 20:01:09 +0000886 if (!DbgNode || DbgNode->getNumOperands() < 7)
Bill Wendling523bea82013-11-08 08:13:15 +0000887 return DIArray();
888
Duncan P. N. Exon Smith176b6912014-10-03 20:01:09 +0000889 return DIArray(getNodeField(DbgNode, 6));
890}
891
892void DICompileUnit::replaceSubprograms(DIArray Subprograms) {
893 assert(Verify() && "Expected compile unit");
894 if (Subprograms == getSubprograms())
895 return;
896
897 const_cast<MDNode *>(DbgNode)->replaceOperandWith(4, Subprograms);
898}
899
900void DICompileUnit::replaceGlobalVariables(DIArray GlobalVariables) {
901 assert(Verify() && "Expected compile unit");
902 if (GlobalVariables == getGlobalVariables())
903 return;
904
905 const_cast<MDNode *>(DbgNode)->replaceOperandWith(5, GlobalVariables);
Bill Wendling523bea82013-11-08 08:13:15 +0000906}
907
Diego Novillof5041ce2014-03-03 20:06:11 +0000908DILocation DILocation::copyWithNewScope(LLVMContext &Ctx,
David Blaikie2f3f76f2014-08-21 22:45:21 +0000909 DILexicalBlockFile NewScope) {
Diego Novillof5041ce2014-03-03 20:06:11 +0000910 assert(Verify());
Duncan P. N. Exon Smith98854692015-01-14 22:27:36 +0000911 assert(NewScope && "Expected valid scope");
912
913 const auto *Old = cast<MDLocation>(DbgNode);
914 return DILocation(MDLocation::get(Ctx, Old->getLine(), Old->getColumn(),
915 NewScope, Old->getInlinedAt()));
Diego Novillof5041ce2014-03-03 20:06:11 +0000916}
917
Diego Novillof5041ce2014-03-03 20:06:11 +0000918unsigned DILocation::computeNewDiscriminator(LLVMContext &Ctx) {
919 std::pair<const char *, unsigned> Key(getFilename().data(), getLineNumber());
920 return ++Ctx.pImpl->DiscriminatorTable[Key];
921}
922
Bill Wendling523bea82013-11-08 08:13:15 +0000923DIVariable llvm::createInlinedVariable(MDNode *DV, MDNode *InlinedScope,
924 LLVMContext &VMContext) {
Duncan P. N. Exon Smith176b6912014-10-03 20:01:09 +0000925 assert(DIVariable(DV).Verify() && "Expected a DIVariable");
926 if (!InlinedScope)
927 return cleanseInlinedVariable(DV, VMContext);
928
929 // Insert inlined scope.
Benjamin Kramer6cd780f2015-02-17 15:29:18 +0000930 SmallVector<Metadata *, 8> Elts(DV->op_begin(),
931 DV->op_begin() + DIVariableInlinedAtIndex);
Duncan P. N. Exon Smith176b6912014-10-03 20:01:09 +0000932 Elts.push_back(InlinedScope);
933
934 DIVariable Inlined(MDNode::get(VMContext, Elts));
935 assert(Inlined.Verify() && "Expected to create a DIVariable");
936 return Inlined;
Bill Wendling523bea82013-11-08 08:13:15 +0000937}
938
Bill Wendling523bea82013-11-08 08:13:15 +0000939DIVariable llvm::cleanseInlinedVariable(MDNode *DV, LLVMContext &VMContext) {
Duncan P. N. Exon Smith176b6912014-10-03 20:01:09 +0000940 assert(DIVariable(DV).Verify() && "Expected a DIVariable");
941 if (!DIVariable(DV).getInlinedAt())
942 return DIVariable(DV);
943
944 // Remove inlined scope.
Benjamin Kramer6cd780f2015-02-17 15:29:18 +0000945 SmallVector<Metadata *, 8> Elts(DV->op_begin(),
946 DV->op_begin() + DIVariableInlinedAtIndex);
Duncan P. N. Exon Smith176b6912014-10-03 20:01:09 +0000947
948 DIVariable Cleansed(MDNode::get(VMContext, Elts));
949 assert(Cleansed.Verify() && "Expected to create a DIVariable");
950 return Cleansed;
Bill Wendling523bea82013-11-08 08:13:15 +0000951}
952
Bill Wendling523bea82013-11-08 08:13:15 +0000953DISubprogram llvm::getDISubprogram(const MDNode *Scope) {
954 DIDescriptor D(Scope);
955 if (D.isSubprogram())
956 return DISubprogram(Scope);
957
958 if (D.isLexicalBlockFile())
959 return getDISubprogram(DILexicalBlockFile(Scope).getContext());
960
961 if (D.isLexicalBlock())
962 return getDISubprogram(DILexicalBlock(Scope).getContext());
963
964 return DISubprogram();
965}
966
Timur Iskhodzhanoveb229ca2014-10-23 23:46:28 +0000967DISubprogram llvm::getDISubprogram(const Function *F) {
968 // We look for the first instr that has a debug annotation leading back to F.
Timur Iskhodzhanoveb229ca2014-10-23 23:46:28 +0000969 for (auto &BB : *F) {
David Majnemerc758df42014-11-01 07:57:14 +0000970 auto Inst = std::find_if(BB.begin(), BB.end(), [](const Instruction &Inst) {
971 return !Inst.getDebugLoc().isUnknown();
972 });
973 if (Inst == BB.end())
974 continue;
975 DebugLoc DLoc = Inst->getDebugLoc();
Duncan P. N. Exon Smith5bf8fef2014-12-09 18:38:53 +0000976 const MDNode *Scope = DLoc.getScopeNode();
David Majnemerc758df42014-11-01 07:57:14 +0000977 DISubprogram Subprogram = getDISubprogram(Scope);
978 return Subprogram.describes(F) ? Subprogram : DISubprogram();
Timur Iskhodzhanoveb229ca2014-10-23 23:46:28 +0000979 }
980
981 return DISubprogram();
982}
983
Bill Wendling523bea82013-11-08 08:13:15 +0000984DICompositeType llvm::getDICompositeType(DIType T) {
985 if (T.isCompositeType())
986 return DICompositeType(T);
987
988 if (T.isDerivedType()) {
989 // This function is currently used by dragonegg and dragonegg does
990 // not generate identifier for types, so using an empty map to resolve
991 // DerivedFrom should be fine.
992 DITypeIdentifierMap EmptyMap;
993 return getDICompositeType(
994 DIDerivedType(T).getTypeDerivedFrom().resolve(EmptyMap));
995 }
996
997 return DICompositeType();
998}
999
Bill Wendling523bea82013-11-08 08:13:15 +00001000DITypeIdentifierMap
1001llvm::generateDITypeIdentifierMap(const NamedMDNode *CU_Nodes) {
1002 DITypeIdentifierMap Map;
1003 for (unsigned CUi = 0, CUe = CU_Nodes->getNumOperands(); CUi != CUe; ++CUi) {
Duncan P. N. Exon Smithde36e802014-11-11 21:30:22 +00001004 DICompileUnit CU(CU_Nodes->getOperand(CUi));
Bill Wendling523bea82013-11-08 08:13:15 +00001005 DIArray Retain = CU.getRetainedTypes();
1006 for (unsigned Ti = 0, Te = Retain.getNumElements(); Ti != Te; ++Ti) {
1007 if (!Retain.getElement(Ti).isCompositeType())
1008 continue;
1009 DICompositeType Ty(Retain.getElement(Ti));
1010 if (MDString *TypeId = Ty.getIdentifier()) {
1011 // Definition has priority over declaration.
1012 // Try to insert (TypeId, Ty) to Map.
1013 std::pair<DITypeIdentifierMap::iterator, bool> P =
1014 Map.insert(std::make_pair(TypeId, Ty));
1015 // If TypeId already exists in Map and this is a definition, replace
1016 // whatever we had (declaration or definition) with the definition.
1017 if (!P.second && !Ty.isForwardDecl())
1018 P.first->second = Ty;
1019 }
1020 }
1021 }
1022 return Map;
1023}
1024
1025//===----------------------------------------------------------------------===//
1026// DebugInfoFinder implementations.
1027//===----------------------------------------------------------------------===//
1028
1029void DebugInfoFinder::reset() {
1030 CUs.clear();
1031 SPs.clear();
1032 GVs.clear();
1033 TYs.clear();
1034 Scopes.clear();
1035 NodesSeen.clear();
1036 TypeIdentifierMap.clear();
Manman Ren2085ccc2013-11-17 18:42:37 +00001037 TypeMapInitialized = false;
1038}
1039
Manman Renb46e5502013-11-17 19:35:03 +00001040void DebugInfoFinder::InitializeTypeMap(const Module &M) {
Manman Ren2085ccc2013-11-17 18:42:37 +00001041 if (!TypeMapInitialized)
1042 if (NamedMDNode *CU_Nodes = M.getNamedMetadata("llvm.dbg.cu")) {
1043 TypeIdentifierMap = generateDITypeIdentifierMap(CU_Nodes);
1044 TypeMapInitialized = true;
1045 }
Bill Wendling523bea82013-11-08 08:13:15 +00001046}
1047
Bill Wendling523bea82013-11-08 08:13:15 +00001048void DebugInfoFinder::processModule(const Module &M) {
Manman Renb46e5502013-11-17 19:35:03 +00001049 InitializeTypeMap(M);
Bill Wendling523bea82013-11-08 08:13:15 +00001050 if (NamedMDNode *CU_Nodes = M.getNamedMetadata("llvm.dbg.cu")) {
Bill Wendling523bea82013-11-08 08:13:15 +00001051 for (unsigned i = 0, e = CU_Nodes->getNumOperands(); i != e; ++i) {
Duncan P. N. Exon Smithde36e802014-11-11 21:30:22 +00001052 DICompileUnit CU(CU_Nodes->getOperand(i));
Bill Wendling523bea82013-11-08 08:13:15 +00001053 addCompileUnit(CU);
1054 DIArray GVs = CU.getGlobalVariables();
1055 for (unsigned i = 0, e = GVs.getNumElements(); i != e; ++i) {
1056 DIGlobalVariable DIG(GVs.getElement(i));
1057 if (addGlobalVariable(DIG)) {
Manman Renf0a582b2014-11-21 19:55:23 +00001058 processScope(DIG.getContext());
Adrian Prantl1a1647c2014-03-18 02:34:58 +00001059 processType(DIG.getType().resolve(TypeIdentifierMap));
Bill Wendling523bea82013-11-08 08:13:15 +00001060 }
1061 }
1062 DIArray SPs = CU.getSubprograms();
1063 for (unsigned i = 0, e = SPs.getNumElements(); i != e; ++i)
1064 processSubprogram(DISubprogram(SPs.getElement(i)));
1065 DIArray EnumTypes = CU.getEnumTypes();
1066 for (unsigned i = 0, e = EnumTypes.getNumElements(); i != e; ++i)
1067 processType(DIType(EnumTypes.getElement(i)));
1068 DIArray RetainedTypes = CU.getRetainedTypes();
1069 for (unsigned i = 0, e = RetainedTypes.getNumElements(); i != e; ++i)
1070 processType(DIType(RetainedTypes.getElement(i)));
1071 DIArray Imports = CU.getImportedEntities();
1072 for (unsigned i = 0, e = Imports.getNumElements(); i != e; ++i) {
1073 DIImportedEntity Import = DIImportedEntity(Imports.getElement(i));
Adrian Prantld09ba232014-04-01 03:41:04 +00001074 DIDescriptor Entity = Import.getEntity().resolve(TypeIdentifierMap);
Bill Wendling523bea82013-11-08 08:13:15 +00001075 if (Entity.isType())
1076 processType(DIType(Entity));
1077 else if (Entity.isSubprogram())
1078 processSubprogram(DISubprogram(Entity));
1079 else if (Entity.isNameSpace())
1080 processScope(DINameSpace(Entity).getContext());
1081 }
1082 }
1083 }
1084}
1085
Manman Ren2085ccc2013-11-17 18:42:37 +00001086void DebugInfoFinder::processLocation(const Module &M, DILocation Loc) {
Bill Wendling523bea82013-11-08 08:13:15 +00001087 if (!Loc)
1088 return;
Manman Renb46e5502013-11-17 19:35:03 +00001089 InitializeTypeMap(M);
Bill Wendling523bea82013-11-08 08:13:15 +00001090 processScope(Loc.getScope());
Manman Ren2085ccc2013-11-17 18:42:37 +00001091 processLocation(M, Loc.getOrigLocation());
Bill Wendling523bea82013-11-08 08:13:15 +00001092}
1093
Bill Wendling523bea82013-11-08 08:13:15 +00001094void DebugInfoFinder::processType(DIType DT) {
1095 if (!addType(DT))
1096 return;
1097 processScope(DT.getContext().resolve(TypeIdentifierMap));
1098 if (DT.isCompositeType()) {
1099 DICompositeType DCT(DT);
1100 processType(DCT.getTypeDerivedFrom().resolve(TypeIdentifierMap));
Manman Renf8a19672014-07-28 22:24:06 +00001101 if (DT.isSubroutineType()) {
1102 DITypeArray DTA = DISubroutineType(DT).getTypeArray();
1103 for (unsigned i = 0, e = DTA.getNumElements(); i != e; ++i)
1104 processType(DTA.getElement(i).resolve(TypeIdentifierMap));
1105 return;
1106 }
Manman Renab8ffba2014-07-28 19:14:13 +00001107 DIArray DA = DCT.getElements();
Bill Wendling523bea82013-11-08 08:13:15 +00001108 for (unsigned i = 0, e = DA.getNumElements(); i != e; ++i) {
1109 DIDescriptor D = DA.getElement(i);
1110 if (D.isType())
1111 processType(DIType(D));
1112 else if (D.isSubprogram())
1113 processSubprogram(DISubprogram(D));
1114 }
1115 } else if (DT.isDerivedType()) {
1116 DIDerivedType DDT(DT);
1117 processType(DDT.getTypeDerivedFrom().resolve(TypeIdentifierMap));
1118 }
1119}
1120
1121void DebugInfoFinder::processScope(DIScope Scope) {
1122 if (Scope.isType()) {
1123 DIType Ty(Scope);
1124 processType(Ty);
1125 return;
1126 }
1127 if (Scope.isCompileUnit()) {
1128 addCompileUnit(DICompileUnit(Scope));
1129 return;
1130 }
1131 if (Scope.isSubprogram()) {
1132 processSubprogram(DISubprogram(Scope));
1133 return;
1134 }
1135 if (!addScope(Scope))
1136 return;
1137 if (Scope.isLexicalBlock()) {
1138 DILexicalBlock LB(Scope);
1139 processScope(LB.getContext());
1140 } else if (Scope.isLexicalBlockFile()) {
1141 DILexicalBlockFile LBF = DILexicalBlockFile(Scope);
1142 processScope(LBF.getScope());
1143 } else if (Scope.isNameSpace()) {
1144 DINameSpace NS(Scope);
1145 processScope(NS.getContext());
1146 }
1147}
1148
Bill Wendling523bea82013-11-08 08:13:15 +00001149void DebugInfoFinder::processSubprogram(DISubprogram SP) {
1150 if (!addSubprogram(SP))
1151 return;
1152 processScope(SP.getContext().resolve(TypeIdentifierMap));
1153 processType(SP.getType());
1154 DIArray TParams = SP.getTemplateParams();
1155 for (unsigned I = 0, E = TParams.getNumElements(); I != E; ++I) {
1156 DIDescriptor Element = TParams.getElement(I);
1157 if (Element.isTemplateTypeParameter()) {
1158 DITemplateTypeParameter TType(Element);
Bill Wendling523bea82013-11-08 08:13:15 +00001159 processType(TType.getType().resolve(TypeIdentifierMap));
1160 } else if (Element.isTemplateValueParameter()) {
1161 DITemplateValueParameter TVal(Element);
Bill Wendling523bea82013-11-08 08:13:15 +00001162 processType(TVal.getType().resolve(TypeIdentifierMap));
1163 }
1164 }
1165}
1166
Manman Ren2085ccc2013-11-17 18:42:37 +00001167void DebugInfoFinder::processDeclare(const Module &M,
1168 const DbgDeclareInst *DDI) {
Bill Wendling523bea82013-11-08 08:13:15 +00001169 MDNode *N = dyn_cast<MDNode>(DDI->getVariable());
1170 if (!N)
1171 return;
Manman Renb46e5502013-11-17 19:35:03 +00001172 InitializeTypeMap(M);
Bill Wendling523bea82013-11-08 08:13:15 +00001173
1174 DIDescriptor DV(N);
1175 if (!DV.isVariable())
1176 return;
1177
David Blaikie70573dc2014-11-19 07:49:26 +00001178 if (!NodesSeen.insert(DV).second)
Bill Wendling523bea82013-11-08 08:13:15 +00001179 return;
1180 processScope(DIVariable(N).getContext());
Adrian Prantl1a1647c2014-03-18 02:34:58 +00001181 processType(DIVariable(N).getType().resolve(TypeIdentifierMap));
Bill Wendling523bea82013-11-08 08:13:15 +00001182}
1183
Manman Ren2085ccc2013-11-17 18:42:37 +00001184void DebugInfoFinder::processValue(const Module &M, const DbgValueInst *DVI) {
Bill Wendling523bea82013-11-08 08:13:15 +00001185 MDNode *N = dyn_cast<MDNode>(DVI->getVariable());
1186 if (!N)
1187 return;
Manman Renb46e5502013-11-17 19:35:03 +00001188 InitializeTypeMap(M);
Bill Wendling523bea82013-11-08 08:13:15 +00001189
1190 DIDescriptor DV(N);
1191 if (!DV.isVariable())
1192 return;
1193
David Blaikie70573dc2014-11-19 07:49:26 +00001194 if (!NodesSeen.insert(DV).second)
Bill Wendling523bea82013-11-08 08:13:15 +00001195 return;
1196 processScope(DIVariable(N).getContext());
Adrian Prantl1a1647c2014-03-18 02:34:58 +00001197 processType(DIVariable(N).getType().resolve(TypeIdentifierMap));
Bill Wendling523bea82013-11-08 08:13:15 +00001198}
1199
Bill Wendling523bea82013-11-08 08:13:15 +00001200bool DebugInfoFinder::addType(DIType DT) {
1201 if (!DT)
1202 return false;
1203
David Blaikie70573dc2014-11-19 07:49:26 +00001204 if (!NodesSeen.insert(DT).second)
Bill Wendling523bea82013-11-08 08:13:15 +00001205 return false;
1206
1207 TYs.push_back(DT);
1208 return true;
1209}
1210
Bill Wendling523bea82013-11-08 08:13:15 +00001211bool DebugInfoFinder::addCompileUnit(DICompileUnit CU) {
1212 if (!CU)
1213 return false;
David Blaikie70573dc2014-11-19 07:49:26 +00001214 if (!NodesSeen.insert(CU).second)
Bill Wendling523bea82013-11-08 08:13:15 +00001215 return false;
1216
1217 CUs.push_back(CU);
1218 return true;
1219}
1220
Bill Wendling523bea82013-11-08 08:13:15 +00001221bool DebugInfoFinder::addGlobalVariable(DIGlobalVariable DIG) {
1222 if (!DIG)
1223 return false;
1224
David Blaikie70573dc2014-11-19 07:49:26 +00001225 if (!NodesSeen.insert(DIG).second)
Bill Wendling523bea82013-11-08 08:13:15 +00001226 return false;
1227
1228 GVs.push_back(DIG);
1229 return true;
1230}
1231
Bill Wendling523bea82013-11-08 08:13:15 +00001232bool DebugInfoFinder::addSubprogram(DISubprogram SP) {
1233 if (!SP)
1234 return false;
1235
David Blaikie70573dc2014-11-19 07:49:26 +00001236 if (!NodesSeen.insert(SP).second)
Bill Wendling523bea82013-11-08 08:13:15 +00001237 return false;
1238
1239 SPs.push_back(SP);
1240 return true;
1241}
1242
1243bool DebugInfoFinder::addScope(DIScope Scope) {
1244 if (!Scope)
1245 return false;
1246 // FIXME: Ocaml binding generates a scope with no content, we treat it
1247 // as null for now.
1248 if (Scope->getNumOperands() == 0)
1249 return false;
David Blaikie70573dc2014-11-19 07:49:26 +00001250 if (!NodesSeen.insert(Scope).second)
Bill Wendling523bea82013-11-08 08:13:15 +00001251 return false;
1252 Scopes.push_back(Scope);
1253 return true;
1254}
1255
1256//===----------------------------------------------------------------------===//
1257// DIDescriptor: dump routines for all descriptors.
1258//===----------------------------------------------------------------------===//
1259
Bill Wendling523bea82013-11-08 08:13:15 +00001260void DIDescriptor::dump() const {
1261 print(dbgs());
1262 dbgs() << '\n';
1263}
1264
Bill Wendling523bea82013-11-08 08:13:15 +00001265void DIDescriptor::print(raw_ostream &OS) const {
1266 if (!DbgNode)
1267 return;
1268
1269 if (const char *Tag = dwarf::TagString(getTag()))
1270 OS << "[ " << Tag << " ]";
1271
1272 if (this->isSubrange()) {
1273 DISubrange(DbgNode).printInternal(OS);
1274 } else if (this->isCompileUnit()) {
1275 DICompileUnit(DbgNode).printInternal(OS);
1276 } else if (this->isFile()) {
1277 DIFile(DbgNode).printInternal(OS);
1278 } else if (this->isEnumerator()) {
1279 DIEnumerator(DbgNode).printInternal(OS);
1280 } else if (this->isBasicType()) {
1281 DIType(DbgNode).printInternal(OS);
1282 } else if (this->isDerivedType()) {
1283 DIDerivedType(DbgNode).printInternal(OS);
1284 } else if (this->isCompositeType()) {
1285 DICompositeType(DbgNode).printInternal(OS);
1286 } else if (this->isSubprogram()) {
1287 DISubprogram(DbgNode).printInternal(OS);
1288 } else if (this->isGlobalVariable()) {
1289 DIGlobalVariable(DbgNode).printInternal(OS);
1290 } else if (this->isVariable()) {
1291 DIVariable(DbgNode).printInternal(OS);
1292 } else if (this->isObjCProperty()) {
1293 DIObjCProperty(DbgNode).printInternal(OS);
1294 } else if (this->isNameSpace()) {
1295 DINameSpace(DbgNode).printInternal(OS);
1296 } else if (this->isScope()) {
1297 DIScope(DbgNode).printInternal(OS);
Adrian Prantl87b7eb92014-10-01 18:55:02 +00001298 } else if (this->isExpression()) {
1299 DIExpression(DbgNode).printInternal(OS);
Bill Wendling523bea82013-11-08 08:13:15 +00001300 }
1301}
1302
1303void DISubrange::printInternal(raw_ostream &OS) const {
1304 int64_t Count = getCount();
1305 if (Count != -1)
1306 OS << " [" << getLo() << ", " << Count - 1 << ']';
1307 else
1308 OS << " [unbounded]";
1309}
1310
1311void DIScope::printInternal(raw_ostream &OS) const {
1312 OS << " [" << getDirectory() << "/" << getFilename() << ']';
1313}
1314
1315void DICompileUnit::printInternal(raw_ostream &OS) const {
1316 DIScope::printInternal(OS);
1317 OS << " [";
1318 unsigned Lang = getLanguage();
1319 if (const char *LangStr = dwarf::LanguageString(Lang))
1320 OS << LangStr;
1321 else
1322 (OS << "lang 0x").write_hex(Lang);
1323 OS << ']';
1324}
1325
1326void DIEnumerator::printInternal(raw_ostream &OS) const {
1327 OS << " [" << getName() << " :: " << getEnumValue() << ']';
1328}
1329
1330void DIType::printInternal(raw_ostream &OS) const {
Manman Renf93ac4b2014-07-29 18:20:39 +00001331 if (!DbgNode)
Bill Wendling523bea82013-11-08 08:13:15 +00001332 return;
1333
1334 StringRef Res = getName();
1335 if (!Res.empty())
1336 OS << " [" << Res << "]";
1337
1338 // TODO: Print context?
1339
1340 OS << " [line " << getLineNumber() << ", size " << getSizeInBits()
1341 << ", align " << getAlignInBits() << ", offset " << getOffsetInBits();
1342 if (isBasicType())
1343 if (const char *Enc =
1344 dwarf::AttributeEncodingString(DIBasicType(DbgNode).getEncoding()))
1345 OS << ", enc " << Enc;
1346 OS << "]";
1347
1348 if (isPrivate())
1349 OS << " [private]";
1350 else if (isProtected())
1351 OS << " [protected]";
Adrian Prantldaedfda2014-08-29 22:44:07 +00001352 else if (isPublic())
1353 OS << " [public]";
Bill Wendling523bea82013-11-08 08:13:15 +00001354
1355 if (isArtificial())
1356 OS << " [artificial]";
1357
1358 if (isForwardDecl())
1359 OS << " [decl]";
1360 else if (getTag() == dwarf::DW_TAG_structure_type ||
1361 getTag() == dwarf::DW_TAG_union_type ||
1362 getTag() == dwarf::DW_TAG_enumeration_type ||
1363 getTag() == dwarf::DW_TAG_class_type)
1364 OS << " [def]";
1365 if (isVector())
1366 OS << " [vector]";
1367 if (isStaticMember())
1368 OS << " [static]";
Adrian Prantl99c7af22013-12-18 21:48:19 +00001369
1370 if (isLValueReference())
1371 OS << " [reference]";
1372
1373 if (isRValueReference())
1374 OS << " [rvalue reference]";
Bill Wendling523bea82013-11-08 08:13:15 +00001375}
1376
1377void DIDerivedType::printInternal(raw_ostream &OS) const {
1378 DIType::printInternal(OS);
1379 OS << " [from " << getTypeDerivedFrom().getName() << ']';
1380}
1381
1382void DICompositeType::printInternal(raw_ostream &OS) const {
1383 DIType::printInternal(OS);
Manman Renab8ffba2014-07-28 19:14:13 +00001384 DIArray A = getElements();
Bill Wendling523bea82013-11-08 08:13:15 +00001385 OS << " [" << A.getNumElements() << " elements]";
1386}
1387
1388void DINameSpace::printInternal(raw_ostream &OS) const {
1389 StringRef Name = getName();
1390 if (!Name.empty())
1391 OS << " [" << Name << ']';
1392
1393 OS << " [line " << getLineNumber() << ']';
1394}
1395
1396void DISubprogram::printInternal(raw_ostream &OS) const {
1397 // TODO : Print context
1398 OS << " [line " << getLineNumber() << ']';
1399
1400 if (isLocalToUnit())
1401 OS << " [local]";
1402
1403 if (isDefinition())
1404 OS << " [def]";
1405
1406 if (getScopeLineNumber() != getLineNumber())
1407 OS << " [scope " << getScopeLineNumber() << "]";
1408
1409 if (isPrivate())
1410 OS << " [private]";
1411 else if (isProtected())
1412 OS << " [protected]";
Adrian Prantldaedfda2014-08-29 22:44:07 +00001413 else if (isPublic())
1414 OS << " [public]";
Bill Wendling523bea82013-11-08 08:13:15 +00001415
Adrian Prantl99c7af22013-12-18 21:48:19 +00001416 if (isLValueReference())
1417 OS << " [reference]";
1418
1419 if (isRValueReference())
1420 OS << " [rvalue reference]";
1421
Bill Wendling523bea82013-11-08 08:13:15 +00001422 StringRef Res = getName();
1423 if (!Res.empty())
1424 OS << " [" << Res << ']';
1425}
1426
1427void DIGlobalVariable::printInternal(raw_ostream &OS) const {
1428 StringRef Res = getName();
1429 if (!Res.empty())
1430 OS << " [" << Res << ']';
1431
1432 OS << " [line " << getLineNumber() << ']';
1433
1434 // TODO : Print context
1435
1436 if (isLocalToUnit())
1437 OS << " [local]";
1438
1439 if (isDefinition())
1440 OS << " [def]";
1441}
1442
1443void DIVariable::printInternal(raw_ostream &OS) const {
1444 StringRef Res = getName();
1445 if (!Res.empty())
1446 OS << " [" << Res << ']';
1447
1448 OS << " [line " << getLineNumber() << ']';
Adrian Prantl87b7eb92014-10-01 18:55:02 +00001449}
Adrian Prantlb1416832014-08-01 22:11:58 +00001450
Adrian Prantl87b7eb92014-10-01 18:55:02 +00001451void DIExpression::printInternal(raw_ostream &OS) const {
Adrian Prantl4bd0a0c2015-01-23 21:24:41 +00001452 for (auto Op : *this) {
1453 OS << " [" << OperationEncodingString(Op);
1454 switch (Op) {
Adrian Prantl87b7eb92014-10-01 18:55:02 +00001455 case DW_OP_plus: {
Adrian Prantl4bd0a0c2015-01-23 21:24:41 +00001456 OS << " " << Op.getArg(1);
Adrian Prantl87b7eb92014-10-01 18:55:02 +00001457 break;
1458 }
Adrian Prantl27bd01f2015-02-09 23:57:15 +00001459 case DW_OP_bit_piece: {
Adrian Prantl4bd0a0c2015-01-23 21:24:41 +00001460 OS << " offset=" << Op.getArg(1) << ", size=" << Op.getArg(2);
Adrian Prantl87b7eb92014-10-01 18:55:02 +00001461 break;
1462 }
Adrian Prantlb9a88e22014-12-05 18:19:38 +00001463 case DW_OP_deref:
1464 // No arguments.
1465 break;
Adrian Prantl87b7eb92014-10-01 18:55:02 +00001466 default:
Adrian Prantl0d7d8e42015-01-22 16:55:22 +00001467 llvm_unreachable("unhandled operation");
Adrian Prantl87b7eb92014-10-01 18:55:02 +00001468 }
1469 OS << "]";
1470 }
Bill Wendling523bea82013-11-08 08:13:15 +00001471}
1472
1473void DIObjCProperty::printInternal(raw_ostream &OS) const {
1474 StringRef Name = getObjCPropertyName();
1475 if (!Name.empty())
1476 OS << " [" << Name << ']';
1477
1478 OS << " [line " << getLineNumber() << ", properties " << getUnsignedField(6)
1479 << ']';
1480}
1481
1482static void printDebugLoc(DebugLoc DL, raw_ostream &CommentOS,
1483 const LLVMContext &Ctx) {
1484 if (!DL.isUnknown()) { // Print source line info.
1485 DIScope Scope(DL.getScope(Ctx));
1486 assert(Scope.isScope() && "Scope of a DebugLoc should be a DIScope.");
1487 // Omit the directory, because it's likely to be long and uninteresting.
1488 CommentOS << Scope.getFilename();
1489 CommentOS << ':' << DL.getLine();
1490 if (DL.getCol() != 0)
1491 CommentOS << ':' << DL.getCol();
1492 DebugLoc InlinedAtDL = DebugLoc::getFromDILocation(DL.getInlinedAt(Ctx));
1493 if (!InlinedAtDL.isUnknown()) {
1494 CommentOS << " @[ ";
1495 printDebugLoc(InlinedAtDL, CommentOS, Ctx);
1496 CommentOS << " ]";
1497 }
1498 }
1499}
1500
1501void DIVariable::printExtendedName(raw_ostream &OS) const {
1502 const LLVMContext &Ctx = DbgNode->getContext();
1503 StringRef Res = getName();
1504 if (!Res.empty())
1505 OS << Res << "," << getLineNumber();
1506 if (MDNode *InlinedAt = getInlinedAt()) {
1507 DebugLoc InlinedAtDL = DebugLoc::getFromDILocation(InlinedAt);
1508 if (!InlinedAtDL.isUnknown()) {
1509 OS << " @[";
1510 printDebugLoc(InlinedAtDL, OS, Ctx);
1511 OS << "]";
1512 }
1513 }
1514}
1515
Duncan P. N. Exon Smith2a78e9b2015-02-18 19:39:36 +00001516template <> DIRef<DIDescriptor>::DIRef(const Metadata *V) : Val(V) {
1517 assert(isDescriptorRef(V) &&
1518 "DIDescriptorRef should be a MDString or MDNode");
1519}
Duncan P. N. Exon Smithc81307a2014-11-14 23:55:03 +00001520template <> DIRef<DIScope>::DIRef(const Metadata *V) : Val(V) {
Bill Wendling523bea82013-11-08 08:13:15 +00001521 assert(isScopeRef(V) && "DIScopeRef should be a MDString or MDNode");
1522}
Duncan P. N. Exon Smithc81307a2014-11-14 23:55:03 +00001523template <> DIRef<DIType>::DIRef(const Metadata *V) : Val(V) {
Bill Wendling523bea82013-11-08 08:13:15 +00001524 assert(isTypeRef(V) && "DITypeRef should be a MDString or MDNode");
1525}
1526
Bill Wendling523bea82013-11-08 08:13:15 +00001527template <>
Duncan P. N. Exon Smith2a78e9b2015-02-18 19:39:36 +00001528DIDescriptorRef DIDescriptor::getFieldAs<DIDescriptorRef>(unsigned Elt) const {
1529 return DIDescriptorRef(cast_or_null<Metadata>(getField(DbgNode, Elt)));
1530}
1531template <>
Bill Wendling523bea82013-11-08 08:13:15 +00001532DIScopeRef DIDescriptor::getFieldAs<DIScopeRef>(unsigned Elt) const {
Duncan P. N. Exon Smithc81307a2014-11-14 23:55:03 +00001533 return DIScopeRef(cast_or_null<Metadata>(getField(DbgNode, Elt)));
Bill Wendling523bea82013-11-08 08:13:15 +00001534}
Bill Wendling523bea82013-11-08 08:13:15 +00001535template <> DITypeRef DIDescriptor::getFieldAs<DITypeRef>(unsigned Elt) const {
Duncan P. N. Exon Smithc81307a2014-11-14 23:55:03 +00001536 return DITypeRef(cast_or_null<Metadata>(getField(DbgNode, Elt)));
Bill Wendling523bea82013-11-08 08:13:15 +00001537}
Manman Rencb14bbc2013-11-22 22:06:31 +00001538
Manman Rencb14bbc2013-11-22 22:06:31 +00001539bool llvm::StripDebugInfo(Module &M) {
Manman Rencb14bbc2013-11-22 22:06:31 +00001540 bool Changed = false;
1541
1542 // Remove all of the calls to the debugger intrinsics, and remove them from
1543 // the module.
1544 if (Function *Declare = M.getFunction("llvm.dbg.declare")) {
1545 while (!Declare->use_empty()) {
Chandler Carruthcdf47882014-03-09 03:16:01 +00001546 CallInst *CI = cast<CallInst>(Declare->user_back());
Manman Rencb14bbc2013-11-22 22:06:31 +00001547 CI->eraseFromParent();
1548 }
1549 Declare->eraseFromParent();
1550 Changed = true;
1551 }
1552
1553 if (Function *DbgVal = M.getFunction("llvm.dbg.value")) {
1554 while (!DbgVal->use_empty()) {
Chandler Carruthcdf47882014-03-09 03:16:01 +00001555 CallInst *CI = cast<CallInst>(DbgVal->user_back());
Manman Rencb14bbc2013-11-22 22:06:31 +00001556 CI->eraseFromParent();
1557 }
1558 DbgVal->eraseFromParent();
1559 Changed = true;
1560 }
1561
1562 for (Module::named_metadata_iterator NMI = M.named_metadata_begin(),
1563 NME = M.named_metadata_end(); NMI != NME;) {
1564 NamedMDNode *NMD = NMI;
1565 ++NMI;
1566 if (NMD->getName().startswith("llvm.dbg.")) {
1567 NMD->eraseFromParent();
1568 Changed = true;
1569 }
1570 }
1571
1572 for (Module::iterator MI = M.begin(), ME = M.end(); MI != ME; ++MI)
1573 for (Function::iterator FI = MI->begin(), FE = MI->end(); FI != FE;
1574 ++FI)
1575 for (BasicBlock::iterator BI = FI->begin(), BE = FI->end(); BI != BE;
1576 ++BI) {
1577 if (!BI->getDebugLoc().isUnknown()) {
1578 Changed = true;
1579 BI->setDebugLoc(DebugLoc());
1580 }
1581 }
1582
1583 return Changed;
1584}
Manman Ren8b4306c2013-12-02 21:29:56 +00001585
Manman Renbd4daf82013-12-03 00:12:14 +00001586unsigned llvm::getDebugMetadataVersionFromModule(const Module &M) {
David Majnemere7a9cdb2015-02-16 06:04:53 +00001587 if (auto *Val = mdconst::dyn_extract_or_null<ConstantInt>(
Duncan P. N. Exon Smith5bf8fef2014-12-09 18:38:53 +00001588 M.getModuleFlag("Debug Info Version")))
1589 return Val->getZExtValue();
1590 return 0;
Manman Ren8b4306c2013-12-02 21:29:56 +00001591}
David Blaikie6876b3b2014-07-01 20:05:26 +00001592
David Blaikiea8c35092014-07-02 18:30:05 +00001593llvm::DenseMap<const llvm::Function *, llvm::DISubprogram>
1594llvm::makeSubprogramMap(const Module &M) {
1595 DenseMap<const Function *, DISubprogram> R;
David Blaikie6876b3b2014-07-01 20:05:26 +00001596
1597 NamedMDNode *CU_Nodes = M.getNamedMetadata("llvm.dbg.cu");
1598 if (!CU_Nodes)
1599 return R;
1600
Duncan P. N. Exon Smithde36e802014-11-11 21:30:22 +00001601 for (MDNode *N : CU_Nodes->operands()) {
1602 DICompileUnit CUNode(N);
David Blaikie6876b3b2014-07-01 20:05:26 +00001603 DIArray SPs = CUNode.getSubprograms();
1604 for (unsigned i = 0, e = SPs.getNumElements(); i != e; ++i) {
1605 DISubprogram SP(SPs.getElement(i));
1606 if (Function *F = SP.getFunction())
1607 R.insert(std::make_pair(F, SP));
1608 }
1609 }
1610 return R;
1611}