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

274 lines
7.6 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
// <optional>
// template <class U> optional<T>& operator=(U&& v);
#include <optional>
#include <type_traits>
#include <cassert>
#include <memory>
#include "test_macros.h"
#include "archetypes.hpp"
using std::optional;
struct ThrowAssign {
static int dtor_called;
ThrowAssign() = default;
ThrowAssign(int) { TEST_THROW(42); }
ThrowAssign& operator=(int) {
TEST_THROW(42);
}
~ThrowAssign() { ++dtor_called; }
};
int ThrowAssign::dtor_called = 0;
template <class T, class Arg = T, bool Expect = true>
void assert_assignable() {
static_assert(std::is_assignable<optional<T>&, Arg>::value == Expect, "");
static_assert(!std::is_assignable<const optional<T>&, Arg>::value, "");
}
struct MismatchType {
explicit MismatchType(int) {}
explicit MismatchType(char*) {}
explicit MismatchType(int*) = delete;
MismatchType& operator=(int) { return *this; }
MismatchType& operator=(int*) { return *this; }
MismatchType& operator=(char*) = delete;
};
struct FromOptionalType {
using Opt = std::optional<FromOptionalType>;
FromOptionalType() = default;
FromOptionalType(FromOptionalType const&) = delete;
template <class Dummy = void>
constexpr FromOptionalType(Opt&) { Dummy::BARK; }
template <class Dummy = void>
constexpr FromOptionalType& operator=(Opt&) { Dummy::BARK; return *this; }
};
void test_sfinae() {
using I = TestTypes::TestType;
using E = ExplicitTestTypes::TestType;
assert_assignable<int>();
assert_assignable<int, int&>();
assert_assignable<int, int const&>();
// Implicit test type
assert_assignable<I, I const&>();
assert_assignable<I, I&&>();
assert_assignable<I, int>();
assert_assignable<I, void*, false>();
// Explicit test type
assert_assignable<E, E const&>();
assert_assignable<E, E &&>();
assert_assignable<E, int>();
assert_assignable<E, void*, false>();
// Mismatch type
assert_assignable<MismatchType, int>();
assert_assignable<MismatchType, int*, false>();
assert_assignable<MismatchType, char*, false>();
// Type constructible from optional
assert_assignable<FromOptionalType, std::optional<FromOptionalType>&, false>();
}
void test_with_test_type()
{
using T = TestTypes::TestType;
T::reset();
{ // to empty
optional<T> opt;
opt = 3;
assert(T::alive == 1);
assert(T::constructed == 1);
assert(T::value_constructed == 1);
assert(T::assigned == 0);
assert(T::destroyed == 0);
assert(static_cast<bool>(opt) == true);
assert(*opt == T(3));
}
{ // to existing
optional<T> opt(42);
T::reset_constructors();
opt = 3;
assert(T::alive == 1);
assert(T::constructed == 0);
assert(T::assigned == 1);
assert(T::value_assigned == 1);
assert(T::destroyed == 0);
assert(static_cast<bool>(opt) == true);
assert(*opt == T(3));
}
{ // test default argument
optional<T> opt;
T::reset_constructors();
opt = {1, 2};
assert(T::alive == 1);
assert(T::constructed == 2);
assert(T::value_constructed == 1);
assert(T::move_constructed == 1);
assert(T::assigned == 0);
assert(T::destroyed == 1);
assert(static_cast<bool>(opt) == true);
assert(*opt == T(1, 2));
}
{ // test default argument
optional<T> opt(42);
T::reset_constructors();
opt = {1, 2};
assert(T::alive == 1);
assert(T::constructed == 1);
assert(T::value_constructed == 1);
assert(T::assigned == 1);
assert(T::move_assigned == 1);
assert(T::destroyed == 1);
assert(static_cast<bool>(opt) == true);
assert(*opt == T(1, 2));
}
{ // test default argument
optional<T> opt;
T::reset_constructors();
opt = {1};
assert(T::alive == 1);
assert(T::constructed == 2);
assert(T::value_constructed == 1);
assert(T::move_constructed == 1);
assert(T::assigned == 0);
assert(T::destroyed == 1);
assert(static_cast<bool>(opt) == true);
assert(*opt == T(1));
}
{ // test default argument
optional<T> opt(42);
T::reset_constructors();
opt = {};
assert(static_cast<bool>(opt) == false);
assert(T::alive == 0);
assert(T::constructed == 0);
assert(T::assigned == 0);
assert(T::destroyed == 1);
}
}
template <class T, class Value = int>
void test_with_type() {
{ // to empty
optional<T> opt;
opt = Value(3);
assert(static_cast<bool>(opt) == true);
assert(*opt == T(3));
}
{ // to existing
optional<T> opt(Value(42));
opt = Value(3);
assert(static_cast<bool>(opt) == true);
assert(*opt == T(3));
}
{ // test const
optional<T> opt(Value(42));
const T t(Value(3));
opt = t;
assert(static_cast<bool>(opt) == true);
assert(*opt == T(3));
}
{ // test default argument
optional<T> opt;
opt = {Value(1)};
assert(static_cast<bool>(opt) == true);
assert(*opt == T(1));
}
{ // test default argument
optional<T> opt(Value(42));
opt = {};
assert(static_cast<bool>(opt) == false);
}
}
template <class T>
void test_with_type_multi() {
test_with_type<T>();
{ // test default argument
optional<T> opt;
opt = {1, 2};
assert(static_cast<bool>(opt) == true);
assert(*opt == T(1, 2));
}
{ // test default argument
optional<T> opt(42);
opt = {1, 2};
assert(static_cast<bool>(opt) == true);
assert(*opt == T(1, 2));
}
}
void test_throws()
{
#ifndef TEST_HAS_NO_EXCEPTIONS
using T = ThrowAssign;
{
optional<T> opt;
try {
opt = 42;
assert(false);
} catch (int) {}
assert(static_cast<bool>(opt) == false);
}
assert(T::dtor_called == 0);
{
T::dtor_called = 0;
optional<T> opt(std::in_place);
try {
opt = 42;
assert(false);
} catch (int) {}
assert(static_cast<bool>(opt) == true);
assert(T::dtor_called == 0);
}
assert(T::dtor_called == 1);
#endif
}
enum MyEnum { Zero, One, Two, Three, FortyTwo = 42 };
using Fn = void(*)();
int main(int, char**)
{
test_sfinae();
// Test with instrumented type
test_with_test_type();
// Test with various scalar types
test_with_type<int>();
test_with_type<MyEnum, MyEnum>();
test_with_type<int, MyEnum>();
test_with_type<Fn, Fn>();
// Test types with multi argument constructors
test_with_type_multi<ConstexprTestTypes::TestType>();
test_with_type_multi<TrivialTestTypes::TestType>();
// Test move only types
{
optional<std::unique_ptr<int>> opt;
opt = std::unique_ptr<int>(new int(3));
assert(static_cast<bool>(opt) == true);
assert(**opt == 3);
}
{
optional<std::unique_ptr<int>> opt(std::unique_ptr<int>(new int(2)));
opt = std::unique_ptr<int>(new int(3));
assert(static_cast<bool>(opt) == true);
assert(**opt == 3);
}
test_throws();
return 0;
}