xref: /freebsd/contrib/llvm-project/lld/Common/Reproduce.cpp (revision 0b57cec536236d46e3dba9bd041533462f33dbb7)
1*0b57cec5SDimitry Andric //===- Reproduce.cpp - Utilities for creating reproducers -----------------===//
2*0b57cec5SDimitry Andric //
3*0b57cec5SDimitry Andric // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4*0b57cec5SDimitry Andric // See https://llvm.org/LICENSE.txt for license information.
5*0b57cec5SDimitry Andric // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6*0b57cec5SDimitry Andric //
7*0b57cec5SDimitry Andric //===----------------------------------------------------------------------===//
8*0b57cec5SDimitry Andric 
9*0b57cec5SDimitry Andric #include "lld/Common/Reproduce.h"
10*0b57cec5SDimitry Andric #include "llvm/Option/Arg.h"
11*0b57cec5SDimitry Andric #include "llvm/Support/Error.h"
12*0b57cec5SDimitry Andric #include "llvm/Support/FileSystem.h"
13*0b57cec5SDimitry Andric #include "llvm/Support/Path.h"
14*0b57cec5SDimitry Andric 
15*0b57cec5SDimitry Andric using namespace lld;
16*0b57cec5SDimitry Andric using namespace llvm;
17*0b57cec5SDimitry Andric using namespace llvm::sys;
18*0b57cec5SDimitry Andric 
19*0b57cec5SDimitry Andric // Makes a given pathname an absolute path first, and then remove
20*0b57cec5SDimitry Andric // beginning /. For example, "../foo.o" is converted to "home/john/foo.o",
21*0b57cec5SDimitry Andric // assuming that the current directory is "/home/john/bar".
22*0b57cec5SDimitry Andric // Returned string is a forward slash separated path even on Windows to avoid
23*0b57cec5SDimitry Andric // a mess with backslash-as-escape and backslash-as-path-separator.
24*0b57cec5SDimitry Andric std::string lld::relativeToRoot(StringRef path) {
25*0b57cec5SDimitry Andric   SmallString<128> abs = path;
26*0b57cec5SDimitry Andric   if (fs::make_absolute(abs))
27*0b57cec5SDimitry Andric     return path;
28*0b57cec5SDimitry Andric   path::remove_dots(abs, /*remove_dot_dot=*/true);
29*0b57cec5SDimitry Andric 
30*0b57cec5SDimitry Andric   // This is Windows specific. root_name() returns a drive letter
31*0b57cec5SDimitry Andric   // (e.g. "c:") or a UNC name (//net). We want to keep it as part
32*0b57cec5SDimitry Andric   // of the result.
33*0b57cec5SDimitry Andric   SmallString<128> res;
34*0b57cec5SDimitry Andric   StringRef root = path::root_name(abs);
35*0b57cec5SDimitry Andric   if (root.endswith(":"))
36*0b57cec5SDimitry Andric     res = root.drop_back();
37*0b57cec5SDimitry Andric   else if (root.startswith("//"))
38*0b57cec5SDimitry Andric     res = root.substr(2);
39*0b57cec5SDimitry Andric 
40*0b57cec5SDimitry Andric   path::append(res, path::relative_path(abs));
41*0b57cec5SDimitry Andric   return path::convert_to_slash(res);
42*0b57cec5SDimitry Andric }
43*0b57cec5SDimitry Andric 
44*0b57cec5SDimitry Andric // Quote a given string if it contains a space character.
45*0b57cec5SDimitry Andric std::string lld::quote(StringRef s) {
46*0b57cec5SDimitry Andric   if (s.contains(' '))
47*0b57cec5SDimitry Andric     return ("\"" + s + "\"").str();
48*0b57cec5SDimitry Andric   return s;
49*0b57cec5SDimitry Andric }
50*0b57cec5SDimitry Andric 
51*0b57cec5SDimitry Andric // Converts an Arg to a string representation suitable for a response file.
52*0b57cec5SDimitry Andric // To show an Arg in a diagnostic, use Arg::getAsString() instead.
53*0b57cec5SDimitry Andric std::string lld::toString(const opt::Arg &arg) {
54*0b57cec5SDimitry Andric   std::string k = arg.getSpelling();
55*0b57cec5SDimitry Andric   if (arg.getNumValues() == 0)
56*0b57cec5SDimitry Andric     return k;
57*0b57cec5SDimitry Andric   std::string v = quote(arg.getValue());
58*0b57cec5SDimitry Andric   if (arg.getOption().getRenderStyle() == opt::Option::RenderJoinedStyle)
59*0b57cec5SDimitry Andric     return k + v;
60*0b57cec5SDimitry Andric   return k + " " + v;
61*0b57cec5SDimitry Andric }
62