aboutsummaryrefslogtreecommitdiff
path: root/llvm/lib/CodeGen/MachineModuleInfo.cpp
diff options
context:
space:
mode:
authorYuanfang Chen <yuanfang.chen@sony.com>2021-02-23 09:47:15 -0800
committerYuanfang Chen <yuanfang.chen@sony.com>2021-03-01 15:58:37 -0800
commit5de2d189e6ad466a1f0616195e8c524a4eb3cbc0 (patch)
tree861ec0d1385d31afebaaff10f9af7b7e415f8b5e /llvm/lib/CodeGen/MachineModuleInfo.cpp
parent2b5f3f446f36a68dfcee2fa23534044b491a2ad8 (diff)
downloadllvm-5de2d189e6ad466a1f0616195e8c524a4eb3cbc0.zip
llvm-5de2d189e6ad466a1f0616195e8c524a4eb3cbc0.tar.gz
llvm-5de2d189e6ad466a1f0616195e8c524a4eb3cbc0.tar.bz2
[Diagnose] Unify MCContext and LLVMContext diagnosing
The situation with inline asm/MC error reporting is kind of messy at the moment. The errors from MC layout are not reliably propagated and users have to specify an inlineasm handler separately to get inlineasm diagnose. The latter issue is not a correctness issue but could be improved. * Kill LLVMContext inlineasm diagnose handler and migrate it to use DiagnoseInfo/DiagnoseHandler. * Introduce `DiagnoseInfoSrcMgr` to diagnose SourceMgr backed errors. This covers use cases like inlineasm, MC, and any clients using SourceMgr. * Move AsmPrinter::SrcMgrDiagInfo and its instance to MCContext. The next step is to combine MCContext::SrcMgr and MCContext::InlineSrcMgr because in all use cases, only one of them is used. * If LLVMContext is available, let MCContext uses LLVMContext's diagnose handler; if LLVMContext is not available, MCContext uses its own default diagnose handler which just prints SMDiagnostic. * Change a few clients(Clang, llc, lldb) to use the new way of reporting. Reviewed By: MaskRay Differential Revision: https://reviews.llvm.org/D97449
Diffstat (limited to 'llvm/lib/CodeGen/MachineModuleInfo.cpp')
-rw-r--r--llvm/lib/CodeGen/MachineModuleInfo.cpp37
1 files changed, 37 insertions, 0 deletions
diff --git a/llvm/lib/CodeGen/MachineModuleInfo.cpp b/llvm/lib/CodeGen/MachineModuleInfo.cpp
index 0379dbd..8a3aef39 100644
--- a/llvm/lib/CodeGen/MachineModuleInfo.cpp
+++ b/llvm/lib/CodeGen/MachineModuleInfo.cpp
@@ -16,7 +16,9 @@
#include "llvm/CodeGen/Passes.h"
#include "llvm/IR/BasicBlock.h"
#include "llvm/IR/DerivedTypes.h"
+#include "llvm/IR/DiagnosticInfo.h"
#include "llvm/IR/Instructions.h"
+#include "llvm/IR/LLVMContext.h"
#include "llvm/IR/Module.h"
#include "llvm/IR/Value.h"
#include "llvm/IR/ValueHandle.h"
@@ -315,9 +317,44 @@ INITIALIZE_PASS(MachineModuleInfoWrapperPass, "machinemoduleinfo",
"Machine Module Information", false, false)
char MachineModuleInfoWrapperPass::ID = 0;
+static unsigned getLocCookie(const SMDiagnostic &SMD, const SourceMgr &SrcMgr,
+ std::vector<const MDNode *> &LocInfos) {
+ // Look up a LocInfo for the buffer this diagnostic is coming from.
+ unsigned BufNum = SrcMgr.FindBufferContainingLoc(SMD.getLoc());
+ const MDNode *LocInfo = nullptr;
+ if (BufNum > 0 && BufNum <= LocInfos.size())
+ LocInfo = LocInfos[BufNum - 1];
+
+ // If the inline asm had metadata associated with it, pull out a location
+ // cookie corresponding to which line the error occurred on.
+ unsigned LocCookie = 0;
+ if (LocInfo) {
+ unsigned ErrorLine = SMD.getLineNo() - 1;
+ if (ErrorLine >= LocInfo->getNumOperands())
+ ErrorLine = 0;
+
+ if (LocInfo->getNumOperands() != 0)
+ if (const ConstantInt *CI =
+ mdconst::dyn_extract<ConstantInt>(LocInfo->getOperand(ErrorLine)))
+ LocCookie = CI->getZExtValue();
+ }
+
+ return LocCookie;
+}
+
bool MachineModuleInfoWrapperPass::doInitialization(Module &M) {
MMI.initialize();
MMI.TheModule = &M;
+ // FIXME: Do this for new pass manager.
+ LLVMContext &Ctx = M.getContext();
+ MMI.getContext().setDiagnosticHandler(
+ [&Ctx](const SMDiagnostic &SMD, bool IsInlineAsm, const SourceMgr &SrcMgr,
+ std::vector<const MDNode *> &LocInfos) {
+ unsigned LocCookie = 0;
+ if (IsInlineAsm)
+ LocCookie = getLocCookie(SMD, SrcMgr, LocInfos);
+ Ctx.diagnose(DiagnosticInfoSrcMgr(SMD, IsInlineAsm, LocCookie));
+ });
MMI.DbgInfoAvailable = !M.debug_compile_units().empty();
return false;
}