xref: /llvm-project/llvm/lib/TableGen/TableGenBackend.cpp (revision b91f0def6f261064cedfe0e62395c0097130b8f1)
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/CommandLine.h"
16 #include "llvm/Support/ManagedStatic.h"
17 #include "llvm/Support/Path.h"
18 #include "llvm/Support/raw_ostream.h"
19 #include <algorithm>
20 #include <cassert>
21 #include <cstddef>
22 
23 using namespace llvm;
24 using namespace TableGen::Emitter;
25 
26 const size_t MAX_LINE_LEN = 80U;
27 
28 // CommandLine options of class type are not directly supported with some
29 // specific exceptions like std::string which are safe to copy. In our case,
30 // the `FnT` function_ref object is also safe to copy. So provide a
31 // specialization of `OptionValue` for `FnT` type that stores it as a copy.
32 // This is essentially similar to OptionValue<std::string> specialization for
33 // strings.
34 template <> struct cl::OptionValue<FnT> final : cl::OptionValueCopy<FnT> {
35   OptionValue() = default;
36 
37   OptionValue(const FnT &V) { this->setValue(V); }
38 
39   OptionValue<FnT> &operator=(const FnT &V) {
40     setValue(V);
41     return *this;
42   }
43 };
44 
45 namespace {
46 struct OptCreatorT {
47   static void *call() {
48     return new cl::opt<FnT>(cl::desc("Action to perform:"));
49   }
50 };
51 } // namespace
52 
53 static ManagedStatic<cl::opt<FnT>, OptCreatorT> CallbackFunction;
54 
55 Opt::Opt(StringRef Name, FnT CB, StringRef Desc, bool ByDefault) {
56   if (ByDefault)
57     CallbackFunction->setInitialValue(CB);
58   CallbackFunction->getParser().addLiteralOption(Name, CB, Desc);
59 }
60 
61 /// Apply callback specified on the command line. Returns true if no callback
62 /// was applied.
63 bool llvm::TableGen::Emitter::ApplyCallback(const RecordKeeper &Records,
64                                             raw_ostream &OS) {
65   FnT Fn = CallbackFunction->getValue();
66   if (!Fn)
67     return true;
68   Fn(Records, OS);
69   return false;
70 }
71 
72 static void printLine(raw_ostream &OS, const Twine &Prefix, char Fill,
73                       StringRef Suffix) {
74   size_t Pos = (size_t)OS.tell();
75   assert((Prefix.str().size() + Suffix.size() <= MAX_LINE_LEN) &&
76          "header line exceeds max limit");
77   OS << Prefix;
78   for (size_t i = (size_t)OS.tell() - Pos, e = MAX_LINE_LEN - Suffix.size();
79          i < e; ++i)
80     OS << Fill;
81   OS << Suffix << '\n';
82 }
83 
84 void llvm::emitSourceFileHeader(StringRef Desc, raw_ostream &OS,
85                                 const RecordKeeper &Record) {
86   printLine(OS, "/*===- TableGen'erated file ", '-', "*- C++ -*-===*\\");
87   StringRef Prefix("|* ");
88   StringRef Suffix(" *|");
89   printLine(OS, Prefix, ' ', Suffix);
90   size_t PSLen = Prefix.size() + Suffix.size();
91   assert(PSLen < MAX_LINE_LEN);
92   size_t Pos = 0U;
93   do {
94     size_t Length = std::min(Desc.size() - Pos, MAX_LINE_LEN - PSLen);
95     printLine(OS, Prefix + Desc.substr(Pos, Length), ' ', Suffix);
96     Pos += Length;
97   } while (Pos < Desc.size());
98   printLine(OS, Prefix, ' ', Suffix);
99   printLine(OS, Prefix + "Automatically generated file, do not edit!", ' ',
100             Suffix);
101 
102   // Print the filename of source file.
103   if (!Record.getInputFilename().empty())
104     printLine(
105         OS, Prefix + "From: " + sys::path::filename(Record.getInputFilename()),
106         ' ', Suffix);
107   printLine(OS, Prefix, ' ', Suffix);
108   printLine(OS, "\\*===", '-', "===*/");
109   OS << '\n';
110 }
111