Skip to content

Commit

Permalink
Support 2 leading dashes in manual CLI pre-parsing (#3275)
Browse files Browse the repository at this point in the history
Fixes #3268 and related issues.
  • Loading branch information
kinke authored Jan 12, 2020
1 parent 630cfca commit f7a15a5
Show file tree
Hide file tree
Showing 2 changed files with 37 additions and 15 deletions.
38 changes: 23 additions & 15 deletions driver/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand All @@ -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");
}
}
}
Expand All @@ -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;
}
Expand All @@ -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));
Expand Down
14 changes: 14 additions & 0 deletions tests/driver/cli_preparsing.d
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
// REQUIRES: target_X86


// RUN: not %ldc -o- -conf= %s 2>&1 | FileCheck --check-prefix=NO_CONF %s
// RUN: not %ldc -o- --conf "" %s 2>&1 | FileCheck --check-prefix=NO_CONF %s

// NO_CONF: Error: cannot find source code for runtime library file 'object.d'


// RUN: %ldc -v -o- -mtriple x86_64-vendor-windows-msvc %s 2>&1 | FileCheck --check-prefix=TRIPLE %s
// RUN: %ldc -v -o- --mtriple=x86_64-vendor-windows-msvc %s 2>&1 | FileCheck --check-prefix=TRIPLE %s

// TRIPLE: config
// TRIPLE-SAME: (x86_64-vendor-windows-msvc)

0 comments on commit f7a15a5

Please sign in to comment.