[cfe-dev] What's the best way to...?

Alexei Svitkine alexei.svitkine at gmail.com
Thu Jan 29 08:47:42 PST 2009


Hi,

I'm looking to determine if some given code fragment is "complete" -
and by that I mean that, for example, it can be completely enclosed by
an AST node if parsed in-context.

For example,

printf("%d\n",

is not complete by my definition.

But "printf("%d\n", i);" is. Similarly, "if (blah) {" is not. But "if
(blah) { doStuff(); }" is. Or likewise "int fib(int n)" is not
complete - but with a semicolon at the end, its a function
declaration, or with a function body following, it's a function
definition - and thus complete.

I'm looking at what's the best way to do such an analysis using the
clang libraries. I have some ideas, but I thought I'd ask the list to
make sure I'm not missing some better way...

One way I thought about is using the Lexer to lex it into a list of
tokens, then use a stack to match {}, [], etc. If the code fragment is
balanced, then look at the last non-comment token and check if its
either a "}" or a ";", in which case the fragment is complete.
Otherwise it's not.

Obviously, this technique is just an approximation - and there's
probably edge cases it does not cover. Ideally, it would be nice to
re-use logic from the Parser - but looking at the public APIs - I
didn't see an easy way to accomplish this.

Any suggestions would be appreciated. Thanks.

-Alexei



More information about the cfe-dev mailing list