使用DiagnosticInfoUnsupported可以向用户抛出error信息并且终止程序,效果如同report_fatal_error、Error。后端用法如下:
void xxxx::reportErrorMsg(const MachineFunction &MF)const {
const Function &F = MF.getFunction();
// Diagnostic information for unsupported feature in backend.
F.getContext().diagnose(DiagnosticInfoUnsupported{F, "report msg."});
}
使用时clang得加上-Rpass=name,llc得加上–pass-remarks=name才行,后端用法如下:
#include "llvm/Analysis/OptimizationRemarkEmitter.h"
void getAnalysisUsage(AnalysisUsage &AU) const override {
AU.addRequired();
MachineFunctionPass::getAnalysisUsage(AU);
}
void xxx::reportWarningMsg(const MachineFunction &MF)const {
auto *ORE = &getAnalysis().getORE();
const Function &F = MF.getFunction();
// Diagnostic information for applied optimization remarks
OptimizationRemark R(DEBUG_TYPE, "replace-spill-register", &F);
R << "report msg";
ORE->emit(R);
}
该接口同样支持DS_Error,DS_Warning,DS_Remark和DS_Note 4种方式,后端用法如下:
#include "llvm/IR/DiagnosticInfo.h"
#include "llvm/Support/SourceMgr.h"
void xxx::reportWarningMsg(const MachineFunction &MF)const {
const Function &F = MF.getFunction();
// Diagnostic information for machine IR parser.
F.getContext().diagnose(DiagnosticInfoMIRParser(
DS_Warning,
SMDiagnostic(
DEBUG_TYPE, SourceMgr::DK_Warning, "report msg")));
}
基本用法如下:
XXXAsmParser &Parser;
void Warning(SMLoc L, const Twine &Msg) { Parser.Warning(L, Msg); }
基本用法如下:
MCContext &Context;
void MCContext::reportWarning(SMLoc Loc, const Twine &Msg)
Context.reportWarning(MCxxx.getLoc(), Msg);
包括DiagnosticInfoInlineAsm,DiagnosticInfoResourceLimit,DiagnosticInfoStackSize,DiagnosticInfoOptimizationBase,DiagnosticInfoIROptimization以及debug,Profileing,remarks等。
// RUN: not %clang --target=xx -mcpu=xxx -O3 -c %s -o - 2>&1 | FileCheck %s --check-prefix=expected-error
....
// expected-error: xxxx
// RUN: %clang_cc1 -triple xxx -target-feature xx -fsyntax-only -verify %s
// expected-warning{{xxxxxxxx}} expected-error{{xxxxxxxxxxxxxxx}}