-3

Let's that we are looking for a function body of foo inside a string or file. Capturing the signature is easy.

However is there a regex expression that would capture the body of a function. We do not make any assumptions about the internal structure of the function.

It could be something flat like:

int foo()
{
    return i; 
}

Or something layered like this:

void foo()
{
    if(true)
    {
       for (int i=0; i<5; i++ )
       {
           cout<< i << std::endl;
       }
    }  
}

So is there a way to do it?

Alperen AYDIN
  • 547
  • 1
  • 6
  • 17

1 Answers1

1

Regex is incapable of doing that (obligatory link).

Simple counterexample: Regex can't parse arbitrarily nested stuff (see link), and

void foo()
{
  struct Helper
  {
    void bar() {}
  };
}

demonstrates that you can have functions nested in functions arbitrarily deep.

And no, matching opening and closing braces (aside from being yet another thing that regex can't do) is insufficient because of comments, strings/character literals and the preprocessor. For example:

#define EVIL {

void foo()
{
  if (someCondition)
  EVIL

    // Closing } is inside the ifdef (and we have another one in this comment).
#ifdef SOME_DEFINE
    doStuff1("I like strings with unmatched braces }");
  } // Who likes trigraphs ??/
  doStuff2();
#else
    doStuff3();
  }
#endif
}

It's totally legal C++. Good luck with a regex.

In general, C++ is very hard to parse and I would recommend using a tool that is built for the job, such as clang's AST matcher.

Max Langhof
  • 23,383
  • 5
  • 39
  • 72