10b57cec5SDimitry Andric //===- TableGenBackend.cpp - Utilities for TableGen Backends ----*- C++ -*-===//
20b57cec5SDimitry Andric //
30b57cec5SDimitry Andric // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
40b57cec5SDimitry Andric // See https://llvm.org/LICENSE.txt for license information.
50b57cec5SDimitry Andric // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
60b57cec5SDimitry Andric //
70b57cec5SDimitry Andric //===----------------------------------------------------------------------===//
80b57cec5SDimitry Andric //
90b57cec5SDimitry Andric // This file provides useful services for TableGen backends...
100b57cec5SDimitry Andric //
110b57cec5SDimitry Andric //===----------------------------------------------------------------------===//
120b57cec5SDimitry Andric 
130b57cec5SDimitry Andric #include "llvm/TableGen/TableGenBackend.h"
140b57cec5SDimitry Andric #include "llvm/ADT/Twine.h"
155f757f3fSDimitry Andric #include "llvm/Support/Path.h"
160b57cec5SDimitry Andric #include "llvm/Support/raw_ostream.h"
1706c3fb27SDimitry Andric #include <algorithm>
185ffd83dbSDimitry Andric #include <cassert>
1906c3fb27SDimitry Andric #include <cstddef>
200b57cec5SDimitry Andric 
210b57cec5SDimitry Andric using namespace llvm;
220b57cec5SDimitry Andric 
230b57cec5SDimitry Andric const size_t MAX_LINE_LEN = 80U;
240b57cec5SDimitry Andric 
2506c3fb27SDimitry Andric namespace llvm::TableGen::Emitter {
2606c3fb27SDimitry Andric ManagedStatic<cl::opt<FnT>, OptCreatorT> Action;
call()2706c3fb27SDimitry Andric void *OptCreatorT::call() {
2806c3fb27SDimitry Andric   return new cl::opt<FnT>(cl::desc("Action to perform:"));
2906c3fb27SDimitry Andric }
3006c3fb27SDimitry Andric } // namespace llvm::TableGen::Emitter
3106c3fb27SDimitry Andric 
printLine(raw_ostream & OS,const Twine & Prefix,char Fill,StringRef Suffix)320b57cec5SDimitry Andric static void printLine(raw_ostream &OS, const Twine &Prefix, char Fill,
330b57cec5SDimitry Andric                       StringRef Suffix) {
340b57cec5SDimitry Andric   size_t Pos = (size_t)OS.tell();
350b57cec5SDimitry Andric   assert((Prefix.str().size() + Suffix.size() <= MAX_LINE_LEN) &&
360b57cec5SDimitry Andric          "header line exceeds max limit");
370b57cec5SDimitry Andric   OS << Prefix;
380b57cec5SDimitry Andric   for (size_t i = (size_t)OS.tell() - Pos, e = MAX_LINE_LEN - Suffix.size();
390b57cec5SDimitry Andric          i < e; ++i)
400b57cec5SDimitry Andric     OS << Fill;
410b57cec5SDimitry Andric   OS << Suffix << '\n';
420b57cec5SDimitry Andric }
430b57cec5SDimitry Andric 
emitSourceFileHeader(StringRef Desc,raw_ostream & OS,const RecordKeeper & Record)445f757f3fSDimitry Andric void llvm::emitSourceFileHeader(StringRef Desc, raw_ostream &OS,
455f757f3fSDimitry Andric                                 const RecordKeeper &Record) {
460b57cec5SDimitry Andric   printLine(OS, "/*===- TableGen'erated file ", '-', "*- C++ -*-===*\\");
470b57cec5SDimitry Andric   StringRef Prefix("|* ");
480b57cec5SDimitry Andric   StringRef Suffix(" *|");
490b57cec5SDimitry Andric   printLine(OS, Prefix, ' ', Suffix);
500b57cec5SDimitry Andric   size_t PSLen = Prefix.size() + Suffix.size();
510b57cec5SDimitry Andric   assert(PSLen < MAX_LINE_LEN);
520b57cec5SDimitry Andric   size_t Pos = 0U;
530b57cec5SDimitry Andric   do {
540b57cec5SDimitry Andric     size_t Length = std::min(Desc.size() - Pos, MAX_LINE_LEN - PSLen);
550b57cec5SDimitry Andric     printLine(OS, Prefix + Desc.substr(Pos, Length), ' ', Suffix);
560b57cec5SDimitry Andric     Pos += Length;
570b57cec5SDimitry Andric   } while (Pos < Desc.size());
580b57cec5SDimitry Andric   printLine(OS, Prefix, ' ', Suffix);
590b57cec5SDimitry Andric   printLine(OS, Prefix + "Automatically generated file, do not edit!", ' ',
600b57cec5SDimitry Andric             Suffix);
615f757f3fSDimitry Andric 
625f757f3fSDimitry Andric   // Print the filename of source file
635f757f3fSDimitry Andric   if (!Record.getInputFilename().empty())
645f757f3fSDimitry Andric     printLine(
655f757f3fSDimitry Andric         OS, Prefix + "From: " + sys::path::filename(Record.getInputFilename()),
665f757f3fSDimitry Andric         ' ', Suffix);
670b57cec5SDimitry Andric   printLine(OS, Prefix, ' ', Suffix);
680b57cec5SDimitry Andric   printLine(OS, "\\*===", '-', "===*/");
690b57cec5SDimitry Andric   OS << '\n';
700b57cec5SDimitry Andric }
71