0

I want to modify C source code like this: I wany to modify loop condition in AST, how can I do this?

for example:

for(int i = 0; i < 10; i ++){...}

change to :

for (int i = 0; i < 1; i ++){...}

I get the forStmt from AST by ASTMatcher in clang tools and i also get the conditon:

  auto cond = forstmt -> getCond();
  if(cond){
    if(auto bo = dyn_cast<BinaryOperator>(cond)){
       auto borhs = bo -> getRHS();
       if (auto condvar = dyn_cast<IntegerLiteral>(borhs)){
         // how can i modify this condvar and dump ast to out.ast file?
         string var_value = condvar -> getValue().toString(10, 1);
       }
    }
  }

1 Answers1

-1

I got it from the Microsoft C++ Team Blog Exploring Clang Tooling Part 3: Rewriting Code with clang-tidy

Using clang-tidy and writing a clang tidy FixInt:CreatReplacement

Nimantha
  • 6,405
  • 6
  • 28
  • 69