b4a5318bd08dfe51b4446e48c599edce117fa00c
[oota-llvm.git] / lib / TableGen / TableGenBackend.cpp
1 //===- TableGenBackend.cpp - Utilities for TableGen Backends ----*- C++ -*-===//
2 //
3 //                     The LLVM Compiler Infrastructure
4 //
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
7 //
8 //===----------------------------------------------------------------------===//
9 //
10 // This file provides useful services for TableGen backends...
11 //
12 //===----------------------------------------------------------------------===//
13
14 #include "llvm/TableGen/TableGenBackend.h"
15 #include "llvm/ADT/Twine.h"
16 #include "llvm/Support/raw_ostream.h"
17
18 using namespace llvm;
19
20 const size_t MAX_LINE_LEN = 80U;
21
22 static void printLine(raw_ostream &OS, const Twine &Prefix, char Fill,
23                       StringRef Suffix) {
24   size_t Pos = (size_t)OS.tell();
25   assert((MAX_LINE_LEN - Prefix.str().size() - Suffix.size() > 0) &&
26          "header line exceeds max limit");
27   OS << Prefix;
28   const size_t e = MAX_LINE_LEN - Suffix.size();
29   for (size_t i = (size_t)OS.tell() - Pos; i < e; ++i)
30     OS << Fill;
31   OS << Suffix << '\n';
32 }
33
34 void llvm::emitSourceFileHeader(StringRef Desc, raw_ostream &OS) {
35   printLine(OS, "/*===- TableGen'erated file ", '-', "*- C++ -*-===*\\");
36   printLine(OS, "|*", ' ', "*|");
37   size_t Pos = 0U;
38   size_t PosE;
39   StringRef Prefix("|*");
40   StringRef Suffix(" *|");
41   do{
42     size_t PSLen = Suffix.size() + Prefix.size();
43     PosE = Pos + ((MAX_LINE_LEN > (Desc.size() - PSLen)) ?
44       Desc.size() :
45       MAX_LINE_LEN - PSLen);
46     printLine(OS, Prefix + Desc.slice(Pos, PosE), ' ', Suffix);
47     Pos = PosE;
48   } while(Pos < Desc.size());
49   printLine(OS, Prefix, ' ', Suffix);
50   printLine(OS, Prefix + " Automatically generated file, do not edit!", ' ',
51     Suffix);
52   printLine(OS, Prefix, ' ', Suffix);
53   printLine(OS, "\\*===", '-', "===*/");
54   OS << '\n';
55 }