Skip to content

[clang-doc] Adds a mustache backend #133161

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 25 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
25 commits
Select commit Hold shift + click to select a range
3d7441a
[clang-doc] add suport for clang-doc enum generation
PeterChou1 Jul 31, 2024
59df7dd
[clang-doc] remove useless code
PeterChou1 Jul 31, 2024
ad61ab4
[clang-doc] address pr comments
PeterChou1 Aug 12, 2024
8e2d7fa
[clang-doc] fix unittest
PeterChou1 Aug 12, 2024
1e62f7d
[clang-doc] address pr comments
PeterChou1 Aug 12, 2024
233b3c1
[clang-doc] clang-format
PeterChou1 Aug 12, 2024
af48431
[llvm] implement support for mustache template language
PeterChou1 Aug 23, 2024
85cb28e
[llvm][support] clang-format
PeterChou1 Sep 6, 2024
c685493
[llvm][support] clang-format
PeterChou1 Sep 6, 2024
8c85923
[llvm] add mustache verification tool
PeterChou1 Oct 8, 2024
3c19f18
[llvm] modify mustache tool
PeterChou1 Oct 12, 2024
0d459f8
[llvm] add mustache verification tool
PeterChou1 Oct 8, 2024
75925a4
[llvm] add mustache verification tool
PeterChou1 Oct 8, 2024
c753075
[clang-doc] init mustache implementation
PeterChou1 Sep 13, 2024
19bed2c
[clang-doc] add a mustache backend init implementation
PeterChou1 Sep 13, 2024
2a3c40e
[clang-doc] update
PeterChou1 Sep 24, 2024
1b07553
[clang-doc] add more templates
PeterChou1 Oct 7, 2024
1cf5eb0
[clang-doc] init mustache implementation
PeterChou1 Sep 13, 2024
8ba6a5d
[clang-doc] add a mustache backend init implementation
PeterChou1 Sep 13, 2024
79ea4bc
[clang-doc] update
PeterChou1 Sep 24, 2024
6010fb2
[clang-doc] add more templates
PeterChou1 Oct 7, 2024
85c4c21
[clang-doc] init mustache implementation
PeterChou1 Sep 13, 2024
356217c
[clang-doc] add a mustache backend init implementation
PeterChou1 Sep 13, 2024
450a6b1
[clang-doc] add class template
PeterChou1 Oct 11, 2024
9dbd1b8
Merge branch 'main' into clang-doc-mustache-rebase
PeterChou1 Mar 26, 2025
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions clang-tools-extra/clang-doc/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,8 @@ add_clang_library(clangDoc STATIC
Representation.cpp
Serialize.cpp
YAMLGenerator.cpp
HTMLMustacheGenerator.cpp
FileHelpersClangDoc.cpp

DEPENDS
omp_gen
Expand Down
75 changes: 75 additions & 0 deletions clang-tools-extra/clang-doc/FileHelpersClangDoc.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,75 @@
//===-- 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 "FileHelpersClangDoc.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 OK;
std::error_code FileErr = llvm::sys::fs::copy_file(PathRead, PathWrite);
if (FileErr != OK) {
Comment on lines +22 to +24
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
std::error_code OK;
std::error_code FileErr = llvm::sys::fs::copy_file(PathRead, PathWrite);
if (FileErr != OK) {
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) {
Comment on lines +34 to +35
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Maybe we should drop this in favor of std::filesystem::relative()?

// 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
26 changes: 26 additions & 0 deletions clang-tools-extra/clang-doc/FileHelpersClangDoc.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
//===-- FileHelpersClangDoc.h --- File Helpers -------------------*- C++-*-===//
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

probably putting this in clang-doc/support/File.h is a bit more typical. At the least the file shouldn't have helper in the name, and probably doesn't need ClangDoc in it either.

//
// 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
4 changes: 3 additions & 1 deletion clang-tools-extra/clang-doc/Generators.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -100,12 +100,14 @@ void Generator::addInfoToIndex(Index &Idx, const doc::Info *Info) {
extern volatile int YAMLGeneratorAnchorSource;
extern volatile int MDGeneratorAnchorSource;
extern volatile int HTMLGeneratorAnchorSource;
extern volatile int MHTMLGeneratorAnchorSource;
static int LLVM_ATTRIBUTE_UNUSED YAMLGeneratorAnchorDest =
YAMLGeneratorAnchorSource;
static int LLVM_ATTRIBUTE_UNUSED MDGeneratorAnchorDest =
MDGeneratorAnchorSource;
static int LLVM_ATTRIBUTE_UNUSED HTMLGeneratorAnchorDest =
HTMLGeneratorAnchorSource;

static int LLVM_ATTRIBUTE_UNUSED MHTMLGeneratorAnchorDest =
MHTMLGeneratorAnchorSource;
} // namespace doc
} // namespace clang
9 changes: 5 additions & 4 deletions clang-tools-extra/clang-doc/HTMLGenerator.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -496,7 +496,7 @@ writeFileDefinition(const Location &L,
std::optional<StringRef> RepositoryUrl = std::nullopt) {
if (!L.IsFileInRootDir && !RepositoryUrl)
return std::make_unique<TagNode>(
HTMLTag::TAG_P, "Defined at line " + std::to_string(L.LineNumber) +
HTMLTag::TAG_P, "Defined at line " + std::to_string(L.StartLineNumber) +
" of file " + L.Filename);
SmallString<128> FileURL(RepositoryUrl.value_or(""));
llvm::sys::path::append(
Expand All @@ -513,14 +513,14 @@ writeFileDefinition(const Location &L,
auto Node = std::make_unique<TagNode>(HTMLTag::TAG_P);
Node->Children.emplace_back(std::make_unique<TextNode>("Defined at line "));
auto LocNumberNode =
std::make_unique<TagNode>(HTMLTag::TAG_A, std::to_string(L.LineNumber));
std::make_unique<TagNode>(HTMLTag::TAG_A, std::to_string(L.StartLineNumber));
// The links to a specific line in the source code use the github /
// googlesource notation so it won't work for all hosting pages.
// FIXME: we probably should have a configuration setting for line number
// rendering in the HTML. For example, GitHub uses #L22, while googlesource
// uses #22 for line numbers.
LocNumberNode->Attributes.emplace_back(
"href", (FileURL + "#" + std::to_string(L.LineNumber)).str());
"href", (FileURL + "#" + std::to_string(L.StartLineNumber)).str());
Node->Children.emplace_back(std::move(LocNumberNode));
Node->Children.emplace_back(std::make_unique<TextNode>(" of file "));
auto LocFileNode = std::make_unique<TagNode>(
Expand Down Expand Up @@ -1146,7 +1146,8 @@ static llvm::Error genIndex(const ClangDocContext &CDCtx) {
return llvm::Error::success();
}

static llvm::Error copyFile(StringRef FilePath, StringRef OutDirectory) {
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));
Expand Down
Loading
Loading