Index: llvm/test/TableGen/opt-remark-diag.td =================================================================== --- /dev/null +++ llvm/test/TableGen/opt-remark-diag.td @@ -0,0 +1,18 @@ +// RUN: llvm-tblgen -gen-optremark-diags %s 2>&1 | FileCheck %s + +// CHECK: OPT_REMARK(remark_example_remark, "RemarkName", "Format string with optional specifier like %0", "Verbose format string") +// CHECK: OPT_REMARK(remark_example_remark2, "RemarkName2", "Format string", "") + +class OptRemark { + string RemarkName = Name; + string FormatStr = Format; + string VerboseFormatStr = FormatVerbose; +} + +def remark_example_remark : OptRemark<"RemarkName", + "Format string with optional specifier like %0", + "Verbose format string">; +def remark_example_remark2 : OptRemark<"RemarkName2", + "Format string">; Index: llvm/utils/TableGen/CMakeLists.txt =================================================================== --- llvm/utils/TableGen/CMakeLists.txt +++ llvm/utils/TableGen/CMakeLists.txt @@ -35,6 +35,7 @@ IntrinsicEmitter.cpp OptEmitter.cpp OptParserEmitter.cpp + OptRemarkDiagEmitter.cpp OptRSTEmitter.cpp PredicateExpander.cpp PseudoLoweringEmitter.cpp Index: llvm/utils/TableGen/OptRemarkDiagEmitter.cpp =================================================================== --- /dev/null +++ llvm/utils/TableGen/OptRemarkDiagEmitter.cpp @@ -0,0 +1,40 @@ +//===- OptEmitter.cpp - Helper for emitting options.----------- -----------===// +// +// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. +// See https://llvm.org/LICENSE.txt for license information. +// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception +// +//===----------------------------------------------------------------------===// + +#include "llvm/TableGen/Record.h" + +/// EmitOptRemarkDiags - This tablegen backend takes an input .td file +/// describing a list of optimization remarks and emits a series of macros +/// that will be associate an ID with each remark and allow the associated +/// strings to be looked up from the ID. +/// +/// The expected input format for each diagnostic is: +/// +/// def remark_example_remark : OptRemark<"RemarkName", +/// "Format string with optional specifier like %0" +/// "Verbose format string">; +/// +/// This will be transformed into this format: +/// +/// OPT_REMARK(remark_example_remark, +/// "RemarkName", "Format string with optional specifier like %0", +/// "Verbose format string") + +namespace llvm { + +void EmitOptRemarkDiags(RecordKeeper &Records, raw_ostream &OS) { + std::vector Remarks = Records.getAllDerivedDefinitions("OptRemark"); + for (Record *Remark : Remarks) { + OS << "OPT_REMARK(" << Remark->getName() << ", " + << "\"" << Remark->getValueAsString("RemarkName") << "\", " + << "\"" << Remark->getValueAsString("FormatStr") << "\", " + << "\"" << Remark->getValueAsString("VerboseFormatStr") << "\")\n"; + } +} + +} // namespace llvm Index: llvm/utils/TableGen/TableGen.cpp =================================================================== --- llvm/utils/TableGen/TableGen.cpp +++ llvm/utils/TableGen/TableGen.cpp @@ -54,6 +54,7 @@ GenRegisterBank, GenExegesis, GenAutomata, + GenOptRemarkDiags, }; namespace llvm { @@ -122,7 +123,9 @@ "Generate registers bank descriptions"), clEnumValN(GenExegesis, "gen-exegesis", "Generate llvm-exegesis tables"), - clEnumValN(GenAutomata, "gen-automata", "Generate generic automata"))); + clEnumValN(GenAutomata, "gen-automata", "Generate generic automata"), + clEnumValN(GenOptRemarkDiags, "gen-optremark-diags", + "Generate optimization remark diagnostics"))); cl::OptionCategory PrintEnumsCat("Options for -print-enums"); cl::opt Class("class", cl::desc("Print Enum list for this class"), @@ -247,6 +250,9 @@ case GenAutomata: EmitAutomata(Records, OS); break; + case GenOptRemarkDiags: + EmitOptRemarkDiags(Records, OS); + break; } return false; Index: llvm/utils/TableGen/TableGenBackends.h =================================================================== --- llvm/utils/TableGen/TableGenBackends.h +++ llvm/utils/TableGen/TableGenBackends.h @@ -90,6 +90,7 @@ void EmitRegisterBank(RecordKeeper &RK, raw_ostream &OS); void EmitExegesis(RecordKeeper &RK, raw_ostream &OS); void EmitAutomata(RecordKeeper &RK, raw_ostream &OS); +void EmitOptRemarkDiags(RecordKeeper &Records, raw_ostream &OS); } // End llvm namespace