1 //===----------------------------------------------------------------------===// 2 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. 3 // See https://llvm.org/LICENSE.txt for license information. 4 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception 5 // 6 //===----------------------------------------------------------------------===// 7 8 // UNSUPPORTED: c++03, c++11, c++14, c++17 9 // UNSUPPORTED: libcpp-has-no-incomplete-format 10 11 // Basic test to validate ill-formed code is properly detected. 12 13 // <format> 14 15 // template<class... Args> 16 // string format(format-string<Args...> fmt, const Args&... args); 17 // template<class... Args> 18 // wstring format(wformat-string<Args...> fmt, const Args&... args); 19 20 #include <format> 21 22 #include "test_macros.h" 23 24 // clang-format off 25 26 void f() { 27 std::format("{"); // expected-error-re{{call to consteval function '{{.*}}' is not a constant expression}} 28 // expected-note@*:* {{non-constexpr function '__throw_format_error' cannot be used in a constant expression}} 29 30 std::format("}"); // expected-error-re{{call to consteval function '{{.*}}' is not a constant expression}} 31 // expected-note@*:* {{non-constexpr function '__throw_format_error' cannot be used in a constant expression}} 32 33 std::format("{}"); // expected-error-re{{call to consteval function '{{.*}}' is not a constant expression}} 34 // expected-note@*:* {{non-constexpr function '__throw_format_error' cannot be used in a constant expression}} 35 36 std::format("{0}"); // expected-error-re{{call to consteval function '{{.*}}' is not a constant expression}} 37 // expected-note@*:* {{non-constexpr function '__throw_format_error' cannot be used in a constant expression}} 38 39 std::format("{:-}", "Forty-two"); // expected-error-re{{call to consteval function '{{.*}}' is not a constant expression}} 40 // expected-note@*:* {{non-constexpr function '__throw_format_error' cannot be used in a constant expression}} 41 42 std::format("{:#}", "Forty-two"); // expected-error-re{{call to consteval function '{{.*}}' is not a constant expression}} 43 // expected-note@*:* {{non-constexpr function '__throw_format_error' cannot be used in a constant expression}} 44 45 std::format("{:L}", "Forty-two"); // expected-error-re{{call to consteval function '{{.*}}' is not a constant expression}} 46 // expected-note@*:* {{non-constexpr function '__throw_format_error' cannot be used in a constant expression}} 47 48 std::format("{0:{0}}", "Forty-two"); // expected-error-re{{call to consteval function '{{.*}}' is not a constant expression}} 49 // expected-note@*:* {{non-constexpr function '__throw_format_error' cannot be used in a constant expression}} 50 51 std::format("{:.42d}", "Forty-two"); // expected-error-re{{call to consteval function '{{.*}}' is not a constant expression}} 52 // expected-note@*:* {{non-constexpr function '__throw_format_error' cannot be used in a constant expression}} 53 54 std::format("{:d}", "Forty-two"); // expected-error-re{{call to consteval function '{{.*}}' is not a constant expression}} 55 // expected-note@*:* {{non-constexpr function '__throw_format_error' cannot be used in a constant expression}} 56 57 #ifndef TEST_HAS_NO_WIDE_CHARACTERS 58 std::format(L"{"); // expected-error-re{{call to consteval function '{{.*}}' is not a constant expression}} 59 // expected-note@*:* {{non-constexpr function '__throw_format_error' cannot be used in a constant expression}} 60 61 std::format(L"}"); // expected-error-re{{call to consteval function '{{.*}}' is not a constant expression}} 62 // expected-note@*:* {{non-constexpr function '__throw_format_error' cannot be used in a constant expression}} 63 64 std::format(L"{}"); // expected-error-re{{call to consteval function '{{.*}}' is not a constant expression}} 65 // expected-note@*:* {{non-constexpr function '__throw_format_error' cannot be used in a constant expression}} 66 67 std::format(L"{0}"); // expected-error-re{{call to consteval function '{{.*}}' is not a constant expression}} 68 // expected-note@*:* {{non-constexpr function '__throw_format_error' cannot be used in a constant expression}} 69 70 std::format(L"{:-}", L"Forty-two"); // expected-error-re{{call to consteval function '{{.*}}' is not a constant expression}} 71 // expected-note@*:* {{non-constexpr function '__throw_format_error' cannot be used in a constant expression}} 72 73 std::format(L"{:#}", L"Forty-two"); // expected-error-re{{call to consteval function '{{.*}}' is not a constant expression}} 74 // expected-note@*:* {{non-constexpr function '__throw_format_error' cannot be used in a constant expression}} 75 76 std::format(L"{:L}", L"Forty-two"); // expected-error-re{{call to consteval function '{{.*}}' is not a constant expression}} 77 // expected-note@*:* {{non-constexpr function '__throw_format_error' cannot be used in a constant expression}} 78 79 std::format(L"{0:{0}}", L"Forty-two"); // expected-error-re{{call to consteval function '{{.*}}' is not a constant expression}} 80 // expected-note@*:* {{non-constexpr function '__throw_format_error' cannot be used in a constant expression}} 81 82 std::format(L"{:.42d}", L"Forty-two"); // expected-error-re{{call to consteval function '{{.*}}' is not a constant expression}} 83 // expected-note@*:* {{non-constexpr function '__throw_format_error' cannot be used in a constant expression}} 84 85 std::format(L"{:d}", L"Forty-two"); // expected-error-re{{call to consteval function '{{.*}}' is not a constant expression}} 86 // expected-note@*:* {{non-constexpr function '__throw_format_error' cannot be used in a constant expression}} 87 #endif 88 } 89 90 struct tiny { 91 int bit : 1; 92 }; 93 94 void P2418() 95 { 96 auto t = tiny{}; 97 std::format("{}", t.bit); // expected-error{{non-const reference cannot bind to bit-field 'bit'}} 98 99 #ifndef TEST_HAS_NO_WIDE_CHARACTERS 100 std::format(L"{}", t.bit); // expected-error{{non-const reference cannot bind to bit-field 'bit'}} 101 #endif 102 } 103