1

I need to write a regex which will match only lines with the C function call, not its declaration.

So, I need it to match only lines, where funcName() is not preceeded by int, double, float, char etc. and an arbitrary number of spaces.


The problem is, I can run into following expressions:

printf("Hello"); int f() {return 1;};

So I must consider even the situation, where there are some other characters before the date-type name.

myStruct f();

In this situation I want regex to match it, ONLY basic data-types should be excluded.


So far I've got to this expression:

^(?!(void|int|double|char))\s*f\(\).*$

But I have no idea, how to take care of the situation with characters before the type name.

Eenoku
  • 2,741
  • 4
  • 32
  • 64
  • If your line ends with semi-colon, it means it's not a function declaration, am I correct ? – Martin Verjans Feb 26 '16 at 11:51
  • 1
    You should be aware that any general solution needs to be hooked in between the preprocessor and the parser ( or must be a full-fledged preprocessor on its own ). – collapsar Feb 26 '16 at 11:51
  • @collapsar I'm aware of that, I want to use it just for some very primitive cases. – Eenoku Feb 26 '16 at 11:54
  • Your example does not appear to be valid C. Should you be able to parse it nevertheless? – Jongware Feb 26 '16 at 11:55
  • @RadLexus Not necessary, the parsed code will be valid. Did you mean the missing semicolon in the first example? – Eenoku Feb 26 '16 at 11:57
  • Can you be more specific about the cases you wish to cover? Note that C allows for arbitrary whitespace between tokens, so catching `int \n f();` would require you not to limit yourself to testing within single lines. ( `\n` meaning at least one physical newline character ) – collapsar Feb 26 '16 at 11:59
  • `int f() {..}` looks like a function declaration to me. While allowed in GCC as an extension, it [is not standard C](http://stackoverflow.com/questions/957592/functions-inside-functions-in-c). Also, a missing function argument. Also, the function after that declaration ought not to end with `};`. – Jongware Feb 26 '16 at 12:12
  • Functions can return structs and typedef types, so `myStruct f();` looks more like a declaration than a call to me. Note that it is nigh impossible to parse the C grammar with regex. Whatever regex you come up with it most probably won't be able to cover all cases. – Klas Lindbäck Feb 26 '16 at 12:22

1 Answers1

0

The following regex meets your specs:

(^|((^|\s)(?!(void|int|double|char))[^\s]+)\s+)([a-zA-Z_]+\(\)?)
  • The function name is defined by a character class containing letters and the underscore.
  • The line starts with the function call, or
  • the line contains at least one non-whitespace character before the function name. In that case ...
    • this non-WS sequence does not match the excluded keywords
    • there is at least 1 WS character before the function name

See the live demo at regex101.

Caveat

As several commentors have noted, this is not a robust solution. It will work for a tightly constrained set of function call and declaration patterns only.

A general regex-based solution (if possible at all, which would heavily depend on the regex engine features available) will be of theoretical interest only as it had to mimic completely the C preprocessor.

collapsar
  • 17,010
  • 4
  • 35
  • 61