Change behavior when invoking LDC/LDMD without source files

Only display the appropriate usage help (and then fail) if invoked
without any explicit cmdline options. Otherwise emit an error about
missing source files and fail immediately, without displaying the usage
help.

Besides making LDC and LDMD behave identically in this regard, it makes
just more sense IMO (when forgetting to specify a file, LDC previously
just printed the cmdline help without any error message).

It also makes `ldmd2 -transition=?` and `ldmd2 -preview=help` etc. print
the expected help without LDMD special cases.
This commit is contained in:
Martin Kinkelin 2019-02-21 22:11:08 +01:00
parent 12d6193b9a
commit 6765e51799
3 changed files with 14 additions and 28 deletions

View file

@ -66,7 +66,6 @@ version (IN_LLVM)
// in driver/main.cpp // in driver/main.cpp
void registerPredefinedVersions(); void registerPredefinedVersions();
void printLDCUsage();
void codegenModules(ref Modules modules); void codegenModules(ref Modules modules);
// in driver/archiver.cpp // in driver/archiver.cpp
int createStaticLibrary(); int createStaticLibrary();
@ -379,7 +378,7 @@ version (IN_LLVM) {} else
} }
version (IN_LLVM) version (IN_LLVM)
{ {
printLDCUsage(); error(Loc.initial, "No source files");
} }
else else
{ {

View file

@ -371,7 +371,6 @@ void translateArgs(size_t originalArgc, char **originalArgv,
ldcArgs.push_back("-ldmd"); ldcArgs.push_back("-ldmd");
bool vdmd = false; bool vdmd = false;
bool noFiles = true;
bool pic = false; // -fPIC already encountered? bool pic = false; // -fPIC already encountered?
for (size_t i = 1; i < args.size(); i++) { for (size_t i = 1; i < args.size(); i++) {
@ -529,13 +528,9 @@ void translateArgs(size_t originalArgc, char **originalArgv,
} }
} }
/* -extern-std /* -extern-std
*/ * -transition
else if (strcmp(p + 1, "transition=?") == 0) { * -preview
const char *transitionargs[] = {ldcPath.c_str(), p, nullptr}; * -revert
execute(ldcPath, transitionargs);
exit(EXIT_SUCCESS);
}
/* -transition=<id>
* -w * -w
* -wi * -wi
* -O * -O
@ -656,11 +651,9 @@ void translateArgs(size_t originalArgc, char **originalArgv,
exit(EXIT_SUCCESS); exit(EXIT_SUCCESS);
} else if (strcmp(p + 1, "run") == 0) { } else if (strcmp(p + 1, "run") == 0) {
ldcArgs.insert(ldcArgs.end(), args.begin() + i, args.end()); ldcArgs.insert(ldcArgs.end(), args.begin() + i, args.end());
noFiles = (i == args.size() - 1);
break; break;
} else if (p[1] == '\0') { } else if (p[1] == '\0') {
ldcArgs.push_back("-"); ldcArgs.push_back("-");
noFiles = false;
} else if (p[1] == 'C') { } else if (p[1] == 'C') {
ldcArgs.push_back(concat("-", p + 2)); ldcArgs.push_back(concat("-", p + 2));
} else { } else {
@ -686,21 +679,9 @@ void translateArgs(size_t originalArgc, char **originalArgv,
} }
#endif #endif
ldcArgs.push_back(p); ldcArgs.push_back(p);
noFiles = false;
} }
} }
// at least one file is mandatory, except when `-Xi=…` is used
if (noFiles && std::find_if(args.begin(), args.end(), [](const char *arg) {
return startsWith(arg, "-Xi=");
}) == args.end()) {
printUsage(originalArgv[0], ldcPath);
if (originalArgc == 1)
exit(EXIT_FAILURE); // compatible with DMD
else
error("No source file specified.");
}
if (vdmd) { if (vdmd) {
printf(" -- Invoking:"); printf(" -- Invoking:");
for (const auto &arg : ldcArgs) { for (const auto &arg : ldcArgs) {
@ -762,6 +743,11 @@ int cppmain(int argc, char **argv) {
error("Could not locate " LDC_EXE_NAME " executable."); error("Could not locate " LDC_EXE_NAME " executable.");
} }
if (argc == 1) {
printUsage(argv[0], ldcPath);
exit(EXIT_FAILURE);
}
// We need to manually set up argv[0] and the terminating NULL. // We need to manually set up argv[0] and the terminating NULL.
std::vector<const char *> args; std::vector<const char *> args;
args.push_back(ldcPath.c_str()); args.push_back(ldcPath.c_str());

View file

@ -896,6 +896,11 @@ int cppmain(int argc, char **argv) {
Strings files; Strings files;
parseCommandLine(argc, argv, files); parseCommandLine(argc, argv, files);
if (argc == 1) {
cl::PrintHelpMessage(/*Hidden=*/false, /*Categorized=*/true);
exit(EXIT_FAILURE);
}
if (global.errors) { if (global.errors) {
fatal(); fatal();
} }
@ -977,10 +982,6 @@ int cppmain(int argc, char **argv) {
return mars_mainBody(global.params, files, libmodules); return mars_mainBody(global.params, files, libmodules);
} }
void printLDCUsage() {
cl::PrintHelpMessage(/*Hidden=*/false, /*Categorized=*/true);
}
void codegenModules(Modules &modules) { void codegenModules(Modules &modules) {
// Generate one or more object/IR/bitcode files/dcompute kernels. // Generate one or more object/IR/bitcode files/dcompute kernels.
if (global.params.obj && !modules.empty()) { if (global.params.obj && !modules.empty()) {