要扫描的源代码:
template <typename T>
class HB {
T m;
public:
void HBfunc1();
};
template <typename T>
void HB<T>::HBfunc1() {
class HC {
public:
void HCfunc2() { };
};
HC().HCfunc2();
}
void TestTemplate() { HB<int>().HBfunc1(); };
当使用 Clang LibTooling 扫描 AST 时,上面的代码在 VS2019 中运行良好:
virtual bool VisitCallExpr(CallExpr *call) {
std::cout << "VisitCallExpr: ";
if (call->getDirectCallee()) {
std::cout << " "<<call->getDirectCallee()->getQualifiedNameAsString();
}
else {
Decl *callee = call->getCalleeDecl();
if (!callee) {
std::cout << "\nNow dump call:\n";
call->dump();
}
}
std::cout<<"\n";
return true;
}
在扫描源中访问此行的 CallExpr 时:
HC().HCfunc2();
被调用者为 null,CallExpr 的转储为:
VisitCallExpr:
Now dump call:
CallExpr 0x1c2ef83b3a0 '<dependent type>'
`-CXXDependentScopeMemberExpr 0x1c2ef83b348 '<dependent type>' lvalue .HCfunc2
`-CXXUnresolvedConstructExpr 0x1c2ef83b320 'class HC' 'class HC'
Clang 不会在扫描过程中报告错误(因为代码工作正常)。
在 LLVM 源代码中有:
// This represents the type of an expression whose type is
// totally unknown, e.g. 'T::foo'. It is permitted for this to
// appear in situations where the structure of the type is
// theoretically deducible.
BUILTIN_TYPE(Dependent, DependentTy)
为什么HC类型被认为是未知的?在不发出任何警告/错误的情况下扫描 AST 时是否会出现未知类型?如何访问这样的调用并提取有关其被调用者的信息?扫码有问题吗?