• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 //===----------------------------------------------------------------------===//
8 
9 #ifndef TEST_SUPPORT_CONCAT_MACROS_H
10 #define TEST_SUPPORT_CONCAT_MACROS_H
11 
12 #include <cstdio>
13 #include <string>
14 
15 #include "assert_macros.h"
16 #include "test_macros.h"
17 
18 #ifndef TEST_HAS_NO_LOCALIZATION
19 #  include <sstream>
20 #endif
21 
22 #if TEST_STD_VER > 17
23 
24 #  ifndef TEST_HAS_NO_LOCALIZATION
25 template <class T>
requires(T && value)26 concept test_char_streamable = requires(T&& value) { std::stringstream{} << std::forward<T>(value); };
27 #  endif
28 
29 // If possible concatenates message for the assertion function, else returns a
30 // default message. Not being able to stream is not considered an 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.
33 //
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>
test_concat_message(Args &&...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";
47 }
48 
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()); }
51 
52 #else
53 
54 // Fallback definition before C++20 that allows using the macro but doesn't provide a very good message.
55 #  define TEST_WRITE_CONCATENATED(...) [&] { ::test_eprintf("%s", TEST_STRINGIZE(__VA_ARGS__)); }
56 
57 #endif // TEST_STD_VER > 17
58 
59 #endif //  TEST_SUPPORT_CONCAT_MACROS_H
60