mc-imperial / dredd

Framework for evaluating C/C++ compiler testing tools
Apache License 2.0
11 stars 3 forks source link

Dredd mutates constexpr #233

Closed JamesLee-Jones closed 3 months ago

JamesLee-Jones commented 3 months ago

Dredd currently mutates constexpr expressions. Since these should be possible to evaluate at compile time, this is not valid unless the Dredd mutation functions are made constexpr accordingly or we avoid mutating them.

The following example demonstrates this error.

#include <iostream>

constexpr int Max(int a, int b) { return a > b ? a : b }

int main() {
  std::cout << Max(5,4) << std::endl;
}

This gives the following error upon compilation:

test.cc:111:15: error: constexpr function never produces a constant expression [-Winvalid-constexpr]
constexpr int Max(int a, int b) { if (!__dredd_enabled_mutation(27)) { return __dredd_replace_expr_int(__dredd_replace_expr_int_lvalue(__dredd_replace_expr_bool_omit_true(__dredd_replace_binary_operator_GT_arg1_int_arg2_int(__dredd_replace_expr_int(__dredd_replace_expr_int_lvalue(a, 0), 2) , __dredd_replace_expr_int(__dredd_replace_expr_int_lvalue(b, 8), 10), 16), 18) ? a : b, 19), 21); } }
              ^
test.cc:111:40: note: non-constexpr function '__dredd_enabled_mutation' cannot be used in a constant expression
constexpr int Max(int a, int b) { if (!__dredd_enabled_mutation(27)) { return __dredd_replace_expr_int(__dredd_replace_expr_int_lvalue(__dredd_replace_expr_bool_omit_true(__dredd_replace_binary_operator_GT_arg1_int_arg2_int(__dredd_replace_expr_int(__dredd_replace_expr_int_lvalue(a, 0), 2) , __dredd_replace_expr_int(__dredd_replace_expr_int_lvalue(b, 8), 10), 16), 18) ? a : b, 19), 21); } }
                                       ^
test.cc:16:13: note: declared here
static bool __dredd_enabled_mutation(int local_mutation_id) {
            ^
1 error generated.
JamesLee-Jones commented 3 months ago

Since we can't tell at compile time whether a mutation is enabled, I suppose we will have to avoid mutating these expressions. The approach to this may be different in #179.