Path: blob/main/contrib/llvm-project/llvm/utils/TableGen/Common/GlobalISel/CodeExpander.cpp
35315 views
//===- CodeExpander.cpp - Expand variables in a string --------------------===//1//2// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.3// See https://llvm.org/LICENSE.txt for license information.4// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception5//6//===----------------------------------------------------------------------===//7//8/// \file Expand the variables in a string.9//10//===----------------------------------------------------------------------===//1112#include "CodeExpander.h"13#include "CodeExpansions.h"14#include "llvm/Support/raw_ostream.h"15#include "llvm/TableGen/Error.h"1617using namespace llvm;1819void CodeExpander::emit(raw_ostream &OS) const {20StringRef Current = Code;2122while (!Current.empty()) {23size_t Pos = Current.find_first_of("$\n\\");24if (Pos == StringRef::npos) {25OS << Current;26Current = "";27continue;28}2930OS << Current.substr(0, Pos);31Current = Current.substr(Pos);3233if (Current.consume_front("\n")) {34OS << "\n" << Indent;35continue;36}3738if (Current.starts_with("\\$") || Current.starts_with("\\\\")) {39OS << Current[1];40Current = Current.drop_front(2);41continue;42}4344if (Current.consume_front("\\"))45continue;4647if (Current.starts_with("${")) {48StringRef StartVar = Current;49Current = Current.drop_front(2);50StringRef Var;51std::tie(Var, Current) = Current.split("}");5253// Warn if we split because no terminator was found.54StringRef EndVar = StartVar.drop_front(2 /* ${ */ + Var.size());55if (EndVar.empty()) {56PrintWarning(Loc, "Unterminated expansion '${" + Var + "'");57PrintNote("Code: [{" + Code + "}]");58}5960auto ValueI = Expansions.find(Var);61if (ValueI == Expansions.end()) {62PrintError(Loc,63"Attempt to expand an undeclared variable '" + Var + "'");64PrintNote("Code: [{" + Code + "}]");65}66if (ShowExpansions)67OS << "/*$" << Var << "{*/";68OS << Expansions.lookup(Var);69if (ShowExpansions)70OS << "/*}*/";71continue;72}7374PrintWarning(Loc, "Assuming missing escape character: \\$");75PrintNote("Code: [{" + Code + "}]");76OS << "$";77Current = Current.drop_front(1);78}79}808182