-
Notifications
You must be signed in to change notification settings - Fork 13.6k
[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
Conversation
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 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. |
@llvm/pr-subscribers-clang-tools-extra Author: Samarth Narang (snarang181) ChangesThis patch refactors the clang-doc tool to replace manual error handling (e.g., checking error codes and calling exit()) with llvm::ExitOnError. Addresses: Benefits:
No functional or user-facing behavior changes are expected.
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:
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";
}
|
@ilovepi, requesting your review here. |
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. |
oh, can update the description? Not all of that belongs in the text of the commit body.
|
Also, thanks for the patch! 😄 |
Thanks for reviewing @ilovepi. I think I have addressed your comments, please let me know if you would like some more changes. |
✅ With the latest revision this PR passed the C/C++ code formatter. |
@ilovepi -- In the updated version, I have used the |
There was a problem hiding this 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:
- Generator name
- HTML asset file that's missing
- A mapping failure (there may already be tests that handle this)
- Failing to create directories
- Failure to generateDocs
- 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.
@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 |
Also, from the CI test run, I see the Linux build failing. This seems unrelated, right? Does it warrant a rerun?
EDIT: I see this |
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...). |
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 |
94519cb
to
3862eb9
Compare
@ilovepi, For Case 2, HTML needs a .js file. If it does not find any, it tries to use For Case 3, added two tests: one with 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 Also, addressed other changes. Let me know how it looks. |
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
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.
Perfect :)
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.
|
@ilovepi, wrapped the creation of the |
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.
Address issue with the lit tests
Some more cleanup -- redundant code removed Cleanup of test cases according to conventions
Add test case
89cc1d5
to
94ca232
Compare
in case of output dir existence too
There was a problem hiding this 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.
Thanks again @ilovepi for the handholding, appreciate it. BTW -- do you know what's going on with the Linux CI? |
I made the requested changes, can the CI workflows be triggered? I do not have access. |
@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! |
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: