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/Path.h" |
16 | #include "llvm/Support/raw_ostream.h" |
17 | #include <algorithm> |
18 | #include <cassert> |
19 | #include <cstddef> |
20 | |
21 | using namespace llvm; |
22 | |
23 | const size_t MAX_LINE_LEN = 80U; |
24 | |
25 | namespace llvm::TableGen::Emitter { |
26 | ManagedStatic<cl::opt<FnT>, OptCreatorT> Action; |
27 | void *OptCreatorT::call() { |
28 | return new cl::opt<FnT>(cl::desc("Action to perform:" )); |
29 | } |
30 | } // namespace llvm::TableGen::Emitter |
31 | |
32 | static void printLine(raw_ostream &OS, const Twine &Prefix, char Fill, |
33 | StringRef Suffix) { |
34 | size_t Pos = (size_t)OS.tell(); |
35 | assert((Prefix.str().size() + Suffix.size() <= MAX_LINE_LEN) && |
36 | "header line exceeds max limit" ); |
37 | OS << Prefix; |
38 | for (size_t i = (size_t)OS.tell() - Pos, e = MAX_LINE_LEN - Suffix.size(); |
39 | i < e; ++i) |
40 | OS << Fill; |
41 | OS << Suffix << '\n'; |
42 | } |
43 | |
44 | void llvm::(StringRef Desc, raw_ostream &OS, |
45 | const RecordKeeper &Record) { |
46 | printLine(OS, Prefix: "/*===- TableGen'erated file " , Fill: '-', Suffix: "*- C++ -*-===*\\" ); |
47 | StringRef Prefix("|* " ); |
48 | StringRef Suffix(" *|" ); |
49 | printLine(OS, Prefix, Fill: ' ', Suffix); |
50 | size_t PSLen = Prefix.size() + Suffix.size(); |
51 | assert(PSLen < MAX_LINE_LEN); |
52 | size_t Pos = 0U; |
53 | do { |
54 | size_t Length = std::min(a: Desc.size() - Pos, b: MAX_LINE_LEN - PSLen); |
55 | printLine(OS, Prefix: Prefix + Desc.substr(Start: Pos, N: Length), Fill: ' ', Suffix); |
56 | Pos += Length; |
57 | } while (Pos < Desc.size()); |
58 | printLine(OS, Prefix, Fill: ' ', Suffix); |
59 | printLine(OS, Prefix: Prefix + "Automatically generated file, do not edit!" , Fill: ' ', |
60 | Suffix); |
61 | |
62 | // Print the filename of source file |
63 | if (!Record.getInputFilename().empty()) |
64 | printLine( |
65 | OS, Prefix: Prefix + "From: " + sys::path::filename(path: Record.getInputFilename()), |
66 | Fill: ' ', Suffix); |
67 | printLine(OS, Prefix, Fill: ' ', Suffix); |
68 | printLine(OS, Prefix: "\\*===" , Fill: '-', Suffix: "===*/" ); |
69 | OS << '\n'; |
70 | } |
71 | |