Skip to content

[clang-doc] Refactor error handling to use ExitOnError #141699

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 17 commits into from
May 30, 2025

Conversation

snarang181
Copy link
Contributor

@snarang181 snarang181 commented May 28, 2025

Fixes #140085
This patch refactors the clang-doc tool to replace manual error handling (e.g., checking error codes and calling exit()) with llvm::ExitOnError.

Addresses:

Copy link

Thank you for submitting a Pull Request (PR) to the LLVM Project!

This PR will be automatically labeled and the relevant teams will be notified.

If you wish to, you can add reviewers by using the "Reviewers" section on this page.

If this is not working for you, it is probably because you do not have write permissions for the repository. In which case you can instead tag reviewers by name in a comment by using @ followed by their GitHub username.

If you have received no comments on your PR for a week, you can request a review by "ping"ing the PR by adding a comment “Ping”. The common courtesy "ping" rate is once a week. Please remember that you are asking for valuable time from other developers.

If you have further questions, they may be answered by the LLVM GitHub User Guide.

You can also ask questions in a comment on this PR, on the LLVM Discord or on the forums.

@llvmbot
Copy link
Member

llvmbot commented May 28, 2025

@llvm/pr-subscribers-clang-tools-extra

Author: Samarth Narang (snarang181)

Changes

This patch refactors the clang-doc tool to replace manual error handling (e.g., checking error codes and calling exit()) with llvm::ExitOnError.

Addresses:

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 --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";
   }

@snarang181
Copy link
Contributor Author

@ilovepi, requesting your review here.

@qcolombet qcolombet requested a review from ilovepi May 28, 2025 13:53
Copy link
Contributor

ilovepi commented May 28, 2025

Other than some nits the patch is mostly ready to go. It does need a test though, which sadly we don't already have. Please add a lit test under clang-tools-extra/tests/clang-doc.

Copy link
Contributor

ilovepi commented May 28, 2025

oh, can update the description? Not all of that belongs in the text of the commit body.

  1. Use Fixes #140085 which will allow the issue to be auto closed once this lands
  2. drop the bit at the bottom from "No functional ..." since that doesn't need to be in the commit log
  3. instead of bullet points, you can just summarize those points in a short paragraph, or omit altogether, since its linked to the bug which outlines all of that.

Copy link
Contributor

ilovepi commented May 28, 2025

Also, thanks for the patch! 😄

@snarang181
Copy link
Contributor Author

Thanks for reviewing @ilovepi. I think I have addressed your comments, please let me know if you would like some more changes.

@snarang181 snarang181 requested a review from ilovepi May 28, 2025 22:52
Copy link

github-actions bot commented May 29, 2025

✅ With the latest revision this PR passed the C/C++ code formatter.

@snarang181
Copy link
Contributor Author

@ilovepi -- In the updated version, I have used the createFileError() API and checked that the output is reliable. Also, I have renamed the lit test file; in the tests, I create a temporary file using %t and then obviously, it cannot create a directory there. Hopefully that is a reliable way of testing and we are not hardcoding any paths like /root or /dev which are not portable, also not changing permissions, which you were concerned about.
The second test I have added in the test file is of a bad format option; however, the ExitOnErr handler never gets invoked because I believe this is parsed earlier and reported as an error already.

@snarang181 snarang181 requested a review from ilovepi May 29, 2025 14:07
Copy link
Contributor

@ilovepi ilovepi left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for the work on this. I think this is getting pretty close, but I think there are more test cases required.

Tests that we need:

  1. Generator name
  2. HTML asset file that's missing
  3. A mapping failure (there may already be tests that handle this)
  4. Failing to create directories
  5. Failure to generateDocs
  6. Failure from createResources

If there are things that are more difficult to test, then we should have some TODOs in the code and file a tracking bug in the issue tracker. I think the generateDocs failures could be in the hard to test scenario w/o adding something like a test generator/backend. Something like that is out of scope for this patch, and should be handled as a follow up, so I doubt we'll get all of them in this patch. But the asset directory should be pretty easy to add another check for.

If you hadn't seen it the programmers manual has some advice for ExitOnError https://llvm.org/docs/ProgrammersManual.html#using-exitonerror-to-simplify-tool-code, and a lot to say about error handling in general.

@snarang181
Copy link
Contributor Author

@ilovepi, thanks for the patience and taking out the time to give detailed reviews. I will work on addresing your comments. One word on the tests: I was trying to test some of the other fail conditions but seems like a lot of the error conditions are caught by the parsing early on and do not actually invoke our error handler (like the current existing test case). Would adding those cases be OK regardless? Even though this patch does not directly address those issues. In that vein, it might be better to add clang-doc tests as part of a different PR, and I will be happy to work on that follow up too. Let me know what you think.

@snarang181
Copy link
Contributor Author

snarang181 commented May 29, 2025

Also, from the CI test run, I see the Linux build failing. This seems unrelated, right? Does it warrant a rerun?

/home/gha/actions-runner/_work/llvm-project/llvm-project/libc/test/src/math/smoke/HypotTest.h:31: FAILURE
Failed to match aNaN against LIBC_NAMESPACE::testing::getMatcher< LIBC_NAMESPACE::testing::TestCond::EQ>(func(sNaN, neg_inf)).
Expected floating point value: (+Infinity)
Actual floating point value: (NaN)
[  FAILED  ] LlvmLibcHypotf16Test.SpecialNumbers
Ran 1 tests.  PASS: 0  FAIL: 1/home/gha/actions-runner/_work/llvm-project/llvm-project/libc/test/src/math/smoke/HypotTest.h:31: FAILURE
Failed to match aNaN against LIBC_NAMESPACE::testing::getMatcher< LIBC_NAMESPACE::testing::TestCond::EQ>(func(sNaN, neg_inf)).
Expected floating point value: (+Infinity)
Actual floating point value: (NaN)
[  FAILED  ] LlvmLibcHypotf16Test.SpecialNumbers
Ran 1 tests.  PASS: 0  FAIL: 1

EDIT: I see this libc/src/math/generic/hypotf16.cpp was touched recently here. Might get fixed with the rebase.

@ilovepi
Copy link
Contributor

ilovepi commented May 29, 2025

Also, from the CI test run, I see the Linux build failing. This seems unrelated, right? Does it warrant a rerun?

/home/gha/actions-runner/_work/llvm-project/llvm-project/libc/test/src/math/smoke/HypotTest.h:31: FAILURE
Failed to match aNaN against LIBC_NAMESPACE::testing::getMatcher< LIBC_NAMESPACE::testing::TestCond::EQ>(func(sNaN, neg_inf)).
Expected floating point value: (+Infinity)
Actual floating point value: (NaN)
[  FAILED  ] LlvmLibcHypotf16Test.SpecialNumbers
Ran 1 tests.  PASS: 0  FAIL: 1/home/gha/actions-runner/_work/llvm-project/llvm-project/libc/test/src/math/smoke/HypotTest.h:31: FAILURE
Failed to match aNaN against LIBC_NAMESPACE::testing::getMatcher< LIBC_NAMESPACE::testing::TestCond::EQ>(func(sNaN, neg_inf)).
Expected floating point value: (+Infinity)
Actual floating point value: (NaN)
[  FAILED  ] LlvmLibcHypotf16Test.SpecialNumbers
Ran 1 tests.  PASS: 0  FAIL: 1

EDIT: I see this libc/src/math/generic/hypotf16.cpp was touched recently here. Might get fixed with the rebase.

So, what you're seeing is that main was broken by another patch. Indeed, I don't think this has anything to do with you, but if you rebase your patch, or update files, it will re-run on a newer main. Likely the failure you see has already been reverted, but its hard to say. This happens a lot, since we can't run all of our CI as part of pre-merge testing, and there are lots of downstream tests running both in our CI (buildbot) and from companies (Google, Meta, Intel, SiFive, etc...).

@ilovepi
Copy link
Contributor

ilovepi commented May 29, 2025

@ilovepi, thanks for the patience and taking out the time to give detailed reviews. I will work on addresing your comments. One word on the tests: I was trying to test some of the other fail conditions but seems like a lot of the error conditions are caught by the parsing early on and do not actually invoke our error handler (like the current existing test case). Would adding those cases be OK regardless? Even though this patch does not directly address those issues. In that vein, it might be better to add clang-doc tests as part of a different PR, and I will be happy to work on that follow up too. Let me know what you think.

Its no problem. If we don't mentor folks to be better contributors, we won't get new ones, and if people didn't take the time to explain the best practices to me in my own reviews, I wouldn't be able to do this either.

As for tests if we're exercising those cases, then its fine if another mechanism triggers the error in something like the options parsing code from cl::opt. For this patch, I'd like to see a test for each of the places we added ExitOnErr() calls (at least to the extent that's practical). If its not trivial to do so, lets add TODOs and file Issues on GitHub as necessary, so that work doesn't get lost/forgotten. If you have a follow up patch, then you can remove the TODOs as you address them.

@snarang181 snarang181 force-pushed the sn29547/clangdoc-exitonerror branch from 94519cb to 3862eb9 Compare May 30, 2025 00:26
@snarang181
Copy link
Contributor Author

Tests that we need:

  1. Generator name
  2. HTML asset file that's missing
  3. A mapping failure (there may already be tests that handle this)
  4. Failing to create directories
  5. Failure to generateDocs
  6. Failure from createResources

@ilovepi,
For Case 1, I cannot simulate an invalid generator name test case. For the invalid generator name case to be triggered, we need to first pass the valid format option, and then the generator name is checked. If we pass a bad format, we fail early (there is a test case covering this). Currently, all formats have registered generators; so maybe the bad format option is an OK test case for now?

For Case 2, HTML needs a .js file. If it does not find any, it tries to use index.js in the assets/ dir, and just warns out that it is using a fallback file. I have added a test case for this but with a warning only as the command successfully returns.

For Case 3, added two tests: one with ignore-map-errors=false which errors out with the ExitOnErr handler and the other with the default true value of ignore-map-errors which gives a map warning and continues.

For Case 4, we had a test.

For Cases 5 and 6, I am not sure on how we can test these right off the bat. All ideas I can think of involve patching the code with a hook to make a certain step fail so that we can reach that error handling condition, which of course is not possible from a CI perspective. I added a TODO in the test file.

Also, addressed other changes. Let me know how it looks.

@snarang181 snarang181 requested review from ilovepi May 30, 2025 00:28
@ilovepi
Copy link
Contributor

ilovepi commented May 30, 2025

@ilovepi, For Case 1, I cannot simulate an invalid generator name test case. For the invalid generator name case to be triggered, we need to first pass the valid format option, and then the generator name is checked. If we pass a bad format, we fail early (there is a test case covering this).

If we have coverage of that, then its fine if it doesn't trigger the precise code path. That does kind of make me wonder if we need that code at all though... there's cantFail ... but I'm hesitant to add that anywhere.

For Case 2, HTML needs a .js file. If it does not find any, it tries to use index.js in the assets/ dir, and just warns out that it is using a fallback file. I have added a test case for this but with a warning only as the command successfully returns.

Hmm, that's OK if we can't trigger the behavior, but again if we can't hit the exit case, maybe we don't need the code. For now lets leave a TODO and move on.

For Case 3, added two tests: one with ignore-map-errors=false which errors out with the ExitOnErr handler and the other with the default true value of ignore-map-errors which gives a map warning and continues.

Perfect :)

For Case 4, we had a test.

For Cases 5 and 6, I am not sure on how we can test these right off the bat. All ideas I can think of involve patching the code with a hook to make a certain step fail so that we can reach that error handling condition, which of course is not possible from a CI perspective. I added a TODO in the test file.

That's fine. There's probably some way we can structure the input to trigger those cases, but it may be hard/complex, and this patch is still an improvement over the status quo.

Also, addressed other changes. Let me know how it looks.

@snarang181
Copy link
Contributor Author

@ilovepi, wrapped the creation of the Executor in the error handler, created a helper like you suggested, and added a test case for the creation of the Executor failing.

@snarang181 snarang181 force-pushed the sn29547/clangdoc-exitonerror branch from 89cc1d5 to 94ca232 Compare May 30, 2025 11:27
@snarang181 snarang181 requested a review from ilovepi May 30, 2025 11:35
Copy link
Contributor

@ilovepi ilovepi left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think this will be fine to land after the small changes I've requested are done, and CI is passing. Thanks for helping out clang-doc, and bearing with the review process.

@snarang181
Copy link
Contributor Author

Thanks again @ilovepi for the handholding, appreciate it.

BTW -- do you know what's going on with the Linux CI?

@snarang181
Copy link
Contributor Author

I made the requested changes, can the CI workflows be triggered? I do not have access.

@ilovepi ilovepi merged commit ff94ba6 into llvm:main May 30, 2025
9 of 11 checks passed
Copy link

@snarang181 Congratulations on having your first Pull Request (PR) merged into the LLVM Project!

Your changes will be combined with recent changes from other authors, then tested by our build bots. If there is a problem with a build, you may receive a report in an email or a comment on this PR.

Please check whether problems have been caused by your change specifically, as the builds can include changes from many authors. It is not uncommon for your change to be included in a build that fails due to someone else's changes, or infrastructure issues.

How to do this, and the rest of the post-merge process, is covered in detail here.

If your change does cause a problem, it may be reverted, or you can revert it yourself. This is a normal part of LLVM development. You can fix your changes and open a new PR to merge them again.

If you don't get any reports, no action is required from you. Your changes are working as expected, well done!

@snarang181 snarang181 deleted the sn29547/clangdoc-exitonerror branch May 30, 2025 21:53
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

[clang-doc] Use ExitOnError over manually handling exit conditions
3 participants