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