Skip to content

Commit 6169bbd

Browse files
committed
refactor command line
1 parent f1d1690 commit 6169bbd

File tree

2 files changed

+38
-108
lines changed

2 files changed

+38
-108
lines changed

include/mrdox/Config.hpp

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,8 @@
1515
#include "Generators.h"
1616
#include "Representation.h"
1717
#include <clang/Tooling/ArgumentsAdjusters.h>
18+
#include <clang/Tooling/CommonOptionsParser.h>
19+
#include <clang/Tooling/CompilationDatabase.h>
1820
#include <clang/Tooling/Execution.h>
1921
#include <llvm/ADT/Optional.h>
2022
#include <llvm/ADT/SmallVector.h>
@@ -36,6 +38,8 @@ struct Config
3638
Config(Config&&) = delete;
3739
Config& operator=(Config&&) = delete;
3840

41+
std::unique_ptr<tooling::CommonOptionsParser> options;
42+
3943
std::unique_ptr<tooling::ToolExecutor> Executor;
4044

4145
tooling::ArgumentsAdjuster ArgAdjuster;
@@ -59,8 +63,6 @@ struct Config
5963
llvm::Optional<std::string> RepositoryUrl;
6064

6165
bool IgnoreMappingFailures = false;
62-
63-
std::unique_ptr<Generator> G;
6466
};
6567

6668
} // mrdox

source/tool/ToolMain.cpp

Lines changed: 34 additions & 106 deletions
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,9 @@
3030
#include <clang/ASTMatchers/ASTMatchersInternal.h>
3131
#include <clang/Driver/Options.h>
3232
#include <clang/Frontend/FrontendActions.h>
33+
#include <clang/Tooling/AllTUsExecution.h>
3334
#include <clang/Tooling/CommonOptionsParser.h>
35+
#include <clang/Tooling/JSONCompilationDatabase.h>
3436
#include <clang/Tooling/Execution.h>
3537
#include <clang/Tooling/Tooling.h>
3638
#include <llvm/ADT/APFloat.h>
@@ -41,6 +43,7 @@
4143
#include <llvm/Support/Process.h>
4244
#include <llvm/Support/Signals.h>
4345
#include <llvm/Support/raw_ostream.h>
46+
#include <memory>
4447
#include <string>
4548

4649
// VFALCO GARBAGE
@@ -56,13 +59,10 @@ namespace {
5659
const char* Overview =
5760
R"(Generates documentation from source code and comments.
5861
59-
Example usage for files without flags (default):
62+
Examples
6063
61-
$ mrdox File1.cpp File2.cpp ... FileN.cpp
62-
63-
Example usage for a project using a compile commands database:
64-
65-
$ mrdox --executor=all-TUs compile_commands.json
64+
$ mrdox mrdox.yml
65+
$ mrdox --output ./docs mrdox.yml
6666
)";
6767

6868
static
@@ -74,13 +74,6 @@ static
7474
llvm::cl::OptionCategory
7575
ToolCategory("mrdox options");
7676

77-
static
78-
llvm::cl::opt<std::string>
79-
ProjectName(
80-
"project-name",
81-
llvm::cl::desc("Name of project."),
82-
llvm::cl::cat(ToolCategory));
83-
8477
static
8578
llvm::cl::opt<bool>
8679
IgnoreMappingFailures(
@@ -94,32 +87,7 @@ llvm::cl::opt<std::string>
9487
OutDirectory(
9588
"output",
9689
llvm::cl::desc("Directory for outputting generated files."),
97-
llvm::cl::init("docs"),
98-
llvm::cl::cat(ToolCategory));
99-
100-
static
101-
llvm::cl::opt<bool>
102-
PublicOnly(
103-
"public",
104-
llvm::cl::desc("Document only public declarations."),
105-
llvm::cl::init(false),
106-
llvm::cl::cat(ToolCategory));
107-
108-
static
109-
llvm::cl::opt<std::string>
110-
SourceRoot(
111-
"source-root", llvm::cl::desc(R"(
112-
Directory where processed files are stored.
113-
Links to definition locations will only be
114-
generated if the file is in this dir.)"),
115-
llvm::cl::cat(ToolCategory));
116-
117-
static
118-
llvm::cl::opt<std::string>
119-
RepositoryUrl(
120-
"repository", llvm::cl::desc(R"(
121-
URL of repository that hosts code.
122-
Used for links to definition locations.)"),
90+
llvm::cl::init("."),
12391
llvm::cl::cat(ToolCategory));
12492

12593
enum OutputFormatTy
@@ -158,39 +126,27 @@ getFormatString()
158126

159127
//------------------------------------------------
160128

161-
bool
162-
setupConfig(
163-
Config& cfg,
164-
llvm::SmallVectorImpl<llvm::StringRef> const& args,
165-
Reporter& R)
129+
int
130+
toolMain(int argc, const char** argv)
166131
{
167-
llvm::SmallVector<char const*> argv;
168-
for(auto const& s : args)
169-
argv.push_back(s.data());
170-
int argc = argv.size();
132+
// VFALCO GARBAGE
133+
force_xml_generator_linkage();
171134

172-
// create executor
173-
{
174-
Result rv = clang::tooling::createExecutorFromCommandLineArgs(
175-
argc, argv.begin(), ToolCategory, Overview);
176-
if(! rv)
177-
{
178-
R.fail("createExecutorFromCommandLineArgs", rv);
179-
return false;
180-
}
181-
cfg.Executor = std::move(*rv);
182-
}
135+
Config cfg;
136+
Corpus corpus;
137+
Reporter R;
138+
ErrorCode ec;
183139

184-
// Fail early if an invalid format was provided.
185-
std::string Format = getFormatString();
140+
// parse command line options
186141
{
187-
Result rv = findGeneratorByName(Format);
142+
Result rv = tooling::CommonOptionsParser::create(
143+
argc, argv, ToolCategory, llvm::cl::OneOrMore, Overview);
188144
if(! rv)
189145
{
190-
R.fail("findGeneratorByName", rv);
191-
return false;
146+
R.fail("CommonOptionsParser::create", rv);
147+
return EXIT_FAILURE;
192148
}
193-
cfg.G = std::move(*rv);
149+
cfg.options = std::make_unique<tooling::CommonOptionsParser>(std::move(*rv));
194150
}
195151

196152
/*
@@ -202,54 +158,26 @@ setupConfig(
202158
cfg.ArgAdjuster);
203159
*/
204160

205-
cfg.ProjectName = ProjectName;
206-
cfg.PublicOnly = PublicOnly;
161+
cfg.PublicOnly = true;
207162
cfg.OutDirectory = OutDirectory;
208-
cfg.SourceRoot = SourceRoot;
209-
cfg.RepositoryUrl = RepositoryUrl;
210163
cfg.IgnoreMappingFailures = IgnoreMappingFailures;
211-
cfg.OutDirectory = OutDirectory;
212164

213-
return true;
214-
}
215-
216-
bool
217-
setupConfig(
218-
Config& cfg,
219-
int argc, const char** argv,
220-
Reporter& R)
221-
{
222-
llvm::SmallVector<llvm::StringRef, 16> args;
223-
for(int i = 0; i < argc; ++i)
224-
args.push_back(argv[i]);
225-
return setupConfig(cfg, args, R);
226-
}
227-
228-
//------------------------------------------------
229-
230-
int
231-
toolMain(int argc, const char** argv)
232-
{
233-
// VFALCO GARBAGE
234-
force_xml_generator_linkage();
235-
236-
Config cfg;
237-
Corpus corpus;
238-
Reporter R;
239-
ErrorCode ec;
165+
// create the executor
166+
cfg.Executor = std::make_unique<tooling::AllTUsToolExecutor>(
167+
cfg.options->getCompilations(), 0);
240168

169+
// create the generator
170+
std::unique_ptr<Generator> G;
241171
{
242-
Result rv = tooling::CommonOptionsParser::create(
243-
argc, argv, ToolCategory, llvm::cl::OneOrMore, Overview);
172+
std::string Format = getFormatString();
173+
Result rv = findGeneratorByName(Format);
244174
if(! rv)
245175
{
246-
R.fail("CommonOptionsParser::create", rv);
247-
return EXIT_FAILURE;
176+
R.fail("findGeneratorByName", rv);
177+
return false;
248178
}
249-
179+
G = std::move(*rv);
250180
}
251-
if(! setupConfig(cfg, argc, argv, R))
252-
return EXIT_FAILURE;
253181

254182
// Extract the AST first
255183
if(llvm::Error err = doMapping(corpus, cfg))
@@ -281,7 +209,7 @@ toolMain(int argc, const char** argv)
281209

282210
// Run the generator.
283211
llvm::outs() << "Generating docs...\n";
284-
if(auto Err = cfg.G->generateDocs(
212+
if(auto Err = G->generateDocs(
285213
cfg.OutDirectory,
286214
corpus,
287215
cfg))
@@ -295,7 +223,7 @@ toolMain(int argc, const char** argv)
295223
//
296224
{
297225
llvm::outs() << "Generating assets for docs...\n";
298-
auto Err = cfg.G->createResources(cfg, corpus);
226+
auto Err = G->createResources(cfg, corpus);
299227
if (Err) {
300228
llvm::errs() << toString(std::move(Err)) << "\n";
301229
return EXIT_FAILURE;

0 commit comments

Comments
 (0)