kadircet created this revision. kadircet added a reviewer: sammccall. Herald added subscribers: usaxena95, arphaman. kadircet requested review of this revision. Herald added subscribers: cfe-commits, MaskRay, ilya-biryukov. Herald added a project: clang.
Will be used in other components that need ancestor traversal. Repository: rG LLVM Github Monorepo https://reviews.llvm.org/D96123 Files: clang-tools-extra/clangd/GlobalCompilationDatabase.cpp clang-tools-extra/clangd/support/Path.cpp clang-tools-extra/clangd/support/Path.h Index: clang-tools-extra/clangd/support/Path.h =================================================================== --- clang-tools-extra/clangd/support/Path.h +++ clang-tools-extra/clangd/support/Path.h @@ -28,6 +28,10 @@ std::string maybeCaseFoldPath(PathRef Path); bool pathEqual(PathRef, PathRef); +// 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, + llvm::function_ref<bool(PathRef)> Action); } // 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 @@ -7,8 +7,27 @@ //===----------------------------------------------------------------------===// #include "support/Path.h" +#include "llvm/Support/Path.h" + namespace clang { namespace clangd { +namespace { +// Variant of parent_path that operates only on absolute paths. +PathRef absoluteParent(PathRef Path) { + assert(llvm::sys::path::is_absolute(Path)); +#if defined(_WIN32) + // llvm::sys says "C:\" is absolute, and its parent is "C:" which is relative. + // This unhelpful behavior seems to have been inherited from boost. + if (llvm::sys::path::relative_path(Path).empty()) { + return PathRef(); + } +#endif + PathRef Result = llvm::sys::path::parent_path(Path); + assert(Result.empty() || llvm::sys::path::is_absolute(Result)); + return Result; +} + +} // namespace std::string maybeCaseFoldPath(PathRef Path) { #if defined(_WIN32) || defined(__APPLE__) @@ -26,5 +45,11 @@ #endif } +void actOnAllParentDirectories(PathRef FileName, + llvm::function_ref<bool(PathRef)> Action) { + for (auto Path = absoluteParent(FileName); !Path.empty() && !Action(Path); + Path = absoluteParent(Path)) + ; +} } // namespace clangd } // namespace clang Index: clang-tools-extra/clangd/GlobalCompilationDatabase.cpp =================================================================== --- clang-tools-extra/clangd/GlobalCompilationDatabase.cpp +++ clang-tools-extra/clangd/GlobalCompilationDatabase.cpp @@ -41,34 +41,6 @@ namespace clang { namespace clangd { -namespace { - -// Variant of parent_path that operates only on absolute paths. -PathRef absoluteParent(PathRef Path) { - assert(llvm::sys::path::is_absolute(Path)); -#if defined(_WIN32) - // llvm::sys says "C:\" is absolute, and its parent is "C:" which is relative. - // This unhelpful behavior seems to have been inherited from boost. - if (llvm::sys::path::relative_path(Path).empty()) { - return PathRef(); - } -#endif - PathRef Result = llvm::sys::path::parent_path(Path); - assert(Result.empty() || llvm::sys::path::is_absolute(Result)); - return Result; -} - -// 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, - llvm::function_ref<bool(PathRef)> Action) { - for (auto Path = absoluteParent(FileName); !Path.empty() && !Action(Path); - Path = absoluteParent(Path)) - ; -} - -} // namespace - tooling::CompileCommand GlobalCompilationDatabase::getFallbackCommand(PathRef File) const { std::vector<std::string> Argv = {"clang"};
Index: clang-tools-extra/clangd/support/Path.h =================================================================== --- clang-tools-extra/clangd/support/Path.h +++ clang-tools-extra/clangd/support/Path.h @@ -28,6 +28,10 @@ std::string maybeCaseFoldPath(PathRef Path); bool pathEqual(PathRef, PathRef); +// 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, + llvm::function_ref<bool(PathRef)> Action); } // 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 @@ -7,8 +7,27 @@ //===----------------------------------------------------------------------===// #include "support/Path.h" +#include "llvm/Support/Path.h" + namespace clang { namespace clangd { +namespace { +// Variant of parent_path that operates only on absolute paths. +PathRef absoluteParent(PathRef Path) { + assert(llvm::sys::path::is_absolute(Path)); +#if defined(_WIN32) + // llvm::sys says "C:\" is absolute, and its parent is "C:" which is relative. + // This unhelpful behavior seems to have been inherited from boost. + if (llvm::sys::path::relative_path(Path).empty()) { + return PathRef(); + } +#endif + PathRef Result = llvm::sys::path::parent_path(Path); + assert(Result.empty() || llvm::sys::path::is_absolute(Result)); + return Result; +} + +} // namespace std::string maybeCaseFoldPath(PathRef Path) { #if defined(_WIN32) || defined(__APPLE__) @@ -26,5 +45,11 @@ #endif } +void actOnAllParentDirectories(PathRef FileName, + llvm::function_ref<bool(PathRef)> Action) { + for (auto Path = absoluteParent(FileName); !Path.empty() && !Action(Path); + Path = absoluteParent(Path)) + ; +} } // namespace clangd } // namespace clang Index: clang-tools-extra/clangd/GlobalCompilationDatabase.cpp =================================================================== --- clang-tools-extra/clangd/GlobalCompilationDatabase.cpp +++ clang-tools-extra/clangd/GlobalCompilationDatabase.cpp @@ -41,34 +41,6 @@ namespace clang { namespace clangd { -namespace { - -// Variant of parent_path that operates only on absolute paths. -PathRef absoluteParent(PathRef Path) { - assert(llvm::sys::path::is_absolute(Path)); -#if defined(_WIN32) - // llvm::sys says "C:\" is absolute, and its parent is "C:" which is relative. - // This unhelpful behavior seems to have been inherited from boost. - if (llvm::sys::path::relative_path(Path).empty()) { - return PathRef(); - } -#endif - PathRef Result = llvm::sys::path::parent_path(Path); - assert(Result.empty() || llvm::sys::path::is_absolute(Result)); - return Result; -} - -// 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, - llvm::function_ref<bool(PathRef)> Action) { - for (auto Path = absoluteParent(FileName); !Path.empty() && !Action(Path); - Path = absoluteParent(Path)) - ; -} - -} // namespace - tooling::CompileCommand GlobalCompilationDatabase::getFallbackCommand(PathRef File) const { std::vector<std::string> Argv = {"clang"};
_______________________________________________ cfe-commits mailing list cfe-commits@lists.llvm.org https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits