覆盖每个特殊膜函数的天真方法,并在每个特征体中包含printf
或cout
(例如C++: Implicit Member Functions在这里; Vandevoorde and Josuttis称之为“追踪者”),不上诉:
将-E
(或-save-temps
)赋予g ++会导致后者为每个源.ii
(或.cpp
)文件发出.cxx
个文件;在这样的.ii
中,源将使用cpp
预处理器采取的确切步骤进行注释。理想情况下,我希望类似的东西,但源代码注释为
Foo baz;
//// Line 55, choosing Foo::Foo(); implicit; empty body
Foo qux(Bar::mkFoo(42));
//// Line 56, choosing Foo::Foo(int)
//// Line 56, choosing Foo::Foo(const Foo&)
//// Line 56, choosing Foo::~Foo(); implicit
Foo qux2(std::move(Bar::mkFoo(4.2)));
//// Line 57, choosing Foo::Foo(double)
//// Line 57, choosing Foo::Foo(Foo&&)
//// Line 57, choosing Foo::~Foo(); implicit
理想的方法将显示为编译器提供的特定优化选项所做的选择。
任何人都知道用g ++实现这种渴望的方法吗?我已经准备好开始编写一个g ++插件,但在重新发明这个特定的轮子之前想要问一下。
如果有办法用clang做到这一点,那也很好。
非常感谢提前。
答案 0 :(得分:2)
我猜你可以在clang之上构建类似的东西。 clang提供了几个库,可以访问所使用的内部数据结构,包括AST。但是,AST应该包含大量信息,并且可能并不明显感兴趣的是什么。
例如,使用此输入
struct foo {
foo();
explicit foo(int);
foo(foo&&);
foo(foo const&);
~foo();
};
struct bar {
static auto mkfoo(int x) -> foo;
};
template <typename T>
auto move(T&& t) -> T&&;
int main() {
foo f0;
foo f1{bar::mkfoo(17)};
foo f2{move(bar::mkfoo(17))};
}
使用内置命令转储AST,即使用命令行
clang -cc1 -std=c++11 -ast-dump ast.cpp
产生以下输出。如果你仔细观察,你会看到你所经历的大部分操作。但是,您可能需要忽略您不感兴趣的细节。
TranslationUnitDecl 0x7fea3b82ccc0 <<invalid sloc>> <invalid sloc>
|-TypedefDecl 0x7fea3b82d200 <<invalid sloc>> <invalid sloc> implicit __int128_t '__int128'
|-TypedefDecl 0x7fea3b82d260 <<invalid sloc>> <invalid sloc> implicit __uint128_t 'unsigned __int128'
|-TypedefDecl 0x7fea3b82d620 <<invalid sloc>> <invalid sloc> implicit __builtin_va_list '__va_list_tag [1]'
|-CXXRecordDecl 0x7fea3b82d670 <ast.cpp:1:1, line:7:1> line:1:8 referenced struct foo definition
| |-CXXRecordDecl 0x7fea3b82d780 <col:1, col:8> col:8 implicit referenced struct foo
| |-CXXConstructorDecl 0x7fea3b82d8b0 <line:2:5, col:9> col:5 used foo 'void (void)'
| |-CXXConstructorDecl 0x7fea3b873260 <line:3:5, col:21> col:14 foo 'void (int)'
| | `-ParmVarDecl 0x7fea3b82d980 <col:18> col:21 'int'
| |-CXXConstructorDecl 0x7fea3b873420 <line:4:5, col:14> col:5 used foo 'void (struct foo &&)'
| | `-ParmVarDecl 0x7fea3b873360 <col:9, col:12> col:14 'struct foo &&'
| |-CXXConstructorDecl 0x7fea3b8735e0 <line:5:5, col:19> col:5 foo 'void (const struct foo &)'
| | `-ParmVarDecl 0x7fea3b873520 <col:9, col:18> col:19 'const struct foo &'
| `-CXXDestructorDecl 0x7fea3b8736f0 <line:6:5, col:10> col:5 used ~foo 'void (void) noexcept'
|-CXXRecordDecl 0x7fea3b8737e0 <line:9:1, line:11:1> line:9:8 struct bar definition
| |-CXXRecordDecl 0x7fea3b8738f0 <col:1, col:8> col:8 implicit struct bar
| `-CXXMethodDecl 0x7fea3b873af0 <line:10:5, col:33> col:17 used mkfoo 'auto (int) -> struct foo' static
| `-ParmVarDecl 0x7fea3b873990 <col:23, col:27> col:27 x 'int'
|-FunctionTemplateDecl 0x7fea3b873e70 <line:13:1, line:14:22> col:6 move
| |-TemplateTypeParmDecl 0x7fea3b873ba0 <line:13:11, col:20> col:20 referenced typename T
| |-FunctionDecl 0x7fea3b873dd0 <line:14:1, col:22> col:6 move 'auto (T &&) -> T &&'
| | `-ParmVarDecl 0x7fea3b873cc0 <col:11, col:15> col:15 t 'T &&'
| `-FunctionDecl 0x7fea3b8757b0 <col:1, col:22> col:6 used move 'auto (struct foo &&) -> struct foo &&'
| |-TemplateArgument type 'struct foo'
| `-ParmVarDecl 0x7fea3b8756b0 <col:11, col:15> col:15 t 'struct foo &&'
`-FunctionDecl 0x7fea3b873f10 <line:16:1, line:20:1> line:16:5 main 'int (void)'
`-CompoundStmt 0x7fea3b875a48 <col:12, line:20:1>
|-DeclStmt 0x7fea3b8740e0 <line:17:5, col:11>
| `-VarDecl 0x7fea3b874020 <col:5, col:9> col:9 f0 'struct foo' callinit
| `-CXXConstructExpr 0x7fea3b874078 <col:9> 'struct foo' 'void (void)'
|-DeclStmt 0x7fea3b875398 <line:18:5, col:27>
| `-VarDecl 0x7fea3b874110 <col:5, col:26> col:9 f1 'struct foo' listinit
| `-ExprWithCleanups 0x7fea3b875380 <col:9, col:26> 'struct foo'
| `-CXXConstructExpr 0x7fea3b875348 <col:9, col:26> 'struct foo' 'void (struct foo &&)' elidable
| `-MaterializeTemporaryExpr 0x7fea3b875330 <col:12, col:25> 'struct foo' xvalue
| `-CXXBindTemporaryExpr 0x7fea3b8752c8 <col:12, col:25> 'struct foo' (CXXTemporary 0x7fea3b8752c0)
| `-CallExpr 0x7fea3b875290 <col:12, col:25> 'struct foo'
| |-ImplicitCastExpr 0x7fea3b875278 <col:12, col:17> 'auto (*)(int) -> struct foo' <FunctionToPointerDecay>
| | `-DeclRefExpr 0x7fea3b8741b8 <col:12, col:17> 'auto (int) -> struct foo' lvalue CXXMethod 0x7fea3b873af0 'mkfoo' 'auto (int) -> struct foo'
| `-IntegerLiteral 0x7fea3b875200 <col:23> 'int' 17
`-DeclStmt 0x7fea3b875a30 <line:19:5, col:33>
`-VarDecl 0x7fea3b8753c0 <col:5, col:32> col:9 f2 'struct foo' listinit
`-ExprWithCleanups 0x7fea3b875a18 <col:9, col:32> 'struct foo'
`-CXXConstructExpr 0x7fea3b8759e0 <col:9, col:32> 'struct foo' 'void (struct foo &&)'
`-CallExpr 0x7fea3b875950 <col:12, col:31> 'struct foo':'struct foo' xvalue
|-ImplicitCastExpr 0x7fea3b875938 <col:12> 'auto (*)(struct foo &&) -> struct foo &&' <FunctionToPointerDecay>
| `-DeclRefExpr 0x7fea3b8758b0 <col:12> 'auto (struct foo &&) -> struct foo &&' lvalue Function 0x7fea3b8757b0 'move' 'auto (struct foo &&) -> struct foo &&' (FunctionTemplate 0x7fea3b873e70 'move')
`-MaterializeTemporaryExpr 0x7fea3b875980 <col:17, col:30> 'struct foo':'struct foo' xvalue
`-CXXBindTemporaryExpr 0x7fea3b875558 <col:17, col:30> 'struct foo' (CXXTemporary 0x7fea3b875550)
`-CallExpr 0x7fea3b875518 <col:17, col:30> 'struct foo'
|-ImplicitCastExpr 0x7fea3b875500 <col:17, col:22> 'auto (*)(int) -> struct foo' <FunctionToPointerDecay>
| `-DeclRefExpr 0x7fea3b8754a8 <col:17, col:22> 'auto (int) -> struct foo' lvalue CXXMethod 0x7fea3b873af0 'mkfoo' 'auto (int) -> struct foo'
`-IntegerLiteral 0x7fea3b8754e0 <col:28> 'int' 17