[NFC][Py Reformat] Reformat python files in libcxx/libcxxabi
[llvm-project.git] / libcxx / test / support / concat_macros.h
blobd6396a76dde737ea011e367e2c3b18f33561e0cb
1 //===----------------------------------------------------------------------===//
2 //
3 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4 // See https://llvm.org/LICENSE.txt for license information.
5 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6 //
7 //===----------------------------------------------------------------------===//
9 #ifndef TEST_SUPPORT_CONCAT_MACROS_H
10 #define TEST_SUPPORT_CONCAT_MACROS_H
12 #include <cstdio>
13 #include <string>
15 #include "assert_macros.h"
16 #include "test_macros.h"
18 #ifndef TEST_HAS_NO_LOCALIZATION
19 # include <sstream>
20 #endif
22 #if TEST_STD_VER > 17
24 # ifndef TEST_HAS_NO_LOCALIZATION
25 template <class T>
26 concept test_char_streamable = requires(T&& value) { std::stringstream{} << std::forward<T>(value); };
27 # endif
29 // If possible concatenates message for the assertion function, else returns a
30 // default message. Not being able to stream is not considered and error. For
31 // example, streaming to std::wcerr doesn't work properly in the CI. Therefore
32 // the formatting tests should only stream to std::string.
34 // The macro TEST_WRITE_CONCATENATED can be used to evaluate the arguments
35 // lazily. This useful when using this function in combination with
36 // assert_macros.h.
37 template <class... Args>
38 std::string test_concat_message([[maybe_unused]] Args&&... args) {
39 # ifndef TEST_HAS_NO_LOCALIZATION
40 if constexpr ((test_char_streamable<Args> && ...)) {
41 std::stringstream sstr;
42 ((sstr << std::forward<Args>(args)), ...);
43 return sstr.str();
44 } else
45 # endif
46 return "Message discarded since it can't be streamed to std::cerr.\n";
49 // Writes its arguments to stderr, using the test_concat_message helper.
50 # define TEST_WRITE_CONCATENATED(...) [&] { ::test_eprintf("%s", ::test_concat_message(__VA_ARGS__).c_str()); }
52 #endif // TEST_STD_VER > 17
54 #endif // TEST_SUPPORT_CONCAT_MACROS_H