Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is it possible to view C++ code with instantiated templates before it gets compiled (g++)?

the g++ compiler has a flag that produces macro-expanded code (-E), so I am wondering if there is a way to view the program coude after template instantiation before the actual compilation takes place?

like image 204
tmaric Avatar asked Jan 25 '13 12:01

tmaric


1 Answers

Well, the closer you can get is to read the AST/ABT generated by the compiler:

  • AST: Abstract Syntax Tree
  • ABT: Abstract Binding Tree

The former represents the view of the syntax as the compiler understands it and the latter is similar after resolution of the bindings (ie, that the a here is actually the variable that was declared 3 lines before or that the foo correspdonds to the function defined in that header...).

Clang allows to dump its AST... which is in fact the ABT, actually, it's being improved at this very moment; sneak developer preview:

int Test __attribute__((visibility("default")));

int main(int argc, char** argv) {
  int x __attribute__((aligned(4))) = 5;
  int y = 2;
  for (;;)
    if (x != y++)
      break;
  return (x * y);
}

enter image description here

Normally you should see how the template was instantiated there.

Note: to get it you need the -ast-dump pass to the clang front-end.

like image 164
Matthieu M. Avatar answered Oct 14 '22 16:10

Matthieu M.