nickdesaulniers updated this revision to Diff 255808.
nickdesaulniers added a comment.

- emit warning just for CallExprs


Repository:
  rG LLVM Github Monorepo

CHANGES SINCE LAST ACTION
  https://reviews.llvm.org/D77611/new/

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,15 @@
 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
@@ -247,8 +247,9 @@
   // 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())
+  // false positive. But we do want to check for WarnUnusedResultAttr on
+  // CallExprs below.
+  if (isa<StmtExpr>(E) && Loc.isMacroID() && !isa<CallExpr>(WarnExpr))
     return;
 
   // Check if this is the UNREFERENCED_PARAMETER from the Microsoft headers.


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,15 @@
 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
@@ -247,8 +247,9 @@
   // 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())
+  // false positive. But we do want to check for WarnUnusedResultAttr on
+  // CallExprs below.
+  if (isa<StmtExpr>(E) && Loc.isMacroID() && !isa<CallExpr>(WarnExpr))
     return;
 
   // Check if this is the UNREFERENCED_PARAMETER from the Microsoft headers.
_______________________________________________
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits

Reply via email to