2016-07-26 10:00:42 +08:00
|
|
|
//===- Reproduce.cpp - Utilities for creating reproducers -----------------===//
|
|
|
|
//
|
2019-01-19 16:50:56 +08:00
|
|
|
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
|
|
|
|
// See https://llvm.org/LICENSE.txt for license information.
|
|
|
|
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
|
2016-07-26 10:00:42 +08:00
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
2017-10-03 05:00:41 +08:00
|
|
|
#include "lld/Common/Reproduce.h"
|
2016-07-26 10:00:42 +08:00
|
|
|
#include "llvm/Option/Arg.h"
|
|
|
|
#include "llvm/Support/Error.h"
|
|
|
|
#include "llvm/Support/FileSystem.h"
|
|
|
|
#include "llvm/Support/Path.h"
|
|
|
|
|
|
|
|
using namespace lld;
|
|
|
|
using namespace llvm;
|
2017-01-06 10:33:53 +08:00
|
|
|
using namespace llvm::sys;
|
2016-07-26 10:00:42 +08:00
|
|
|
|
|
|
|
// Makes a given pathname an absolute path first, and then remove
|
|
|
|
// beginning /. For example, "../foo.o" is converted to "home/john/foo.o",
|
|
|
|
// assuming that the current directory is "/home/john/bar".
|
2016-11-12 14:27:42 +08:00
|
|
|
// Returned string is a forward slash separated path even on Windows to avoid
|
2016-11-11 21:23:13 +08:00
|
|
|
// a mess with backslash-as-escape and backslash-as-path-separator.
|
2016-07-26 10:00:42 +08:00
|
|
|
std::string lld::relativeToRoot(StringRef path) {
|
|
|
|
SmallString<128> abs = path;
|
2017-01-06 10:33:53 +08:00
|
|
|
if (fs::make_absolute(abs))
|
2020-01-29 03:23:46 +08:00
|
|
|
return std::string(path);
|
2016-07-26 10:00:42 +08:00
|
|
|
path::remove_dots(abs, /*remove_dot_dot=*/true);
|
|
|
|
|
|
|
|
// This is Windows specific. root_name() returns a drive letter
|
|
|
|
// (e.g. "c:") or a UNC name (//net). We want to keep it as part
|
|
|
|
// of the result.
|
|
|
|
SmallString<128> res;
|
|
|
|
StringRef root = path::root_name(abs);
|
|
|
|
if (root.endswith(":"))
|
|
|
|
res = root.drop_back();
|
|
|
|
else if (root.startswith("//"))
|
|
|
|
res = root.substr(2);
|
|
|
|
|
|
|
|
path::append(res, path::relative_path(abs));
|
2017-01-09 09:47:15 +08:00
|
|
|
return path::convert_to_slash(res);
|
2016-07-26 10:00:42 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
// Quote a given string if it contains a space character.
|
|
|
|
std::string lld::quote(StringRef s) {
|
2017-07-20 05:40:26 +08:00
|
|
|
if (s.contains(' '))
|
|
|
|
return ("\"" + s + "\"").str();
|
2020-01-29 03:23:46 +08:00
|
|
|
return std::string(s);
|
2016-07-26 10:00:42 +08:00
|
|
|
}
|
|
|
|
|
Let unaliased Args track which Alias they were created from, and use that in Arg::getAsString() for diagnostics
With this, `clang-cl /source-charset:utf-16 test.cc` now prints `invalid
value 'utf-16' in '/source-charset:utf-16'` instead of `invalid value
'utf-16' in '-finput-charset=utf-16'` before, and several other clang-cl
flags produce much less confusing output as well.
Fixes PR29106.
Since an arg and its alias can have different arg types (joined vs not)
and different values (because of AliasArgs<>), I chose to give the Alias
its own Arg object. For convenience, I just store the alias directly in
the unaliased arg – there aren't many arg objects at runtime, so that
seems ok.
Finally, I changed Arg::getAsString() to use the alias's representation
if it's present – that function was already documented as being the
suitable function for diagnostics, and most callers already used it for
diagnostics.
Implementation-wise, Arg::accept() previously used to parse things as
the unaliased option. The core of that switch is now extracted into a
new function acceptInternal() which parses as the _aliased_ option, and
the previously-intermingled unaliasing is now done as an explicit step
afterwards.
(This also changes one place in lld that didn't use getAsString() for
diagnostics, so that that one place now also prints the flag as the user
wrote it, not as it looks after it went through unaliasing.)
Differential Revision: https://reviews.llvm.org/D64253
llvm-svn: 365413
2019-07-09 08:34:08 +08:00
|
|
|
// Converts an Arg to a string representation suitable for a response file.
|
|
|
|
// To show an Arg in a diagnostic, use Arg::getAsString() instead.
|
2017-12-06 00:50:46 +08:00
|
|
|
std::string lld::toString(const opt::Arg &arg) {
|
2020-01-29 03:23:46 +08:00
|
|
|
std::string k = std::string(arg.getSpelling());
|
2017-12-06 00:50:46 +08:00
|
|
|
if (arg.getNumValues() == 0)
|
2016-07-26 10:00:42 +08:00
|
|
|
return k;
|
2020-11-29 11:38:27 +08:00
|
|
|
std::string v;
|
|
|
|
for (size_t i = 0; i < arg.getNumValues(); ++i) {
|
|
|
|
if (i > 0)
|
|
|
|
v.push_back(' ');
|
|
|
|
v += quote(arg.getValue(i));
|
|
|
|
}
|
2017-12-06 00:50:46 +08:00
|
|
|
if (arg.getOption().getRenderStyle() == opt::Option::RenderJoinedStyle)
|
2016-07-26 10:00:42 +08:00
|
|
|
return k + v;
|
|
|
|
return k + " " + v;
|
|
|
|
}
|