10b57cec5SDimitry Andric //===- Reproduce.cpp - Utilities for creating reproducers -----------------===// 20b57cec5SDimitry Andric // 30b57cec5SDimitry Andric // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. 40b57cec5SDimitry Andric // See https://llvm.org/LICENSE.txt for license information. 50b57cec5SDimitry Andric // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception 60b57cec5SDimitry Andric // 70b57cec5SDimitry Andric //===----------------------------------------------------------------------===// 80b57cec5SDimitry Andric 90b57cec5SDimitry Andric #include "lld/Common/Reproduce.h" 100b57cec5SDimitry Andric #include "llvm/Option/Arg.h" 110b57cec5SDimitry Andric #include "llvm/Support/Error.h" 120b57cec5SDimitry Andric #include "llvm/Support/FileSystem.h" 130b57cec5SDimitry Andric #include "llvm/Support/Path.h" 140b57cec5SDimitry Andric 150b57cec5SDimitry Andric using namespace lld; 160b57cec5SDimitry Andric using namespace llvm; 170b57cec5SDimitry Andric using namespace llvm::sys; 180b57cec5SDimitry Andric 190b57cec5SDimitry Andric // Makes a given pathname an absolute path first, and then remove 200b57cec5SDimitry Andric // beginning /. For example, "../foo.o" is converted to "home/john/foo.o", 210b57cec5SDimitry Andric // assuming that the current directory is "/home/john/bar". 220b57cec5SDimitry Andric // Returned string is a forward slash separated path even on Windows to avoid 230b57cec5SDimitry Andric // a mess with backslash-as-escape and backslash-as-path-separator. relativeToRoot(StringRef path)240b57cec5SDimitry Andricstd::string lld::relativeToRoot(StringRef path) { 250b57cec5SDimitry Andric SmallString<128> abs = path; 260b57cec5SDimitry Andric if (fs::make_absolute(abs)) 275ffd83dbSDimitry Andric return std::string(path); 280b57cec5SDimitry Andric path::remove_dots(abs, /*remove_dot_dot=*/true); 290b57cec5SDimitry Andric 300b57cec5SDimitry Andric // This is Windows specific. root_name() returns a drive letter 310b57cec5SDimitry Andric // (e.g. "c:") or a UNC name (//net). We want to keep it as part 320b57cec5SDimitry Andric // of the result. 330b57cec5SDimitry Andric SmallString<128> res; 340b57cec5SDimitry Andric StringRef root = path::root_name(abs); 35*06c3fb27SDimitry Andric if (root.ends_with(":")) 360b57cec5SDimitry Andric res = root.drop_back(); 37*06c3fb27SDimitry Andric else if (root.starts_with("//")) 380b57cec5SDimitry Andric res = root.substr(2); 390b57cec5SDimitry Andric 400b57cec5SDimitry Andric path::append(res, path::relative_path(abs)); 410b57cec5SDimitry Andric return path::convert_to_slash(res); 420b57cec5SDimitry Andric } 430b57cec5SDimitry Andric 440b57cec5SDimitry Andric // Quote a given string if it contains a space character. quote(StringRef s)450b57cec5SDimitry Andricstd::string lld::quote(StringRef s) { 460b57cec5SDimitry Andric if (s.contains(' ')) 470b57cec5SDimitry Andric return ("\"" + s + "\"").str(); 485ffd83dbSDimitry Andric return std::string(s); 490b57cec5SDimitry Andric } 500b57cec5SDimitry Andric 510b57cec5SDimitry Andric // Converts an Arg to a string representation suitable for a response file. 520b57cec5SDimitry Andric // To show an Arg in a diagnostic, use Arg::getAsString() instead. toString(const opt::Arg & arg)530b57cec5SDimitry Andricstd::string lld::toString(const opt::Arg &arg) { 545ffd83dbSDimitry Andric std::string k = std::string(arg.getSpelling()); 550b57cec5SDimitry Andric if (arg.getNumValues() == 0) 560b57cec5SDimitry Andric return k; 57e8d8bef9SDimitry Andric std::string v; 58e8d8bef9SDimitry Andric for (size_t i = 0; i < arg.getNumValues(); ++i) { 59e8d8bef9SDimitry Andric if (i > 0) 60e8d8bef9SDimitry Andric v.push_back(' '); 61e8d8bef9SDimitry Andric v += quote(arg.getValue(i)); 62e8d8bef9SDimitry Andric } 630b57cec5SDimitry Andric if (arg.getOption().getRenderStyle() == opt::Option::RenderJoinedStyle) 640b57cec5SDimitry Andric return k + v; 650b57cec5SDimitry Andric return k + " " + v; 660b57cec5SDimitry Andric } 67