[clang-tools-extra] [clang-doc] Refactor error handling to use ExitOnError (PR #141699)
Samarth Narang via cfe-commits
cfe-commits at lists.llvm.org
Wed May 28 15:51:40 PDT 2025
https://github.com/snarang181 updated https://github.com/llvm/llvm-project/pull/141699
>From b2dc4f8a50d86ecdc5fd27fedd7efde5c6882df5 Mon Sep 17 00:00:00 2001
From: Samarth Narang <snarang at umass.edu>
Date: Tue, 27 May 2025 21:32:41 -0400
Subject: [PATCH 1/2] [clang-doc] Refactor error handling to use ExitOnError
This patch replaces manual error checks and exit() calls in clang-doc
with llvm::ExitOnError for consistency and maintainability.
No functional changes to outputs or APIs.
---
.../clang-doc/tool/ClangDocMain.cpp | 32 ++++++-------------
1 file changed, 10 insertions(+), 22 deletions(-)
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";
}
>From 51238bcaf1a6450eb2b7dfbc259a1d7c4bba0a17 Mon Sep 17 00:00:00 2001
From: Samarth Narang <snarang at umass.edu>
Date: Wed, 28 May 2025 18:47:17 -0400
Subject: [PATCH 2/2] Addressing PR feedback
---
clang-tools-extra/clang-doc/tool/ClangDocMain.cpp | 12 +++++++++---
.../test/clang-doc/invalid-file-error.test | 4 ++++
2 files changed, 13 insertions(+), 3 deletions(-)
create mode 100644 clang-tools-extra/test/clang-doc/invalid-file-error.test
diff --git a/clang-tools-extra/clang-doc/tool/ClangDocMain.cpp b/clang-tools-extra/clang-doc/tool/ClangDocMain.cpp
index e7efa4b15d80f..78ad1521397e6 100644
--- a/clang-tools-extra/clang-doc/tool/ClangDocMain.cpp
+++ b/clang-tools-extra/clang-doc/tool/ClangDocMain.cpp
@@ -49,7 +49,6 @@
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");
@@ -128,6 +127,8 @@ static llvm::cl::opt<OutputFormatTy>
llvm::cl::init(OutputFormatTy::yaml),
llvm::cl::cat(ClangDocCategory));
+static llvm::ExitOnError ExitOnErr;
+
static std::string getFormatString() {
switch (FormatEnum) {
case OutputFormatTy::yaml:
@@ -367,8 +368,13 @@ Example usage for a project using a compile commands database:
sortUsrToInfo(USRToInfo);
// Ensure the root output directory exists.
- ExitOnErr(
- llvm::errorCodeToError(llvm::sys::fs::create_directories(OutDirectory)));
+ if (std::error_code Err = llvm::sys::fs::create_directories(OutDirectory);
+ Err != std::error_code()) {
+ ExitOnErr(llvm::createStringError(
+ llvm::inconvertibleErrorCode(), "Failed to create directory '%s': %s",
+ OutDirectory.c_str(), Err.message().c_str()));
+ }
+
// Run the generator.
llvm::outs() << "Generating docs...\n";
diff --git a/clang-tools-extra/test/clang-doc/invalid-file-error.test b/clang-tools-extra/test/clang-doc/invalid-file-error.test
new file mode 100644
index 0000000000000..8d391842aa452
--- /dev/null
+++ b/clang-tools-extra/test/clang-doc/invalid-file-error.test
@@ -0,0 +1,4 @@
+// RUN: not clang-doc %S/Inputs/basic-project/src/Circle.cpp -output=/root/docs 2>&1 | FileCheck %s
+
+// CHECK: clang-doc error: Failed to create directory
+
More information about the cfe-commits
mailing list