forked from einride/can-go
-
Notifications
You must be signed in to change notification settings - Fork 0
/
analyzer.go
60 lines (55 loc) · 1.37 KB
/
analyzer.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
58
59
60
package boolprefix
import (
"strings"
"github.com/seanbao/can-go/pkg/dbc"
"github.com/seanbao/can-go/pkg/dbc/analysis"
)
func Analyzer() *analysis.Analyzer {
return &analysis.Analyzer{
Name: "boolprefix",
Doc: "check that bools (1-bit signals) have a correct prefix",
Run: run,
}
}
func allowedPrefixes() []string {
return []string{
"Is",
"Has",
}
}
func run(pass *analysis.Pass) error {
for _, d := range pass.File.Defs {
messageDef, ok := d.(*dbc.MessageDef)
if !ok {
continue
}
SignalLoop:
for _, signalDef := range messageDef.Signals {
if signalDef.Size != 1 {
continue // skip all non-bool signals
}
for _, allowedPrefix := range allowedPrefixes() {
if strings.HasPrefix(string(signalDef.Name), allowedPrefix) {
continue SignalLoop // has allowed prefix
}
}
// edge-case: allow non-prefixed 1-bit signals with value descriptions
for _, d := range pass.File.Defs {
valueDescriptionsDef, ok := d.(*dbc.ValueDescriptionsDef)
if !ok {
continue // not value descriptions
}
if valueDescriptionsDef.MessageID == messageDef.MessageID &&
valueDescriptionsDef.SignalName == signalDef.Name {
continue SignalLoop // has value descriptions
}
}
pass.Reportf(
signalDef.Pos,
"bool signals (1-bit) must have prefix %s",
strings.Join(allowedPrefixes(), " or "),
)
}
}
return nil
}