diff options
| author | Sam McCall <sam.mccall@gmail.com> | 2019-11-29 19:37:48 +0100 |
|---|---|---|
| committer | Sam McCall <sam.mccall@gmail.com> | 2019-12-06 09:47:03 +0100 |
| commit | 99768b243cd7afd312745da58b20ea067f39f89e (patch) | |
| tree | 3f4b3c4a6f2a404a2c82e0da1d0b86ce0a8bc05d /clang-tools-extra/clangd/GlobalCompilationDatabase.cpp | |
| parent | 4dac97eb1e6563750e682e482e68f29ac076a4f7 (diff) | |
| download | bcm5719-llvm-99768b243cd7afd312745da58b20ea067f39f89e.tar.gz bcm5719-llvm-99768b243cd7afd312745da58b20ea067f39f89e.zip | |
[clangd] (take 2) Try harder to find a plausible `clang` as argv0, particularly on Mac.
Summary:
This was originally committed in 88bccded8fa169481fa367debf5ec615640635a1,
and reverted in 93f77617abba512d2861e2fc50ce385883f587b6.
This version is now much more testable: the "detect toolchain properties" part
is still not tested but also not active in tests.
All the command manipulation based on the detected properties is
directly tested, and also not active in other tests.
Fixes https://github.com/clangd/clangd/issues/211
Fixes https://github.com/clangd/clangd/issues/178
Reviewers: kbobyrev, ilya-biryukov
Subscribers: mgorny, ormris, cfe-commits, usaxena95, kadircet, arphaman, jkorous, MaskRay
Tags: #clang
Differential Revision: https://reviews.llvm.org/D71029
Diffstat (limited to 'clang-tools-extra/clangd/GlobalCompilationDatabase.cpp')
| -rw-r--r-- | clang-tools-extra/clangd/GlobalCompilationDatabase.cpp | 49 |
1 files changed, 9 insertions, 40 deletions
diff --git a/clang-tools-extra/clangd/GlobalCompilationDatabase.cpp b/clang-tools-extra/clangd/GlobalCompilationDatabase.cpp index ed3b86f0f55..34a0346b996 100644 --- a/clang-tools-extra/clangd/GlobalCompilationDatabase.cpp +++ b/clang-tools-extra/clangd/GlobalCompilationDatabase.cpp @@ -18,7 +18,9 @@ #include "llvm/ADT/STLExtras.h" #include "llvm/ADT/SmallString.h" #include "llvm/Support/FileSystem.h" +#include "llvm/Support/FileUtilities.h" #include "llvm/Support/Path.h" +#include "llvm/Support/Program.h" #include <string> #include <tuple> #include <vector> @@ -27,30 +29,6 @@ namespace clang { namespace clangd { namespace { -void adjustArguments(tooling::CompileCommand &Cmd, - llvm::StringRef ResourceDir) { - tooling::ArgumentsAdjuster ArgsAdjuster = tooling::combineAdjusters( - // clangd should not write files to disk, including dependency files - // requested on the command line. - tooling::getClangStripDependencyFileAdjuster(), - // Strip plugin related command line arguments. Clangd does - // not support plugins currently. Therefore it breaks if - // compiler tries to load plugins. - tooling::combineAdjusters(tooling::getStripPluginsAdjuster(), - tooling::getClangSyntaxOnlyAdjuster())); - - Cmd.CommandLine = ArgsAdjuster(Cmd.CommandLine, Cmd.Filename); - // Inject the resource dir. - // FIXME: Don't overwrite it if it's already there. - if (!ResourceDir.empty()) - Cmd.CommandLine.push_back(("-resource-dir=" + ResourceDir).str()); -} - -std::string getStandardResourceDir() { - static int Dummy; // Just an address in this process. - return CompilerInvocation::GetResourcesPath("clangd", (void *)&Dummy); -} - // Runs the given action on all parent directories of filename, starting from // deepest directory and going up to root. Stops whenever action succeeds. void actOnAllParentDirectories(PathRef FileName, @@ -63,19 +41,9 @@ void actOnAllParentDirectories(PathRef FileName, } // namespace -static std::string getFallbackClangPath() { - static int Dummy; - std::string ClangdExecutable = - llvm::sys::fs::getMainExecutable("clangd", (void *)&Dummy); - SmallString<128> ClangPath; - ClangPath = llvm::sys::path::parent_path(ClangdExecutable); - llvm::sys::path::append(ClangPath, "clang"); - return ClangPath.str(); -} - tooling::CompileCommand GlobalCompilationDatabase::getFallbackCommand(PathRef File) const { - std::vector<std::string> Argv = {getFallbackClangPath()}; + std::vector<std::string> Argv = {"clang"}; // Clang treats .h files as C by default and files without extension as linker // input, resulting in unhelpful diagnostics. // Parsing as Objective C++ is friendly to more cases. @@ -263,9 +231,8 @@ DirectoryBasedGlobalCompilationDatabase::getProjectInfo(PathRef File) const { OverlayCDB::OverlayCDB(const GlobalCompilationDatabase *Base, std::vector<std::string> FallbackFlags, - llvm::Optional<std::string> ResourceDir) - : Base(Base), ResourceDir(ResourceDir ? std::move(*ResourceDir) - : getStandardResourceDir()), + tooling::ArgumentsAdjuster Adjuster) + : Base(Base), ArgsAdjuster(std::move(Adjuster)), FallbackFlags(std::move(FallbackFlags)) { if (Base) BaseChanged = Base->watch([this](const std::vector<std::string> Changes) { @@ -286,7 +253,8 @@ OverlayCDB::getCompileCommand(PathRef File) const { Cmd = Base->getCompileCommand(File); if (!Cmd) return llvm::None; - adjustArguments(*Cmd, ResourceDir); + if (ArgsAdjuster) + Cmd->CommandLine = ArgsAdjuster(Cmd->CommandLine, Cmd->Filename); return Cmd; } @@ -296,7 +264,8 @@ tooling::CompileCommand OverlayCDB::getFallbackCommand(PathRef File) const { std::lock_guard<std::mutex> Lock(Mutex); Cmd.CommandLine.insert(Cmd.CommandLine.end(), FallbackFlags.begin(), FallbackFlags.end()); - adjustArguments(Cmd, ResourceDir); + if (ArgsAdjuster) + Cmd.CommandLine = ArgsAdjuster(Cmd.CommandLine, Cmd.Filename); return Cmd; } |

