JF Bastien 2df59c5068 Support tests in freestanding
Summary:
Freestanding is *weird*. The standard allows it to differ in a bunch of odd
manners from regular C++, and the committee would like to improve that
situation. I'd like to make libc++ behave better with what freestanding should
be, so that it can be a tool we use in improving the standard. To do that we
need to try stuff out, both with "freestanding the language mode" and
"freestanding the library subset".

Let's start with the super basic: run the libc++ tests in freestanding, using
clang as the compiler, and see what works. The easiest hack to do this:

In utils/libcxx/test/config.py add:

  self.cxx.compile_flags += ['-ffreestanding']

Run the tests and they all fail.

Why? Because in freestanding `main` isn't special. This "not special" property
has two effects: main doesn't get mangled, and main isn't allowed to omit its
`return` statement. The first means main gets mangled and the linker can't
create a valid executable for us to test. The second means we spew out warnings
(ew) and the compiler doesn't insert the `return` we omitted, and main just
falls of the end and does whatever undefined behavior (if you're luck, ud2
leading to non-zero return code).

Let's start my work with the basics. This patch changes all libc++ tests to
declare `main` as `int main(int, char**` so it mangles consistently (enabling us
to declare another `extern "C"` main for freestanding which calls the mangled
one), and adds `return 0;` to all places where it was missing. This touches 6124
files, and I apologize.

The former was done with The Magic Of Sed.

The later was done with a (not quite correct but decent) clang tool:

  https://gist.github.com/jfbastien/793819ff360baa845483dde81170feed

This works for most tests, though I did have to adjust a few places when e.g.
the test runs with `-x c`, macros are used for main (such as for the filesystem
tests), etc.

Once this is in we can create a freestanding bot which will prevent further
regressions. After that, we can start the real work of supporting C++
freestanding fairly well in libc++.

<rdar://problem/47754795>

Reviewers: ldionne, mclow.lists, EricWF

Subscribers: christof, jkorous, dexonsmith, arphaman, miyuki, libcxx-commits

Differential Revision: https://reviews.llvm.org/D57624

llvm-svn: 353086
2019-02-04 20:31:13 +00:00

149 lines
5.1 KiB
C++

//===----------------------------------------------------------------------===//
//
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
// See https://llvm.org/LICENSE.txt for license information.
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
//
//===----------------------------------------------------------------------===//
// UNSUPPORTED: c++98, c++03, c++11, c++14
#include <unordered_set>
#include <unordered_map>
#include <set>
#include <map>
#include "min_allocator.h"
using namespace std;
// [container.node.overview] Table 83.
template <class K, class T, class C1, class C2, class H1, class H2, class E1, class E2, class A_set, class A_map>
struct node_compatibility_table
{
static constexpr bool value =
is_same_v<typename map<K, T, C1, A_map>::node_type, typename map<K, T, C2, A_map>::node_type> &&
is_same_v<typename map<K, T, C1, A_map>::node_type, typename multimap<K, T, C2, A_map>::node_type> &&
is_same_v<typename set<K, C1, A_set>::node_type, typename set<K, C2, A_set>::node_type> &&
is_same_v<typename set<K, C1, A_set>::node_type, typename multiset<K, C2, A_set>::node_type> &&
is_same_v<typename unordered_map<K, T, H1, E1, A_map>::node_type, typename unordered_map<K, T, H2, E2, A_map>::node_type> &&
is_same_v<typename unordered_map<K, T, H1, E1, A_map>::node_type, typename unordered_multimap<K, T, H2, E2, A_map>::node_type> &&
is_same_v<typename unordered_set<K, H1, E1, A_set>::node_type, typename unordered_set<K, H2, E2, A_set>::node_type> &&
is_same_v<typename unordered_set<K, H1, E1, A_set>::node_type, typename unordered_multiset<K, H2, E2, A_set>::node_type>;
};
template <class T> struct my_hash
{
using argument_type = T;
using result_type = size_t;
my_hash() = default;
size_t operator()(const T&) const {return 0;}
};
template <class T> struct my_compare
{
my_compare() = default;
bool operator()(const T&, const T&) const {return true;}
};
template <class T> struct my_equal
{
my_equal() = default;
bool operator()(const T&, const T&) const {return true;}
};
struct Static
{
Static() = default;
Static(const Static&) = delete;
Static(Static&&) = delete;
Static& operator=(const Static&) = delete;
Static& operator=(Static&&) = delete;
};
namespace std
{
template <> struct hash<Static>
{
using argument_type = Static;
using result_type = size_t;
hash() = default;
size_t operator()(const Static&) const;
};
}
static_assert(node_compatibility_table<
int, int, std::less<int>, std::less<int>, std::hash<int>,
std::hash<int>, std::equal_to<int>, std::equal_to<int>,
std::allocator<int>,
std::allocator<std::pair<const int, int>>>::value,
"");
static_assert(
node_compatibility_table<int, int, std::less<int>, my_compare<int>,
std::hash<int>, my_hash<int>, std::equal_to<int>,
my_equal<int>, allocator<int>,
allocator<std::pair<const int, int>>>::value,
"");
static_assert(node_compatibility_table<
Static, int, my_compare<Static>, std::less<Static>,
my_hash<Static>, std::hash<Static>, my_equal<Static>,
std::equal_to<Static>, min_allocator<Static>,
min_allocator<std::pair<const Static, int>>>::value,
"");
template <class Container>
void test_node_handle_operations()
{
Container c;
typename Container::node_type nt1, nt2 = c.extract(c.emplace().first);
assert(nt2.get_allocator() == c.get_allocator());
assert(!nt2.empty());
assert(nt1.empty());
std::swap(nt1, nt2);
assert(nt1.get_allocator() == c.get_allocator());
assert(nt2.empty());
}
template <class Container>
void test_node_handle_operations_multi()
{
Container c;
typename Container::node_type nt1, nt2 = c.extract(c.emplace());
assert(nt2.get_allocator() == c.get_allocator());
assert(!nt2.empty());
assert(nt1.empty());
std::swap(nt1, nt2);
assert(nt1.get_allocator() == c.get_allocator());
assert(nt2.empty());
}
template <class> void test_typedef() {}
template <class Container>
void test_insert_return_type()
{
test_typedef<typename Container::insert_return_type>();
}
int main(int, char**)
{
test_node_handle_operations<std::map<int, int>>();
test_node_handle_operations_multi<std::multimap<int, int>>();
test_node_handle_operations<std::set<int>>();
test_node_handle_operations_multi<std::multiset<int>>();
test_node_handle_operations<std::unordered_map<int, int>>();
test_node_handle_operations_multi<std::unordered_multimap<int, int>>();
test_node_handle_operations<std::unordered_set<int>>();
test_node_handle_operations_multi<std::unordered_multiset<int>>();
test_insert_return_type<std::map<int, int>>();
test_insert_return_type<std::set<int>>();
test_insert_return_type<std::unordered_map<int, int>>();
test_insert_return_type<std::unordered_set<int>>();
return 0;
}