aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorKohei Asano <32860920+khei4@users.noreply.github.com>2024-04-29 22:14:22 +0900
committerGitHub <noreply@github.com>2024-04-29 22:14:22 +0900
commitde6b2b9dbf9a18e9e160cff60f7eb238909a931c (patch)
treefe07f782483625f6f3226552a822e797d8464973
parent8e17c84836b08be9a23d76c2cc234777712347de (diff)
downloadllvm-de6b2b9dbf9a18e9e160cff60f7eb238909a931c.zip
llvm-de6b2b9dbf9a18e9e160cff60f7eb238909a931c.tar.gz
llvm-de6b2b9dbf9a18e9e160cff60f7eb238909a931c.tar.bz2
[Clang][Docs] use CommonOptionsParser::create instead of protected constructor on libTooling tutorial (NFC) (#70427)
This patch fixes the code example on CommonOptionParser on https://intel.github.io/llvm-docs/clang/LibTooling.html CommonOptionParser's constructor is protected, and we can use `CommonOptionParser::create` instead of that. It seems like the LibASTMatcher tutorial already uses that. https://clang.llvm.org/docs/LibASTMatchersTutorial.html --------- Co-authored-by: Sirraide <aeternalmail@gmail.com>
-rw-r--r--clang/docs/LibTooling.rst22
1 files changed, 17 insertions, 5 deletions
diff --git a/clang/docs/LibTooling.rst b/clang/docs/LibTooling.rst
index df50dceb..87d8432 100644
--- a/clang/docs/LibTooling.rst
+++ b/clang/docs/LibTooling.rst
@@ -63,15 +63,22 @@ and automatic location of the compilation database using source files paths.
#include "llvm/Support/CommandLine.h"
using namespace clang::tooling;
+ using namespace llvm;
// Apply a custom category to all command-line options so that they are the
// only ones displayed.
- static llvm::cl::OptionCategory MyToolCategory("my-tool options");
+ static cl::OptionCategory MyToolCategory("my-tool options");
int main(int argc, const char **argv) {
- // CommonOptionsParser constructor will parse arguments and create a
- // CompilationDatabase. In case of error it will terminate the program.
- CommonOptionsParser OptionsParser(argc, argv, MyToolCategory);
+ // CommonOptionsParser::create will parse arguments and create a
+ // CompilationDatabase.
+ auto ExpectedParser = CommonOptionsParser::create(argc, argv, MyToolCategory);
+ if (!ExpectedParser) {
+ // Fail gracefully for unsupported options.
+ llvm::errs() << ExpectedParser.takeError();
+ return 1;
+ }
+ CommonOptionsParser& OptionsParser = ExpectedParser.get();
// Use OptionsParser.getCompilations() and OptionsParser.getSourcePathList()
// to retrieve CompilationDatabase and the list of input file paths.
@@ -133,7 +140,12 @@ version of this example tool is also checked into the clang tree at
static cl::extrahelp MoreHelp("\nMore help text...\n");
int main(int argc, const char **argv) {
- CommonOptionsParser OptionsParser(argc, argv, MyToolCategory);
+ auto ExpectedParser = CommonOptionsParser::create(argc, argv, MyToolCategory);
+ if (!ExpectedParser) {
+ llvm::errs() << ExpectedParser.takeError();
+ return 1;
+ }
+ CommonOptionsParser& OptionsParser = ExpectedParser.get();
ClangTool Tool(OptionsParser.getCompilations(),
OptionsParser.getSourcePathList());
return Tool.run(newFrontendActionFactory<clang::SyntaxOnlyAction>().get());