title: clang - getting body of the function
body: <p>I'm trying to get the entire code of the function body.</p>
  
  <p>I have the following code</p>
  
  <pre><code>    bool VisitFunctionDecl(FunctionDecl *f) {
        Stmt *FuncBody = f-&gt;getBody();
        stringstream SSAfter;
        SSAfter &lt;&lt; f-&gt;getBody();
        SourceLocation ST = f-&gt;getSourceRange().getBegin();
        ST = FuncBody-&gt;getLocEnd().getLocWithOffset(1);
        TheRewriter.InsertText(ST, SSAfter.str(), true, true);
     }
  </code></pre>
  
  <p>I have the following code example</p>
  
  <pre><code>int multiplyOrSum (int a, int b, bool t)
  {
      int c=0;
      if (a&lt;=0){
          return 0;
      }
      if (t){
          c= a*b;
      }
      else {
          c = a+b;
      }
      __asm
      {
          jz  _P01
          jnz _P01
      //  _emit 0e9h
      _P01:
      }
  
      return (c + multiplyOrSum(a-1, b, t)+ loopOne (-1));
  }
  </code></pre>
  
  <p>After parsing with the code that i provided. The outcome is</p>
  
  <pre><code>int multiplyOrSum (int a, int b, bool t)
  
  {
      int c=0;
      if (a&lt;=0){
          return 0;
      }
      if (t){
          c= a*b;
      }
      else {
          c = a+b;
      }
      __asm
      {
          jz  _P01
          jnz _P01
      //  _emit 0e9h
      _P01:
      }
      return (c + multiplyOrSum(a-1, b, t)+ loopOne (-1));
  }
  02053B28
  </code></pre>
  
  <p>What i want to achieve is</p>
  
  <pre><code>int multiplyOrSum (int a, int b, bool t)
  
      {
          int c=0;
          if (a&lt;=0){
              return 0;
          }
          if (t){
              c= a*b;
          }
          else {
              c = a+b;
          }
          __asm
          {
              jz  _P01
              jnz _P01
          //  _emit 0e9h
          _P01:
          }
          return (c + multiplyOrSum(a-1, b, t)+ loopOne (-1));
      }
  
      {
          int c=0;
          if (a&lt;=0){
              return 0;
          }
          if (t){
              c= a*b;
          }
          else {
              c = a+b;
          }
          __asm
          {
              jz  _P01
              jnz _P01
          //  _emit 0e9h
          _P01:
          }
          return (c + multiplyOrSum(a-1, b, t)+ loopOne (-1));
      }
  </code></pre>
  
  <p>Please advice. Thanks a lot</p>
  
  <p>EDIT : Basically what i want to do is to duplicate the function body.</p>
  
  <p>EDIT 2 :</p>
  
  <p>I have tried the following code with some editing to my original</p>
  
  <pre><code>bool VisitFunctionDecl(FunctionDecl *f) {
        Stmt *FuncBody = f-&gt;getBody();
        stringstream SSAfter;
        SSAfter &lt;&lt; f-&gt;getBody();
        LangOptions LangOpts;
        LangOpts.CPlusPlus = true;
        PrintingPolicy Policy(LangOpts);
        std::string s;
        llvm::raw_string_ostream as(s);
        FuncBody-&gt;printPretty(as, 0, Policy);
        SSAfter &lt;&lt; as.str() &lt;&lt; "\n";
        SourceLocation ST = f-&gt;getSourceRange().getBegin();
        ST = FuncBody-&gt;getLocEnd().getLocWithOffset(1);
        TheRewriter.InsertText(ST, SSAfter.str(), true, true);
     }
  </code></pre>
  
  <p>My output now is </p>
  
  <pre><code>int multiplyOrSum (int a, int b, bool t)
  {
      int c=0;
      if (a&lt;=0){
          return 0;
      }
      if (t){
          c= a*b;
      }
      else {
          c = a+b;
      }
      __asm
      {
          jz  _P01
          jnz _P01
      //  _emit 0e9h
      _P01:
      }
      return (c + multiplyOrSum(a-1, b, t)+ loopOne (-1));
  }
  {
      int c = 0;
      if (a &lt;= 0) {
          return 0;
      }
  }
  </code></pre>
  
  <p>How am i going to get the entire body of the function? I have now gotten the first part. But what about the rest?</p>