Skip to content
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

[clang-doc] Add --asset option to clang-doc #94717

Merged
merged 20 commits into from
Jun 20, 2024
Merged
Show file tree
Hide file tree
Changes from 6 commits
Commits
Show all changes
20 commits
Select commit Hold shift + click to select a range
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
107 changes: 88 additions & 19 deletions clang-tools-extra/clang-doc/tool/ClangDocMain.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -81,6 +81,12 @@ static llvm::cl::list<std::string> UserStylesheets(
llvm::cl::desc("CSS stylesheets to extend the default styles."),
llvm::cl::cat(ClangDocCategory));

static llvm::cl::opt<std::string> UserAssetPath(
"asset",
llvm::cl::desc("User supplied asset path to "
"override the default css and js files for html output"),
llvm::cl::cat(ClangDocCategory));

static llvm::cl::opt<std::string> SourceRoot("source-root", llvm::cl::desc(R"(
Directory where processed files are stored.
Links to definition locations will only be
Expand Down Expand Up @@ -127,16 +133,91 @@ std::string getFormatString() {
// GetMainExecutable (since some platforms don't support taking the
// address of main, and some platforms can't implement GetMainExecutable
// without being given the address of a function in the main executable).
std::string GetExecutablePath(const char *Argv0, void *MainAddr) {
std::string getExecutablePath(const char *Argv0, void *MainAddr) {
return llvm::sys::fs::getMainExecutable(Argv0, MainAddr);
}

llvm::Error getAssetFiles(clang::doc::ClangDocContext &CDCtx) {
std::error_code Code;
Copy link
Contributor

Choose a reason for hiding this comment

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

nit: I'd use Err.

There isn't a completely consistent practice in Clang-Doc code, but FileErr seems most common. https://llvm.org/docs/ProgrammersManual.html#interoperability-with-std-error-code-and-erroror has a little to say, and the Error handling section on that page may be useful, but we don't have a firm convention.

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 Code;
std::error_code Err;
Suggested change
std::error_code Code;
std::error_code FileErr;

Copy link
Contributor

Choose a reason for hiding this comment

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

This still uses Code. please use a naming convention consistent w/ this tool and other parts of LLVM before marking it resolved.

llvm::SmallString<128> FilePath = llvm::SmallString<128>(UserAssetPath);
PeterChou1 marked this conversation as resolved.
Show resolved Hide resolved
for (auto DirIt = llvm::sys::fs::directory_iterator(UserAssetPath, Code),
DirEnd = llvm::sys::fs::directory_iterator();
!Code && DirIt != DirEnd; DirIt.increment(Code)) {
FilePath = llvm::SmallString<128>(DirIt->path());
PeterChou1 marked this conversation as resolved.
Show resolved Hide resolved
if (llvm::sys::fs::is_regular_file(FilePath)) {
if (llvm::sys::path::extension(FilePath) == ".css") {
CDCtx.UserStylesheets.insert(CDCtx.UserStylesheets.begin(),
std::string(FilePath));
} else if (llvm::sys::path::extension(FilePath) == ".js") {
CDCtx.FilesToCopy.emplace_back(FilePath.str());
}
PeterChou1 marked this conversation as resolved.
Show resolved Hide resolved
}
}
if (Code) {
return llvm::createFileError(FilePath, Code);
}
PeterChou1 marked this conversation as resolved.
Show resolved Hide resolved
return llvm::Error::success();
}
ilovepi marked this conversation as resolved.
Show resolved Hide resolved

llvm::Error getDefaultAssetFiles(const char *Argv0,
clang::doc::ClangDocContext &CDCtx) {
void *MainAddr = (void *)(intptr_t)getExecutablePath;
std::string ClangDocPath = getExecutablePath(Argv0, MainAddr);
llvm::SmallString<128> NativeClangDocPath;
llvm::sys::path::native(ClangDocPath, NativeClangDocPath);

llvm::SmallString<128> AssetsPath;
AssetsPath = llvm::sys::path::parent_path(NativeClangDocPath);
llvm::sys::path::append(AssetsPath, "..", "share", "clang");
llvm::SmallString<128> DefaultStylesheet;
llvm::sys::path::native(AssetsPath, DefaultStylesheet);
llvm::sys::path::append(DefaultStylesheet,
"clang-doc-default-stylesheet.css");
llvm::SmallString<128> IndexJS;
llvm::sys::path::native(AssetsPath, IndexJS);
llvm::sys::path::append(IndexJS, "index.js");
ilovepi marked this conversation as resolved.
Show resolved Hide resolved

if (!llvm::sys::fs::is_regular_file(IndexJS)) {
return llvm::createStringError(llvm::inconvertibleErrorCode(),
"error default index.js file missing at " +
IndexJS + "\n");
}

if (!llvm::sys::fs::is_regular_file(DefaultStylesheet)) {
return llvm::createStringError(
llvm::inconvertibleErrorCode(),
"error default clang-doc-default-stylesheet.css file missing at " +
DefaultStylesheet + "\n");
}
PeterChou1 marked this conversation as resolved.
Show resolved Hide resolved

CDCtx.UserStylesheets.insert(CDCtx.UserStylesheets.begin(),
std::string(DefaultStylesheet));
CDCtx.FilesToCopy.emplace_back(IndexJS.str());

llvm::outs() << "Using default asset: " << AssetsPath << "\n";

return llvm::Error::success();
}

llvm::Error getHtmlAssetFiles(const char *Argv0,
clang::doc::ClangDocContext &CDCtx) {
if (!UserAssetPath.empty() &&
!llvm::sys::fs::is_directory(std::string(UserAssetPath))) {
llvm::outs() << "Asset path supply is not a directory: " << UserAssetPath
<< " falling back to default\n";
}
if (llvm::sys::fs::is_directory(std::string(UserAssetPath))) {
return getAssetFiles(CDCtx);
}
PeterChou1 marked this conversation as resolved.
Show resolved Hide resolved
return getDefaultAssetFiles(Argv0, CDCtx);
}

int main(int argc, const char **argv) {
llvm::sys::PrintStackTraceOnErrorSignal(argv[0]);
std::error_code OK;

const char *Overview =
R"(Generates documentation from source code and comments.
R"(Generates documentation from source code and comments.

Example usage for files without flags (default):

Expand Down Expand Up @@ -182,23 +263,11 @@ Example usage for a project using a compile commands database:
{"index.js", "index_json.js"}};

if (Format == "html") {
void *MainAddr = (void *)(intptr_t)GetExecutablePath;
std::string ClangDocPath = GetExecutablePath(argv[0], MainAddr);
llvm::SmallString<128> NativeClangDocPath;
llvm::sys::path::native(ClangDocPath, NativeClangDocPath);
llvm::SmallString<128> AssetsPath;
AssetsPath = llvm::sys::path::parent_path(NativeClangDocPath);
llvm::sys::path::append(AssetsPath, "..", "share", "clang");
llvm::SmallString<128> DefaultStylesheet;
llvm::sys::path::native(AssetsPath, DefaultStylesheet);
llvm::sys::path::append(DefaultStylesheet,
"clang-doc-default-stylesheet.css");
llvm::SmallString<128> IndexJS;
llvm::sys::path::native(AssetsPath, IndexJS);
llvm::sys::path::append(IndexJS, "index.js");
CDCtx.UserStylesheets.insert(CDCtx.UserStylesheets.begin(),
std::string(DefaultStylesheet));
CDCtx.FilesToCopy.emplace_back(IndexJS.str());
auto Err = getHtmlAssetFiles(argv[0], CDCtx);
if (Err) {
llvm::errs() << toString(std::move(Err)) << "\n";
return 1;
}
ilovepi marked this conversation as resolved.
Show resolved Hide resolved
PeterChou1 marked this conversation as resolved.
Show resolved Hide resolved
}

// Mapping phase
Expand Down
8 changes: 8 additions & 0 deletions clang-tools-extra/test/clang-doc/single-source-html.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
// RUN: rm -rf %t
// RUN: mkdir %t
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
// RUN: rm -rf %t
// RUN: mkdir %t
// RUN: rm -rf %t && mkdir %t

Copy link
Contributor

Choose a reason for hiding this comment

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

These still seem to be different steps. combining them is intentional. Actually, do you even need this directory? I don't think you do.

// RUN: echo "" > %t/compile_flags.txt
PeterChou1 marked this conversation as resolved.
Show resolved Hide resolved
// RUN: cp "%s" "%t/test.cpp"
PeterChou1 marked this conversation as resolved.
Show resolved Hide resolved
// RUN: clang-doc --format=html --executor=standalone -p %t %t/test.cpp -output=%t/docs > %t/output.txt
PeterChou1 marked this conversation as resolved.
Show resolved Hide resolved
// RUN: cat %t/output.txt | FileCheck %s --check-prefix=CHECK
PeterChou1 marked this conversation as resolved.
Show resolved Hide resolved

// CHECK: Using default asset: {{.*}}..\share\clang
Loading