r176114 - Warn on dropping the return value from a warn_unused_result function, even in

Matt Beaumont-Gay matthewbg at google.com
Tue Feb 26 11:34:08 PST 2013


Author: matthewbg
Date: Tue Feb 26 13:34:08 2013
New Revision: 176114

URL: http://llvm.org/viewvc/llvm-project?rev=176114&view=rev
Log:
Warn on dropping the return value from a warn_unused_result function, even in
macros.

Modified:
    cfe/trunk/lib/Sema/SemaStmt.cpp
    cfe/trunk/test/Sema/unused-expr.c

Modified: cfe/trunk/lib/Sema/SemaStmt.cpp
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/lib/Sema/SemaStmt.cpp?rev=176114&r1=176113&r2=176114&view=diff
==============================================================================
--- cfe/trunk/lib/Sema/SemaStmt.cpp (original)
+++ cfe/trunk/lib/Sema/SemaStmt.cpp Tue Feb 26 13:34:08 2013
@@ -158,9 +158,14 @@ void Sema::DiagnoseUnusedExprResult(cons
   if (!E)
     return;
   SourceLocation ExprLoc = E->IgnoreParens()->getExprLoc();
-  if (SourceMgr.isInSystemMacro(ExprLoc) ||
-      SourceMgr.isMacroBodyExpansion(ExprLoc))
-    return;
+  // 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
+  // expression is a call to a function with the warn_unused_result attribute,
+  // we warn no matter the location. Because of the order in which the various
+  // checks need to happen, we factor out the macro-related test here.
+  bool ShouldSuppress = 
+      SourceMgr.isMacroBodyExpansion(ExprLoc) ||
+      SourceMgr.isInSystemMacro(ExprLoc);
 
   const Expr *WarnExpr;
   SourceLocation Loc;
@@ -193,12 +198,16 @@ void Sema::DiagnoseUnusedExprResult(cons
       return;
 
     // If the callee has attribute pure, const, or warn_unused_result, warn with
-    // a more specific message to make it clear what is happening.
+    // a more specific message to make it clear what is happening. If the call
+    // is written in a macro body, only warn if it has the warn_unused_result
+    // attribute.
     if (const Decl *FD = CE->getCalleeDecl()) {
       if (FD->getAttr<WarnUnusedResultAttr>()) {
         Diag(Loc, diag::warn_unused_result) << R1 << R2;
         return;
       }
+      if (ShouldSuppress)
+        return;
       if (FD->getAttr<PureAttr>()) {
         Diag(Loc, diag::warn_unused_call) << R1 << R2 << "pure";
         return;
@@ -208,7 +217,10 @@ void Sema::DiagnoseUnusedExprResult(cons
         return;
       }
     }
-  } else if (const ObjCMessageExpr *ME = dyn_cast<ObjCMessageExpr>(E)) {
+  } else if (ShouldSuppress)
+    return;
+
+  if (const ObjCMessageExpr *ME = dyn_cast<ObjCMessageExpr>(E)) {
     if (getLangOpts().ObjCAutoRefCount && ME->isDelegateInitCall()) {
       Diag(Loc, diag::err_arc_unused_init_message) << R1;
       return;

Modified: cfe/trunk/test/Sema/unused-expr.c
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/test/Sema/unused-expr.c?rev=176114&r1=176113&r2=176114&view=diff
==============================================================================
--- cfe/trunk/test/Sema/unused-expr.c (original)
+++ cfe/trunk/test/Sema/unused-expr.c Tue Feb 26 13:34:08 2013
@@ -132,6 +132,8 @@ int fn5() __attribute__ ((__const));
 #define M3(a) (t3(a), fn2())
 #define M4(a, b) (foo((a), (b)) ? 0 : t3(a), 1)
 #define M5(a, b) (foo((a), (b)), 1)
+#define M6() fn1()
+#define M7() fn2()
 void t11(int i, int j) {
   M1(i, j);  // no warning
   NOP((long)foo(i, j)); // expected-warning {{expression result unused}}
@@ -143,6 +145,8 @@ void t11(int i, int j) {
   NOP((foo(i, j) ? 0 : t3(i), 1)); // expected-warning {{expression result unused}}
   M5(i, j); // no warning
   NOP((foo(i, j), 1)); // expected-warning {{expression result unused}}
+  M6(); // expected-warning {{ignoring return value}}
+  M7(); // no warning
 }
 #undef NOP
 #undef M1
@@ -150,3 +154,5 @@ void t11(int i, int j) {
 #undef M3
 #undef M4
 #undef M5
+#undef M6
+#undef M7





More information about the cfe-commits mailing list