可以使用Clang AST打印名称的QualType函数指针吗?

问题描述 投票:4回答:1

是否有任何简单可靠的方法(a.k.a.而不是正则表达式)将函数指针声明的QualType作为字符串但附加了名称?我试图利用QualType::getAsString(const PrintingPolicy &Policy),但不幸的是,没有选择配置。

EG

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

c++ clang function-pointers clang++ llvm-clang
1个回答
0
投票

你可以创建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());
© www.soinside.com 2019 - 2024. All rights reserved.