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

Argiris Kirtzidis akyrtzi at gmail.com
Thu Jan 29 14:34:43 PST 2009


Hi Alexei,

Alexei Svitkine wrote:
> 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.
>   

Here's an idea, not sure how well it will work or whether it suits your 
purposes.

The preprocessor has a "FileChanged" callback, look in "Lex/PPCallbacks.h".
The idea is that you "intercept" that callback (using 
Preprocessor::setPPCallbacks) and enter the code fragment using 
something like:

  llvm::MemoryBuffer *SB = llvm::MemoryBuffer::getMemBufferCopy(buf, 
buf_len); // buf contains the code fragment string
  unsigned FileID = SourceMgr.createFileIDForMemBuffer(SB);
  PP.EnterSourceFile(FileID, 0);  // PP is the Preprocessor

Then you call "Parser::ParseTopLevelDecl".
If the code fragment is incomplete, the FileChanged callback will be 
invoked (the preprocessor will close the "file" trying to consume more 
tokens), otherwise ParseTopLevelDecl will return the AST node (or null 
in case of error).

Also, you will probably need to make some Parser methods like 
"ParseExpression" public so that you can use them.

-Argiris

> 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
> _______________________________________________
> cfe-dev mailing list
> cfe-dev at cs.uiuc.edu
> http://lists.cs.uiuc.edu/mailman/listinfo/cfe-dev
>
>   



More information about the cfe-dev mailing list