Support 2 leading dashes in manual CLI pre-parsing (#3275)

Fixes #3268 and related issues.
This commit is contained in:
Martin Kinkelin 2020-01-12 13:31:22 +01:00 committed by GitHub
parent 630cfcaf63
commit f7a15a59c5
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
2 changed files with 37 additions and 15 deletions

View file

@ -169,17 +169,23 @@ void processVersions(std::vector<std::string> &list, const char *type,
}
}
// Tries to parse the value of `-[-]<option>{=, }<value>` at `args[i]`.
template <int N> // option length incl. terminating null
void tryParse(const llvm::SmallVectorImpl<const char *> &args, size_t i,
const char *&output, const char (&option)[N]) {
if (strncmp(args[i], option, N - 1) != 0)
const char *&value, const char (&option)[N]) {
const char *arg = args[i];
if (arg[0] != '-')
return;
char nextChar = args[i][N - 1];
const char *start = arg + (arg[1] == '-' ? 2 : 1);
if (strncmp(start, option, N - 1) != 0)
return;
const char nextChar = start[N - 1];
if (nextChar == '=')
output = args[i] + N;
value = start + N;
else if (nextChar == 0 && i < args.size() - 1)
output = args[i + 1];
value = args[i + 1];
}
bool tryParseLowmem(const llvm::SmallVectorImpl<const char *> &args) {
@ -188,13 +194,14 @@ bool tryParseLowmem(const llvm::SmallVectorImpl<const char *> &args) {
if (args::isRunArg(args[i]))
break;
if (strncmp(args[i], "-lowmem", 7) == 0) {
auto remainder = args[i] + 7;
if (remainder[0] == 0) {
llvm::StringRef arg = args[i];
if (arg.startswith("-lowmem") || arg.startswith("--lowmem")) {
auto remainder = arg.substr(arg[1] == '-' ? 8 : 7);
if (remainder.empty()) {
lowmem = true;
} else if (remainder[0] == '=') {
lowmem = strcmp(remainder + 1, "true") == 0 ||
strcmp(remainder + 1, "TRUE") == 0;
auto value = remainder.substr(1);
lowmem = (value == "true" || value == "TRUE");
}
}
}
@ -207,7 +214,7 @@ tryGetExplicitConfFile(const llvm::SmallVectorImpl<const char *> &args) {
for (size_t i = 1; i < args.size(); ++i) {
if (args::isRunArg(args[i]))
break;
tryParse(args, i, conf, "-conf");
tryParse(args, i, conf, "conf");
}
return conf;
}
@ -223,18 +230,19 @@ tryGetExplicitTriple(const llvm::SmallVectorImpl<const char *> &args) {
if (args::isRunArg(args[i]))
break;
if (sizeof(void *) != 4 && strcmp(args[i], "-m32") == 0) {
llvm::StringRef arg = args[i];
if (sizeof(void *) != 4 && (arg == "-m32" || arg == "--m32")) {
triple = triple.get32BitArchVariant();
if (triple.getArch() == llvm::Triple::ArchType::x86)
triple.setArchName("i686"); // instead of i386
return triple;
}
if (sizeof(void *) != 8 && strcmp(args[i], "-m64") == 0)
if (sizeof(void *) != 8 && (arg == "-m64" || arg == "--m64"))
return triple.get64BitArchVariant();
tryParse(args, i, mtriple, "-mtriple");
tryParse(args, i, march, "-march");
tryParse(args, i, mtriple, "mtriple");
tryParse(args, i, march, "march");
}
if (mtriple)
triple = llvm::Triple(llvm::Triple::normalize(mtriple));