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