1 /**
2 Copyright: Copyright (c) 2018, Joakim Brännström. All rights reserved.
3 License: MPL-2
4 Author: Joakim Brännström (joakim.brannstrom@gmx.com)
5 
6 This Source Code Form is subject to the terms of the Mozilla Public License,
7 v.2.0. If a copy of the MPL was not distributed with this file, You can obtain
8 one at http://mozilla.org/MPL/2.0/.
9 */
10 module dextool.plugin.mutate.backend.mutation_type.dcr;
11 
12 import logger = std.experimental.logger;
13 
14 import dextool.plugin.mutate.backend.type;
15 import dextool.plugin.mutate.backend.analyze.ast;
16 
17 /// Information used to intelligently generate ror mutants;
18 struct DcrInfo {
19     Kind operator;
20     Type ty;
21 }
22 
23 Mutation.Kind[] dcrMutations(DcrInfo info) @safe {
24     import std.algorithm : among;
25 
26     typeof(return) rval;
27     // an operator is a predicate, leaf.
28     // the condition is obviously the top node.
29     switch (info.operator) with (Mutation.Kind) {
30     case Kind.Call:
31         goto case;
32     case Kind.Expr:
33         // conservative only replace an expr if it is a boolean.
34         // replace the functions body with return true/false;
35         if (info.ty !is null && info.ty.kind == TypeKind.boolean)
36             rval = [dcrTrue, dcrFalse];
37         break;
38     case Kind.Condition:
39         rval = [dcrTrue, dcrFalse];
40         break;
41     case Kind.OpAnd:
42         goto case;
43     case Kind.OpOr:
44         goto case;
45     case Kind.OpLess:
46         goto case;
47     case Kind.OpGreater:
48         goto case;
49     case Kind.OpLessEq:
50         goto case;
51     case Kind.OpGreaterEq:
52         goto case;
53     case Kind.OpEqual:
54         goto case;
55     case Kind.OpNotEqual:
56         // pessimistic, no type then do nothing.
57         // discrete because it degenerate to a boolean.
58         if (info.ty !is null
59                 && info.ty.kind.among(TypeKind.boolean, TypeKind.discrete))
60             rval = [dcrTrue, dcrFalse];
61         break;
62     default:
63     }
64 
65     return rval;
66 }
67 
68 immutable Mutation.Kind[] dcrMutationsAll;
69 
70 shared static this() {
71     with (Mutation.Kind) {
72         dcrMutationsAll = [dcrTrue, dcrFalse];
73     }
74 }