getting parameter information from FunctionDecl class in clang
Asked Answered
S

4

7

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’

Scarlett answered 23/6, 2014 at 10:29 Comment(8)
Show your code and the errors you get.Scram
question is edited with codeScarlett
Assuming the documentation is correct: clang.llvm.org/doxygen/… Where's that error coming from? Are you sure it's from that line? Can you try to fully qualify it with clang::FunctionDecl ?Comfortable
qualifying didn't help and the error is from that line onlyScarlett
That's weird, please go and check Decl.h for getReturnType(). If you don't have it you might have to update your clang sourcesComfortable
Please search for "getReturnType" inside Decl.hComfortable
Yeah its not defined in Decl.h. I downloaded the source code from github as svn server was not working.Thanks .Scarlett
o.k. sure. How to accept your answer ? I'm new to stack-overflow.Scarlett
C
3

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!

Comfortable answered 23/6, 2014 at 10:46 Comment(2)
Then why is it saying that getReturnType() is not a member of FunctionDecl.Scarlett
Either that error is coming from somewhere else or you're getting the wrong FunctionDecl - GitHub repository mirror (github.com/llvm-mirror/clang/blob/master/include/clang/AST/…)Comfortable
D
2

try this,

getResultType() 

rather than

getReturnType()

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

Dalliance answered 1/10, 2015 at 17:54 Comment(0)
D
1

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";
}
Dutra answered 25/3, 2019 at 13:33 Comment(0)
A
0

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();
}
...     
Accustomed answered 10/7, 2015 at 5:18 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.