annotate lld/Common/Reproduce.cpp @ 237:c80f45b162ad llvm-original

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