-
Notifications
You must be signed in to change notification settings - Fork 0
/
find-demo.cpp
49 lines (41 loc) · 1.34 KB
/
find-demo.cpp
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
#ifndef FINDDEMO_H
#define FINDDEMO_H
#include "common.h"
// matches a field in a specific class
namespace clang { namespace ast_matchers {
AST_MATCHER_P(FieldDecl, inClass, internal::Matcher<CXXRecordDecl>, InnerMatcher) {
const CXXRecordDecl * Parent = dynamic_cast<const CXXRecordDecl*>(Node.getParent());
return (Parent && InnerMatcher.matches(*Parent, Finder, Builder));
}
} }
struct FindDemo : clang::ast_matchers::MatchFinder {
struct Callback : clang::ast_matchers::MatchFinder::MatchCallback {
void run(const MatchFinder::MatchResult & result) override {
if(const clang::Decl * decl = result.Nodes.getNodeAs<clang::Decl>("x")) {
decl->dump();
}
}
};
static int run(const clang::tooling::CompilationDatabase & db, llvm::ArrayRef<std::string> sources) {
using namespace clang::ast_matchers;
auto match_x_field =
fieldDecl(
hasName("x"),
isPublic(),
inClass(
isDerivedFrom("base")
)
).bind("x");
clang::tooling::ClangTool tool(db, sources);
Callback callback;
FindDemo find_demo;
find_demo.addMatcher(match_x_field, &callback);
return tool.run(clang::tooling::newFrontendActionFactory(&find_demo));
}
};
namespace tools {
int find_demo(const clang::tooling::CompilationDatabase & db, llvm::ArrayRef<std::string> sources) {
return FindDemo::run(db, sources);
}
}
#endif // NAMINGCONVENTION_H