C++ 编译器输出

问题描述 投票:0回答:1

我想知道是否有一个工具/GCC 编译开关可以用来在运行前查看 GCC 编译器的跟踪/输出。一般来说,我希望有一个工具能够注释 GCC 编译器如何解析我的代码并显示所有预先计算的静态值/类型。

示例:以我在这里写的metaFactoril为例,http://pastebin.com/N6eN9jNx .

某些工具可以显示调用 MetaFactorial<5>::value 时递归调用和替换值的扩展吗?

c++ gcc metaprogramming compile-time
1个回答
0
投票

gcc 确实允许转储中间文件和各种其他诊断工具,但它并不真正适合“普通人”阅读。有十多个

-fdump-tree-*
选项,我希望其中一个能够满足您的需求。
-fdump-tree-original
看起来不错,但它只显示 120 作为最终结果 - 我没有尝试任何其他变体。

使用

clang++ -Xclang -ast-dump
你会得到这个(带有颜色),在我看来读起来很清楚。

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
© www.soinside.com 2019 - 2024. All rights reserved.