Skip to content

Commit

Permalink
workaround gcc/clang bugs
Browse files Browse the repository at this point in the history
  • Loading branch information
kelbon committed Aug 13, 2023
1 parent b543946 commit e9e53cc
Show file tree
Hide file tree
Showing 4 changed files with 43 additions and 4 deletions.
8 changes: 8 additions & 0 deletions include/channel.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,10 @@

#include "common.hpp"

#ifdef __GNUC__
#pragma GCC diagnostic push
#pragma GCC diagnostic ignored "-Wattributes"
#endif
namespace dd {

// behavior very similar to generator, but channel may suspend before co_yield
Expand Down Expand Up @@ -289,3 +293,7 @@ struct channel {
if (VARDECL = *dd_b_; true)

} // namespace dd

#ifdef __GNUC__
#pragma GCC diagnostic pop
#endif
13 changes: 11 additions & 2 deletions include/common.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -491,8 +491,15 @@ struct elements_extractor {

template <typename U>
static channel<Yield> do_extract(channel<U>& c) {
#ifndef __GNUC__
for (auto b = co_await c.begin(); b != c.end(); co_await ++b)
co_yield static_cast<Yield>(*b);
#else
for (auto b = co_await c.begin(); b != c.end();) {
co_yield static_cast<Yield>(*b);
co_await ++b; // workaround gcc bug "not enougth contextual info about type"
}
#endif
}
template <typename U>
static channel<Yield> do_extract(channel<U>&& c) {
Expand All @@ -504,8 +511,10 @@ struct elements_extractor {
static Generator<Yield> do_extract(auto&& rng) {
if constexpr (!std::ranges::borrowed_range<decltype(rng)> &&
std::is_same_v<std::ranges::range_rvalue_reference_t<decltype(rng)>, Yield&&>) {
auto&& b = std::ranges::begin(rng);
auto&& e = std::ranges::end(rng);
using std::begin;
using std::end;
auto&& b = begin(rng);
auto&& e = end(rng);
for (; b != e; ++b)
co_yield std::ranges::iter_move(b);
} else {
Expand Down
11 changes: 9 additions & 2 deletions include/generator.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,14 @@

#include "common.hpp"

#if __clang__
#ifdef __clang__
#pragma clang diagnostic push
#pragma clang diagnostic ignored "-Wunknown-attributes"
#endif
#ifdef __GNUC__
#pragma GCC diagnostic push
#pragma GCC diagnostic ignored "-Wattributes"
#endif

namespace dd {

Expand Down Expand Up @@ -243,6 +247,9 @@ struct generator {

} // namespace dd

#if __clang__
#ifdef __clang__
#pragma clang diagnostic pop
#endif
#ifdef __GNUC__
#pragma GCC diagnostic pop
#endif
15 changes: 15 additions & 0 deletions tests/tests_generator.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,20 @@ TEST(base) {
}
return error_count;
}
channel<float> base_case_chan() {
channel<int> c = base_case<channel>();
co_yield elements_of(c);
}
CHANNEL_TEST(chan_yield) {
std::vector<int> v;
co_foreach(int i, base_case_chan()) v.push_back(i);
std::vector<int> check(100, 0);
std::iota(begin(check), end(check), 0);
error_if(v != check);
co_return error_count;
}
CO_TEST(chan_yield);

TEST(base2) {
std::vector<int> vec;
auto g = base_case<dd::generator>();
Expand Down Expand Up @@ -661,6 +675,7 @@ int main() {
RUN(input_rng_channel);
RUN(nomove_gen);
RUN(nomove_gen_channel);
RUN(chan_yield);

return ec;
}

0 comments on commit e9e53cc

Please sign in to comment.