-
Notifications
You must be signed in to change notification settings - Fork 8
/
arithmeticassignmentinvert.go
50 lines (42 loc) · 1.12 KB
/
arithmeticassignmentinvert.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
package arithmeticassignmentinvert
import (
"go/ast"
"go/token"
"go/types"
"github.com/gtramontina/ooze/viruses"
)
type ArithmeticAssignmentInvert struct {
mutations map[token.Token]token.Token
}
// New creates a new ArithmeticAssignmentInvert virus.
//
// It replaces `+=` with `-=`, `*=` with `/=`, `%=` with `*=` and vice versa.
func New() *ArithmeticAssignmentInvert {
return &ArithmeticAssignmentInvert{
mutations: map[token.Token]token.Token{
token.ADD_ASSIGN: token.SUB_ASSIGN,
token.SUB_ASSIGN: token.ADD_ASSIGN,
token.MUL_ASSIGN: token.QUO_ASSIGN,
token.QUO_ASSIGN: token.MUL_ASSIGN,
token.REM_ASSIGN: token.MUL_ASSIGN,
},
}
}
func (v *ArithmeticAssignmentInvert) Incubate(node ast.Node, _ *types.Info) []*viruses.Infection {
statement, matches := node.(*ast.AssignStmt)
if !matches {
return nil
}
originalToken := statement.Tok
mutatedToken, matches := v.mutations[statement.Tok]
if !matches {
return nil
}
return []*viruses.Infection{
viruses.NewInfection(
"Arithmetic Assignment Invert",
func() { statement.Tok = mutatedToken },
func() { statement.Tok = originalToken },
),
}
}