Skip to content

Commit 9a7b0a9

Browse files
committed
bugfix: don't do things with side-effects in assertins
1 parent 10a8055 commit 9a7b0a9

File tree

7 files changed

+47
-33
lines changed

7 files changed

+47
-33
lines changed

include/reactor-cpp/assert.hh

Lines changed: 0 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -33,17 +33,8 @@ constexpr bool runtime_assertion = true;
3333
#include <unistd.h>
3434
#endif
3535

36-
// assert macro that avoids unused variable warnings
37-
#ifdef NDEBUG
38-
// NOLINTNEXTLINE(cppcoreguidelines-macro-usage)
39-
#define reactor_assert(x) \
40-
do { \
41-
(void)sizeof(x); \
42-
} while (0)
43-
#else
4436
// NOLINTNEXTLINE(cppcoreguidelines-macro-usage)
4537
#define reactor_assert(x) assert(x)
46-
#endif
4738

4839
namespace reactor {
4940
using EnvPhase = Environment::Phase;

include/reactor-cpp/connection.hh

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,10 @@ public:
4848
port->register_set_callback(upstream_set_callback());
4949
}
5050

51-
virtual void bind_downstream_port(Port<T>* port) { reactor_assert(this->downstream_ports_.insert(port).second); };
51+
virtual void bind_downstream_port(Port<T>* port) {
52+
[[maybe_unused]] bool result = this->downstream_ports_.insert(port).second;
53+
reactor_assert(result);
54+
};
5255
};
5356

5457
template <class T> class BaseDelayedConnection : public Connection<T> {

lib/action.cc

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,8 @@ void BaseAction::register_trigger(Reaction* reaction) {
2222
validate(this->container() == reaction->container(),
2323
"Action triggers must belong to the same reactor as the triggered "
2424
"reaction");
25-
reactor_assert(triggers_.insert(reaction).second);
25+
[[maybe_unused]] bool result = triggers_.insert(reaction).second;
26+
reactor_assert(result);
2627
}
2728

2829
void BaseAction::register_scheduler(Reaction* reaction) {
@@ -32,7 +33,8 @@ void BaseAction::register_scheduler(Reaction* reaction) {
3233
// the reaction must belong to the same reactor as this action
3334
validate(this->container() == reaction->container(), "Scheduable actions must belong to the same reactor as the "
3435
"triggered reaction");
35-
reactor_assert(schedulers_.insert(reaction).second);
36+
[[maybe_unused]] bool result = schedulers_.insert(reaction).second;
37+
reactor_assert(result);
3638
}
3739

3840
void Timer::startup() {

lib/environment.cc

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -40,20 +40,23 @@ Environment::Environment(const std::string& name, Environment* containing_enviro
4040
, top_environment_(containing_environment_->top_environment_)
4141
, scheduler_(this)
4242
, timeout_(containing_environment->timeout()) {
43-
reactor_assert(containing_environment->contained_environments_.insert(this).second);
43+
[[maybe_unused]] bool result = containing_environment->contained_environments_.insert(this).second;
44+
reactor_assert(result);
4445
}
4546

4647
void Environment::register_reactor(Reactor* reactor) {
4748
reactor_assert(reactor != nullptr);
4849
validate(this->phase() == Phase::Construction, "Reactors may only be registered during construction phase!");
4950
validate(reactor->is_top_level(), "The environment may only contain top level reactors!");
50-
reactor_assert(top_level_reactors_.insert(reactor).second);
51+
[[maybe_unused]] bool result = top_level_reactors_.insert(reactor).second;
52+
reactor_assert(result);
5153
}
5254

5355
void Environment::register_input_action(BaseAction* action) {
5456
reactor_assert(action != nullptr);
5557
validate(this->phase() == Phase::Construction, "Input actions may only be registered during construction phase!");
56-
reactor_assert(input_actions_.insert(action).second);
58+
[[maybe_unused]] bool result = input_actions_.insert(action).second;
59+
reactor_assert(result);
5760
run_forever_ = true;
5861
}
5962

lib/port.cc

Lines changed: 11 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ namespace reactor {
1616

1717
void BasePort::base_bind_to(BasePort* port) {
1818
reactor_assert(port != nullptr);
19-
reactor_assert(this->environment() == port->environment()); // NOLINT
19+
reactor_assert(this->environment() == port->environment());
2020
validate(!port->has_inward_binding(), "Ports may only be connected once");
2121
validate(!port->has_anti_dependencies(), "Ports with anti dependencies may not be connected to other ports");
2222
assert_phase(this, Environment::Phase::Assembly);
@@ -41,12 +41,13 @@ void BasePort::base_bind_to(BasePort* port) {
4141
}
4242

4343
port->inward_binding_ = this;
44-
reactor_assert(this->outward_bindings_.insert(port).second);
44+
[[maybe_unused]] bool result = this->outward_bindings_.insert(port).second;
45+
reactor_assert(result);
4546
}
4647

4748
void BasePort::register_dependency(Reaction* reaction, bool is_trigger) noexcept {
4849
reactor_assert(reaction != nullptr);
49-
reactor_assert(this->environment() == reaction->environment()); // NOLINT
50+
reactor_assert(this->environment() == reaction->environment());
5051
validate(!this->has_outward_bindings(), "Dependencies may no be declared on ports with an outward binding!");
5152
assert_phase(this, Environment::Phase::Assembly);
5253

@@ -58,15 +59,17 @@ void BasePort::register_dependency(Reaction* reaction, bool is_trigger) noexcept
5859
"Dependent output ports must belong to a contained reactor");
5960
}
6061

61-
reactor_assert(dependencies_.insert(reaction).second);
62+
[[maybe_unused]] bool result = dependencies_.insert(reaction).second;
63+
reactor_assert(result);
6264
if (is_trigger) {
63-
reactor_assert(triggers_.insert(reaction).second);
65+
result = triggers_.insert(reaction).second;
66+
reactor_assert(result);
6467
}
6568
}
6669

6770
void BasePort::register_antidependency(Reaction* reaction) noexcept {
6871
reactor_assert(reaction != nullptr);
69-
reactor_assert(this->environment() == reaction->environment()); // NOLINT
72+
reactor_assert(this->environment() == reaction->environment());
7073
validate(!this->has_inward_binding(), "Antidependencies may no be declared on ports with an inward binding!");
7174
assert_phase(this, Environment::Phase::Assembly);
7275

@@ -79,7 +82,8 @@ void BasePort::register_antidependency(Reaction* reaction) noexcept {
7982
"Antidependent input ports must belong to a contained reactor");
8083
}
8184

82-
reactor_assert(anti_dependencies_.insert(reaction).second);
85+
[[maybe_unused]] bool result = anti_dependencies_.insert(reaction).second;
86+
reactor_assert(result);
8387
}
8488

8589
[[maybe_unused]] auto Port<void>::typed_outward_bindings() const noexcept -> const std::set<Port<void>*>& {

lib/reaction.cc

Lines changed: 12 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,8 @@ void Reaction::declare_trigger(BaseAction* action) {
3131
validate(this->container() == action->container(), "Action triggers must belong to the same reactor as the triggered "
3232
"reaction");
3333

34-
reactor_assert(action_triggers_.insert(action).second);
34+
[[maybe_unused]] bool result = action_triggers_.insert(action).second;
35+
reactor_assert(result);
3536
action->register_trigger(this);
3637
}
3738

@@ -42,7 +43,8 @@ void Reaction::declare_schedulable_action(BaseAction* action) {
4243
validate(this->container() == action->container(), "Scheduable actions must belong to the same reactor as the "
4344
"triggered reaction");
4445

45-
reactor_assert(scheduable_actions_.insert(action).second);
46+
[[maybe_unused]] bool result = scheduable_actions_.insert(action).second;
47+
reactor_assert(result);
4648
action->register_scheduler(this);
4749
}
4850

@@ -61,8 +63,10 @@ void Reaction::declare_trigger(BasePort* port) {
6163
"Output port triggers must belong to a contained reactor");
6264
}
6365

64-
reactor_assert(port_trigger_.insert(port).second);
65-
reactor_assert(dependencies_.insert(port).second);
66+
[[maybe_unused]] bool result = port_trigger_.insert(port).second;
67+
reactor_assert(result);
68+
result = dependencies_.insert(port).second;
69+
reactor_assert(result);
6670
port->register_dependency(this, true);
6771
}
6872

@@ -79,7 +83,8 @@ void Reaction::declare_dependency(BasePort* port) {
7983
"Dependent output ports must belong to a contained reactor");
8084
}
8185

82-
reactor_assert(dependencies_.insert(port).second);
86+
[[maybe_unused]] bool result = dependencies_.insert(port).second;
87+
reactor_assert(result);
8388
port->register_dependency(this, false);
8489
}
8590

@@ -96,7 +101,8 @@ void Reaction::declare_antidependency(BasePort* port) {
96101
"Antidependent input ports must belong to a contained reactor");
97102
}
98103

99-
reactor_assert(antidependencies_.insert(port).second);
104+
[[maybe_unused]] bool result = antidependencies_.insert(port).second;
105+
reactor_assert(result);
100106
port->register_antidependency(this);
101107
}
102108

lib/reactor.cc

Lines changed: 10 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -80,36 +80,41 @@ void Reactor::register_action([[maybe_unused]] BaseAction* action) {
8080
reactor::validate(this->environment()->phase() == Environment::Phase::Construction ||
8181
this->environment()->phase() == Environment::Phase::Assembly,
8282
"Actions can only be registered during construction phase!");
83-
reactor_assert(actions_.insert(action).second);
83+
[[maybe_unused]] bool result = actions_.insert(action).second;
84+
reactor_assert(result);
8485
}
8586

8687
void Reactor::register_input(BasePort* port) {
8788
reactor_assert(port != nullptr);
8889
reactor::validate(this->environment()->phase() == Environment::Phase::Construction,
8990
"Ports can only be registered during construction phase!");
90-
reactor_assert(inputs_.insert(port).second);
91+
[[maybe_unused]] bool result = inputs_.insert(port).second;
92+
reactor_assert(result);
9193
}
9294

9395
void Reactor::register_output(BasePort* port) {
9496
reactor_assert(port != nullptr);
9597
reactor::validate(this->environment()->phase() == Environment::Phase::Construction,
9698
"Ports can only be registered during construction phase!");
97-
reactor_assert(outputs_.insert(port).second);
99+
[[maybe_unused]] bool result = inputs_.insert(port).second;
100+
reactor_assert(result);
98101
}
99102

100103
void Reactor::register_reaction([[maybe_unused]] Reaction* reaction) {
101104
reactor_assert(reaction != nullptr);
102105

103106
validate(this->environment()->phase() == Environment::Phase::Construction,
104107
"Reactions can only be registered during construction phase!");
105-
reactor_assert(reactions_.insert(reaction).second);
108+
[[maybe_unused]] bool result = reactions_.insert(reaction).second;
109+
reactor_assert(result);
106110
}
107111

108112
void Reactor::register_reactor([[maybe_unused]] Reactor* reactor) {
109113
reactor_assert(reactor != nullptr);
110114
validate(this->environment()->phase() == Environment::Phase::Construction,
111115
"Reactions can only be registered during construction phase!");
112-
reactor_assert(reactors_.insert(reactor).second);
116+
[[maybe_unused]] bool result = reactors_.insert(reactor).second;
117+
reactor_assert(result);
113118
}
114119

115120
void Reactor::startup() {

0 commit comments

Comments
 (0)