JOJ3/internal/parsers/cppcheck/parser.go
张泊明518370910136 b40072a485
All checks were successful
build-image / create-empty-commit (push) Successful in 5s
checks / build (push) Successful in 1m19s
feat: rename module
2024-09-30 22:04:18 -04:00

86 lines
1.7 KiB
Go

package cppcheck
import (
"encoding/json"
"fmt"
"strings"
"github.com/criyle/go-judge/envexec"
"github.com/joint-online-judge/JOJ3/internal/stage"
)
type CppCheck struct{}
type Match struct {
Severity []string
Score int
}
type Conf struct {
Score int `default:"100"`
Matches []Match
}
type Record struct {
File string `json:"file"`
Line int `json:"line"`
Column int `json:"column"`
Severity string `json:"severity"`
Message string `json:"message"`
Id string `json:"id"`
}
func Parse(executorResult stage.ExecutorResult, conf Conf) stage.ParserResult {
// stdout := executorResult.Files["stdout"]
stderr := executorResult.Files["stderr"]
if executorResult.Status != stage.Status(envexec.StatusAccepted) {
return stage.ParserResult{
Score: 0,
Comment: fmt.Sprintf(
"Unexpected executor status: %s.\nStderr: %s",
executorResult.Status, stderr,
),
}
}
records := make([]Record, 0)
lines := strings.Split(stderr, "\n")
for _, line := range lines {
if strings.TrimSpace(line) == "" {
continue
}
var record Record
_ = json.Unmarshal([]byte(line), &record)
records = append(records, record)
}
comment, score, err := GetResult(records, conf)
if err != nil {
return stage.ParserResult{
Score: 0,
Comment: fmt.Sprintf(
"Unexpected parser error: %s.",
err,
),
}
}
return stage.ParserResult{
Score: score,
Comment: comment,
}
}
func (*CppCheck) Run(results []stage.ExecutorResult, confAny any) (
[]stage.ParserResult, bool, error,
) {
conf, err := stage.DecodeConf[Conf](confAny)
if err != nil {
return nil, true, err
}
var res []stage.ParserResult
for _, result := range results {
res = append(res, Parse(result, *conf))
}
return res, false, nil
}