diff --git a/clang-tools-extra/clangd/GlobalCompilationDatabase.h b/clang-tools-extra/clangd/GlobalCompilationDatabase.h --- a/clang-tools-extra/clangd/GlobalCompilationDatabase.h +++ b/clang-tools-extra/clangd/GlobalCompilationDatabase.h @@ -80,7 +80,8 @@ llvm::Optional getProjectInfo(PathRef File) const override; private: - std::pair + std::tuple getCDBInDirLocked(PathRef File) const; struct CDBLookupRequest { @@ -101,9 +102,11 @@ /// Caches compilation databases loaded from directories(keys are /// directories). struct CachedCDB { + std::string Path; // Not case-folded. std::unique_ptr CDB = nullptr; bool SentBroadcast = false; }; + // Keyed by possibly-case-folded path! mutable llvm::StringMap CompilationDatabases; /// Used for command argument pointing to folder where compile_commands.json diff --git a/clang-tools-extra/clangd/GlobalCompilationDatabase.cpp b/clang-tools-extra/clangd/GlobalCompilationDatabase.cpp --- a/clang-tools-extra/clangd/GlobalCompilationDatabase.cpp +++ b/clang-tools-extra/clangd/GlobalCompilationDatabase.cpp @@ -117,20 +117,48 @@ return None; } -std::pair +// For platforms where paths are case-insensitive (but case-preserving), +// we need to do case-insensitive comparisons and use lowercase keys. +// FIXME: Make Path a real class with desired semantics instead. +// This is not the only place this problem exists. +// FIXME: Mac filesystems default to case-insensitive, but may be sensitive. + +static std::string maybeCaseFoldPath(PathRef Path) { +#if defined(_WIN32) || defined(__APPLE__) + return Path.lower(); +#else + return Path; +#endif +} + +static bool pathEqual(PathRef A, PathRef B) { +#if defined(_WIN32) || defined(__APPLE__) + return A.equals_lower(B); +#else + return A == B; +#endif +} + +// If a CDB is returned, *CanonDir is it's original, non-case-folded directory. +std::tuple DirectoryBasedGlobalCompilationDatabase::getCDBInDirLocked(PathRef Dir) const { // FIXME(ibiryukov): Invalidate cached compilation databases on changes - auto CachedIt = CompilationDatabases.find(Dir); - if (CachedIt != CompilationDatabases.end()) - return {CachedIt->second.CDB.get(), CachedIt->second.SentBroadcast}; + auto Key = maybeCaseFoldPath(Dir); + auto CachedIt = CompilationDatabases.find(Key); + if (CachedIt != CompilationDatabases.end()) { + return {CachedIt->second.CDB.get(), CachedIt->second.SentBroadcast, + CachedIt->second.Path}; + } std::string Error = ""; CachedCDB Entry; Entry.CDB = tooling::CompilationDatabase::loadFromDirectory(Dir, Error); + Entry.Path = Dir; auto Result = Entry.CDB.get(); - CompilationDatabases[Dir] = std::move(Entry); + CompilationDatabases[Key] = std::move(Entry); - return {Result, false}; + return {Result, false, Dir}; } llvm::Optional @@ -145,17 +173,16 @@ { std::lock_guard Lock(Mutex); if (CompileCommandsDir) { - std::tie(Result.CDB, SentBroadcast) = + std::tie(Result.CDB, SentBroadcast, Result.PI.SourceRoot) = getCDBInDirLocked(*CompileCommandsDir); - Result.PI.SourceRoot = *CompileCommandsDir; } else { // Traverse the canonical version to prevent false positives. i.e.: // src/build/../a.cc can detect a CDB in /src/build if not canonicalized. actOnAllParentDirectories(removeDots(Request.FileName), [this, &SentBroadcast, &Result](PathRef Path) { - std::tie(Result.CDB, SentBroadcast) = + std::tie(Result.CDB, SentBroadcast, + Result.PI.SourceRoot) = getCDBInDirLocked(Path); - Result.PI.SourceRoot = Path; return Result.CDB != nullptr; }); } @@ -201,8 +228,8 @@ return true; auto Res = getCDBInDirLocked(Path); - It.first->second = Res.first != nullptr; - return Path == Result.PI.SourceRoot; + It.first->second = std::get<0>(Res) != nullptr; + return pathEqual(Path, Result.PI.SourceRoot); }); } } @@ -213,7 +240,7 @@ // Independent of whether it has an entry for that file or not. actOnAllParentDirectories(File, [&](PathRef Path) { if (DirectoryHasCDB.lookup(Path)) { - if (Path == Result.PI.SourceRoot) + if (pathEqual(Path, Result.PI.SourceRoot)) // Make sure listeners always get a canonical path for the file. GovernedFiles.push_back(removeDots(File)); // Stop as soon as we hit a CDB.