[PATCH] -Wcomma, a new warning for questionable uses of the comma operator

Arthur O'Dwyer arthur.j.odwyer at gmail.com
Fri May 30 21:26:42 PDT 2014


The name "-Wcomma" is pretty generic; I wonder if you could give it a
more specific name. (But then again, "-Wquestionable-comma-usage" or
whatever isn't any more helpful.)

Please make sure the warning doesn't trigger on parameter-pack
expansions that are being used for their cardinality; for example,
this case from MPL11:

    template <bool ...> struct bool_seq;
    template <typename ...xs> using and_ =
      std::is_same< bool_seq<xs::value...>, bool_seq<(xs::value, true)...> >;

    static_assert(and_<std::true_type, std::true_type,
std::true_type>::value == true, "");
    static_assert(and_<std::true_type, std::false_type,
std::true_type>::value == false, "");

The expression "(xs::value, true)" might seem silly, but because it's
followed by "..." it's actually sensible.

I feel like there are other cases involving assert-esque macros that
would be problematic, but I can't think of any good examples off the
top of my head.

HTH,
Arthur


On Fri, May 30, 2014 at 8:03 PM, Richard Trieu <rtrieu at google.com> wrote:
> -Wcomma emits a warning when there is a questionable use of the comma operator.  It does this by only allowing certain expressions on the LHS of the comma operator, with all other expressions giving a warning.  The current whitelisted expressions are increments, decrements, assignments, compound assignments, overloaded versions of these operators, and void returning functions.  Some examples of code that will be detected:
>
>   int foo();
>   if (foo(), 5) {} // should be "=="
>   cout << "foo is " , foo();  // should be "<<"
>
>   void bar(int);
>   void bar(int, int);
>   bar((foo(), foo()));  // Too many parens, calls the one argument function
>
> http://reviews.llvm.org/D3976
>
> Files:
>   include/clang/Basic/DiagnosticSemaKinds.td
>   lib/Sema/SemaExpr.cpp
>   test/SemaCXX/warn-comma-operator.cpp
>
> _______________________________________________
> cfe-commits mailing list
> cfe-commits at cs.uiuc.edu
> http://lists.cs.uiuc.edu/mailman/listinfo/cfe-commits
>




More information about the cfe-commits mailing list