https://github.com/PeterChou1 updated https://github.com/llvm/llvm-project/pull/134298
>From fa35468f673ace035036a1c15d2d6ab756c2581e Mon Sep 17 00:00:00 2001 From: PeterChou1 <peter.c...@mail.utoronto.ca> Date: Thu, 3 Apr 2025 15:59:34 -0400 Subject: [PATCH 1/3] factor out file helpers --- clang-tools-extra/clang-doc/CMakeLists.txt | 2 + clang-tools-extra/clang-doc/HTMLGenerator.cpp | 18 +---- .../clang-doc/support/CMakeLists.txt | 9 +++ clang-tools-extra/clang-doc/support/File.cpp | 74 +++++++++++++++++++ clang-tools-extra/clang-doc/support/File.h | 26 +++++++ 5 files changed, 112 insertions(+), 17 deletions(-) create mode 100644 clang-tools-extra/clang-doc/support/CMakeLists.txt create mode 100644 clang-tools-extra/clang-doc/support/File.cpp create mode 100644 clang-tools-extra/clang-doc/support/File.h diff --git a/clang-tools-extra/clang-doc/CMakeLists.txt b/clang-tools-extra/clang-doc/CMakeLists.txt index 520fe58cbe68e..f4f62c74d6592 100644 --- a/clang-tools-extra/clang-doc/CMakeLists.txt +++ b/clang-tools-extra/clang-doc/CMakeLists.txt @@ -3,6 +3,7 @@ set(LLVM_LINK_COMPONENTS BitstreamReader FrontendOpenMP ) +add_subdirectory(support) add_clang_library(clangDoc STATIC BitcodeReader.cpp @@ -23,6 +24,7 @@ add_clang_library(clangDoc STATIC clang_target_link_libraries(clangDoc PRIVATE + clangDocSupport clangAnalysis clangAST clangASTMatchers diff --git a/clang-tools-extra/clang-doc/HTMLGenerator.cpp b/clang-tools-extra/clang-doc/HTMLGenerator.cpp index 18a0de826630c..49ccffa0d2269 100644 --- a/clang-tools-extra/clang-doc/HTMLGenerator.cpp +++ b/clang-tools-extra/clang-doc/HTMLGenerator.cpp @@ -8,6 +8,7 @@ #include "Generators.h" #include "Representation.h" +#include "support/File.h" #include "clang/Basic/Version.h" #include "llvm/ADT/StringExtras.h" #include "llvm/ADT/StringRef.h" @@ -1146,23 +1147,6 @@ static llvm::Error genIndex(const ClangDocContext &CDCtx) { return llvm::Error::success(); } -static llvm::Error copyFile(StringRef FilePath, StringRef OutDirectory) { - llvm::SmallString<128> PathWrite; - llvm::sys::path::native(OutDirectory, PathWrite); - llvm::sys::path::append(PathWrite, llvm::sys::path::filename(FilePath)); - llvm::SmallString<128> PathRead; - llvm::sys::path::native(FilePath, PathRead); - std::error_code OK; - std::error_code FileErr = llvm::sys::fs::copy_file(PathRead, PathWrite); - if (FileErr != OK) { - return llvm::createStringError(llvm::inconvertibleErrorCode(), - "error creating file " + - llvm::sys::path::filename(FilePath) + - ": " + FileErr.message() + "\n"); - } - return llvm::Error::success(); -} - llvm::Error HTMLGenerator::createResources(ClangDocContext &CDCtx) { auto Err = serializeIndex(CDCtx); if (Err) diff --git a/clang-tools-extra/clang-doc/support/CMakeLists.txt b/clang-tools-extra/clang-doc/support/CMakeLists.txt new file mode 100644 index 0000000000000..a4f7993d5c9d8 --- /dev/null +++ b/clang-tools-extra/clang-doc/support/CMakeLists.txt @@ -0,0 +1,9 @@ +# clang-doc/support contains support libraries that do not depend +# on clang either programmatically or conceptually. +set(LLVM_LINK_COMPONENTS + Support + ) + +add_clang_library(clangDocSupport STATIC + File.cpp + ) \ No newline at end of file diff --git a/clang-tools-extra/clang-doc/support/File.cpp b/clang-tools-extra/clang-doc/support/File.cpp new file mode 100644 index 0000000000000..a9162bee8cd70 --- /dev/null +++ b/clang-tools-extra/clang-doc/support/File.cpp @@ -0,0 +1,74 @@ +//===-- FileHelpersClangDoc.cpp - File Helpers -------------------*- C++-*-===// +// +// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. +// See https://llvm.org/LICENSE.txt for license information. +// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception +// +//===----------------------------------------------------------------------===// +#include "File.h" +#include "llvm/Support/FileSystem.h" +#include "llvm/Support/Path.h" + +namespace clang { +namespace doc { + +llvm::Error +copyFile(llvm::StringRef FilePath, llvm::StringRef OutDirectory) { + llvm::SmallString<128> PathWrite; + llvm::sys::path::native(OutDirectory, PathWrite); + llvm::sys::path::append(PathWrite, llvm::sys::path::filename(FilePath)); + llvm::SmallString<128> PathRead; + llvm::sys::path::native(FilePath, PathRead); + std::error_code FileErr = llvm::sys::fs::copy_file(PathRead, PathWrite); + if (FileErr) { + return llvm::createStringError(llvm::inconvertibleErrorCode(), + "error creating file " + + llvm::sys::path::filename(FilePath) + + ": " + FileErr.message() + "\n"); + } + return llvm::Error::success(); +} + + +llvm::SmallString<128> computeRelativePath(llvm::StringRef Destination, + llvm::StringRef Origin) { + // If Origin is empty, the relative path to the Destination is its complete + // path. + if (Origin.empty()) + return Destination; + + // The relative path is an empty path if both directories are the same. + if (Destination == Origin) + return {}; + + // These iterators iterate through each of their parent directories + llvm::sys::path::const_iterator FileI = llvm::sys::path::begin(Destination); + llvm::sys::path::const_iterator FileE = llvm::sys::path::end(Destination); + llvm::sys::path::const_iterator DirI = llvm::sys::path::begin(Origin); + llvm::sys::path::const_iterator DirE = llvm::sys::path::end(Origin); + // Advance both iterators until the paths differ. Example: + // Destination = A/B/C/D + // Origin = A/B/E/F + // FileI will point to C and DirI to E. The directories behind them is the + // directory they share (A/B). + while (FileI != FileE && DirI != DirE && *FileI == *DirI) { + ++FileI; + ++DirI; + } + llvm::SmallString<128> Result; // This will hold the resulting path. + // Result has to go up one directory for each of the remaining directories in + // Origin + while (DirI != DirE) { + llvm::sys::path::append(Result, ".."); + ++DirI; + } + // Result has to append each of the remaining directories in Destination + while (FileI != FileE) { + llvm::sys::path::append(Result, *FileI); + ++FileI; + } + return Result; +} + +} // namespace doc +} // namespace clang \ No newline at end of file diff --git a/clang-tools-extra/clang-doc/support/File.h b/clang-tools-extra/clang-doc/support/File.h new file mode 100644 index 0000000000000..fc52d185a5093 --- /dev/null +++ b/clang-tools-extra/clang-doc/support/File.h @@ -0,0 +1,26 @@ +//===-- File.h --- File Helpers -------------------*- C++-*-===// +// +// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. +// See https://llvm.org/LICENSE.txt for license information. +// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception +// +//===----------------------------------------------------------------------===// +#ifndef LLVM_CLANG_TOOLS_EXTRA_CLANG_DOC_FILEHELPER_H +#define LLVM_CLANG_TOOLS_EXTRA_CLANG_DOC_FILEHELPER_H + +#include "llvm/ADT/StringExtras.h" +#include "llvm/Support/Error.h" + +namespace clang { +namespace doc { + +llvm::Error +copyFile(llvm::StringRef FilePath, llvm::StringRef OutDirectory); + +llvm::SmallString<128> +computeRelativePath(llvm::StringRef Destination,llvm::StringRef Origin); + +} // namespace doc +} // namespace clang + +#endif // LLVM_CLANG_TOOLS_EXTRA_CLANG_DOC_FILEHELPER_H \ No newline at end of file >From 870f17bbdd9badc02e7d81b26be3c677f8ca1a0f Mon Sep 17 00:00:00 2001 From: PeterChou1 <peter.c...@mail.utoronto.ca> Date: Thu, 3 Apr 2025 16:01:10 -0400 Subject: [PATCH 2/3] remove redundant code --- clang-tools-extra/clang-doc/HTMLGenerator.cpp | 41 ------------------- 1 file changed, 41 deletions(-) diff --git a/clang-tools-extra/clang-doc/HTMLGenerator.cpp b/clang-tools-extra/clang-doc/HTMLGenerator.cpp index 49ccffa0d2269..edcaf27094661 100644 --- a/clang-tools-extra/clang-doc/HTMLGenerator.cpp +++ b/clang-tools-extra/clang-doc/HTMLGenerator.cpp @@ -252,47 +252,6 @@ static void appendVector(std::vector<Derived> &&New, std::move(New.begin(), New.end(), std::back_inserter(Original)); } -// Compute the relative path from an Origin directory to a Destination directory -static SmallString<128> computeRelativePath(StringRef Destination, - StringRef Origin) { - // If Origin is empty, the relative path to the Destination is its complete - // path. - if (Origin.empty()) - return Destination; - - // The relative path is an empty path if both directories are the same. - if (Destination == Origin) - return {}; - - // These iterators iterate through each of their parent directories - llvm::sys::path::const_iterator FileI = llvm::sys::path::begin(Destination); - llvm::sys::path::const_iterator FileE = llvm::sys::path::end(Destination); - llvm::sys::path::const_iterator DirI = llvm::sys::path::begin(Origin); - llvm::sys::path::const_iterator DirE = llvm::sys::path::end(Origin); - // Advance both iterators until the paths differ. Example: - // Destination = A/B/C/D - // Origin = A/B/E/F - // FileI will point to C and DirI to E. The directories behind them is the - // directory they share (A/B). - while (FileI != FileE && DirI != DirE && *FileI == *DirI) { - ++FileI; - ++DirI; - } - SmallString<128> Result; // This will hold the resulting path. - // Result has to go up one directory for each of the remaining directories in - // Origin - while (DirI != DirE) { - llvm::sys::path::append(Result, ".."); - ++DirI; - } - // Result has to append each of the remaining directories in Destination - while (FileI != FileE) { - llvm::sys::path::append(Result, *FileI); - ++FileI; - } - return Result; -} - // HTML generation static std::vector<std::unique_ptr<TagNode>> >From 0c597103df166ca39c9bcc7cbb128f1958e4ca70 Mon Sep 17 00:00:00 2001 From: PeterChou1 <peter.c...@mail.utoronto.ca> Date: Thu, 3 Apr 2025 16:07:54 -0400 Subject: [PATCH 3/3] clang-format --- clang-tools-extra/clang-doc/support/File.cpp | 9 ++++----- 1 file changed, 4 insertions(+), 5 deletions(-) diff --git a/clang-tools-extra/clang-doc/support/File.cpp b/clang-tools-extra/clang-doc/support/File.cpp index a9162bee8cd70..b736b464a0e57 100644 --- a/clang-tools-extra/clang-doc/support/File.cpp +++ b/clang-tools-extra/clang-doc/support/File.cpp @@ -12,8 +12,7 @@ namespace clang { namespace doc { -llvm::Error -copyFile(llvm::StringRef FilePath, llvm::StringRef OutDirectory) { +llvm::Error copyFile(llvm::StringRef FilePath, llvm::StringRef OutDirectory) { llvm::SmallString<128> PathWrite; llvm::sys::path::native(OutDirectory, PathWrite); llvm::sys::path::append(PathWrite, llvm::sys::path::filename(FilePath)); @@ -30,13 +29,13 @@ copyFile(llvm::StringRef FilePath, llvm::StringRef OutDirectory) { } -llvm::SmallString<128> computeRelativePath(llvm::StringRef Destination, - llvm::StringRef Origin) { +llvm::SmallString<128> +computeRelativePath(llvm::StringRef Destination, llvm::StringRef Origin) { // If Origin is empty, the relative path to the Destination is its complete // path. if (Origin.empty()) return Destination; - + // The relative path is an empty path if both directories are the same. if (Destination == Origin) return {}; _______________________________________________ cfe-commits mailing list cfe-commits@lists.llvm.org https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits