kadircet updated this revision to Diff 414324. kadircet added a comment. - Adjust after tests on windows
Repository: rG LLVM Github Monorepo CHANGES SINCE LAST ACTION https://reviews.llvm.org/D121286/new/ https://reviews.llvm.org/D121286 Files: clang-tools-extra/clangd/HeaderSourceSwitch.cpp clang-tools-extra/clangd/support/Path.cpp clang-tools-extra/clangd/support/Path.h clang-tools-extra/clangd/unittests/HeaderSourceSwitchTests.cpp clang-tools-extra/clangd/unittests/TestFS.cpp
Index: clang-tools-extra/clangd/unittests/TestFS.cpp =================================================================== --- clang-tools-extra/clangd/unittests/TestFS.cpp +++ clang-tools-extra/clangd/unittests/TestFS.cpp @@ -99,7 +99,7 @@ llvm::Expected<std::string> getAbsolutePath(llvm::StringRef /*Authority*/, llvm::StringRef Body, llvm::StringRef HintPath) const override { - if (!HintPath.empty() && !HintPath.startswith(testRoot())) + if (!HintPath.empty() && !pathStartsWith(testRoot(), HintPath)) return error("Hint path is not empty and doesn't start with {0}: {1}", testRoot(), HintPath); if (!Body.consume_front("/")) @@ -111,12 +111,11 @@ llvm::Expected<URI> uriFromAbsolutePath(llvm::StringRef AbsolutePath) const override { - llvm::StringRef Body = AbsolutePath; - if (!Body.consume_front(testRoot())) + if (!pathConsumeFront(AbsolutePath, testRoot())) return error("{0} does not start with {1}", AbsolutePath, testRoot()); return URI(Scheme, /*Authority=*/"", - llvm::sys::path::convert_to_slash(Body)); + llvm::sys::path::convert_to_slash(AbsolutePath)); } }; Index: clang-tools-extra/clangd/unittests/HeaderSourceSwitchTests.cpp =================================================================== --- clang-tools-extra/clangd/unittests/HeaderSourceSwitchTests.cpp +++ clang-tools-extra/clangd/unittests/HeaderSourceSwitchTests.cpp @@ -12,7 +12,9 @@ #include "TestFS.h" #include "TestTU.h" #include "index/MemIndex.h" +#include "support/Path.h" #include "llvm/ADT/None.h" +#include "llvm/ADT/StringExtras.h" #include "gmock/gmock.h" #include "gtest/gtest.h" @@ -271,6 +273,31 @@ *llvm::cantFail(runSwitchHeaderSource(Server, CppPath))); } +#ifdef _WIN32 +// Outside of windows systems, we usually have case sensitive file systems. +TEST(HeaderSourceSwitchTest, CaseSensitivity) { + TestTU TU = TestTU::withCode("void foo() {}"); + TU.HeaderCode = R"cpp( + inline void bar1() {} + inline void bar2() {} + void foo();)cpp"; + // Give main file and header different base names to make sure file system + // heuristics don't work. + TU.Filename = "Source.cpp"; + TU.HeaderFilename = "Header.h"; + + auto Index = TU.index(); + TU.Code = std::move(TU.HeaderCode); + TU.HeaderCode.clear(); + auto AST = TU.build(); + + auto HeaderAbsPath = testPath(TU.HeaderFilename); + HeaderAbsPath[0] = llvm::toLower(HeaderAbsPath[0]); + EXPECT_EQ(testPath(TU.Filename), + getCorrespondingHeaderOrSource(HeaderAbsPath, AST, Index.get())); +} +#endif + } // namespace } // namespace clangd } // namespace clang Index: clang-tools-extra/clangd/support/Path.h =================================================================== --- clang-tools-extra/clangd/support/Path.h +++ clang-tools-extra/clangd/support/Path.h @@ -44,6 +44,11 @@ /// Variant of parent_path that operates only on absolute paths. /// Unlike parent_path doesn't consider C: a parent of C:\. PathRef absoluteParent(PathRef Path); + +/// Tries to strip \p Prefix from beginning of \p Path. Returns true on success. +/// If \p Prefix doesn't match, leaves \p Path untouched and returns false. +bool pathConsumeFront(PathRef &Path, PathRef Prefix); + } // namespace clangd } // namespace clang Index: clang-tools-extra/clangd/support/Path.cpp =================================================================== --- clang-tools-extra/clangd/support/Path.cpp +++ clang-tools-extra/clangd/support/Path.cpp @@ -49,5 +49,12 @@ // afterwards. return Path.empty() || llvm::sys::path::is_separator(Path.front(), Style); } + +bool pathConsumeFront(PathRef &Path, PathRef Prefix) { + if (!pathStartsWith(Prefix, Path)) + return false; + Path = Path.drop_front(Prefix.size()); + return true; +} } // namespace clangd } // namespace clang Index: clang-tools-extra/clangd/HeaderSourceSwitch.cpp =================================================================== --- clang-tools-extra/clangd/HeaderSourceSwitch.cpp +++ clang-tools-extra/clangd/HeaderSourceSwitch.cpp @@ -11,6 +11,7 @@ #include "SourceCode.h" #include "index/SymbolCollector.h" #include "support/Logger.h" +#include "support/Path.h" #include "clang/AST/Decl.h" namespace clang { @@ -82,7 +83,7 @@ llvm::StringMap<int> Candidates; // Target path => score. auto AwardTarget = [&](const char *TargetURI) { if (auto TargetPath = URI::resolve(TargetURI, OriginalFile)) { - if (*TargetPath != OriginalFile) // exclude the original file. + if (!pathEqual(*TargetPath, OriginalFile)) // exclude the original file. ++Candidates[*TargetPath]; } else { elog("Failed to resolve URI {0}: {1}", TargetURI, TargetPath.takeError());
_______________________________________________ cfe-commits mailing list cfe-commits@lists.llvm.org https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits