summaryrefslogtreecommitdiff
path: root/lib/Driver/Driver.cpp
diff options
context:
space:
mode:
authorNico Weber <nicolasweber@gmx.de>2019-05-23 17:58:33 +0000
committerNico Weber <nicolasweber@gmx.de>2019-05-23 17:58:33 +0000
commita663efb444b6006ea3832063f5daacede5f9f888 (patch)
tree3bdec72d6863fc85ab5fc17eccdb35e921e72d99 /lib/Driver/Driver.cpp
parent5d63c8b6bdf9b8e8620e69cd0eebdc33a621cfb2 (diff)
downloadclang-a663efb444b6006ea3832063f5daacede5f9f888.tar.gz
lld-link, clang: Treat non-existent input files as possible spellos for option flags
OptTable treats arguments starting with / that aren't a known option as filenames. This means lld-link's and clang-cl's typo correction for unknown flags didn't do spell checking for misspelled options that start with /. I first tried changing OptTable, but that got pretty messy, see PR41787 comments 2 and 3. Instead, let lld-link's and clang's (including clang-cl's) "file not found" diagnostic check if a non-existent file looks like it could be a mis-spelled option, and if so add a "did you mean" suggestion to the "file not found" diagnostic. While here, make formatting of a few diagnostics a bit more self-consistent. Fixes PR41787. Differential Revision: https://reviews.llvm.org/D62276 git-svn-id: https://llvm.org/svn/llvm-project/cfe/trunk@361518 91177308-0d34-0410-b5e6-96231b3b80d8
Diffstat (limited to 'lib/Driver/Driver.cpp')
-rw-r--r--lib/Driver/Driver.cpp41
1 files changed, 30 insertions, 11 deletions
diff --git a/lib/Driver/Driver.cpp b/lib/Driver/Driver.cpp
index 06c0e3bdb3..0f7bbb6eb0 100644
--- a/lib/Driver/Driver.cpp
+++ b/lib/Driver/Driver.cpp
@@ -1975,11 +1975,9 @@ void Driver::BuildUniversalActions(Compilation &C, const ToolChain &TC,
}
}
-/// Check that the file referenced by Value exists. If it doesn't,
-/// issue a diagnostic and return false.
-static bool DiagnoseInputExistence(const Driver &D, const DerivedArgList &Args,
- StringRef Value, types::ID Ty) {
- if (!D.getCheckInputsExist())
+bool Driver::DiagnoseInputExistence(const DerivedArgList &Args, StringRef Value,
+ types::ID Ty, bool TypoCorrect) const {
+ if (!getCheckInputsExist())
return true;
// stdin always exists.
@@ -1995,10 +1993,10 @@ static bool DiagnoseInputExistence(const Driver &D, const DerivedArgList &Args,
}
}
- if (D.getVFS().exists(Path))
+ if (getVFS().exists(Path))
return true;
- if (D.IsCLMode()) {
+ if (IsCLMode()) {
if (!llvm::sys::path::is_absolute(Twine(Path)) &&
llvm::sys::Process::FindInEnvPath("LIB", Value))
return true;
@@ -2011,7 +2009,26 @@ static bool DiagnoseInputExistence(const Driver &D, const DerivedArgList &Args,
}
}
- D.Diag(clang::diag::err_drv_no_such_file) << Path;
+ if (TypoCorrect) {
+ // Check if the filename is a typo for an option flag. OptTable thinks
+ // that all args that are not known options and that start with / are
+ // filenames, but e.g. `/diagnostic:caret` is more likely a typo for
+ // the option `/diagnostics:caret` than a reference to a file in the root
+ // directory.
+ unsigned IncludedFlagsBitmask;
+ unsigned ExcludedFlagsBitmask;
+ std::tie(IncludedFlagsBitmask, ExcludedFlagsBitmask) =
+ getIncludeExcludeOptionFlagMasks(IsCLMode());
+ std::string Nearest;
+ if (getOpts().findNearest(Value, Nearest, IncludedFlagsBitmask,
+ ExcludedFlagsBitmask) <= 1) {
+ Diag(clang::diag::err_drv_no_such_file_with_suggestion)
+ << Path << Nearest;
+ return false;
+ }
+ }
+
+ Diag(clang::diag::err_drv_no_such_file) << Path;
return false;
}
@@ -2128,19 +2145,21 @@ void Driver::BuildInputs(const ToolChain &TC, DerivedArgList &Args,
}
}
- if (DiagnoseInputExistence(*this, Args, Value, Ty))
+ if (DiagnoseInputExistence(Args, Value, Ty, /*TypoCorrect=*/true))
Inputs.push_back(std::make_pair(Ty, A));
} else if (A->getOption().matches(options::OPT__SLASH_Tc)) {
StringRef Value = A->getValue();
- if (DiagnoseInputExistence(*this, Args, Value, types::TY_C)) {
+ if (DiagnoseInputExistence(Args, Value, types::TY_C,
+ /*TypoCorrect=*/false)) {
Arg *InputArg = MakeInputArg(Args, *Opts, A->getValue());
Inputs.push_back(std::make_pair(types::TY_C, InputArg));
}
A->claim();
} else if (A->getOption().matches(options::OPT__SLASH_Tp)) {
StringRef Value = A->getValue();
- if (DiagnoseInputExistence(*this, Args, Value, types::TY_CXX)) {
+ if (DiagnoseInputExistence(Args, Value, types::TY_CXX,
+ /*TypoCorrect=*/false)) {
Arg *InputArg = MakeInputArg(Args, *Opts, A->getValue());
Inputs.push_back(std::make_pair(types::TY_CXX, InputArg));
}