Skip to content

Commit 6e1c1fd

Browse files
committed
serializer has state
1 parent 5c76530 commit 6e1c1fd

11 files changed

+289
-216
lines changed

include/mrdox/Corpus.hpp

+10-2
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@
2020
#include <clang/Tooling/Execution.h>
2121
#include <llvm/Support/Mutex.h>
2222
#include <memory>
23+
#include <string>
2324
#include <type_traits>
2425
#include <vector>
2526

@@ -176,13 +177,20 @@ class Corpus
176177
std::shared_ptr<Config const> config,
177178
Reporter& R);
178179

179-
/** Store the Info in the tool results, keyed by SymbolID.
180+
/** Store a key/value pair in the tool results.
181+
182+
This function inserts the bitcode for the
183+
specified symbol ID into the tool results
184+
of the execution context.
185+
186+
Each symbol ID can have multiple bitcodes.
180187
*/
181188
static
182189
void
183190
reportResult(
184191
tooling::ExecutionContext& exc,
185-
Info const& I);
192+
SymbolID id,
193+
std::string bitcode);
186194

187195
private:
188196
struct Temps;

include/mrdox/Reporter.hpp

+2-1
Original file line numberDiff line numberDiff line change
@@ -27,8 +27,9 @@ namespace mrdox {
2727

2828
/** Used to check and report errors uniformly.
2929
*/
30-
struct Reporter
30+
class Reporter
3131
{
32+
public:
3233
/** Return a suitable exit code.
3334
*/
3435
int getExitCode() const noexcept;

source/lib/ast/Bitcode.hpp source/lib/Bitcode.hpp

+2-2
Original file line numberDiff line numberDiff line change
@@ -9,8 +9,8 @@
99
// Official repository: https://github.com/cppalliance/mrdox
1010
//
1111

12-
#ifndef MRDOX_SOURCE_AST_BITCODE_HPP
13-
#define MRDOX_SOURCE_AST_BITCODE_HPP
12+
#ifndef MRDOX_SOURCE_BITCODE_HPP
13+
#define MRDOX_SOURCE_BITCODE_HPP
1414

1515
#include <mrdox/MetadataFwd.hpp>
1616
#include <mrdox/Reporter.hpp>

source/lib/Corpus.cpp

+5-15
Original file line numberDiff line numberDiff line change
@@ -9,8 +9,8 @@
99
// Official repository: https://github.com/cppalliance/mrdox
1010
//
1111

12-
#include "ast/Bitcode.hpp"
13-
#include "ast/Serialize.hpp"
12+
#include "FrontendAction.hpp"
13+
#include "Bitcode.hpp"
1414
#include "meta/Reduce.hpp"
1515
#include <mrdox/Corpus.hpp>
1616
#include <mrdox/Error.hpp>
@@ -22,16 +22,6 @@
2222
namespace clang {
2323
namespace mrdox {
2424

25-
// VFALCO This is in FrontendAction.cpp
26-
/** Return a factory used to visit the AST nodes.
27-
*/
28-
std::unique_ptr<tooling::FrontendActionFactory>
29-
extern
30-
makeFrontendActionFactory(
31-
tooling::ExecutionContext& exc,
32-
Config const& config,
33-
Reporter& R);
34-
3525
struct Corpus::Temps
3626
{
3727
std::string s0;
@@ -284,11 +274,11 @@ void
284274
Corpus::
285275
reportResult(
286276
tooling::ExecutionContext& exc,
287-
Info const& I)
277+
SymbolID id,
278+
std::string bitcode)
288279
{
289-
std::string s = serialize(I);
290280
exc.reportResult(
291-
llvm::toStringRef(I.USR), std::move(s));
281+
llvm::toStringRef(id), std::move(bitcode));
292282
}
293283

294284
//------------------------------------------------

source/lib/FrontendAction.hpp

+36
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_SOURCE_FRONTENDACTION_HPP
13+
#define MRDOX_SOURCE_FRONTENDACTION_HPP
14+
15+
#include <clang/Tooling/Execution.h>
16+
#include <clang/Tooling/Tooling.h>
17+
#include <memory>
18+
19+
namespace clang {
20+
namespace mrdox {
21+
22+
class Config;
23+
class Reporter;
24+
25+
/** Return a factory used to create our visitor.
26+
*/
27+
std::unique_ptr<tooling::FrontendActionFactory>
28+
makeFrontendActionFactory(
29+
tooling::ExecutionContext& exc,
30+
Config const& config,
31+
Reporter& R);
32+
33+
} // mrdox
34+
} // clang
35+
36+
#endif

source/lib/ast/FrontendAction.cpp

+17-24
Original file line numberDiff line numberDiff line change
@@ -11,30 +11,21 @@
1111

1212
#include "Commands.hpp"
1313
#include "utility.hpp"
14-
#include "ast/Serialize.hpp"
15-
#include "ast/FrontendAction.hpp"
14+
#include "Serialize.hpp"
15+
#include "FrontendAction.hpp"
1616
#include <mrdox/Corpus.hpp>
1717
#include <clang/Index/USRGeneration.h>
1818
#include <clang/AST/RecursiveASTVisitor.h>
1919
#include <llvm/ADT/StringExtras.h>
2020
#include <llvm/Support/Error.h>
2121
#include <llvm/Support/Path.h>
2222

23-
//
24-
// This file implements the Mapper piece of the clang-doc tool. It implements
25-
// a RecursiveASTVisitor to look at each declaration and populate the info
26-
// into the internal representation. Each seen declaration is serialized to
27-
// to bitcode and written out to the ExecutionContext as a KV pair where the
28-
// key is the declaration's USR and the value is the serialized bitcode.
29-
//
30-
3123
namespace clang {
3224
namespace mrdox {
3325

3426
//------------------------------------------------
3527

36-
/* An instance of Visitor runs on one translation unit.
37-
*/
28+
// An instance of Visitor runs on one translation unit.
3829
void
3930
Visitor::
4031
HandleTranslationUnit(
@@ -45,13 +36,15 @@ HandleTranslationUnit(
4536
llvm::Optional<llvm::StringRef> filePath =
4637
Context.getSourceManager().getNonBuiltinFilenameForID(
4738
Context.getSourceManager().getMainFileID());
48-
if(filePath)
49-
{
50-
llvm::SmallString<0> s(*filePath);
51-
convert_to_slash(s);
52-
if(config_.shouldVisitTU(s))
53-
TraverseDecl(Context.getTranslationUnitDecl());
54-
}
39+
if(! filePath)
40+
return;
41+
42+
llvm::SmallString<0> s(*filePath);
43+
convert_to_slash(s);
44+
if(! config_.shouldVisitTU(s))
45+
return;
46+
47+
TraverseDecl(Context.getTranslationUnitDecl());
5548
}
5649

5750
template<typename T>
@@ -123,21 +116,21 @@ mapDecl(T const* D)
123116
// VFALCO is this right?
124117
bool const IsFileInRootDir = true;
125118

126-
auto I = buildInfoPair(
127-
D,
119+
Serializer sr(
128120
getLine(D, D->getASTContext()),
129121
filePath,
130122
IsFileInRootDir,
131-
! config_.includePrivate(),
123+
config_,
132124
R_);
125+
auto I = sr.buildInfoPair(D);
133126

134127
// A null in place of I indicates that the
135128
// serializer is skipping this decl for some
136129
// reason (e.g. we're only reporting public decls).
137130
if (I.first)
138-
Corpus::reportResult(ex_, *I.first);
131+
Corpus::reportResult(ex_, I.first->USR, serialize(*I.first));
139132
if (I.second)
140-
Corpus::reportResult(ex_, *I.second);
133+
Corpus::reportResult(ex_, I.second->USR, serialize(*I.second));
141134

142135
return true;
143136
}

source/lib/ast/FrontendAction.hpp

+13-3
Original file line numberDiff line numberDiff line change
@@ -9,8 +9,8 @@
99
// Official repository: https://github.com/cppalliance/mrdox
1010
//
1111

12-
#ifndef MRDOX_FRONTENDACTION_HPP
13-
#define MRDOX_FRONTENDACTION_HPP
12+
#ifndef MRDOX_SOURCE_AST_FRONTENDACTION_HPP
13+
#define MRDOX_SOURCE_AST_FRONTENDACTION_HPP
1414

1515
#include <mrdox/Config.hpp>
1616
#include <mrdox/Reporter.hpp>
@@ -21,7 +21,17 @@
2121
namespace clang {
2222
namespace mrdox {
2323

24-
/** Visits AST and converts it to our metadata.
24+
/** Convert AST to our metadata and serialize to bitcode.
25+
26+
An instance of this object visits the AST
27+
for exactly one translation unit. The AST is
28+
converted into our metadata, and this metadata
29+
is then serialized into bitcode. The resulting
30+
bitcode is inserted into the tool results,
31+
keyed by USR. Each USR can have multiple
32+
serialized bitcodes, as the same declaration
33+
in a particular include file can be seen by
34+
more than one translation unit.
2535
*/
2636
class Visitor
2737
: public RecursiveASTVisitor<Visitor>

0 commit comments

Comments
 (0)