//===--- DIBuilder.cpp - Debug Information Builder ------------------------===//
//
// The LLVM Compiler Infrastructure
//
// This file is distributed under the University of Illinois Open Source
// License. See LICENSE.TXT for details.
//
//===----------------------------------------------------------------------===//
//
// This file implements the DIBuilder.
//
//===----------------------------------------------------------------------===//
#include "llvm/DIBuilder.h"
#include "llvm/ADT/STLExtras.h"
#include "llvm/DebugInfo.h"
#include "llvm/IR/Constants.h"
#include "llvm/IR/IntrinsicInst.h"
#include "llvm/IR/Module.h"
#include "llvm/Support/Debug.h"
#include "llvm/Support/Dwarf.h"
using namespace llvm;
using namespace llvm::dwarf;
static Constant *GetTagConstant(LLVMContext &VMContext, unsigned Tag) {
assert((Tag & LLVMDebugVersionMask) == 0 &&
"Tag too large for debug encoding!");
return ConstantInt::get(Type::getInt32Ty(VMContext), Tag | LLVMDebugVersion);
}
DIBuilder::DIBuilder(Module &m)
: M(m), VMContext(M.getContext()), TheCU(0), TempEnumTypes(0),
TempRetainTypes(0), TempSubprograms(0), TempGVs(0), DeclareFn(0),
ValueFn(0)
{}
/// finalize - Construct any deferred debug info descriptors.
void DIBuilder::finalize() {
DIArray Enums = getOrCreateArray(AllEnumTypes);
DIType(TempEnumTypes).replaceAllUsesWith(Enums);
DIArray RetainTypes = getOrCreateArray(AllRetainTypes);
DIType(TempRetainTypes).replaceAllUsesWith(RetainTypes);
DIArray SPs = getOrCreateArray(AllSubprograms);
DIType(TempSubprograms).replaceAllUsesWith(SPs);
for (unsigned i = 0, e = SPs.getNumElements(); i != e; ++i) {
DISubprogram SP(SPs.getElement(i));
SmallVector<Value *, 4> Variables;
if (NamedMDNode *NMD = getFnSpecificMDNode(M, SP)) {
for (unsigned ii = 0, ee = NMD->getNumOperands(); ii != ee; ++ii)
Variables.push_back(NMD->getOperand(ii));
NMD->eraseFromParent();
}
if (MDNode *Temp = SP.getVariablesNodes()) {
DIArray AV = getOrCreateArray(Variables);
DIType(Temp).replaceAllUsesWith(AV);
}
}
DIArray GVs = getOrCreateArray(AllGVs);
DIType(TempGVs).replaceAllUsesWith(GVs);
DIArray IMs = getOrCreateArray(AllImportedModules);
DIType(TempImportedModules).replaceAllUsesWith(IMs);
}
/// getNonCompileUnitScope - If N is compile unit return NULL otherwise return
/// N.
static MDNode *getNonCompileUnitScope(MDNode *N) {
if (DIDescriptor(N).isCompileUnit())
return NULL;
return N;
}
static MDNode *createFilePathPair(LLVMContext &VMContext, StringRef Filename,
StringRef Directory) {
assert(!Filename.empty() && "Unable to create file without name");
Value *Pair[] = {
MDString::get(VMContext, Filename),
MDString::get(VMContext, Directory),
};
return MDNode::get(VMContext, Pair);
}
/// createCompileUnit - A CompileUnit provides an anchor for all debugging
/// information generated during this instance of compilation.
void DIBuilder::createCompileUnit(unsigned Lang, StringRef Filename,
StringRef Directory, StringRef Producer,
bool isOptimized, StringRef Flags,
unsigned RunTimeVer, StringRef SplitName) {
assert(((Lang <= dwarf::DW_LANG_Python && Lang >= dwarf::DW_LANG_C89) ||
(Lang <= dwarf::DW_LANG_hi_user && Lang >= dwarf::DW_LANG_lo_user)) &&
"Invalid Language tag");
assert(!Filename.empty()