[clang] [Clang][Docs] use CommonOptionsParser::create instead of protected constructor on libTooling tutorial (NFC) (PR #70427)

Kohei Asano via cfe-commits cfe-commits at lists.llvm.org
Mon Apr 29 04:24:23 PDT 2024


https://github.com/khei4 updated https://github.com/llvm/llvm-project/pull/70427

>From 12e29c46366e93c98c96e7561258bc83f66755c1 Mon Sep 17 00:00:00 2001
From: khei4 <kk.asano.luxy at gmail.com>
Date: Fri, 27 Oct 2023 17:46:34 +0900
Subject: [PATCH 1/2] use create instead of protected Constructor

---
 clang/docs/LibTooling.rst | 22 +++++++++++++++++-----
 1 file changed, 17 insertions(+), 5 deletions(-)

diff --git a/clang/docs/LibTooling.rst b/clang/docs/LibTooling.rst
index df50dcebf9b83c..8ef59da67417ba 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. In case of error it will terminate the program.
+    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());

>From 44b1b694f57eeb16cd6b9269bcb2177ce03c2b7c Mon Sep 17 00:00:00 2001
From: Kohei Asano <32860920+khei4 at users.noreply.github.com>
Date: Mon, 29 Apr 2024 20:24:16 +0900
Subject: [PATCH 2/2] Update clang/docs/LibTooling.rst

Co-authored-by: Sirraide <aeternalmail at gmail.com>
---
 clang/docs/LibTooling.rst | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)

diff --git a/clang/docs/LibTooling.rst b/clang/docs/LibTooling.rst
index 8ef59da67417ba..87d84321ab2830 100644
--- a/clang/docs/LibTooling.rst
+++ b/clang/docs/LibTooling.rst
@@ -71,7 +71,7 @@ and automatic location of the compilation database using source files paths.
 
   int main(int argc, const char **argv) {
     // CommonOptionsParser::create will parse arguments and create a
-    // CompilationDatabase. In case of error it will terminate the program.
+    // CompilationDatabase.
     auto ExpectedParser = CommonOptionsParser::create(argc, argv, MyToolCategory);
     if (!ExpectedParser) {
       // Fail gracefully for unsupported options.



More information about the cfe-commits mailing list