CodeExpander.h 1.8 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455
  1. //===- CodeExpander.h - Expand variables in a string ----------------------===//
  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. /// \file Expand the variables in a string.
  10. //
  11. //===----------------------------------------------------------------------===//
  12. #ifndef LLVM_UTILS_TABLEGEN_CODEEXPANDER_H
  13. #define LLVM_UTILS_TABLEGEN_CODEEXPANDER_H
  14. #include "llvm/ADT/ArrayRef.h"
  15. #include "llvm/ADT/StringRef.h"
  16. namespace llvm {
  17. class CodeExpansions;
  18. class SMLoc;
  19. class raw_ostream;
  20. /// Emit the given code with all '${foo}' placeholders expanded to their
  21. /// replacements.
  22. ///
  23. /// It's an error to use an undefined expansion and expansion-like output that
  24. /// needs to be emitted verbatim can be escaped as '\${foo}'
  25. ///
  26. /// The emitted code can be given a custom indent to enable both indentation by
  27. /// an arbitrary amount of whitespace and emission of the code as a comment.
  28. class CodeExpander {
  29. StringRef Code;
  30. const CodeExpansions &Expansions;
  31. const ArrayRef<SMLoc> &Loc;
  32. bool ShowExpansions;
  33. StringRef Indent;
  34. public:
  35. CodeExpander(StringRef Code, const CodeExpansions &Expansions,
  36. const ArrayRef<SMLoc> &Loc, bool ShowExpansions,
  37. StringRef Indent = " ")
  38. : Code(Code), Expansions(Expansions), Loc(Loc),
  39. ShowExpansions(ShowExpansions), Indent(Indent) {}
  40. void emit(raw_ostream &OS) const;
  41. };
  42. inline raw_ostream &operator<<(raw_ostream &OS, const CodeExpander &Expander) {
  43. Expander.emit(OS);
  44. return OS;
  45. }
  46. } // end namespace llvm
  47. #endif // ifndef LLVM_UTILS_TABLEGEN_CODEEXPANDER_H