This clang-tidy checker will make sure macros have proper parentheses.
I previously made a compiler-check for this but that only warned when bad usage was seen. This clang-tidy checker warns when dangerous macros are seen.
The rules are:
- Expressions must be surrounded with parentheses.
- Arguments must be surrounded with parentheses.
I have tested this on 193 debian projects so far. there was 47668 warnings. I have not looked at every warning in detail but overall it looks very good. The majority of the warnings point out dangerous macros that can be misused, and I therefore classify them as true positives. Maybe there are some 1-5% that are "false positives", in most of these cases it would not hurt to add parentheses.
Example false positives:
#define A (INT)&a
Is the & a bitand or a address-of operator. It's not trivial to know when looking at the tokens. Putting parentheses around the macro doesn't hurt.
#define A INT*
... I warn about this. Can be fixed by using typedef instead. But maybe user wants to have it this way.
and then sometimes there are warnings inside ({..}) that I think ideally would not be shown. I could bailout for instance when a { is seen in the macro, but most warnings are true positives so I think such bailout would mostly cause false negatives.
Please add a class comment describing the purpose of this check, motivation for it and what specific code patterns it triggers on.