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 // This test checks that we can explicitly instantiate std::string with a custom
10 // character type and traits and then use `shrink_to_fit`. In particular, this is
11 // a regression test for the bug that was reported at https://stackoverflow.com/q/69520633/627587
12 // and https://seedcentral.apple.com/sm/feedback_collector/radar/85053279.
13
14 // RUN: %{cxx} %{flags} %{compile_flags} %s %{link_flags} -DTU1 -c -o %t.tu1.o
15 // RUN: %{cxx} %{flags} %{compile_flags} %s %{link_flags} -DTU2 -c -o %t.tu2.o
16 // RUN: %{cxx} %{flags} %t.tu1.o %t.tu2.o %{link_flags} -o %t.exe
17
18 // UNSUPPORTED: no-localization
19
20 #include <cstdint>
21 #include <ios>
22 #include <string>
23
24 typedef std::uint16_t char16;
25
26 struct string16_char_traits {
27 typedef char16 char_type;
28 typedef int int_type;
29
30 typedef std::streamoff off_type;
31 typedef std::mbstate_t state_type;
32 typedef std::fpos<state_type> pos_type;
33
assignstring16_char_traits34 static void assign(char_type&, const char_type&) {}
eqstring16_char_traits35 static bool eq(const char_type&, const char_type&) { return false; }
ltstring16_char_traits36 static bool lt(const char_type&, const char_type&) { return false; }
comparestring16_char_traits37 static int compare(const char_type*, const char_type*, std::size_t) { return 0; }
lengthstring16_char_traits38 static std::size_t length(const char_type*) { return 0; }
findstring16_char_traits39 static const char_type* find(const char_type*, std::size_t, const char_type&) { return nullptr; }
movestring16_char_traits40 static char_type* move(char_type*, const char_type*, std::size_t) { return nullptr; }
copystring16_char_traits41 static char_type* copy(char_type*, const char_type*, std::size_t) { return nullptr; }
assignstring16_char_traits42 static char_type* assign(char_type*, std::size_t, char_type) { return nullptr; }
not_eofstring16_char_traits43 static int_type not_eof(const int_type&) { return 0; }
to_char_typestring16_char_traits44 static char_type to_char_type(const int_type&) { return char_type(); }
to_int_typestring16_char_traits45 static int_type to_int_type(const char_type&) { return int_type(); }
eq_int_typestring16_char_traits46 static bool eq_int_type(const int_type&, const int_type&) { return false; }
eofstring16_char_traits47 static int_type eof() { return int_type(); }
48 };
49
50 #if defined(TU1)
51 template class std::basic_string<char16, string16_char_traits>;
52 #else
53 extern template class std::basic_string<char16, string16_char_traits>;
54
main(int,char **)55 int main(int, char**) {
56 std::basic_string<char16, string16_char_traits> s;
57 s.shrink_to_fit();
58 }
59 #endif
60