Skip to content

Commit 69dc304

Browse files
marsupialvgvassilev
authored andcommitted
Hide DeclCollectorPPAdapter and use DeclCollector for setup.
No need for these details to be public.
1 parent 77afad9 commit 69dc304

File tree

3 files changed

+50
-67
lines changed

3 files changed

+50
-67
lines changed

interpreter/cling/lib/Interpreter/DeclCollector.cpp

Lines changed: 40 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818
#include "clang/AST/DeclCXX.h"
1919
#include "clang/AST/DeclGroup.h"
2020
#include "clang/Lex/MacroInfo.h"
21+
#include "clang/Lex/Preprocessor.h"
2122
#include "clang/Lex/Token.h"
2223

2324
using namespace clang;
@@ -59,6 +60,45 @@ namespace {
5960
}
6061

6162
namespace cling {
63+
///\brief Serves as DeclCollector's connector to the PPCallbacks interface.
64+
///
65+
class DeclCollector::PPAdapter : public clang::PPCallbacks {
66+
cling::DeclCollector* m_Parent;
67+
68+
void MacroDirective(const clang::Token& MacroNameTok,
69+
const clang::MacroDirective* MD) {
70+
assert(m_Parent->m_CurTransaction && "Missing transction");
71+
Transaction::MacroDirectiveInfo MDE(MacroNameTok.getIdentifierInfo(), MD);
72+
m_Parent->m_CurTransaction->append(MDE);
73+
}
74+
75+
public:
76+
PPAdapter(cling::DeclCollector* P) : m_Parent(P) {}
77+
78+
/// \name PPCallbacks overrides
79+
/// Macro support
80+
void MacroDefined(const clang::Token& MacroNameTok,
81+
const clang::MacroDirective* MD) final {
82+
MacroDirective(MacroNameTok, MD);
83+
}
84+
85+
/// \name PPCallbacks overrides
86+
/// Macro support
87+
void MacroUndefined(const clang::Token& MacroNameTok,
88+
const clang::MacroDefinition& MD,
89+
const clang::MacroDirective* Undef) final {
90+
if (Undef)
91+
MacroDirective(MacroNameTok, Undef);
92+
}
93+
};
94+
95+
void DeclCollector::Setup(IncrementalParser* IncrParser, ASTConsumer* Consumer,
96+
clang::Preprocessor& PP) {
97+
m_IncrParser = IncrParser;
98+
m_Consumer = Consumer;
99+
PP.addPPCallbacks(std::unique_ptr<PPCallbacks>(new PPAdapter(this)));
100+
}
101+
62102
bool DeclCollector::comesFromASTReader(DeclGroupRef DGR) const {
63103
assert(!DGR.isNull() && "DeclGroupRef is Null!");
64104
assert(m_CurTransaction && "No current transaction when deserializing");
@@ -271,26 +311,4 @@ namespace cling {
271311
m_Consumer->HandleCXXStaticMemberVarInstantiation(D);
272312
}
273313

274-
void DeclCollector::MacroDirective(const clang::Token &MacroNameTok,
275-
const clang::MacroDirective *MD) {
276-
assert(m_CurTransaction && "Missing transction");
277-
Transaction::MacroDirectiveInfo MDE(MacroNameTok.getIdentifierInfo(), MD);
278-
m_CurTransaction->append(MDE);
279-
}
280-
281-
void
282-
DeclCollectorPPAdapter::MacroDefined(const clang::Token &MacroNameTok,
283-
const clang::MacroDirective *MD) {
284-
m_parent->MacroDirective(MacroNameTok, MD);
285-
}
286-
287-
void
288-
DeclCollectorPPAdapter::MacroUndefined(const clang::Token &MacroNameTok,
289-
const clang::MacroDefinition &MD,
290-
const clang::MacroDirective *Undef) {
291-
// If Undef is null, the macro was never defined
292-
if (Undef)
293-
m_parent->MacroDirective(MacroNameTok, Undef);
294-
}
295-
296314
} // namespace cling

interpreter/cling/lib/Interpreter/DeclCollector.h

Lines changed: 7 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,6 @@
1111
#define CLING_DECL_COLLECTOR_H
1212

1313
#include "clang/AST/ASTConsumer.h"
14-
#include "clang/Lex/PPCallbacks.h"
1514

1615
#include "ASTTransformer.h"
1716

@@ -23,6 +22,7 @@ namespace clang {
2322
class CodeGenerator;
2423
class Decl;
2524
class DeclGroupRef;
25+
class Preprocessor;
2626
class Token;
2727
}
2828

@@ -34,36 +34,16 @@ namespace cling {
3434
class IncrementalParser;
3535
class Transaction;
3636

37-
///\brief Serves as DeclCollector's connector to the PPCallbacks interface.
38-
///
39-
class DeclCollectorPPAdapter: public clang::PPCallbacks {
40-
DeclCollector* m_parent;
41-
public:
42-
DeclCollectorPPAdapter(DeclCollector* parent):
43-
m_parent(parent)
44-
{}
45-
46-
/// \name PPCallbacks overrides
47-
/// Macro support
48-
void MacroDefined(const clang::Token &MacroNameTok,
49-
const clang::MacroDirective *MD) final;
50-
/// \}
51-
52-
/// \name PPCallbacks overrides
53-
/// Macro support
54-
void MacroUndefined(const clang::Token &MacroNameTok,
55-
const clang::MacroDefinition &MD,
56-
const clang::MacroDirective *Undef) final;
57-
};
58-
5937
///\brief Collects declarations and fills them in cling::Transaction.
6038
///
6139
/// cling::Transaction becomes is a main building block in the interpreter.
6240
/// cling::DeclCollector is responsible for appending all the declarations
6341
/// seen by clang.
6442
///
65-
class DeclCollector: public clang::ASTConsumer {
66-
private:
43+
class DeclCollector : public clang::ASTConsumer {
44+
/// \brief PPCallbacks overrides/ Macro support
45+
class PPAdapter;
46+
6747
///\brief Contains the transaction AST transformers.
6848
///
6949
std::vector<std::unique_ptr<ASTTransformer>> m_TransactionTransformers;
@@ -99,11 +79,6 @@ namespace cling {
9979

10080
virtual ~DeclCollector();
10181

102-
std::unique_ptr<DeclCollectorPPAdapter> MakePPAdapter() {
103-
return std::unique_ptr<DeclCollectorPPAdapter>
104-
(new DeclCollectorPPAdapter(this));
105-
}
106-
10782
void SetTransformers(std::vector<std::unique_ptr<ASTTransformer>>&& allTT,
10883
std::vector<std::unique_ptr<WrapperTransformer>>&& allWT){
10984
m_TransactionTransformers.swap(allTT);
@@ -114,16 +89,8 @@ namespace cling {
11489
WT->SetConsumer(this);
11590
}
11691

117-
void setContext(IncrementalParser* IncrParser, ASTConsumer* Consumer) {
118-
m_IncrParser = IncrParser;
119-
m_Consumer = Consumer;
120-
}
121-
122-
/// \name PPCallbacks overrides
123-
/// Macro support
124-
void MacroDirective(const clang::Token &MacroNameTok,
125-
const clang::MacroDirective *MD);
126-
/// \}
92+
void Setup(IncrementalParser* IncrParser, ASTConsumer* Consumer,
93+
clang::Preprocessor& PP);
12794

12895
/// \{
12996
/// \name ASTConsumer overrides

interpreter/cling/lib/Interpreter/IncrementalParser.cpp

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -181,20 +181,18 @@ namespace cling {
181181
cling::errs() << "No AST consumer available.\n";
182182
return;
183183
}
184-
// Add the callback keeping track of the macro definitions.
185-
m_CI->getPreprocessor().addPPCallbacks(m_Consumer->MakePPAdapter());
186184

187185
DiagnosticsEngine& Diag = m_CI->getDiagnostics();
188186
if (m_CI->getFrontendOpts().ProgramAction != frontend::ParseSyntaxOnly) {
189187
m_CodeGen.reset(CreateLLVMCodeGen(
190188
Diag, makeModuleName(), m_CI->getHeaderSearchOpts(),
191189
m_CI->getPreprocessorOpts(), m_CI->getCodeGenOpts(),
192190
*m_Interpreter->getLLVMContext()));
193-
m_Consumer->setContext(this, m_CodeGen.get());
194-
} else {
195-
m_Consumer->setContext(this, 0);
196191
}
197192

193+
// Initialize the DeclCollector and add callbacks keeping track of macros.
194+
m_Consumer->Setup(this, m_CodeGen.get(), m_CI->getPreprocessor());
195+
198196
m_DiagConsumer.reset(new FilteringDiagConsumer(Diag, false));
199197

200198
initializeVirtualFile();

0 commit comments

Comments
 (0)