17ae0e2c9SDimitry Andric //===- TableGenBackend.cpp - Utilities for TableGen Backends ----*- C++ -*-===//
26122f3e6SDimitry Andric //
36122f3e6SDimitry Andric // The LLVM Compiler Infrastructure
46122f3e6SDimitry Andric //
56122f3e6SDimitry Andric // This file is distributed under the University of Illinois Open Source
66122f3e6SDimitry Andric // License. See LICENSE.TXT for details.
76122f3e6SDimitry Andric //
86122f3e6SDimitry Andric //===----------------------------------------------------------------------===//
96122f3e6SDimitry Andric //
106122f3e6SDimitry Andric // This file provides useful services for TableGen backends...
116122f3e6SDimitry Andric //
126122f3e6SDimitry Andric //===----------------------------------------------------------------------===//
136122f3e6SDimitry Andric
14*ff0cc061SDimitry Andric #include "llvm/TableGen/TableGenBackend.h"
157ae0e2c9SDimitry Andric #include "llvm/ADT/Twine.h"
167ae0e2c9SDimitry Andric #include "llvm/Support/raw_ostream.h"
17139f7f9bSDimitry Andric
186122f3e6SDimitry Andric using namespace llvm;
196122f3e6SDimitry Andric
20139f7f9bSDimitry Andric const size_t MAX_LINE_LEN = 80U;
21139f7f9bSDimitry Andric
printLine(raw_ostream & OS,const Twine & Prefix,char Fill,StringRef Suffix)227ae0e2c9SDimitry Andric static void printLine(raw_ostream &OS, const Twine &Prefix, char Fill,
237ae0e2c9SDimitry Andric StringRef Suffix) {
24139f7f9bSDimitry Andric size_t Pos = (size_t)OS.tell();
25*ff0cc061SDimitry Andric assert((Prefix.str().size() + Suffix.size() <= MAX_LINE_LEN) &&
26139f7f9bSDimitry Andric "header line exceeds max limit");
277ae0e2c9SDimitry Andric OS << Prefix;
28*ff0cc061SDimitry Andric for (size_t i = (size_t)OS.tell() - Pos, e = MAX_LINE_LEN - Suffix.size();
29*ff0cc061SDimitry Andric i < e; ++i)
307ae0e2c9SDimitry Andric OS << Fill;
317ae0e2c9SDimitry Andric OS << Suffix << '\n';
326122f3e6SDimitry Andric }
336122f3e6SDimitry Andric
emitSourceFileHeader(StringRef Desc,raw_ostream & OS)347ae0e2c9SDimitry Andric void llvm::emitSourceFileHeader(StringRef Desc, raw_ostream &OS) {
357ae0e2c9SDimitry Andric printLine(OS, "/*===- TableGen'erated file ", '-', "*- C++ -*-===*\\");
36139f7f9bSDimitry Andric StringRef Prefix("|* ");
37139f7f9bSDimitry Andric StringRef Suffix(" *|");
38*ff0cc061SDimitry Andric printLine(OS, Prefix, ' ', Suffix);
39*ff0cc061SDimitry Andric size_t PSLen = Prefix.size() + Suffix.size();
40*ff0cc061SDimitry Andric assert(PSLen < MAX_LINE_LEN);
41*ff0cc061SDimitry Andric size_t Pos = 0U;
42139f7f9bSDimitry Andric do {
43*ff0cc061SDimitry Andric size_t Length = std::min(Desc.size() - Pos, MAX_LINE_LEN - PSLen);
44*ff0cc061SDimitry Andric printLine(OS, Prefix + Desc.substr(Pos, Length), ' ', Suffix);
45*ff0cc061SDimitry Andric Pos += Length;
46139f7f9bSDimitry Andric } while (Pos < Desc.size());
47139f7f9bSDimitry Andric printLine(OS, Prefix, ' ', Suffix);
48139f7f9bSDimitry Andric printLine(OS, Prefix + "Automatically generated file, do not edit!", ' ',
49139f7f9bSDimitry Andric Suffix);
50139f7f9bSDimitry Andric printLine(OS, Prefix, ' ', Suffix);
517ae0e2c9SDimitry Andric printLine(OS, "\\*===", '-', "===*/");
527ae0e2c9SDimitry Andric OS << '\n';
537ae0e2c9SDimitry Andric }
54