xref: /freebsd/contrib/llvm-project/lld/Common/Reproduce.cpp (revision 5ffd83dbcc34f10e07f6d3e968ae6365869615f4)
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.
240b57cec5SDimitry Andric std::string lld::relativeToRoot(StringRef path) {
250b57cec5SDimitry Andric   SmallString<128> abs = path;
260b57cec5SDimitry Andric   if (fs::make_absolute(abs))
27*5ffd83dbSDimitry 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);
350b57cec5SDimitry Andric   if (root.endswith(":"))
360b57cec5SDimitry Andric     res = root.drop_back();
370b57cec5SDimitry Andric   else if (root.startswith("//"))
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.
450b57cec5SDimitry Andric std::string lld::quote(StringRef s) {
460b57cec5SDimitry Andric   if (s.contains(' '))
470b57cec5SDimitry Andric     return ("\"" + s + "\"").str();
48*5ffd83dbSDimitry 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.
530b57cec5SDimitry Andric std::string lld::toString(const opt::Arg &arg) {
54*5ffd83dbSDimitry Andric   std::string k = std::string(arg.getSpelling());
550b57cec5SDimitry Andric   if (arg.getNumValues() == 0)
560b57cec5SDimitry Andric     return k;
570b57cec5SDimitry Andric   std::string v = quote(arg.getValue());
580b57cec5SDimitry Andric   if (arg.getOption().getRenderStyle() == opt::Option::RenderJoinedStyle)
590b57cec5SDimitry Andric     return k + v;
600b57cec5SDimitry Andric   return k + " " + v;
610b57cec5SDimitry Andric }
62