Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Creating parameters for a function with clang

Tags:

llvm

clang

I have source code which looks like this,

void update();

void update()
{
}

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)
{
}

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

MyRecursiveASTVisitor::VisitFunctionDecl(FunctionDecl *f) 

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);

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.

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.

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

like image 697
excray Avatar asked May 25 '12 01:05

excray


1 Answers

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.

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.

like image 63
Richard Smith Avatar answered Sep 29 '22 10:09

Richard Smith