Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to get function name using FunctionDecl *D in clang

In one of my checker, i am using FunctionDecl class to get the function declaration. Now i want to get the name of the function for which i enter into the checkASTDecl method. As we know that in checkASTDecl() we get pointer of class FunctionDecl. So, can any one help me with way to get the name of function for which i entered into checkASTDecl.

Here is the sample code i have written:

namespace {

    class FuncPrototypeChecker :  public Checker<check::ASTDecl<FunctioeDecl> > {
            mutable OwningPtr<BugType> TernaryOperatorBug;

            public:
            void checkASTDecl(const FunctionDecl *D,
                AnalysisManager &mgr, BugReporter &BR) const;

    };

}

void FuncPrototypeChecker::checkASTDecl(const FunctionDecl *D,
                                    AnalysisManager &mgr,
                                    BugReporter &BR) const {
/* Get the name of the function from FunctionDecl *D */
}

I want to get the name of the function for which i entered the method FuncPrototypeChecker::checkASTDecl(). Please help me with the way i can achieve it. Thanks in advance.

like image 435
user1497818 Avatar asked Dec 20 '13 05:12

user1497818


1 Answers

In fact, clang has a class to store names of decls (not just a FunctionDecl), called DeclarationNameInfo (see clang api for DeclarationNameInfo)

You can get a DeclarationNameInfo instance for a FunctionDecl using the API call:

functionDecl->getNameInfo();

and the get the name as a string by:

functionDecl->getNameInfo().getAsString();

The result returned will be a std::string.

There is more information about the FunctionDecl API available in clang api for FunctionDecl.

like image 146
wengsht Avatar answered Oct 13 '22 05:10

wengsht