Skip to content

Commit

Permalink
get more working
Browse files Browse the repository at this point in the history
  • Loading branch information
EricWF committed Aug 1, 2024
1 parent aa460bb commit dd42a17
Show file tree
Hide file tree
Showing 4 changed files with 97 additions and 109 deletions.
63 changes: 49 additions & 14 deletions libcxx/test/std/contracts/exceptions-test.pass.cpp
Original file line number Diff line number Diff line change
@@ -1,24 +1,59 @@
// ADDITIONAL_COMPILE_FLAGS: -std=c++26 -Xclang -fcontracts -fcontract-evaluation-semantic=enforce -fcontract-group-evaluation-semantic=observe=observe,enforce=enforce


#include <contracts>
#include "contracts_test.h"
#include "nttp_string.h"
#include "contracts_support.h"
#include "contracts_handler.h"
#include "test_register.h"
#include <exception>
#include <stdexcept>

struct A : CAliveCounter<"A"> {
namespace exception_handling {
#define OBSERVE [[clang::contract_group("observe")]]
void foo() {

};
contract_assert [[clang::contract_group("observe")]] ((true) ? throw 42 : true);


}

int main() {
ContractHandlerInstaller installer;
installer.install([&]() {
installer.uninstall();
REGISTER_TEST(test_ex_handling) {
ContractHandlerInstaller CHI;
int called = 0;
CHI.install([&]() {
++called;
assert(std::current_exception());
});
contract_assert OBSERVE ((true) ? throw 42 : true);
assert(called);
called = 0;

})
{
A a;
{
A a2;
CHI.install([&]() {
++called;
assert(!std::current_exception());
assert(KV<"Local"> == 1);
assert(KV<"Local2"> == 0);

}
throw 42;
});

try {

[] {
CAliveCounter<"Local"> Local;
contract_assert((CAliveCounter<"Local2">{}, false));
assert(KV<"Local2"> == 1);
}();
assert(false);
} catch (int) {
KV<"InCatch">++;
}
}
assert(called == 1);
assert(NC<"Local"> == 0);
assert(KV<"Local2"> == 0);
assert(NC<"InCatch">.consume() == 1);

};

} // namespace exception_handling
51 changes: 2 additions & 49 deletions libcxx/test/std/contracts/member_function_tests.pass.cpp
Original file line number Diff line number Diff line change
@@ -1,10 +1,11 @@
// ADDITIONAL_COMPILE_FLAGS: -std=c++26 -Xclang -fcontracts -fcontract-evaluation-semantic=enforce -fcontract-group-evaluation-semantic=observe=observe,enforce=enforce

#include "nttp_string.h"
#include "contracts_test.h"
#include "contracts_support.h"
#include "test_register.h"
#include "contracts_handler.h"


namespace fn_template_test {
template <class T>
void f(T v) pre(++KV<"Pre">&& v != 1024) post(++KV<"Post">) pre(++KV<std::is_same_v<T, int> ? "Int" : "NotInt">) {
Expand Down Expand Up @@ -198,51 +199,3 @@ REGISTER_TEST(order_test) {
};

} // namespace order_test

namespace exception_handling {

void foo(int x) {

contract_assert [[clang::contract_group("observe")]] ((true) ? throw 42 : true);


}

REGISTER_TEST(test_ex_handling) {
ContractHandlerInstaller CHI;
int called = 0;
CHI.install([&]() {
++called;
assert(std::current_exception());
});
foo(0);
assert(called);
called = 0;

CHI.install([&]() {
++called;
assert(!std::current_exception());
assert(KV<"Local"> == 1);
assert(KV<"Local2"> == 0);

throw 42;
});

try {

[]() {
CAliveCounter<"Local"> Local;
contract_assert((CAliveCounter<"Local2">{}, false));
}();
assert(false);
} catch (int) {
KV<"InCatch">++;
}
assert(called == 1);
assert(KV<"Local"> == 0);
assert(KV<"Local2"> == 0);
assert(KV<"InCatch"> == 1);

};

} // namespace exception_handling
62 changes: 46 additions & 16 deletions libcxx/test/support/contracts_support.h
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
#include <variant>
#include <optional>
#include <iostream>
#include <functional>
#include <string_view>
#include <tuple>
#include <regex>
Expand All @@ -15,10 +16,18 @@
#include <format>
#include <fstream>
#include <cassert>

#include <set>
#include <map>
#include "dump_struct.h"
#include "nttp_string.h"

#pragma clang diagnostic push
#pragma GCC diagnostic ignored "-Wunused-variable"
#pragma GCC diagnostic ignored "-Wunused"
#pragma GCC diagnostic ignored "-Wunused-template"
#pragma GCC diagnostic ignored "-Wunused-parameter"


#define COUNTERS_EQ(list, ...) assert(eq(list, {__VA_ARGS__}))
#define SLOC(name) std::source_location name = std::source_location::current()

Expand Down Expand Up @@ -564,6 +573,7 @@ template <TStr Str, class ValueT = int>
auto& KV = get_or_insert<ValueT>(Str.str());

struct NamedCounter {
constexpr NamedCounter(const char* name, int* counter, SLOC(loc)) : Name(get_global_string(name ? name : "")), Counter(counter), LastLoc(loc) {}
constexpr NamedCounter(const char* name, SLOC(loc)) : Name(get_global_string( name ? name : "")),
Counter(&get_or_insert<int>(std::string(Name))), LastLoc(loc) {}
constexpr NamedCounter(TStr name, SLOC(loc)) : Name(get_global_string(name.sv())), Counter(&get_or_insert<int>(name.str())), LastLoc(loc) {}
Expand Down Expand Up @@ -608,8 +618,23 @@ struct NamedCounter {
return LHS;
}

friend auto operator<=>(NamedCounter& LHS, int RHS) {
return *LHS.Counter <=> RHS;
friend NamedCounter& operator-=(NamedCounter& LHS, int RHS) {
*LHS.Counter -= RHS;
return LHS;
}

bool operator==(int RHS) const {
return *Counter == RHS;
}

auto operator<=>(int RHS) const {
return *Counter <=> RHS;
}

int consume() {
int tmp = *Counter;
*Counter = 0;
return tmp;
}

bool assert_eq(int RHS, SLOC(loc)) {
Expand Down Expand Up @@ -657,7 +682,7 @@ struct NamedCounter {


template <TStr Key>
auto NC = NamedCounter{Key.str(), &KV<Key>};
auto NC = NamedCounter{get_global_string(Key.str()).data(), &KV<Key>};


inline bool count(bool value) {
Expand Down Expand Up @@ -734,39 +759,44 @@ template <TStr... Key>
auto CounterGroup = std::tuple<AsType<Key, int&>...>{GetCounterStore<int>()[Key.str()]...};

struct AliveCounter {
explicit AliveCounter(const char* key) : Counter(&CounterStore[key]) {
assert(Counter && *Counter >= 0);
*Counter += 1;
explicit AliveCounter(NamedCounter counter, SLOC(loc)) : Counter(counter) {
assert(Counter >= 0);
Counter += 1;
}
explicit AliveCounter(const char* key, SLOC(loc)) : Counter(key, loc) {
assert(Counter >= 0);
Counter += 1;
}

AliveCounter(nullptr_t) = delete;
AliveCounter(void*) = delete;

constexpr AliveCounter(int* dest) : Counter(dest) {
assert(Counter && *Counter >= 0);
*Counter += 1;
constexpr AliveCounter(int* dest, SLOC(loc)) : Counter("", dest, loc) {
assert(Counter >= 0);
Counter += 1;
}

constexpr AliveCounter(AliveCounter const& RHS) : Counter(RHS.Counter) {
assert(Counter && *Counter >= 0);
*Counter += 1;
assert(Counter >= 0);
Counter += 1;
}

~AliveCounter() {
assert(*Counter >= 1);
*Counter -= 1;
assert(Counter >= 1);
Counter -= 1;
}

int* Counter;
NamedCounter Counter;
};

template <TStr Key>
struct CAliveCounter : private AliveCounter {
CAliveCounter() : AliveCounter(&KV<Key>) {}
CAliveCounter(SLOC(loc)) : AliveCounter(Key.sv().data(), loc) {}
CAliveCounter(CAliveCounter const& RHS) : AliveCounter(RHS) {}

~CAliveCounter() = default;
};

#pragma clang diagnostic pop

#endif // LIBCXX_TEST_CONTRACTS_SUPPORT_H
30 changes: 0 additions & 30 deletions libcxx/test/support/contracts_test.h

This file was deleted.

0 comments on commit dd42a17

Please sign in to comment.