一个人可以使用clang ast使用名称打印功能指针的质量

Can one print the QualType of function pointers with a name using Clang AST?

本文关键字:指针 功能 打印 clang 可以使 ast 一个人      更新时间:2023-10-16

是否有任何简单可靠的方式(又称不正表)将函数指针的 QualType获取为字符串,但是附加了一个名称?我试图考虑利用QualType::getAsString(const PrintingPolicy &Policy),但不幸的是,该配置没有选择。

,例如

int (*)(void) ----> int (*whatever_name_here)(void)

您可以创建VarDecl并打印。

假设ctx是您的ASTContexttype是您的QualType,这是您可以做的:

// Create identifier.
IdentifierInfo id = ctx.Idents.get("whatever_name_here");
// Create variable declaration.
VarDecl *vd = VarDecl::Create(ctx, ctx.getTranslationUnitDecl(), 
    SourceLocation(), SourceLocation(), &id, type, nullptr, StorageClass:SC_None);
// Print it.
vd->print(/* put your llvm::raw_stream here */, ctx.getPrintingPolicy());