How to get function name using FunctionDecl *D in clang

4.5k views Asked by At

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.

1

There are 1 answers

2
wengsht On

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.