We can get the following dumped AST with command clang++ -Xclang -ast-dump -fsyntax-only. The following text says that there are two separate VarDecls in the ast.
…
|-VarDecl c …
`-VarDecl d …
However, in eclipse cdt, we would get something like
SimpleDeclaration
VarDecl c …
VarDecl d …
which reflects the original code layout.
How do I achieve the same effect as eclipse cdt in clang? Big thanks!
Because it essentially is syntactic sugar for two individual variable declarations.
I don’t think the Clang AST concerns itself with accurately describing the source-code.
If you want to match on this pattern, the naïve way would be to match on any VarDecl, get the source-location and check if there are multiple variable declarations on the same line.
You could also do something more complex like matching all VarDecls of the scope, but to my understanding you would still need to compare the source-locations
Not quite. The Clang AST is expected to accurately reflect the user’s source code, though we have some deficiencies from time to time. For example, we have a dedicated AST node for a range-based for loop rather than storing a form rewritten as a regular for loop, etc.
We have the DeclGroupRef and DeclGroup classes to store information about a declaration group, but these are not exposed via the AST.
This is currently the best you can do, but it’s still not quite sufficient (which suggests we probably should expose a declaration group as an AST node). e.g.,
int x, y; // This is a declaration group, everything is on the same line
int a; int b; // This is not a declaration group, everything is on the same line
int c,
d; // This is a declaration group, everything is not on the same line