120 lines · cpp
1//===- TableGenBackend.cpp - Utilities for TableGen Backends ----*- C++ -*-===//2//3// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.4// See https://llvm.org/LICENSE.txt for license information.5// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception6//7//===----------------------------------------------------------------------===//8//9// This file provides useful services for TableGen backends...10//11//===----------------------------------------------------------------------===//12 13#include "llvm/TableGen/TableGenBackend.h"14#include "llvm/ADT/Twine.h"15#include "llvm/Support/CommandLine.h"16#include "llvm/Support/ManagedStatic.h"17#include "llvm/Support/Path.h"18#include "llvm/Support/raw_ostream.h"19#include <algorithm>20#include <cassert>21#include <cstddef>22 23using namespace llvm;24using namespace TableGen::Emitter;25 26const size_t MAX_LINE_LEN = 80U;27 28// CommandLine options of class type are not directly supported with some29// specific exceptions like std::string which are safe to copy. In our case,30// the `FnT` function_ref object is also safe to copy. So provide a31// specialization of `OptionValue` for `FnT` type that stores it as a copy.32// This is essentially similar to OptionValue<std::string> specialization for33// strings.34template <> struct cl::OptionValue<FnT> final : cl::OptionValueCopy<FnT> {35 OptionValue() = default;36 37 OptionValue(const FnT &V) { this->setValue(V); }38 39 OptionValue<FnT> &operator=(const FnT &V) {40 setValue(V);41 return *this;42 }43};44 45namespace {46struct OptCreatorT {47 static void *call() {48 return new cl::opt<FnT>(cl::desc("Action to perform:"));49 }50};51} // namespace52 53static ManagedStatic<cl::opt<FnT>, OptCreatorT> CallbackFunction;54 55Opt::Opt(StringRef Name, FnT CB, StringRef Desc, bool ByDefault) {56 if (ByDefault)57 CallbackFunction->setInitialValue(CB);58 CallbackFunction->getParser().addLiteralOption(Name, CB, Desc);59}60 61/// Apply callback specified on the command line. Returns true if no callback62/// was applied.63bool llvm::TableGen::Emitter::ApplyCallback(const RecordKeeper &Records,64 TableGenOutputFiles &OutFiles,65 StringRef FilenamePrefix) {66 FnT Fn = CallbackFunction->getValue();67 if (Fn.SingleFileGenerator) {68 std::string S;69 raw_string_ostream OS(S);70 Fn.SingleFileGenerator(Records, OS);71 OutFiles = {S, {}};72 return false;73 }74 if (Fn.MultiFileGenerator) {75 OutFiles = Fn.MultiFileGenerator(FilenamePrefix, Records);76 return false;77 }78 return true;79}80 81static void printLine(raw_ostream &OS, const Twine &Prefix, char Fill,82 StringRef Suffix) {83 size_t Pos = (size_t)OS.tell();84 assert((Prefix.str().size() + Suffix.size() <= MAX_LINE_LEN) &&85 "header line exceeds max limit");86 OS << Prefix;87 for (size_t i = (size_t)OS.tell() - Pos, e = MAX_LINE_LEN - Suffix.size();88 i < e; ++i)89 OS << Fill;90 OS << Suffix << '\n';91}92 93void llvm::emitSourceFileHeader(StringRef Desc, raw_ostream &OS,94 const RecordKeeper &Record) {95 printLine(OS, "/*===- TableGen'erated file ", '-', "*- C++ -*-===*\\");96 StringRef Prefix("|* ");97 StringRef Suffix(" *|");98 printLine(OS, Prefix, ' ', Suffix);99 size_t PSLen = Prefix.size() + Suffix.size();100 assert(PSLen < MAX_LINE_LEN);101 size_t Pos = 0U;102 do {103 size_t Length = std::min(Desc.size() - Pos, MAX_LINE_LEN - PSLen);104 printLine(OS, Prefix + Desc.substr(Pos, Length), ' ', Suffix);105 Pos += Length;106 } while (Pos < Desc.size());107 printLine(OS, Prefix, ' ', Suffix);108 printLine(OS, Prefix + "Automatically generated file, do not edit!", ' ',109 Suffix);110 111 // Print the filename of source file.112 if (!Record.getInputFilename().empty())113 printLine(114 OS, Prefix + "From: " + sys::path::filename(Record.getInputFilename()),115 ' ', Suffix);116 printLine(OS, Prefix, ' ', Suffix);117 printLine(OS, "\\*===", '-', "===*/");118 OS << '\n';119}120