Path: blob/main/contrib/llvm-project/llvm/lib/TableGen/TableGenBackend.cpp
35233 views
//===- TableGenBackend.cpp - Utilities for TableGen Backends ----*- C++ -*-===//1//2// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.3// See https://llvm.org/LICENSE.txt for license information.4// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception5//6//===----------------------------------------------------------------------===//7//8// This file provides useful services for TableGen backends...9//10//===----------------------------------------------------------------------===//1112#include "llvm/TableGen/TableGenBackend.h"13#include "llvm/ADT/Twine.h"14#include "llvm/Support/Path.h"15#include "llvm/Support/raw_ostream.h"16#include <algorithm>17#include <cassert>18#include <cstddef>1920using namespace llvm;2122const size_t MAX_LINE_LEN = 80U;2324namespace llvm::TableGen::Emitter {25ManagedStatic<cl::opt<FnT>, OptCreatorT> Action;26void *OptCreatorT::call() {27return new cl::opt<FnT>(cl::desc("Action to perform:"));28}29} // namespace llvm::TableGen::Emitter3031static void printLine(raw_ostream &OS, const Twine &Prefix, char Fill,32StringRef Suffix) {33size_t Pos = (size_t)OS.tell();34assert((Prefix.str().size() + Suffix.size() <= MAX_LINE_LEN) &&35"header line exceeds max limit");36OS << Prefix;37for (size_t i = (size_t)OS.tell() - Pos, e = MAX_LINE_LEN - Suffix.size();38i < e; ++i)39OS << Fill;40OS << Suffix << '\n';41}4243void llvm::emitSourceFileHeader(StringRef Desc, raw_ostream &OS,44const RecordKeeper &Record) {45printLine(OS, "/*===- TableGen'erated file ", '-', "*- C++ -*-===*\\");46StringRef Prefix("|* ");47StringRef Suffix(" *|");48printLine(OS, Prefix, ' ', Suffix);49size_t PSLen = Prefix.size() + Suffix.size();50assert(PSLen < MAX_LINE_LEN);51size_t Pos = 0U;52do {53size_t Length = std::min(Desc.size() - Pos, MAX_LINE_LEN - PSLen);54printLine(OS, Prefix + Desc.substr(Pos, Length), ' ', Suffix);55Pos += Length;56} while (Pos < Desc.size());57printLine(OS, Prefix, ' ', Suffix);58printLine(OS, Prefix + "Automatically generated file, do not edit!", ' ',59Suffix);6061// Print the filename of source file62if (!Record.getInputFilename().empty())63printLine(64OS, Prefix + "From: " + sys::path::filename(Record.getInputFilename()),65' ', Suffix);66printLine(OS, Prefix, ' ', Suffix);67printLine(OS, "\\*===", '-', "===*/");68OS << '\n';69}707172