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