diff --git a/internal/parser/clangtidy/convert.go b/internal/parser/clangtidy/convert.go index c6ea7c3..57ea330 100644 --- a/internal/parser/clangtidy/convert.go +++ b/internal/parser/clangtidy/convert.go @@ -77,7 +77,7 @@ func isIgnored(line string) bool { } func parseMessage(line string) ClangMessage { - messageRegex := regexp.MustCompile(`^(?P.+):(?P\d+):(?P\d+): (?P\S+): (?P.*?)(?: \[(?P.*)\])?\n$`) + messageRegex := regexp.MustCompile(`^(?P.+):(?P\d+):(?P\d+): (?P\S+): (?P.*?) \[(?P[^\]]+)\]?\n$`) regexRes := messageRegex.FindStringSubmatch(line) if len(regexRes) == 0 { return *newClangMessage("", 0, 0, UNKNOWN, "", "", nil, nil) @@ -138,7 +138,7 @@ func ParseLines(lines []string, conf Conf) []ClangMessage { message := parseMessage(string(line)) if message.level == UNKNOWN && len(messages) > 0 { messages[len(messages)-1].detailsLines = append(messages[len(messages)-1].detailsLines, string(line)) - } else { + } else if message.level != UNKNOWN { messages = append(messages, message) } } diff --git a/internal/parser/clangtidy/score.go b/internal/parser/clangtidy/score.go index f1027d5..4a92787 100644 --- a/internal/parser/clangtidy/score.go +++ b/internal/parser/clangtidy/score.go @@ -2,60 +2,49 @@ package clangtidy import ( "fmt" + "sort" "strings" ) func GetResult(jsonMessages []JsonMessage, conf Conf) (int, string) { score := conf.Score comment := "### Test results summary\n\n" - keys := [...]string{ - "codequality-unchecked-malloc-result", - "codequality-no-global-variables", - "codequality-no-header-guard", - "codequality-no-fflush-stdin", - "readability-function-size", - "readability-duplicate-include", - "readability-identifier-naming", - "readability-redundant", - "readability-misleading-indentation", - "readability-misplaced-array-index", - "cppcoreguidelines-init-variables", - "bugprone-suspicious-string-compare", - "google-global-names-in-headers", - "clang-diagnostic", - "clang-analyzer", - "misc", - "performance", - "others", - } - mapping := map[string]int{} - for _, key := range keys { - mapping[key] = 0 - } + categoryCount := map[string]int{} for _, jsonMessage := range jsonMessages { + // checkName is commas separated string here checkName := jsonMessage.CheckName for _, match := range conf.Matches { for _, keyword := range match.Keywords { - // TODO: The keyword in json report might also be an array, need to split it if strings.Contains(checkName, keyword) { score -= match.Score } } } - listed := false - for key := range mapping { - if strings.Contains(checkName, key) { - mapping[key] += 1 - listed = true + checkNames := strings.Split(checkName, ",") + for _, checkName := range checkNames { + parts := strings.Split(checkName, "-") + if len(parts) > 0 { + category := parts[0] + categoryCount[category] += 1 } } - if !listed { - mapping["others"] += 1 - } } - - for i, key := range keys { - comment = fmt.Sprintf("%s%d. %s: %d\n", comment, i+1, key, mapping[key]) + type kv struct { + Key string + Value int + } + var ss []kv + for k, v := range categoryCount { + ss = append(ss, kv{k, v}) + } + sort.Slice(ss, func(i, j int) bool { + if ss[i].Value == ss[j].Value { + return ss[i].Key < ss[j].Key + } + return ss[i].Value > ss[j].Value + }) + for i, kv := range ss { + comment += fmt.Sprintf("%d. %s: %d\n", i+1, kv.Key, kv.Value) } return score, comment }