Skip to content

[clang-doc] [feat] add --repository-line-prefix argument #131280

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

Merged
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
Show all changes
22 commits
Select commit Hold shift + click to select a range
bf9bd41
[clang-doc] [feat] add `--repository-line-prefix` argument (fix #59814)
hulxv Mar 14, 2025
fea0a3d
[clang-doc][test] reuse `basic-project.test` for testing `--repositor…
hulxv Mar 14, 2025
4b769dd
[clang] [chore] revert important notes
hulxv Mar 17, 2025
a7f7f6c
[clang] fix formatting for help of `--repository-line-prefix`
hulxv Mar 17, 2025
b61c01f
[clang] formatting `LineAnchor` using `formatv`
hulxv Mar 20, 2025
74952a4
[clang-doc] add `--repository-line-prefix` to the existing test inste…
hulxv Mar 20, 2025
78682f1
Merge branch 'main' into clang-doc/fix/repository-doesnt-work-with-gi…
hulxv Mar 21, 2025
5d184b0
[clang-doc][refactor] improve `HtmlGenerator::writeFileDescription`
hulxv Mar 22, 2025
109fd75
Merge branch 'main' into clang-doc/fix/repository-doesnt-work-with-gi…
hulxv Mar 22, 2025
7f57fe6
[clang-doc] fix formatting
hulxv Mar 22, 2025
2fce1c1
fix: avoid useless string copies
hulxv Mar 23, 2025
02c9e07
[clang-doc] improving `LINE-PREFIX` test
hulxv Mar 24, 2025
91d9bcd
avoid unnecessary string copies
hulxv Mar 24, 2025
322a188
[clang-doc] make a helper function for creating file definition
hulxv Mar 24, 2025
a33876d
[clang-doc] avoid unnecessary string casting and concatenatation
hulxv Mar 25, 2025
779117a
[clang-doc][refactor] improve naming of `writeFileDefinition`
hulxv Mar 25, 2025
54ede23
[clang-doc] add missing new line to clang-doc basic-project test
hulxv Mar 25, 2025
af71ae8
fix formatting
hulxv Mar 25, 2025
331c786
fix: building error in `HTMLGeneratorTest.cpp`
hulxv Mar 25, 2025
fdb1bed
Merge branch 'main' into clang-doc/fix/repository-doesnt-work-with-gi…
hulxv Mar 26, 2025
258bd3b
Merge branch 'main' into clang-doc/fix/repository-doesnt-work-with-gi…
hulxv Mar 28, 2025
d6477a9
[clang-doc] fix formatting
hulxv Mar 28, 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
66 changes: 41 additions & 25 deletions clang-tools-extra/clang-doc/HTMLGenerator.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -491,9 +491,9 @@ genReferencesBlock(const std::vector<Reference> &References,
return Out;
}

static std::unique_ptr<TagNode>
writeFileDefinition(const Location &L,
std::optional<StringRef> RepositoryUrl = std::nullopt) {
static std::unique_ptr<TagNode> writeFileDefinition(
const Location &L, std::optional<StringRef> RepositoryUrl = std::nullopt,
std::optional<StringRef> RepositoryLinePrefix = std::nullopt) {
if (!L.IsFileInRootDir && !RepositoryUrl)
return std::make_unique<TagNode>(
HTMLTag::TAG_P, "Defined at line " + std::to_string(L.LineNumber) +
Expand All @@ -514,17 +514,21 @@ writeFileDefinition(const Location &L,
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));
// 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());

std::string LineAnchor = "#";

if (RepositoryLinePrefix)
LineAnchor += RepositoryLinePrefix.value().str();

LineAnchor += std::to_string(L.LineNumber);

LocNumberNode->Attributes.emplace_back("href", (FileURL + LineAnchor).str());
Node->Children.emplace_back(std::move(LocNumberNode));
Node->Children.emplace_back(std::make_unique<TextNode>(" of file "));

auto LocFileNode = std::make_unique<TagNode>(
HTMLTag::TAG_A, llvm::sys::path::filename(FileURL));

LocFileNode->Attributes.emplace_back("href", std::string(FileURL));
Node->Children.emplace_back(std::move(LocFileNode));
return Node;
Expand Down Expand Up @@ -750,11 +754,15 @@ genHTML(const EnumInfo &I, const ClangDocContext &CDCtx) {
Out.emplace_back(std::move(Table));

if (I.DefLoc) {
if (!CDCtx.RepositoryUrl)
Out.emplace_back(writeFileDefinition(*I.DefLoc));
else
Out.emplace_back(
writeFileDefinition(*I.DefLoc, StringRef{*CDCtx.RepositoryUrl}));
std::optional<StringRef> RepoUrl;
std::optional<StringRef> RepoLinePrefix;

if (CDCtx.RepositoryUrl)
RepoUrl = StringRef{*CDCtx.RepositoryUrl};
if (CDCtx.RepositoryLinePrefix)
RepoLinePrefix = StringRef{*CDCtx.RepositoryLinePrefix};

Out.emplace_back(writeFileDefinition(*I.DefLoc, RepoUrl, RepoLinePrefix));
}

std::string Description;
Expand Down Expand Up @@ -799,11 +807,15 @@ genHTML(const FunctionInfo &I, const ClangDocContext &CDCtx,
FunctionHeader->Children.emplace_back(std::make_unique<TextNode>(")"));

if (I.DefLoc) {
if (!CDCtx.RepositoryUrl)
Out.emplace_back(writeFileDefinition(*I.DefLoc));
else
Out.emplace_back(writeFileDefinition(
*I.DefLoc, StringRef{*CDCtx.RepositoryUrl}));
std::optional<StringRef> RepoUrl;
std::optional<StringRef> RepoLinePrefix;

if (CDCtx.RepositoryUrl)
RepoUrl = StringRef{*CDCtx.RepositoryUrl};
if (CDCtx.RepositoryLinePrefix)
RepoLinePrefix = StringRef{*CDCtx.RepositoryLinePrefix};

Out.emplace_back(writeFileDefinition(*I.DefLoc, RepoUrl, RepoLinePrefix));
}

std::string Description;
Expand Down Expand Up @@ -866,11 +878,15 @@ genHTML(const RecordInfo &I, Index &InfoIndex, const ClangDocContext &CDCtx,
Out.emplace_back(std::make_unique<TagNode>(HTMLTag::TAG_H1, InfoTitle));

if (I.DefLoc) {
if (!CDCtx.RepositoryUrl)
Out.emplace_back(writeFileDefinition(*I.DefLoc));
else
Out.emplace_back(writeFileDefinition(
*I.DefLoc, StringRef{*CDCtx.RepositoryUrl}));
std::optional<StringRef> RepoUrl;
std::optional<StringRef> RepoLinePrefix;

if (CDCtx.RepositoryUrl)
RepoUrl = StringRef{*CDCtx.RepositoryUrl};
if (CDCtx.RepositoryLinePrefix)
RepoLinePrefix = StringRef{*CDCtx.RepositoryLinePrefix};

Out.emplace_back(writeFileDefinition(*I.DefLoc, RepoUrl, RepoLinePrefix));
}

std::string Description;
Expand Down
7 changes: 6 additions & 1 deletion clang-tools-extra/clang-doc/MDGenerator.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,12 @@ static void writeFileDefinition(const ClangDocContext &CDCtx, const Location &L,
OS << "*Defined at " << L.Filename << "#" << std::to_string(L.LineNumber)
<< "*";
} else {
OS << "*Defined at [" << L.Filename << "#" << std::to_string(L.LineNumber)
OS << "*Defined at [" << L.Filename << "#";

if (!CDCtx.RepositoryLinePrefix)
OS << StringRef{*CDCtx.RepositoryLinePrefix};

OS << std::to_string(L.LineNumber)
<< "](" << StringRef{*CDCtx.RepositoryUrl}
<< llvm::sys::path::relative_path(L.Filename) << "#"
<< std::to_string(L.LineNumber) << ")"
Expand Down
4 changes: 4 additions & 0 deletions clang-tools-extra/clang-doc/Representation.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -368,6 +368,7 @@ ClangDocContext::ClangDocContext(tooling::ExecutionContext *ECtx,
StringRef ProjectName, bool PublicOnly,
StringRef OutDirectory, StringRef SourceRoot,
StringRef RepositoryUrl,
StringRef RepositoryLinePrefix,
std::vector<std::string> UserStylesheets)
: ECtx(ECtx), ProjectName(ProjectName), PublicOnly(PublicOnly),
OutDirectory(OutDirectory), UserStylesheets(UserStylesheets) {
Expand All @@ -381,6 +382,9 @@ ClangDocContext::ClangDocContext(tooling::ExecutionContext *ECtx,
if (!RepositoryUrl.empty() && !RepositoryUrl.starts_with("http://") &&
!RepositoryUrl.starts_with("https://"))
this->RepositoryUrl->insert(0, "https://");

if (!RepositoryLinePrefix.empty())
this->RepositoryLinePrefix = std::string(RepositoryLinePrefix);
}
}

Expand Down
5 changes: 4 additions & 1 deletion clang-tools-extra/clang-doc/Representation.h
Original file line number Diff line number Diff line change
Expand Up @@ -508,6 +508,7 @@ struct ClangDocContext {
ClangDocContext(tooling::ExecutionContext *ECtx, StringRef ProjectName,
bool PublicOnly, StringRef OutDirectory, StringRef SourceRoot,
StringRef RepositoryUrl,
StringRef RepositoryCodeLinePrefix,
std::vector<std::string> UserStylesheets);
tooling::ExecutionContext *ECtx;
std::string ProjectName; // Name of project clang-doc is documenting.
Expand All @@ -518,10 +519,12 @@ struct ClangDocContext {
// the file is in this dir.
// URL of repository that hosts code used for links to definition locations.
std::optional<std::string> RepositoryUrl;
// Prefix of line code for repository.
std::optional<std::string> RepositoryLinePrefix;
// Path of CSS stylesheets that will be copied to OutDirectory and used to
// style all HTML files.
std::vector<std::string> UserStylesheets;
// JavaScript files that will be imported in allHTML file.
// JavaScript files that will be imported in all HTML file.
std::vector<std::string> JsScripts;
Index Idx;
};
Expand Down
13 changes: 9 additions & 4 deletions clang-tools-extra/clang-doc/tool/ClangDocMain.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -99,6 +99,12 @@ URL of repository that hosts code.
Used for links to definition locations.)"),
llvm::cl::cat(ClangDocCategory));

static llvm::cl::opt<std::string>
RepositoryCodeLinePrefix("repository-line-prefix", llvm::cl::desc(R"(
Prefix of line code for repository.
)"),
llvm::cl::cat(ClangDocCategory));

enum OutputFormatTy {
md,
yaml,
Expand Down Expand Up @@ -141,8 +147,7 @@ llvm::Error getAssetFiles(clang::doc::ClangDocContext &CDCtx) {
using DirIt = llvm::sys::fs::directory_iterator;
std::error_code FileErr;
llvm::SmallString<128> FilePath(UserAssetPath);
for (DirIt DirStart = DirIt(UserAssetPath, FileErr),
DirEnd;
for (DirIt DirStart = DirIt(UserAssetPath, FileErr), DirEnd;
!FileErr && DirStart != DirEnd; DirStart.increment(FileErr)) {
FilePath = DirStart->path();
if (llvm::sys::fs::is_regular_file(FilePath)) {
Expand Down Expand Up @@ -268,8 +273,8 @@ Example usage for a project using a compile commands database:
OutDirectory,
SourceRoot,
RepositoryUrl,
{UserStylesheets.begin(), UserStylesheets.end()}
};
RepositoryCodeLinePrefix,
{UserStylesheets.begin(), UserStylesheets.end()}};

if (Format == "html") {
if (auto Err = getHtmlAssetFiles(argv[0], CDCtx)) {
Expand Down
Loading