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