-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathparser.go
More file actions
400 lines (344 loc) · 9.45 KB
/
parser.go
File metadata and controls
400 lines (344 loc) · 9.45 KB
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
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
package main
import (
"regexp"
"strings"
)
// DiagnosticLevel represents the severity of a diagnostic
type DiagnosticLevel string
const (
LevelError DiagnosticLevel = "error"
LevelWarning DiagnosticLevel = "warning"
LevelNote DiagnosticLevel = "note"
)
// Diagnostic represents a single diagnostic message from clang-tidy or sanitizers
type Diagnostic struct {
File string
Line int
Column int
Level DiagnosticLevel
Message string
Check string // clang-tidy check name (e.g., "bugprone-unused-return-value")
Context string // Additional context lines
}
// ParseClangTidyOutput parses clang-tidy output into structured diagnostics
func ParseClangTidyOutput(output string) []Diagnostic {
var diagnostics []Diagnostic
// Pattern: /src/code.cpp:10:5: warning: some message [check-name]
re := regexp.MustCompile(`(?m)^([^:]+):(\d+):(\d+): (error|warning|note): (.+?)(?:\s+\[([^\]]+)\])?$`)
matches := re.FindAllStringSubmatch(output, -1)
for _, match := range matches {
if len(match) >= 6 {
line := 0
col := 0
parseIntSafe(match[2], &line)
parseIntSafe(match[3], &col)
d := Diagnostic{
File: match[1],
Line: line,
Column: col,
Level: DiagnosticLevel(match[4]),
Message: match[5],
}
if len(match) >= 7 && match[6] != "" {
d.Check = match[6]
}
diagnostics = append(diagnostics, d)
}
}
return diagnostics
}
// ParseCppcheckOutput parses cppcheck output into structured diagnostics
func ParseCppcheckOutput(output string) []Diagnostic {
var diagnostics []Diagnostic
// cppcheck patterns:
// [/src/code.cpp:10]: (error) Message text
// /src/code.cpp:10:5: error: Message text [errorId]
re := regexp.MustCompile(`(?m)^(?:\[)?([^:\]]+):(\d+)(?::(\d+))?(?:\])?: \((error|warning|style|performance|portability|information)\) (.+)$`)
re2 := regexp.MustCompile(`(?m)^([^:]+):(\d+):(\d+): (error|warning|note): (.+?) \[([^\]]+)\]$`)
// Try standard format first
matches := re.FindAllStringSubmatch(output, -1)
for _, match := range matches {
if len(match) >= 6 {
line := 0
col := 0
parseIntSafe(match[2], &line)
if len(match) >= 4 && match[3] != "" {
parseIntSafe(match[3], &col)
}
level := LevelWarning
if match[4] == "error" {
level = LevelError
}
diagnostics = append(diagnostics, Diagnostic{
File: match[1],
Line: line,
Column: col,
Level: level,
Message: match[5],
Check: "cppcheck-" + match[4],
})
}
}
// Try GCC-style format
if len(diagnostics) == 0 {
matches = re2.FindAllStringSubmatch(output, -1)
for _, match := range matches {
if len(match) >= 7 {
line := 0
col := 0
parseIntSafe(match[2], &line)
parseIntSafe(match[3], &col)
level := LevelWarning
if match[4] == "error" {
level = LevelError
} else if match[4] == "note" {
level = LevelNote
}
diagnostics = append(diagnostics, Diagnostic{
File: match[1],
Line: line,
Column: col,
Level: level,
Message: match[5],
Check: match[6],
})
}
}
}
return diagnostics
}
// ParseSanitizerOutput parses ASAN/UBSAN/TSAN output into structured diagnostics
func ParseSanitizerOutput(output string, sanitizerType string) []Diagnostic {
var diagnostics []Diagnostic
// ASAN pattern: ==PID==ERROR: AddressSanitizer: heap-buffer-overflow
// UBSAN pattern: code.cpp:10:5: runtime error: ...
// TSAN pattern: WARNING: ThreadSanitizer: data race
lines := strings.Split(output, "\n")
var currentDiag *Diagnostic
for i, line := range lines {
line = strings.TrimSpace(line)
if line == "" {
continue
}
// ASAN/MSAN/TSAN summary line
if strings.Contains(line, "ERROR: AddressSanitizer:") ||
strings.Contains(line, "ERROR: LeakSanitizer:") ||
strings.Contains(line, "WARNING: MemorySanitizer:") ||
strings.Contains(line, "WARNING: ThreadSanitizer:") {
if currentDiag != nil {
diagnostics = append(diagnostics, *currentDiag)
}
currentDiag = &Diagnostic{
Level: LevelError,
Message: extractSanitizerMessage(line),
Check: sanitizerType,
}
continue
}
// UBSAN runtime error pattern
if strings.Contains(line, "runtime error:") {
if currentDiag != nil {
diagnostics = append(diagnostics, *currentDiag)
}
d := parseUBSANLine(line)
d.Check = "ubsan"
currentDiag = &d
continue
}
// Stack trace location: #0 0x... in func /path/file.cpp:10
if strings.HasPrefix(line, "#") && currentDiag != nil {
if loc := extractStackLocation(line); loc != "" {
if currentDiag.Context == "" {
currentDiag.Context = loc
} else {
currentDiag.Context += "\n" + loc
}
}
}
// Limit context to avoid huge outputs
if currentDiag != nil && i > 0 && len(currentDiag.Context) < 500 {
if strings.HasPrefix(line, " ") || strings.HasPrefix(line, "\t") {
// Code snippet or additional context
if currentDiag.Context != "" {
currentDiag.Context += "\n"
}
currentDiag.Context += line
}
}
}
if currentDiag != nil {
diagnostics = append(diagnostics, *currentDiag)
}
return diagnostics
}
// FormatDiagnosticsForLLM formats diagnostics in a compact format for LLM processing
// No colors, minimal tokens, maximum clarity
func FormatDiagnosticsForLLM(diagnostics []Diagnostic) string {
if len(diagnostics) == 0 {
return ""
}
var sb strings.Builder
for _, d := range diagnostics {
// Compact format: file:line check-name: message
// Example: code.cpp:15 modernize-use-nullptr: use nullptr instead of NULL
// File and line
if d.File != "" {
// Strip /src/ prefix for cleaner output
file := strings.TrimPrefix(d.File, "/src/")
sb.WriteString(file)
if d.Line > 0 {
sb.WriteString(":")
sb.WriteString(intToStr(d.Line))
}
sb.WriteString(" ")
}
// Check name (if available)
if d.Check != "" {
sb.WriteString(d.Check)
sb.WriteString(": ")
} else {
sb.WriteString(string(d.Level))
sb.WriteString(": ")
}
// Message
sb.WriteString(d.Message)
sb.WriteString("\n")
// Include context (stack traces, code snippets) if available
if d.Context != "" {
// Indent context for clarity
contextLines := strings.Split(d.Context, "\n")
for _, line := range contextLines {
if line != "" {
sb.WriteString(" ")
sb.WriteString(line)
sb.WriteString("\n")
}
}
}
}
return sb.String()
}
// FormatDiagnostics formats diagnostics for user display
func FormatDiagnostics(diagnostics []Diagnostic) string {
if len(diagnostics) == 0 {
return ""
}
var sb strings.Builder
for _, d := range diagnostics {
// Format: [error/warning] message (check-name)
// at file:line:col
// context...
levelColor := "\033[91m" // red for error
if d.Level == LevelWarning {
levelColor = "\033[93m" // yellow for warning
} else if d.Level == LevelNote {
levelColor = "\033[94m" // blue for note
}
sb.WriteString(levelColor)
sb.WriteString(string(d.Level))
sb.WriteString("\033[0m: ")
sb.WriteString(d.Message)
if d.Check != "" {
sb.WriteString(" \033[90m[")
sb.WriteString(d.Check)
sb.WriteString("]\033[0m")
}
sb.WriteString("\n")
if d.File != "" {
sb.WriteString(" at ")
sb.WriteString(d.File)
if d.Line > 0 {
sb.WriteString(":")
sb.WriteString(intToStr(d.Line))
if d.Column > 0 {
sb.WriteString(":")
sb.WriteString(intToStr(d.Column))
}
}
sb.WriteString("\n")
}
if d.Context != "" {
// Indent context lines
contextLines := strings.Split(d.Context, "\n")
for _, cl := range contextLines {
if cl != "" {
sb.WriteString(" ")
sb.WriteString(cl)
sb.WriteString("\n")
}
}
}
sb.WriteString("\n")
}
return sb.String()
}
// Helper functions
func parseIntSafe(s string, out *int) {
val := 0
for _, c := range s {
if c >= '0' && c <= '9' {
val = val*10 + int(c-'0')
} else {
return
}
}
*out = val
}
func intToStr(n int) string {
if n == 0 {
return "0"
}
var digits []byte
for n > 0 {
digits = append([]byte{byte('0' + n%10)}, digits...)
n /= 10
}
return string(digits)
}
func extractSanitizerMessage(line string) string {
// Extract the error type after "ERROR: AddressSanitizer: " or similar
patterns := []string{
"ERROR: AddressSanitizer: ",
"ERROR: LeakSanitizer: ",
"WARNING: MemorySanitizer: ",
"WARNING: ThreadSanitizer: ",
}
for _, prefix := range patterns {
if idx := strings.Index(line, prefix); idx >= 0 {
msg := line[idx+len(prefix):]
// Trim trailing location info
if endIdx := strings.Index(msg, " on address"); endIdx > 0 {
msg = msg[:endIdx]
}
return strings.TrimSpace(msg)
}
}
return line
}
func parseUBSANLine(line string) Diagnostic {
d := Diagnostic{Level: LevelError}
// Pattern: /path/file.cpp:10:5: runtime error: message
re := regexp.MustCompile(`^([^:]+):(\d+):(\d+): runtime error: (.+)$`)
if match := re.FindStringSubmatch(line); len(match) >= 5 {
d.File = match[1]
parseIntSafe(match[2], &d.Line)
parseIntSafe(match[3], &d.Column)
d.Message = match[4]
} else {
// Fallback: just extract message after "runtime error:"
if idx := strings.Index(line, "runtime error:"); idx >= 0 {
d.Message = strings.TrimSpace(line[idx+14:])
} else {
d.Message = line
}
}
return d
}
func extractStackLocation(line string) string {
// Pattern: #0 0x... in func_name /path/file.cpp:10
re := regexp.MustCompile(`#\d+\s+\S+\s+in\s+(\S+)\s+([^:]+):(\d+)`)
if match := re.FindStringSubmatch(line); len(match) >= 4 {
return match[1] + " at " + match[2] + ":" + match[3]
}
return ""
}