Author: ioeric Date: Tue Oct 16 03:41:17 2018 New Revision: 344604 URL: http://llvm.org/viewvc/llvm-project?rev=344604&view=rev Log: [clangd] Allow disble down traversals from root.
Summary: This is useful for symbo scope proximity, where down traversals from the global scope if not desired. Reviewers: sammccall Subscribers: ilya-biryukov, MaskRay, jkorous, arphaman, kadircet, cfe-commits Differential Revision: https://reviews.llvm.org/D53317 Modified: clang-tools-extra/trunk/clangd/FileDistance.cpp clang-tools-extra/trunk/clangd/FileDistance.h clang-tools-extra/trunk/unittests/clangd/FileDistanceTests.cpp Modified: clang-tools-extra/trunk/clangd/FileDistance.cpp URL: http://llvm.org/viewvc/llvm-project/clang-tools-extra/trunk/clangd/FileDistance.cpp?rev=344604&r1=344603&r2=344604&view=diff ============================================================================== --- clang-tools-extra/trunk/clangd/FileDistance.cpp (original) +++ clang-tools-extra/trunk/clangd/FileDistance.cpp Tue Oct 16 03:41:17 2018 @@ -54,6 +54,7 @@ static SmallString<128> canonicalize(Str } constexpr const unsigned FileDistance::Unreachable; +const llvm::hash_code FileDistance::RootHash = hash_value(StringRef("/")); FileDistance::FileDistance(StringMap<SourceParams> Sources, const FileDistanceOptions &Opts) @@ -99,15 +100,18 @@ FileDistance::FileDistance(StringMap<Sou for (auto Child : DownEdges.lookup(hash_value(llvm::StringRef("")))) Next.push(Child); while (!Next.empty()) { - auto ParentCost = Cache.lookup(Next.front()); - for (auto Child : DownEdges.lookup(Next.front())) { - auto &ChildCost = - Cache.try_emplace(Child, Unreachable).first->getSecond(); - if (ParentCost + Opts.DownCost < ChildCost) - ChildCost = ParentCost + Opts.DownCost; + auto Parent = Next.front(); + Next.pop(); + auto ParentCost = Cache.lookup(Parent); + for (auto Child : DownEdges.lookup(Parent)) { + if (Parent != RootHash || Opts.AllowDownTraversalFromRoot) { + auto &ChildCost = + Cache.try_emplace(Child, Unreachable).first->getSecond(); + if (ParentCost + Opts.DownCost < ChildCost) + ChildCost = ParentCost + Opts.DownCost; + } Next.push(Child); } - Next.pop(); } } @@ -119,6 +123,11 @@ unsigned FileDistance::distance(StringRe for (StringRef Rest = Canonical; !Rest.empty(); Rest = parent_path(Rest, sys::path::Style::posix)) { auto Hash = hash_value(Rest); + if (Hash == RootHash && !Ancestors.empty() && + !Opts.AllowDownTraversalFromRoot) { + Cost = Unreachable; + break; + } auto It = Cache.find(Hash); if (It != Cache.end()) { Cost = It->second; Modified: clang-tools-extra/trunk/clangd/FileDistance.h URL: http://llvm.org/viewvc/llvm-project/clang-tools-extra/trunk/clangd/FileDistance.h?rev=344604&r1=344603&r2=344604&view=diff ============================================================================== --- clang-tools-extra/trunk/clangd/FileDistance.h (original) +++ clang-tools-extra/trunk/clangd/FileDistance.h Tue Oct 16 03:41:17 2018 @@ -56,6 +56,7 @@ struct FileDistanceOptions { unsigned UpCost = 2; // |foo/bar.h -> foo| unsigned DownCost = 1; // |foo -> foo/bar.h| unsigned IncludeCost = 2; // |foo.cc -> included_header.h| + bool AllowDownTraversalFromRoot = true; // | / -> /a | }; struct SourceParams { @@ -70,6 +71,7 @@ struct SourceParams { class FileDistance { public: static constexpr unsigned Unreachable = std::numeric_limits<unsigned>::max(); + static const llvm::hash_code RootHash; FileDistance(llvm::StringMap<SourceParams> Sources, const FileDistanceOptions &Opts = {}); Modified: clang-tools-extra/trunk/unittests/clangd/FileDistanceTests.cpp URL: http://llvm.org/viewvc/llvm-project/clang-tools-extra/trunk/unittests/clangd/FileDistanceTests.cpp?rev=344604&r1=344603&r2=344604&view=diff ============================================================================== --- clang-tools-extra/trunk/unittests/clangd/FileDistanceTests.cpp (original) +++ clang-tools-extra/trunk/unittests/clangd/FileDistanceTests.cpp Tue Oct 16 03:41:17 2018 @@ -95,6 +95,20 @@ TEST(FileDistance, LimitUpTraversals) { EXPECT_EQ(D.distance("/a/b/z"), 2u); } +TEST(FileDistance, DisallowDownTraversalsFromRoot) { + FileDistanceOptions Opts; + Opts.UpCost = Opts.DownCost = 1; + Opts.AllowDownTraversalFromRoot = false; + SourceParams CostLots; + CostLots.Cost = 100; + + FileDistance D({{"/", SourceParams()}, {"/a/b/c", CostLots}}, Opts); + EXPECT_EQ(D.distance("/"), 0u); + EXPECT_EQ(D.distance("/a"), 102u); + EXPECT_EQ(D.distance("/a/b"), 101u); + EXPECT_EQ(D.distance("/x"), FileDistance::Unreachable); +} + } // namespace } // namespace clangd } // namespace clang _______________________________________________ cfe-commits mailing list cfe-commits@lists.llvm.org http://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits