|
15 | 15 |
|
16 | 16 | package diagnostics
|
17 | 17 |
|
| 18 | +import ( |
| 19 | + "fmt" |
| 20 | + "strings" |
| 21 | + |
| 22 | + rpc "github.com/arduino/arduino-cli/rpc/cc/arduino/cli/commands/v1" |
| 23 | +) |
| 24 | + |
18 | 25 | type CompilerOutputParserCB func(cmdline []string, out []byte)
|
| 26 | + |
| 27 | +// Diagnostics represents a list of diagnostics |
| 28 | +type Diagnostics []*Diagnostic |
| 29 | + |
| 30 | +// Diagnostic represents a diagnostic (a compiler error, warning, note, etc.) |
| 31 | +type Diagnostic struct { |
| 32 | + Severity Severity `json:"severity,omitempty"` |
| 33 | + Message string `json:"message"` |
| 34 | + File string `json:"file,omitempty"` |
| 35 | + Line int `json:"line,omitempty"` |
| 36 | + Column int `json:"col,omitempty"` |
| 37 | + Context FullContext `json:"context,omitempty"` |
| 38 | + Suggestions Notes `json:"suggestions,omitempty"` |
| 39 | +} |
| 40 | + |
| 41 | +// Severity is a diagnostic severity |
| 42 | +type Severity string |
| 43 | + |
| 44 | +const ( |
| 45 | + // SeverityUnspecified is the undefined severity |
| 46 | + SeverityUnspecified Severity = "" |
| 47 | + // SeverityWarning is a warning |
| 48 | + SeverityWarning = "WARNING" |
| 49 | + // SeverityError is an error |
| 50 | + SeverityError = "ERROR" |
| 51 | + // SeverityFatal is a fatal error |
| 52 | + SeverityFatal = "FATAL" |
| 53 | +) |
| 54 | + |
| 55 | +// Notes represents a list of Note |
| 56 | +type Notes []*Note |
| 57 | + |
| 58 | +// Note represents a compiler annotation or suggestion |
| 59 | +type Note struct { |
| 60 | + Message string `json:"message"` |
| 61 | + File string `json:"file,omitempty"` |
| 62 | + Line int `json:"line,omitempty"` |
| 63 | + Column int `json:"col,omitempty"` |
| 64 | +} |
| 65 | + |
| 66 | +// FullContext represents a list of Context |
| 67 | +type FullContext []*Context |
| 68 | + |
| 69 | +// Context represents a context, i.e. a reference to a file, line and column |
| 70 | +// or a part of the code that a Diagnostic refers to. |
| 71 | +type Context struct { |
| 72 | + Message string `json:"message"` |
| 73 | + File string `json:"file,omitempty"` |
| 74 | + Line int `json:"line,omitempty"` |
| 75 | + Column int `json:"col,omitempty"` |
| 76 | +} |
| 77 | + |
| 78 | +// ParseCompilerOutput parses the output of a compiler and returns a list of |
| 79 | +// diagnostics. |
| 80 | +func ParseCompilerOutput(compiler *DetectedCompiler, out []byte) ([]*Diagnostic, error) { |
| 81 | + lines := splitLines(out) |
| 82 | + switch compiler.Family { |
| 83 | + case "gcc": |
| 84 | + return parseGccOutput(lines) |
| 85 | + default: |
| 86 | + return nil, fmt.Errorf("unsupported compiler: %s", compiler) |
| 87 | + } |
| 88 | +} |
| 89 | + |
| 90 | +func splitLines(in []byte) []string { |
| 91 | + res := strings.Split(string(in), "\n") |
| 92 | + for i, line := range res { |
| 93 | + res[i] = strings.TrimSuffix(line, "\r") |
| 94 | + } |
| 95 | + if l := len(res) - 1; res[l] == "" { |
| 96 | + res = res[:l] |
| 97 | + } |
| 98 | + return res |
| 99 | +} |
| 100 | + |
| 101 | +// ToRPC converts a Diagnostics to a slice of rpc.CompileDiagnostic |
| 102 | +func (d Diagnostics) ToRPC() []*rpc.CompileDiagnostic { |
| 103 | + if len(d) == 0 { |
| 104 | + return nil |
| 105 | + } |
| 106 | + var res []*rpc.CompileDiagnostic |
| 107 | + for _, diag := range d { |
| 108 | + res = append(res, diag.ToRPC()) |
| 109 | + } |
| 110 | + return res |
| 111 | +} |
| 112 | + |
| 113 | +// ToRPC converts a Diagnostic to a rpc.CompileDiagnostic |
| 114 | +func (d *Diagnostic) ToRPC() *rpc.CompileDiagnostic { |
| 115 | + if d == nil { |
| 116 | + return nil |
| 117 | + } |
| 118 | + return &rpc.CompileDiagnostic{ |
| 119 | + Severity: string(d.Severity), |
| 120 | + Message: d.Message, |
| 121 | + File: d.File, |
| 122 | + Line: int64(d.Line), |
| 123 | + Column: int64(d.Column), |
| 124 | + Context: d.Context.ToRPC(), |
| 125 | + Notes: d.Suggestions.ToRPC(), |
| 126 | + } |
| 127 | +} |
| 128 | + |
| 129 | +// ToRPC converts a Notes to a slice of rpc.CompileDiagnosticNote |
| 130 | +func (s Notes) ToRPC() []*rpc.CompileDiagnosticNote { |
| 131 | + var res []*rpc.CompileDiagnosticNote |
| 132 | + for _, suggestion := range s { |
| 133 | + res = append(res, suggestion.ToRPC()) |
| 134 | + } |
| 135 | + return res |
| 136 | +} |
| 137 | + |
| 138 | +// ToRPC converts a Note to a rpc.CompileDiagnosticNote |
| 139 | +func (s *Note) ToRPC() *rpc.CompileDiagnosticNote { |
| 140 | + if s == nil { |
| 141 | + return nil |
| 142 | + } |
| 143 | + return &rpc.CompileDiagnosticNote{ |
| 144 | + File: s.File, |
| 145 | + Line: int64(s.Line), |
| 146 | + Column: int64(s.Column), |
| 147 | + Message: s.Message, |
| 148 | + } |
| 149 | +} |
| 150 | + |
| 151 | +// ToRPC converts a FullContext to a slice of rpc.CompileDiagnosticContext |
| 152 | +func (t FullContext) ToRPC() []*rpc.CompileDiagnosticContext { |
| 153 | + var res []*rpc.CompileDiagnosticContext |
| 154 | + for _, trace := range t { |
| 155 | + res = append(res, trace.ToRPC()) |
| 156 | + } |
| 157 | + return res |
| 158 | +} |
| 159 | + |
| 160 | +// ToRPC converts a Context to a rpc.CompileDiagnosticContext |
| 161 | +func (d *Context) ToRPC() *rpc.CompileDiagnosticContext { |
| 162 | + if d == nil { |
| 163 | + return nil |
| 164 | + } |
| 165 | + return &rpc.CompileDiagnosticContext{ |
| 166 | + File: d.File, |
| 167 | + Line: int64(d.Line), |
| 168 | + Column: int64(d.Column), |
| 169 | + Message: d.Message, |
| 170 | + } |
| 171 | +} |
0 commit comments