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/
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 "
_______________________________________________
cfe-commits mailing list
[email protected]
http://lists.cs.uiuc.edu/mailman/listinfo/cfe-commits

Reply via email to