Skip to content

Commit

Permalink
Only avoid mutating constinit, not const
Browse files Browse the repository at this point in the history
  • Loading branch information
JamesLee-Jones committed Jul 8, 2024
1 parent fb63028 commit ccd130b
Show file tree
Hide file tree
Showing 3 changed files with 53 additions and 13 deletions.
2 changes: 1 addition & 1 deletion src/libdredd/src/mutate_visitor.cc
Original file line number Diff line number Diff line change
Expand Up @@ -138,7 +138,7 @@ bool MutateVisitor::TraverseDecl(clang::Decl* decl) {
}
}
if (const auto* var_decl = llvm::dyn_cast<clang::VarDecl>(decl)) {
if (var_decl->isConstexpr() || var_decl->hasConstantInitialization()) {
if (var_decl->isConstexpr() || var_decl->hasAttr<clang::ConstInitAttr>()) {
// Because Dredd's mutations occur dynamically, they cannot be applied to
// C++ constexprs or variables that require constant initialization as
// these both require compile-time evaluation.
Expand Down
44 changes: 38 additions & 6 deletions test/single_file/signed_int_constants.cc.expected
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ static bool __dredd_enabled_mutation(int local_mutation_id) {
if (!token.empty()) {
int value = std::stoi(token);
int local_value = value - 0;
if (local_value >= 0 && local_value < 8) {
if (local_value >= 0 && local_value < 23) {
enabled_bitset[local_value / 64] |= (1 << (local_value % 64));
some_mutation_enabled = true;
}
Expand All @@ -42,13 +42,45 @@ static bool __dredd_enabled_mutation(int local_mutation_id) {
return (enabled_bitset[local_mutation_id / 64] & (1 << (local_mutation_id % 64))) != 0;
}

static int __dredd_replace_expr_int_zero(int arg, int local_mutation_id) {
if (!__dredd_some_mutation_enabled) return arg;
if (__dredd_enabled_mutation(local_mutation_id + 0)) return 1;
if (__dredd_enabled_mutation(local_mutation_id + 1)) return -1;
return arg;
}

static int __dredd_replace_expr_int_one(int arg, int local_mutation_id) {
if (!__dredd_some_mutation_enabled) return arg;
if (__dredd_enabled_mutation(local_mutation_id + 0)) return ~(arg);
if (__dredd_enabled_mutation(local_mutation_id + 1)) return 0;
if (__dredd_enabled_mutation(local_mutation_id + 2)) return -1;
return arg;
}

static int __dredd_replace_expr_int_minus_one(int arg, int local_mutation_id) {
if (!__dredd_some_mutation_enabled) return arg;
if (__dredd_enabled_mutation(local_mutation_id + 0)) return 0;
if (__dredd_enabled_mutation(local_mutation_id + 1)) return 1;
return arg;
}

static int __dredd_replace_expr_int_lvalue(int& arg, int local_mutation_id) {
if (!__dredd_some_mutation_enabled) return arg;
if (__dredd_enabled_mutation(local_mutation_id + 0)) return ++(arg);
if (__dredd_enabled_mutation(local_mutation_id + 1)) return --(arg);
return arg;
}

static int __dredd_replace_expr_int_constant(int arg, int local_mutation_id) {
if (!__dredd_some_mutation_enabled) return arg;
if (__dredd_enabled_mutation(local_mutation_id + 0)) return ~(arg);
if (__dredd_enabled_mutation(local_mutation_id + 1)) return -(arg);
if (__dredd_enabled_mutation(local_mutation_id + 2)) return 0;
if (__dredd_enabled_mutation(local_mutation_id + 3)) return 1;
if (__dredd_enabled_mutation(local_mutation_id + 4)) return -1;
return arg;
}

static int __dredd_replace_expr_int(int arg, int local_mutation_id) {
if (!__dredd_some_mutation_enabled) return arg;
if (__dredd_enabled_mutation(local_mutation_id + 0)) return !(arg);
Expand All @@ -61,9 +93,9 @@ static int __dredd_replace_expr_int(int arg, int local_mutation_id) {
}

void foo(int a) {
const int x = 0;
const int y = 1;
const int z = -1;
const int w = 2;
const int v = __dredd_replace_expr_int(__dredd_replace_expr_int_lvalue(a, 0), 2);
const int x = __dredd_replace_expr_int_zero(0, 0);
const int y = __dredd_replace_expr_int_one(1, 2);
const int z = __dredd_replace_expr_int_minus_one(-__dredd_replace_expr_int_one(1, 5), 8);
const int w = __dredd_replace_expr_int_constant(2, 10);
const int v = __dredd_replace_expr_int(__dredd_replace_expr_int_lvalue(a, 15), 17);
}
20 changes: 14 additions & 6 deletions test/single_file/signed_int_constants.cc.noopt.expected
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ static bool __dredd_enabled_mutation(int local_mutation_id) {
if (!token.empty()) {
int value = std::stoi(token);
int local_value = value - 0;
if (local_value >= 0 && local_value < 8) {
if (local_value >= 0 && local_value < 41) {
enabled_bitset[local_value / 64] |= (1 << (local_value % 64));
some_mutation_enabled = true;
}
Expand All @@ -42,6 +42,14 @@ static bool __dredd_enabled_mutation(int local_mutation_id) {
return (enabled_bitset[local_mutation_id / 64] & (1 << (local_mutation_id % 64))) != 0;
}

static int __dredd_replace_unary_operator_Minus_int(int arg, int local_mutation_id) {
if (!__dredd_some_mutation_enabled) return -arg;
if (__dredd_enabled_mutation(local_mutation_id + 0)) return ~arg;
if (__dredd_enabled_mutation(local_mutation_id + 1)) return !arg;
if (__dredd_enabled_mutation(local_mutation_id + 2)) return arg;
return -arg;
}

static int __dredd_replace_expr_int_lvalue(int& arg, int local_mutation_id) {
if (!__dredd_some_mutation_enabled) return arg;
if (__dredd_enabled_mutation(local_mutation_id + 0)) return ++(arg);
Expand All @@ -61,9 +69,9 @@ static int __dredd_replace_expr_int(int arg, int local_mutation_id) {
}

void foo(int a) {
const int x = 0;
const int y = 1;
const int z = -1;
const int w = 2;
const int v = __dredd_replace_expr_int(__dredd_replace_expr_int_lvalue(a, 0), 2);
const int x = __dredd_replace_expr_int(0, 0);
const int y = __dredd_replace_expr_int(1, 6);
const int z = __dredd_replace_expr_int(__dredd_replace_unary_operator_Minus_int(__dredd_replace_expr_int(1, 12), 18), 21);
const int w = __dredd_replace_expr_int(2, 27);
const int v = __dredd_replace_expr_int(__dredd_replace_expr_int_lvalue(a, 33), 35);
}

0 comments on commit ccd130b

Please sign in to comment.