I am new to Clang libTooling development.
consider the following variable declaration
int i, j, k = 10;
^ ^
For my project requirement, I want to capture the whole declaration expression including "i", "j" and "k".
How to capture the complete declaration expression including all variables with clang libTooling?
What I am experiencing is that I do not get the visitor for complete expression instead I get the visitor for individual variable declaration.
Is this the expected behavior in clang libTooling OR am I missing something?
Please suggest me the correct way to capture single line multiple declarations or any workaround? Any kind of help will be really appreciated.
Thanks, Hemant
To solve the described problem you could for instance write a recursive AST visitor that visits
DeclStmt
nodes (not onlyVarDecl
). Check this site to see how to write such a visitor: http://clang.llvm.org/docs/RAVFrontendAction.htmlThe reason why you should visit
DeclStmt
nodes and not onlyVarDecl
nodes can be explained by having a look at the AST representation of your declaration statement:As you can see the
DeclStmt
"captures" all theVarDecl
nodes (and the initialization, if given). Once your visitor visits theDeclStmt
you can check with theisSingleDecl()
member function if your declaration refers to a single declaration or not. If not (as in your case) you can retrieve an iterator to the differentVarDecl
nodes withdecl_begin()
,decl_end()
, etc.