I am working on a source-to-source conversion tool, using Clang's AST via libTooling.
There is one problem which I can't seem to workaround. Consider the following C++ code:
template<typename T>
struct s {
void lazy1() {}
void lazy2() {} // unused
};
int main() {
s<int> inst;
inst.lazy1();
return 0;
}
In Clang's AST, definition of lazy1()
is fully available (i.e., a CompoundStmt exists as child), while lazy2()
has no children:
[...]
`-ClassTemplateSpecializationDecl 0x7de4a8 <line:1:1, line:5:1> line:2:8 struct s definition
[...]
|-CXXMethodDecl 0x7de790 <line:3:2, col:16> col:7 used lazy1 'void ()'
| `-CompoundStmt 0x7de310 <col:15, col:16>
|-CXXMethodDecl 0x7de840 <line:4:2, col:16> col:7 lazy2 'void ()'
[...]
As I understand, Clang only emits method definitions if they are actually used. In this example, lazy2()
is unused and I can't access any definition data in the AST.
I need to access all method definitions of all template specializations.
I am interested in a non-hacky, reliable solution. I am ready to patch Clang, if necessary. I've already tried this answer on Stackoverflow, however, in my case it doesn't work.