-
Notifications
You must be signed in to change notification settings - Fork 38
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(analyze): add unique message ID analyzer
- Loading branch information
Showing
3 changed files
with
75 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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", | ||
}, | ||
}, | ||
}, | ||
}) | ||
} |