llvmbot wrote:
<!--LLVM PR SUMMARY COMMENT--> @llvm/pr-subscribers-clang-tools-extra Author: Samarth Narang (snarang181) <details> <summary>Changes</summary> This patch refactors the clang-doc tool to replace manual error handling (e.g., checking error codes and calling exit()) with llvm::ExitOnError. Addresses: - https://github.com/llvm/llvm-project/issues/140085 Benefits: - Improves maintainability by reducing boilerplate error checks - Provides consistent error handling behavior across the tool - Aligns clang-doc error handling with modern LLVM patterns No functional or user-facing behavior changes are expected. - Ran `ninja check-clang-tools` to ensure no regressions Please let me know if additional reviewers or tests are needed! --- Full diff: https://github.com/llvm/llvm-project/pull/141699.diff 1 Files Affected: - (modified) clang-tools-extra/clang-doc/tool/ClangDocMain.cpp (+10-22) ``````````diff diff --git a/clang-tools-extra/clang-doc/tool/ClangDocMain.cpp b/clang-tools-extra/clang-doc/tool/ClangDocMain.cpp index 8e8f7053a8f87..e7efa4b15d80f 100644 --- a/clang-tools-extra/clang-doc/tool/ClangDocMain.cpp +++ b/clang-tools-extra/clang-doc/tool/ClangDocMain.cpp @@ -49,6 +49,7 @@ using namespace clang::ast_matchers; using namespace clang::tooling; using namespace clang; +static llvm::ExitOnError ExitOnErr; static llvm::cl::extrahelp CommonHelp(CommonOptionsParser::HelpMessage); static llvm::cl::OptionCategory ClangDocCategory("clang-doc options"); @@ -236,6 +237,8 @@ int main(int argc, const char **argv) { llvm::sys::PrintStackTraceOnErrorSignal(argv[0]); std::error_code OK; + ExitOnErr.setBanner("clang-doc error: "); + const char *Overview = R"(Generates documentation from source code and comments. @@ -259,11 +262,7 @@ Example usage for a project using a compile commands database: // Fail early if an invalid format was provided. std::string Format = getFormatString(); llvm::outs() << "Emiting docs in " << Format << " format.\n"; - auto G = doc::findGeneratorByName(Format); - if (!G) { - llvm::errs() << toString(G.takeError()) << "\n"; - return 1; - } + auto G = ExitOnErr(doc::findGeneratorByName(Format)); ArgumentsAdjuster ArgAdjuster; if (!DoxygenOnly) @@ -284,10 +283,7 @@ Example usage for a project using a compile commands database: {UserStylesheets.begin(), UserStylesheets.end()}}; if (Format == "html") { - if (auto Err = getHtmlAssetFiles(argv[0], CDCtx)) { - llvm::errs() << toString(std::move(Err)) << "\n"; - return 1; - } + ExitOnErr(getHtmlAssetFiles(argv[0], CDCtx)); } // Mapping phase @@ -300,7 +296,7 @@ Example usage for a project using a compile commands database: "these files and continue:\n" << toString(std::move(Err)) << "\n"; else { - llvm::errs() << toString(std::move(Err)) << "\n"; + ExitOnErr(std::move(Err)); return 1; } } @@ -371,22 +367,14 @@ Example usage for a project using a compile commands database: sortUsrToInfo(USRToInfo); // Ensure the root output directory exists. - if (std::error_code Err = llvm::sys::fs::create_directories(OutDirectory); - Err != std::error_code()) { - llvm::errs() << "Failed to create directory '" << OutDirectory << "'\n"; - return 1; - } + ExitOnErr( + llvm::errorCodeToError(llvm::sys::fs::create_directories(OutDirectory))); // Run the generator. llvm::outs() << "Generating docs...\n"; - if (auto Err = - G->get()->generateDocs(OutDirectory, std::move(USRToInfo), CDCtx)) { - llvm::errs() << toString(std::move(Err)) << "\n"; - return 1; - } - + ExitOnErr(G->generateDocs(OutDirectory, std::move(USRToInfo), CDCtx)); llvm::outs() << "Generating assets for docs...\n"; - Err = G->get()->createResources(CDCtx); + Err = G->createResources(CDCtx); if (Err) { llvm::outs() << "warning: " << toString(std::move(Err)) << "\n"; } `````````` </details> https://github.com/llvm/llvm-project/pull/141699 _______________________________________________ cfe-commits mailing list cfe-commits@lists.llvm.org https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits