• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 // -*- C++ -*-
2 //===----------------------------------------------------------------------===//
3 //
4 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
5 // See https://llvm.org/LICENSE.txt for license information.
6 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
7 //
8 //===----------------------------------------------------------------------===//
9 
10 // Test that _LIBCPP_NODISCARD_EXT and _LIBCPP_NODISCARD_AFTER_CXX17 are defined
11 // to the appropriate warning-generating attribute when _LIBCPP_ENABLE_NODISCARD
12 // is explicitly provided.
13 
14 // UNSUPPORTED: c++03
15 
16 // GCC 7 is the first version to introduce [[nodiscard]]
17 // UNSUPPORTED: gcc-5, gcc-6
18 
19 // ADDITIONAL_COMPILE_FLAGS: -D_LIBCPP_ENABLE_NODISCARD
20 
21 #include <__config>
22 
foo()23 _LIBCPP_NODISCARD_EXT int foo() { return 42; }
bar()24 _LIBCPP_NODISCARD_AFTER_CXX17 int bar() { return 42; }
25 
main(int,char **)26 int main(int, char**) {
27   foo(); // expected-warning-re {{ignoring return value of function declared with {{'nodiscard'|warn_unused_result}} attribute}}
28   bar(); // expected-warning-re {{ignoring return value of function declared with {{'nodiscard'|warn_unused_result}} attribute}}
29   (void)foo(); // OK. void casts disable the diagnostic.
30   (void)bar();
31 
32   return 0;
33 }
34