diff options
| author | Erich Keane <erich.keane@intel.com> | 2019-07-25 15:10:56 +0000 |
|---|---|---|
| committer | Erich Keane <erich.keane@intel.com> | 2019-07-25 15:10:56 +0000 |
| commit | 46441fdb3c1da5d2c40263d160bdd585f0c3f7d3 (patch) | |
| tree | a28227894949958ce45cccfaa7849b40d711d6ee /clang/test | |
| parent | 207726c8825aa24d8464f6b349c1723a2b7df902 (diff) | |
| download | bcm5719-llvm-46441fdb3c1da5d2c40263d160bdd585f0c3f7d3.tar.gz bcm5719-llvm-46441fdb3c1da5d2c40263d160bdd585f0c3f7d3.zip | |
Implement P1771
As passed in the Cologne meeting and treated by Core as a DR,
[[nodiscard]] was applied to constructors so that they can be diagnosed
in cases where the user forgets a variable name for a type.
The intent is to enable the library to start using this on the
constructors of scope_guard/lock_guard.
Differential Revision: https://reviews.llvm.org/D64914
llvm-svn: 367027
Diffstat (limited to 'clang/test')
| -rw-r--r-- | clang/test/CXX/dcl.dcl/dcl.attr/dcl.attr.nodiscard/p2.cpp | 50 | ||||
| -rw-r--r-- | clang/test/Preprocessor/has_attribute.cpp | 2 |
2 files changed, 51 insertions, 1 deletions
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 9b876086b3e..bdf829210e0 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 @@ -80,6 +80,50 @@ void cxx2a_use() { conflicting_reason(); // expected-warning {{ignoring return value of function declared with 'nodiscard' attribute: special reason}} } +namespace p1771 { +struct[[nodiscard("Don't throw me away!")]] ConvertTo{}; +struct S { + [[nodiscard]] S(); + [[nodiscard("Don't let that S-Char go!")]] S(char); + S(int); + [[gnu::warn_unused_result]] S(double); + operator ConvertTo(); + [[nodiscard]] operator int(); + [[nodiscard("Don't throw away as a double")]] operator double(); +}; + +struct[[nodiscard("Don't throw me away either!")]] Y{}; + +void usage() { + S(); // expected-warning {{ignoring temporary created by a constructor declared with 'nodiscard' attribute}} + S('A'); // expected-warning {{ignoring temporary created by a constructor declared with 'nodiscard' attribute: Don't let that S-Char go!}} + S(1); + S(2.2); + Y(); // expected-warning {{ignoring temporary created by a constructor declared with 'nodiscard' attribute: Don't throw me away either!}} + S s; + ConvertTo{}; // expected-warning {{ignoring return value of function declared with 'nodiscard' attribute: Don't throw me away!}} + +// AST is different in C++20 mode, pre-2017 a move ctor for ConvertTo is there +// as well, hense the constructor warning. +#if __cplusplus >= 201703L +// expected-warning@+4 {{ignoring return value of function declared with 'nodiscard' attribute: Don't throw me away!}} +#else +// expected-warning@+2 {{ignoring temporary created by a constructor declared with 'nodiscard' attribute: Don't throw me away!}} +#endif + (ConvertTo) s; + (int)s; // expected-warning {{ignoring return value of function declared with 'nodiscard' attribute}} + (S)'c'; // expected-warning {{ignoring temporary created by a constructor declared with 'nodiscard' attribute: Don't let that S-Char go!}} +#if __cplusplus >= 201703L +// expected-warning@+4 {{ignoring return value of function declared with 'nodiscard' attribute: Don't throw me away!}} +#else +// expected-warning@+2 {{ignoring temporary created by a constructor declared with 'nodiscard' attribute: Don't throw me away!}} +#endif + static_cast<ConvertTo>(s); + static_cast<int>(s); // expected-warning {{ignoring return value of function declared with 'nodiscard' attribute}} + static_cast<double>(s); // expected-warning {{ignoring return value of function declared with 'nodiscard' attribute: Don't throw away as a double}} +} +}; // namespace p1771 + #ifdef EXT // expected-warning@5 {{use of the 'nodiscard' attribute is a C++17 extension}} // expected-warning@9 {{use of the 'nodiscard' attribute is a C++17 extension}} @@ -91,4 +135,10 @@ void cxx2a_use() { // expected-warning@71 {{use of the 'nodiscard' attribute is a C++2a extension}} // expected-warning@73 {{use of the 'nodiscard' attribute is a C++2a extension}} // expected-warning@74 {{use of the 'nodiscard' attribute is a C++2a extension}} +// expected-warning@84 {{use of the 'nodiscard' attribute is a C++2a extension}} +// expected-warning@86 {{use of the 'nodiscard' attribute is a C++17 extension}} +// expected-warning@87 {{use of the 'nodiscard' attribute is a C++2a extension}} +// expected-warning@91 {{use of the 'nodiscard' attribute is a C++17 extension}} +// expected-warning@92 {{use of the 'nodiscard' attribute is a C++2a extension}} +// expected-warning@95 {{use of the 'nodiscard' attribute is a C++2a extension}} #endif diff --git a/clang/test/Preprocessor/has_attribute.cpp b/clang/test/Preprocessor/has_attribute.cpp index d35b673e68b..83ee0e3c6cf 100644 --- a/clang/test/Preprocessor/has_attribute.cpp +++ b/clang/test/Preprocessor/has_attribute.cpp @@ -63,7 +63,7 @@ CXX11(unlikely) // CHECK: maybe_unused: 201603L // ITANIUM: no_unique_address: 201803L // WINDOWS: no_unique_address: 0 -// CHECK: nodiscard: 201603L +// CHECK: nodiscard: 201907L // CHECK: noreturn: 200809L // FIXME(201803L) CHECK: unlikely: 0 |

