@@ -118,6 +118,8 @@ static llvm::cl::opt<OutputFormatTy> FormatEnum(
118
118
" Documentation in mustache HTML format" )),
119
119
llvm::cl::init(OutputFormatTy::yaml), llvm::cl::cat(ClangDocCategory));
120
120
121
+ static llvm::ExitOnError ExitOnErr;
122
+
121
123
static std::string getFormatString () {
122
124
switch (FormatEnum) {
123
125
case OutputFormatTy::yaml:
@@ -245,10 +247,30 @@ sortUsrToInfo(llvm::StringMap<std::unique_ptr<doc::Info>> &USRToInfo) {
245
247
}
246
248
}
247
249
250
+ static llvm::Error handleMappingFailures (llvm::Error Err) {
251
+ if (!Err)
252
+ return llvm::Error::success ();
253
+ if (IgnoreMappingFailures) {
254
+ llvm::errs () << " Error mapping decls in files. Clang-doc will ignore these "
255
+ " files and continue:\n "
256
+ << toString (std::move (Err)) << " \n " ;
257
+ return llvm::Error::success ();
258
+ }
259
+ return Err;
260
+ }
261
+
262
+ static llvm::Error createDirectories (llvm::StringRef OutDirectory) {
263
+ if (std::error_code Err = llvm::sys::fs::create_directories (OutDirectory))
264
+ return llvm::createFileError (OutDirectory, Err);
265
+ return llvm::Error::success ();
266
+ }
267
+
248
268
int main (int argc, const char **argv) {
249
269
llvm::sys::PrintStackTraceOnErrorSignal (argv[0 ]);
250
270
std::error_code OK;
251
271
272
+ ExitOnErr.setBanner (" clang-doc error: " );
273
+
252
274
const char *Overview =
253
275
R"( Generates documentation from source code and comments.
254
276
@@ -261,22 +283,13 @@ Example usage for a project using a compile commands database:
261
283
$ clang-doc --executor=all-TUs compile_commands.json
262
284
)" ;
263
285
264
- auto Executor = clang::tooling::createExecutorFromCommandLineArgs (
265
- argc, argv, ClangDocCategory, Overview);
266
-
267
- if (!Executor) {
268
- llvm::errs () << toString (Executor.takeError ()) << " \n " ;
269
- return 1 ;
270
- }
286
+ auto Executor = ExitOnErr (clang::tooling::createExecutorFromCommandLineArgs (
287
+ argc, argv, ClangDocCategory, Overview));
271
288
272
289
// Fail early if an invalid format was provided.
273
290
std::string Format = getFormatString ();
274
291
llvm::outs () << " Emiting docs in " << Format << " format.\n " ;
275
- auto G = doc::findGeneratorByName (Format);
276
- if (!G) {
277
- llvm::errs () << toString (G.takeError ()) << " \n " ;
278
- return 1 ;
279
- }
292
+ auto G = ExitOnErr (doc::findGeneratorByName (Format));
280
293
281
294
ArgumentsAdjuster ArgAdjuster;
282
295
if (!DoxygenOnly)
@@ -286,7 +299,7 @@ Example usage for a project using a compile commands database:
286
299
ArgAdjuster);
287
300
288
301
clang::doc::ClangDocContext CDCtx = {
289
- Executor->get ()-> getExecutionContext (),
302
+ Executor->getExecutionContext (),
290
303
ProjectName,
291
304
PublicOnly,
292
305
OutDirectory,
@@ -297,40 +310,24 @@ Example usage for a project using a compile commands database:
297
310
{UserStylesheets.begin (), UserStylesheets.end ()}};
298
311
299
312
if (Format == " html" ) {
300
- if (auto Err = getHtmlAssetFiles (argv[0 ], CDCtx)) {
301
- llvm::errs () << toString (std::move (Err)) << " \n " ;
302
- return 1 ;
303
- }
313
+ ExitOnErr (getHtmlAssetFiles (argv[0 ], CDCtx));
304
314
}
305
315
306
316
if (Format == " mustache" ) {
307
- if (auto Err = getMustacheHtmlFiles (argv[0 ], CDCtx)) {
308
- llvm::errs () << toString (std::move (Err)) << " \n " ;
309
- return 1 ;
310
- }
317
+ ExitOnErr (getMustacheHtmlFiles (argv[0 ], CDCtx));
311
318
}
312
319
313
320
// Mapping phase
314
321
llvm::outs () << " Mapping decls...\n " ;
315
- auto Err =
316
- Executor->get ()->execute (doc::newMapperActionFactory (CDCtx), ArgAdjuster);
317
- if (Err) {
318
- if (IgnoreMappingFailures)
319
- llvm::errs () << " Error mapping decls in files. Clang-doc will ignore "
320
- " these files and continue:\n "
321
- << toString (std::move (Err)) << " \n " ;
322
- else {
323
- llvm::errs () << toString (std::move (Err)) << " \n " ;
324
- return 1 ;
325
- }
326
- }
322
+ ExitOnErr (handleMappingFailures (
323
+ Executor->execute (doc::newMapperActionFactory (CDCtx), ArgAdjuster)));
327
324
328
325
// Collect values into output by key.
329
326
// In ToolResults, the Key is the hashed USR and the value is the
330
327
// bitcode-encoded representation of the Info object.
331
328
llvm::outs () << " Collecting infos...\n " ;
332
329
llvm::StringMap<std::vector<StringRef>> USRToBitcode;
333
- Executor->get ()-> getToolResults ()->forEachResult (
330
+ Executor->getToolResults ()->forEachResult (
334
331
[&](StringRef Key, StringRef Value) {
335
332
USRToBitcode[Key].emplace_back (Value);
336
333
});
@@ -391,25 +388,13 @@ Example usage for a project using a compile commands database:
391
388
sortUsrToInfo (USRToInfo);
392
389
393
390
// Ensure the root output directory exists.
394
- if (std::error_code Err = llvm::sys::fs::create_directories (OutDirectory);
395
- Err != std::error_code ()) {
396
- llvm::errs () << " Failed to create directory '" << OutDirectory << " '\n " ;
397
- return 1 ;
398
- }
391
+ ExitOnErr (createDirectories (OutDirectory));
399
392
400
393
// Run the generator.
401
394
llvm::outs () << " Generating docs...\n " ;
402
- if (auto Err =
403
- G->get ()->generateDocs (OutDirectory, std::move (USRToInfo), CDCtx)) {
404
- llvm::errs () << toString (std::move (Err)) << " \n " ;
405
- return 1 ;
406
- }
407
-
395
+ ExitOnErr (G->generateDocs (OutDirectory, std::move (USRToInfo), CDCtx));
408
396
llvm::outs () << " Generating assets for docs...\n " ;
409
- Err = G->get ()->createResources (CDCtx);
410
- if (Err) {
411
- llvm::outs () << " warning: " << toString (std::move (Err)) << " \n " ;
412
- }
397
+ ExitOnErr (G->createResources (CDCtx));
413
398
414
399
return 0 ;
415
400
}
0 commit comments