使用clang为函数创建参数 [英] Creating parameters for a function with clang

查看:223
本文介绍了使用clang为函数创建参数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有看起来像这样的源代码

I have source code which looks like this,

void update();

void update()
{
}

我正在尝试使用clang解析此代码,并对此进行修改.

Iam trying to parse this code with clang and modify the code to this.

typedef float v4sf attribute ((vector_size(16)));
void update(v4sf& v1, v4sf& v2);

void update(v4sf& v1, v4sf& v2)
{
}

我查看了clang的Rewriter类.在我编写的函数中,如下所示,

I looked at the Rewriter classes of clang. In the function which i wrote as shown below,

MyRecursiveASTVisitor::VisitFunctionDecl(FunctionDecl *f) 

FunctionDecl具有我可以使用的setParams()方法.我将不得不使用此方法创建参数.

FunctionDecl has setParams() method which i could use. I would have to create params with this method.

  static ParmVarDecl *Create(ASTContext &C, DeclContext *DC,
                             SourceLocation StartLoc,
                             SourceLocation IdLoc, IdentifierInfo *Id,
                             QualType T, TypeSourceInfo *TInfo,
                             StorageClass S, StorageClass SCAsWritten,
                             Expr *DefArg);

可以从FunctionDecl获得create函数的前四个参数.我不确定其余的必须是什么.

The first four arguments to the create function can be obtained from FunctionDecl. I am not sure what the rest of them have to be.

如何创建类型并使用clang为其分配值?这些类型不必是内置的,可以与转换后的源代码中添加的类型(v4sf)类似.

How do i create types and also assign values to them in clang? The types need not be builtin and could be the like the one added(v4sf) in transformed source code.

是以这种方式(使用clang方法)进行转换还是可以使用Rewriter.InsertText()添加参数?

Is this way(using clang methods) to do transformations or can i use Rewriter.InsertText() to add the parameters?

推荐答案

Clang不旨在支持AST的变异,并且不支持将AST重新导出为源代码(保留注释,宏和预处理程序指令) .手动添加AST节点很可能会违反AST不变量,从而导致崩溃.您应该根据从AST提取的信息,使用Rewriter重写源代码.

Clang is not designed to support mutation of its AST, and it does not support re-exporting the AST as source code (preserving comments, macros and preprocessor directives). Adding AST nodes manually is likely to violate AST invariants, which can lead to crashes. You should use the Rewriter to perform rewrites of the source code, based on information you extract from the AST.

如果您仍要执行AST修改,则应通过重建要修改的AST部分来实现,而不是在原地进行更改.重建步骤应通过调用Sema上的方法来执行,该方法知道在构建AST时如何提供适当的不变式.

If you still want to perform AST modifications, you should do so by rebuilding the part of the AST you wish to modify, rather than changing it in place. The rebuild steps should be performed by calling methods on Sema, which knows how to provide the appropriate invariants when building the AST.

这篇关于使用clang为函数创建参数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆