v / cmd / tools / modules / vbugreport / c_error_storage.v
70 lines · 64 sloc · 1.86 KB · a72a784ee3b66252e3f32b7bb52815e9dda96d67
Raw
1module vbugreport
2
3import os
4
5pub struct StoredCErrorReport {
6pub:
7 c_file_name string
8 target_os string
9 ccompiler string
10 error_string string
11 lines string
12 v_lines string
13}
14
15// new_stored_c_error_report builds the fields stored by the C error report receiver.
16// The generated C context is stored in `lines`, while the corresponding V source
17// context (the V line that caused the C error and its surrounding lines) is stored
18// separately in `v_lines`.
19pub fn new_stored_c_error_report(c_file string, target_os string, ccompiler string, c_error string, c_lines []string, v_lines []string) StoredCErrorReport {
20 return StoredCErrorReport{
21 c_file_name: normalized_file_name(c_file)
22 target_os: target_os
23 ccompiler: ccompiler
24 error_string: c_error_string(c_error)
25 lines: c_lines.join('\n')
26 v_lines: v_lines.join('\n')
27 }
28}
29
30fn normalized_file_name(path string) string {
31 return os.file_name(path.replace('\\', '/'))
32}
33
34fn c_error_string(c_output string) string {
35 for line in c_output.split_into_lines() {
36 trimmed := line.trim_space()
37 lower := trimmed.to_lower_ascii()
38 if lower.starts_with('warning:') {
39 continue
40 }
41 if error_string := normalized_error_string(trimmed, lower) {
42 return error_string
43 }
44 }
45 return ''
46}
47
48fn normalized_error_string(trimmed string, lower string) ?string {
49 if lower.starts_with('fatal error:') || lower.starts_with('fatal error ') {
50 return 'error: ${trimmed}'
51 }
52 if lower.starts_with('error:') {
53 return trimmed
54 }
55 if lower.starts_with('error ') {
56 return 'error: ${trimmed['error '.len..]}'
57 }
58 for needle in [' fatal error:', ' fatal error '] {
59 if idx := lower.index(needle) {
60 return 'error: ${trimmed[idx + 1..]}'
61 }
62 }
63 if idx := lower.index(' error:') {
64 return trimmed[idx + 1..]
65 }
66 if idx := lower.index(' error ') {
67 return 'error: ${trimmed[idx + ' error '.len..]}'
68 }
69 return none
70}
71