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