Skip to content

Commit

Permalink
feat(analyze): add unique message ID analyzer
Browse files Browse the repository at this point in the history
  • Loading branch information
maxekman committed Jun 24, 2024
1 parent 357da1e commit 36c7f19
Show file tree
Hide file tree
Showing 3 changed files with 75 additions and 0 deletions.
2 changes: 2 additions & 0 deletions cmd/cantool/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ import (
"go.einride.tech/can/pkg/dbc/analysis/passes/signalnames"
"go.einride.tech/can/pkg/dbc/analysis/passes/singletondefinitions"
"go.einride.tech/can/pkg/dbc/analysis/passes/siunits"
"go.einride.tech/can/pkg/dbc/analysis/passes/uniquemessageids"
"go.einride.tech/can/pkg/dbc/analysis/passes/uniquenodenames"
"go.einride.tech/can/pkg/dbc/analysis/passes/uniquesignalnames"
"go.einride.tech/can/pkg/dbc/analysis/passes/unitsuffixes"
Expand Down Expand Up @@ -133,6 +134,7 @@ func analyzers() []*analysis.Analyzer {
signalnames.Analyzer(),
singletondefinitions.Analyzer(),
siunits.Analyzer(),
uniquemessageids.Analyzer(),
uniquenodenames.Analyzer(),
uniquesignalnames.Analyzer(),
unitsuffixes.Analyzer(),
Expand Down
30 changes: 30 additions & 0 deletions pkg/dbc/analysis/passes/uniquemessageids/analyzer.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
package uniquemessageids

import (
"go.einride.tech/can/pkg/dbc"
"go.einride.tech/can/pkg/dbc/analysis"
)

func Analyzer() *analysis.Analyzer {
return &analysis.Analyzer{
Name: "uniquemessageids",
Doc: "check that all message IDs are unique",
Run: run,
}
}

func run(pass *analysis.Pass) error {
messageIDs := make(map[dbc.MessageID]struct{})
for _, def := range pass.File.Defs {
message, ok := def.(*dbc.MessageDef)
if !ok || dbc.IsIndependentSignalsMessage(message) {
continue
}
if _, ok := messageIDs[message.MessageID]; ok {
pass.Reportf(message.Pos, "non-unique message ID")
} else {
messageIDs[message.MessageID] = struct{}{}
}
}
return nil
}
43 changes: 43 additions & 0 deletions pkg/dbc/analysis/passes/uniquemessageids/analyzer_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
package uniquemessageids

import (
"testing"
"text/scanner"

"go.einride.tech/can/pkg/dbc/analysis"
"go.einride.tech/can/pkg/dbc/analysis/analysistest"
)

func TestAnalyzer(t *testing.T) {
analysistest.Run(t, Analyzer(), []*analysistest.Case{
{
Name: "ok",
Data: `
BO_ 101 MOTOR_CMD: 1 DRIVER
SG_ MOTOR_CMD_steer : 0|4@1- (1,-5) [-5|5] "" MOTOR
SG_ MOTOR_CMD_drive : 4|4@1+ (1,0) [0|9] "" MOTOR
BO_ 102 MOTOR_CMD: 1 DRIVER
SG_ MOTOR_CMD_steer : 0|4@1- (1,-5) [-5|5] "" MOTOR
SG_ MOTOR_CMD_drive : 4|4@1+ (1,0) [0|9] "" MOTOR
`,
},

{
Name: "duplicate",
Data: `
BO_ 103 MOTOR_CMD: 1 DRIVER
SG_ MOTOR_CMD_steer : 0|4@1- (1,-5) [-5|5] "" MOTOR
SG_ MOTOR_CMD_steer : 4|4@1+ (1,0) [0|9] "" MOTOR
BO_ 103 MOTOR_CMD: 1 DRIVER
SG_ MOTOR_CMD_steer : 0|4@1- (1,-5) [-5|5] "" MOTOR
SG_ MOTOR_CMD_steer : 4|4@1+ (1,0) [0|9] "" MOTOR
`,
Diagnostics: []*analysis.Diagnostic{
{
Pos: scanner.Position{Line: 4, Column: 1},
Message: "non-unique message ID",
},
},
},
})
}

0 comments on commit 36c7f19

Please sign in to comment.