Bill Wendling | 523bea8 | 2013-11-08 08:13:15 +0000 | [diff] [blame] | 1 | //===--- 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 Carruth | 9a4c9e5 | 2014-03-06 00:46:21 +0000 | [diff] [blame] | 15 | #include "llvm/IR/DebugInfo.h" |
Chandler Carruth | 442f784 | 2014-03-04 10:07:28 +0000 | [diff] [blame] | 16 | #include "LLVMContextImpl.h" |
Bill Wendling | 523bea8 | 2013-11-08 08:13:15 +0000 | [diff] [blame] | 17 | #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 Prantl | b141683 | 2014-08-01 22:11:58 +0000 | [diff] [blame] | 22 | #include "llvm/IR/DIBuilder.h" |
Bill Wendling | 523bea8 | 2013-11-08 08:13:15 +0000 | [diff] [blame] | 23 | #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 Carruth | 4220e9c | 2014-03-04 11:17:44 +0000 | [diff] [blame] | 28 | #include "llvm/IR/ValueHandle.h" |
Bill Wendling | 523bea8 | 2013-11-08 08:13:15 +0000 | [diff] [blame] | 29 | #include "llvm/Support/Debug.h" |
| 30 | #include "llvm/Support/Dwarf.h" |
Bill Wendling | 523bea8 | 2013-11-08 08:13:15 +0000 | [diff] [blame] | 31 | #include "llvm/Support/raw_ostream.h" |
| 32 | using namespace llvm; |
| 33 | using namespace llvm::dwarf; |
| 34 | |
| 35 | //===----------------------------------------------------------------------===// |
| 36 | // DIDescriptor |
| 37 | //===----------------------------------------------------------------------===// |
| 38 | |
| 39 | bool 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 Prantl | 87b7eb9 | 2014-10-01 18:55:02 +0000 | [diff] [blame] | 52 | DIImportedEntity(DbgNode).Verify() || DIExpression(DbgNode).Verify()); |
Bill Wendling | 523bea8 | 2013-11-08 08:13:15 +0000 | [diff] [blame] | 53 | } |
| 54 | |
Duncan P. N. Exon Smith | 5bf8fef | 2014-12-09 18:38:53 +0000 | [diff] [blame] | 55 | static Metadata *getField(const MDNode *DbgNode, unsigned Elt) { |
Craig Topper | c620761 | 2014-04-09 06:08:46 +0000 | [diff] [blame] | 56 | if (!DbgNode || Elt >= DbgNode->getNumOperands()) |
| 57 | return nullptr; |
Bill Wendling | 523bea8 | 2013-11-08 08:13:15 +0000 | [diff] [blame] | 58 | return DbgNode->getOperand(Elt); |
| 59 | } |
| 60 | |
| 61 | static MDNode *getNodeField(const MDNode *DbgNode, unsigned Elt) { |
| 62 | return dyn_cast_or_null<MDNode>(getField(DbgNode, Elt)); |
| 63 | } |
| 64 | |
| 65 | static 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 | |
| 71 | StringRef DIDescriptor::getStringField(unsigned Elt) const { |
| 72 | return ::getStringField(DbgNode, Elt); |
| 73 | } |
| 74 | |
| 75 | uint64_t DIDescriptor::getUInt64Field(unsigned Elt) const { |
Duncan P. N. Exon Smith | 5bf8fef | 2014-12-09 18:38:53 +0000 | [diff] [blame] | 76 | if (auto *C = getConstantField(Elt)) |
| 77 | if (ConstantInt *CI = dyn_cast<ConstantInt>(C)) |
Bill Wendling | 523bea8 | 2013-11-08 08:13:15 +0000 | [diff] [blame] | 78 | return CI->getZExtValue(); |
| 79 | |
| 80 | return 0; |
| 81 | } |
| 82 | |
| 83 | int64_t DIDescriptor::getInt64Field(unsigned Elt) const { |
Duncan P. N. Exon Smith | 5bf8fef | 2014-12-09 18:38:53 +0000 | [diff] [blame] | 84 | if (auto *C = getConstantField(Elt)) |
| 85 | if (ConstantInt *CI = dyn_cast<ConstantInt>(C)) |
| 86 | return CI->getZExtValue(); |
Bill Wendling | 523bea8 | 2013-11-08 08:13:15 +0000 | [diff] [blame] | 87 | |
| 88 | return 0; |
| 89 | } |
| 90 | |
| 91 | DIDescriptor DIDescriptor::getDescriptorField(unsigned Elt) const { |
| 92 | MDNode *Field = getNodeField(DbgNode, Elt); |
| 93 | return DIDescriptor(Field); |
| 94 | } |
| 95 | |
| 96 | GlobalVariable *DIDescriptor::getGlobalVariableField(unsigned Elt) const { |
Duncan P. N. Exon Smith | 5bf8fef | 2014-12-09 18:38:53 +0000 | [diff] [blame] | 97 | return dyn_cast_or_null<GlobalVariable>(getConstantField(Elt)); |
Bill Wendling | 523bea8 | 2013-11-08 08:13:15 +0000 | [diff] [blame] | 98 | } |
| 99 | |
| 100 | Constant *DIDescriptor::getConstantField(unsigned Elt) const { |
Craig Topper | c620761 | 2014-04-09 06:08:46 +0000 | [diff] [blame] | 101 | if (!DbgNode) |
| 102 | return nullptr; |
Bill Wendling | 523bea8 | 2013-11-08 08:13:15 +0000 | [diff] [blame] | 103 | |
| 104 | if (Elt < DbgNode->getNumOperands()) |
Duncan P. N. Exon Smith | 5bf8fef | 2014-12-09 18:38:53 +0000 | [diff] [blame] | 105 | if (auto *C = |
| 106 | dyn_cast_or_null<ConstantAsMetadata>(DbgNode->getOperand(Elt))) |
| 107 | return C->getValue(); |
Craig Topper | c620761 | 2014-04-09 06:08:46 +0000 | [diff] [blame] | 108 | return nullptr; |
Bill Wendling | 523bea8 | 2013-11-08 08:13:15 +0000 | [diff] [blame] | 109 | } |
| 110 | |
| 111 | Function *DIDescriptor::getFunctionField(unsigned Elt) const { |
Duncan P. N. Exon Smith | 5bf8fef | 2014-12-09 18:38:53 +0000 | [diff] [blame] | 112 | return dyn_cast_or_null<Function>(getConstantField(Elt)); |
Bill Wendling | 523bea8 | 2013-11-08 08:13:15 +0000 | [diff] [blame] | 113 | } |
| 114 | |
| 115 | void DIDescriptor::replaceFunctionField(unsigned Elt, Function *F) { |
Craig Topper | c620761 | 2014-04-09 06:08:46 +0000 | [diff] [blame] | 116 | if (!DbgNode) |
Bill Wendling | 523bea8 | 2013-11-08 08:13:15 +0000 | [diff] [blame] | 117 | return; |
| 118 | |
| 119 | if (Elt < DbgNode->getNumOperands()) { |
| 120 | MDNode *Node = const_cast<MDNode *>(DbgNode); |
Duncan P. N. Exon Smith | 5bf8fef | 2014-12-09 18:38:53 +0000 | [diff] [blame] | 121 | Node->replaceOperandWith(Elt, F ? ConstantAsMetadata::get(F) : nullptr); |
Bill Wendling | 523bea8 | 2013-11-08 08:13:15 +0000 | [diff] [blame] | 122 | } |
| 123 | } |
| 124 | |
Duncan P. N. Exon Smith | 176b691 | 2014-10-03 20:01:09 +0000 | [diff] [blame] | 125 | static unsigned DIVariableInlinedAtIndex = 4; |
| 126 | MDNode *DIVariable::getInlinedAt() const { |
| 127 | return getNodeField(DbgNode, DIVariableInlinedAtIndex); |
| 128 | } |
Bill Wendling | 523bea8 | 2013-11-08 08:13:15 +0000 | [diff] [blame] | 129 | |
Duncan P. N. Exon Smith | 7f637a9 | 2014-10-15 17:01:28 +0000 | [diff] [blame] | 130 | /// \brief Return the size reported by the variable's type. |
Adrian Prantl | b141683 | 2014-08-01 22:11:58 +0000 | [diff] [blame] | 131 | unsigned 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 Prantl | 87b7eb9 | 2014-10-01 18:55:02 +0000 | [diff] [blame] | 143 | uint64_t DIExpression::getElement(unsigned Idx) const { |
| 144 | unsigned I = Idx + 1; |
Duncan P. N. Exon Smith | 176b691 | 2014-10-03 20:01:09 +0000 | [diff] [blame] | 145 | assert(I < getNumHeaderFields() && |
| 146 | "non-existing complex address element requested"); |
| 147 | return getHeaderFieldAs<int64_t>(I); |
Adrian Prantl | 87b7eb9 | 2014-10-01 18:55:02 +0000 | [diff] [blame] | 148 | } |
Adrian Prantl | b141683 | 2014-08-01 22:11:58 +0000 | [diff] [blame] | 149 | |
Adrian Prantl | 27bd01f | 2015-02-09 23:57:15 +0000 | [diff] [blame] | 150 | bool DIExpression::isBitPiece() const { |
Adrian Prantl | 34bcbee | 2015-01-21 00:59:20 +0000 | [diff] [blame] | 151 | unsigned N = getNumElements(); |
Adrian Prantl | 27bd01f | 2015-02-09 23:57:15 +0000 | [diff] [blame] | 152 | return N >=3 && getElement(N-3) == dwarf::DW_OP_bit_piece; |
Adrian Prantl | 87b7eb9 | 2014-10-01 18:55:02 +0000 | [diff] [blame] | 153 | } |
| 154 | |
Adrian Prantl | 27bd01f | 2015-02-09 23:57:15 +0000 | [diff] [blame] | 155 | uint64_t DIExpression::getBitPieceOffset() const { |
| 156 | assert(isBitPiece() && "not a piece"); |
Adrian Prantl | 34bcbee | 2015-01-21 00:59:20 +0000 | [diff] [blame] | 157 | return getElement(getNumElements()-2); |
Adrian Prantl | 87b7eb9 | 2014-10-01 18:55:02 +0000 | [diff] [blame] | 158 | } |
| 159 | |
Adrian Prantl | 27bd01f | 2015-02-09 23:57:15 +0000 | [diff] [blame] | 160 | uint64_t DIExpression::getBitPieceSize() const { |
| 161 | assert(isBitPiece() && "not a piece"); |
Adrian Prantl | 34bcbee | 2015-01-21 00:59:20 +0000 | [diff] [blame] | 162 | return getElement(getNumElements()-1); |
Adrian Prantl | 87b7eb9 | 2014-10-01 18:55:02 +0000 | [diff] [blame] | 163 | } |
Adrian Prantl | b141683 | 2014-08-01 22:11:58 +0000 | [diff] [blame] | 164 | |
Adrian Prantl | 2585a98 | 2015-01-22 16:55:20 +0000 | [diff] [blame] | 165 | DIExpression::iterator DIExpression::begin() const { |
| 166 | return DIExpression::iterator(*this); |
Adrian Prantl | 9260cca | 2015-01-22 00:00:52 +0000 | [diff] [blame] | 167 | } |
| 168 | |
Adrian Prantl | 2585a98 | 2015-01-22 16:55:20 +0000 | [diff] [blame] | 169 | DIExpression::iterator DIExpression::end() const { |
| 170 | return DIExpression::iterator(); |
Adrian Prantl | 9260cca | 2015-01-22 00:00:52 +0000 | [diff] [blame] | 171 | } |
| 172 | |
Benjamin Kramer | 4d50b6c | 2015-01-24 19:55:23 +0000 | [diff] [blame] | 173 | DIExpression::Operand DIExpression::Operand::getNext() const { |
Adrian Prantl | 70f2a73 | 2015-01-23 23:40:47 +0000 | [diff] [blame] | 174 | iterator it(I); |
| 175 | return *(++it); |
| 176 | } |
| 177 | |
Bill Wendling | 523bea8 | 2013-11-08 08:13:15 +0000 | [diff] [blame] | 178 | //===----------------------------------------------------------------------===// |
| 179 | // Predicates |
| 180 | //===----------------------------------------------------------------------===// |
| 181 | |
Manman Ren | f8a1967 | 2014-07-28 22:24:06 +0000 | [diff] [blame] | 182 | bool DIDescriptor::isSubroutineType() const { |
Yaron Keren | e827022 | 2014-12-19 22:15:09 +0000 | [diff] [blame] | 183 | return DbgNode && getTag() == dwarf::DW_TAG_subroutine_type; |
Manman Ren | f8a1967 | 2014-07-28 22:24:06 +0000 | [diff] [blame] | 184 | } |
| 185 | |
Bill Wendling | 523bea8 | 2013-11-08 08:13:15 +0000 | [diff] [blame] | 186 | bool DIDescriptor::isBasicType() const { |
| 187 | if (!DbgNode) |
| 188 | return false; |
| 189 | switch (getTag()) { |
| 190 | case dwarf::DW_TAG_base_type: |
| 191 | case dwarf::DW_TAG_unspecified_type: |
| 192 | return true; |
| 193 | default: |
| 194 | return false; |
| 195 | } |
| 196 | } |
| 197 | |
Bill Wendling | 523bea8 | 2013-11-08 08:13:15 +0000 | [diff] [blame] | 198 | bool DIDescriptor::isDerivedType() const { |
| 199 | if (!DbgNode) |
| 200 | return false; |
| 201 | switch (getTag()) { |
| 202 | case dwarf::DW_TAG_typedef: |
| 203 | case dwarf::DW_TAG_pointer_type: |
| 204 | case dwarf::DW_TAG_ptr_to_member_type: |
| 205 | case dwarf::DW_TAG_reference_type: |
| 206 | case dwarf::DW_TAG_rvalue_reference_type: |
| 207 | case dwarf::DW_TAG_const_type: |
| 208 | case dwarf::DW_TAG_volatile_type: |
| 209 | case dwarf::DW_TAG_restrict_type: |
| 210 | case dwarf::DW_TAG_member: |
| 211 | case dwarf::DW_TAG_inheritance: |
| 212 | case dwarf::DW_TAG_friend: |
| 213 | return true; |
| 214 | default: |
| 215 | // CompositeTypes are currently modelled as DerivedTypes. |
| 216 | return isCompositeType(); |
| 217 | } |
| 218 | } |
| 219 | |
Bill Wendling | 523bea8 | 2013-11-08 08:13:15 +0000 | [diff] [blame] | 220 | bool DIDescriptor::isCompositeType() const { |
| 221 | if (!DbgNode) |
| 222 | return false; |
| 223 | switch (getTag()) { |
| 224 | case dwarf::DW_TAG_array_type: |
| 225 | case dwarf::DW_TAG_structure_type: |
| 226 | case dwarf::DW_TAG_union_type: |
| 227 | case dwarf::DW_TAG_enumeration_type: |
| 228 | case dwarf::DW_TAG_subroutine_type: |
| 229 | case dwarf::DW_TAG_class_type: |
| 230 | return true; |
| 231 | default: |
| 232 | return false; |
| 233 | } |
| 234 | } |
| 235 | |
Bill Wendling | 523bea8 | 2013-11-08 08:13:15 +0000 | [diff] [blame] | 236 | bool DIDescriptor::isVariable() const { |
| 237 | if (!DbgNode) |
| 238 | return false; |
| 239 | switch (getTag()) { |
| 240 | case dwarf::DW_TAG_auto_variable: |
| 241 | case dwarf::DW_TAG_arg_variable: |
| 242 | return true; |
| 243 | default: |
| 244 | return false; |
| 245 | } |
| 246 | } |
| 247 | |
Bill Wendling | 523bea8 | 2013-11-08 08:13:15 +0000 | [diff] [blame] | 248 | bool DIDescriptor::isType() const { |
Manman Ren | f93ac4b | 2014-07-29 18:20:39 +0000 | [diff] [blame] | 249 | return isBasicType() || isCompositeType() || isDerivedType(); |
Bill Wendling | 523bea8 | 2013-11-08 08:13:15 +0000 | [diff] [blame] | 250 | } |
| 251 | |
Bill Wendling | 523bea8 | 2013-11-08 08:13:15 +0000 | [diff] [blame] | 252 | bool DIDescriptor::isSubprogram() const { |
| 253 | return DbgNode && getTag() == dwarf::DW_TAG_subprogram; |
| 254 | } |
| 255 | |
Bill Wendling | 523bea8 | 2013-11-08 08:13:15 +0000 | [diff] [blame] | 256 | bool DIDescriptor::isGlobalVariable() const { |
Duncan P. N. Exon Smith | b407bb2 | 2015-02-09 22:48:04 +0000 | [diff] [blame] | 257 | return DbgNode && getTag() == dwarf::DW_TAG_variable; |
Bill Wendling | 523bea8 | 2013-11-08 08:13:15 +0000 | [diff] [blame] | 258 | } |
| 259 | |
Bill Wendling | 523bea8 | 2013-11-08 08:13:15 +0000 | [diff] [blame] | 260 | bool DIDescriptor::isScope() const { |
| 261 | if (!DbgNode) |
| 262 | return false; |
| 263 | switch (getTag()) { |
| 264 | case dwarf::DW_TAG_compile_unit: |
| 265 | case dwarf::DW_TAG_lexical_block: |
| 266 | case dwarf::DW_TAG_subprogram: |
| 267 | case dwarf::DW_TAG_namespace: |
| 268 | case dwarf::DW_TAG_file_type: |
| 269 | return true; |
| 270 | default: |
| 271 | break; |
| 272 | } |
| 273 | return isType(); |
| 274 | } |
| 275 | |
Bill Wendling | 523bea8 | 2013-11-08 08:13:15 +0000 | [diff] [blame] | 276 | bool DIDescriptor::isTemplateTypeParameter() const { |
| 277 | return DbgNode && getTag() == dwarf::DW_TAG_template_type_parameter; |
| 278 | } |
| 279 | |
Bill Wendling | 523bea8 | 2013-11-08 08:13:15 +0000 | [diff] [blame] | 280 | bool DIDescriptor::isTemplateValueParameter() const { |
| 281 | return DbgNode && (getTag() == dwarf::DW_TAG_template_value_parameter || |
| 282 | getTag() == dwarf::DW_TAG_GNU_template_template_param || |
| 283 | getTag() == dwarf::DW_TAG_GNU_template_parameter_pack); |
| 284 | } |
| 285 | |
Bill Wendling | 523bea8 | 2013-11-08 08:13:15 +0000 | [diff] [blame] | 286 | bool DIDescriptor::isCompileUnit() const { |
| 287 | return DbgNode && getTag() == dwarf::DW_TAG_compile_unit; |
| 288 | } |
| 289 | |
Bill Wendling | 523bea8 | 2013-11-08 08:13:15 +0000 | [diff] [blame] | 290 | bool DIDescriptor::isFile() const { |
| 291 | return DbgNode && getTag() == dwarf::DW_TAG_file_type; |
| 292 | } |
| 293 | |
Bill Wendling | 523bea8 | 2013-11-08 08:13:15 +0000 | [diff] [blame] | 294 | bool DIDescriptor::isNameSpace() const { |
| 295 | return DbgNode && getTag() == dwarf::DW_TAG_namespace; |
| 296 | } |
| 297 | |
Bill Wendling | 523bea8 | 2013-11-08 08:13:15 +0000 | [diff] [blame] | 298 | bool DIDescriptor::isLexicalBlockFile() const { |
| 299 | return DbgNode && getTag() == dwarf::DW_TAG_lexical_block && |
Duncan P. N. Exon Smith | 176b691 | 2014-10-03 20:01:09 +0000 | [diff] [blame] | 300 | DbgNode->getNumOperands() == 3 && getNumHeaderFields() == 2; |
Bill Wendling | 523bea8 | 2013-11-08 08:13:15 +0000 | [diff] [blame] | 301 | } |
| 302 | |
Bill Wendling | 523bea8 | 2013-11-08 08:13:15 +0000 | [diff] [blame] | 303 | bool DIDescriptor::isLexicalBlock() const { |
Duncan P. N. Exon Smith | 176b691 | 2014-10-03 20:01:09 +0000 | [diff] [blame] | 304 | // FIXME: There are always exactly 4 header fields in DILexicalBlock, but |
| 305 | // something relies on this returning true for DILexicalBlockFile. |
Bill Wendling | 523bea8 | 2013-11-08 08:13:15 +0000 | [diff] [blame] | 306 | return DbgNode && getTag() == dwarf::DW_TAG_lexical_block && |
Duncan P. N. Exon Smith | 176b691 | 2014-10-03 20:01:09 +0000 | [diff] [blame] | 307 | DbgNode->getNumOperands() == 3 && |
| 308 | (getNumHeaderFields() == 2 || getNumHeaderFields() == 4); |
Bill Wendling | 523bea8 | 2013-11-08 08:13:15 +0000 | [diff] [blame] | 309 | } |
| 310 | |
Bill Wendling | 523bea8 | 2013-11-08 08:13:15 +0000 | [diff] [blame] | 311 | bool DIDescriptor::isSubrange() const { |
| 312 | return DbgNode && getTag() == dwarf::DW_TAG_subrange_type; |
| 313 | } |
| 314 | |
Bill Wendling | 523bea8 | 2013-11-08 08:13:15 +0000 | [diff] [blame] | 315 | bool DIDescriptor::isEnumerator() const { |
| 316 | return DbgNode && getTag() == dwarf::DW_TAG_enumerator; |
| 317 | } |
| 318 | |
Bill Wendling | 523bea8 | 2013-11-08 08:13:15 +0000 | [diff] [blame] | 319 | bool DIDescriptor::isObjCProperty() const { |
| 320 | return DbgNode && getTag() == dwarf::DW_TAG_APPLE_property; |
| 321 | } |
| 322 | |
Bill Wendling | 523bea8 | 2013-11-08 08:13:15 +0000 | [diff] [blame] | 323 | bool DIDescriptor::isImportedEntity() const { |
| 324 | return DbgNode && (getTag() == dwarf::DW_TAG_imported_module || |
| 325 | getTag() == dwarf::DW_TAG_imported_declaration); |
| 326 | } |
| 327 | |
Adrian Prantl | 87b7eb9 | 2014-10-01 18:55:02 +0000 | [diff] [blame] | 328 | bool DIDescriptor::isExpression() const { |
| 329 | return DbgNode && (getTag() == dwarf::DW_TAG_expression); |
| 330 | } |
| 331 | |
Bill Wendling | 523bea8 | 2013-11-08 08:13:15 +0000 | [diff] [blame] | 332 | //===----------------------------------------------------------------------===// |
| 333 | // Simple Descriptor Constructors and other Methods |
| 334 | //===----------------------------------------------------------------------===// |
| 335 | |
Frederic Riss | 36acf0f | 2014-09-15 07:50:36 +0000 | [diff] [blame] | 336 | void DIDescriptor::replaceAllUsesWith(LLVMContext &VMContext, DIDescriptor D) { |
Bill Wendling | 523bea8 | 2013-11-08 08:13:15 +0000 | [diff] [blame] | 337 | |
| 338 | assert(DbgNode && "Trying to replace an unverified type!"); |
| 339 | |
| 340 | // Since we use a TrackingVH for the node, its easy for clients to manufacture |
| 341 | // legitimate situations where they want to replaceAllUsesWith() on something |
| 342 | // which, due to uniquing, has merged with the source. We shield clients from |
| 343 | // this detail by allowing a value to be replaced with replaceAllUsesWith() |
| 344 | // itself. |
David Blaikie | d3f094a | 2014-05-06 03:41:57 +0000 | [diff] [blame] | 345 | const MDNode *DN = D; |
| 346 | if (DbgNode == DN) { |
Duncan P. N. Exon Smith | 5bf8fef | 2014-12-09 18:38:53 +0000 | [diff] [blame] | 347 | SmallVector<Metadata *, 10> Ops(DbgNode->getNumOperands()); |
David Blaikie | d3f094a | 2014-05-06 03:41:57 +0000 | [diff] [blame] | 348 | for (size_t i = 0; i != Ops.size(); ++i) |
| 349 | Ops[i] = DbgNode->getOperand(i); |
| 350 | DN = MDNode::get(VMContext, Ops); |
Bill Wendling | 523bea8 | 2013-11-08 08:13:15 +0000 | [diff] [blame] | 351 | } |
David Blaikie | d3f094a | 2014-05-06 03:41:57 +0000 | [diff] [blame] | 352 | |
Duncan P. N. Exon Smith | 946fdcc | 2015-01-19 20:36:39 +0000 | [diff] [blame] | 353 | assert(DbgNode->isTemporary() && "Expected temporary node"); |
| 354 | auto *Node = const_cast<MDNode *>(DbgNode); |
Duncan P. N. Exon Smith | 5bf8fef | 2014-12-09 18:38:53 +0000 | [diff] [blame] | 355 | Node->replaceAllUsesWith(const_cast<MDNode *>(DN)); |
David Blaikie | d3f094a | 2014-05-06 03:41:57 +0000 | [diff] [blame] | 356 | MDNode::deleteTemporary(Node); |
Frederic Riss | dd7aec5 | 2014-09-15 07:50:42 +0000 | [diff] [blame] | 357 | DbgNode = DN; |
Bill Wendling | 523bea8 | 2013-11-08 08:13:15 +0000 | [diff] [blame] | 358 | } |
| 359 | |
Frederic Riss | 36acf0f | 2014-09-15 07:50:36 +0000 | [diff] [blame] | 360 | void DIDescriptor::replaceAllUsesWith(MDNode *D) { |
Bill Wendling | 523bea8 | 2013-11-08 08:13:15 +0000 | [diff] [blame] | 361 | assert(DbgNode && "Trying to replace an unverified type!"); |
David Blaikie | d3f094a | 2014-05-06 03:41:57 +0000 | [diff] [blame] | 362 | assert(DbgNode != D && "This replacement should always happen"); |
Duncan P. N. Exon Smith | 946fdcc | 2015-01-19 20:36:39 +0000 | [diff] [blame] | 363 | assert(DbgNode->isTemporary() && "Expected temporary node"); |
| 364 | auto *Node = const_cast<MDNode *>(DbgNode); |
Duncan P. N. Exon Smith | 5bf8fef | 2014-12-09 18:38:53 +0000 | [diff] [blame] | 365 | Node->replaceAllUsesWith(D); |
David Blaikie | d3f094a | 2014-05-06 03:41:57 +0000 | [diff] [blame] | 366 | MDNode::deleteTemporary(Node); |
Bill Wendling | 523bea8 | 2013-11-08 08:13:15 +0000 | [diff] [blame] | 367 | } |
| 368 | |
Bill Wendling | 523bea8 | 2013-11-08 08:13:15 +0000 | [diff] [blame] | 369 | bool DICompileUnit::Verify() const { |
| 370 | if (!isCompileUnit()) |
| 371 | return false; |
| 372 | |
| 373 | // Don't bother verifying the compilation directory or producer string |
| 374 | // as those could be empty. |
| 375 | if (getFilename().empty()) |
| 376 | return false; |
| 377 | |
Duncan P. N. Exon Smith | 176b691 | 2014-10-03 20:01:09 +0000 | [diff] [blame] | 378 | return DbgNode->getNumOperands() == 7 && getNumHeaderFields() == 8; |
Bill Wendling | 523bea8 | 2013-11-08 08:13:15 +0000 | [diff] [blame] | 379 | } |
| 380 | |
Bill Wendling | 523bea8 | 2013-11-08 08:13:15 +0000 | [diff] [blame] | 381 | bool DIObjCProperty::Verify() const { |
| 382 | if (!isObjCProperty()) |
| 383 | return false; |
| 384 | |
| 385 | // Don't worry about the rest of the strings for now. |
Duncan P. N. Exon Smith | 176b691 | 2014-10-03 20:01:09 +0000 | [diff] [blame] | 386 | return DbgNode->getNumOperands() == 3 && getNumHeaderFields() == 6; |
Bill Wendling | 523bea8 | 2013-11-08 08:13:15 +0000 | [diff] [blame] | 387 | } |
| 388 | |
Duncan P. N. Exon Smith | 7f637a9 | 2014-10-15 17:01:28 +0000 | [diff] [blame] | 389 | /// \brief Check if a field at position Elt of a MDNode is a MDNode. |
| 390 | /// |
Bill Wendling | 523bea8 | 2013-11-08 08:13:15 +0000 | [diff] [blame] | 391 | /// We currently allow an empty string and an integer. |
| 392 | /// But we don't allow a non-empty string in a MDNode field. |
| 393 | static bool fieldIsMDNode(const MDNode *DbgNode, unsigned Elt) { |
| 394 | // FIXME: This function should return true, if the field is null or the field |
| 395 | // is indeed a MDNode: return !Fld || isa<MDNode>(Fld). |
Duncan P. N. Exon Smith | 5bf8fef | 2014-12-09 18:38:53 +0000 | [diff] [blame] | 396 | Metadata *Fld = getField(DbgNode, Elt); |
Bill Wendling | 523bea8 | 2013-11-08 08:13:15 +0000 | [diff] [blame] | 397 | if (Fld && isa<MDString>(Fld) && !cast<MDString>(Fld)->getString().empty()) |
| 398 | return false; |
| 399 | return true; |
| 400 | } |
| 401 | |
Duncan P. N. Exon Smith | 7f637a9 | 2014-10-15 17:01:28 +0000 | [diff] [blame] | 402 | /// \brief Check if a field at position Elt of a MDNode is a MDString. |
Bill Wendling | 523bea8 | 2013-11-08 08:13:15 +0000 | [diff] [blame] | 403 | static bool fieldIsMDString(const MDNode *DbgNode, unsigned Elt) { |
Duncan P. N. Exon Smith | 5bf8fef | 2014-12-09 18:38:53 +0000 | [diff] [blame] | 404 | Metadata *Fld = getField(DbgNode, Elt); |
Bill Wendling | 523bea8 | 2013-11-08 08:13:15 +0000 | [diff] [blame] | 405 | return !Fld || isa<MDString>(Fld); |
| 406 | } |
| 407 | |
Duncan P. N. Exon Smith | 7f637a9 | 2014-10-15 17:01:28 +0000 | [diff] [blame] | 408 | /// \brief Check if a value can be a reference to a type. |
Duncan P. N. Exon Smith | c81307a | 2014-11-14 23:55:03 +0000 | [diff] [blame] | 409 | static bool isTypeRef(const Metadata *MD) { |
| 410 | if (!MD) |
| 411 | return true; |
| 412 | if (auto *S = dyn_cast<MDString>(MD)) |
| 413 | return !S->getString().empty(); |
| 414 | if (auto *N = dyn_cast<MDNode>(MD)) |
| 415 | return DIType(N).isType(); |
| 416 | return false; |
Bill Wendling | 523bea8 | 2013-11-08 08:13:15 +0000 | [diff] [blame] | 417 | } |
| 418 | |
Duncan P. N. Exon Smith | 7f637a9 | 2014-10-15 17:01:28 +0000 | [diff] [blame] | 419 | /// \brief Check if referenced field might be a type. |
Bill Wendling | 523bea8 | 2013-11-08 08:13:15 +0000 | [diff] [blame] | 420 | static bool fieldIsTypeRef(const MDNode *DbgNode, unsigned Elt) { |
Duncan P. N. Exon Smith | c81307a | 2014-11-14 23:55:03 +0000 | [diff] [blame] | 421 | return isTypeRef(dyn_cast_or_null<Metadata>(getField(DbgNode, Elt))); |
Bill Wendling | 523bea8 | 2013-11-08 08:13:15 +0000 | [diff] [blame] | 422 | } |
| 423 | |
Duncan P. N. Exon Smith | 7f637a9 | 2014-10-15 17:01:28 +0000 | [diff] [blame] | 424 | /// \brief Check if a value can be a ScopeRef. |
Duncan P. N. Exon Smith | c81307a | 2014-11-14 23:55:03 +0000 | [diff] [blame] | 425 | static bool isScopeRef(const Metadata *MD) { |
| 426 | if (!MD) |
| 427 | return true; |
| 428 | if (auto *S = dyn_cast<MDString>(MD)) |
| 429 | return !S->getString().empty(); |
| 430 | return isa<MDNode>(MD); |
Bill Wendling | 523bea8 | 2013-11-08 08:13:15 +0000 | [diff] [blame] | 431 | } |
| 432 | |
Duncan P. N. Exon Smith | 7f637a9 | 2014-10-15 17:01:28 +0000 | [diff] [blame] | 433 | /// \brief Check if a field at position Elt of a MDNode can be a ScopeRef. |
Bill Wendling | 523bea8 | 2013-11-08 08:13:15 +0000 | [diff] [blame] | 434 | static bool fieldIsScopeRef(const MDNode *DbgNode, unsigned Elt) { |
Duncan P. N. Exon Smith | c81307a | 2014-11-14 23:55:03 +0000 | [diff] [blame] | 435 | return isScopeRef(dyn_cast_or_null<Metadata>(getField(DbgNode, Elt))); |
Bill Wendling | 523bea8 | 2013-11-08 08:13:15 +0000 | [diff] [blame] | 436 | } |
| 437 | |
Bill Wendling | 523bea8 | 2013-11-08 08:13:15 +0000 | [diff] [blame] | 438 | bool DIType::Verify() const { |
| 439 | if (!isType()) |
| 440 | return false; |
| 441 | // Make sure Context @ field 2 is MDNode. |
| 442 | if (!fieldIsScopeRef(DbgNode, 2)) |
| 443 | return false; |
| 444 | |
| 445 | // FIXME: Sink this into the various subclass verifies. |
| 446 | uint16_t Tag = getTag(); |
Manman Ren | f93ac4b | 2014-07-29 18:20:39 +0000 | [diff] [blame] | 447 | if (!isBasicType() && Tag != dwarf::DW_TAG_const_type && |
Bill Wendling | 523bea8 | 2013-11-08 08:13:15 +0000 | [diff] [blame] | 448 | Tag != dwarf::DW_TAG_volatile_type && Tag != dwarf::DW_TAG_pointer_type && |
| 449 | Tag != dwarf::DW_TAG_ptr_to_member_type && |
| 450 | Tag != dwarf::DW_TAG_reference_type && |
| 451 | Tag != dwarf::DW_TAG_rvalue_reference_type && |
| 452 | Tag != dwarf::DW_TAG_restrict_type && Tag != dwarf::DW_TAG_array_type && |
| 453 | Tag != dwarf::DW_TAG_enumeration_type && |
| 454 | Tag != dwarf::DW_TAG_subroutine_type && |
| 455 | Tag != dwarf::DW_TAG_inheritance && Tag != dwarf::DW_TAG_friend && |
| 456 | getFilename().empty()) |
| 457 | return false; |
Adrian Prantl | daedfda | 2014-08-29 22:44:07 +0000 | [diff] [blame] | 458 | |
Bill Wendling | 523bea8 | 2013-11-08 08:13:15 +0000 | [diff] [blame] | 459 | // DIType is abstract, it should be a BasicType, a DerivedType or |
| 460 | // a CompositeType. |
| 461 | if (isBasicType()) |
Renato Golin | 47f46fd | 2013-11-26 16:47:00 +0000 | [diff] [blame] | 462 | return DIBasicType(DbgNode).Verify(); |
Bill Wendling | 523bea8 | 2013-11-08 08:13:15 +0000 | [diff] [blame] | 463 | else if (isCompositeType()) |
Renato Golin | 47f46fd | 2013-11-26 16:47:00 +0000 | [diff] [blame] | 464 | return DICompositeType(DbgNode).Verify(); |
Bill Wendling | 523bea8 | 2013-11-08 08:13:15 +0000 | [diff] [blame] | 465 | else if (isDerivedType()) |
Renato Golin | 47f46fd | 2013-11-26 16:47:00 +0000 | [diff] [blame] | 466 | return DIDerivedType(DbgNode).Verify(); |
Bill Wendling | 523bea8 | 2013-11-08 08:13:15 +0000 | [diff] [blame] | 467 | else |
| 468 | return false; |
Bill Wendling | 523bea8 | 2013-11-08 08:13:15 +0000 | [diff] [blame] | 469 | } |
| 470 | |
Bill Wendling | 523bea8 | 2013-11-08 08:13:15 +0000 | [diff] [blame] | 471 | bool DIBasicType::Verify() const { |
Duncan P. N. Exon Smith | 176b691 | 2014-10-03 20:01:09 +0000 | [diff] [blame] | 472 | return isBasicType() && DbgNode->getNumOperands() == 3 && |
| 473 | getNumHeaderFields() == 8; |
Bill Wendling | 523bea8 | 2013-11-08 08:13:15 +0000 | [diff] [blame] | 474 | } |
| 475 | |
Bill Wendling | 523bea8 | 2013-11-08 08:13:15 +0000 | [diff] [blame] | 476 | bool DIDerivedType::Verify() const { |
Duncan P. N. Exon Smith | 176b691 | 2014-10-03 20:01:09 +0000 | [diff] [blame] | 477 | // Make sure DerivedFrom @ field 3 is TypeRef. |
| 478 | if (!fieldIsTypeRef(DbgNode, 3)) |
Bill Wendling | 523bea8 | 2013-11-08 08:13:15 +0000 | [diff] [blame] | 479 | return false; |
| 480 | if (getTag() == dwarf::DW_TAG_ptr_to_member_type) |
Duncan P. N. Exon Smith | 176b691 | 2014-10-03 20:01:09 +0000 | [diff] [blame] | 481 | // Make sure ClassType @ field 4 is a TypeRef. |
| 482 | if (!fieldIsTypeRef(DbgNode, 4)) |
Bill Wendling | 523bea8 | 2013-11-08 08:13:15 +0000 | [diff] [blame] | 483 | return false; |
| 484 | |
Duncan P. N. Exon Smith | 176b691 | 2014-10-03 20:01:09 +0000 | [diff] [blame] | 485 | return isDerivedType() && DbgNode->getNumOperands() >= 4 && |
| 486 | DbgNode->getNumOperands() <= 8 && getNumHeaderFields() >= 7 && |
| 487 | getNumHeaderFields() <= 8; |
Bill Wendling | 523bea8 | 2013-11-08 08:13:15 +0000 | [diff] [blame] | 488 | } |
| 489 | |
Bill Wendling | 523bea8 | 2013-11-08 08:13:15 +0000 | [diff] [blame] | 490 | bool DICompositeType::Verify() const { |
| 491 | if (!isCompositeType()) |
| 492 | return false; |
| 493 | |
Duncan P. N. Exon Smith | 176b691 | 2014-10-03 20:01:09 +0000 | [diff] [blame] | 494 | // Make sure DerivedFrom @ field 3 and ContainingType @ field 5 are TypeRef. |
| 495 | if (!fieldIsTypeRef(DbgNode, 3)) |
Bill Wendling | 523bea8 | 2013-11-08 08:13:15 +0000 | [diff] [blame] | 496 | return false; |
Duncan P. N. Exon Smith | 176b691 | 2014-10-03 20:01:09 +0000 | [diff] [blame] | 497 | if (!fieldIsTypeRef(DbgNode, 5)) |
Bill Wendling | 523bea8 | 2013-11-08 08:13:15 +0000 | [diff] [blame] | 498 | return false; |
| 499 | |
Duncan P. N. Exon Smith | 176b691 | 2014-10-03 20:01:09 +0000 | [diff] [blame] | 500 | // Make sure the type identifier at field 7 is MDString, it can be null. |
| 501 | if (!fieldIsMDString(DbgNode, 7)) |
Bill Wendling | 523bea8 | 2013-11-08 08:13:15 +0000 | [diff] [blame] | 502 | return false; |
| 503 | |
Adrian Prantl | 99c7af2 | 2013-12-18 21:48:19 +0000 | [diff] [blame] | 504 | // A subroutine type can't be both & and &&. |
| 505 | if (isLValueReference() && isRValueReference()) |
| 506 | return false; |
| 507 | |
Duncan P. N. Exon Smith | 176b691 | 2014-10-03 20:01:09 +0000 | [diff] [blame] | 508 | return DbgNode->getNumOperands() == 8 && getNumHeaderFields() == 8; |
Bill Wendling | 523bea8 | 2013-11-08 08:13:15 +0000 | [diff] [blame] | 509 | } |
| 510 | |
Bill Wendling | 523bea8 | 2013-11-08 08:13:15 +0000 | [diff] [blame] | 511 | bool DISubprogram::Verify() const { |
| 512 | if (!isSubprogram()) |
| 513 | return false; |
| 514 | |
Duncan P. N. Exon Smith | 176b691 | 2014-10-03 20:01:09 +0000 | [diff] [blame] | 515 | // Make sure context @ field 2 is a ScopeRef and type @ field 3 is a MDNode. |
Bill Wendling | 523bea8 | 2013-11-08 08:13:15 +0000 | [diff] [blame] | 516 | if (!fieldIsScopeRef(DbgNode, 2)) |
| 517 | return false; |
Duncan P. N. Exon Smith | 176b691 | 2014-10-03 20:01:09 +0000 | [diff] [blame] | 518 | if (!fieldIsMDNode(DbgNode, 3)) |
Bill Wendling | 523bea8 | 2013-11-08 08:13:15 +0000 | [diff] [blame] | 519 | return false; |
Duncan P. N. Exon Smith | 176b691 | 2014-10-03 20:01:09 +0000 | [diff] [blame] | 520 | // Containing type @ field 4. |
| 521 | if (!fieldIsTypeRef(DbgNode, 4)) |
Bill Wendling | 523bea8 | 2013-11-08 08:13:15 +0000 | [diff] [blame] | 522 | return false; |
Adrian Prantl | 99c7af2 | 2013-12-18 21:48:19 +0000 | [diff] [blame] | 523 | |
| 524 | // A subprogram can't be both & and &&. |
| 525 | if (isLValueReference() && isRValueReference()) |
| 526 | return false; |
| 527 | |
David Blaikie | 3dfe478 | 2014-10-14 18:22:52 +0000 | [diff] [blame] | 528 | // If a DISubprogram has an llvm::Function*, then scope chains from all |
| 529 | // instructions within the function should lead to this DISubprogram. |
| 530 | if (auto *F = getFunction()) { |
David Blaikie | 3dfe478 | 2014-10-14 18:22:52 +0000 | [diff] [blame] | 531 | for (auto &BB : *F) { |
| 532 | for (auto &I : BB) { |
| 533 | DebugLoc DL = I.getDebugLoc(); |
| 534 | if (DL.isUnknown()) |
| 535 | continue; |
| 536 | |
| 537 | MDNode *Scope = nullptr; |
| 538 | MDNode *IA = nullptr; |
| 539 | // walk the inlined-at scopes |
Duncan P. N. Exon Smith | 5bf8fef | 2014-12-09 18:38:53 +0000 | [diff] [blame] | 540 | while ((IA = DL.getInlinedAt())) |
David Blaikie | 3dfe478 | 2014-10-14 18:22:52 +0000 | [diff] [blame] | 541 | DL = DebugLoc::getFromDILocation(IA); |
Duncan P. N. Exon Smith | 5bf8fef | 2014-12-09 18:38:53 +0000 | [diff] [blame] | 542 | DL.getScopeAndInlinedAt(Scope, IA); |
Adrian Prantl | de200df | 2015-01-20 22:37:25 +0000 | [diff] [blame] | 543 | if (!Scope) |
| 544 | return false; |
David Blaikie | 3dfe478 | 2014-10-14 18:22:52 +0000 | [diff] [blame] | 545 | assert(!IA); |
| 546 | while (!DIDescriptor(Scope).isSubprogram()) { |
| 547 | DILexicalBlockFile D(Scope); |
| 548 | Scope = D.isLexicalBlockFile() |
| 549 | ? D.getScope() |
Duncan P. N. Exon Smith | 5bf8fef | 2014-12-09 18:38:53 +0000 | [diff] [blame] | 550 | : DebugLoc::getFromDILexicalBlock(Scope).getScope(); |
Adrian Prantl | 1292e24 | 2015-01-21 18:32:56 +0000 | [diff] [blame] | 551 | if (!Scope) |
| 552 | return false; |
David Blaikie | 3dfe478 | 2014-10-14 18:22:52 +0000 | [diff] [blame] | 553 | } |
| 554 | if (!DISubprogram(Scope).describes(F)) |
| 555 | return false; |
| 556 | } |
| 557 | } |
| 558 | } |
Duncan P. N. Exon Smith | 176b691 | 2014-10-03 20:01:09 +0000 | [diff] [blame] | 559 | return DbgNode->getNumOperands() == 9 && getNumHeaderFields() == 12; |
Bill Wendling | 523bea8 | 2013-11-08 08:13:15 +0000 | [diff] [blame] | 560 | } |
| 561 | |
Bill Wendling | 523bea8 | 2013-11-08 08:13:15 +0000 | [diff] [blame] | 562 | bool DIGlobalVariable::Verify() const { |
| 563 | if (!isGlobalVariable()) |
| 564 | return false; |
| 565 | |
| 566 | if (getDisplayName().empty()) |
| 567 | return false; |
Manman Ren | f0a582b | 2014-11-21 19:55:23 +0000 | [diff] [blame] | 568 | // Make sure context @ field 1 is an MDNode. |
| 569 | if (!fieldIsMDNode(DbgNode, 1)) |
Bill Wendling | 523bea8 | 2013-11-08 08:13:15 +0000 | [diff] [blame] | 570 | return false; |
Duncan P. N. Exon Smith | 176b691 | 2014-10-03 20:01:09 +0000 | [diff] [blame] | 571 | // Make sure that type @ field 3 is a DITypeRef. |
| 572 | if (!fieldIsTypeRef(DbgNode, 3)) |
Bill Wendling | 523bea8 | 2013-11-08 08:13:15 +0000 | [diff] [blame] | 573 | return false; |
Duncan P. N. Exon Smith | 176b691 | 2014-10-03 20:01:09 +0000 | [diff] [blame] | 574 | // Make sure StaticDataMemberDeclaration @ field 5 is MDNode. |
| 575 | if (!fieldIsMDNode(DbgNode, 5)) |
Bill Wendling | 523bea8 | 2013-11-08 08:13:15 +0000 | [diff] [blame] | 576 | return false; |
| 577 | |
Duncan P. N. Exon Smith | 176b691 | 2014-10-03 20:01:09 +0000 | [diff] [blame] | 578 | return DbgNode->getNumOperands() == 6 && getNumHeaderFields() == 7; |
Bill Wendling | 523bea8 | 2013-11-08 08:13:15 +0000 | [diff] [blame] | 579 | } |
| 580 | |
Bill Wendling | 523bea8 | 2013-11-08 08:13:15 +0000 | [diff] [blame] | 581 | bool DIVariable::Verify() const { |
| 582 | if (!isVariable()) |
| 583 | return false; |
| 584 | |
Adrian Prantl | 1a1647c | 2014-03-18 02:34:58 +0000 | [diff] [blame] | 585 | // Make sure context @ field 1 is an MDNode. |
Bill Wendling | 523bea8 | 2013-11-08 08:13:15 +0000 | [diff] [blame] | 586 | if (!fieldIsMDNode(DbgNode, 1)) |
| 587 | return false; |
Duncan P. N. Exon Smith | 176b691 | 2014-10-03 20:01:09 +0000 | [diff] [blame] | 588 | // Make sure that type @ field 3 is a DITypeRef. |
| 589 | if (!fieldIsTypeRef(DbgNode, 3)) |
| 590 | return false; |
| 591 | |
| 592 | // Check the number of header fields, which is common between complex and |
| 593 | // simple variables. |
| 594 | if (getNumHeaderFields() != 4) |
Bill Wendling | 523bea8 | 2013-11-08 08:13:15 +0000 | [diff] [blame] | 595 | return false; |
Adrian Prantl | da7d92e | 2014-06-30 17:17:35 +0000 | [diff] [blame] | 596 | |
Adrian Prantl | 87b7eb9 | 2014-10-01 18:55:02 +0000 | [diff] [blame] | 597 | // Variable without an inline location. |
Duncan P. N. Exon Smith | 176b691 | 2014-10-03 20:01:09 +0000 | [diff] [blame] | 598 | if (DbgNode->getNumOperands() == 4) |
Adrian Prantl | da7d92e | 2014-06-30 17:17:35 +0000 | [diff] [blame] | 599 | return true; |
| 600 | |
Duncan P. N. Exon Smith | 176b691 | 2014-10-03 20:01:09 +0000 | [diff] [blame] | 601 | // Variable with an inline location. |
| 602 | return getInlinedAt() != nullptr && DbgNode->getNumOperands() == 5; |
Adrian Prantl | 87b7eb9 | 2014-10-01 18:55:02 +0000 | [diff] [blame] | 603 | } |
| 604 | |
Adrian Prantl | 87b7eb9 | 2014-10-01 18:55:02 +0000 | [diff] [blame] | 605 | bool DIExpression::Verify() const { |
| 606 | // Empty DIExpressions may be represented as a nullptr. |
| 607 | if (!DbgNode) |
| 608 | return true; |
| 609 | |
Adrian Prantl | 9260cca | 2015-01-22 00:00:52 +0000 | [diff] [blame] | 610 | if (!(isExpression() && DbgNode->getNumOperands() == 1)) |
| 611 | return false; |
| 612 | |
Adrian Prantl | 70f2a73 | 2015-01-23 23:40:47 +0000 | [diff] [blame] | 613 | for (auto Op : *this) |
| 614 | switch (Op) { |
Adrian Prantl | 27bd01f | 2015-02-09 23:57:15 +0000 | [diff] [blame] | 615 | case DW_OP_bit_piece: |
Adrian Prantl | 9260cca | 2015-01-22 00:00:52 +0000 | [diff] [blame] | 616 | // Must be the last element of the expression. |
Adrian Prantl | 70f2a73 | 2015-01-23 23:40:47 +0000 | [diff] [blame] | 617 | return std::distance(Op.getBase(), DIHeaderFieldIterator()) == 3; |
Adrian Prantl | 34bcbee | 2015-01-21 00:59:20 +0000 | [diff] [blame] | 618 | case DW_OP_plus: |
Adrian Prantl | 70f2a73 | 2015-01-23 23:40:47 +0000 | [diff] [blame] | 619 | if (std::distance(Op.getBase(), DIHeaderFieldIterator()) < 2) |
Adrian Prantl | 34bcbee | 2015-01-21 00:59:20 +0000 | [diff] [blame] | 620 | return false; |
Adrian Prantl | 34bcbee | 2015-01-21 00:59:20 +0000 | [diff] [blame] | 621 | break; |
Adrian Prantl | 9260cca | 2015-01-22 00:00:52 +0000 | [diff] [blame] | 622 | case DW_OP_deref: |
| 623 | break; |
| 624 | default: |
| 625 | // Other operators are not yet supported by the backend. |
| 626 | return false; |
| 627 | } |
| 628 | return true; |
Bill Wendling | 523bea8 | 2013-11-08 08:13:15 +0000 | [diff] [blame] | 629 | } |
| 630 | |
Bill Wendling | 523bea8 | 2013-11-08 08:13:15 +0000 | [diff] [blame] | 631 | bool DILocation::Verify() const { |
Duncan P. N. Exon Smith | 9885469 | 2015-01-14 22:27:36 +0000 | [diff] [blame] | 632 | return DbgNode && isa<MDLocation>(DbgNode); |
Bill Wendling | 523bea8 | 2013-11-08 08:13:15 +0000 | [diff] [blame] | 633 | } |
| 634 | |
Bill Wendling | 523bea8 | 2013-11-08 08:13:15 +0000 | [diff] [blame] | 635 | bool DINameSpace::Verify() const { |
| 636 | if (!isNameSpace()) |
| 637 | return false; |
Duncan P. N. Exon Smith | 176b691 | 2014-10-03 20:01:09 +0000 | [diff] [blame] | 638 | return DbgNode->getNumOperands() == 3 && getNumHeaderFields() == 3; |
Bill Wendling | 523bea8 | 2013-11-08 08:13:15 +0000 | [diff] [blame] | 639 | } |
| 640 | |
Bill Wendling | 523bea8 | 2013-11-08 08:13:15 +0000 | [diff] [blame] | 641 | MDNode *DIFile::getFileNode() const { return getNodeField(DbgNode, 1); } |
| 642 | |
Bill Wendling | 523bea8 | 2013-11-08 08:13:15 +0000 | [diff] [blame] | 643 | bool DIFile::Verify() const { |
| 644 | return isFile() && DbgNode->getNumOperands() == 2; |
| 645 | } |
| 646 | |
Bill Wendling | 523bea8 | 2013-11-08 08:13:15 +0000 | [diff] [blame] | 647 | bool DIEnumerator::Verify() const { |
Duncan P. N. Exon Smith | 176b691 | 2014-10-03 20:01:09 +0000 | [diff] [blame] | 648 | return isEnumerator() && DbgNode->getNumOperands() == 1 && |
| 649 | getNumHeaderFields() == 3; |
Bill Wendling | 523bea8 | 2013-11-08 08:13:15 +0000 | [diff] [blame] | 650 | } |
| 651 | |
Bill Wendling | 523bea8 | 2013-11-08 08:13:15 +0000 | [diff] [blame] | 652 | bool DISubrange::Verify() const { |
Duncan P. N. Exon Smith | 176b691 | 2014-10-03 20:01:09 +0000 | [diff] [blame] | 653 | return isSubrange() && DbgNode->getNumOperands() == 1 && |
| 654 | getNumHeaderFields() == 3; |
Bill Wendling | 523bea8 | 2013-11-08 08:13:15 +0000 | [diff] [blame] | 655 | } |
| 656 | |
Bill Wendling | 523bea8 | 2013-11-08 08:13:15 +0000 | [diff] [blame] | 657 | bool DILexicalBlock::Verify() const { |
Duncan P. N. Exon Smith | 176b691 | 2014-10-03 20:01:09 +0000 | [diff] [blame] | 658 | return isLexicalBlock() && DbgNode->getNumOperands() == 3 && |
| 659 | getNumHeaderFields() == 4; |
Bill Wendling | 523bea8 | 2013-11-08 08:13:15 +0000 | [diff] [blame] | 660 | } |
| 661 | |
Bill Wendling | 523bea8 | 2013-11-08 08:13:15 +0000 | [diff] [blame] | 662 | bool DILexicalBlockFile::Verify() const { |
Duncan P. N. Exon Smith | 176b691 | 2014-10-03 20:01:09 +0000 | [diff] [blame] | 663 | return isLexicalBlockFile() && DbgNode->getNumOperands() == 3 && |
| 664 | getNumHeaderFields() == 2; |
Bill Wendling | 523bea8 | 2013-11-08 08:13:15 +0000 | [diff] [blame] | 665 | } |
| 666 | |
Bill Wendling | 523bea8 | 2013-11-08 08:13:15 +0000 | [diff] [blame] | 667 | bool DITemplateTypeParameter::Verify() const { |
Duncan P. N. Exon Smith | 176b691 | 2014-10-03 20:01:09 +0000 | [diff] [blame] | 668 | return isTemplateTypeParameter() && DbgNode->getNumOperands() == 4 && |
| 669 | getNumHeaderFields() == 4; |
Bill Wendling | 523bea8 | 2013-11-08 08:13:15 +0000 | [diff] [blame] | 670 | } |
| 671 | |
Bill Wendling | 523bea8 | 2013-11-08 08:13:15 +0000 | [diff] [blame] | 672 | bool DITemplateValueParameter::Verify() const { |
Duncan P. N. Exon Smith | 176b691 | 2014-10-03 20:01:09 +0000 | [diff] [blame] | 673 | return isTemplateValueParameter() && DbgNode->getNumOperands() == 5 && |
| 674 | getNumHeaderFields() == 4; |
Bill Wendling | 523bea8 | 2013-11-08 08:13:15 +0000 | [diff] [blame] | 675 | } |
| 676 | |
Bill Wendling | 523bea8 | 2013-11-08 08:13:15 +0000 | [diff] [blame] | 677 | bool DIImportedEntity::Verify() const { |
Duncan P. N. Exon Smith | 176b691 | 2014-10-03 20:01:09 +0000 | [diff] [blame] | 678 | return isImportedEntity() && DbgNode->getNumOperands() == 3 && |
| 679 | getNumHeaderFields() == 3; |
Bill Wendling | 523bea8 | 2013-11-08 08:13:15 +0000 | [diff] [blame] | 680 | } |
| 681 | |
Bill Wendling | 523bea8 | 2013-11-08 08:13:15 +0000 | [diff] [blame] | 682 | MDNode *DIDerivedType::getObjCProperty() const { |
Duncan P. N. Exon Smith | 176b691 | 2014-10-03 20:01:09 +0000 | [diff] [blame] | 683 | return getNodeField(DbgNode, 4); |
Bill Wendling | 523bea8 | 2013-11-08 08:13:15 +0000 | [diff] [blame] | 684 | } |
| 685 | |
| 686 | MDString *DICompositeType::getIdentifier() const { |
Duncan P. N. Exon Smith | 176b691 | 2014-10-03 20:01:09 +0000 | [diff] [blame] | 687 | return cast_or_null<MDString>(getField(DbgNode, 7)); |
Bill Wendling | 523bea8 | 2013-11-08 08:13:15 +0000 | [diff] [blame] | 688 | } |
| 689 | |
| 690 | #ifndef NDEBUG |
| 691 | static void VerifySubsetOf(const MDNode *LHS, const MDNode *RHS) { |
| 692 | for (unsigned i = 0; i != LHS->getNumOperands(); ++i) { |
| 693 | // Skip the 'empty' list (that's a single i32 0, rather than truly empty). |
Duncan P. N. Exon Smith | 5bf8fef | 2014-12-09 18:38:53 +0000 | [diff] [blame] | 694 | if (i == 0 && mdconst::hasa<ConstantInt>(LHS->getOperand(i))) |
Bill Wendling | 523bea8 | 2013-11-08 08:13:15 +0000 | [diff] [blame] | 695 | continue; |
| 696 | const MDNode *E = cast<MDNode>(LHS->getOperand(i)); |
| 697 | bool found = false; |
| 698 | for (unsigned j = 0; !found && j != RHS->getNumOperands(); ++j) |
Hans Wennborg | e242e8b | 2014-12-09 20:39:15 +0000 | [diff] [blame] | 699 | found = (E == cast<MDNode>(RHS->getOperand(j))); |
Bill Wendling | 523bea8 | 2013-11-08 08:13:15 +0000 | [diff] [blame] | 700 | assert(found && "Losing a member during member list replacement"); |
| 701 | } |
| 702 | } |
| 703 | #endif |
| 704 | |
Manman Ren | 1a125c9 | 2014-07-28 19:33:20 +0000 | [diff] [blame] | 705 | void DICompositeType::setArraysHelper(MDNode *Elements, MDNode *TParams) { |
Duncan P. N. Exon Smith | 5bf8fef | 2014-12-09 18:38:53 +0000 | [diff] [blame] | 706 | TrackingMDNodeRef N(*this); |
Bill Wendling | 523bea8 | 2013-11-08 08:13:15 +0000 | [diff] [blame] | 707 | if (Elements) { |
| 708 | #ifndef NDEBUG |
| 709 | // Check that the new list of members contains all the old members as well. |
Duncan P. N. Exon Smith | 176b691 | 2014-10-03 20:01:09 +0000 | [diff] [blame] | 710 | if (const MDNode *El = cast_or_null<MDNode>(N->getOperand(4))) |
Bill Wendling | 523bea8 | 2013-11-08 08:13:15 +0000 | [diff] [blame] | 711 | VerifySubsetOf(El, Elements); |
| 712 | #endif |
Duncan P. N. Exon Smith | 176b691 | 2014-10-03 20:01:09 +0000 | [diff] [blame] | 713 | N->replaceOperandWith(4, Elements); |
Bill Wendling | 523bea8 | 2013-11-08 08:13:15 +0000 | [diff] [blame] | 714 | } |
| 715 | if (TParams) |
Duncan P. N. Exon Smith | 176b691 | 2014-10-03 20:01:09 +0000 | [diff] [blame] | 716 | N->replaceOperandWith(6, TParams); |
Bill Wendling | 523bea8 | 2013-11-08 08:13:15 +0000 | [diff] [blame] | 717 | DbgNode = N; |
| 718 | } |
| 719 | |
Bill Wendling | 523bea8 | 2013-11-08 08:13:15 +0000 | [diff] [blame] | 720 | DIScopeRef DIScope::getRef() const { |
| 721 | if (!isCompositeType()) |
| 722 | return DIScopeRef(*this); |
| 723 | DICompositeType DTy(DbgNode); |
| 724 | if (!DTy.getIdentifier()) |
| 725 | return DIScopeRef(*this); |
| 726 | return DIScopeRef(DTy.getIdentifier()); |
| 727 | } |
| 728 | |
Bill Wendling | 523bea8 | 2013-11-08 08:13:15 +0000 | [diff] [blame] | 729 | void DICompositeType::setContainingType(DICompositeType ContainingType) { |
Duncan P. N. Exon Smith | 5bf8fef | 2014-12-09 18:38:53 +0000 | [diff] [blame] | 730 | TrackingMDNodeRef N(*this); |
Duncan P. N. Exon Smith | 176b691 | 2014-10-03 20:01:09 +0000 | [diff] [blame] | 731 | N->replaceOperandWith(5, ContainingType.getRef()); |
Bill Wendling | 523bea8 | 2013-11-08 08:13:15 +0000 | [diff] [blame] | 732 | DbgNode = N; |
| 733 | } |
| 734 | |
Bill Wendling | 523bea8 | 2013-11-08 08:13:15 +0000 | [diff] [blame] | 735 | bool DIVariable::isInlinedFnArgument(const Function *CurFn) { |
| 736 | assert(CurFn && "Invalid function"); |
| 737 | if (!getContext().isSubprogram()) |
| 738 | return false; |
| 739 | // This variable is not inlined function argument if its scope |
| 740 | // does not describe current function. |
| 741 | return !DISubprogram(getContext()).describes(CurFn); |
| 742 | } |
| 743 | |
Bill Wendling | 523bea8 | 2013-11-08 08:13:15 +0000 | [diff] [blame] | 744 | bool DISubprogram::describes(const Function *F) { |
| 745 | assert(F && "Invalid function"); |
| 746 | if (F == getFunction()) |
| 747 | return true; |
| 748 | StringRef Name = getLinkageName(); |
| 749 | if (Name.empty()) |
| 750 | Name = getName(); |
| 751 | if (F->getName() == Name) |
| 752 | return true; |
| 753 | return false; |
| 754 | } |
| 755 | |
Bill Wendling | 523bea8 | 2013-11-08 08:13:15 +0000 | [diff] [blame] | 756 | MDNode *DISubprogram::getVariablesNodes() const { |
Duncan P. N. Exon Smith | 176b691 | 2014-10-03 20:01:09 +0000 | [diff] [blame] | 757 | return getNodeField(DbgNode, 8); |
Bill Wendling | 523bea8 | 2013-11-08 08:13:15 +0000 | [diff] [blame] | 758 | } |
| 759 | |
| 760 | DIArray DISubprogram::getVariables() const { |
Duncan P. N. Exon Smith | 176b691 | 2014-10-03 20:01:09 +0000 | [diff] [blame] | 761 | return DIArray(getNodeField(DbgNode, 8)); |
Bill Wendling | 523bea8 | 2013-11-08 08:13:15 +0000 | [diff] [blame] | 762 | } |
| 763 | |
Duncan P. N. Exon Smith | 5bf8fef | 2014-12-09 18:38:53 +0000 | [diff] [blame] | 764 | Metadata *DITemplateValueParameter::getValue() const { |
| 765 | return DbgNode->getOperand(3); |
Bill Wendling | 523bea8 | 2013-11-08 08:13:15 +0000 | [diff] [blame] | 766 | } |
| 767 | |
Bill Wendling | 523bea8 | 2013-11-08 08:13:15 +0000 | [diff] [blame] | 768 | DIScopeRef DIScope::getContext() const { |
| 769 | |
| 770 | if (isType()) |
| 771 | return DIType(DbgNode).getContext(); |
| 772 | |
| 773 | if (isSubprogram()) |
| 774 | return DIScopeRef(DISubprogram(DbgNode).getContext()); |
| 775 | |
| 776 | if (isLexicalBlock()) |
| 777 | return DIScopeRef(DILexicalBlock(DbgNode).getContext()); |
| 778 | |
| 779 | if (isLexicalBlockFile()) |
| 780 | return DIScopeRef(DILexicalBlockFile(DbgNode).getContext()); |
| 781 | |
| 782 | if (isNameSpace()) |
| 783 | return DIScopeRef(DINameSpace(DbgNode).getContext()); |
| 784 | |
| 785 | assert((isFile() || isCompileUnit()) && "Unhandled type of scope."); |
Craig Topper | c620761 | 2014-04-09 06:08:46 +0000 | [diff] [blame] | 786 | return DIScopeRef(nullptr); |
Bill Wendling | 523bea8 | 2013-11-08 08:13:15 +0000 | [diff] [blame] | 787 | } |
| 788 | |
Bill Wendling | 523bea8 | 2013-11-08 08:13:15 +0000 | [diff] [blame] | 789 | StringRef DIScope::getName() const { |
| 790 | if (isType()) |
| 791 | return DIType(DbgNode).getName(); |
| 792 | if (isSubprogram()) |
| 793 | return DISubprogram(DbgNode).getName(); |
| 794 | if (isNameSpace()) |
| 795 | return DINameSpace(DbgNode).getName(); |
| 796 | assert((isLexicalBlock() || isLexicalBlockFile() || isFile() || |
| 797 | isCompileUnit()) && |
| 798 | "Unhandled type of scope."); |
| 799 | return StringRef(); |
| 800 | } |
| 801 | |
| 802 | StringRef DIScope::getFilename() const { |
| 803 | if (!DbgNode) |
| 804 | return StringRef(); |
| 805 | return ::getStringField(getNodeField(DbgNode, 1), 0); |
| 806 | } |
| 807 | |
| 808 | StringRef DIScope::getDirectory() const { |
| 809 | if (!DbgNode) |
| 810 | return StringRef(); |
| 811 | return ::getStringField(getNodeField(DbgNode, 1), 1); |
| 812 | } |
| 813 | |
| 814 | DIArray DICompileUnit::getEnumTypes() const { |
Duncan P. N. Exon Smith | 176b691 | 2014-10-03 20:01:09 +0000 | [diff] [blame] | 815 | if (!DbgNode || DbgNode->getNumOperands() < 7) |
Bill Wendling | 523bea8 | 2013-11-08 08:13:15 +0000 | [diff] [blame] | 816 | return DIArray(); |
| 817 | |
Duncan P. N. Exon Smith | 176b691 | 2014-10-03 20:01:09 +0000 | [diff] [blame] | 818 | return DIArray(getNodeField(DbgNode, 2)); |
Bill Wendling | 523bea8 | 2013-11-08 08:13:15 +0000 | [diff] [blame] | 819 | } |
| 820 | |
| 821 | DIArray DICompileUnit::getRetainedTypes() const { |
Duncan P. N. Exon Smith | 176b691 | 2014-10-03 20:01:09 +0000 | [diff] [blame] | 822 | if (!DbgNode || DbgNode->getNumOperands() < 7) |
Bill Wendling | 523bea8 | 2013-11-08 08:13:15 +0000 | [diff] [blame] | 823 | return DIArray(); |
| 824 | |
Duncan P. N. Exon Smith | 176b691 | 2014-10-03 20:01:09 +0000 | [diff] [blame] | 825 | return DIArray(getNodeField(DbgNode, 3)); |
Bill Wendling | 523bea8 | 2013-11-08 08:13:15 +0000 | [diff] [blame] | 826 | } |
| 827 | |
| 828 | DIArray DICompileUnit::getSubprograms() const { |
Duncan P. N. Exon Smith | 176b691 | 2014-10-03 20:01:09 +0000 | [diff] [blame] | 829 | if (!DbgNode || DbgNode->getNumOperands() < 7) |
Bill Wendling | 523bea8 | 2013-11-08 08:13:15 +0000 | [diff] [blame] | 830 | return DIArray(); |
| 831 | |
Duncan P. N. Exon Smith | 176b691 | 2014-10-03 20:01:09 +0000 | [diff] [blame] | 832 | return DIArray(getNodeField(DbgNode, 4)); |
Bill Wendling | 523bea8 | 2013-11-08 08:13:15 +0000 | [diff] [blame] | 833 | } |
| 834 | |
| 835 | DIArray DICompileUnit::getGlobalVariables() const { |
Duncan P. N. Exon Smith | 176b691 | 2014-10-03 20:01:09 +0000 | [diff] [blame] | 836 | if (!DbgNode || DbgNode->getNumOperands() < 7) |
Bill Wendling | 523bea8 | 2013-11-08 08:13:15 +0000 | [diff] [blame] | 837 | return DIArray(); |
| 838 | |
Duncan P. N. Exon Smith | 176b691 | 2014-10-03 20:01:09 +0000 | [diff] [blame] | 839 | return DIArray(getNodeField(DbgNode, 5)); |
Bill Wendling | 523bea8 | 2013-11-08 08:13:15 +0000 | [diff] [blame] | 840 | } |
| 841 | |
| 842 | DIArray DICompileUnit::getImportedEntities() const { |
Duncan P. N. Exon Smith | 176b691 | 2014-10-03 20:01:09 +0000 | [diff] [blame] | 843 | if (!DbgNode || DbgNode->getNumOperands() < 7) |
Bill Wendling | 523bea8 | 2013-11-08 08:13:15 +0000 | [diff] [blame] | 844 | return DIArray(); |
| 845 | |
Duncan P. N. Exon Smith | 176b691 | 2014-10-03 20:01:09 +0000 | [diff] [blame] | 846 | return DIArray(getNodeField(DbgNode, 6)); |
| 847 | } |
| 848 | |
| 849 | void DICompileUnit::replaceSubprograms(DIArray Subprograms) { |
| 850 | assert(Verify() && "Expected compile unit"); |
| 851 | if (Subprograms == getSubprograms()) |
| 852 | return; |
| 853 | |
| 854 | const_cast<MDNode *>(DbgNode)->replaceOperandWith(4, Subprograms); |
| 855 | } |
| 856 | |
| 857 | void DICompileUnit::replaceGlobalVariables(DIArray GlobalVariables) { |
| 858 | assert(Verify() && "Expected compile unit"); |
| 859 | if (GlobalVariables == getGlobalVariables()) |
| 860 | return; |
| 861 | |
| 862 | const_cast<MDNode *>(DbgNode)->replaceOperandWith(5, GlobalVariables); |
Bill Wendling | 523bea8 | 2013-11-08 08:13:15 +0000 | [diff] [blame] | 863 | } |
| 864 | |
Diego Novillo | f5041ce | 2014-03-03 20:06:11 +0000 | [diff] [blame] | 865 | DILocation DILocation::copyWithNewScope(LLVMContext &Ctx, |
David Blaikie | 2f3f76f | 2014-08-21 22:45:21 +0000 | [diff] [blame] | 866 | DILexicalBlockFile NewScope) { |
Diego Novillo | f5041ce | 2014-03-03 20:06:11 +0000 | [diff] [blame] | 867 | assert(Verify()); |
Duncan P. N. Exon Smith | 9885469 | 2015-01-14 22:27:36 +0000 | [diff] [blame] | 868 | assert(NewScope && "Expected valid scope"); |
| 869 | |
| 870 | const auto *Old = cast<MDLocation>(DbgNode); |
| 871 | return DILocation(MDLocation::get(Ctx, Old->getLine(), Old->getColumn(), |
| 872 | NewScope, Old->getInlinedAt())); |
Diego Novillo | f5041ce | 2014-03-03 20:06:11 +0000 | [diff] [blame] | 873 | } |
| 874 | |
Diego Novillo | f5041ce | 2014-03-03 20:06:11 +0000 | [diff] [blame] | 875 | unsigned DILocation::computeNewDiscriminator(LLVMContext &Ctx) { |
| 876 | std::pair<const char *, unsigned> Key(getFilename().data(), getLineNumber()); |
| 877 | return ++Ctx.pImpl->DiscriminatorTable[Key]; |
| 878 | } |
| 879 | |
Bill Wendling | 523bea8 | 2013-11-08 08:13:15 +0000 | [diff] [blame] | 880 | DIVariable llvm::createInlinedVariable(MDNode *DV, MDNode *InlinedScope, |
| 881 | LLVMContext &VMContext) { |
Duncan P. N. Exon Smith | 176b691 | 2014-10-03 20:01:09 +0000 | [diff] [blame] | 882 | assert(DIVariable(DV).Verify() && "Expected a DIVariable"); |
| 883 | if (!InlinedScope) |
| 884 | return cleanseInlinedVariable(DV, VMContext); |
| 885 | |
| 886 | // Insert inlined scope. |
Duncan P. N. Exon Smith | 5bf8fef | 2014-12-09 18:38:53 +0000 | [diff] [blame] | 887 | SmallVector<Metadata *, 8> Elts; |
Duncan P. N. Exon Smith | 176b691 | 2014-10-03 20:01:09 +0000 | [diff] [blame] | 888 | for (unsigned I = 0, E = DIVariableInlinedAtIndex; I != E; ++I) |
| 889 | Elts.push_back(DV->getOperand(I)); |
| 890 | Elts.push_back(InlinedScope); |
| 891 | |
| 892 | DIVariable Inlined(MDNode::get(VMContext, Elts)); |
| 893 | assert(Inlined.Verify() && "Expected to create a DIVariable"); |
| 894 | return Inlined; |
Bill Wendling | 523bea8 | 2013-11-08 08:13:15 +0000 | [diff] [blame] | 895 | } |
| 896 | |
Bill Wendling | 523bea8 | 2013-11-08 08:13:15 +0000 | [diff] [blame] | 897 | DIVariable llvm::cleanseInlinedVariable(MDNode *DV, LLVMContext &VMContext) { |
Duncan P. N. Exon Smith | 176b691 | 2014-10-03 20:01:09 +0000 | [diff] [blame] | 898 | assert(DIVariable(DV).Verify() && "Expected a DIVariable"); |
| 899 | if (!DIVariable(DV).getInlinedAt()) |
| 900 | return DIVariable(DV); |
| 901 | |
| 902 | // Remove inlined scope. |
Duncan P. N. Exon Smith | 5bf8fef | 2014-12-09 18:38:53 +0000 | [diff] [blame] | 903 | SmallVector<Metadata *, 8> Elts; |
Duncan P. N. Exon Smith | 176b691 | 2014-10-03 20:01:09 +0000 | [diff] [blame] | 904 | for (unsigned I = 0, E = DIVariableInlinedAtIndex; I != E; ++I) |
| 905 | Elts.push_back(DV->getOperand(I)); |
| 906 | |
| 907 | DIVariable Cleansed(MDNode::get(VMContext, Elts)); |
| 908 | assert(Cleansed.Verify() && "Expected to create a DIVariable"); |
| 909 | return Cleansed; |
Bill Wendling | 523bea8 | 2013-11-08 08:13:15 +0000 | [diff] [blame] | 910 | } |
| 911 | |
Bill Wendling | 523bea8 | 2013-11-08 08:13:15 +0000 | [diff] [blame] | 912 | DISubprogram llvm::getDISubprogram(const MDNode *Scope) { |
| 913 | DIDescriptor D(Scope); |
| 914 | if (D.isSubprogram()) |
| 915 | return DISubprogram(Scope); |
| 916 | |
| 917 | if (D.isLexicalBlockFile()) |
| 918 | return getDISubprogram(DILexicalBlockFile(Scope).getContext()); |
| 919 | |
| 920 | if (D.isLexicalBlock()) |
| 921 | return getDISubprogram(DILexicalBlock(Scope).getContext()); |
| 922 | |
| 923 | return DISubprogram(); |
| 924 | } |
| 925 | |
Timur Iskhodzhanov | eb229ca | 2014-10-23 23:46:28 +0000 | [diff] [blame] | 926 | DISubprogram llvm::getDISubprogram(const Function *F) { |
| 927 | // We look for the first instr that has a debug annotation leading back to F. |
Timur Iskhodzhanov | eb229ca | 2014-10-23 23:46:28 +0000 | [diff] [blame] | 928 | for (auto &BB : *F) { |
David Majnemer | c758df4 | 2014-11-01 07:57:14 +0000 | [diff] [blame] | 929 | auto Inst = std::find_if(BB.begin(), BB.end(), [](const Instruction &Inst) { |
| 930 | return !Inst.getDebugLoc().isUnknown(); |
| 931 | }); |
| 932 | if (Inst == BB.end()) |
| 933 | continue; |
| 934 | DebugLoc DLoc = Inst->getDebugLoc(); |
Duncan P. N. Exon Smith | 5bf8fef | 2014-12-09 18:38:53 +0000 | [diff] [blame] | 935 | const MDNode *Scope = DLoc.getScopeNode(); |
David Majnemer | c758df4 | 2014-11-01 07:57:14 +0000 | [diff] [blame] | 936 | DISubprogram Subprogram = getDISubprogram(Scope); |
| 937 | return Subprogram.describes(F) ? Subprogram : DISubprogram(); |
Timur Iskhodzhanov | eb229ca | 2014-10-23 23:46:28 +0000 | [diff] [blame] | 938 | } |
| 939 | |
| 940 | return DISubprogram(); |
| 941 | } |
| 942 | |
Bill Wendling | 523bea8 | 2013-11-08 08:13:15 +0000 | [diff] [blame] | 943 | DICompositeType llvm::getDICompositeType(DIType T) { |
| 944 | if (T.isCompositeType()) |
| 945 | return DICompositeType(T); |
| 946 | |
| 947 | if (T.isDerivedType()) { |
| 948 | // This function is currently used by dragonegg and dragonegg does |
| 949 | // not generate identifier for types, so using an empty map to resolve |
| 950 | // DerivedFrom should be fine. |
| 951 | DITypeIdentifierMap EmptyMap; |
| 952 | return getDICompositeType( |
| 953 | DIDerivedType(T).getTypeDerivedFrom().resolve(EmptyMap)); |
| 954 | } |
| 955 | |
| 956 | return DICompositeType(); |
| 957 | } |
| 958 | |
Bill Wendling | 523bea8 | 2013-11-08 08:13:15 +0000 | [diff] [blame] | 959 | DITypeIdentifierMap |
| 960 | llvm::generateDITypeIdentifierMap(const NamedMDNode *CU_Nodes) { |
| 961 | DITypeIdentifierMap Map; |
| 962 | for (unsigned CUi = 0, CUe = CU_Nodes->getNumOperands(); CUi != CUe; ++CUi) { |
Duncan P. N. Exon Smith | de36e80 | 2014-11-11 21:30:22 +0000 | [diff] [blame] | 963 | DICompileUnit CU(CU_Nodes->getOperand(CUi)); |
Bill Wendling | 523bea8 | 2013-11-08 08:13:15 +0000 | [diff] [blame] | 964 | DIArray Retain = CU.getRetainedTypes(); |
| 965 | for (unsigned Ti = 0, Te = Retain.getNumElements(); Ti != Te; ++Ti) { |
| 966 | if (!Retain.getElement(Ti).isCompositeType()) |
| 967 | continue; |
| 968 | DICompositeType Ty(Retain.getElement(Ti)); |
| 969 | if (MDString *TypeId = Ty.getIdentifier()) { |
| 970 | // Definition has priority over declaration. |
| 971 | // Try to insert (TypeId, Ty) to Map. |
| 972 | std::pair<DITypeIdentifierMap::iterator, bool> P = |
| 973 | Map.insert(std::make_pair(TypeId, Ty)); |
| 974 | // If TypeId already exists in Map and this is a definition, replace |
| 975 | // whatever we had (declaration or definition) with the definition. |
| 976 | if (!P.second && !Ty.isForwardDecl()) |
| 977 | P.first->second = Ty; |
| 978 | } |
| 979 | } |
| 980 | } |
| 981 | return Map; |
| 982 | } |
| 983 | |
| 984 | //===----------------------------------------------------------------------===// |
| 985 | // DebugInfoFinder implementations. |
| 986 | //===----------------------------------------------------------------------===// |
| 987 | |
| 988 | void DebugInfoFinder::reset() { |
| 989 | CUs.clear(); |
| 990 | SPs.clear(); |
| 991 | GVs.clear(); |
| 992 | TYs.clear(); |
| 993 | Scopes.clear(); |
| 994 | NodesSeen.clear(); |
| 995 | TypeIdentifierMap.clear(); |
Manman Ren | 2085ccc | 2013-11-17 18:42:37 +0000 | [diff] [blame] | 996 | TypeMapInitialized = false; |
| 997 | } |
| 998 | |
Manman Ren | b46e550 | 2013-11-17 19:35:03 +0000 | [diff] [blame] | 999 | void DebugInfoFinder::InitializeTypeMap(const Module &M) { |
Manman Ren | 2085ccc | 2013-11-17 18:42:37 +0000 | [diff] [blame] | 1000 | if (!TypeMapInitialized) |
| 1001 | if (NamedMDNode *CU_Nodes = M.getNamedMetadata("llvm.dbg.cu")) { |
| 1002 | TypeIdentifierMap = generateDITypeIdentifierMap(CU_Nodes); |
| 1003 | TypeMapInitialized = true; |
| 1004 | } |
Bill Wendling | 523bea8 | 2013-11-08 08:13:15 +0000 | [diff] [blame] | 1005 | } |
| 1006 | |
Bill Wendling | 523bea8 | 2013-11-08 08:13:15 +0000 | [diff] [blame] | 1007 | void DebugInfoFinder::processModule(const Module &M) { |
Manman Ren | b46e550 | 2013-11-17 19:35:03 +0000 | [diff] [blame] | 1008 | InitializeTypeMap(M); |
Bill Wendling | 523bea8 | 2013-11-08 08:13:15 +0000 | [diff] [blame] | 1009 | if (NamedMDNode *CU_Nodes = M.getNamedMetadata("llvm.dbg.cu")) { |
Bill Wendling | 523bea8 | 2013-11-08 08:13:15 +0000 | [diff] [blame] | 1010 | for (unsigned i = 0, e = CU_Nodes->getNumOperands(); i != e; ++i) { |
Duncan P. N. Exon Smith | de36e80 | 2014-11-11 21:30:22 +0000 | [diff] [blame] | 1011 | DICompileUnit CU(CU_Nodes->getOperand(i)); |
Bill Wendling | 523bea8 | 2013-11-08 08:13:15 +0000 | [diff] [blame] | 1012 | addCompileUnit(CU); |
| 1013 | DIArray GVs = CU.getGlobalVariables(); |
| 1014 | for (unsigned i = 0, e = GVs.getNumElements(); i != e; ++i) { |
| 1015 | DIGlobalVariable DIG(GVs.getElement(i)); |
| 1016 | if (addGlobalVariable(DIG)) { |
Manman Ren | f0a582b | 2014-11-21 19:55:23 +0000 | [diff] [blame] | 1017 | processScope(DIG.getContext()); |
Adrian Prantl | 1a1647c | 2014-03-18 02:34:58 +0000 | [diff] [blame] | 1018 | processType(DIG.getType().resolve(TypeIdentifierMap)); |
Bill Wendling | 523bea8 | 2013-11-08 08:13:15 +0000 | [diff] [blame] | 1019 | } |
| 1020 | } |
| 1021 | DIArray SPs = CU.getSubprograms(); |
| 1022 | for (unsigned i = 0, e = SPs.getNumElements(); i != e; ++i) |
| 1023 | processSubprogram(DISubprogram(SPs.getElement(i))); |
| 1024 | DIArray EnumTypes = CU.getEnumTypes(); |
| 1025 | for (unsigned i = 0, e = EnumTypes.getNumElements(); i != e; ++i) |
| 1026 | processType(DIType(EnumTypes.getElement(i))); |
| 1027 | DIArray RetainedTypes = CU.getRetainedTypes(); |
| 1028 | for (unsigned i = 0, e = RetainedTypes.getNumElements(); i != e; ++i) |
| 1029 | processType(DIType(RetainedTypes.getElement(i))); |
| 1030 | DIArray Imports = CU.getImportedEntities(); |
| 1031 | for (unsigned i = 0, e = Imports.getNumElements(); i != e; ++i) { |
| 1032 | DIImportedEntity Import = DIImportedEntity(Imports.getElement(i)); |
Adrian Prantl | d09ba23 | 2014-04-01 03:41:04 +0000 | [diff] [blame] | 1033 | DIDescriptor Entity = Import.getEntity().resolve(TypeIdentifierMap); |
Bill Wendling | 523bea8 | 2013-11-08 08:13:15 +0000 | [diff] [blame] | 1034 | if (Entity.isType()) |
| 1035 | processType(DIType(Entity)); |
| 1036 | else if (Entity.isSubprogram()) |
| 1037 | processSubprogram(DISubprogram(Entity)); |
| 1038 | else if (Entity.isNameSpace()) |
| 1039 | processScope(DINameSpace(Entity).getContext()); |
| 1040 | } |
| 1041 | } |
| 1042 | } |
| 1043 | } |
| 1044 | |
Manman Ren | 2085ccc | 2013-11-17 18:42:37 +0000 | [diff] [blame] | 1045 | void DebugInfoFinder::processLocation(const Module &M, DILocation Loc) { |
Bill Wendling | 523bea8 | 2013-11-08 08:13:15 +0000 | [diff] [blame] | 1046 | if (!Loc) |
| 1047 | return; |
Manman Ren | b46e550 | 2013-11-17 19:35:03 +0000 | [diff] [blame] | 1048 | InitializeTypeMap(M); |
Bill Wendling | 523bea8 | 2013-11-08 08:13:15 +0000 | [diff] [blame] | 1049 | processScope(Loc.getScope()); |
Manman Ren | 2085ccc | 2013-11-17 18:42:37 +0000 | [diff] [blame] | 1050 | processLocation(M, Loc.getOrigLocation()); |
Bill Wendling | 523bea8 | 2013-11-08 08:13:15 +0000 | [diff] [blame] | 1051 | } |
| 1052 | |
Bill Wendling | 523bea8 | 2013-11-08 08:13:15 +0000 | [diff] [blame] | 1053 | void DebugInfoFinder::processType(DIType DT) { |
| 1054 | if (!addType(DT)) |
| 1055 | return; |
| 1056 | processScope(DT.getContext().resolve(TypeIdentifierMap)); |
| 1057 | if (DT.isCompositeType()) { |
| 1058 | DICompositeType DCT(DT); |
| 1059 | processType(DCT.getTypeDerivedFrom().resolve(TypeIdentifierMap)); |
Manman Ren | f8a1967 | 2014-07-28 22:24:06 +0000 | [diff] [blame] | 1060 | if (DT.isSubroutineType()) { |
| 1061 | DITypeArray DTA = DISubroutineType(DT).getTypeArray(); |
| 1062 | for (unsigned i = 0, e = DTA.getNumElements(); i != e; ++i) |
| 1063 | processType(DTA.getElement(i).resolve(TypeIdentifierMap)); |
| 1064 | return; |
| 1065 | } |
Manman Ren | ab8ffba | 2014-07-28 19:14:13 +0000 | [diff] [blame] | 1066 | DIArray DA = DCT.getElements(); |
Bill Wendling | 523bea8 | 2013-11-08 08:13:15 +0000 | [diff] [blame] | 1067 | for (unsigned i = 0, e = DA.getNumElements(); i != e; ++i) { |
| 1068 | DIDescriptor D = DA.getElement(i); |
| 1069 | if (D.isType()) |
| 1070 | processType(DIType(D)); |
| 1071 | else if (D.isSubprogram()) |
| 1072 | processSubprogram(DISubprogram(D)); |
| 1073 | } |
| 1074 | } else if (DT.isDerivedType()) { |
| 1075 | DIDerivedType DDT(DT); |
| 1076 | processType(DDT.getTypeDerivedFrom().resolve(TypeIdentifierMap)); |
| 1077 | } |
| 1078 | } |
| 1079 | |
| 1080 | void DebugInfoFinder::processScope(DIScope Scope) { |
| 1081 | if (Scope.isType()) { |
| 1082 | DIType Ty(Scope); |
| 1083 | processType(Ty); |
| 1084 | return; |
| 1085 | } |
| 1086 | if (Scope.isCompileUnit()) { |
| 1087 | addCompileUnit(DICompileUnit(Scope)); |
| 1088 | return; |
| 1089 | } |
| 1090 | if (Scope.isSubprogram()) { |
| 1091 | processSubprogram(DISubprogram(Scope)); |
| 1092 | return; |
| 1093 | } |
| 1094 | if (!addScope(Scope)) |
| 1095 | return; |
| 1096 | if (Scope.isLexicalBlock()) { |
| 1097 | DILexicalBlock LB(Scope); |
| 1098 | processScope(LB.getContext()); |
| 1099 | } else if (Scope.isLexicalBlockFile()) { |
| 1100 | DILexicalBlockFile LBF = DILexicalBlockFile(Scope); |
| 1101 | processScope(LBF.getScope()); |
| 1102 | } else if (Scope.isNameSpace()) { |
| 1103 | DINameSpace NS(Scope); |
| 1104 | processScope(NS.getContext()); |
| 1105 | } |
| 1106 | } |
| 1107 | |
Bill Wendling | 523bea8 | 2013-11-08 08:13:15 +0000 | [diff] [blame] | 1108 | void DebugInfoFinder::processSubprogram(DISubprogram SP) { |
| 1109 | if (!addSubprogram(SP)) |
| 1110 | return; |
| 1111 | processScope(SP.getContext().resolve(TypeIdentifierMap)); |
| 1112 | processType(SP.getType()); |
| 1113 | DIArray TParams = SP.getTemplateParams(); |
| 1114 | for (unsigned I = 0, E = TParams.getNumElements(); I != E; ++I) { |
| 1115 | DIDescriptor Element = TParams.getElement(I); |
| 1116 | if (Element.isTemplateTypeParameter()) { |
| 1117 | DITemplateTypeParameter TType(Element); |
| 1118 | processScope(TType.getContext().resolve(TypeIdentifierMap)); |
| 1119 | processType(TType.getType().resolve(TypeIdentifierMap)); |
| 1120 | } else if (Element.isTemplateValueParameter()) { |
| 1121 | DITemplateValueParameter TVal(Element); |
| 1122 | processScope(TVal.getContext().resolve(TypeIdentifierMap)); |
| 1123 | processType(TVal.getType().resolve(TypeIdentifierMap)); |
| 1124 | } |
| 1125 | } |
| 1126 | } |
| 1127 | |
Manman Ren | 2085ccc | 2013-11-17 18:42:37 +0000 | [diff] [blame] | 1128 | void DebugInfoFinder::processDeclare(const Module &M, |
| 1129 | const DbgDeclareInst *DDI) { |
Bill Wendling | 523bea8 | 2013-11-08 08:13:15 +0000 | [diff] [blame] | 1130 | MDNode *N = dyn_cast<MDNode>(DDI->getVariable()); |
| 1131 | if (!N) |
| 1132 | return; |
Manman Ren | b46e550 | 2013-11-17 19:35:03 +0000 | [diff] [blame] | 1133 | InitializeTypeMap(M); |
Bill Wendling | 523bea8 | 2013-11-08 08:13:15 +0000 | [diff] [blame] | 1134 | |
| 1135 | DIDescriptor DV(N); |
| 1136 | if (!DV.isVariable()) |
| 1137 | return; |
| 1138 | |
David Blaikie | 70573dc | 2014-11-19 07:49:26 +0000 | [diff] [blame] | 1139 | if (!NodesSeen.insert(DV).second) |
Bill Wendling | 523bea8 | 2013-11-08 08:13:15 +0000 | [diff] [blame] | 1140 | return; |
| 1141 | processScope(DIVariable(N).getContext()); |
Adrian Prantl | 1a1647c | 2014-03-18 02:34:58 +0000 | [diff] [blame] | 1142 | processType(DIVariable(N).getType().resolve(TypeIdentifierMap)); |
Bill Wendling | 523bea8 | 2013-11-08 08:13:15 +0000 | [diff] [blame] | 1143 | } |
| 1144 | |
Manman Ren | 2085ccc | 2013-11-17 18:42:37 +0000 | [diff] [blame] | 1145 | void DebugInfoFinder::processValue(const Module &M, const DbgValueInst *DVI) { |
Bill Wendling | 523bea8 | 2013-11-08 08:13:15 +0000 | [diff] [blame] | 1146 | MDNode *N = dyn_cast<MDNode>(DVI->getVariable()); |
| 1147 | if (!N) |
| 1148 | return; |
Manman Ren | b46e550 | 2013-11-17 19:35:03 +0000 | [diff] [blame] | 1149 | InitializeTypeMap(M); |
Bill Wendling | 523bea8 | 2013-11-08 08:13:15 +0000 | [diff] [blame] | 1150 | |
| 1151 | DIDescriptor DV(N); |
| 1152 | if (!DV.isVariable()) |
| 1153 | return; |
| 1154 | |
David Blaikie | 70573dc | 2014-11-19 07:49:26 +0000 | [diff] [blame] | 1155 | if (!NodesSeen.insert(DV).second) |
Bill Wendling | 523bea8 | 2013-11-08 08:13:15 +0000 | [diff] [blame] | 1156 | return; |
| 1157 | processScope(DIVariable(N).getContext()); |
Adrian Prantl | 1a1647c | 2014-03-18 02:34:58 +0000 | [diff] [blame] | 1158 | processType(DIVariable(N).getType().resolve(TypeIdentifierMap)); |
Bill Wendling | 523bea8 | 2013-11-08 08:13:15 +0000 | [diff] [blame] | 1159 | } |
| 1160 | |
Bill Wendling | 523bea8 | 2013-11-08 08:13:15 +0000 | [diff] [blame] | 1161 | bool DebugInfoFinder::addType(DIType DT) { |
| 1162 | if (!DT) |
| 1163 | return false; |
| 1164 | |
David Blaikie | 70573dc | 2014-11-19 07:49:26 +0000 | [diff] [blame] | 1165 | if (!NodesSeen.insert(DT).second) |
Bill Wendling | 523bea8 | 2013-11-08 08:13:15 +0000 | [diff] [blame] | 1166 | return false; |
| 1167 | |
| 1168 | TYs.push_back(DT); |
| 1169 | return true; |
| 1170 | } |
| 1171 | |
Bill Wendling | 523bea8 | 2013-11-08 08:13:15 +0000 | [diff] [blame] | 1172 | bool DebugInfoFinder::addCompileUnit(DICompileUnit CU) { |
| 1173 | if (!CU) |
| 1174 | return false; |
David Blaikie | 70573dc | 2014-11-19 07:49:26 +0000 | [diff] [blame] | 1175 | if (!NodesSeen.insert(CU).second) |
Bill Wendling | 523bea8 | 2013-11-08 08:13:15 +0000 | [diff] [blame] | 1176 | return false; |
| 1177 | |
| 1178 | CUs.push_back(CU); |
| 1179 | return true; |
| 1180 | } |
| 1181 | |
Bill Wendling | 523bea8 | 2013-11-08 08:13:15 +0000 | [diff] [blame] | 1182 | bool DebugInfoFinder::addGlobalVariable(DIGlobalVariable DIG) { |
| 1183 | if (!DIG) |
| 1184 | return false; |
| 1185 | |
David Blaikie | 70573dc | 2014-11-19 07:49:26 +0000 | [diff] [blame] | 1186 | if (!NodesSeen.insert(DIG).second) |
Bill Wendling | 523bea8 | 2013-11-08 08:13:15 +0000 | [diff] [blame] | 1187 | return false; |
| 1188 | |
| 1189 | GVs.push_back(DIG); |
| 1190 | return true; |
| 1191 | } |
| 1192 | |
Bill Wendling | 523bea8 | 2013-11-08 08:13:15 +0000 | [diff] [blame] | 1193 | bool DebugInfoFinder::addSubprogram(DISubprogram SP) { |
| 1194 | if (!SP) |
| 1195 | return false; |
| 1196 | |
David Blaikie | 70573dc | 2014-11-19 07:49:26 +0000 | [diff] [blame] | 1197 | if (!NodesSeen.insert(SP).second) |
Bill Wendling | 523bea8 | 2013-11-08 08:13:15 +0000 | [diff] [blame] | 1198 | return false; |
| 1199 | |
| 1200 | SPs.push_back(SP); |
| 1201 | return true; |
| 1202 | } |
| 1203 | |
| 1204 | bool DebugInfoFinder::addScope(DIScope Scope) { |
| 1205 | if (!Scope) |
| 1206 | return false; |
| 1207 | // FIXME: Ocaml binding generates a scope with no content, we treat it |
| 1208 | // as null for now. |
| 1209 | if (Scope->getNumOperands() == 0) |
| 1210 | return false; |
David Blaikie | 70573dc | 2014-11-19 07:49:26 +0000 | [diff] [blame] | 1211 | if (!NodesSeen.insert(Scope).second) |
Bill Wendling | 523bea8 | 2013-11-08 08:13:15 +0000 | [diff] [blame] | 1212 | return false; |
| 1213 | Scopes.push_back(Scope); |
| 1214 | return true; |
| 1215 | } |
| 1216 | |
| 1217 | //===----------------------------------------------------------------------===// |
| 1218 | // DIDescriptor: dump routines for all descriptors. |
| 1219 | //===----------------------------------------------------------------------===// |
| 1220 | |
Bill Wendling | 523bea8 | 2013-11-08 08:13:15 +0000 | [diff] [blame] | 1221 | void DIDescriptor::dump() const { |
| 1222 | print(dbgs()); |
| 1223 | dbgs() << '\n'; |
| 1224 | } |
| 1225 | |
Bill Wendling | 523bea8 | 2013-11-08 08:13:15 +0000 | [diff] [blame] | 1226 | void DIDescriptor::print(raw_ostream &OS) const { |
| 1227 | if (!DbgNode) |
| 1228 | return; |
| 1229 | |
| 1230 | if (const char *Tag = dwarf::TagString(getTag())) |
| 1231 | OS << "[ " << Tag << " ]"; |
| 1232 | |
| 1233 | if (this->isSubrange()) { |
| 1234 | DISubrange(DbgNode).printInternal(OS); |
| 1235 | } else if (this->isCompileUnit()) { |
| 1236 | DICompileUnit(DbgNode).printInternal(OS); |
| 1237 | } else if (this->isFile()) { |
| 1238 | DIFile(DbgNode).printInternal(OS); |
| 1239 | } else if (this->isEnumerator()) { |
| 1240 | DIEnumerator(DbgNode).printInternal(OS); |
| 1241 | } else if (this->isBasicType()) { |
| 1242 | DIType(DbgNode).printInternal(OS); |
| 1243 | } else if (this->isDerivedType()) { |
| 1244 | DIDerivedType(DbgNode).printInternal(OS); |
| 1245 | } else if (this->isCompositeType()) { |
| 1246 | DICompositeType(DbgNode).printInternal(OS); |
| 1247 | } else if (this->isSubprogram()) { |
| 1248 | DISubprogram(DbgNode).printInternal(OS); |
| 1249 | } else if (this->isGlobalVariable()) { |
| 1250 | DIGlobalVariable(DbgNode).printInternal(OS); |
| 1251 | } else if (this->isVariable()) { |
| 1252 | DIVariable(DbgNode).printInternal(OS); |
| 1253 | } else if (this->isObjCProperty()) { |
| 1254 | DIObjCProperty(DbgNode).printInternal(OS); |
| 1255 | } else if (this->isNameSpace()) { |
| 1256 | DINameSpace(DbgNode).printInternal(OS); |
| 1257 | } else if (this->isScope()) { |
| 1258 | DIScope(DbgNode).printInternal(OS); |
Adrian Prantl | 87b7eb9 | 2014-10-01 18:55:02 +0000 | [diff] [blame] | 1259 | } else if (this->isExpression()) { |
| 1260 | DIExpression(DbgNode).printInternal(OS); |
Bill Wendling | 523bea8 | 2013-11-08 08:13:15 +0000 | [diff] [blame] | 1261 | } |
| 1262 | } |
| 1263 | |
| 1264 | void DISubrange::printInternal(raw_ostream &OS) const { |
| 1265 | int64_t Count = getCount(); |
| 1266 | if (Count != -1) |
| 1267 | OS << " [" << getLo() << ", " << Count - 1 << ']'; |
| 1268 | else |
| 1269 | OS << " [unbounded]"; |
| 1270 | } |
| 1271 | |
| 1272 | void DIScope::printInternal(raw_ostream &OS) const { |
| 1273 | OS << " [" << getDirectory() << "/" << getFilename() << ']'; |
| 1274 | } |
| 1275 | |
| 1276 | void DICompileUnit::printInternal(raw_ostream &OS) const { |
| 1277 | DIScope::printInternal(OS); |
| 1278 | OS << " ["; |
| 1279 | unsigned Lang = getLanguage(); |
| 1280 | if (const char *LangStr = dwarf::LanguageString(Lang)) |
| 1281 | OS << LangStr; |
| 1282 | else |
| 1283 | (OS << "lang 0x").write_hex(Lang); |
| 1284 | OS << ']'; |
| 1285 | } |
| 1286 | |
| 1287 | void DIEnumerator::printInternal(raw_ostream &OS) const { |
| 1288 | OS << " [" << getName() << " :: " << getEnumValue() << ']'; |
| 1289 | } |
| 1290 | |
| 1291 | void DIType::printInternal(raw_ostream &OS) const { |
Manman Ren | f93ac4b | 2014-07-29 18:20:39 +0000 | [diff] [blame] | 1292 | if (!DbgNode) |
Bill Wendling | 523bea8 | 2013-11-08 08:13:15 +0000 | [diff] [blame] | 1293 | return; |
| 1294 | |
| 1295 | StringRef Res = getName(); |
| 1296 | if (!Res.empty()) |
| 1297 | OS << " [" << Res << "]"; |
| 1298 | |
| 1299 | // TODO: Print context? |
| 1300 | |
| 1301 | OS << " [line " << getLineNumber() << ", size " << getSizeInBits() |
| 1302 | << ", align " << getAlignInBits() << ", offset " << getOffsetInBits(); |
| 1303 | if (isBasicType()) |
| 1304 | if (const char *Enc = |
| 1305 | dwarf::AttributeEncodingString(DIBasicType(DbgNode).getEncoding())) |
| 1306 | OS << ", enc " << Enc; |
| 1307 | OS << "]"; |
| 1308 | |
| 1309 | if (isPrivate()) |
| 1310 | OS << " [private]"; |
| 1311 | else if (isProtected()) |
| 1312 | OS << " [protected]"; |
Adrian Prantl | daedfda | 2014-08-29 22:44:07 +0000 | [diff] [blame] | 1313 | else if (isPublic()) |
| 1314 | OS << " [public]"; |
Bill Wendling | 523bea8 | 2013-11-08 08:13:15 +0000 | [diff] [blame] | 1315 | |
| 1316 | if (isArtificial()) |
| 1317 | OS << " [artificial]"; |
| 1318 | |
| 1319 | if (isForwardDecl()) |
| 1320 | OS << " [decl]"; |
| 1321 | else if (getTag() == dwarf::DW_TAG_structure_type || |
| 1322 | getTag() == dwarf::DW_TAG_union_type || |
| 1323 | getTag() == dwarf::DW_TAG_enumeration_type || |
| 1324 | getTag() == dwarf::DW_TAG_class_type) |
| 1325 | OS << " [def]"; |
| 1326 | if (isVector()) |
| 1327 | OS << " [vector]"; |
| 1328 | if (isStaticMember()) |
| 1329 | OS << " [static]"; |
Adrian Prantl | 99c7af2 | 2013-12-18 21:48:19 +0000 | [diff] [blame] | 1330 | |
| 1331 | if (isLValueReference()) |
| 1332 | OS << " [reference]"; |
| 1333 | |
| 1334 | if (isRValueReference()) |
| 1335 | OS << " [rvalue reference]"; |
Bill Wendling | 523bea8 | 2013-11-08 08:13:15 +0000 | [diff] [blame] | 1336 | } |
| 1337 | |
| 1338 | void DIDerivedType::printInternal(raw_ostream &OS) const { |
| 1339 | DIType::printInternal(OS); |
| 1340 | OS << " [from " << getTypeDerivedFrom().getName() << ']'; |
| 1341 | } |
| 1342 | |
| 1343 | void DICompositeType::printInternal(raw_ostream &OS) const { |
| 1344 | DIType::printInternal(OS); |
Manman Ren | ab8ffba | 2014-07-28 19:14:13 +0000 | [diff] [blame] | 1345 | DIArray A = getElements(); |
Bill Wendling | 523bea8 | 2013-11-08 08:13:15 +0000 | [diff] [blame] | 1346 | OS << " [" << A.getNumElements() << " elements]"; |
| 1347 | } |
| 1348 | |
| 1349 | void DINameSpace::printInternal(raw_ostream &OS) const { |
| 1350 | StringRef Name = getName(); |
| 1351 | if (!Name.empty()) |
| 1352 | OS << " [" << Name << ']'; |
| 1353 | |
| 1354 | OS << " [line " << getLineNumber() << ']'; |
| 1355 | } |
| 1356 | |
| 1357 | void DISubprogram::printInternal(raw_ostream &OS) const { |
| 1358 | // TODO : Print context |
| 1359 | OS << " [line " << getLineNumber() << ']'; |
| 1360 | |
| 1361 | if (isLocalToUnit()) |
| 1362 | OS << " [local]"; |
| 1363 | |
| 1364 | if (isDefinition()) |
| 1365 | OS << " [def]"; |
| 1366 | |
| 1367 | if (getScopeLineNumber() != getLineNumber()) |
| 1368 | OS << " [scope " << getScopeLineNumber() << "]"; |
| 1369 | |
| 1370 | if (isPrivate()) |
| 1371 | OS << " [private]"; |
| 1372 | else if (isProtected()) |
| 1373 | OS << " [protected]"; |
Adrian Prantl | daedfda | 2014-08-29 22:44:07 +0000 | [diff] [blame] | 1374 | else if (isPublic()) |
| 1375 | OS << " [public]"; |
Bill Wendling | 523bea8 | 2013-11-08 08:13:15 +0000 | [diff] [blame] | 1376 | |
Adrian Prantl | 99c7af2 | 2013-12-18 21:48:19 +0000 | [diff] [blame] | 1377 | if (isLValueReference()) |
| 1378 | OS << " [reference]"; |
| 1379 | |
| 1380 | if (isRValueReference()) |
| 1381 | OS << " [rvalue reference]"; |
| 1382 | |
Bill Wendling | 523bea8 | 2013-11-08 08:13:15 +0000 | [diff] [blame] | 1383 | StringRef Res = getName(); |
| 1384 | if (!Res.empty()) |
| 1385 | OS << " [" << Res << ']'; |
| 1386 | } |
| 1387 | |
| 1388 | void DIGlobalVariable::printInternal(raw_ostream &OS) const { |
| 1389 | StringRef Res = getName(); |
| 1390 | if (!Res.empty()) |
| 1391 | OS << " [" << Res << ']'; |
| 1392 | |
| 1393 | OS << " [line " << getLineNumber() << ']'; |
| 1394 | |
| 1395 | // TODO : Print context |
| 1396 | |
| 1397 | if (isLocalToUnit()) |
| 1398 | OS << " [local]"; |
| 1399 | |
| 1400 | if (isDefinition()) |
| 1401 | OS << " [def]"; |
| 1402 | } |
| 1403 | |
| 1404 | void DIVariable::printInternal(raw_ostream &OS) const { |
| 1405 | StringRef Res = getName(); |
| 1406 | if (!Res.empty()) |
| 1407 | OS << " [" << Res << ']'; |
| 1408 | |
| 1409 | OS << " [line " << getLineNumber() << ']'; |
Adrian Prantl | 87b7eb9 | 2014-10-01 18:55:02 +0000 | [diff] [blame] | 1410 | } |
Adrian Prantl | b141683 | 2014-08-01 22:11:58 +0000 | [diff] [blame] | 1411 | |
Adrian Prantl | 87b7eb9 | 2014-10-01 18:55:02 +0000 | [diff] [blame] | 1412 | void DIExpression::printInternal(raw_ostream &OS) const { |
Adrian Prantl | 4bd0a0c | 2015-01-23 21:24:41 +0000 | [diff] [blame] | 1413 | for (auto Op : *this) { |
| 1414 | OS << " [" << OperationEncodingString(Op); |
| 1415 | switch (Op) { |
Adrian Prantl | 87b7eb9 | 2014-10-01 18:55:02 +0000 | [diff] [blame] | 1416 | case DW_OP_plus: { |
Adrian Prantl | 4bd0a0c | 2015-01-23 21:24:41 +0000 | [diff] [blame] | 1417 | OS << " " << Op.getArg(1); |
Adrian Prantl | 87b7eb9 | 2014-10-01 18:55:02 +0000 | [diff] [blame] | 1418 | break; |
| 1419 | } |
Adrian Prantl | 27bd01f | 2015-02-09 23:57:15 +0000 | [diff] [blame] | 1420 | case DW_OP_bit_piece: { |
Adrian Prantl | 4bd0a0c | 2015-01-23 21:24:41 +0000 | [diff] [blame] | 1421 | OS << " offset=" << Op.getArg(1) << ", size=" << Op.getArg(2); |
Adrian Prantl | 87b7eb9 | 2014-10-01 18:55:02 +0000 | [diff] [blame] | 1422 | break; |
| 1423 | } |
Adrian Prantl | b9a88e2 | 2014-12-05 18:19:38 +0000 | [diff] [blame] | 1424 | case DW_OP_deref: |
| 1425 | // No arguments. |
| 1426 | break; |
Adrian Prantl | 87b7eb9 | 2014-10-01 18:55:02 +0000 | [diff] [blame] | 1427 | default: |
Adrian Prantl | 0d7d8e4 | 2015-01-22 16:55:22 +0000 | [diff] [blame] | 1428 | llvm_unreachable("unhandled operation"); |
Adrian Prantl | 87b7eb9 | 2014-10-01 18:55:02 +0000 | [diff] [blame] | 1429 | } |
| 1430 | OS << "]"; |
| 1431 | } |
Bill Wendling | 523bea8 | 2013-11-08 08:13:15 +0000 | [diff] [blame] | 1432 | } |
| 1433 | |
| 1434 | void DIObjCProperty::printInternal(raw_ostream &OS) const { |
| 1435 | StringRef Name = getObjCPropertyName(); |
| 1436 | if (!Name.empty()) |
| 1437 | OS << " [" << Name << ']'; |
| 1438 | |
| 1439 | OS << " [line " << getLineNumber() << ", properties " << getUnsignedField(6) |
| 1440 | << ']'; |
| 1441 | } |
| 1442 | |
| 1443 | static void printDebugLoc(DebugLoc DL, raw_ostream &CommentOS, |
| 1444 | const LLVMContext &Ctx) { |
| 1445 | if (!DL.isUnknown()) { // Print source line info. |
| 1446 | DIScope Scope(DL.getScope(Ctx)); |
| 1447 | assert(Scope.isScope() && "Scope of a DebugLoc should be a DIScope."); |
| 1448 | // Omit the directory, because it's likely to be long and uninteresting. |
| 1449 | CommentOS << Scope.getFilename(); |
| 1450 | CommentOS << ':' << DL.getLine(); |
| 1451 | if (DL.getCol() != 0) |
| 1452 | CommentOS << ':' << DL.getCol(); |
| 1453 | DebugLoc InlinedAtDL = DebugLoc::getFromDILocation(DL.getInlinedAt(Ctx)); |
| 1454 | if (!InlinedAtDL.isUnknown()) { |
| 1455 | CommentOS << " @[ "; |
| 1456 | printDebugLoc(InlinedAtDL, CommentOS, Ctx); |
| 1457 | CommentOS << " ]"; |
| 1458 | } |
| 1459 | } |
| 1460 | } |
| 1461 | |
| 1462 | void DIVariable::printExtendedName(raw_ostream &OS) const { |
| 1463 | const LLVMContext &Ctx = DbgNode->getContext(); |
| 1464 | StringRef Res = getName(); |
| 1465 | if (!Res.empty()) |
| 1466 | OS << Res << "," << getLineNumber(); |
| 1467 | if (MDNode *InlinedAt = getInlinedAt()) { |
| 1468 | DebugLoc InlinedAtDL = DebugLoc::getFromDILocation(InlinedAt); |
| 1469 | if (!InlinedAtDL.isUnknown()) { |
| 1470 | OS << " @["; |
| 1471 | printDebugLoc(InlinedAtDL, OS, Ctx); |
| 1472 | OS << "]"; |
| 1473 | } |
| 1474 | } |
| 1475 | } |
| 1476 | |
Duncan P. N. Exon Smith | c81307a | 2014-11-14 23:55:03 +0000 | [diff] [blame] | 1477 | template <> DIRef<DIScope>::DIRef(const Metadata *V) : Val(V) { |
Bill Wendling | 523bea8 | 2013-11-08 08:13:15 +0000 | [diff] [blame] | 1478 | assert(isScopeRef(V) && "DIScopeRef should be a MDString or MDNode"); |
| 1479 | } |
Duncan P. N. Exon Smith | c81307a | 2014-11-14 23:55:03 +0000 | [diff] [blame] | 1480 | template <> DIRef<DIType>::DIRef(const Metadata *V) : Val(V) { |
Bill Wendling | 523bea8 | 2013-11-08 08:13:15 +0000 | [diff] [blame] | 1481 | assert(isTypeRef(V) && "DITypeRef should be a MDString or MDNode"); |
| 1482 | } |
| 1483 | |
Bill Wendling | 523bea8 | 2013-11-08 08:13:15 +0000 | [diff] [blame] | 1484 | template <> |
| 1485 | DIScopeRef DIDescriptor::getFieldAs<DIScopeRef>(unsigned Elt) const { |
Duncan P. N. Exon Smith | c81307a | 2014-11-14 23:55:03 +0000 | [diff] [blame] | 1486 | return DIScopeRef(cast_or_null<Metadata>(getField(DbgNode, Elt))); |
Bill Wendling | 523bea8 | 2013-11-08 08:13:15 +0000 | [diff] [blame] | 1487 | } |
Bill Wendling | 523bea8 | 2013-11-08 08:13:15 +0000 | [diff] [blame] | 1488 | template <> DITypeRef DIDescriptor::getFieldAs<DITypeRef>(unsigned Elt) const { |
Duncan P. N. Exon Smith | c81307a | 2014-11-14 23:55:03 +0000 | [diff] [blame] | 1489 | return DITypeRef(cast_or_null<Metadata>(getField(DbgNode, Elt))); |
Bill Wendling | 523bea8 | 2013-11-08 08:13:15 +0000 | [diff] [blame] | 1490 | } |
Manman Ren | cb14bbc | 2013-11-22 22:06:31 +0000 | [diff] [blame] | 1491 | |
Manman Ren | cb14bbc | 2013-11-22 22:06:31 +0000 | [diff] [blame] | 1492 | bool llvm::StripDebugInfo(Module &M) { |
Manman Ren | cb14bbc | 2013-11-22 22:06:31 +0000 | [diff] [blame] | 1493 | bool Changed = false; |
| 1494 | |
| 1495 | // Remove all of the calls to the debugger intrinsics, and remove them from |
| 1496 | // the module. |
| 1497 | if (Function *Declare = M.getFunction("llvm.dbg.declare")) { |
| 1498 | while (!Declare->use_empty()) { |
Chandler Carruth | cdf4788 | 2014-03-09 03:16:01 +0000 | [diff] [blame] | 1499 | CallInst *CI = cast<CallInst>(Declare->user_back()); |
Manman Ren | cb14bbc | 2013-11-22 22:06:31 +0000 | [diff] [blame] | 1500 | CI->eraseFromParent(); |
| 1501 | } |
| 1502 | Declare->eraseFromParent(); |
| 1503 | Changed = true; |
| 1504 | } |
| 1505 | |
| 1506 | if (Function *DbgVal = M.getFunction("llvm.dbg.value")) { |
| 1507 | while (!DbgVal->use_empty()) { |
Chandler Carruth | cdf4788 | 2014-03-09 03:16:01 +0000 | [diff] [blame] | 1508 | CallInst *CI = cast<CallInst>(DbgVal->user_back()); |
Manman Ren | cb14bbc | 2013-11-22 22:06:31 +0000 | [diff] [blame] | 1509 | CI->eraseFromParent(); |
| 1510 | } |
| 1511 | DbgVal->eraseFromParent(); |
| 1512 | Changed = true; |
| 1513 | } |
| 1514 | |
| 1515 | for (Module::named_metadata_iterator NMI = M.named_metadata_begin(), |
| 1516 | NME = M.named_metadata_end(); NMI != NME;) { |
| 1517 | NamedMDNode *NMD = NMI; |
| 1518 | ++NMI; |
| 1519 | if (NMD->getName().startswith("llvm.dbg.")) { |
| 1520 | NMD->eraseFromParent(); |
| 1521 | Changed = true; |
| 1522 | } |
| 1523 | } |
| 1524 | |
| 1525 | for (Module::iterator MI = M.begin(), ME = M.end(); MI != ME; ++MI) |
| 1526 | for (Function::iterator FI = MI->begin(), FE = MI->end(); FI != FE; |
| 1527 | ++FI) |
| 1528 | for (BasicBlock::iterator BI = FI->begin(), BE = FI->end(); BI != BE; |
| 1529 | ++BI) { |
| 1530 | if (!BI->getDebugLoc().isUnknown()) { |
| 1531 | Changed = true; |
| 1532 | BI->setDebugLoc(DebugLoc()); |
| 1533 | } |
| 1534 | } |
| 1535 | |
| 1536 | return Changed; |
| 1537 | } |
Manman Ren | 8b4306c | 2013-12-02 21:29:56 +0000 | [diff] [blame] | 1538 | |
Manman Ren | bd4daf8 | 2013-12-03 00:12:14 +0000 | [diff] [blame] | 1539 | unsigned llvm::getDebugMetadataVersionFromModule(const Module &M) { |
David Majnemer | e7a9cdb | 2015-02-16 06:04:53 +0000 | [diff] [blame^] | 1540 | if (auto *Val = mdconst::dyn_extract_or_null<ConstantInt>( |
Duncan P. N. Exon Smith | 5bf8fef | 2014-12-09 18:38:53 +0000 | [diff] [blame] | 1541 | M.getModuleFlag("Debug Info Version"))) |
| 1542 | return Val->getZExtValue(); |
| 1543 | return 0; |
Manman Ren | 8b4306c | 2013-12-02 21:29:56 +0000 | [diff] [blame] | 1544 | } |
David Blaikie | 6876b3b | 2014-07-01 20:05:26 +0000 | [diff] [blame] | 1545 | |
David Blaikie | a8c3509 | 2014-07-02 18:30:05 +0000 | [diff] [blame] | 1546 | llvm::DenseMap<const llvm::Function *, llvm::DISubprogram> |
| 1547 | llvm::makeSubprogramMap(const Module &M) { |
| 1548 | DenseMap<const Function *, DISubprogram> R; |
David Blaikie | 6876b3b | 2014-07-01 20:05:26 +0000 | [diff] [blame] | 1549 | |
| 1550 | NamedMDNode *CU_Nodes = M.getNamedMetadata("llvm.dbg.cu"); |
| 1551 | if (!CU_Nodes) |
| 1552 | return R; |
| 1553 | |
Duncan P. N. Exon Smith | de36e80 | 2014-11-11 21:30:22 +0000 | [diff] [blame] | 1554 | for (MDNode *N : CU_Nodes->operands()) { |
| 1555 | DICompileUnit CUNode(N); |
David Blaikie | 6876b3b | 2014-07-01 20:05:26 +0000 | [diff] [blame] | 1556 | DIArray SPs = CUNode.getSubprograms(); |
| 1557 | for (unsigned i = 0, e = SPs.getNumElements(); i != e; ++i) { |
| 1558 | DISubprogram SP(SPs.getElement(i)); |
| 1559 | if (Function *F = SP.getFunction()) |
| 1560 | R.insert(std::make_pair(F, SP)); |
| 1561 | } |
| 1562 | } |
| 1563 | return R; |
| 1564 | } |