假设我有一个像这样的变量声明:
std::vector<MyType> myVector(1);
这表示为Clang AST中的CXXConstructExpr.我有一个匹配器找到这个CXXConstructExpr,但我想从中提取MyType的decl.
我尝试了各种各样的东西,但似乎没有任何作用:
const CXXConstructExpr* construct = Result.Nodes.getNodeAs<CXXConstructExpr>("expr");
construct->getConstructor()->getTemplateSpecializationArgs() // Always nullptr
construct->getConstructor()->getParent() // Seems to lose the template parameters
construct->getConstructor()->getDescribedTemplate() // Always nullptr
最佳答案 这是一个匹配器:
varDecl(
has(
cxxConstructExpr()
)
,hasType(
classTemplateSpecializationDecl().bind(sp_dcl_bd_name_)
)
).bind(var_bd_name_);
它从VarDecl开始并遍历到类型,这是一个埋在向量的ClassTemplateDecl中的ClassTemplateSpecializationDecl.在回调中,可以从ClassTemplateSpecializationDecl工作到模板参数列表,并对各个模板参数进行操作:
using CTSD = ClassTemplateSpecializationDecl;
CTSD * spec_decl =
const_cast<CTSD *>(result.Nodes.getNodeAs<CTSD>(sp_dcl_bd_name_));
VarDecl * var_decl =
const_cast<VarDecl *>(result.Nodes.getNodeAs<VarDecl>(var_bd_name_));
if(spec_decl && var_decl) {
// get the template args
TemplateArgumentList const &tal(spec_decl->getTemplateArgs());
for(unsigned i = 0; i < tal.size(); ++i){
TemplateArgument const &ta(tal[i]);
// is this arg a type arg? If so, get that type
TemplateArgument::ArgKind k(ta.getKind());
std::string argName = "";
if(k==TemplateArgument::ArgKind::Type){
QualType t = ta.getAsType();
argName = t.getAsString();
}
// Could do similar actions for integral args, etc...
std::cout << "For variable declared at "
<< corct::sourceRangeAsString(var_decl->getSourceRange(),&sm) << ":"
<< spec_decl->getNameAsString()
<< ": template arg " << (i+1) << ": " << argName << std::endl;
} // for template args
} // if
对于此代码:
struct B{int b_;};
std::vector<B> vb(1);
这会产生:
For variable declared at <line:14:1, col:20>:vector: template arg 1: struct B
For variable declared at <col:1, col:20>:vector: template arg 2: class std::__1::allocator<struct B>
完整示例位于代码分析和重构与Clang工具示例repo在github:https://github.com/lanl/CoARCT(请参阅apps / TemplateType.cc)