-
Notifications
You must be signed in to change notification settings - Fork 10
/
Visitors.h
106 lines (77 loc) · 2.3 KB
/
Visitors.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
#ifndef CCONS_VISITORS_H
#define CCONS_VISITORS_H
//
// Defines utility visitor classes for operating on the clang AST.
//
// Part of ccons, the interactive console for the C programming language.
//
// Copyright (c) 2009 Alexei Svitkine. This file is distributed under the
// terms of MIT Open Source License. See file LICENSE for details.
//
#include <string>
#include <vector>
#include <llvm/ADT/OwningPtr.h>
#include <clang/AST/AST.h>
#include <clang/AST/ASTConsumer.h>
#include <clang/AST/DeclGroup.h>
namespace clang {
class ASTContext;
class SourceManager;
} // namespace clang
namespace ccons {
// StmtFinder will attempt to find a clang Stmt at the specified
// offset in the source (pos).
class StmtFinder : public clang::StmtVisitor<StmtFinder> {
public:
StmtFinder(unsigned pos, const clang::SourceManager& sm);
~StmtFinder();
void VisitChildren(clang::Stmt *S);
void VisitStmt(clang::Stmt *S);
clang::Stmt * getStmt() const;
private:
unsigned _pos;
const clang::SourceManager& _sm;
clang::Stmt *_S;
};
// StmtSplitter will extract clang Stmts from the specified source.
class StmtSplitter : public clang::StmtVisitor<StmtSplitter> {
public:
StmtSplitter(const std::string& src,
const clang::SourceManager& sm,
const clang::LangOptions& options,
std::vector<clang::Stmt*> *stmts);
~StmtSplitter();
void VisitChildren(clang::Stmt *S);
void VisitStmt(clang::Stmt *S);
private:
const std::string& _src;
const clang::SourceManager& _sm;
const clang::LangOptions& _options;
std::vector<clang::Stmt*> *_stmts;
};
// ASTConsumer that visits function body Stmts and passes
// those to a specific StmtVisitor.
template <typename T>
class FunctionBodyConsumer : public clang::ASTConsumer {
private:
T *_SV;
std::string _funcName;
public:
FunctionBodyConsumer<T>(T *SV, const char *funcName)
: _SV(SV), _funcName(funcName) {}
~FunctionBodyConsumer<T>() {}
bool HandleTopLevelDecl(clang::DeclGroupRef D) {
for (clang::DeclGroupRef::iterator I = D.begin(), E = D.end(); I != E; ++I) {
if (clang::FunctionDecl *FD = llvm::dyn_cast<clang::FunctionDecl>(*I)) {
if (FD->getName().str() == _funcName) {
if (clang::Stmt *S = FD->getBody()) {
_SV->VisitChildren(S);
}
}
}
}
return true;
}
};
} // namespace ccons
#endif // CCONS_VISITORS_H