Skip to content

Commit 6fee7d0

Browse files
committed
[clang][clang-doc] address pr comments about --asset option
1 parent 85581ed commit 6fee7d0

File tree

1 file changed

+45
-21
lines changed

1 file changed

+45
-21
lines changed

clang-tools-extra/clang-doc/tool/ClangDocMain.cpp

Lines changed: 45 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -83,8 +83,8 @@ static llvm::cl::list<std::string> UserStylesheets(
8383

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

9090
static llvm::cl::opt<std::string> SourceRoot("source-root", llvm::cl::desc(R"(
@@ -137,25 +137,27 @@ std::string GetExecutablePath(const char *Argv0, void *MainAddr) {
137137
return llvm::sys::fs::getMainExecutable(Argv0, MainAddr);
138138
}
139139

140-
void GetAssetFiles(clang::doc::ClangDocContext &CDCtx) {
140+
llvm::Error GetAssetFiles(clang::doc::ClangDocContext &CDCtx) {
141141
std::error_code Code;
142-
for (auto DirIt = llvm::sys::fs::directory_iterator(
143-
std::string(UserAssetPath), Code),
144-
dir_end = llvm::sys::fs::directory_iterator();
145-
!Code && DirIt != dir_end; DirIt.increment(Code)) {
146-
llvm::SmallString<128> filePath = llvm::SmallString<128>(DirIt->path());
147-
if (llvm::sys::fs::is_regular_file(filePath)) {
148-
if (filePath.ends_with(".css")) {
142+
for (auto DirIt = llvm::sys::fs::directory_iterator(UserAssetPath, Code),
143+
DirEnd = llvm::sys::fs::directory_iterator();
144+
!Code && DirIt != DirEnd; DirIt.increment(Code)) {
145+
llvm::SmallString<128> FilePath = llvm::SmallString<128>(DirIt->path());
146+
if (!Code) {
147+
return llvm::createFileError(FilePath, Code);
148+
}
149+
if (llvm::sys::fs::is_regular_file(FilePath)) {
150+
if (llvm::sys::path::extension(FilePath) == ".css") {
149151
CDCtx.UserStylesheets.insert(CDCtx.UserStylesheets.begin(),
150-
std::string(filePath));
151-
} else if (filePath.ends_with(".js")) {
152-
CDCtx.FilesToCopy.emplace_back(filePath.str());
152+
std::string(FilePath));
153+
} else if (llvm::sys::path::extension(FilePath) == ".js") {
154+
CDCtx.FilesToCopy.emplace_back(FilePath.str());
153155
}
154156
}
155157
}
156158
}
157159

158-
void GetDefaultAssetFiles(const char *Argv0,
160+
llvm::Error GetDefaultAssetFiles(const char *Argv0,
159161
clang::doc::ClangDocContext &CDCtx) {
160162
void *MainAddr = (void *)(intptr_t)GetExecutablePath;
161163
std::string ClangDocPath = GetExecutablePath(Argv0, MainAddr);
@@ -172,12 +174,35 @@ void GetDefaultAssetFiles(const char *Argv0,
172174
llvm::SmallString<128> IndexJS;
173175
llvm::sys::path::native(AssetsPath, IndexJS);
174176
llvm::sys::path::append(IndexJS, "index.js");
177+
178+
if (!llvm::sys::fs::is_regular_file(IndexJS)) {
179+
return llvm::createStringError(llvm::inconvertibleErrorCode(),
180+
"error default index.js file at " + IndexJS + "\n");
181+
}
182+
183+
if (!llvm::sys::fs::is_regular_file(DefaultStylesheet)) {
184+
return llvm::createStringError(llvm::inconvertibleErrorCode(),
185+
"error default clang-doc-default-stylesheet.css file at " + DefaultStylesheet + "\n");
186+
}
187+
175188
CDCtx.UserStylesheets.insert(CDCtx.UserStylesheets.begin(),
176189
std::string(DefaultStylesheet));
177190
CDCtx.FilesToCopy.emplace_back(IndexJS.str());
178191

179-
llvm::outs() << "No default asset path found using default asset path: "
192+
llvm::outs() << "Using default asset: "
180193
<< AssetsPath << "\n";
194+
195+
return llvm::Error::success();
196+
}
197+
198+
llvm::Error GetHTMLAssetFiles(const char *Argv0, clang::doc::ClangDocContext &CDCtx) {
199+
if (!UserAssetPath.empty() && !llvm::sys::fs::is_directory(std::string(UserAssetPath))) {
200+
llvm::outs() << "Asset path supply is not a directory: " << UserAssetPath << " falling back to default\n";
201+
}
202+
if (llvm::sys::fs::is_directory(std::string(UserAssetPath))) {
203+
return GetAssetFiles(CDCtx);
204+
}
205+
return GetDefaultAssetFiles(Argv0, CDCtx);
181206
}
182207

183208
int main(int argc, const char **argv) {
@@ -231,12 +256,11 @@ Example usage for a project using a compile commands database:
231256
{"index.js", "index_json.js"}};
232257

233258
if (Format == "html") {
234-
if (!UserAssetPath.empty() &&
235-
llvm::sys::fs::is_directory(std::string(UserAssetPath))) {
236-
GetAssetFiles(CDCtx);
237-
} else {
238-
GetDefaultAssetFiles(argv[0], CDCtx);
239-
}
259+
auto Err = GetHTMLAssetFiles(argv[0], CDCtx);
260+
if (Err) {
261+
llvm::errs() << toString(std::move(Err)) << "\n";
262+
return 1;
263+
}
240264
}
241265

242266
// Mapping phase

0 commit comments

Comments
 (0)