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 
17*0b57cec5SDimitry Andric using namespace llvm;
18*0b57cec5SDimitry Andric 
19*0b57cec5SDimitry Andric const size_t MAX_LINE_LEN = 80U;
20*0b57cec5SDimitry Andric 
21*0b57cec5SDimitry Andric static void printLine(raw_ostream &OS, const Twine &Prefix, char Fill,
22*0b57cec5SDimitry Andric                       StringRef Suffix) {
23*0b57cec5SDimitry Andric   size_t Pos = (size_t)OS.tell();
24*0b57cec5SDimitry Andric   assert((Prefix.str().size() + Suffix.size() <= MAX_LINE_LEN) &&
25*0b57cec5SDimitry Andric          "header line exceeds max limit");
26*0b57cec5SDimitry Andric   OS << Prefix;
27*0b57cec5SDimitry Andric   for (size_t i = (size_t)OS.tell() - Pos, e = MAX_LINE_LEN - Suffix.size();
28*0b57cec5SDimitry Andric          i < e; ++i)
29*0b57cec5SDimitry Andric     OS << Fill;
30*0b57cec5SDimitry Andric   OS << Suffix << '\n';
31*0b57cec5SDimitry Andric }
32*0b57cec5SDimitry Andric 
33*0b57cec5SDimitry Andric void llvm::emitSourceFileHeader(StringRef Desc, raw_ostream &OS) {
34*0b57cec5SDimitry Andric   printLine(OS, "/*===- TableGen'erated file ", '-', "*- C++ -*-===*\\");
35*0b57cec5SDimitry Andric   StringRef Prefix("|* ");
36*0b57cec5SDimitry Andric   StringRef Suffix(" *|");
37*0b57cec5SDimitry Andric   printLine(OS, Prefix, ' ', Suffix);
38*0b57cec5SDimitry Andric   size_t PSLen = Prefix.size() + Suffix.size();
39*0b57cec5SDimitry Andric   assert(PSLen < MAX_LINE_LEN);
40*0b57cec5SDimitry Andric   size_t Pos = 0U;
41*0b57cec5SDimitry Andric   do {
42*0b57cec5SDimitry Andric     size_t Length = std::min(Desc.size() - Pos, MAX_LINE_LEN - PSLen);
43*0b57cec5SDimitry Andric     printLine(OS, Prefix + Desc.substr(Pos, Length), ' ', Suffix);
44*0b57cec5SDimitry Andric     Pos += Length;
45*0b57cec5SDimitry Andric   } while (Pos < Desc.size());
46*0b57cec5SDimitry Andric   printLine(OS, Prefix, ' ', Suffix);
47*0b57cec5SDimitry Andric   printLine(OS, Prefix + "Automatically generated file, do not edit!", ' ',
48*0b57cec5SDimitry Andric     Suffix);
49*0b57cec5SDimitry Andric   printLine(OS, Prefix, ' ', Suffix);
50*0b57cec5SDimitry Andric   printLine(OS, "\\*===", '-', "===*/");
51*0b57cec5SDimitry Andric   OS << '\n';
52*0b57cec5SDimitry Andric }
53