[cfe-dev] How to walk identifiers of multi-variable declaration (int i, j; )?

Richard legalize at xmission.com
Sun May 31 20:08:55 PDT 2015


Hi,

I'm interested in transforming:

int i, j;

into

int i;
int j;

However, they both end up with the same AST:

separate declaration statements:
clanger> clang -Xclang -ast-dump -fsyntax-only /tmp/decl.cpp
TranslationUnitDecl 0x49539c0 <<invalid sloc>> <invalid sloc>
|-VarDecl 0x4954380 </tmp/decl.cpp:1:1, col:5> col:5 i 'int'
`-VarDecl 0x49543f0 <line:2:1, col:5> col:5 j 'int'
~/dev/llvm/tools/clang/tools/extra

combined declaration statement:
clanger> clang -Xclang -ast-dump -fsyntax-only /tmp/decl2.cpp
TranslationUnitDecl 0x39579c0 <<invalid sloc>> <invalid sloc>
|-VarDecl 0x3958380 </tmp/decl2.cpp:1:1, col:5> col:5 i 'int'
`-VarDecl 0x39583f0 <col:1, col:8> col:8 j 'int'
~/dev/llvm/tools/clang/tools/extra
clanger> 

What's worse is that the source range for the VarDecl 'j' contains the
identifier 'i'!

Given that the single declaration statement declaring both i and j
isn't distinctly represented in the AST, how do I even detect such a
thing and extract each individual variable identifier from the single
statement without walking all the tokens all over again?  (For
something declaring pointers to members, this could be a complex chain
of tokens.)

I hacked up something that matched varDecls() and did something like:

init:
	LastSourceLoc = empty;

match:
	if (LastSourceLoc == match.LocStart) {
	  diag("multiple variables declared");
	}
	LastSourceLoc = match.LocStart;

which sorta worked in that it issued a diagnostic on the right line,
but then when attempting to build a replacement string, there didn't
seem to be anything better than re-walking all the tokens in the
varDecl again, which feels lame.

I thought *everything* about the source files was represented in the AST,
but this one leaves me stumped as to how to identify that two VarDecls
came from the same statement by directly querying the AST.

What's the best way to do this?
-- 
"The Direct3D Graphics Pipeline" free book <http://tinyurl.com/d3d-pipeline>
     The Computer Graphics Museum <http://ComputerGraphicsMuseum.org>
         The Terminals Wiki <http://terminals.classiccmp.org>
  Legalize Adulthood! (my blog) <http://LegalizeAdulthood.wordpress.com>

PS: Mail filters are blocking my messages, so ignore this, its
only for them.  Lorem ipsum dolor sit amet, consectetur adipiscing
elit. Phasellus consequat ullamcorper mollis. Sed blandit semper tortor
ultricies dictum. Proin hendrerit et quam in sagittis. Maecenas vel
blandit ante, in auctor sem.  Phasellus condimentum leo vel finibus
viverra. Duis fermentum sollicitudin est, ac iaculis lectus auctor vel.
Nam condimentum nulla feugiat, venenatis nibh a, elementum nulla. Nulla
vitae malesuada eros. Nulla cursus maximus ligula non hendrerit.

Curabitur lobortis nulla vel sapien posuere, id aliquam orci
bibendum. Vestibulum at vulputate risus. Proin in purus commodo,
tempus lectus vitae, faucibus nunc. Aenean congue faucibus elit, sit
amet facilisis nibh ultrices eget. Nam pulvinar leo ac nunc ultricies,
nec tincidunt nulla tincidunt. Etiam placerat felis tellus, ut ultricies
nunc ultricies quis. In placerat sapien nec ultricies mattis. Integer
sed tempor orci, ac imperdiet orci. Integer ac augue et augue convallis
faucibus.



More information about the cfe-dev mailing list