【问题标题】:Can one print the QualType of function pointers with a name using Clang AST?可以使用 Clang AST 打印具有名称的函数指针的 QualType 吗?
【发布时间】:2018-02-06 05:51:04
【问题描述】:
是否有任何简单可靠的方法(也称为不是正则表达式)将函数指针声明的QualType 作为字符串但附加名称?我尝试研究利用 QualType::getAsString(const PrintingPolicy &Policy),但不幸的是,该配置没有选项。
例如
int (*)(void) ----> int (*whatever_name_here)(void)
【问题讨论】:
标签:
c++
clang
function-pointers
clang++
llvm-clang
【解决方案1】:
您可以创建VarDecl 并打印它。
假设ctx 是您的ASTContext 并且type 是您的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());