https://github.com/PeterChou1 updated https://github.com/llvm/llvm-project/pull/93276
>From 0b6d536133f63e078fbde491a8c92c7ec916cb47 Mon Sep 17 00:00:00 2001 From: PeterChou1 <peter.c...@mail.utoronto.ca> Date: Fri, 24 May 2024 05:18:05 -0400 Subject: [PATCH 1/5] [clang-doc] make loading of json side bar async --- clang-tools-extra/clang-doc/HTMLGenerator.cpp | 28 +++++++++---------- clang-tools-extra/clang-doc/Representation.h | 1 + clang-tools-extra/clang-doc/assets/index.js | 5 ++-- .../clang-doc/tool/ClangDocMain.cpp | 6 +++- 4 files changed, 23 insertions(+), 17 deletions(-) diff --git a/clang-tools-extra/clang-doc/HTMLGenerator.cpp b/clang-tools-extra/clang-doc/HTMLGenerator.cpp index c0faf5f7e8fd9..fb1a7f94c9094 100644 --- a/clang-tools-extra/clang-doc/HTMLGenerator.cpp +++ b/clang-tools-extra/clang-doc/HTMLGenerator.cpp @@ -964,7 +964,7 @@ static llvm::Error SerializeIndex(ClangDocContext &CDCtx) { std::error_code FileErr; llvm::SmallString<128> FilePath; llvm::sys::path::native(CDCtx.OutDirectory, FilePath); - llvm::sys::path::append(FilePath, "index_json.js"); + llvm::sys::path::append(FilePath, "index.json"); llvm::raw_fd_ostream OS(FilePath, FileErr, llvm::sys::fs::OF_None); if (FileErr != OK) { return llvm::createStringError(llvm::inconvertibleErrorCode(), @@ -985,9 +985,7 @@ static llvm::Error SerializeIndex(ClangDocContext &CDCtx) { }); }); }; - OS << "var JsonIndex = `\n"; IndexToJSON(CDCtx.Idx); - OS << "`;\n"; return llvm::Error::success(); } @@ -1049,31 +1047,33 @@ static llvm::Error CopyFile(StringRef FilePath, StringRef OutDirectory) { 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"); + "error creating file " + FilePath + ": " + + FileErr.message() + "\n"); } return llvm::Error::success(); } llvm::Error HTMLGenerator::createResources(ClangDocContext &CDCtx) { - auto Err = SerializeIndex(CDCtx); - if (Err) + if (auto Err = SerializeIndex(CDCtx)) { return Err; - Err = GenIndex(CDCtx); - if (Err) + } + + if (auto Err = GenIndex(CDCtx)) { return Err; + } for (const auto &FilePath : CDCtx.UserStylesheets) { - Err = CopyFile(FilePath, CDCtx.OutDirectory); - if (Err) + if (auto Err = CopyFile(FilePath, CDCtx.OutDirectory)) { return Err; + } } + for (const auto &FilePath : CDCtx.FilesToCopy) { - Err = CopyFile(FilePath, CDCtx.OutDirectory); - if (Err) + if (auto Err = CopyFile(FilePath, CDCtx.OutDirectory)) { return Err; + } } + return llvm::Error::success(); } diff --git a/clang-tools-extra/clang-doc/Representation.h b/clang-tools-extra/clang-doc/Representation.h index a6b144eb7fa2a..23323f1cbdf46 100644 --- a/clang-tools-extra/clang-doc/Representation.h +++ b/clang-tools-extra/clang-doc/Representation.h @@ -491,6 +491,7 @@ struct ClangDocContext { std::string SourceRoot; // Directory where processed files are stored. Links // to definition locations will only be generated if // the file is in this dir. + // URL of repository that hosts code used for links to definition locations. std::optional<std::string> RepositoryUrl; // Path of CSS stylesheets that will be copied to OutDirectory and used to diff --git a/clang-tools-extra/clang-doc/assets/index.js b/clang-tools-extra/clang-doc/assets/index.js index a5ac90f2e06e7..379867268527e 100644 --- a/clang-tools-extra/clang-doc/assets/index.js +++ b/clang-tools-extra/clang-doc/assets/index.js @@ -82,6 +82,7 @@ function createIndex(Index) { document.addEventListener("DOMContentLoaded", function() { // JsonIndex is a variable from another file that contains the index // in JSON format - var Index = JSON.parse(JsonIndex); - createIndex(Index); + fetch("/index.json") + .then((response) => response.json()) + .then((Index) => { createIndex(Index); }); }); diff --git a/clang-tools-extra/clang-doc/tool/ClangDocMain.cpp b/clang-tools-extra/clang-doc/tool/ClangDocMain.cpp index 21b581fa6df2e..53108a77dab21 100644 --- a/clang-tools-extra/clang-doc/tool/ClangDocMain.cpp +++ b/clang-tools-extra/clang-doc/tool/ClangDocMain.cpp @@ -155,6 +155,10 @@ Example usage for a project using a compile commands database: return 1; } + // add option to customize url fragment + // such as + // https://github.com/llvm/llvm-project/blob/main/clang-tools-extra/clang-doc/ClangDocMain.cpp#L1 + // Fail early if an invalid format was provided. std::string Format = getFormatString(); llvm::outs() << "Emiting docs in " << Format << " format.\n"; @@ -179,7 +183,7 @@ Example usage for a project using a compile commands database: SourceRoot, RepositoryUrl, {UserStylesheets.begin(), UserStylesheets.end()}, - {"index.js", "index_json.js"}}; + {"index.js"}}; if (Format == "html") { void *MainAddr = (void *)(intptr_t)GetExecutablePath; >From 26dc42ff5f6f4fd4d2d0bea3dae785f70f835803 Mon Sep 17 00:00:00 2001 From: PeterChou1 <peter.c...@mail.utoronto.ca> Date: Thu, 30 May 2024 21:55:27 -0400 Subject: [PATCH 2/5] [clang-doc] add e2e unit test --- .../Inputs/clang-doc-project1/CMakeLists.txt | 14 ++++++ .../Inputs/clang-doc-project1/Calculator.cpp | 21 +++++++++ .../Inputs/clang-doc-project1/Calculator.h | 46 +++++++++++++++++++ .../Inputs/clang-doc-project1/Circle.cpp | 11 +++++ .../Inputs/clang-doc-project1/Circle.h | 35 ++++++++++++++ .../Inputs/clang-doc-project1/Rectangle.cpp | 12 +++++ .../Inputs/clang-doc-project1/Rectangle.h | 37 +++++++++++++++ .../Inputs/clang-doc-project1/Shape.h | 30 ++++++++++++ .../clang-doc-project1/database_template.json | 22 +++++++++ .../Inputs/clang-doc-project1/main.cpp | 25 ++++++++++ .../test/clang-doc/clang-doc-project1.cpp | 11 +++++ 11 files changed, 264 insertions(+) create mode 100644 clang-tools-extra/test/clang-doc/Inputs/clang-doc-project1/CMakeLists.txt create mode 100644 clang-tools-extra/test/clang-doc/Inputs/clang-doc-project1/Calculator.cpp create mode 100644 clang-tools-extra/test/clang-doc/Inputs/clang-doc-project1/Calculator.h create mode 100644 clang-tools-extra/test/clang-doc/Inputs/clang-doc-project1/Circle.cpp create mode 100644 clang-tools-extra/test/clang-doc/Inputs/clang-doc-project1/Circle.h create mode 100644 clang-tools-extra/test/clang-doc/Inputs/clang-doc-project1/Rectangle.cpp create mode 100644 clang-tools-extra/test/clang-doc/Inputs/clang-doc-project1/Rectangle.h create mode 100644 clang-tools-extra/test/clang-doc/Inputs/clang-doc-project1/Shape.h create mode 100644 clang-tools-extra/test/clang-doc/Inputs/clang-doc-project1/database_template.json create mode 100644 clang-tools-extra/test/clang-doc/Inputs/clang-doc-project1/main.cpp create mode 100644 clang-tools-extra/test/clang-doc/clang-doc-project1.cpp diff --git a/clang-tools-extra/test/clang-doc/Inputs/clang-doc-project1/CMakeLists.txt b/clang-tools-extra/test/clang-doc/Inputs/clang-doc-project1/CMakeLists.txt new file mode 100644 index 0000000000000..a865714e04b17 --- /dev/null +++ b/clang-tools-extra/test/clang-doc/Inputs/clang-doc-project1/CMakeLists.txt @@ -0,0 +1,14 @@ +project(clang_doc_project1) + +set(CMAKE_CXX_STANDARD 17) + +# Add the executable +add_executable(ProjectClangDoc1 + src/main.cpp + src/Calculator.cpp + src/Circle.cpp + src/Rectangle.cpp +) + +# Include directories +target_include_directories(ProjectClangDoc1 PUBLIC include) diff --git a/clang-tools-extra/test/clang-doc/Inputs/clang-doc-project1/Calculator.cpp b/clang-tools-extra/test/clang-doc/Inputs/clang-doc-project1/Calculator.cpp new file mode 100644 index 0000000000000..df1778c3b9d55 --- /dev/null +++ b/clang-tools-extra/test/clang-doc/Inputs/clang-doc-project1/Calculator.cpp @@ -0,0 +1,21 @@ +#include "Calculator.h" +#include <stdexcept> + +int Calculator::add(int a, int b) { + return a + b; +} + +int Calculator::subtract(int a, int b) { + return a - b; +} + +int Calculator::multiply(int a, int b) { + return a * b; +} + +double Calculator::divide(int a, int b) { + if (b == 0) { + throw std::invalid_argument("Division by zero"); + } + return static_cast<double>(a) / b; +} diff --git a/clang-tools-extra/test/clang-doc/Inputs/clang-doc-project1/Calculator.h b/clang-tools-extra/test/clang-doc/Inputs/clang-doc-project1/Calculator.h new file mode 100644 index 0000000000000..6811834bc0159 --- /dev/null +++ b/clang-tools-extra/test/clang-doc/Inputs/clang-doc-project1/Calculator.h @@ -0,0 +1,46 @@ +#pragma once + +/** + * @brief A simple calculator class. + * + * Provides basic arithmetic operations. + */ +class Calculator { +public: + /** + * @brief Adds two integers. + * + * @param a First integer. + * @param b Second integer. + * @return int The sum of a and b. + */ + int add(int a, int b); + + /** + * @brief Subtracts the second integer from the first. + * + * @param a First integer. + * @param b Second integer. + * @return int The result of a - b. + */ + int subtract(int a, int b); + + /** + * @brief Multiplies two integers. + * + * @param a First integer. + * @param b Second integer. + * @return int The product of a and b. + */ + int multiply(int a, int b); + + /** + * @brief Divides the first integer by the second. + * + * @param a First integer. + * @param b Second integer. + * @return double The result of a / b. + * @throw std::invalid_argument if b is zero. + */ + double divide(int a, int b); +}; \ No newline at end of file diff --git a/clang-tools-extra/test/clang-doc/Inputs/clang-doc-project1/Circle.cpp b/clang-tools-extra/test/clang-doc/Inputs/clang-doc-project1/Circle.cpp new file mode 100644 index 0000000000000..823384a4d97e8 --- /dev/null +++ b/clang-tools-extra/test/clang-doc/Inputs/clang-doc-project1/Circle.cpp @@ -0,0 +1,11 @@ +#include "Circle.h" + +Circle::Circle(double radius) : radius_(radius) {} + +double Circle::area() const { + return 3.141 * radius_ * radius_; +} + +double Circle::perimeter() const { + return 3.141 * radius_; +} \ No newline at end of file diff --git a/clang-tools-extra/test/clang-doc/Inputs/clang-doc-project1/Circle.h b/clang-tools-extra/test/clang-doc/Inputs/clang-doc-project1/Circle.h new file mode 100644 index 0000000000000..7bee3ffa92539 --- /dev/null +++ b/clang-tools-extra/test/clang-doc/Inputs/clang-doc-project1/Circle.h @@ -0,0 +1,35 @@ +#pragma once + +#include "Shape.h" + +/** + * @brief Circle class derived from Shape. + * + * Represents a circle with a given radius. + */ +class Circle : public Shape { +public: + /** + * @brief Constructs a new Circle object. + * + * @param radius Radius of the circle. + */ + Circle(double radius); + + /** + * @brief Calculates the area of the circle. + * + * @return double The area of the circle. + */ + double area() const override; + + /** + * @brief Calculates the perimeter of the circle. + * + * @return double The perimeter of the circle. + */ + double perimeter() const override; + +private: + double radius_; ///< Radius of the circle. +}; diff --git a/clang-tools-extra/test/clang-doc/Inputs/clang-doc-project1/Rectangle.cpp b/clang-tools-extra/test/clang-doc/Inputs/clang-doc-project1/Rectangle.cpp new file mode 100644 index 0000000000000..7ffc769157ebc --- /dev/null +++ b/clang-tools-extra/test/clang-doc/Inputs/clang-doc-project1/Rectangle.cpp @@ -0,0 +1,12 @@ +#include "Rectangle.h" + +Rectangle::Rectangle(double width, double height) + : width_(width), height_(height) {} + +double Rectangle::area() const { + return width_ * height_; +} + +double Rectangle::perimeter() const { + return 2 * (width_ + height_); +} \ No newline at end of file diff --git a/clang-tools-extra/test/clang-doc/Inputs/clang-doc-project1/Rectangle.h b/clang-tools-extra/test/clang-doc/Inputs/clang-doc-project1/Rectangle.h new file mode 100644 index 0000000000000..8c6223a4f6180 --- /dev/null +++ b/clang-tools-extra/test/clang-doc/Inputs/clang-doc-project1/Rectangle.h @@ -0,0 +1,37 @@ +#pragma once + +#include "Shape.h" + +/** + * @brief Rectangle class derived from Shape. + * + * Represents a rectangle with a given width and height. + */ +class Rectangle : public Shape { +public: + /** + * @brief Constructs a new Rectangle object. + * + * @param width Width of the rectangle. + * @param height Height of the rectangle. + */ + Rectangle(double width, double height); + + /** + * @brief Calculates the area of the rectangle. + * + * @return double The area of the rectangle. + */ + double area() const override; + + /** + * @brief Calculates the perimeter of the rectangle. + * + * @return double The perimeter of the rectangle. + */ + double perimeter() const override; + +private: + double width_; ///< Width of the rectangle. + double height_; ///< Height of the rectangle. +}; \ No newline at end of file diff --git a/clang-tools-extra/test/clang-doc/Inputs/clang-doc-project1/Shape.h b/clang-tools-extra/test/clang-doc/Inputs/clang-doc-project1/Shape.h new file mode 100644 index 0000000000000..e5c5d4c9e4412 --- /dev/null +++ b/clang-tools-extra/test/clang-doc/Inputs/clang-doc-project1/Shape.h @@ -0,0 +1,30 @@ +#pragma once + +/** + * @brief Abstract base class for shapes. + * + * Provides a common interface for different types of shapes. + */ +class Shape { +public: + /** + * @brief Virtual destructor. + */ + virtual ~Shape() {} + + /** + * @brief Calculates the area of the shape. + * + * @return double The area of the shape. + */ + virtual double area() const = 0; + + /** + * @brief Calculates the perimeter of the shape. + * + * @return double The perimeter of the shape. + */ + virtual double perimeter() const = 0; +}; + + diff --git a/clang-tools-extra/test/clang-doc/Inputs/clang-doc-project1/database_template.json b/clang-tools-extra/test/clang-doc/Inputs/clang-doc-project1/database_template.json new file mode 100644 index 0000000000000..0549c5b718f08 --- /dev/null +++ b/clang-tools-extra/test/clang-doc/Inputs/clang-doc-project1/database_template.json @@ -0,0 +1,22 @@ +[ +{ + "directory": "$test_dir/build", + "command": "clang++ -o main.o -I../include $test_dir/src/main.cpp", + "file": "$test_dir/src/main.cpp" +}, +{ + "directory": "$test_dir/build", + "command": "clang++ -o Calculator.o -I../include $test_dir/src/Calculator.cpp", + "file": "$test_dir/src/Calculator.cpp" +}, +{ + "directory": "$test_dir/build", + "command": "clang++ -o Circle.o -I../include $test_dir/src/Circle.cpp", + "file": "$test_dir/src/Circle.cpp" +}, +{ + "directory": "$test_dir/build", + "command": "clang++ -o Rectangle.o -I../include $test_dir/src/Rectangle.cpp", + "file": "$test_dir/src/Rectangle.cpp" +} +] \ No newline at end of file diff --git a/clang-tools-extra/test/clang-doc/Inputs/clang-doc-project1/main.cpp b/clang-tools-extra/test/clang-doc/Inputs/clang-doc-project1/main.cpp new file mode 100644 index 0000000000000..e20732c7de371 --- /dev/null +++ b/clang-tools-extra/test/clang-doc/Inputs/clang-doc-project1/main.cpp @@ -0,0 +1,25 @@ +#include <iostream> +#include "Calculator.h" +#include "Circle.h" +#include "Rectangle.h" + +int main() { + // Calculator + Calculator calc; + std::cout << "Add: " << calc.add(3, 4) << std::endl; + std::cout << "Subtract: " << calc.subtract(10, 5) << std::endl; + std::cout << "Multiply: " << calc.multiply(2, 3) << std::endl; + std::cout << "Divide: " << calc.divide(10, 2) << std::endl; + + // Circle + Circle circle(5.0); + std::cout << "Circle Area: " << circle.area() << std::endl; + std::cout << "Circle Perimeter: " << circle.perimeter() << std::endl; + + // Rectangle + Rectangle rectangle(4.0, 6.0); + std::cout << "Rectangle Area: " << rectangle.area() << std::endl; + std::cout << "Rectangle Perimeter: " << rectangle.perimeter() << std::endl; + + return 0; +} \ No newline at end of file diff --git a/clang-tools-extra/test/clang-doc/clang-doc-project1.cpp b/clang-tools-extra/test/clang-doc/clang-doc-project1.cpp new file mode 100644 index 0000000000000..8c0289519854d --- /dev/null +++ b/clang-tools-extra/test/clang-doc/clang-doc-project1.cpp @@ -0,0 +1,11 @@ +// RUN: mkdir -p %T/clang-doc/build +// RUN: mkdir -p %T/clang-doc/include +// RUN: mkdir -p %T/clang-doc/src +// RUN: mkdir -p %T/clang-doc/docs +// RUN: sed 's|$test_dir|%/T/clang-doc|g' %S/Inputs/clang-doc-project1/database_template.json > %T/clang-doc/build/compile_commands.json +// RUN: cp %S/Inputs/clang-doc-project1/*.h %T/clang-doc/include +// RUN: cp %S/Inputs/clang-doc-project1/*.cpp %T/clang-doc/src +// RUN: cd %T/clang-doc/build +// RUN: clang-doc --format=html --executor=all-TUs --output=%T/clang-doc/docs ./compile_commands.json + + >From 032ab7615ecab7bc92ae85587d3632778f04fa64 Mon Sep 17 00:00:00 2001 From: PeterChou1 <peter.c...@mail.utoronto.ca> Date: Fri, 28 Jun 2024 19:36:00 -0400 Subject: [PATCH 3/5] [clang-doc] delete redundant files --- clang-tools-extra/clang-doc/HTMLGenerator.cpp | 2 +- .../Inputs/clang-doc-project1/CMakeLists.txt | 14 ------ .../Inputs/clang-doc-project1/Calculator.cpp | 21 --------- .../Inputs/clang-doc-project1/Calculator.h | 46 ------------------- .../Inputs/clang-doc-project1/Circle.cpp | 11 ----- .../Inputs/clang-doc-project1/Circle.h | 35 -------------- .../Inputs/clang-doc-project1/Rectangle.cpp | 12 ----- .../Inputs/clang-doc-project1/Rectangle.h | 37 --------------- .../Inputs/clang-doc-project1/Shape.h | 30 ------------ .../clang-doc-project1/database_template.json | 22 --------- .../Inputs/clang-doc-project1/main.cpp | 25 ---------- .../test/clang-doc/clang-doc-project1.cpp | 11 ----- 12 files changed, 1 insertion(+), 265 deletions(-) delete mode 100644 clang-tools-extra/test/clang-doc/Inputs/clang-doc-project1/CMakeLists.txt delete mode 100644 clang-tools-extra/test/clang-doc/Inputs/clang-doc-project1/Calculator.cpp delete mode 100644 clang-tools-extra/test/clang-doc/Inputs/clang-doc-project1/Calculator.h delete mode 100644 clang-tools-extra/test/clang-doc/Inputs/clang-doc-project1/Circle.cpp delete mode 100644 clang-tools-extra/test/clang-doc/Inputs/clang-doc-project1/Circle.h delete mode 100644 clang-tools-extra/test/clang-doc/Inputs/clang-doc-project1/Rectangle.cpp delete mode 100644 clang-tools-extra/test/clang-doc/Inputs/clang-doc-project1/Rectangle.h delete mode 100644 clang-tools-extra/test/clang-doc/Inputs/clang-doc-project1/Shape.h delete mode 100644 clang-tools-extra/test/clang-doc/Inputs/clang-doc-project1/database_template.json delete mode 100644 clang-tools-extra/test/clang-doc/Inputs/clang-doc-project1/main.cpp delete mode 100644 clang-tools-extra/test/clang-doc/clang-doc-project1.cpp diff --git a/clang-tools-extra/clang-doc/HTMLGenerator.cpp b/clang-tools-extra/clang-doc/HTMLGenerator.cpp index 4c72b329507d3..c82527ecdf5b4 100644 --- a/clang-tools-extra/clang-doc/HTMLGenerator.cpp +++ b/clang-tools-extra/clang-doc/HTMLGenerator.cpp @@ -972,7 +972,7 @@ static llvm::Error serializeIndex(ClangDocContext &CDCtx) { std::error_code FileErr; llvm::SmallString<128> FilePath; llvm::sys::path::native(CDCtx.OutDirectory, FilePath); - llvm::sys::path::append(FilePath, "index_json.js"); + llvm::sys::path::append(FilePath, "index.json"); llvm::raw_fd_ostream OS(FilePath, FileErr, llvm::sys::fs::OF_None); if (FileErr != OK) { return llvm::createStringError(llvm::inconvertibleErrorCode(), diff --git a/clang-tools-extra/test/clang-doc/Inputs/clang-doc-project1/CMakeLists.txt b/clang-tools-extra/test/clang-doc/Inputs/clang-doc-project1/CMakeLists.txt deleted file mode 100644 index a865714e04b17..0000000000000 --- a/clang-tools-extra/test/clang-doc/Inputs/clang-doc-project1/CMakeLists.txt +++ /dev/null @@ -1,14 +0,0 @@ -project(clang_doc_project1) - -set(CMAKE_CXX_STANDARD 17) - -# Add the executable -add_executable(ProjectClangDoc1 - src/main.cpp - src/Calculator.cpp - src/Circle.cpp - src/Rectangle.cpp -) - -# Include directories -target_include_directories(ProjectClangDoc1 PUBLIC include) diff --git a/clang-tools-extra/test/clang-doc/Inputs/clang-doc-project1/Calculator.cpp b/clang-tools-extra/test/clang-doc/Inputs/clang-doc-project1/Calculator.cpp deleted file mode 100644 index df1778c3b9d55..0000000000000 --- a/clang-tools-extra/test/clang-doc/Inputs/clang-doc-project1/Calculator.cpp +++ /dev/null @@ -1,21 +0,0 @@ -#include "Calculator.h" -#include <stdexcept> - -int Calculator::add(int a, int b) { - return a + b; -} - -int Calculator::subtract(int a, int b) { - return a - b; -} - -int Calculator::multiply(int a, int b) { - return a * b; -} - -double Calculator::divide(int a, int b) { - if (b == 0) { - throw std::invalid_argument("Division by zero"); - } - return static_cast<double>(a) / b; -} diff --git a/clang-tools-extra/test/clang-doc/Inputs/clang-doc-project1/Calculator.h b/clang-tools-extra/test/clang-doc/Inputs/clang-doc-project1/Calculator.h deleted file mode 100644 index 6811834bc0159..0000000000000 --- a/clang-tools-extra/test/clang-doc/Inputs/clang-doc-project1/Calculator.h +++ /dev/null @@ -1,46 +0,0 @@ -#pragma once - -/** - * @brief A simple calculator class. - * - * Provides basic arithmetic operations. - */ -class Calculator { -public: - /** - * @brief Adds two integers. - * - * @param a First integer. - * @param b Second integer. - * @return int The sum of a and b. - */ - int add(int a, int b); - - /** - * @brief Subtracts the second integer from the first. - * - * @param a First integer. - * @param b Second integer. - * @return int The result of a - b. - */ - int subtract(int a, int b); - - /** - * @brief Multiplies two integers. - * - * @param a First integer. - * @param b Second integer. - * @return int The product of a and b. - */ - int multiply(int a, int b); - - /** - * @brief Divides the first integer by the second. - * - * @param a First integer. - * @param b Second integer. - * @return double The result of a / b. - * @throw std::invalid_argument if b is zero. - */ - double divide(int a, int b); -}; \ No newline at end of file diff --git a/clang-tools-extra/test/clang-doc/Inputs/clang-doc-project1/Circle.cpp b/clang-tools-extra/test/clang-doc/Inputs/clang-doc-project1/Circle.cpp deleted file mode 100644 index 823384a4d97e8..0000000000000 --- a/clang-tools-extra/test/clang-doc/Inputs/clang-doc-project1/Circle.cpp +++ /dev/null @@ -1,11 +0,0 @@ -#include "Circle.h" - -Circle::Circle(double radius) : radius_(radius) {} - -double Circle::area() const { - return 3.141 * radius_ * radius_; -} - -double Circle::perimeter() const { - return 3.141 * radius_; -} \ No newline at end of file diff --git a/clang-tools-extra/test/clang-doc/Inputs/clang-doc-project1/Circle.h b/clang-tools-extra/test/clang-doc/Inputs/clang-doc-project1/Circle.h deleted file mode 100644 index 7bee3ffa92539..0000000000000 --- a/clang-tools-extra/test/clang-doc/Inputs/clang-doc-project1/Circle.h +++ /dev/null @@ -1,35 +0,0 @@ -#pragma once - -#include "Shape.h" - -/** - * @brief Circle class derived from Shape. - * - * Represents a circle with a given radius. - */ -class Circle : public Shape { -public: - /** - * @brief Constructs a new Circle object. - * - * @param radius Radius of the circle. - */ - Circle(double radius); - - /** - * @brief Calculates the area of the circle. - * - * @return double The area of the circle. - */ - double area() const override; - - /** - * @brief Calculates the perimeter of the circle. - * - * @return double The perimeter of the circle. - */ - double perimeter() const override; - -private: - double radius_; ///< Radius of the circle. -}; diff --git a/clang-tools-extra/test/clang-doc/Inputs/clang-doc-project1/Rectangle.cpp b/clang-tools-extra/test/clang-doc/Inputs/clang-doc-project1/Rectangle.cpp deleted file mode 100644 index 7ffc769157ebc..0000000000000 --- a/clang-tools-extra/test/clang-doc/Inputs/clang-doc-project1/Rectangle.cpp +++ /dev/null @@ -1,12 +0,0 @@ -#include "Rectangle.h" - -Rectangle::Rectangle(double width, double height) - : width_(width), height_(height) {} - -double Rectangle::area() const { - return width_ * height_; -} - -double Rectangle::perimeter() const { - return 2 * (width_ + height_); -} \ No newline at end of file diff --git a/clang-tools-extra/test/clang-doc/Inputs/clang-doc-project1/Rectangle.h b/clang-tools-extra/test/clang-doc/Inputs/clang-doc-project1/Rectangle.h deleted file mode 100644 index 8c6223a4f6180..0000000000000 --- a/clang-tools-extra/test/clang-doc/Inputs/clang-doc-project1/Rectangle.h +++ /dev/null @@ -1,37 +0,0 @@ -#pragma once - -#include "Shape.h" - -/** - * @brief Rectangle class derived from Shape. - * - * Represents a rectangle with a given width and height. - */ -class Rectangle : public Shape { -public: - /** - * @brief Constructs a new Rectangle object. - * - * @param width Width of the rectangle. - * @param height Height of the rectangle. - */ - Rectangle(double width, double height); - - /** - * @brief Calculates the area of the rectangle. - * - * @return double The area of the rectangle. - */ - double area() const override; - - /** - * @brief Calculates the perimeter of the rectangle. - * - * @return double The perimeter of the rectangle. - */ - double perimeter() const override; - -private: - double width_; ///< Width of the rectangle. - double height_; ///< Height of the rectangle. -}; \ No newline at end of file diff --git a/clang-tools-extra/test/clang-doc/Inputs/clang-doc-project1/Shape.h b/clang-tools-extra/test/clang-doc/Inputs/clang-doc-project1/Shape.h deleted file mode 100644 index e5c5d4c9e4412..0000000000000 --- a/clang-tools-extra/test/clang-doc/Inputs/clang-doc-project1/Shape.h +++ /dev/null @@ -1,30 +0,0 @@ -#pragma once - -/** - * @brief Abstract base class for shapes. - * - * Provides a common interface for different types of shapes. - */ -class Shape { -public: - /** - * @brief Virtual destructor. - */ - virtual ~Shape() {} - - /** - * @brief Calculates the area of the shape. - * - * @return double The area of the shape. - */ - virtual double area() const = 0; - - /** - * @brief Calculates the perimeter of the shape. - * - * @return double The perimeter of the shape. - */ - virtual double perimeter() const = 0; -}; - - diff --git a/clang-tools-extra/test/clang-doc/Inputs/clang-doc-project1/database_template.json b/clang-tools-extra/test/clang-doc/Inputs/clang-doc-project1/database_template.json deleted file mode 100644 index 0549c5b718f08..0000000000000 --- a/clang-tools-extra/test/clang-doc/Inputs/clang-doc-project1/database_template.json +++ /dev/null @@ -1,22 +0,0 @@ -[ -{ - "directory": "$test_dir/build", - "command": "clang++ -o main.o -I../include $test_dir/src/main.cpp", - "file": "$test_dir/src/main.cpp" -}, -{ - "directory": "$test_dir/build", - "command": "clang++ -o Calculator.o -I../include $test_dir/src/Calculator.cpp", - "file": "$test_dir/src/Calculator.cpp" -}, -{ - "directory": "$test_dir/build", - "command": "clang++ -o Circle.o -I../include $test_dir/src/Circle.cpp", - "file": "$test_dir/src/Circle.cpp" -}, -{ - "directory": "$test_dir/build", - "command": "clang++ -o Rectangle.o -I../include $test_dir/src/Rectangle.cpp", - "file": "$test_dir/src/Rectangle.cpp" -} -] \ No newline at end of file diff --git a/clang-tools-extra/test/clang-doc/Inputs/clang-doc-project1/main.cpp b/clang-tools-extra/test/clang-doc/Inputs/clang-doc-project1/main.cpp deleted file mode 100644 index e20732c7de371..0000000000000 --- a/clang-tools-extra/test/clang-doc/Inputs/clang-doc-project1/main.cpp +++ /dev/null @@ -1,25 +0,0 @@ -#include <iostream> -#include "Calculator.h" -#include "Circle.h" -#include "Rectangle.h" - -int main() { - // Calculator - Calculator calc; - std::cout << "Add: " << calc.add(3, 4) << std::endl; - std::cout << "Subtract: " << calc.subtract(10, 5) << std::endl; - std::cout << "Multiply: " << calc.multiply(2, 3) << std::endl; - std::cout << "Divide: " << calc.divide(10, 2) << std::endl; - - // Circle - Circle circle(5.0); - std::cout << "Circle Area: " << circle.area() << std::endl; - std::cout << "Circle Perimeter: " << circle.perimeter() << std::endl; - - // Rectangle - Rectangle rectangle(4.0, 6.0); - std::cout << "Rectangle Area: " << rectangle.area() << std::endl; - std::cout << "Rectangle Perimeter: " << rectangle.perimeter() << std::endl; - - return 0; -} \ No newline at end of file diff --git a/clang-tools-extra/test/clang-doc/clang-doc-project1.cpp b/clang-tools-extra/test/clang-doc/clang-doc-project1.cpp deleted file mode 100644 index 8c0289519854d..0000000000000 --- a/clang-tools-extra/test/clang-doc/clang-doc-project1.cpp +++ /dev/null @@ -1,11 +0,0 @@ -// RUN: mkdir -p %T/clang-doc/build -// RUN: mkdir -p %T/clang-doc/include -// RUN: mkdir -p %T/clang-doc/src -// RUN: mkdir -p %T/clang-doc/docs -// RUN: sed 's|$test_dir|%/T/clang-doc|g' %S/Inputs/clang-doc-project1/database_template.json > %T/clang-doc/build/compile_commands.json -// RUN: cp %S/Inputs/clang-doc-project1/*.h %T/clang-doc/include -// RUN: cp %S/Inputs/clang-doc-project1/*.cpp %T/clang-doc/src -// RUN: cd %T/clang-doc/build -// RUN: clang-doc --format=html --executor=all-TUs --output=%T/clang-doc/docs ./compile_commands.json - - >From b11f5fa4bba0c8338a601cb803d3a9858a026a7a Mon Sep 17 00:00:00 2001 From: PeterChou1 <peter.c...@mail.utoronto.ca> Date: Fri, 28 Jun 2024 20:21:45 -0400 Subject: [PATCH 4/5] [clang-doc] switch implementation approach to LoadIndex via a js script --- clang-tools-extra/clang-doc/HTMLGenerator.cpp | 6 +++--- clang-tools-extra/clang-doc/assets/index.js | 7 ++----- .../test/clang-doc/basic-project.test | 19 ++++++++++--------- 3 files changed, 15 insertions(+), 17 deletions(-) diff --git a/clang-tools-extra/clang-doc/HTMLGenerator.cpp b/clang-tools-extra/clang-doc/HTMLGenerator.cpp index c82527ecdf5b4..b5da17564c2e0 100644 --- a/clang-tools-extra/clang-doc/HTMLGenerator.cpp +++ b/clang-tools-extra/clang-doc/HTMLGenerator.cpp @@ -972,7 +972,7 @@ static llvm::Error serializeIndex(ClangDocContext &CDCtx) { std::error_code FileErr; llvm::SmallString<128> FilePath; llvm::sys::path::native(CDCtx.OutDirectory, FilePath); - llvm::sys::path::append(FilePath, "index.json"); + llvm::sys::path::append(FilePath, "index_json.js"); llvm::raw_fd_ostream OS(FilePath, FileErr, llvm::sys::fs::OF_None); if (FileErr != OK) { return llvm::createStringError(llvm::inconvertibleErrorCode(), @@ -993,9 +993,9 @@ static llvm::Error serializeIndex(ClangDocContext &CDCtx) { }); }); }; - OS << "var JsonIndex = `\n"; + OS << "async function LoadIndex() {\nreturn"; IndexToJSON(CDCtx.Idx); - OS << "`;\n"; + OS << ";\n}"; return llvm::Error::success(); } diff --git a/clang-tools-extra/clang-doc/assets/index.js b/clang-tools-extra/clang-doc/assets/index.js index 379867268527e..2d2da86fd5357 100644 --- a/clang-tools-extra/clang-doc/assets/index.js +++ b/clang-tools-extra/clang-doc/assets/index.js @@ -80,9 +80,6 @@ function createIndex(Index) { // Runs after DOM loads document.addEventListener("DOMContentLoaded", function() { - // JsonIndex is a variable from another file that contains the index - // in JSON format - fetch("/index.json") - .then((response) => response.json()) - .then((Index) => { createIndex(Index); }); + // Loadindex is an asynchronous function that will be generated clang-doc + LoadIndex().then((Index) => { createIndex(Index); }); }); diff --git a/clang-tools-extra/test/clang-doc/basic-project.test b/clang-tools-extra/test/clang-doc/basic-project.test index 3118c20495987..f2fd0f0678402 100644 --- a/clang-tools-extra/test/clang-doc/basic-project.test +++ b/clang-tools-extra/test/clang-doc/basic-project.test @@ -7,42 +7,42 @@ // RUN: FileCheck %s -input-file=%t/docs/GlobalNamespace/Rectangle.html -check-prefix=HTML-RECTANGLE // RUN: FileCheck %s -input-file=%t/docs/GlobalNamespace/Circle.html -check-prefix=HTML-CIRCLE -// JSON-INDEX: var JsonIndex = ` -// JSON-INDEX-NEXT: { -// JSON-INDEX-NEXT: "USR": "{{([0-9A-F]{40})}}", +// JSON-INDEX: async function LoadIndex() { +// JSON-INDEX-NEXT: return{ +// JSON-INDEX-NEXT: "USR": "0000000000000000000000000000000000000000", // JSON-INDEX-NEXT: "Name": "", // JSON-INDEX-NEXT: "RefType": "default", // JSON-INDEX-NEXT: "Path": "", // JSON-INDEX-NEXT: "Children": [ // JSON-INDEX-NEXT: { -// JSON-INDEX-NEXT: "USR": "{{([0-9A-F]{40})}}", +// JSON-INDEX-NEXT: "USR": "0000000000000000000000000000000000000000", // JSON-INDEX-NEXT: "Name": "GlobalNamespace", // JSON-INDEX-NEXT: "RefType": "namespace", // JSON-INDEX-NEXT: "Path": "GlobalNamespace", // JSON-INDEX-NEXT: "Children": [ // JSON-INDEX-NEXT: { -// JSON-INDEX-NEXT: "USR": "{{([0-9A-F]{40})}}", +// JSON-INDEX-NEXT: "USR": "49BA9D09A2363C7ADC5AE39DD0FDD500B18DC6AB", // JSON-INDEX-NEXT: "Name": "Calculator", // JSON-INDEX-NEXT: "RefType": "record", // JSON-INDEX-NEXT: "Path": "GlobalNamespace", // JSON-INDEX-NEXT: "Children": [] // JSON-INDEX-NEXT: }, // JSON-INDEX-NEXT: { -// JSON-INDEX-NEXT: "USR": "{{([0-9A-F]{40})}}", +// JSON-INDEX-NEXT: "USR": "EED5CC8668044E01443E4004714123CDBB550272", // JSON-INDEX-NEXT: "Name": "Circle", // JSON-INDEX-NEXT: "RefType": "record", // JSON-INDEX-NEXT: "Path": "GlobalNamespace", // JSON-INDEX-NEXT: "Children": [] // JSON-INDEX-NEXT: }, // JSON-INDEX-NEXT: { -// JSON-INDEX-NEXT: "USR": "{{([0-9A-F]{40})}}", +// JSON-INDEX-NEXT: "USR": "9D820FDE192D63198713DAB951621E6A3ED24EA2", // JSON-INDEX-NEXT: "Name": "Rectangle", // JSON-INDEX-NEXT: "RefType": "record", // JSON-INDEX-NEXT: "Path": "GlobalNamespace", // JSON-INDEX-NEXT: "Children": [] // JSON-INDEX-NEXT: }, // JSON-INDEX-NEXT: { -// JSON-INDEX-NEXT: "USR": "{{([0-9A-F]{40})}}", +// JSON-INDEX-NEXT: "USR": "4769D2967A9F68AE11B7339276D408F289B3D4CC", // JSON-INDEX-NEXT: "Name": "Shape", // JSON-INDEX-NEXT: "RefType": "record", // JSON-INDEX-NEXT: "Path": "GlobalNamespace", @@ -51,7 +51,8 @@ // JSON-INDEX-NEXT: ] // JSON-INDEX-NEXT: } // JSON-INDEX-NEXT: ] -// JSON-INDEX-NEXT: }`; +// JSON-INDEX-NEXT: }; +// JSON-INDEX-NEXT: } // HTML-SHAPE: <!DOCTYPE html> // HTML-SHAPE-NEXT: <meta charset="utf-8"/> >From e80aac14092d8aefa060756066ef81e6f66c93ee Mon Sep 17 00:00:00 2001 From: PeterChou1 <peter.c...@mail.utoronto.ca> Date: Fri, 28 Jun 2024 20:24:38 -0400 Subject: [PATCH 5/5] [clang-doc] make USR matching in test file use a regex --- clang-tools-extra/test/clang-doc/basic-project.test | 12 ++++++------ 1 file changed, 6 insertions(+), 6 deletions(-) diff --git a/clang-tools-extra/test/clang-doc/basic-project.test b/clang-tools-extra/test/clang-doc/basic-project.test index f2fd0f0678402..bab5f8e1761bc 100644 --- a/clang-tools-extra/test/clang-doc/basic-project.test +++ b/clang-tools-extra/test/clang-doc/basic-project.test @@ -9,40 +9,40 @@ // JSON-INDEX: async function LoadIndex() { // JSON-INDEX-NEXT: return{ -// JSON-INDEX-NEXT: "USR": "0000000000000000000000000000000000000000", +// JSON-INDEX-NEXT: "USR": "{{([0-9A-F]{40})}}", // JSON-INDEX-NEXT: "Name": "", // JSON-INDEX-NEXT: "RefType": "default", // JSON-INDEX-NEXT: "Path": "", // JSON-INDEX-NEXT: "Children": [ // JSON-INDEX-NEXT: { -// JSON-INDEX-NEXT: "USR": "0000000000000000000000000000000000000000", +// JSON-INDEX-NEXT: "USR": "{{([0-9A-F]{40})}}", // JSON-INDEX-NEXT: "Name": "GlobalNamespace", // JSON-INDEX-NEXT: "RefType": "namespace", // JSON-INDEX-NEXT: "Path": "GlobalNamespace", // JSON-INDEX-NEXT: "Children": [ // JSON-INDEX-NEXT: { -// JSON-INDEX-NEXT: "USR": "49BA9D09A2363C7ADC5AE39DD0FDD500B18DC6AB", +// JSON-INDEX-NEXT: "USR": "{{([0-9A-F]{40})}}", // JSON-INDEX-NEXT: "Name": "Calculator", // JSON-INDEX-NEXT: "RefType": "record", // JSON-INDEX-NEXT: "Path": "GlobalNamespace", // JSON-INDEX-NEXT: "Children": [] // JSON-INDEX-NEXT: }, // JSON-INDEX-NEXT: { -// JSON-INDEX-NEXT: "USR": "EED5CC8668044E01443E4004714123CDBB550272", +// JSON-INDEX-NEXT: "USR": "{{([0-9A-F]{40})}}", // JSON-INDEX-NEXT: "Name": "Circle", // JSON-INDEX-NEXT: "RefType": "record", // JSON-INDEX-NEXT: "Path": "GlobalNamespace", // JSON-INDEX-NEXT: "Children": [] // JSON-INDEX-NEXT: }, // JSON-INDEX-NEXT: { -// JSON-INDEX-NEXT: "USR": "9D820FDE192D63198713DAB951621E6A3ED24EA2", +// JSON-INDEX-NEXT: "USR": "{{([0-9A-F]{40})}}", // JSON-INDEX-NEXT: "Name": "Rectangle", // JSON-INDEX-NEXT: "RefType": "record", // JSON-INDEX-NEXT: "Path": "GlobalNamespace", // JSON-INDEX-NEXT: "Children": [] // JSON-INDEX-NEXT: }, // JSON-INDEX-NEXT: { -// JSON-INDEX-NEXT: "USR": "4769D2967A9F68AE11B7339276D408F289B3D4CC", +// JSON-INDEX-NEXT: "USR": "{{([0-9A-F]{40})}}", // JSON-INDEX-NEXT: "Name": "Shape", // JSON-INDEX-NEXT: "RefType": "record", // JSON-INDEX-NEXT: "Path": "GlobalNamespace", _______________________________________________ cfe-commits mailing list cfe-commits@lists.llvm.org https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits