[clang] 2ec5520 - Disallow [[nodiscard]] on a function pointer declaration.

Aaron Ballman via cfe-commits cfe-commits at lists.llvm.org
Thu Apr 16 06:28:58 PDT 2020


Author: Aaron Ballman
Date: 2020-04-16T09:28:49-04:00
New Revision: 2ec5520a54ef9b359c6154adf857ba690bc117f1

URL: https://github.com/llvm/llvm-project/commit/2ec5520a54ef9b359c6154adf857ba690bc117f1
DIFF: https://github.com/llvm/llvm-project/commit/2ec5520a54ef9b359c6154adf857ba690bc117f1.diff

LOG: Disallow [[nodiscard]] on a function pointer declaration.

This is not allowed by [dcl.attr.nodiscard]p1 for the standard attribute, but
is still supported for the [[clang::warn_unused_result]] spelling.

Added: 
    

Modified: 
    clang/lib/Sema/SemaDeclAttr.cpp
    clang/test/CXX/dcl.dcl/dcl.attr/dcl.attr.nodiscard/p2.cpp
    clang/test/SemaCXX/warn-unused-result.cpp

Removed: 
    


################################################################################
diff  --git a/clang/lib/Sema/SemaDeclAttr.cpp b/clang/lib/Sema/SemaDeclAttr.cpp
index 1a8a73660cf4..3205b4472db2 100644
--- a/clang/lib/Sema/SemaDeclAttr.cpp
+++ b/clang/lib/Sema/SemaDeclAttr.cpp
@@ -2826,6 +2826,12 @@ static void handleWarnUnusedResult(Sema &S, Decl *D, const ParsedAttr &AL) {
 
   StringRef Str;
   if ((AL.isCXX11Attribute() || AL.isC2xAttribute()) && !AL.getScopeName()) {
+    // The standard attribute cannot be applied to variable declarations such
+    // as a function pointer.
+    if (isa<VarDecl>(D))
+      S.Diag(AL.getLoc(), diag::warn_attribute_wrong_decl_type_str)
+          << AL << "functions, classes, or enumerations";
+
     // If this is spelled as the standard C++17 attribute, but not in C++17,
     // warn about using it as an extension. If there are attribute arguments,
     // then claim it's a C++2a extension instead.

diff  --git a/clang/test/CXX/dcl.dcl/dcl.attr/dcl.attr.nodiscard/p2.cpp b/clang/test/CXX/dcl.dcl/dcl.attr/dcl.attr.nodiscard/p2.cpp
index 3d3223cda756..e2397c12e2e9 100644
--- a/clang/test/CXX/dcl.dcl/dcl.attr/dcl.attr.nodiscard/p2.cpp
+++ b/clang/test/CXX/dcl.dcl/dcl.attr/dcl.attr.nodiscard/p2.cpp
@@ -26,7 +26,7 @@ void f() {
   (void)get_e();
 }
 
-[[nodiscard]] volatile char &(*fp)();
+[[nodiscard]] volatile char &(*fp)(); // expected-warning {{'nodiscard' attribute only applies to functions, classes, or enumerations}}
 void g() {
   fp(); // expected-warning {{ignoring return value of function declared with 'nodiscard' attribute}}
 

diff  --git a/clang/test/SemaCXX/warn-unused-result.cpp b/clang/test/SemaCXX/warn-unused-result.cpp
index f1de4618a741..d0bb4c9317dd 100644
--- a/clang/test/SemaCXX/warn-unused-result.cpp
+++ b/clang/test/SemaCXX/warn-unused-result.cpp
@@ -246,3 +246,11 @@ void g() {
     f(b); // expected-warning {{ignoring return value}}
 }
 } // namespace PR39837
+
+namespace PR45520 {
+[[nodiscard]] bool (*f)(); // expected-warning {{'nodiscard' attribute only applies to functions, classes, or enumerations}}
+[[clang::warn_unused_result]] bool (*g)();
+__attribute__((warn_unused_result)) bool (*h)();
+
+void i([[nodiscard]] bool (*fp)()); // expected-warning {{'nodiscard' attribute only applies to functions, classes, or enumerations}}
+}


        


More information about the cfe-commits mailing list