llvm向用户抛出warning、error信息

1、抛出error信息并终止程序

使用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."});
}

2、优化pass中抛出警告信息之OptimizationRemark

使用时clang得加上-Rpass=name,llc得加上–pass-remarks=name才行,后端用法如下:

#include "llvm/Analysis/OptimizationRemarkEmitter.h"
 
void getAnalysisUsage(AnalysisUsage &AU) const override {
    AU.addRequired<OptimizationRemarkEmitterWrapperPass>();
    MachineFunctionPass::getAnalysisUsage(AU);
 }
void xxx::reportWarningMsg(const MachineFunction &MF)const {
  auto *ORE = &getAnalysis<OptimizationRemarkEmitterWrapperPass>().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);
}

3、machine ir抛信息之DiagnosticInfoMIRParser

该接口同样支持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")));
}

4、AsmParser之Warning

基本用法如下:

XXXAsmParser &Parser;
void Warning(SMLoc L, const Twine &Msg) { Parser.Warning(L, Msg); }

5、MCContext之reportWarning

基本用法如下:

MCContext &Context;
void MCContext::reportWarning(SMLoc Loc, const Twine &Msg)
Context.reportWarning(MCxxx.getLoc(), Msg);

6、其它类型的信息

包括DiagnosticInfoInlineAsm,DiagnosticInfoResourceLimit,DiagnosticInfoStackSize,DiagnosticInfoOptimizationBase,DiagnosticInfoIROptimization以及debug,Profileing,remarks等。

  1. 其中llvm_unreachable为开发者使用,发布时用户不可见,所看到的信息为segment fatal。
  2. assert由于存在判断开销,一般也不在发布中开启。

7、用例如何检查report_fatal_error

  1. RUN命令需要使用not,用法如下:
// RUN: not %clang --target=xx -mcpu=xxx -O3 -c %s -o - 2>&1 | FileCheck %s --check-prefix=expected-error
....
// expected-error: xxxx
  1. 使用-fsyntax-only -verify,用法如下:
// RUN: %clang_cc1 -triple xxx  -target-feature xx -fsyntax-only -verify %s
// expected-warning{{xxxxxxxx}} expected-error{{xxxxxxxxxxxxxxx}}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值