I need to preprocess some C++ files to automatically insert code for testing and profiling, and I need to do it with the clang API.
Fo
I'm wondering why you want to do this transform on the fly (if I understand you right). This is especially tricky with C++ methods defined in-class, since they're compiled as-if they're defined outside the class (i.e. all class members are usable, even those not declared yet).
Anyway, when you have a RecordDecl, you can access its members via field_begin.
Obviously, when you encounter nexted classes, you'll need to enumerate those as well. In fact, since you can define methods in classes in functions, you'll need to check for nested declarations almost everywhere.
I agree that the documentation of CLang might be lacking in some area. This is, unfortunately, the way Open Source works: it will lacking until someone needs it, figures it out and decide to contribute back his findings.
For you specific problem I suggest you post on the Clang Dev mailing list (or begin by sifting through the archives). Questions about ASTConsumers or other transform analysis are quite frequent and usually promptly answered to.
And then, when you've learned what you were looking for, do think about upgrading the documentation ;)
Are there any settings to switch between C and C++ ?
Finally I found out how to handle this:
I extended from ASTConsumer
and RecursiveASTVisitor<MyConsumer>
to traverse the AST and implemented VisitCXXRecordDecl(CXXRecordDecl* D)
. Then I had to set the LangOptions
parameter for the preprocessor, so it parses C++.
langOpts.CPlusPlus = 1;
My fault was to think that it would parse C++ right away, but that was not the case, it parses C as default and so doesn't recognize classes.