[PATCH] fix false positives in clang-tidy macro parentheses checker

Daniel Marjamäki daniel.marjamaki at evidente.se
Tue Jun 23 03:44:23 PDT 2015


Hi alexfh,

I got email from Robert Reif about false positive(s) for such code:

```
struct Fred
{
    enum Foo { First, Second };
};

const char * to_string(Fred::Foo foo, bool verbose)
{
    switch (foo)
    {
#define CASE(a, b)    case Fred::a: if (verbose) return b; else return #a
    CASE(First, "This is the first one");
    CASE(Second, "This is the second one");
#undef CASE
    }
    return "unknown";
}
```

The checker currently warns about both macro arguments a and b. The warning about a is wrong. The warning about b is annoying.

This patch fixes so no warning is shown for that example code.

http://reviews.llvm.org/D10644

Files:
  clang-tidy/misc/MacroParenthesesCheck.cpp
  test/clang-tidy/misc-macro-parentheses.cpp

Index: test/clang-tidy/misc-macro-parentheses.cpp
===================================================================
--- test/clang-tidy/misc-macro-parentheses.cpp
+++ test/clang-tidy/misc-macro-parentheses.cpp
@@ -30,6 +30,8 @@
 #define GOOD18(x)         ;x;
 #define GOOD19            ;-2;
 #define GOOD20            void*
+#define GOOD21(a)         case Fred::a:
+#define GOOD22(a)         if (verbose) return a;
 
 // These are allowed for now..
 #define MAYBE1            *12.34
Index: clang-tidy/misc/MacroParenthesesCheck.cpp
===================================================================
--- clang-tidy/misc/MacroParenthesesCheck.cpp
+++ clang-tidy/misc/MacroParenthesesCheck.cpp
@@ -148,7 +148,7 @@
       continue;
 
     // Argument is a struct member.
-    if (Prev.isOneOf(tok::period, tok::arrow))
+    if (Prev.isOneOf(tok::period, tok::arrow, tok::coloncolon))
       continue;
 
     // String concatenation.
@@ -169,8 +169,8 @@
         TI + 2 != MI->tokens_end() && (TI + 2)->is(tok::r_paren))
       continue;
 
-    // Assignment.
-    if (Prev.is(tok::equal) && Next.is(tok::semi))
+    // Assignment/Return, i.e. '=x;' or 'return x;'.
+    if (Prev.isOneOf(tok::equal, tok::kw_return) && Next.is(tok::semi))
       continue;
 
     Check->diag(Tok.getLocation(), "macro argument should be enclosed in "

EMAIL PREFERENCES
  http://reviews.llvm.org/settings/panel/emailpreferences/
-------------- next part --------------
A non-text attachment was scrubbed...
Name: D10644.28216.patch
Type: text/x-patch
Size: 1332 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/cfe-commits/attachments/20150623/941b67e8/attachment.bin>


More information about the cfe-commits mailing list