| 1 | module vbugreport |
| 2 | |
| 3 | import os |
| 4 | |
| 5 | pub struct StoredCErrorReport { |
| 6 | pub: |
| 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`. |
| 19 | pub 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 | |
| 30 | fn normalized_file_name(path string) string { |
| 31 | return os.file_name(path.replace('\\', '/')) |
| 32 | } |
| 33 | |
| 34 | fn 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 | |
| 48 | fn 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 | |