Creating parameters for a function with clang

I have the source code that looks like this:

void update(); void update() { } 

I am trying to parse this code with clang and change 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 clang classes. In the function I wrote as shown below

 MyRecursiveASTVisitor::VisitFunctionDecl(FunctionDecl *f) 

FunctionDecl has a setParams () method that I could use. I would have to create parameters using 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'm not sure what the rest should be.

How can I create types and also assign values ​​to them in clang? Types do not have to be inlined and can be similar to the added (v4sf) in the converted source code.

Is this a way (using clang methods) to do the conversion, or can I use Rewriter.InsertText () to add parameters?

+6
source share
1 answer

Clang is not intended to support the mutation of its AST, and it does not support the re-export of AST as source code (saving comments, macros, and preprocessor directives). Adding AST nodes manually can violate AST invariants, which can lead to crashes. You must use Rewriter to rewrite the source code based on the information you extract from the AST.

If you still want to make AST modifications, you must do this by restoring the part of the AST you want to change, rather than changing it in place. Recovery actions must be performed by calling methods on Sema , who knows how to provide the appropriate invariants when constructing the AST.

+6
source

Source: https://habr.com/ru/post/916616/


All Articles