Skip to content

Commit 521deec

Browse files
committed
Refactor visitor
1 parent 1b5a3b9 commit 521deec

File tree

12 files changed

+169
-46
lines changed

12 files changed

+169
-46
lines changed

source/lib/Mapper.h renamed to include/mrdox/BasicVisitor.hpp

Lines changed: 16 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -17,34 +17,34 @@
1717
// key is the declaration's USR and the value is the serialized bitcode.
1818
//
1919

20-
#ifndef LLVM_CLANG_TOOLS_EXTRA_CLANG_DOC_MAPPER_H
21-
#define LLVM_CLANG_TOOLS_EXTRA_CLANG_DOC_MAPPER_H
20+
#ifndef MRDOX_BASIC_VISITOR_HPP
21+
#define MRDOX_BASIC_VISITOR_HPP
2222

2323
#include "Representation.h"
2424
#include <mrdox/Config.hpp>
25-
#include "clang/AST/RecursiveASTVisitor.h"
26-
#include "clang/Tooling/Execution.h"
27-
28-
using namespace clang::comments;
29-
using namespace clang::tooling;
25+
#include <clang/AST/ASTConsumer.h>
26+
#include <clang/AST/RecursiveASTVisitor.h>
27+
#include <utility>
3028

3129
namespace clang {
3230
namespace mrdox {
3331

34-
class MapASTVisitor
35-
: public clang::RecursiveASTVisitor<MapASTVisitor>
32+
class BasicVisitor
33+
: public RecursiveASTVisitor<BasicVisitor>
3634
, public ASTConsumer
3735
{
36+
protected:
37+
Config const& cfg_;
38+
3839
public:
3940
explicit
40-
MapASTVisitor(
41-
Corpus& corpus,
42-
Config const& cfg)
43-
: corpus_(corpus)
44-
, cfg_(cfg)
41+
BasicVisitor(
42+
Config const& cfg) noexcept
43+
: cfg_(cfg)
4544
{
4645
}
4746

47+
// private
4848
void HandleTranslationUnit(ASTContext& Context) override;
4949
bool VisitNamespaceDecl(NamespaceDecl const* D);
5050
bool VisitRecordDecl(RecordDecl const* D);
@@ -55,6 +55,8 @@ class MapASTVisitor
5555
bool VisitTypeAliasDecl(TypeAliasDecl const* D);
5656

5757
private:
58+
virtual void reportResult(StringRef Key, StringRef Value) = 0;
59+
5860
template <typename T>
5961
bool mapDecl(T const* D);
6062

@@ -74,9 +76,6 @@ class MapASTVisitor
7476
getComment(
7577
NamedDecl const* D,
7678
ASTContext const& Context) const;
77-
78-
Corpus& corpus_;
79-
Config const& cfg_;
8079
};
8180

8281
} // mrdox

include/mrdox/Config.hpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -69,14 +69,14 @@ struct Config
6969
/** Set up a docs context from command line arguments.
7070
*/
7171
llvm::Error
72-
setupContext(
72+
setupConfig(
7373
Config& cfg,
7474
int argc, const char** argv);
7575

7676
/** Set up a docs context from command line arguments.
7777
*/
7878
llvm::Error
79-
setupContext(
79+
setupConfig(
8080
Config& cfg,
8181
llvm::SmallVector<llvm::StringRef, 16> const& args);
8282

source/lib/Mapper.cpp renamed to source/lib/BasicVisitor.cpp

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

12-
#include "Mapper.h"
1312
#include "BitcodeWriter.h"
1413
#include "Error.hpp"
1514
#include "Serialize.h"
15+
#include <mrdox/BasicVisitor.hpp>
1616
#ifdef _MSC_VER
1717
#pragma warning(push)
1818
#pragma warning(disable: 5054) // C5054: operator '+': deprecated between enumerations of different types
@@ -27,7 +27,7 @@ namespace clang {
2727
namespace mrdox {
2828

2929
void
30-
MapASTVisitor::
30+
BasicVisitor::
3131
HandleTranslationUnit(
3232
ASTContext& Context)
3333
{
@@ -36,7 +36,7 @@ HandleTranslationUnit(
3636

3737
template<typename T>
3838
bool
39-
MapASTVisitor::
39+
BasicVisitor::
4040
mapDecl(T const* D)
4141
{
4242
// If we're looking a decl not in user files, skip this decl.
@@ -61,50 +61,51 @@ mapDecl(T const* D)
6161
// A null in place of I indicates that the serializer is skipping this decl
6262
// for some reason (e.g. we're only reporting public decls).
6363
if (I.first)
64-
cfg_.ECtx->reportResult(
64+
reportResult(
6565
llvm::toHex(llvm::toStringRef(I.first->USR)),
6666
serialize::serialize(I.first));
6767
if (I.second)
68-
cfg_.ECtx->reportResult(
68+
reportResult(
6969
llvm::toHex(llvm::toStringRef(I.second->USR)),
7070
serialize::serialize(I.second));
71+
7172
return true;
7273
}
7374

7475
bool
75-
MapASTVisitor::
76+
BasicVisitor::
7677
VisitNamespaceDecl(
7778
NamespaceDecl const* D)
7879
{
7980
return mapDecl(D);
8081
}
8182

8283
bool
83-
MapASTVisitor::
84+
BasicVisitor::
8485
VisitRecordDecl(
8586
RecordDecl const* D)
8687
{
8788
return mapDecl(D);
8889
}
8990

9091
bool
91-
MapASTVisitor::
92+
BasicVisitor::
9293
VisitEnumDecl(
9394
EnumDecl const* D)
9495
{
9596
return mapDecl(D);
9697
}
9798

9899
bool
99-
MapASTVisitor::
100+
BasicVisitor::
100101
VisitCXXMethodDecl(
101102
CXXMethodDecl const* D)
102103
{
103104
return mapDecl(D);
104105
}
105106

106107
bool
107-
MapASTVisitor::
108+
BasicVisitor::
108109
VisitFunctionDecl(
109110
FunctionDecl const* D)
110111
{
@@ -115,21 +116,21 @@ VisitFunctionDecl(
115116
}
116117

117118
bool
118-
MapASTVisitor::
119+
BasicVisitor::
119120
VisitTypedefDecl(TypedefDecl const* D)
120121
{
121122
return mapDecl(D);
122123
}
123124

124125
bool
125-
MapASTVisitor::
126+
BasicVisitor::
126127
VisitTypeAliasDecl(TypeAliasDecl const* D)
127128
{
128129
return mapDecl(D);
129130
}
130131

131132
comments::FullComment*
132-
MapASTVisitor::
133+
BasicVisitor::
133134
getComment(
134135
NamedDecl const* D,
135136
ASTContext const& Context) const
@@ -145,7 +146,7 @@ getComment(
145146
}
146147

147148
int
148-
MapASTVisitor::
149+
BasicVisitor::
149150
getLine(
150151
NamedDecl const* D,
151152
ASTContext const& Context) const
@@ -155,7 +156,7 @@ getLine(
155156
}
156157

157158
llvm::SmallString<128>
158-
MapASTVisitor::
159+
BasicVisitor::
159160
getFile(NamedDecl const* D,
160161
ASTContext const& Context,
161162
llvm::StringRef RootDir,

source/lib/ClangDoc.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@
1616
//
1717

1818
#include "ClangDoc.h"
19-
#include "Mapper.h"
19+
#include "CorpusVisitor.hpp"
2020
#include "clang/Frontend/CompilerInstance.h"
2121
#include "clang/Frontend/FrontendAction.h"
2222

@@ -43,7 +43,7 @@ struct action
4343
clang::CompilerInstance& Compiler,
4444
llvm::StringRef InFile) override
4545
{
46-
return std::make_unique<MapASTVisitor>(corpus_, cfg_);
46+
return std::make_unique<CorpusVisitor>(corpus_, cfg_);
4747
}
4848

4949
private:

source/lib/Config.cpp

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -138,18 +138,18 @@ Config()
138138
}
139139

140140
llvm::Error
141-
setupContext(
141+
setupConfig(
142142
Config& cfg,
143143
int argc, const char** argv)
144144
{
145145
llvm::SmallVector<llvm::StringRef, 16> args;
146146
for(int i = 0; i < argc; ++i)
147147
args.push_back(argv[i]);
148-
return setupContext(cfg, args);
148+
return setupConfig(cfg, args);
149149
}
150150

151151
llvm::Error
152-
setupContext(
152+
setupConfig(
153153
Config& cfg,
154154
llvm::SmallVector<llvm::StringRef, 16> const& args)
155155
{

source/lib/CorpusVisitor.cpp

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
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+
#include "CorpusVisitor.hpp"
13+
14+
namespace clang {
15+
namespace mrdox {
16+
17+
void
18+
CorpusVisitor::
19+
reportResult(
20+
StringRef Key, StringRef Value)
21+
{
22+
cfg_.ECtx->reportResult(Key, Value);
23+
}
24+
25+
} // mrdox
26+
} // clang

source/lib/CorpusVisitor.hpp

Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
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+
//
13+
// This file implements the Mapper piece of the clang-doc tool. It implements
14+
// a RecursiveASTVisitor to look at each declaration and populate the info
15+
// into the internal representation. Each seen declaration is serialized to
16+
// to bitcode and written out to the ExecutionContext as a KV pair where the
17+
// key is the declaration's USR and the value is the serialized bitcode.
18+
//
19+
20+
#ifndef MRDOX_SOURCE_CORPUS_VISITOR_HPP
21+
#define MRDOX_SOURCE_CORPUS_VISITOR_HPP
22+
23+
#include <mrdox/BasicVisitor.hpp>
24+
25+
namespace clang {
26+
namespace mrdox {
27+
28+
/** A visitor which merges tool results into the corpus.
29+
*/
30+
class CorpusVisitor
31+
: public BasicVisitor
32+
{
33+
Corpus& corpus_;
34+
35+
public:
36+
CorpusVisitor(
37+
Corpus& corpus,
38+
Config const& cfg) noexcept
39+
: BasicVisitor(cfg)
40+
, corpus_(corpus)
41+
{
42+
}
43+
44+
private:
45+
void reportResult(StringRef Key, StringRef Value) override;
46+
};
47+
48+
} // mrdox
49+
} // clang
50+
51+
#endif

source/lib/XML.cpp

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

1212
#include "Generators.h"
13-
#include "Mapper.h"
13+
#include "CorpusVisitor.hpp"
1414
#include "Representation.h"
1515
#include <mrdox/Config.hpp>
1616
#include <clang/Tooling/Tooling.h>
@@ -566,7 +566,7 @@ renderCodeAsXML(
566566
std::unique_ptr<ASTUnit> astUnit =
567567
clang::tooling::buildASTFromCodeWithArgs(cppCode, {});
568568
Corpus corpus;
569-
MapASTVisitor visitor(corpus, cfg);
569+
CorpusVisitor visitor(corpus, cfg);
570570
visitor.HandleTranslationUnit(astUnit->getASTContext());
571571
if(llvm::Error err = buildIndex(corpus, cfg))
572572
return ! err;

source/mrdox/ToolMain.cpp

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -65,7 +65,11 @@ GetExecutablePath(
6565

6666
//------------------------------------------------
6767

68+
namespace clang {
69+
namespace mrdox {
6870

71+
} // mrdox
72+
} // clang
6973

7074
//------------------------------------------------
7175

@@ -79,7 +83,7 @@ main(int argc, const char** argv)
7983
std::error_code OK;
8084

8185
clang::mrdox::Config cfg;
82-
if(llvm::Error err = setupContext(cfg, argc, argv))
86+
if(llvm::Error err = setupConfig(cfg, argc, argv))
8387
{
8488
llvm::errs() << err << "\n";
8589
return EXIT_FAILURE;

0 commit comments

Comments
 (0)