33// tool-template <header-to-look-at> -- <compile-flags-as-usual>
44//
55// Example:
6- // ./bin/extractor $ROCK_ROOT/base/types/base/Pose.hpp -- \
7- // -I$ROCK_ROOT/base/types \
8- // -I/usr/include/eigen3 \
9- // -x c++
6+ // ./bin/extractor $ROCK_ROOT/base/types/base/Pose.hpp -- -I$ROCK_ROOT/base/types -I/usr/include/eigen3 -x c++
107//
118// keep in mind that this particular (not very complicated) example still takes 15seconds to
129// complete...
@@ -48,35 +45,33 @@ namespace {
4845class TypeDefCallback : public MatchFinder ::MatchCallback {
4946 public:
5047
51-
52- // This routine will get called for each thing that the matchers find.
53- virtual void run (const MatchFinder::MatchResult &Result) {
48+ virtual void run (const MatchFinder::MatchResult &Result) {
5449
55- const TypedefType *typeType = Result.Nodes .getNodeAs <TypedefType>(" typeDef" );
56- if (typeType)
57- {
58- builder.registerTypeDef (typeType);
50+ const TypedefType *T = Result.Nodes .getNodeAs <TypedefType>(" typeDef" );
51+
52+ if (T) {
53+ builder.registerTypeDef (T);
54+ }
5955 }
60- }
6156};
6257
63- class ToolTemplateCallback : public MatchFinder ::MatchCallback {
64- public:
65- // This routine will get called for each thing that the matchers find.
66- virtual void run (const MatchFinder::MatchResult &Result) {
58+ class TypeDeclCallback : public MatchFinder ::MatchCallback {
59+ public:
60+
61+ virtual void run (const MatchFinder::MatchResult &Result) {
6762
68- const TypeDecl *decl = Result.Nodes .getNodeAs <TypeDecl>(" match " );
63+ const TypeDecl *D = Result.Nodes .getNodeAs <TypeDecl>(" typeDecl " );
6964
70- if (decl)
71- {
72- builder.registerNamedDecl (decl);
65+ if (D) {
66+
67+ builder.registerNamedDecl (D);
68+ }
69+
70+ const CXXRecordDecl *DD = Result.Nodes .getNodeAs <CXXRecordDecl>(" typeDecl" );
71+ if (DD) {
72+ builder.registerNamedDecl (DD);
73+ }
7374 }
74-
75- const CXXRecordDecl *D = Result.Nodes .getNodeAs <CXXRecordDecl>(" match" );
76- if (D) {
77- builder.registerNamedDecl (D);
78- }
79- }
8075
8176};
8277} // end anonymous namespace
@@ -91,28 +86,18 @@ int main(int argc, const char **argv) {
9186 // }}}
9287
9388 ast_matchers::MatchFinder Finder;
94- ToolTemplateCallback Callback;
95- TypeDefCallback tdCallback;
96-
97- // AST matching ftw...
98- //
99- // the big table: http://clang.llvm.org/docs/LibASTMatchersReference.html
100-
101- // the "bind" will make the match referencable by the given string in the "run()" mathod of the
102- // callback
10389
104- // the "isDefinition()" is needed to reject "Class Name Injection" and forward
105- // declarations. see https://stackoverflow.com/questions/24761684 and
106- // http://www.open-std.org/jtc1/sc22/wg21/docs/papers/1994/N0444.pdf
90+ TypeDeclCallback typeDeclCallback;
10791 internal::VariadicDynCastAllOfMatcher<Decl, TypeDecl> typeDecl;
108-
109- DeclarationMatcher matcher = typeDecl ().bind (" match" );
110-
111- Finder.addMatcher (matcher, &Callback);
92+ Finder.addMatcher (typeDecl ().bind (" typeDecl" ), &typeDeclCallback);
11293
113- Finder.addMatcher (typedefType ().bind (" typeDef" ), &tdCallback);
94+ TypeDefCallback typeDefCallback;
95+ Finder.addMatcher (typedefType ().bind (" typeDef" ), &typeDefCallback);
11496
115- Tool.run (newFrontendActionFactory (&Finder));
97+ if (int retval = Tool.run (newFrontendActionFactory (&Finder)) != 0 ) {
98+ std::cerr << " whoops\n " ;
99+ return retval;
100+ }
116101
117102 builder.buildRegistry ();
118103
0 commit comments