[PATCH] D77611: [Sema] fix -Wunused-result in StmtExpr

Nick Desaulniers via Phabricator via cfe-commits cfe-commits at lists.llvm.org
Mon Apr 6 17:29:29 PDT 2020


nickdesaulniers created this revision.
nickdesaulniers added a reviewer: dblaikie.
Herald added a project: clang.
Herald added a subscriber: cfe-commits.
nickdesaulniers planned changes to this revision.
nickdesaulniers added a comment.

ah, using this to build a Linux kernel triggers tons of warnings. So the behavior of GCC is even more subtle than captured in the test cases here.


This is a partial revert of r163034; turns out it doesn't regress the
test case added with that commit.

Add a test case that demonstrates the previous issue; when a GNU C
statement expression is used in a macro Expr::isUnusedResultAWarning was
returning true, but the block removed in this commit from
Sema::DiagnoseUnusedExprResult was returning without emitting a Diag.

Fixes pr/45394.
Link: https://github.com/ClangBuiltLinux/linux/issues/968


Repository:
  rG LLVM Github Monorepo

https://reviews.llvm.org/D77611

Files:
  clang/lib/Sema/SemaStmt.cpp
  clang/test/Frontend/macros.c


Index: clang/test/Frontend/macros.c
===================================================================
--- clang/test/Frontend/macros.c
+++ clang/test/Frontend/macros.c
@@ -1,5 +1,4 @@
-// RUN: %clang_cc1 -DA= -DB=1 -verify -fsyntax-only %s
-// expected-no-diagnostics
+// RUN: %clang_cc1 -Wunused-result -DA= -DB=1 -verify -fsyntax-only %s
 
 int a[(B A) == 1 ? 1 : -1];
 
@@ -11,3 +10,16 @@
 void foo(int a, int b, int c) {
   memset(a,b,c);  // No warning!
 }
+
+__attribute__((warn_unused_result))
+static inline int bar(void) {
+    return 42;
+}
+
+#define baz() ({\
+  bar(); \
+})
+
+void quux(void) {
+    baz(); // expected-warning {{ignoring return value of function declared with 'warn_unused_result' attribute}}
+}
Index: clang/lib/Sema/SemaStmt.cpp
===================================================================
--- clang/lib/Sema/SemaStmt.cpp
+++ clang/lib/Sema/SemaStmt.cpp
@@ -244,13 +244,6 @@
   if (!E->isUnusedResultAWarning(WarnExpr, Loc, R1, R2, Context))
     return;
 
-  // If this is a GNU statement expression expanded from a macro, it is probably
-  // unused because it is a function-like macro that can be used as either an
-  // expression or statement.  Don't warn, because it is almost certainly a
-  // false positive.
-  if (isa<StmtExpr>(E) && Loc.isMacroID())
-    return;
-
   // Check if this is the UNREFERENCED_PARAMETER from the Microsoft headers.
   // That macro is frequently used to suppress "unused parameter" warnings,
   // but its implementation makes clang's -Wunused-value fire.  Prevent this.


-------------- next part --------------
A non-text attachment was scrubbed...
Name: D77611.255545.patch
Type: text/x-patch
Size: 1552 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/cfe-commits/attachments/20200407/4ad77667/attachment.bin>


More information about the cfe-commits mailing list