1 //===- TableGenBackend.cpp - Utilities for TableGen Backends ----*- C++ -*-===//
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
7 //===----------------------------------------------------------------------===//
9 // This file provides useful services for TableGen backends...
11 //===----------------------------------------------------------------------===//
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"
23 const size_t MAX_LINE_LEN
= 80U;
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:"));
30 } // namespace llvm::TableGen::Emitter
32 static void printLine(raw_ostream
&OS
, const Twine
&Prefix
, char Fill
,
34 size_t Pos
= (size_t)OS
.tell();
35 assert((Prefix
.str().size() + Suffix
.size() <= MAX_LINE_LEN
) &&
36 "header line exceeds max limit");
38 for (size_t i
= (size_t)OS
.tell() - Pos
, e
= MAX_LINE_LEN
- Suffix
.size();
44 void llvm::emitSourceFileHeader(StringRef Desc
, raw_ostream
&OS
,
45 const RecordKeeper
&Record
) {
46 printLine(OS
, "/*===- TableGen'erated file ", '-', "*- C++ -*-===*\\");
47 StringRef
Prefix("|* ");
48 StringRef
Suffix(" *|");
49 printLine(OS
, Prefix
, ' ', Suffix
);
50 size_t PSLen
= Prefix
.size() + Suffix
.size();
51 assert(PSLen
< MAX_LINE_LEN
);
54 size_t Length
= std::min(Desc
.size() - Pos
, MAX_LINE_LEN
- PSLen
);
55 printLine(OS
, Prefix
+ Desc
.substr(Pos
, Length
), ' ', Suffix
);
57 } while (Pos
< Desc
.size());
58 printLine(OS
, Prefix
, ' ', Suffix
);
59 printLine(OS
, Prefix
+ "Automatically generated file, do not edit!", ' ',
62 // Print the filename of source file
63 if (!Record
.getInputFilename().empty())
65 OS
, Prefix
+ "From: " + sys::path::filename(Record
.getInputFilename()),
67 printLine(OS
, Prefix
, ' ', Suffix
);
68 printLine(OS
, "\\*===", '-', "===*/");