Path: blob/main/contrib/llvm-project/lld/Common/Reproduce.cpp
34879 views
//===- Reproduce.cpp - Utilities for creating reproducers -----------------===//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//===----------------------------------------------------------------------===//78#include "lld/Common/Reproduce.h"9#include "llvm/Option/Arg.h"10#include "llvm/Support/Error.h"11#include "llvm/Support/FileSystem.h"12#include "llvm/Support/Path.h"1314using namespace lld;15using namespace llvm;16using namespace llvm::sys;1718// Makes a given pathname an absolute path first, and then remove19// beginning /. For example, "../foo.o" is converted to "home/john/foo.o",20// assuming that the current directory is "/home/john/bar".21// Returned string is a forward slash separated path even on Windows to avoid22// a mess with backslash-as-escape and backslash-as-path-separator.23std::string lld::relativeToRoot(StringRef path) {24SmallString<128> abs = path;25if (fs::make_absolute(abs))26return std::string(path);27path::remove_dots(abs, /*remove_dot_dot=*/true);2829// This is Windows specific. root_name() returns a drive letter30// (e.g. "c:") or a UNC name (//net). We want to keep it as part31// of the result.32SmallString<128> res;33StringRef root = path::root_name(abs);34if (root.ends_with(":"))35res = root.drop_back();36else if (root.starts_with("//"))37res = root.substr(2);3839path::append(res, path::relative_path(abs));40return path::convert_to_slash(res);41}4243// Quote a given string if it contains a space character.44std::string lld::quote(StringRef s) {45if (s.contains(' '))46return ("\"" + s + "\"").str();47return std::string(s);48}4950// Converts an Arg to a string representation suitable for a response file.51// To show an Arg in a diagnostic, use Arg::getAsString() instead.52std::string lld::toString(const opt::Arg &arg) {53std::string k = std::string(arg.getSpelling());54if (arg.getNumValues() == 0)55return k;56std::string v;57for (size_t i = 0; i < arg.getNumValues(); ++i) {58if (i > 0)59v.push_back(' ');60v += quote(arg.getValue(i));61}62if (arg.getOption().getRenderStyle() == opt::Option::RenderJoinedStyle)63return k + v;64return k + " " + v;65}666768