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// Make sure functions specified as being 'addressable' (their address can be
10// taken in a well-defined manner) are indeed addressable. This notion was
11// added by http://wg21.link/p0551. While it was technically only introduced
12// in C++20, we test it in all standard modes because it's basic QOI to provide
13// a consistent behavior for that across standard modes.
14
15// RUN: %{cxx} %{flags} %{compile_flags} -c %s -o %t.tu1.o -DTU1
16// RUN: %{cxx} %{flags} %{compile_flags} -c %s -o %t.tu2.o -DTU2
17// RUN: %{cxx} %t.tu1.o %t.tu2.o %{flags} %{link_flags} -o %t.exe
18// RUN: %{exec} %t.exe
19
20// The functions checked below come from <iostream> & friends
21// UNSUPPORTED: no-localization
22
23#include <cassert>
24#include <ios>
25#include <istream>
26#include <map>
27#include <ostream>
28#include <string>
29#include <utility>
30
31#include "test_macros.h"
32
33typedef std::ios_base& (FormatFlagFunction)(std::ios_base&);
34typedef std::basic_ostream<char>& (OstreamManipFunction)(std::basic_ostream<char>&);
35typedef std::basic_istream<char>& (IstreamManipFunction)(std::basic_istream<char>&);
36#ifndef TEST_HAS_NO_WIDE_CHARACTERS
37typedef std::basic_ostream<wchar_t>& (WOstreamManipFunction)(std::basic_ostream<wchar_t>&);
38typedef std::basic_istream<wchar_t>& (WIstreamManipFunction)(std::basic_istream<wchar_t>&);
39#endif
40
41extern FormatFlagFunction* get_formatflag_tu1(std::string);
42extern FormatFlagFunction* get_formatflag_tu2(std::string);
43
44extern OstreamManipFunction* get_ostreammanip_tu1(std::string);
45extern OstreamManipFunction* get_ostreammanip_tu2(std::string);
46#ifndef TEST_HAS_NO_WIDE_CHARACTERS
47extern WOstreamManipFunction* get_wostreammanip_tu1(std::string);
48extern WOstreamManipFunction* get_wostreammanip_tu2(std::string);
49#endif
50
51extern IstreamManipFunction* get_istreammanip_tu1(std::string);
52extern IstreamManipFunction* get_istreammanip_tu2(std::string);
53#ifndef TEST_HAS_NO_WIDE_CHARACTERS
54extern WIstreamManipFunction* get_wistreammanip_tu1(std::string);
55extern WIstreamManipFunction* get_wistreammanip_tu2(std::string);
56#endif
57
58#ifdef TU1
59FormatFlagFunction* get_formatflag_tu1(std::string func)
60#else
61FormatFlagFunction* get_formatflag_tu2(std::string func)
62#endif
63{
64 std::map<std::string, FormatFlagFunction*> all_funcs;
65
66 // [fmtflags.manip]
67 all_funcs.insert(std::make_pair("boolalpha", &std::boolalpha));
68 all_funcs.insert(std::make_pair("noboolalpha", &std::noboolalpha));
69 all_funcs.insert(std::make_pair("showbase", &std::showbase));
70 all_funcs.insert(std::make_pair("noshowbase", &std::noshowbase));
71 all_funcs.insert(std::make_pair("showpoint", &std::showpoint));
72 all_funcs.insert(std::make_pair("noshowpoint", &std::noshowpoint));
73 all_funcs.insert(std::make_pair("showpos", &std::showpos));
74 all_funcs.insert(std::make_pair("noshowpos", &std::noshowpos));
75 all_funcs.insert(std::make_pair("skipws", &std::skipws));
76 all_funcs.insert(std::make_pair("noskipws", &std::noskipws));
77 all_funcs.insert(std::make_pair("uppercase", &std::uppercase));
78 all_funcs.insert(std::make_pair("nouppercase", &std::nouppercase));
79 all_funcs.insert(std::make_pair("unitbuf", &std::unitbuf));
80 all_funcs.insert(std::make_pair("nounitbuf", &std::nounitbuf));
81
82 // [adjustfield.manip]
83 all_funcs.insert(std::make_pair("internal", &std::internal));
84 all_funcs.insert(std::make_pair("left", &std::left));
85 all_funcs.insert(std::make_pair("right", &std::right));
86
87 // [basefield.manip]
88 all_funcs.insert(std::make_pair("dec", &std::dec));
89 all_funcs.insert(std::make_pair("hex", &std::hex));
90 all_funcs.insert(std::make_pair("oct", &std::oct));
91
92 // [floatfield.manip]
93 all_funcs.insert(std::make_pair("fixed", &std::fixed));
94 all_funcs.insert(std::make_pair("scientific", &std::scientific));
95 all_funcs.insert(std::make_pair("hexfloat", &std::hexfloat));
96 all_funcs.insert(std::make_pair("defaultfloat", &std::defaultfloat));
97
98 return all_funcs.at(func);
99}
100
101// [ostream.manip] (char)
102#ifdef TU1
103OstreamManipFunction* get_ostreammanip_tu1(std::string func)
104#else
105OstreamManipFunction* get_ostreammanip_tu2(std::string func)
106#endif
107{
108 std::map<std::string, OstreamManipFunction*> all_funcs;
109 typedef std::char_traits<char> Traits;
110 all_funcs.insert(x: std::make_pair(x: "endl", y: &std::endl<char, Traits>));
111 all_funcs.insert(x: std::make_pair(x: "ends", y: &std::ends<char, Traits>));
112 all_funcs.insert(x: std::make_pair(x: "flush", y: &std::flush<char, Traits>));
113 return all_funcs.at(k: func);
114}
115
116// [ostream.manip] (wchar_t)
117#ifndef TEST_HAS_NO_WIDE_CHARACTERS
118# ifdef TU1
119WOstreamManipFunction* get_wostreammanip_tu1(std::string func)
120# else
121WOstreamManipFunction* get_wostreammanip_tu2(std::string func)
122# endif
123{
124 std::map<std::string, WOstreamManipFunction*> all_funcs;
125 typedef std::char_traits<wchar_t> Traits;
126 all_funcs.insert(x: std::make_pair(x: "endl", y: &std::endl<wchar_t, Traits>));
127 all_funcs.insert(x: std::make_pair(x: "ends", y: &std::ends<wchar_t, Traits>));
128 all_funcs.insert(x: std::make_pair(x: "flush", y: &std::flush<wchar_t, Traits>));
129 return all_funcs.at(k: func);
130}
131#endif // TEST_HAS_NO_WIDE_CHARACTERS
132
133// [istream.manip] (char)
134#ifdef TU1
135IstreamManipFunction* get_istreammanip_tu1(std::string func)
136#else
137IstreamManipFunction* get_istreammanip_tu2(std::string func)
138#endif
139{
140 std::map<std::string, IstreamManipFunction*> all_funcs;
141 typedef std::char_traits<char> Traits;
142 all_funcs.insert(x: std::make_pair(x: "ws", y: &std::ws<char, Traits>));
143 return all_funcs.at(k: func);
144}
145
146// [istream.manip] (wchar_t)
147#ifndef TEST_HAS_NO_WIDE_CHARACTERS
148# ifdef TU1
149WIstreamManipFunction* get_wistreammanip_tu1(std::string func)
150# else
151WIstreamManipFunction* get_wistreammanip_tu2(std::string func)
152# endif
153{
154 std::map<std::string, WIstreamManipFunction*> all_funcs;
155 typedef std::char_traits<wchar_t> Traits;
156 all_funcs.insert(x: std::make_pair(x: "ws", y: &std::ws<wchar_t, Traits>));
157 return all_funcs.at(k: func);
158}
159#endif // TEST_HAS_NO_WIDE_CHARACTERS
160
161#ifdef TU2
162 int main(int, char**) {
163 assert(get_formatflag_tu1("boolalpha") == get_formatflag_tu2("boolalpha"));
164 assert(get_formatflag_tu1("noboolalpha") == get_formatflag_tu2("noboolalpha"));
165 assert(get_formatflag_tu1("showbase") == get_formatflag_tu2("showbase"));
166 assert(get_formatflag_tu1("noshowbase") == get_formatflag_tu2("noshowbase"));
167 assert(get_formatflag_tu1("showpoint") == get_formatflag_tu2("showpoint"));
168 assert(get_formatflag_tu1("noshowpoint") == get_formatflag_tu2("noshowpoint"));
169 assert(get_formatflag_tu1("showpos") == get_formatflag_tu2("showpos"));
170 assert(get_formatflag_tu1("noshowpos") == get_formatflag_tu2("noshowpos"));
171 assert(get_formatflag_tu1("skipws") == get_formatflag_tu2("skipws"));
172 assert(get_formatflag_tu1("noskipws") == get_formatflag_tu2("noskipws"));
173 assert(get_formatflag_tu1("uppercase") == get_formatflag_tu2("uppercase"));
174 assert(get_formatflag_tu1("nouppercase") == get_formatflag_tu2("nouppercase"));
175 assert(get_formatflag_tu1("unitbuf") == get_formatflag_tu2("unitbuf"));
176 assert(get_formatflag_tu1("nounitbuf") == get_formatflag_tu2("nounitbuf"));
177 assert(get_formatflag_tu1("internal") == get_formatflag_tu2("internal"));
178 assert(get_formatflag_tu1("left") == get_formatflag_tu2("left"));
179 assert(get_formatflag_tu1("right") == get_formatflag_tu2("right"));
180 assert(get_formatflag_tu1("dec") == get_formatflag_tu2("dec"));
181 assert(get_formatflag_tu1("hex") == get_formatflag_tu2("hex"));
182 assert(get_formatflag_tu1("oct") == get_formatflag_tu2("oct"));
183 assert(get_formatflag_tu1("fixed") == get_formatflag_tu2("fixed"));
184 assert(get_formatflag_tu1("scientific") == get_formatflag_tu2("scientific"));
185 assert(get_formatflag_tu1("hexfloat") == get_formatflag_tu2("hexfloat"));
186 assert(get_formatflag_tu1("defaultfloat") == get_formatflag_tu2("defaultfloat"));
187
188 assert(get_ostreammanip_tu1("endl") == get_ostreammanip_tu2("endl"));
189 assert(get_ostreammanip_tu1("ends") == get_ostreammanip_tu2("ends"));
190 assert(get_ostreammanip_tu1("flush") == get_ostreammanip_tu2("flush"));
191
192#ifndef TEST_HAS_NO_WIDE_CHARACTERS
193 assert(get_wostreammanip_tu1("endl") == get_wostreammanip_tu2("endl"));
194 assert(get_wostreammanip_tu1("ends") == get_wostreammanip_tu2("ends"));
195 assert(get_wostreammanip_tu1("flush") == get_wostreammanip_tu2("flush"));
196#endif
197
198 assert(get_istreammanip_tu1("ws") == get_istreammanip_tu2("ws"));
199
200#ifndef TEST_HAS_NO_WIDE_CHARACTERS
201 assert(get_wistreammanip_tu1("ws") == get_wistreammanip_tu2("ws"));
202#endif
203
204 return 0;
205 }
206#endif
207

source code of libcxx/test/std/namespace/addressable_functions.sh.cpp