Run DCE after a LoopFlatten test to reduce spurious output [nfc]
[llvm-project.git] / llvm / utils / TableGen / GlobalISel / CodeExpander.cpp
blob42b4aabf2755e7f8afe714c63309f51b283962bb
1 //===- CodeExpander.cpp - 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.
11 //===----------------------------------------------------------------------===//
13 #include "CodeExpander.h"
14 #include "CodeExpansions.h"
15 #include "llvm/Support/raw_ostream.h"
16 #include "llvm/TableGen/Error.h"
18 using namespace llvm;
20 void CodeExpander::emit(raw_ostream &OS) const {
21 StringRef Current = Code;
23 while (!Current.empty()) {
24 size_t Pos = Current.find_first_of("$\n\\");
25 if (Pos == StringRef::npos) {
26 OS << Current;
27 Current = "";
28 continue;
31 OS << Current.substr(0, Pos);
32 Current = Current.substr(Pos);
34 if (Current.startswith("\n")) {
35 OS << "\n" << Indent;
36 Current = Current.drop_front(1);
37 continue;
40 if (Current.startswith("\\$") || Current.startswith("\\\\")) {
41 OS << Current[1];
42 Current = Current.drop_front(2);
43 continue;
46 if (Current.startswith("\\")) {
47 Current = Current.drop_front(1);
48 continue;
51 if (Current.startswith("${")) {
52 StringRef StartVar = Current;
53 Current = Current.drop_front(2);
54 StringRef Var;
55 std::tie(Var, Current) = Current.split("}");
57 // Warn if we split because no terminator was found.
58 StringRef EndVar = StartVar.drop_front(2 /* ${ */ + Var.size());
59 if (EndVar.empty()) {
60 PrintWarning(Loc, "Unterminated expansion '${" + Var + "'");
61 PrintNote("Code: [{" + Code + "}]");
64 auto ValueI = Expansions.find(Var);
65 if (ValueI == Expansions.end()) {
66 PrintError(Loc,
67 "Attempt to expand an undeclared variable '" + Var + "'");
68 PrintNote("Code: [{" + Code + "}]");
70 if (ShowExpansions)
71 OS << "/*$" << Var << "{*/";
72 OS << Expansions.lookup(Var);
73 if (ShowExpansions)
74 OS << "/*}*/";
75 continue;
78 PrintWarning(Loc, "Assuming missing escape character: \\$");
79 PrintNote("Code: [{" + Code + "}]");
80 OS << "$";
81 Current = Current.drop_front(1);