views:

353

answers:

1

Is there anyway to use the llvm-clang parser in an incremental/online manner?

Say I'm writing an editor and I want to be able to parse the C++ code I have in front of me.

I don't want to write my own hacked up parser.

I'd like to use something full featured, like llvm-clang.

Is there an easy way to hijack the llvm-clang parser? (And is it fast enough to run it continuously in the background)?

Thanks!

+1  A: 

I don't think clang can incrementally parse C++ files, but it's one of this project goals: http://clang.llvm.org/features.html

I've written something similar for my final year project. It wasn't C++ editor, but a Visual Studio plugin, which main task was improving C++ intellisense (like Visual Assist X).

When I was writing this project I've been also thinking about C++ incremental parser, but I haven't found any suitable solution. To solve the C++ intellisense problem I used normal C++ parser from GCC. However it was to slow, to parse file after each code completion request (ctrl+space), just try including boost::spirit. To make this project work properly I parsed files in the background and after each code completion request I compared current file with it's previous version (via diff) to detect changes made from last parsing. Having those changes I updated syntax tree, mostly by adding or removing variables.

Except incremental parsing, there is also another problem with projects like this. Mostly you'll be parsing C++ code which is being edited so it's invalid code. Given the complex C++ grammar, sometimes parser won't be able to recover from syntax errors, so it won't detect correctly some symbols in code.

Another issue are C++ parsers / compilers differences. Let's say I'm using working in Visual Studio and I have used some VC++ compiler specific contruction in my code. Clang parser won't be able to parse it correctly.

Klesk