jhauberg/comply

[split-by-name] should not match #define macros

Closed this issue · 3 comments

For example:

screen shot 2018-05-23 at 10 36 15

There is a deeper issue here and this particular line should never have been matched in the first place.

The reason this is matched, is that further down below this line, there's an actual function with an opening brace ({), and this #define is being matched all the way down to that brace- which coincidentally cause the real function to be missed, as it would otherwise overlap. See #50 for similar.

Here's the actual code:

#define YouMessage(pointer, prefix, text) \
    strcat((YouPrefix(pointer, prefix, text), pointer), text)

/*VARARGS1*/
void You
VA_DECL(const char *, line)
{
    char *tmp;
    VA_START(line);
    VA_INIT(line, const char *);
    vpline(YouMessage(tmp, "You ", line), VA_ARGS);
    VA_END();
}

This is what is matched as one function implementation:

#define YouMessage(pointer, prefix, text) \
    strcat((YouPrefix(pointer, prefix, text), pointer), text)

/*VARARGS1*/
void You
VA_DECL(const char *, line)
{

One way to improve on this is to apply something similar to what we did in the brace-statement-bodies rule with finding balance of braces:

for c in text[ending:]:
if c == '{':
# this body is properly braced
is_missing_opening_brace = False
break
if c in [' ', '\r', '\n']:
# there can be any amount of whitespace or newline before the body
continue
# any other character encountered; must mean that an opening brace is missing
break

So we would find what we expect to be a function implementation, then going by the first opening paren we roll on until determining that we hit the closing paren. From this point we can then determine whether what follows is as expected (a {), or not. If not, discard this match.

This is going into AST territory, but I think doing this would be beneficial for a lot of rules. It could be lazily-loaded in the CheckFile like other properties already are.

Aside:
If we could improve on function matching, and eventually maybe even get rid of using collapsed to strip function bodies, we could gain a nice speedup, as that is currently the time hog.

Another case:

screen shot 2018-06-02 at 13 17 20

Basically fixed as of
0338eb2