LLVM Discussion Forums

How can I get type name of a `ValueDecl` and `ParmVarDecl` in functions of ASTVisitor?

I am a beginner. Recently I am trying to check naming of variables in source file with libtooling. Now I have a way to get it (like below), but is there a better way to retrieve it straightforwardly with API, like getPreviousIdentifier() or getTypeName() ?

Now I do it with this way:

// CheckIt.cpp 
auto BeginLoc = pParamVarDecl->getBeginLoc();
auto CurrLoc  = pParamVarDecl->getLocation();
const char      *szBegin = pSrcMgr->getCharacterData(BeginLoc );
const char      *szCurr  = pSrcMgr->getCharacterData(CurrLoc);
const intptr_t  iPtrLen  = szCurr - szBegin;
std::string TypeName  = std::string(szBegin, iPtrLen);


// InputSrc.cpp
   int    iValue = 10;
// ^^^^^^^   <-- The TypeName I got.


Reply to myself.

I found the function, getType() if the Decl is derived from ValueDecl. For example:

std::string TypeName = pValueDecl->getType().getAsString();