9

在我的一个检查器中,我使用 FunctionDecl 类来获取函数声明。现在我想获取我在 checkASTDecl 方法中输入的函数的名称。正如我们所知,在 checkASTDecl() 中,我们得到类 FunctionDecl 的指针。那么,任何人都可以帮助我获取我输入 checkASTDecl 的函数名称吗?

这是我编写的示例代码:

命名空间{

    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 */
}

我想获取我为其输入方法 FuncPrototypeChecker::checkASTDecl() 的函数的名称。请帮助我实现它的方式。提前致谢。

4

1 回答 1

10

实际上,clang 有一个类来存储 decls 的名称(不仅仅是 a FunctionDecl),称为DeclarationNameInfo(参见clang api for DeclarationNameInfo

您可以使用 API 调用获取一个DeclarationNameInfo实例:FunctionDecl

functionDecl->getNameInfo();

并通过以下方式将名称作为字符串获取:

functionDecl->getNameInfo().getAsString();

返回的结果将是一个std::string.

关于FunctionDecl 的 clang api 中FunctionDecl可用的 API 的更多信息。

于 2014-03-10T02:28:55.863 回答