-
Notifications
You must be signed in to change notification settings - Fork 8
/
arithmeticassignment.go
57 lines (49 loc) · 1.35 KB
/
arithmeticassignment.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
51
52
53
54
55
56
57
package arithmeticassignment
import (
"go/ast"
"go/token"
"go/types"
"github.com/gtramontina/ooze/viruses"
)
type ArithmeticAssignment struct {
mutations map[token.Token]token.Token
}
// New creates a new ArithmeticAssignment virus.
//
// It replaces `+=`, `-=`, `*=`, `/=`, `%=`, `&=`, <code>|=</code>, `^=`,
// `<<=`, `>>=` and `&^=` with `=`.
func New() *ArithmeticAssignment {
return &ArithmeticAssignment{
mutations: map[token.Token]token.Token{
token.ADD_ASSIGN: token.ASSIGN,
token.SUB_ASSIGN: token.ASSIGN,
token.MUL_ASSIGN: token.ASSIGN,
token.QUO_ASSIGN: token.ASSIGN,
token.REM_ASSIGN: token.ASSIGN,
token.AND_ASSIGN: token.ASSIGN,
token.OR_ASSIGN: token.ASSIGN,
token.XOR_ASSIGN: token.ASSIGN,
token.SHL_ASSIGN: token.ASSIGN,
token.SHR_ASSIGN: token.ASSIGN,
token.AND_NOT_ASSIGN: token.ASSIGN,
},
}
}
func (v *ArithmeticAssignment) 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",
func() { statement.Tok = mutatedToken },
func() { statement.Tok = originalToken },
),
}
}