Dynamic Scopes and code snippet parsing

Hello,

My end goal is to parse C++ code snippets which are incomplete, for instance :

	a.f();

or

	while(g() + 2 < foo)

I’m exploring several paths and one involve clang. I stumbled upon Cling: Implementing Dynamic Scopes with clang which states

[quote]One of the major concerns was: Is it possible to make LLVM and particularly Sema skip the diagnostic errors and build a pseudo-valid abstract syntax tree (AST). Pseudo-valid AST, in the terms of Cling, means that there is an AST which is build successfully but LLVM cannot generate code from it.

According to the advice of the Sebastian Redl (from the clang dev team), we needed to mark all unknown symbols as of dependent type. The e-mail can be found here (link is external).

In order to mark an unknown symbol as dependent we need to watch clang::Sema’s lookup facilities. We were able to patch Sema in less intrusive way than we did before. We provide last resort lookup, when a lookup fails in Sema. The patch was accepted in clang r126387 and r126648. Now we don’t even need to patch clang, we just provide implementation for the existing hook in Sema![/quote]

This sounds a lot like what I’m looking for but I don’t know where to start with that information. Do you have any pointers I could use to start, files to look at…

Many thanks,
David

Hi,
The link you are reading from is very outdated :frowning:

Some more details are available here: researchgate.net/publicatio … for_ROOT_6

Have a look at cling/test/Extensions/Lookup/

If you want to build a ‘semi-valid’ AST only you will need to have a look at how turn off the diagnostics system by marking the unknown nodes as dependent.
– Vassil

:astonished:, I didn’t know that.

Thanks for a link to the paper, I’ll look at it.

[quote]
If you want to build a ‘semi-valid’ AST only you will need to have a look at how turn off the diagnostics system by marking the unknown nodes as dependent.[/quote]

That sounds easy, where is it done ? I’ve only been able to find the part where the Sema analysis falls back when clang finds an unknown symbol.

Thanks,
David.

[quote=“Davidbrcz”][quote]
That sounds easy, where is it done ? I’ve only been able to find the part where the Sema analysis falls back when clang finds an unknown symbol.
[/quote][/quote]

github.com/root-mirror/root/blo … lbacks.cxx have a look at TClingCallbacks::tryResolveAtRuntimeInternal. So, just before Sema issues a diagnostic (for something not found) it gives the control for external tools to try to provide information about the unknown name. There one could trick Sema by defining the unknown name as of dependent type, which tells the compiler to turn off many of the diagnostics. Of course this leads to creation of an invalid AST which you should fix if you want to generate code out of it.