title: Tools to expand or inline sub-functions in C-code
body: <p><strong>Background:</strong>
  Often I have to deal with C source code without any documentation during firmware development/maintenance. When the code deals with big data structures where the data members initialization scattered all over the place, it becomes very challenging to jump around different source and header files and often I got lost.</p>
  
  <p>So my current solution is to spend several days to "flatten out" the code. I don't know what the appropriate term, but basically what I do is expanding the sub-functions and replacing the input parameters variable names with the actual variable names that passed into the sub-function. (Please see an oversimplified example below)</p>
  
  <p>The outcome of this exercise is very helpful, but it is a very manual and tedious process. Since I'm doing firmware, on some chips that supports trace capability, it is possible to do this using JTAG debugger. But this set up is not always available. </p>
  
  <p>I know with macros I can use the preprocessor to do this, but I couldn't figure out how to do it with sub-functions, if such a tool exists. I tried Googling 'subfunction expansion' etc., so far no luck. Please let me know if you know such a tool exists or the proper term(s) of what I'm trying to do so I can search it better on the Internet. Thanks a lot!</p>
  
  <pre><code>int myGlobal;
  
  void increment(int input)
  {
      input++;
  }
  
  void decrement(int input)
  {
      input--;
  }
  
  int doSomething(int input, int op)
  {
      if (op)
      {
          increment(input);
      }
      else
      {
          decrement(input);
      }
  }
  
  int main(void)
  {
      int i, currMax;
      int myOp = 1;
  
      myGlobal = 0;
      currMax = 5;
      for (i = 0; i &lt; currMax; i++)
      {
          doSomething(myGlobal, myOp);
      }
  
      currMax = 4;
      myOp = 0;
      for (i = 0; i &lt; currMax; i++)
      {
          doSomething(myGlobal, myOp);
      }
  
  }
  </code></pre>
  
  <p><strong>BECOMES</strong></p>
  
  <pre><code>int main(void)
  {
      int i, currMax;
      int myOp = 1;
  
      myGlobal = 0;
      currMax = 5;
      for (i = 0; i &lt; currMax; i++)
      {
          // doSomething(myGlobal, myOp);
          if (myOp)
          {
              // increment(myGlobal);
              myGlobal++;
          }
          else
          {
              decrement(myGlobal);
              myGlobal--;
          }
      }
  
      currMax = 4;
      myOp = 0;
      for (i = 0; i &lt; currMax; i++)
      {
          // doSomething(myGlobal, myOp);
          if (myOp)
          {
              // increment(myGlobal);
              myGlobal++;
          }
          else
          {
              // decrement(myGlobal);
              myGlobal--;
          }
      }
  
  }
  </code></pre>