1

I would like to do transformations on AST of a c program but I need to have access to all ASTs created for the program to do right changes. LLVM processes one translation unit at a time and because of it, I do not have access to AST of all the translation units at the same time. Do you have any suggestion how I can access all the ASTs created for a program, do analysis on the ASTs and do modifications on the ASTs?

As a summary:

  1. I need to have access to ASTs of the program at the same time.
  2. Do analysis on ASTs.
  3. Modify ASTs based on my analysis and create llvm IR from modified ASTs.
  • You can tell clang to compile a file and give you the AST. You just do that for each file you want. You can't do that as a plugin, but you can write your own front end action and do whatever you want. Personally, I just do a unity build and run a plugin over that -- it's not identical, but it's sufficient fro my needs. – xaxxon Oct 05 '16 at 08:40
  • @xaxxon: If you process each compilation unit as a separate artifact, you can't implement any transformations that move code from one compilation unit to another. (You can't even *inspect* code in one compilation unit, and have the outcome of that inspect affect another). So you can't do any "global" analyses/transformations [unless you hack in a bag job on the side to remember stuff]. – Ira Baxter Oct 05 '16 at 08:52
  • @IraBaxter I don't understand exactly what you mean, but there's an API to say "compile this file and give me the AST". So you can have all the AST's just sitting around in memory at the same time if that's what you want -- or at least that's what I understand. – xaxxon Oct 05 '16 at 10:44
  • @xaxxon: Agreed. You said "process each compilation unit as a separate artifact". If you can do that and retain the ASTs in a single process instance, then you avoid my objection. It wasn't (isn't) clear that you can do that. (In my answer below I assume you *can* do that, I just don't know the mechanism). – Ira Baxter Oct 05 '16 at 12:45
  • @IraBaxter I don't even know what an "artifact" is, much less say it. The only two mentions of "artifact" on the page are yours, from what I see (except for this comment) Anyhow, I'm pretty sure a clang libtooling tool can do what he wants. – xaxxon Oct 05 '16 at 14:35
  • @xaxxon: You are welcome to look it up, but basically it is a $5 word for "thing". I pretty much agree Clang can do the parsing. I'm not convinced it can to AST modification the way OP imagines. – Ira Baxter Oct 05 '16 at 17:39
  • All: I posted an answer that explained why I thought Clang could not do AST modification the way OP imagines. A moderator deleted it, in spite of the fact that it addressed OP's core question: *how I can access all the ASTs created for a program, do analysis on the ASTs and do modifications on the ASTs?* – Ira Baxter Oct 14 '16 at 06:57

1 Answers1

1

You can try using llvm-link on all of your generated .ll files (from clang with -S -emit-llvm) to create one large llvm source.

You have access to everything at that point.

keyboardsmoke
  • 156
  • 2
  • 6