Sourav Mukherjee
Sourav Mukherjee

Reputation: 309

getting parameter information from FunctionDecl class in clang

How to get parameter information as a string from FunctionDecl class in clang . I'm trying but getting confused by so many inheritances. Also they compiler is saying that getReturnType() is not a member of FunctionDecl but doxygen documentation says otherwise . Please help. http://clang.llvm.org/doxygen/classclang_1_1FunctionDecl.html

using namespace std;
using namespace clang;
using namespace clang::driver;
using namespace clang::tooling;
using namespace llvm;

.......
class ExampleVisitor : public RecursiveASTVisitor<ExampleVisitor> 
{
    ......
    virtual bool VisitFunctionDecl(FunctionDecl *func) 
    {
            numFunctions++;
            string funcName = func->getNameInfo().getName().getAsString();
            string retName = func->getReturnType().getAsString();
            ...
            return true;
    }

}

Errors:-

‘class clang::FunctionDecl’ has no member named ‘getReturnType’

Upvotes: 7

Views: 3161

Answers (4)

jw_
jw_

Reputation: 1785

Here is a working function to print all the information about a FunctionDecl.Tested on Windows/LLVM6.0.0

void printFunctionDecl(FunctionDecl* f) 
{
    std::cout << "FunctionDecl@:"<<f<<":"
        << f->getReturnType().getAsString()<<" "
        << f->getQualifiedNameAsString()
        <<"(";

    for (int i = 0; i < f->getNumParams(); i++)
    {
        if (i > 0) std::cout << ",";             
        std::cout 
            << QualType::getAsString(f->parameters()[i]->getType().split()
                , PrintingPolicy{ {} })<<" "
            << f->parameters()[i]->getQualifiedNameAsString();          
    }

    std::cout << ")"
        <<"   Definition@"<<f->getDefinition()
        <<"\n";
}

Upvotes: 1

user1553539
user1553539

Reputation: 21

try this,

getResultType() 

rather than

getReturnType()

llvm 3.4 has no member getReturnType() but identical function whose name is getResultType() exists.

Upvotes: 2

S S
S S

Reputation: 1020

To get all parameter list dynamically, below code would help.

string retName = func->getReturnType().getAsString();
for(int i=0; i<func->getNumParams(); i++)
{
    std::cout << " " << func->parameters()[i]->getQualifiedNameAsString();
}
...     

Upvotes: 0

Marco A.
Marco A.

Reputation: 43662

Depending if you need a qualified or unqualified name, you can stringify your return type and parameter names as follows

std::string retType = F->getReturnType().getAsString();
std::string arg0;
if(F->getNumParams() > 0)
  arg0 = F->parameters()[0]->getQualifiedNameAsString();

Check out the getAsString() method provided.


Edit: after your comments I figured out that you don't have the latest Clang source code. Please check it out before retrying. Good luck!

Upvotes: 3

Related Questions