Skip to content

Commit fd3bef1

Browse files
committed
more CDCtx
1 parent 3a5b9ad commit fd3bef1

File tree

6 files changed

+214
-150
lines changed

6 files changed

+214
-150
lines changed

include/mrdox/ClangDocContext.hpp

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,8 +12,12 @@
1212
#ifndef MRDOX_CLANGDOCCONTEXT_HPP
1313
#define MRDOX_CLANGDOCCONTEXT_HPP
1414

15+
#include "Generators.h"
1516
#include "Representation.h"
17+
#include <clang/Tooling/ArgumentsAdjusters.h>
1618
#include <clang/Tooling/Execution.h>
19+
#include <llvm/ADT/SmallVector.h>
20+
#include <llvm/ADT/StringRef.h>
1721
#include <memory>
1822
#include <string>
1923

@@ -33,6 +37,8 @@ struct ClangDocContext
3337

3438
std::unique_ptr<tooling::ToolExecutor> Executor;
3539

40+
tooling::ArgumentsAdjuster ArgAdjuster;
41+
3642
tooling::ExecutionContext* ECtx;
3743

3844
// Name of project being documented.
@@ -53,9 +59,27 @@ struct ClangDocContext
5359
// for links to definition locations.
5460
std::optional<std::string> RepositoryUrl;
5561

62+
bool IgnoreMappingFailures = false;
63+
64+
std::unique_ptr<Generator> G;
65+
5666
Index Idx;
5767
};
5868

69+
/** Set up a docs context from command line arguments.
70+
*/
71+
llvm::Error
72+
setupContext(
73+
ClangDocContext& CDCtx,
74+
int argc, const char** argv);
75+
76+
/** Set up a docs context from command line arguments.
77+
*/
78+
llvm::Error
79+
setupContext(
80+
ClangDocContext& CDCtx,
81+
llvm::SmallVector<llvm::StringRef, 16> const& args);
82+
5983
} // namespace mrdox
6084
} // namespace clang
6185

source/lib/AsciidocGenerator.cpp

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
// Official repository: https://github.com/cppalliance/mrdox
1010
//
1111

12+
#include <mrdox/ClangDocContext.hpp>
1213
#include "Generators.h"
1314
#include "Representation.h"
1415
#include "llvm/ADT/StringRef.h"

source/lib/ClangDocContext.cpp

Lines changed: 179 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,11 +9,136 @@
99
// Official repository: https://github.com/cppalliance/mrdox
1010
//
1111

12+
#include "Generators.h"
1213
#include <mrdox/ClangDocContext.hpp>
14+
#include <clang/Tooling/CommonOptionsParser.h>
1315

1416
namespace clang {
1517
namespace mrdox {
1618

19+
namespace {
20+
21+
const char* Overview =
22+
R"(Generates documentation from source code and comments.
23+
24+
Example usage for files without flags (default):
25+
26+
$ mrdox File1.cpp File2.cpp ... FileN.cpp
27+
28+
Example usage for a project using a compile commands database:
29+
30+
$ mrdox --executor=all-TUs compile_commands.json
31+
)";
32+
33+
static
34+
llvm::cl::extrahelp
35+
CommonHelp(
36+
tooling::CommonOptionsParser::HelpMessage);
37+
38+
static
39+
llvm::cl::OptionCategory
40+
MrDoxCategory("mrdox options");
41+
42+
static
43+
llvm::cl::opt<std::string>
44+
ProjectName(
45+
"project-name",
46+
llvm::cl::desc("Name of project."),
47+
llvm::cl::cat(MrDoxCategory));
48+
49+
static
50+
llvm::cl::opt<bool>
51+
IgnoreMappingFailures(
52+
"ignore-map-errors",
53+
llvm::cl::desc("Continue if files are not mapped correctly."),
54+
llvm::cl::init(true),
55+
llvm::cl::cat(MrDoxCategory));
56+
57+
static
58+
llvm::cl::opt<std::string>
59+
OutDirectory(
60+
"output",
61+
llvm::cl::desc("Directory for outputting generated files."),
62+
llvm::cl::init("docs"),
63+
llvm::cl::cat(MrDoxCategory));
64+
65+
static
66+
llvm::cl::opt<bool>
67+
PublicOnly(
68+
"public",
69+
llvm::cl::desc("Document only public declarations."),
70+
llvm::cl::init(false),
71+
llvm::cl::cat(MrDoxCategory));
72+
73+
static
74+
llvm::cl::opt<bool>
75+
DoxygenOnly(
76+
"doxygen",
77+
llvm::cl::desc("Use only doxygen-style comments to generate docs."),
78+
llvm::cl::init(false),
79+
llvm::cl::cat(MrDoxCategory));
80+
81+
static
82+
llvm::cl::list<std::string>
83+
UserStylesheets(
84+
"stylesheets",
85+
llvm::cl::CommaSeparated,
86+
llvm::cl::desc("CSS stylesheets to extend the default styles."),
87+
llvm::cl::cat(MrDoxCategory));
88+
89+
static
90+
llvm::cl::opt<std::string>
91+
SourceRoot(
92+
"source-root", llvm::cl::desc(R"(
93+
Directory where processed files are stored.
94+
Links to definition locations will only be
95+
generated if the file is in this dir.)"),
96+
llvm::cl::cat(MrDoxCategory));
97+
98+
static
99+
llvm::cl::opt<std::string>
100+
RepositoryUrl(
101+
"repository", llvm::cl::desc(R"(
102+
URL of repository that hosts code.
103+
Used for links to definition locations.)"),
104+
llvm::cl::cat(MrDoxCategory));
105+
106+
enum OutputFormatTy
107+
{
108+
adoc,
109+
xml
110+
};
111+
112+
static
113+
llvm::cl::opt<OutputFormatTy>
114+
FormatEnum(
115+
"format",
116+
llvm::cl::desc("Format for outputted docs."),
117+
llvm::cl::values(
118+
clEnumValN(OutputFormatTy::adoc, "adoc",
119+
"Documentation in Asciidoc format."),
120+
clEnumValN(OutputFormatTy::xml, "xml",
121+
"Documentation in XML format.")),
122+
llvm::cl::init(OutputFormatTy::adoc), // default value
123+
llvm::cl::cat(MrDoxCategory));
124+
125+
std::string
126+
getFormatString()
127+
{
128+
switch (FormatEnum)
129+
{
130+
case OutputFormatTy::adoc:
131+
return "adoc";
132+
case OutputFormatTy::xml:
133+
return "xml";
134+
}
135+
llvm_unreachable("Unknown OutputFormatTy");
136+
}
137+
138+
} // (anon)
139+
140+
//------------------------------------------------
141+
17142
ClangDocContext::
18143
ClangDocContext()
19144
{
@@ -22,5 +147,59 @@ ClangDocContext()
22147
SourceRoot = std::string(SourceRootDir.str());
23148
}
24149

150+
llvm::Error
151+
setupContext(
152+
ClangDocContext& CDCtx,
153+
int argc, const char** argv)
154+
{
155+
llvm::SmallVector<llvm::StringRef, 16> args;
156+
for(int i = 0; i < argc; ++i)
157+
args.push_back(argv[i]);
158+
return setupContext(CDCtx, args);
159+
}
160+
161+
llvm::Error
162+
setupContext(
163+
ClangDocContext& CDCtx,
164+
llvm::SmallVector<llvm::StringRef, 16> const& args)
165+
{
166+
llvm::SmallVector<char const*> argv;
167+
for(auto const& s : args)
168+
argv.push_back(s.data());
169+
int argc = argv.size();
170+
if(llvm::Error err = clang::tooling::createExecutorFromCommandLineArgs(
171+
argc,
172+
argv.begin(),
173+
MrDoxCategory,
174+
Overview).moveInto(CDCtx.Executor))
175+
{
176+
return err;
177+
}
178+
179+
// Fail early if an invalid format was provided.
180+
std::string Format = getFormatString();
181+
llvm::outs() << "Emiting docs in " << Format << " format.\n";
182+
if(llvm::Error err =
183+
mrdox::findGeneratorByName(Format).moveInto(CDCtx.G))
184+
return err;
185+
186+
if (! DoxygenOnly)
187+
CDCtx.ArgAdjuster = tooling::combineAdjusters(
188+
getInsertArgumentAdjuster(
189+
"-fparse-all-comments",
190+
tooling::ArgumentInsertPosition::END),
191+
CDCtx.ArgAdjuster);
192+
193+
CDCtx.ECtx = CDCtx.Executor->getExecutionContext(),
194+
CDCtx.ProjectName = ProjectName;
195+
CDCtx.PublicOnly = PublicOnly;
196+
CDCtx.OutDirectory = OutDirectory;
197+
CDCtx.SourceRoot = SourceRoot;
198+
CDCtx.RepositoryUrl = RepositoryUrl;
199+
CDCtx.IgnoreMappingFailures = IgnoreMappingFailures;
200+
CDCtx.OutDirectory = OutDirectory;
201+
return llvm::Error::success();
202+
}
203+
25204
} // mrdox
26205
} // clang

source/lib/Generators.h

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,13 +16,14 @@
1616
#define LLVM_CLANG_TOOLS_EXTRA_CLANG_DOC_GENERATOR_H
1717

1818
#include "Representation.h"
19-
#include <mrdox/ClangDocContext.hpp>
2019
#include "llvm/Support/Error.h"
2120
#include "llvm/Support/Registry.h"
2221

2322
namespace clang {
2423
namespace mrdox {
2524

25+
struct ClangDocContext;
26+
2627
// Abstract base class for generators.
2728
// This is expected to be implemented and exposed via the GeneratorRegistry.
2829
class Generator {

source/lib/XMLGenerator.cpp

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313

1414
#include "Generators.h"
1515
#include "Representation.h"
16+
#include <mrdox/ClangDocContext.hpp>
1617
#include "clang/Tooling/Tooling.h"
1718
#include "llvm/ADT/StringRef.h"
1819
#include "llvm/Support/FileSystem.h"

0 commit comments

Comments
 (0)