Delay Clang CodeGen until AST is fully populated


We are having issues with modifying the Clang AST while it is being built, and we suspect they are due to it being code generated before it is fully populated, and we were wondering if it was possible to require the Sema to be fully finished before doing any CodeGen?

An example would be when we process custom pragma:

int my_global;

int my_function() {
my_global = 1;

int main() {
#pragma FOO SOMETHING var=my_globalmy_function();
return 0;

Here the way I process the pragma is that it attach to the AST node for my_global, an attribute node. So that once I code-generate that global variable I can “do something” based on that attribute. Unfortunately, today Clang might codegen that global as soon as it deem it necessary (typically, when code-generating my_function as it uses it). And the pragma has not yet been semantically parsed yet.

If I were to use -ast-print and dump the result into a file, then the attribute would be there, and if I parse that file again I would get the correct codegen. But I assume it is unsafe to rely on being able to parse -ast-print and/or it’s AST to be the same.

Any input on that issue?

It seems that there’s the same issue with following code:

We are really struggling with separating Sema and CodeGen.

Here are the few dead-end we hit:

  1. We tried to use -emit-ast to get a PCH-like AST, but it seems it does not do template specialization.
    That is, when we feed it again into clang, some of the Sema is still run after CodeGen.
    We stopped there, because we are not sure this can help us.

  2. We tried -traditional-cpp and -no-integrated-cpp which is supposed to forbid clang driver to merge the different stages (it didn’t help). I do not know if this was effective, but we didn’t see any difference. So maybe it is the same issue as the previous one? We also stopped there, because we use clang -cc1 anyway.

  3. We started looking into ASTConsumer(s) and we are thinking if it is possible to write an ASTConsumer that “forward” the AST to the CodeGen and only implement the HandleTranslationUnit so that it ensure it receive the entire AST before forwarding it. But I guess we need to explore it then run manually each of the CodeGen method? Will it have the same issue about template instantiation?

We would really appreciate if anybody could shed some light, we don’t know that part of the infrastructure.
Are we even going into the right direction? Is there already an option that does that?

Hi ,

I Want to unsubscribe emails.
Please do need full.


-----“cfe-dev” <> wrote: -----