Skip to content

Commit de6b2b9

Browse files
khei4Sirraide
andauthored
[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 <[email protected]>
1 parent 8e17c84 commit de6b2b9

File tree

1 file changed

+17
-5
lines changed

1 file changed

+17
-5
lines changed

clang/docs/LibTooling.rst

Lines changed: 17 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -63,15 +63,22 @@ and automatic location of the compilation database using source files paths.
6363
#include "llvm/Support/CommandLine.h"
6464

6565
using namespace clang::tooling;
66+
using namespace llvm;
6667

6768
// Apply a custom category to all command-line options so that they are the
6869
// only ones displayed.
69-
static llvm::cl::OptionCategory MyToolCategory("my-tool options");
70+
static cl::OptionCategory MyToolCategory("my-tool options");
7071

7172
int main(int argc, const char **argv) {
72-
// CommonOptionsParser constructor will parse arguments and create a
73-
// CompilationDatabase. In case of error it will terminate the program.
74-
CommonOptionsParser OptionsParser(argc, argv, MyToolCategory);
73+
// CommonOptionsParser::create will parse arguments and create a
74+
// CompilationDatabase.
75+
auto ExpectedParser = CommonOptionsParser::create(argc, argv, MyToolCategory);
76+
if (!ExpectedParser) {
77+
// Fail gracefully for unsupported options.
78+
llvm::errs() << ExpectedParser.takeError();
79+
return 1;
80+
}
81+
CommonOptionsParser& OptionsParser = ExpectedParser.get();
7582
7683
// Use OptionsParser.getCompilations() and OptionsParser.getSourcePathList()
7784
// 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
133140
static cl::extrahelp MoreHelp("\nMore help text...\n");
134141

135142
int main(int argc, const char **argv) {
136-
CommonOptionsParser OptionsParser(argc, argv, MyToolCategory);
143+
auto ExpectedParser = CommonOptionsParser::create(argc, argv, MyToolCategory);
144+
if (!ExpectedParser) {
145+
llvm::errs() << ExpectedParser.takeError();
146+
return 1;
147+
}
148+
CommonOptionsParser& OptionsParser = ExpectedParser.get();
137149
ClangTool Tool(OptionsParser.getCompilations(),
138150
OptionsParser.getSourcePathList());
139151
return Tool.run(newFrontendActionFactory<clang::SyntaxOnlyAction>().get());

0 commit comments

Comments
 (0)