r219954 - No longer emit diagnostics about unused results (comparisons, etc) from unevaluated contexts. Fixes PR18571.

Aaron Ballman aaron at aaronballman.com
Thu Oct 16 13:13:28 PDT 2014


Author: aaronballman
Date: Thu Oct 16 15:13:28 2014
New Revision: 219954

URL: http://llvm.org/viewvc/llvm-project?rev=219954&view=rev
Log:
No longer emit diagnostics about unused results (comparisons, etc) from unevaluated contexts. Fixes PR18571.

Added:
    cfe/trunk/test/SemaCXX/warn-unused-value-cxx11.cpp
Modified:
    cfe/trunk/lib/Sema/SemaStmt.cpp
    cfe/trunk/test/SemaCXX/warn-unused-comparison.cpp
    cfe/trunk/test/SemaCXX/warn-unused-result.cpp

Modified: cfe/trunk/lib/Sema/SemaStmt.cpp
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/lib/Sema/SemaStmt.cpp?rev=219954&r1=219953&r2=219954&view=diff
==============================================================================
--- cfe/trunk/lib/Sema/SemaStmt.cpp (original)
+++ cfe/trunk/lib/Sema/SemaStmt.cpp Thu Oct 16 15:13:28 2014
@@ -185,6 +185,12 @@ void Sema::DiagnoseUnusedExprResult(cons
   const Expr *E = dyn_cast_or_null<Expr>(S);
   if (!E)
     return;
+
+  // If we are in an unevaluated expression context, then there can be no unused
+  // results because the results aren't expected to be used in the first place.
+  if (isUnevaluatedContext())
+    return;
+
   SourceLocation ExprLoc = E->IgnoreParens()->getExprLoc();
   // In most cases, we don't want to warn if the expression is written in a
   // macro body, or if the macro comes from a system header. If the offending

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=219954&r1=219953&r2=219954&view=diff
==============================================================================
--- cfe/trunk/test/SemaCXX/warn-unused-comparison.cpp (original)
+++ cfe/trunk/test/SemaCXX/warn-unused-comparison.cpp Thu Oct 16 15:13:28 2014
@@ -83,6 +83,8 @@ void test() {
 #define EQ(x,y) (x) == (y)
   EQ(x, 5);
 #undef EQ
+
+  (void)sizeof(1 < 2, true); // No warning; unevaluated context.
 }
 
 namespace PR10291 {

Modified: cfe/trunk/test/SemaCXX/warn-unused-result.cpp
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/test/SemaCXX/warn-unused-result.cpp?rev=219954&r1=219953&r2=219954&view=diff
==============================================================================
--- cfe/trunk/test/SemaCXX/warn-unused-result.cpp (original)
+++ cfe/trunk/test/SemaCXX/warn-unused-result.cpp Thu Oct 16 15:13:28 2014
@@ -94,3 +94,50 @@ void Bar() {
 };
 
 }
+
+namespace PR18571 {
+// Unevaluated contexts should not trigger unused result warnings.
+template <typename T>
+auto foo(T) -> decltype(f(), bool()) { // Should not warn.
+  return true;
+}
+
+void g() {
+  foo(1);
+}
+}
+
+namespace std {
+class type_info { };
+}
+
+namespace {
+// The typeid expression operand is evaluated only when the expression type is
+// a glvalue of polymorphic class type.
+
+struct B {
+  virtual void f() {}
+};
+
+struct D : B {
+  void f() override {}
+};
+
+struct C {};
+
+void g() {
+  // The typeid expression operand is evaluated only when the expression type is
+  // a glvalue of polymorphic class type; otherwise the expression operand is not
+  // evaluated and should not trigger a diagnostic.
+  D d;
+  C c;
+  (void)typeid(f(), c); // Should not warn.
+  (void)typeid(f(), d); // expected-warning {{ignoring return value}}
+
+  // The sizeof expression operand is never evaluated.
+  (void)sizeof(f(), c); // Should not warn.
+
+   // The noexcept expression operand is never evaluated.
+  (void)noexcept(f(), false); // Should not warn.
+}
+}

Added: cfe/trunk/test/SemaCXX/warn-unused-value-cxx11.cpp
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/test/SemaCXX/warn-unused-value-cxx11.cpp?rev=219954&view=auto
==============================================================================
--- cfe/trunk/test/SemaCXX/warn-unused-value-cxx11.cpp (added)
+++ cfe/trunk/test/SemaCXX/warn-unused-value-cxx11.cpp Thu Oct 16 15:13:28 2014
@@ -0,0 +1,16 @@
+// RUN: %clang_cc1 -std=c++11 -fsyntax-only -verify -Wunused-value %s
+// expected-no-diagnostics
+
+void f() __attribute__((const));
+
+namespace PR18571 {
+// Unevaluated contexts should not trigger unused result warnings.
+template <typename T>
+auto foo(T) -> decltype(f(), bool()) { // Should not warn.
+  return true;
+}
+
+void g() {
+  foo(1);
+}
+}





More information about the cfe-commits mailing list