[Tooling][clang-tools-extra] Consume CommonOptionsParser errors in tools (#193675)

Several LibTooling-based tools printed errors returned from
CommonOptionsParser::create() directly. However, printing an
`llvm::Error` does not consume it, so argument parsing failures such as
unknown tool options could abort. This commit fixes the problem.

Closes https://github.com/llvm/llvm-project/issues/183009
This commit is contained in:
Zeyi Xu
2026-04-24 13:25:26 +08:00
committed by GitHub
parent aadf3959eb
commit 3041708a17
16 changed files with 39 additions and 11 deletions

View File

@@ -102,7 +102,7 @@ int main(int argc, const char **argv) {
auto ExpectedParser = auto ExpectedParser =
tooling::CommonOptionsParser::create(argc, argv, ChangeNamespaceCategory); tooling::CommonOptionsParser::create(argc, argv, ChangeNamespaceCategory);
if (!ExpectedParser) { if (!ExpectedParser) {
llvm::errs() << ExpectedParser.takeError(); llvm::errs() << llvm::toString(ExpectedParser.takeError());
return 1; return 1;
} }
tooling::CommonOptionsParser &OptionsParser = ExpectedParser.get(); tooling::CommonOptionsParser &OptionsParser = ExpectedParser.get();

View File

@@ -131,7 +131,7 @@ int main(int argc, const char **argv) {
auto ExpectedParser = auto ExpectedParser =
CommonOptionsParser::create(argc, argv, FindAllSymbolsCategory); CommonOptionsParser::create(argc, argv, FindAllSymbolsCategory);
if (!ExpectedParser) { if (!ExpectedParser) {
llvm::errs() << ExpectedParser.takeError(); llvm::errs() << llvm::toString(ExpectedParser.takeError());
return 1; return 1;
} }

View File

@@ -264,7 +264,7 @@ int includeFixerMain(int argc, const char **argv) {
auto ExpectedParser = auto ExpectedParser =
tooling::CommonOptionsParser::create(argc, argv, IncludeFixerCategory); tooling::CommonOptionsParser::create(argc, argv, IncludeFixerCategory);
if (!ExpectedParser) { if (!ExpectedParser) {
llvm::errs() << ExpectedParser.takeError(); llvm::errs() << llvm::toString(ExpectedParser.takeError());
return 1; return 1;
} }
tooling::CommonOptionsParser &options = ExpectedParser.get(); tooling::CommonOptionsParser &options = ExpectedParser.get();

View File

@@ -99,7 +99,7 @@ int main(int argc, const char **argv) {
auto ExpectedParser = auto ExpectedParser =
tooling::CommonOptionsParser::create(argc, argv, ClangMoveCategory); tooling::CommonOptionsParser::create(argc, argv, ClangMoveCategory);
if (!ExpectedParser) { if (!ExpectedParser) {
llvm::errs() << ExpectedParser.takeError(); llvm::errs() << llvm::toString(ExpectedParser.takeError());
return 1; return 1;
} }
tooling::CommonOptionsParser &OptionsParser = ExpectedParser.get(); tooling::CommonOptionsParser &OptionsParser = ExpectedParser.get();

View File

@@ -53,7 +53,7 @@ int main(int argc, const char **argv) {
auto ExpectedParser = tooling::CommonOptionsParser::create( auto ExpectedParser = tooling::CommonOptionsParser::create(
argc, argv, ClangReorderFieldsCategory, cl::OneOrMore, Usage); argc, argv, ClangReorderFieldsCategory, cl::OneOrMore, Usage);
if (!ExpectedParser) { if (!ExpectedParser) {
llvm::errs() << ExpectedParser.takeError(); llvm::errs() << llvm::toString(ExpectedParser.takeError());
return 1; return 1;
} }

View File

@@ -337,9 +337,9 @@ Changes in existing checks
- Improved :doc:`cppcoreguidelines-missing-std-forward - Improved :doc:`cppcoreguidelines-missing-std-forward
<clang-tidy/checks/cppcoreguidelines/missing-std-forward>` check: <clang-tidy/checks/cppcoreguidelines/missing-std-forward>` check:
- Fixed false positive for constrained template parameters - Fixed false positive for constrained template parameters
- Fixed false positive with ``std::forward`` in brace-init and paren-init - Fixed false positive with ``std::forward`` in brace-init and paren-init
lambda captures such as ``[t{std::forward<T>(t)}]``. lambda captures such as ``[t{std::forward<T>(t)}]``.
@@ -513,7 +513,7 @@ Changes in existing checks
- Fixed incorrect naming style application to C++17 structured bindings. - Fixed incorrect naming style application to C++17 structured bindings.
- Fixed a false positive where function templates could be diagnosed as generic - Fixed a false positive where function templates could be diagnosed as generic
identifiers when `DefaultCase` was enabled. identifiers when `DefaultCase` was enabled.
- Improved :doc:`readability-implicit-bool-conversion - Improved :doc:`readability-implicit-bool-conversion
@@ -581,6 +581,8 @@ Improvements to include-fixer
Improvements to clang-include-fixer Improvements to clang-include-fixer
----------------------------------- -----------------------------------
- Fixed crashes when command-line argument parsing failed at unknown tool options.
Improvements to modularize Improvements to modularize
-------------------------- --------------------------

View File

@@ -0,0 +1,4 @@
// RUN: not clang-change-namespace --nonsense %s -- 2>&1 | FileCheck %s
// CHECK: clang-change-namespace{{(\.exe)?}}: Unknown command line argument '--nonsense'
int main() { return 0; }

View File

@@ -0,0 +1,4 @@
// RUN: not clang-include-fixer --nonsense %s -- 2>&1 | FileCheck %s
// CHECK: clang-include-fixer{{(\.exe)?}}: Unknown command line argument '--nonsense'
int main() { return 0; }

View File

@@ -0,0 +1,4 @@
// RUN: not find-all-symbols --nonsense %s -- 2>&1 | FileCheck %s
// CHECK: find-all-symbols{{(\.exe)?}}: Unknown command line argument '--nonsense'
int main() { return 0; }

View File

@@ -0,0 +1,4 @@
// RUN: not clang-move --nonsense %s -- 2>&1 | FileCheck %s
// CHECK: clang-move{{(\.exe)?}}: Unknown command line argument '--nonsense'
int main() { return 0; }

View File

@@ -0,0 +1,4 @@
// RUN: not clang-reorder-fields --nonsense %s -- 2>&1 | FileCheck %s
// CHECK: clang-reorder-fields{{(\.exe)?}}: Unknown command line argument '--nonsense'
int main() { return 0; }

View File

@@ -57,7 +57,7 @@ namespace tooling {
/// auto ExpectedParser = /// auto ExpectedParser =
/// CommonOptionsParser::create(argc, argv, MyToolCategory); /// CommonOptionsParser::create(argc, argv, MyToolCategory);
/// if (!ExpectedParser) { /// if (!ExpectedParser) {
/// llvm::errs() << ExpectedParser.takeError(); /// llvm::errs() << llvm::toString(ExpectedParser.takeError());
/// return 1; /// return 1;
/// } /// }
/// CommonOptionsParser& OptionsParser = ExpectedParser.get(); /// CommonOptionsParser& OptionsParser = ExpectedParser.get();

View File

@@ -0,0 +1,2 @@
// RUN: not clang-refactor --nonsense 2>&1 | FileCheck %s
// CHECK: clang-refactor{{(\.exe)?}}: Unknown command line argument '--nonsense'

View File

@@ -0,0 +1,4 @@
// RUN: not clang-check --nonsense %s -- 2>&1 | FileCheck %s
// CHECK: clang-check{{(\.exe)?}}: Unknown command line argument '--nonsense'
int main() { return 0; }

View File

@@ -203,7 +203,7 @@ int main(int argc, const char **argv) {
auto ExpectedParser = auto ExpectedParser =
CommonOptionsParser::create(argc, argv, ClangCheckCategory); CommonOptionsParser::create(argc, argv, ClangCheckCategory);
if (!ExpectedParser) { if (!ExpectedParser) {
llvm::errs() << ExpectedParser.takeError(); llvm::errs() << llvm::toString(ExpectedParser.takeError());
return 1; return 1;
} }
CommonOptionsParser &OptionsParser = ExpectedParser.get(); CommonOptionsParser &OptionsParser = ExpectedParser.get();

View File

@@ -617,7 +617,7 @@ int main(int argc, const char **argv) {
argc, argv, cl::getGeneralCategory(), cl::ZeroOrMore, argc, argv, cl::getGeneralCategory(), cl::ZeroOrMore,
"Clang-based refactoring tool for C, C++ and Objective-C"); "Clang-based refactoring tool for C, C++ and Objective-C");
if (!ExpectedParser) { if (!ExpectedParser) {
llvm::errs() << ExpectedParser.takeError(); llvm::errs() << llvm::toString(ExpectedParser.takeError());
return 1; return 1;
} }
CommonOptionsParser &Options = ExpectedParser.get(); CommonOptionsParser &Options = ExpectedParser.get();