Skip to content

Commit

Permalink
[Clang][Docs] use CommonOptionsParser::create instead of protected co…
Browse files Browse the repository at this point in the history
…nstructor 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>
  • Loading branch information
khei4 and Sirraide authored Apr 29, 2024
1 parent 8e17c84 commit de6b2b9
Showing 1 changed file with 17 additions and 5 deletions.
22 changes: 17 additions & 5 deletions clang/docs/LibTooling.rst
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down Expand Up @@ -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());
Expand Down

0 comments on commit de6b2b9

Please sign in to comment.