}
void CGDebugInfo::EmitFunctionDecl(GlobalDecl GD, SourceLocation Loc,
- QualType FnType) {
+ QualType FnType, llvm::Function *Fn) {
StringRef Name;
StringRef LinkageName;
llvm::DINode::DIFlags Flags = llvm::DINode::FlagZero;
llvm::DIFile *Unit = getOrCreateFile(Loc);
- llvm::DIScope *FDContext = getDeclContextDescriptor(D);
+ bool IsDeclForCallSite = Fn ? true : false;
+ llvm::DIScope *FDContext =
+ IsDeclForCallSite ? Unit : getDeclContextDescriptor(D);
llvm::DINodeArray TParamsArray;
if (isa<FunctionDecl>(D)) {
// If there is a DISubprogram for this function available then use it.
if (CGM.getLangOpts().Optimize)
SPFlags |= llvm::DISubprogram::SPFlagOptimized;
- DBuilder.retainType(DBuilder.createFunction(
+ llvm::DISubprogram *SP = DBuilder.createFunction(
FDContext, Name, LinkageName, Unit, LineNo,
getOrCreateFunctionType(D, FnType, Unit), ScopeLine, Flags, SPFlags,
- TParamsArray.get(), getFunctionDeclaration(D)));
+ TParamsArray.get(), getFunctionDeclaration(D));
+
+ if (IsDeclForCallSite)
+ Fn->setSubprogram(SP);
+
+ DBuilder.retainType(SP);
+}
+
+void CGDebugInfo::EmitFuncDeclForCallSite(llvm::CallBase *CallOrInvoke,
+ QualType CalleeType,
+ const FunctionDecl *CalleeDecl) {
+ auto &CGOpts = CGM.getCodeGenOpts();
+ if (!CGOpts.EnableDebugEntryValues || !CGM.getLangOpts().Optimize ||
+ !CallOrInvoke ||
+ CGM.getCodeGenOpts().getDebugInfo() < codegenoptions::LimitedDebugInfo)
+ return;
+
+ auto *Func = CallOrInvoke->getCalledFunction();
+ if (!Func)
+ return;
+
+ // If there is no DISubprogram attached to the function being called,
+ // create the one describing the function in order to have complete
+ // call site debug info.
+ if (Func->getSubprogram())
+ return;
+
+ if (!CalleeDecl->isStatic() && !CalleeDecl->isInlined())
+ EmitFunctionDecl(CalleeDecl, CalleeDecl->getLocation(), CalleeType, Func);
}
void CGDebugInfo::EmitInlineFunctionStart(CGBuilderTy &Builder, GlobalDecl GD) {
void EmitInlineFunctionEnd(CGBuilderTy &Builder);
/// Emit debug info for a function declaration.
- void EmitFunctionDecl(GlobalDecl GD, SourceLocation Loc, QualType FnType);
+ /// \p Fn is set only when a declaration for a debug call site gets created.
+ void EmitFunctionDecl(GlobalDecl GD, SourceLocation Loc,
+ QualType FnType, llvm::Function *Fn = nullptr);
+
+ /// Emit debug info for an extern function being called.
+ /// This is needed for call site debug info.
+ void EmitFuncDeclForCallSite(llvm::CallBase *CallOrInvoke,
+ QualType CalleeType,
+ const FunctionDecl *CalleeDecl);
/// Constructs the debug code for exiting a function.
void EmitFunctionEnd(CGBuilderTy &Builder, llvm::Function *Fn);
Callee.setFunctionPointer(CalleePtr);
}
- return EmitCall(FnInfo, Callee, ReturnValue, Args, nullptr, E->getExprLoc());
+ llvm::CallBase *CallOrInvoke = nullptr;
+ RValue Call = EmitCall(FnInfo, Callee, ReturnValue, Args, &CallOrInvoke,
+ E->getExprLoc());
+
+ // Generate function declaration DISuprogram in order to be used
+ // in debug info about call sites.
+ if (CGDebugInfo *DI = getDebugInfo()) {
+ if (auto *CalleeDecl = dyn_cast_or_null<FunctionDecl>(TargetDecl))
+ DI->EmitFuncDeclForCallSite(CallOrInvoke, QualType(FnType, 0),
+ CalleeDecl);
+ }
+
+ return Call;
}
LValue CodeGenFunction::