Skip to content

Commit

Permalink
Merge pull request #1381 from joakim-brannstrom/mutate-fix-assign-in-…
Browse files Browse the repository at this point in the history
…if-stmt

mutate: relax assignment schema in if stmt
  • Loading branch information
joakim-brannstrom committed May 10, 2021
2 parents 5507627 + 0568128 commit ffd2020
Show file tree
Hide file tree
Showing 3 changed files with 42 additions and 3 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -541,8 +541,7 @@ final class BaseVisitor : ExtendedVisitor {
}

private void visitVar(T)(T v) @trusted {
auto n = ast.make!(analyze.VarDecl);
pushStack(n, v);
pushStack(ast.make!(analyze.VarDecl), v);
}

override void visit(const Directive v) {
Expand Down Expand Up @@ -1263,6 +1262,7 @@ void rewriteCondition(ref analyze.Ast ast, analyze.Condition root) {
import sumtype;
import dextool.plugin.mutate.backend.analyze.ast : TypeId, VarDecl, Kind;

// set the type of the Condition to the first expression with a type.
foreach (ty; BreathFirstRange(root).map!(a => ast.typeId(a))
.filter!(a => a.hasValue)) {
sumtype.match!((Some!TypeId a) => ast.put(root, a), (None a) {})(ty);
Expand All @@ -1271,7 +1271,7 @@ void rewriteCondition(ref analyze.Ast ast, analyze.Condition root) {

foreach (a; BreathFirstRange(root).filter!(a => a.kind == Kind.VarDecl)) {
ast.put(root, ast.location(a));
root.schemaBlacklist = true;
// can't delete the VarDecl explicitly because the code will not compile
a.schemaBlacklist = true;
break;
}
Expand Down
20 changes: 20 additions & 0 deletions plugin/mutate/test/test_schemata.d
Original file line number Diff line number Diff line change
Expand Up @@ -415,3 +415,23 @@ class ShallGenerateValidSchemaForTerneryOp : SchemataFixutre {
r.output);
}
}

class ShallGenerateValidSchemaForIfStmtAssign : SchemataFixutre {
override string programFile() {
return (testData ~ "schemata_ifstmt.cpp").toString;
}

override void test() {
mixin(EnvSetup(globalTestdir));
precondition(testEnv);

makeDextoolAnalyze(testEnv).addInputArg(programCode).addPostArg([
"--mutant", "all"
]).run;

auto r = runDextoolTest(testEnv).addPostArg(["--mutant", "all"]).run;

testAnyOrder!SubStr(["Skipping schema because it failed to compile"]).shouldNotBeIn(
r.output);
}
}
19 changes: 19 additions & 0 deletions plugin/mutate/testdata/schemata_ifstmt.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
/// @copyright Boost License 1.0, http://boost.org/LICENSE_1_0.txt
/// @date 2021
/// @author Joakim Brännström (joakim.brannstrom@gmx.com)

char* fn1() { return nullptr; }
bool fn2() { return true; }
bool fn3(int x) { return true; }

int main(int argc, char** argv) {
if (char* x = fn1()) {
argv[0] = nullptr;
}
if (bool x = fn3(42)) {
}
if (fn2()) {
}

return 0;
}

0 comments on commit ffd2020

Please sign in to comment.