Skip to content

Commit 49da327

Browse files
committed
Wrap executor in ExitOnErr and modify uses
Add test case
1 parent 1a41b0f commit 49da327

File tree

2 files changed

+27
-19
lines changed

2 files changed

+27
-19
lines changed

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

Lines changed: 22 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -247,6 +247,22 @@ sortUsrToInfo(llvm::StringMap<std::unique_ptr<doc::Info>> &USRToInfo) {
247247
}
248248
}
249249

250+
llvm::Error runMappingPhase(tooling::ToolExecutor *Executor,
251+
clang::doc::ClangDocContext &CDCtx, tooling::ArgumentsAdjuster &ArgAdjuster, bool IgnoreMappingFailures ) {
252+
auto Err =
253+
Executor->execute(doc::newMapperActionFactory(CDCtx), ArgAdjuster);
254+
if (Err) {
255+
if (IgnoreMappingFailures) {
256+
llvm::errs() << "Error mapping decls in files. Clang-doc will ignore "
257+
"these files and continue:\n"
258+
<< toString(std::move(Err)) << "\n";
259+
return llvm::Error::success();
260+
}
261+
return Err;
262+
}
263+
return llvm::Error::success();
264+
}
265+
250266
int main(int argc, const char **argv) {
251267
llvm::sys::PrintStackTraceOnErrorSignal(argv[0]);
252268
std::error_code OK;
@@ -265,13 +281,10 @@ Example usage for a project using a compile commands database:
265281
$ clang-doc --executor=all-TUs compile_commands.json
266282
)";
267283

268-
auto Executor = clang::tooling::createExecutorFromCommandLineArgs(
269-
argc, argv, ClangDocCategory, Overview);
284+
auto Executor = ExitOnErr(clang::tooling::createExecutorFromCommandLineArgs(
285+
argc, argv, ClangDocCategory, Overview));
286+
270287

271-
if (!Executor) {
272-
llvm::errs() << toString(Executor.takeError()) << "\n";
273-
return 1;
274-
}
275288

276289
// Fail early if an invalid format was provided.
277290
std::string Format = getFormatString();
@@ -286,7 +299,7 @@ Example usage for a project using a compile commands database:
286299
ArgAdjuster);
287300

288301
clang::doc::ClangDocContext CDCtx = {
289-
Executor->get()->getExecutionContext(),
302+
Executor->getExecutionContext(),
290303
ProjectName,
291304
PublicOnly,
292305
OutDirectory,
@@ -306,24 +319,14 @@ Example usage for a project using a compile commands database:
306319

307320
// Mapping phase
308321
llvm::outs() << "Mapping decls...\n";
309-
auto Err =
310-
Executor->get()->execute(doc::newMapperActionFactory(CDCtx), ArgAdjuster);
311-
if (Err) {
312-
if (IgnoreMappingFailures)
313-
llvm::errs() << "Error mapping decls in files. Clang-doc will ignore "
314-
"these files and continue:\n"
315-
<< toString(std::move(Err)) << "\n";
316-
else {
317-
ExitOnErr(std::move(Err));
318-
}
319-
}
322+
ExitOnErr(runMappingPhase(Executor.get(), CDCtx, ArgAdjuster, IgnoreMappingFailures));
320323

321324
// Collect values into output by key.
322325
// In ToolResults, the Key is the hashed USR and the value is the
323326
// bitcode-encoded representation of the Info object.
324327
llvm::outs() << "Collecting infos...\n";
325328
llvm::StringMap<std::vector<StringRef>> USRToBitcode;
326-
Executor->get()->getToolResults()->forEachResult(
329+
Executor->getToolResults()->forEachResult(
327330
[&](StringRef Key, StringRef Value) {
328331
USRToBitcode[Key].emplace_back(Value);
329332
});

clang-tools-extra/test/clang-doc/invalid-options.cpp

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,4 +21,9 @@
2121
// RUN: clang-doc %t/nonexistent.cpp 2>&1 | FileCheck %s --check-prefix=MAP-WARN
2222
// MAP-WARN: Error mapping decls in files. Clang-doc will ignore these files and continue
2323

24+
///Invalid executor type
25+
// RUN: not clang-doc --executor=invalid %s 2>&1 | FileCheck %s --check-prefix=EXECUTOR-FAIL
26+
// EXECUTOR-FAIL: clang-doc error:
27+
// EXECUTOR-FAIL: Executor "invalid" is not registered
28+
2429
///TODO: Add tests for failures in generateDocs() and in createResources().

0 commit comments

Comments
 (0)