1*0b57cec5SDimitry Andric //===- TableGenBackend.cpp - Utilities for TableGen Backends ----*- C++ -*-===//
2*0b57cec5SDimitry Andric //
3*0b57cec5SDimitry Andric // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4*0b57cec5SDimitry Andric // See https://llvm.org/LICENSE.txt for license information.
5*0b57cec5SDimitry Andric // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6*0b57cec5SDimitry Andric //
7*0b57cec5SDimitry Andric //===----------------------------------------------------------------------===//
8*0b57cec5SDimitry Andric //
9*0b57cec5SDimitry Andric // This file provides useful services for TableGen backends...
10*0b57cec5SDimitry Andric //
11*0b57cec5SDimitry Andric //===----------------------------------------------------------------------===//
12*0b57cec5SDimitry Andric
13*0b57cec5SDimitry Andric #include "llvm/TableGen/TableGenBackend.h"
14*0b57cec5SDimitry Andric #include "llvm/ADT/Twine.h"
15*0b57cec5SDimitry Andric #include "llvm/Support/raw_ostream.h"
16*0b57cec5SDimitry Andric #include <cassert>
17*0b57cec5SDimitry Andric
18*0b57cec5SDimitry Andric using namespace llvm;
19*0b57cec5SDimitry Andric
20*0b57cec5SDimitry Andric const size_t MAX_LINE_LEN = 80U;
21*0b57cec5SDimitry Andric
printLine(raw_ostream & OS,const Twine & Prefix,char Fill,StringRef Suffix)22*0b57cec5SDimitry Andric static void printLine(raw_ostream &OS, const Twine &Prefix, char Fill,
23*0b57cec5SDimitry Andric StringRef Suffix) {
24*0b57cec5SDimitry Andric size_t Pos = (size_t)OS.tell();
25*0b57cec5SDimitry Andric assert((Prefix.str().size() + Suffix.size() <= MAX_LINE_LEN) &&
26*0b57cec5SDimitry Andric "header line exceeds max limit");
27*0b57cec5SDimitry Andric OS << Prefix;
28*0b57cec5SDimitry Andric for (size_t i = (size_t)OS.tell() - Pos, e = MAX_LINE_LEN - Suffix.size();
29*0b57cec5SDimitry Andric i < e; ++i)
30*0b57cec5SDimitry Andric OS << Fill;
31*0b57cec5SDimitry Andric OS << Suffix << '\n';
32*0b57cec5SDimitry Andric }
33*0b57cec5SDimitry Andric
emitSourceFileHeader(StringRef Desc,raw_ostream & OS)34*0b57cec5SDimitry Andric void llvm::emitSourceFileHeader(StringRef Desc, raw_ostream &OS) {
35*0b57cec5SDimitry Andric printLine(OS, "/*===- TableGen'erated file ", '-', "*- C++ -*-===*\\");
36*0b57cec5SDimitry Andric StringRef Prefix("|* ");
37*0b57cec5SDimitry Andric StringRef Suffix(" *|");
38*0b57cec5SDimitry Andric printLine(OS, Prefix, ' ', Suffix);
39*0b57cec5SDimitry Andric size_t PSLen = Prefix.size() + Suffix.size();
40*0b57cec5SDimitry Andric assert(PSLen < MAX_LINE_LEN);
41*0b57cec5SDimitry Andric size_t Pos = 0U;
42*0b57cec5SDimitry Andric do {
43*0b57cec5SDimitry Andric size_t Length = std::min(Desc.size() - Pos, MAX_LINE_LEN - PSLen);
44*0b57cec5SDimitry Andric printLine(OS, Prefix + Desc.substr(Pos, Length), ' ', Suffix);
45*0b57cec5SDimitry Andric Pos += Length;
46*0b57cec5SDimitry Andric } while (Pos < Desc.size());
47*0b57cec5SDimitry Andric printLine(OS, Prefix, ' ', Suffix);
48*0b57cec5SDimitry Andric printLine(OS, Prefix + "Automatically generated file, do not edit!", ' ',
49*0b57cec5SDimitry Andric Suffix);
50*0b57cec5SDimitry Andric printLine(OS, Prefix, ' ', Suffix);
51*0b57cec5SDimitry Andric printLine(OS, "\\*===", '-', "===*/");
52*0b57cec5SDimitry Andric OS << '\n';
53 }
54