[cfe-commits] r137823 - in /cfe/trunk: lib/AST/Expr.cpp test/SemaCXX/overloaded-operator.cpp test/SemaCXX/warn-unused-comparison.cpp test/SemaTemplate/overload-uneval.cpp
Chandler Carruth
chandlerc at gmail.com
Wed Aug 17 02:49:44 PDT 2011
Author: chandlerc
Date: Wed Aug 17 04:49:44 2011
New Revision: 137823
URL: http://llvm.org/viewvc/llvm-project?rev=137823&view=rev
Log:
Whitelist operator== and operator!= as valid for unused value warnings,
even when overloaded and user-defined. These operators are both more
valuable to warn on (due to likely typos) and extremely unlikely to be
reasonable for use to trigger side-effects.
Modified:
cfe/trunk/lib/AST/Expr.cpp
cfe/trunk/test/SemaCXX/overloaded-operator.cpp
cfe/trunk/test/SemaCXX/warn-unused-comparison.cpp
cfe/trunk/test/SemaTemplate/overload-uneval.cpp
Modified: cfe/trunk/lib/AST/Expr.cpp
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/lib/AST/Expr.cpp?rev=137823&r1=137822&r2=137823&view=diff
==============================================================================
--- cfe/trunk/lib/AST/Expr.cpp (original)
+++ cfe/trunk/lib/AST/Expr.cpp Wed Aug 17 04:49:44 2011
@@ -1524,8 +1524,21 @@
R2 = cast<ArraySubscriptExpr>(this)->getRHS()->getSourceRange();
return true;
+ case CXXOperatorCallExprClass: {
+ // We warn about operator== and operator!= even when user-defined operator
+ // overloads as there is no reasonable way to define these such that they
+ // have non-trivial, desirable side-effects. See the -Wunused-comparison
+ // warning: these operators are commonly typo'ed, and so warning on them
+ // provides additional value as well. If this list is updated,
+ // DiagnoseUnusedComparison should be as well.
+ const CXXOperatorCallExpr *Op = cast<CXXOperatorCallExpr>(this);
+ if (Op->getOperator() == OO_EqualEqual ||
+ Op->getOperator() == OO_ExclaimEqual)
+ return true;
+
+ // Fallthrough for generic call handling.
+ }
case CallExprClass:
- case CXXOperatorCallExprClass:
case CXXMemberCallExprClass: {
// If this is a direct call, get the callee.
const CallExpr *CE = cast<CallExpr>(this);
Modified: cfe/trunk/test/SemaCXX/overloaded-operator.cpp
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/test/SemaCXX/overloaded-operator.cpp?rev=137823&r1=137822&r2=137823&view=diff
==============================================================================
--- cfe/trunk/test/SemaCXX/overloaded-operator.cpp (original)
+++ cfe/trunk/test/SemaCXX/overloaded-operator.cpp Wed Aug 17 04:49:44 2011
@@ -36,7 +36,7 @@
bool operator==(A&, Z&); // expected-note 3{{candidate function}}
void h(A a, const A ac, Z z) {
- make_A() == z;
+ make_A() == z; // expected-warning{{equality comparison result unused}}
a == z; // expected-error{{use of overloaded operator '==' is ambiguous}}
ac == z; // expected-error{{invalid operands to binary expression ('const A' and 'Z')}}
}
@@ -45,7 +45,7 @@
bool operator==(const B&) const;
void test(Z z) {
- make_A() == z;
+ make_A() == z; // expected-warning{{equality comparison result unused}}
}
};
Modified: cfe/trunk/test/SemaCXX/warn-unused-comparison.cpp
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/test/SemaCXX/warn-unused-comparison.cpp?rev=137823&r1=137822&r2=137823&view=diff
==============================================================================
--- cfe/trunk/test/SemaCXX/warn-unused-comparison.cpp (original)
+++ cfe/trunk/test/SemaCXX/warn-unused-comparison.cpp Wed Aug 17 04:49:44 2011
@@ -19,13 +19,13 @@
p == p; // expected-warning {{equality comparison result unused}} \
// expected-note {{use '=' to turn this equality comparison into an assignment}} \
// expected-warning {{self-comparison always evaluates to true}}
- a == a; // FIXME: missing-warning {{equality comparison result unused}} \
- // FIXME: missing-note {{use '=' to turn this equality comparison into an assignment}}
- a == b; // FIXME: missing-warning {{equality comparison result unused}} \
- // FIXME: missing-note {{use '=' to turn this equality comparison into an assignment}}
- a != b; // FIXME: missing-warning {{inequality comparison result unused}} \
- // FIXME: missing-note {{use '|=' to turn this inequality comparison into an or-assignment}}
- A() == b; // FIXME: missing-warning {{equality comparison result unused}}
+ a == a; // expected-warning {{equality comparison result unused}} \
+ // expected-note {{use '=' to turn this equality comparison into an assignment}}
+ a == b; // expected-warning {{equality comparison result unused}} \
+ // expected-note {{use '=' to turn this equality comparison into an assignment}}
+ a != b; // expected-warning {{inequality comparison result unused}} \
+ // expected-note {{use '|=' to turn this inequality comparison into an or-assignment}}
+ A() == b; // expected-warning {{equality comparison result unused}}
if (42) x == 7; // expected-warning {{equality comparison result unused}} \
// expected-note {{use '=' to turn this equality comparison into an assignment}}
else if (42) x == 7; // expected-warning {{equality comparison result unused}} \
Modified: cfe/trunk/test/SemaTemplate/overload-uneval.cpp
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/test/SemaTemplate/overload-uneval.cpp?rev=137823&r1=137822&r2=137823&view=diff
==============================================================================
--- cfe/trunk/test/SemaTemplate/overload-uneval.cpp (original)
+++ cfe/trunk/test/SemaTemplate/overload-uneval.cpp Wed Aug 17 04:49:44 2011
@@ -1,4 +1,4 @@
-// RUN: %clang_cc1 -fsyntax-only -verify %s
+// RUN: %clang_cc1 -fsyntax-only -verify -Wno-unused %s
// Tests that overload resolution is treated as an unevaluated context.
// PR5541
More information about the cfe-commits
mailing list