c++gccmetaprogrammingcompile-time

C++ Compiler Output


I would like to know if there is a tool / GCC compilation switch one could use to probably see the trace / output of the GCC compiler before runtime. Generally , I would appreciate a tool annotating how the GCC compiler parsed through my code showing all precomputed static values/types .

Example : Take for instance the case on the metaFactoril I wrote here, http://pastebin.com/N6eN9jNx .

Can some tool show the expansion of the recursive calls and substituted values when MetaFactorial<5>::value is called ?


Solution

  • gcc does allow dumping of intermediate files and various other diagnostic tools, but it's not really meant for "regular people" to read. There are more than a dozen -fdump-tree-* options, and I'd expect one of those does what you want. -fdump-tree-original looked good, but it just shows the 120 as a final result - I didn't try any other variant.

    With clang++ -Xclang -ast-dump you get this (with colours), which is quite clear to read in my view.

    TranslationUnitDecl 0x31853b0 <<invalid sloc>> <invalid sloc>
    |-TypedefDecl 0x31858f0 <<invalid sloc>> <invalid sloc> implicit __int128_t '__int128'
    |-TypedefDecl 0x3185950 <<invalid sloc>> <invalid sloc> implicit __uint128_t 'unsigned __int128'
    |-TypedefDecl 0x3185d50 <<invalid sloc>> <invalid sloc> implicit __builtin_va_list '__va_list_tag [1]'
    |-ClassTemplateDecl 0x3185eb0 <metafact.cpp:1:1, line:5:1> line:2:7 MetaFactorial
    | |-NonTypeTemplateParmDecl 0x3185db0 <line:1:10, col:14> col:14 referenced 'int' n
    | |-CXXRecordDecl 0x3185e20 <line:2:1, line:5:1> line:2:7 class MetaFactorial definition
    | | |-CXXRecordDecl 0x31c77d0 <col:1, col:7> col:7 implicit class MetaFactorial
    | | |-AccessSpecDecl 0x31c7860 <line:3:3, col:9> col:3 public
    | | `-EnumDecl 0x31c7890 <line:4:5, col:47> col:5
    | |   `-EnumConstantDecl 0x31c7ba0 <col:10, col:42> col:10 value 'enum MetaFactorial::(anonymous at metafact.cpp:4:5)'
    | |     `-BinaryOperator 0x31c7b78 <col:18, col:42> '<dependent type>' '*'
    | |       |-DeclRefExpr 0x31c7940 <col:18> 'int' NonTypeTemplateParm 0x3185db0 'n' 'int'
    | |       `-DependentScopeDeclRefExpr 0x31c7b38 <col:22, col:42> '<dependent type>' lvalue
    | |-ClassTemplateSpecialization 0x31c7c20 'MetaFactorial'
    | |-ClassTemplateSpecializationDecl 0x31c83a0 <line:1:1, line:5:1> line:2:7 class MetaFactorial definition
    | | |-TemplateArgument integral 5
    | | |-CXXRecordDecl 0x31c8590 prev 0x31c83a0 <col:1, col:7> col:7 implicit class MetaFactorial
    | | |-AccessSpecDecl 0x31c8620 <line:3:3, col:9> col:3 public
    | | `-EnumDecl 0x31c8650 <line:4:5> col:5
    | |   `-EnumConstantDecl 0x31d1390 <col:10, col:42> col:10 referenced value 'enum MetaFactorial<5>::(anonymous at metafact.cpp:4:5)'
    | |     `-ImplicitCastExpr 0x31d13e0 <col:18, col:42> 'unsigned int' <IntegralCast>
    | |       `-BinaryOperator 0x31d1368 <col:18, col:42> 'int' '*'
    | |         |-SubstNonTypeTemplateParmExpr 0x31c8730 <col:18> 'int'
    | |         | `-IntegerLiteral 0x31c8710 <col:18> 'int' 5
    | |         `-ImplicitCastExpr 0x31d1350 <col:22, col:42> 'int' <IntegralCast>
    | |           `-DeclRefExpr 0x31d1318 <col:22, col:42> 'enum MetaFactorial<4>::(anonymous at metafact.cpp:4:5)' EnumConstant 0x31d12b0 'value' 'enum MetaFactorial<4>::(anonymous at metafact.cpp:4:5)'
    | |-ClassTemplateSpecializationDecl 0x31ce740 <line:1:1, line:5:1> line:2:7 class MetaFactorial definition
    | | |-TemplateArgument integral 4
    | | |-CXXRecordDecl 0x31ce940 prev 0x31ce740 <col:1, col:7> col:7 implicit class MetaFactorial
    | | |-AccessSpecDecl 0x31ce9d0 <line:3:3, col:9> col:3 public
    | | `-EnumDecl 0x31cea00 <line:4:5> col:5
    | |   `-EnumConstantDecl 0x31d12b0 <col:10, col:42> col:10 referenced value 'enum MetaFactorial<4>::(anonymous at metafact.cpp:4:5)'
    | |     `-ImplicitCastExpr 0x31d1300 <col:18, col:42> 'unsigned int' <IntegralCast>
    | |       `-BinaryOperator 0x31d1288 <col:18, col:42> 'int' '*'
    | |         |-SubstNonTypeTemplateParmExpr 0x31ceae0 <col:18> 'int'
    | |         | `-IntegerLiteral 0x31ceac0 <col:18> 'int' 4
    | |         `-ImplicitCastExpr 0x31d1270 <col:22, col:42> 'int' <IntegralCast>
    | |           `-DeclRefExpr 0x31d1238 <col:22, col:42> 'enum MetaFactorial<3>::(anonymous at metafact.cpp:4:5)' EnumConstant 0x31d11d0 'value' 'enum MetaFactorial<3>::(anonymous at metafact.cpp:4:5)'
    | |-ClassTemplateSpecializationDecl 0x31ceb80 <line:1:1, line:5:1> line:2:7 class MetaFactorial definition
    | | |-TemplateArgument integral 3
    | | |-CXXRecordDecl 0x31ced80 prev 0x31ceb80 <col:1, col:7> col:7 implicit class MetaFactorial
    | | |-AccessSpecDecl 0x31cee10 <line:3:3, col:9> col:3 public
    | | `-EnumDecl 0x31cee40 <line:4:5> col:5
    | |   `-EnumConstantDecl 0x31d11d0 <col:10, col:42> col:10 referenced value 'enum MetaFactorial<3>::(anonymous at metafact.cpp:4:5)'
    | |     `-ImplicitCastExpr 0x31d1220 <col:18, col:42> 'unsigned int' <IntegralCast>
    | |       `-BinaryOperator 0x31d11a8 <col:18, col:42> 'int' '*'
    | |         |-SubstNonTypeTemplateParmExpr 0x31cef20 <col:18> 'int'
    | |         | `-IntegerLiteral 0x31cef00 <col:18> 'int' 3
    | |         `-ImplicitCastExpr 0x31d1190 <col:22, col:42> 'int' <IntegralCast>
    | |           `-DeclRefExpr 0x31d1158 <col:22, col:42> 'enum MetaFactorial<2>::(anonymous at metafact.cpp:4:5)' EnumConstant 0x31d10f0 'value' 'enum MetaFactorial<2>::(anonymous at metafact.cpp:4:5)'
    | |-ClassTemplateSpecializationDecl 0x31cefc0 <line:1:1, line:5:1> line:2:7 class MetaFactorial definition
    | | |-TemplateArgument integral 2
    | | |-CXXRecordDecl 0x31cf1c0 prev 0x31cefc0 <col:1, col:7> col:7 implicit class MetaFactorial
    | | |-AccessSpecDecl 0x31cf250 <line:3:3, col:9> col:3 public
    | | `-EnumDecl 0x31cf280 <line:4:5> col:5
    | |   `-EnumConstantDecl 0x31d10f0 <col:10, col:42> col:10 referenced value 'enum MetaFactorial<2>::(anonymous at metafact.cpp:4:5)'
    | |     `-ImplicitCastExpr 0x31d1140 <col:18, col:42> 'unsigned int' <IntegralCast>
    | |       `-BinaryOperator 0x31d10c8 <col:18, col:42> 'int' '*'
    | |         |-SubstNonTypeTemplateParmExpr 0x31cf360 <col:18> 'int'
    | |         | `-IntegerLiteral 0x31cf340 <col:18> 'int' 2
    | |         `-ImplicitCastExpr 0x31d10b0 <col:22, col:42> 'int' <IntegralCast>
    | |           `-DeclRefExpr 0x31d1078 <col:22, col:42> 'enum MetaFactorial<1>::(anonymous at metafact.cpp:4:5)' EnumConstant 0x31d1010 'value' 'enum MetaFactorial<1>::(anonymous at metafact.cpp:4:5)'
    | `-ClassTemplateSpecializationDecl 0x31cf400 <line:1:1, line:5:1> line:2:7 class MetaFactorial definition
    |   |-TemplateArgument integral 1
    |   |-CXXRecordDecl 0x31cf600 prev 0x31cf400 <col:1, col:7> col:7 implicit class MetaFactorial
    |   |-AccessSpecDecl 0x31cf690 <line:3:3, col:9> col:3 public
    |   `-EnumDecl 0x31d0d70 <line:4:5> col:5
    |     `-EnumConstantDecl 0x31d1010 <col:10, col:42> col:10 referenced value 'enum MetaFactorial<1>::(anonymous at metafact.cpp:4:5)'
    |       `-ImplicitCastExpr 0x31d1060 <col:18, col:42> 'unsigned int' <IntegralCast>
    |         `-BinaryOperator 0x31d0fe0 <col:18, col:42> 'int' '*'
    |           |-SubstNonTypeTemplateParmExpr 0x31d0e50 <col:18> 'int'
    |           | `-IntegerLiteral 0x31d0e30 <col:18> 'int' 1
    |           `-ImplicitCastExpr 0x31d0fc8 <col:22, col:42> 'int' <IntegralCast>
    |             `-DeclRefExpr 0x31d0f90 <col:22, col:42> 'enum MetaFactorial<0>::(anonymous at metafact.cpp:10:5)' EnumConstant 0x31c7fd0 'value' 'enum MetaFactorial<0>::(anonymous at metafact.cpp:10:5)'
    |-ClassTemplateSpecializationDecl 0x31c7c20 <line:7:1, line:11:1> line:8:7 class MetaFactorial definition
    | |-TemplateArgument integral 0
    | |-CXXRecordDecl 0x31c7e40 <col:1, col:7> col:7 implicit class MetaFactorial
    | |-AccessSpecDecl 0x31c7ed0 <line:9:3, col:9> col:3 public
    | `-EnumDecl 0x31c7f00 <line:10:5, col:21> col:5
    |   `-EnumConstantDecl 0x31c7fd0 <col:11, col:19> col:11 referenced value 'enum MetaFactorial<0>::(anonymous at metafact.cpp:10:5)'
    |     `-ImplicitCastExpr 0x31c8020 <col:19> 'unsigned int' <IntegralCast>
    |       `-IntegerLiteral 0x31c7fb0 <col:19> 'int' 1
    |-FunctionDecl 0x31c8110 <line:13:1, col:24> col:13 used my_func 'void (int)' extern
    | `-ParmVarDecl 0x31c8050 <col:21> col:24 'int'
    `-FunctionDecl 0x31c8210 <line:15:1, line:22:1> line:15:5 main 'int (void)'
      `-CompoundStmt 0x31d1608 <col:12, line:22:1>
        |-DeclStmt 0x31d1478 <line:17:3, col:34>
        | `-VarDecl 0x31c8320 <col:3, col:29> col:7 used n 'int' cinit
        |   `-ImplicitCastExpr 0x31d1460 <col:11, col:29> 'int' <IntegralCast>
        |     `-DeclRefExpr 0x31d1428 <col:11, col:29> 'enum MetaFactorial<5>::(anonymous at metafact.cpp:4:5)' EnumConstant 0x31d1390 'value' 'enum MetaFactorial<5>::(anonymous at metafact.cpp:4:5)'
        |-CallExpr 0x31d1580 <line:19:3, col:12> 'void'
        | |-ImplicitCastExpr 0x31d1568 <col:3> 'void (*)(int)' <FunctionToPointerDecay>
        | | `-DeclRefExpr 0x31d1510 <col:3> 'void (int)' lvalue Function 0x31c8110 'my_func' 'void (int)'
        | `-ImplicitCastExpr 0x31d15b0 <col:11> 'int' <LValueToRValue>
        |   `-DeclRefExpr 0x31d14e8 <col:11> 'int' lvalue Var 0x31c8320 'n' 'int'
        `-ReturnStmt 0x31d15e8 <line:21:3, col:10>
          `-IntegerLiteral 0x31d15c8 <col:10> 'int' 0