From 0baa2b484d97bb61cfddc7e4df83a9c03667b557 Mon Sep 17 00:00:00 2001 From: Matt Pharr Date: Wed, 25 Apr 2012 08:41:28 -1000 Subject: [PATCH] Fix multiple bugs related to DIBuilder::createFunction() call. The DIType passed to this method should correspond to the FunctionType of the function, not its return type. The first parameter should be the DIScope for the compile unit, not the DIFile. We previously had the unmangled function name and the mangled function name interchanged. The argument corresponding to "first line number of the function" was missing, which in turn led to subsequent arguments being off, and thus providing bogus values vs. what was supposed to be passed. Rename FunctionEmitContext::diFunction to diSubprogram, to better reflect its type. --- ctx.cpp | 50 +++++++++++++++++++++++++++++++++++--------------- ctx.h | 2 +- 2 files changed, 36 insertions(+), 16 deletions(-) diff --git a/ctx.cpp b/ctx.cpp index c805bcf1..1b61fdac 100644 --- a/ctx.cpp +++ b/ctx.cpp @@ -297,23 +297,43 @@ FunctionEmitContext::FunctionEmitContext(Function *func, Symbol *funSym, } if (m->diBuilder) { + currentPos = funSym->pos; + /* If debugging is enabled, tell the debug information emission code about this new function */ diFile = funcStartPos.GetDIFile(); - llvm::DIType retType = function->GetReturnType()->GetDIType(diFile); - int flags = llvm::DIDescriptor::FlagPrototyped; // ?? - diFunction = m->diBuilder->createFunction(diFile, /* scope */ - llvmFunction->getName(), // mangled - funSym->name, - diFile, - funcStartPos.first_line, - retType, - funSym->storageClass == SC_STATIC, - true, /* is definition */ - flags, - g->opt.level > 0, - llvmFunction); Assert(diFile.Verify()); + + llvm::DIScope scope = llvm::DIScope(m->diBuilder->getCU()); + Assert(scope.Verify()); + + const FunctionType *functionType = function->GetType(); + llvm::DIType diSubprogramType; + if (functionType == NULL) + Assert(m->errorCount > 0); + else { + diSubprogramType = functionType->GetDIType(scope); + Assert(diSubprogramType.Verify()); + } + + std::string mangledName = llvmFunction->getName(); + if (mangledName == funSym->name) + mangledName = ""; + + bool isStatic = (funSym->storageClass == SC_STATIC); + bool isOptimized = (g->opt.level > 0); + int firstLine = funcStartPos.first_line; + int flags = (llvm::DIDescriptor::FlagPrototyped); + + diSubprogram = + m->diBuilder->createFunction(diFile /* scope */, funSym->name, + mangledName, diFile, + firstLine, diSubprogramType, + isStatic, true, /* is defn */ + firstLine, flags, + isOptimized, llvmFunction); + Assert(diSubprogram.Verify()); + /* And start a scope representing the initial function scope */ StartScope(); @@ -1439,7 +1459,7 @@ FunctionEmitContext::StartScope() { if (debugScopes.size() > 0) parentScope = debugScopes.back(); else - parentScope = diFunction; + parentScope = diSubprogram; llvm::DILexicalBlock lexicalBlock = m->diBuilder->createLexicalBlock(parentScope, diFile, @@ -1495,7 +1515,7 @@ FunctionEmitContext::EmitFunctionParameterDebugInfo(Symbol *sym, int argNum) { if (m->diBuilder == NULL) return; - llvm::DIScope scope = diFunction; + llvm::DIScope scope = diSubprogram; llvm::DIType diType = sym->type->GetDIType(scope); Assert(diType.Verify()); int flags = 0; diff --git a/ctx.h b/ctx.h index e161e366..304f8af1 100644 --- a/ctx.h +++ b/ctx.h @@ -641,7 +641,7 @@ private: /** DISubprogram corresponding to this function (used for debugging info). */ - llvm::DISubprogram diFunction; + llvm::DISubprogram diSubprogram; /** These correspond to the current set of nested scopes in the function. */