简体   繁体   中英

CLang LibTooling handling function Template arguments

I want to handle template arguments diffrently so for the code:

template <class T> class A { 
public:
     A() {} 
};

void faa(A<int>& param);

I would like to know that param is a template specialisation and get access it's parameters.

So I wrote an ASTVisitor with function

 bool VisitFunctionDecl(FunctionDecl *f) {
    std::cout<< "VisitFunctionDecl" <<std::endl;

    const DependentTemplateSpecializationType* t1;
    const TemplateSpecializationType* t2;
    for(ParmVarDecl* p :f->params())
    {

        t1=p->getType()->getAs<DependentTemplateSpecializationType>();

        t2=p->getType()->getAs<TemplateSpecializationType>();

        if(t1!=nullptr||t2!=nullptr)
        {
            std::cout<< "template param found" <<std::endl;
        }
    }

    return true;
}

But those casts are both nullptr always - I never get the template param found output.

What am I doing wrong? Is there any other way to cast t to some king of type allowing checking of the template parameters?

Type of A<int>& is LValueReference (can be checked with getTypeClassName() ). What you are probably trying to get is type pointed by reference. You can get it with getNonReferenceType() method.

bool VisitFunctionDecl(FunctionDecl *f) {
    llvm::errs() << "VisitFunctionDecl:" << f->getQualifiedNameAsString()
            << "\n";
    for (ParmVarDecl* p : f->params()) {

        llvm::errs() << p->getType().getAsString() << " -> "
                << p->getType()->getTypeClassName() << "\n";
        llvm::errs() << "isPointerType: "
                << p->getType()->hasPointerRepresentation() << "\n"
                << "isTemplateSpecialization: "
                << (nullptr != p->getType().getNonReferenceType()->getAs<
                                TemplateSpecializationType>()) << "\n";
    }
    return true;
}

output is:

VisitFunctionDecl:faa
const A<int> & -> LValueReference
isPointerType: 1
isTemplateSpecialization: 1

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM