From ccd130b1eb5f55e286ab5fd9dc7e619b042642d1 Mon Sep 17 00:00:00 2001 From: James Lee-Jones Date: Mon, 8 Jul 2024 10:27:02 +0100 Subject: [PATCH] Only avoid mutating constinit, not const --- src/libdredd/src/mutate_visitor.cc | 2 +- .../signed_int_constants.cc.expected | 44 ++++++++++++++++--- .../signed_int_constants.cc.noopt.expected | 20 ++++++--- 3 files changed, 53 insertions(+), 13 deletions(-) diff --git a/src/libdredd/src/mutate_visitor.cc b/src/libdredd/src/mutate_visitor.cc index 582611c1..5bd31192 100644 --- a/src/libdredd/src/mutate_visitor.cc +++ b/src/libdredd/src/mutate_visitor.cc @@ -138,7 +138,7 @@ bool MutateVisitor::TraverseDecl(clang::Decl* decl) { } } if (const auto* var_decl = llvm::dyn_cast(decl)) { - if (var_decl->isConstexpr() || var_decl->hasConstantInitialization()) { + if (var_decl->isConstexpr() || var_decl->hasAttr()) { // 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. diff --git a/test/single_file/signed_int_constants.cc.expected b/test/single_file/signed_int_constants.cc.expected index 6ac6d28d..55833874 100644 --- a/test/single_file/signed_int_constants.cc.expected +++ b/test/single_file/signed_int_constants.cc.expected @@ -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; } @@ -42,6 +42,28 @@ 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); @@ -49,6 +71,16 @@ static int __dredd_replace_expr_int_lvalue(int& arg, int local_mutation_id) { 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); @@ -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); } diff --git a/test/single_file/signed_int_constants.cc.noopt.expected b/test/single_file/signed_int_constants.cc.noopt.expected index 6ac6d28d..7fea59d0 100644 --- a/test/single_file/signed_int_constants.cc.noopt.expected +++ b/test/single_file/signed_int_constants.cc.noopt.expected @@ -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; } @@ -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); @@ -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); }