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