-
Notifications
You must be signed in to change notification settings - Fork 2
/
ActionFactory.hpp
66 lines (51 loc) · 1.78 KB
/
ActionFactory.hpp
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
#ifndef MOCKOTO_FACTORY_HPP
#define MOCKOTO_FACTORY_HPP
#include "clang/AST/AST.h"
#include "clang/AST/ASTConsumer.h"
#include "clang/AST/RecursiveASTVisitor.h"
#include "clang/Frontend/CompilerInstance.h"
#include "clang/Frontend/FrontendActions.h"
#include "clang/Frontend/FrontendPluginRegistry.h"
#include "clang/Tooling/CommonOptionsParser.h"
#include "clang/Tooling/Tooling.h"
#include "llvm/Support/raw_ostream.h"
#include "Config.hpp"
namespace mockoto {
template <typename V> class Consumer : public clang::ASTConsumer {
public:
explicit Consumer(clang::ASTContext *Context, Config Config,
llvm::StringRef InFile)
: Visitor(Context, Config, InFile) {}
virtual void HandleTranslationUnit(clang::ASTContext &Context) {
Visitor.TraverseDecl(Context.getTranslationUnitDecl());
}
private:
V Visitor;
};
template <typename V> class Action : public clang::ASTFrontendAction {
Config Config;
public:
Action(mockoto::Config Config) : Config(Config) {}
protected:
virtual std::unique_ptr<clang::ASTConsumer>
CreateASTConsumer(clang::CompilerInstance &Compiler, llvm::StringRef InFile) {
return std::make_unique<Consumer<V>>(&Compiler.getASTContext(), Config,
InFile);
}
};
template <typename V>
std::unique_ptr<clang::tooling::FrontendActionFactory>
newActionFactory(Config Config) {
class actionFactory : public clang::tooling::FrontendActionFactory {
mockoto::Config Config;
public:
actionFactory(mockoto::Config &cfg) : Config(cfg) {}
std::unique_ptr<clang::FrontendAction> create() override {
return std::unique_ptr<Action<V>>(new Action<V>(Config));
}
};
return std::unique_ptr<clang::tooling::FrontendActionFactory>(
new actionFactory(Config));
}
} // namespace mockoto
#endif