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

via cfe-commits cfe-commits at lists.llvm.org
Mon Apr 29 06:14:26 PDT 2024


Author: Kohei Asano
Date: 2024-04-29T22:14:22+09:00
New Revision: de6b2b9dbf9a18e9e160cff60f7eb238909a931c

URL: https://github.com/llvm/llvm-project/commit/de6b2b9dbf9a18e9e160cff60f7eb238909a931c
DIFF: https://github.com/llvm/llvm-project/commit/de6b2b9dbf9a18e9e160cff60f7eb238909a931c.diff

LOG: [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 at gmail.com>

Added: 
    

Modified: 
    clang/docs/LibTooling.rst

Removed: 
    


################################################################################
diff  --git a/clang/docs/LibTooling.rst b/clang/docs/LibTooling.rst
index df50dcebf9b83c..87d84321ab2830 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());


        


More information about the cfe-commits mailing list