Skip to content

Commit 6f5e434

Browse files
committed
use Corpus
1 parent e3948b5 commit 6f5e434

File tree

9 files changed

+166
-72
lines changed

9 files changed

+166
-72
lines changed

include/mrdox/Config.hpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -90,7 +90,7 @@ doMapping(
9090
*/
9191
llvm::Error
9292
buildIndex(
93-
Config& cfg,
93+
Config const& cfg,
9494
Corpus& corpus);
9595

9696
} // mrdox

include/mrdox/XML.hpp

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
//
2+
// This is a derivative work. originally part of the LLVM Project.
3+
// Licensed under the Apache License v2.0 with LLVM Exceptions.
4+
// See https://llvm.org/LICENSE.txt for license information.
5+
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6+
//
7+
// Copyright (c) 2023 Vinnie Falco ([email protected])
8+
//
9+
// Official repository: https://github.com/cppalliance/mrdox
10+
//
11+
12+
#ifndef MRDOX_XML_HPP
13+
#define MRDOX_XML_HPP
14+
15+
#include <mrdox/Config.hpp>
16+
#include <llvm/ADT/StringRef.h>
17+
#include <string>
18+
19+
namespace clang {
20+
namespace mrdox {
21+
22+
// Algorithms for rendering the corpus as XML.
23+
// The XML output is unaffected by the structure
24+
// of the input data and thus, represents a
25+
// canonical form.
26+
27+
bool
28+
renderCodeAsXML(
29+
std::string& xml,
30+
llvm::StringRef cppCode,
31+
Config const& cfg);
32+
33+
} // mrdox
34+
} // clang
35+
36+
#endif

source/lib/AsciidocGenerator.cpp

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -747,7 +747,7 @@ class AsciidocGenerator : public Generator
747747
llvm::Error
748748
generateDocs(
749749
StringRef RootDir,
750-
InfoMap const& Infos,
750+
Corpus const& corpus,
751751
Config const& cfg) override;
752752

753753
llvm::Error
@@ -769,15 +769,16 @@ llvm::Error
769769
AsciidocGenerator::
770770
generateDocs(
771771
StringRef RootDir,
772-
InfoMap const& Infos,
772+
Corpus const& corpus,
773773
Config const& cfg)
774774
{
775775
// Track which directories we already tried to create.
776776
llvm::StringSet<> CreatedDirs;
777777

778778
// Collect all output by file name and create the necessary directories.
779779
llvm::StringMap<std::vector<mrdox::Info*>> FileToInfos;
780-
for (const auto& Group : Infos) {
780+
for (const auto& Group : corpus.USRToInfo)
781+
{
781782
mrdox::Info* Info = Group.getValue().get();
782783

783784
llvm::SmallString<128> Path;

source/lib/Config.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -216,7 +216,7 @@ doMapping(
216216
}
217217

218218
llvm::errs() <<
219-
"GotFailure mapping decls in files. mrdox will ignore "
219+
"Error mapping decls in files. mrdox will ignore "
220220
"these files and continue:\n" <<
221221
toString(std::move(Err)) << "\n";
222222
}
@@ -225,7 +225,7 @@ doMapping(
225225

226226
llvm::Error
227227
buildIndex(
228-
Config& cfg,
228+
Config const& cfg,
229229
Corpus& corpus)
230230
{
231231
// Collect values into output by key.

source/lib/Generators.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,7 @@ class Generator
4141
llvm::Error
4242
generateDocs(
4343
StringRef RootDir,
44-
InfoMap const& Infos,
44+
Corpus const& corpus,
4545
Config const& cfg) = 0;
4646

4747
// This function writes a file with the index previously constructed.

source/lib/Mapper.h

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,7 @@ class MapASTVisitor
3838
public:
3939
explicit
4040
MapASTVisitor(
41-
Config& cfg)
41+
Config const& cfg)
4242
: cfg(cfg)
4343
{
4444
}
@@ -73,7 +73,7 @@ class MapASTVisitor
7373
NamedDecl const* D,
7474
ASTContext const& Context) const;
7575

76-
Config& cfg;
76+
Config const& cfg;
7777
};
7878

7979
} // mrdox

source/lib/XMLGenerator.cpp renamed to source/lib/XML.cpp

Lines changed: 118 additions & 61 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010
//
1111

1212
#include "Generators.h"
13+
#include "Mapper.h"
1314
#include "Representation.h"
1415
#include <mrdox/Config.hpp>
1516
#include <clang/Tooling/Tooling.h>
@@ -55,53 +56,58 @@ struct escape
5556
llvm::raw_ostream&
5657
operator<<(
5758
llvm::raw_ostream& os,
58-
escape const& t)
59+
escape const& t);
60+
61+
private:
62+
llvm::StringRef s_;
63+
};
64+
65+
llvm::raw_ostream&
66+
operator<<(
67+
llvm::raw_ostream& os,
68+
escape const& t)
69+
{
70+
std::size_t pos = 0;
71+
auto const size = t.s_.size();
72+
while(pos < size)
5973
{
60-
std::size_t pos = 0;
61-
auto const size = t.s_.size();
74+
unescaped:
75+
auto const found = t.s_.find_first_of("<>&'\"", pos);
76+
if(found == llvm::StringRef::npos)
77+
{
78+
os.write(t.s_.data() + pos, t.s_.size() - pos);
79+
break;
80+
}
81+
os.write(t.s_.data() + pos, found - pos);
82+
pos = found;
6283
while(pos < size)
6384
{
64-
unescaped:
65-
auto const found = t.s_.find_first_of("<>&'\"", pos);
66-
if(found == llvm::StringRef::npos)
85+
auto const c = t.s_[pos];
86+
switch(c)
6787
{
68-
os.write(t.s_.data() + pos, t.s_.size() - pos);
88+
case '<':
89+
os.write("&lt;", 4);
6990
break;
91+
case '>':
92+
os.write("&gt;", 4);
93+
break;
94+
case '&':
95+
os.write("&amp;", 5);
96+
break;
97+
case '\'':
98+
os.write("&apos;", 6);
99+
break;
100+
case '\"':
101+
os.write("&quot;", 6);
102+
break;
103+
default:
104+
goto unescaped;
70105
}
71-
os.write(t.s_.data() + pos, found - pos);
72-
pos = found;
73-
while(pos < size)
74-
{
75-
auto const c = t.s_[pos];
76-
switch(c)
77-
{
78-
case '<':
79-
os.write("&lt;", 4);
80-
break;
81-
case '>':
82-
os.write("&gt;", 4);
83-
break;
84-
case '&':
85-
os.write("&amp;", 5);
86-
break;
87-
case '\'':
88-
os.write("&apos;", 6);
89-
break;
90-
case '\"':
91-
os.write("&quot;", 6);
92-
break;
93-
default:
94-
goto unescaped;
95-
}
96-
++pos;
97-
}
106+
++pos;
98107
}
99-
return os;
100108
}
101-
102-
private:
103-
llvm::StringRef s_;
104-
};
109+
return os;
110+
}
105111

106112
//------------------------------------------------
107113

@@ -115,10 +121,32 @@ class XMLGenerator
115121
public:
116122
static char const* Format;
117123

124+
XMLGenerator()
125+
: cfg_([]() -> Config&
126+
{
127+
static Config c;
128+
return c;
129+
}())
130+
{
131+
}
132+
133+
explicit
134+
XMLGenerator(
135+
Config const& cfg) noexcept
136+
: cfg_(cfg)
137+
{
138+
}
139+
140+
bool
141+
render(
142+
std::string& xml,
143+
Corpus const& corpus,
144+
Config const& cfg);
145+
118146
llvm::Error
119147
generateDocs(
120148
llvm::StringRef RootDir,
121-
InfoMap const& Infos,
149+
Corpus const& corpus,
122150
Config const& cfg) override;
123151

124152
llvm::Error
@@ -155,22 +183,48 @@ class XMLGenerator
155183

156184
void writeNamespaces(std::vector<Reference> const& v);
157185
void writeRecords(std::vector<Reference> const& v);
158-
186+
187+
Config const& cfg_;
159188
std::string level_;
160-
llvm::raw_fd_ostream* os_ = nullptr;
189+
llvm::raw_ostream* os_ = nullptr;
161190
InfoMap const* infos_ = nullptr;
162191
};
163192

164193
//------------------------------------------------
165194

166-
167-
//------------------------------------------------
195+
bool
196+
XMLGenerator::
197+
render(
198+
std::string& xml,
199+
Corpus const& corpus,
200+
Config const& cfg)
201+
{
202+
xml.clear();
203+
infos_ = &corpus.USRToInfo;
204+
auto const ns = findGlobalNamespace();
205+
assert(ns != nullptr);
206+
llvm::raw_string_ostream os(xml);
207+
os_ = &os;
208+
level_ = {};
209+
#if 0
210+
*os_ <<
211+
"<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n" <<
212+
"<!DOCTYPE mrdox SYSTEM \"mrdox.dtd\">\n" <<
213+
"<mrdox>\n";
214+
#endif
215+
writeNamespace(*ns);
216+
#if 0
217+
*os_ <<
218+
"</mrdox>\n";
219+
#endif
220+
return true;
221+
}
168222

169223
llvm::Error
170224
XMLGenerator::
171225
generateDocs(
172226
llvm::StringRef RootDir,
173-
InfoMap const& Infos,
227+
Corpus const& corpus,
174228
Config const& cfg)
175229
{
176230
llvm::SmallString<256> filename(cfg.OutDirectory);
@@ -184,7 +238,7 @@ generateDocs(
184238
return llvm::createStringError(
185239
llvm::inconvertibleErrorCode(),
186240
"Output file is not regular");
187-
infos_ = &Infos;
241+
infos_ = &corpus.USRToInfo;
188242
auto const ns = findGlobalNamespace();
189243
if(! ns)
190244
return llvm::createStringError(
@@ -211,7 +265,7 @@ generateDocs(
211265
#endif
212266
// VFALCO the error handling needs to be
213267
// propagated through all write functions
214-
if(os_->error())
268+
if(os.error())
215269
return llvm::createStringError(
216270
ec,
217271
"output stream failure");
@@ -503,26 +557,29 @@ Format = "xml";
503557

504558
//------------------------------------------------
505559

506-
#if 0
507-
llvm::Expected<llvm::Twine>
508-
renderXML(
509-
llvm::StringRef fileName)
560+
bool
561+
renderCodeAsXML(
562+
std::string& xml,
563+
llvm::StringRef cppCode,
564+
Config const& cfg)
510565
{
511-
if(! path::extension(fileName).equals_insensitive(".cpp"))
512-
return llvm::createStringError(
513-
llvm::inconvertibleErrorCode(),
514-
"not a .cpp file");
515-
llvm::SmallString<256> dir(
516-
path::parent_path(fileName));
517-
518-
return llvm::Twine();
566+
std::unique_ptr<ASTUnit> astUnit =
567+
clang::tooling::buildASTFromCodeWithArgs(cppCode, {});
568+
MapASTVisitor visitor(cfg);
569+
visitor.HandleTranslationUnit(astUnit->getASTContext());
570+
Corpus corpus;
571+
if(llvm::Error err = buildIndex(cfg, corpus))
572+
return ! err;
573+
XMLGenerator(cfg).render(xml, corpus, cfg);
574+
575+
return true;
519576
}
520-
#endif
521577

522578
//------------------------------------------------
523579

524580
static
525-
GeneratorRegistry::Add<XMLGenerator>
581+
GeneratorRegistry::
582+
Add<XMLGenerator>
526583
xmlGenerator(
527584
XMLGenerator::Format,
528585
"Generator for XML output.");

source/mrdox/ToolMain.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -118,7 +118,7 @@ main(int argc, const char** argv)
118118
llvm::outs() << "Generating docs...\n";
119119
if(auto Err = cfg.G->generateDocs(
120120
cfg.OutDirectory,
121-
std::move(corpus.USRToInfo),
121+
corpus,
122122
cfg))
123123
{
124124
llvm::errs() << toString(std::move(Err)) << "\n";

source/tests/TestMain.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -129,7 +129,7 @@ do_main(int argc, const char** argv)
129129
expectedXml = xmlResult->get()->getBuffer();
130130

131131
std::unique_ptr<ASTUnit> astUnit =
132-
clang::tooling::buildASTFromCodeWithArgs(cppCode, Args);
132+
clang::tooling::buildASTFromCodeWithArgs(cppCode, {});
133133
MapASTVisitor visitor(cfg);
134134
visitor.HandleTranslationUnit(astUnit->getASTContext());
135135
Corpus corpus;

0 commit comments

Comments
 (0)