TableGenBackend.cpp 1.8 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253
  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. #include "llvm/TableGen/TableGenBackend.h"
  13. #include "llvm/ADT/Twine.h"
  14. #include "llvm/Support/raw_ostream.h"
  15. #include <cassert>
  16. using namespace llvm;
  17. const size_t MAX_LINE_LEN = 80U;
  18. static void printLine(raw_ostream &OS, const Twine &Prefix, char Fill,
  19. StringRef Suffix) {
  20. size_t Pos = (size_t)OS.tell();
  21. assert((Prefix.str().size() + Suffix.size() <= MAX_LINE_LEN) &&
  22. "header line exceeds max limit");
  23. OS << Prefix;
  24. for (size_t i = (size_t)OS.tell() - Pos, e = MAX_LINE_LEN - Suffix.size();
  25. i < e; ++i)
  26. OS << Fill;
  27. OS << Suffix << '\n';
  28. }
  29. void llvm::emitSourceFileHeader(StringRef Desc, raw_ostream &OS) {
  30. printLine(OS, "/*===- TableGen'erated file ", '-', "*- C++ -*-===*\\");
  31. StringRef Prefix("|* ");
  32. StringRef Suffix(" *|");
  33. printLine(OS, Prefix, ' ', Suffix);
  34. size_t PSLen = Prefix.size() + Suffix.size();
  35. assert(PSLen < MAX_LINE_LEN);
  36. size_t Pos = 0U;
  37. do {
  38. size_t Length = std::min(Desc.size() - Pos, MAX_LINE_LEN - PSLen);
  39. printLine(OS, Prefix + Desc.substr(Pos, Length), ' ', Suffix);
  40. Pos += Length;
  41. } while (Pos < Desc.size());
  42. printLine(OS, Prefix, ' ', Suffix);
  43. printLine(OS, Prefix + "Automatically generated file, do not edit!", ' ',
  44. Suffix);
  45. printLine(OS, Prefix, ' ', Suffix);
  46. printLine(OS, "\\*===", '-', "===*/");
  47. OS << '\n';
  48. }