blob: e11b28e8cff99c49793fe9bcea7b82765466c075 [file] [log] [blame]
Jakob Stoklund Olesen1c66b872012-06-13 05:15:49 +00001//===- TableGenBackend.cpp - Utilities for TableGen Backends ----*- C++ -*-===//
Misha Brukman650ba8e2005-04-22 00:00:37 +00002//
Chandler Carruth2946cd72019-01-19 08:50:56 +00003// 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
Misha Brukman650ba8e2005-04-22 00:00:37 +00006//
John Criswelld3032032003-10-20 20:20:30 +00007//===----------------------------------------------------------------------===//
Chris Lattnerf5bd1b72003-10-05 19:27:59 +00008//
9// This file provides useful services for TableGen backends...
10//
11//===----------------------------------------------------------------------===//
12
Craig Topper1407b172015-05-26 06:48:46 +000013#include "llvm/TableGen/TableGenBackend.h"
Benjamin Kramer83aa9472012-06-19 17:04:16 +000014#include "llvm/ADT/Twine.h"
Jakob Stoklund Olesen1c66b872012-06-13 05:15:49 +000015#include "llvm/Support/raw_ostream.h"
Nadav Rotemf0705252013-01-28 07:35:33 +000016
Chris Lattner68478662004-08-01 03:55:39 +000017using namespace llvm;
Brian Gaeke960707c2003-11-11 22:41:34 +000018
Nadav Rotemf0705252013-01-28 07:35:33 +000019const size_t MAX_LINE_LEN = 80U;
20
Benjamin Kramer83aa9472012-06-19 17:04:16 +000021static void printLine(raw_ostream &OS, const Twine &Prefix, char Fill,
22 StringRef Suffix) {
Nadav Rotemf0705252013-01-28 07:35:33 +000023 size_t Pos = (size_t)OS.tell();
Craig Topper33684f92015-05-26 08:07:49 +000024 assert((Prefix.str().size() + Suffix.size() <= MAX_LINE_LEN) &&
Craig Toppere59cd0b2015-05-26 06:48:47 +000025 "header line exceeds max limit");
Benjamin Kramer83aa9472012-06-19 17:04:16 +000026 OS << Prefix;
Craig Topper33684f92015-05-26 08:07:49 +000027 for (size_t i = (size_t)OS.tell() - Pos, e = MAX_LINE_LEN - Suffix.size();
28 i < e; ++i)
Benjamin Kramer83aa9472012-06-19 17:04:16 +000029 OS << Fill;
30 OS << Suffix << '\n';
31}
32
Jakob Stoklund Olesene6aed132012-06-11 15:37:55 +000033void llvm::emitSourceFileHeader(StringRef Desc, raw_ostream &OS) {
Benjamin Kramer83aa9472012-06-19 17:04:16 +000034 printLine(OS, "/*===- TableGen'erated file ", '-', "*- C++ -*-===*\\");
Craig Toppercb7648b2015-05-26 08:07:45 +000035 StringRef Prefix("|* ");
Nadav Rotemf0705252013-01-28 07:35:33 +000036 StringRef Suffix(" *|");
Craig Toppercb7648b2015-05-26 08:07:45 +000037 printLine(OS, Prefix, ' ', Suffix);
Craig Topperf58341c2015-05-26 08:07:56 +000038 size_t PSLen = Prefix.size() + Suffix.size();
39 assert(PSLen < MAX_LINE_LEN);
Craig Toppercb7648b2015-05-26 08:07:45 +000040 size_t Pos = 0U;
Craig Topperf58341c2015-05-26 08:07:56 +000041 do {
42 size_t Length = std::min(Desc.size() - Pos, MAX_LINE_LEN - PSLen);
43 printLine(OS, Prefix + Desc.substr(Pos, Length), ' ', Suffix);
44 Pos += Length;
Craig Toppercb7648b2015-05-26 08:07:45 +000045 } while (Pos < Desc.size());
Nadav Rotemf0705252013-01-28 07:35:33 +000046 printLine(OS, Prefix, ' ', Suffix);
Craig Toppercb7648b2015-05-26 08:07:45 +000047 printLine(OS, Prefix + "Automatically generated file, do not edit!", ' ',
Nadav Rotemf0705252013-01-28 07:35:33 +000048 Suffix);
49 printLine(OS, Prefix, ' ', Suffix);
Benjamin Kramer83aa9472012-06-19 17:04:16 +000050 printLine(OS, "\\*===", '-', "===*/");
51 OS << '\n';
Chris Lattnerf5bd1b72003-10-05 19:27:59 +000052}