[PATCH] D39075: Fix nodiscard for volatile references

Erich Keane via Phabricator via cfe-commits cfe-commits at lists.llvm.org
Wed Oct 18 15:51:55 PDT 2017


erichkeane updated this revision to Diff 119535.
erichkeane added a comment.

Added function-pointer test.


https://reviews.llvm.org/D39075

Files:
  lib/AST/Expr.cpp
  test/CXX/dcl.dcl/dcl.attr/dcl.attr.nodiscard/p2.cpp


Index: lib/AST/Expr.cpp
===================================================================
--- lib/AST/Expr.cpp
+++ lib/AST/Expr.cpp
@@ -2298,7 +2298,8 @@
         const DeclRefExpr *DRE =
             dyn_cast<DeclRefExpr>(CE->getSubExpr()->IgnoreParens());
         if (!(DRE && isa<VarDecl>(DRE->getDecl()) &&
-              cast<VarDecl>(DRE->getDecl())->hasLocalStorage())) {
+              cast<VarDecl>(DRE->getDecl())->hasLocalStorage()) &&
+            !isa<CallExpr>(CE->getSubExpr()->IgnoreParens())) {
           return CE->getSubExpr()->isUnusedResultAWarning(WarnE, Loc,
                                                           R1, R2, Ctx);
         }
Index: test/CXX/dcl.dcl/dcl.attr/dcl.attr.nodiscard/p2.cpp
===================================================================
--- test/CXX/dcl.dcl/dcl.attr/dcl.attr.nodiscard/p2.cpp
+++ test/CXX/dcl.dcl/dcl.attr/dcl.attr.nodiscard/p2.cpp
@@ -9,21 +9,33 @@
 E get_e();
 
 [[nodiscard]] int get_i();
+[[nodiscard]] volatile int &get_vi();
 
 void f() {
   get_s(); // expected-warning {{ignoring return value of function declared with 'nodiscard' attribute}}
   get_i(); // expected-warning {{ignoring return value of function declared with 'nodiscard' attribute}}
+  get_vi(); // expected-warning {{ignoring return value of function declared with 'nodiscard' attribute}}
   get_e(); // expected-warning {{ignoring return value of function declared with 'nodiscard' attribute}}
 
   // Okay, warnings are not encouraged
   get_s_ref();
   (void)get_s();
   (void)get_i();
+  (void)get_vi();
   (void)get_e();
 }
 
+[[nodiscard]] volatile char &(*fp)();
+void g() {
+  fp(); // expected-warning {{ignoring return value of function declared with 'nodiscard' attribute}}
+
+  // OK, warning suppressed.
+  (void)fp();
+}
 #ifdef EXT
 // expected-warning at 4 {{use of the 'nodiscard' attribute is a C++17 extension}}
 // expected-warning at 8 {{use of the 'nodiscard' attribute is a C++17 extension}}
 // expected-warning at 11 {{use of the 'nodiscard' attribute is a C++17 extension}}
+// expected-warning at 12 {{use of the 'nodiscard' attribute is a C++17 extension}}
+// expected-warning at 28 {{use of the 'nodiscard' attribute is a C++17 extension}}
 #endif


-------------- next part --------------
A non-text attachment was scrubbed...
Name: D39075.119535.patch
Type: text/x-patch
Size: 2215 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/cfe-commits/attachments/20171018/a8632c22/attachment.bin>


More information about the cfe-commits mailing list