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