[PATCH] clang-tidy checker that enforce proper parentheses in macros

Daniel Marjamäki daniel.marjamaki at evidente.se
Wed May 6 07:16:17 PDT 2015


Hi aaron.ballman, silvas,

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.

http://reviews.llvm.org/D9528

Files:
  clang-tidy/misc/CMakeLists.txt
  clang-tidy/misc/MacroParenthesesCheck.cpp
  clang-tidy/misc/MacroParenthesesCheck.h
  clang-tidy/misc/MiscTidyModule.cpp
  test/clang-tidy/misc-macro-parentheses.cpp

EMAIL PREFERENCES
  http://reviews.llvm.org/settings/panel/emailpreferences/
-------------- next part --------------
A non-text attachment was scrubbed...
Name: D9528.25044.patch
Type: text/x-patch
Size: 10403 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/cfe-commits/attachments/20150506/30ee6bdb/attachment.bin>


More information about the cfe-commits mailing list