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
|
//
// MSBuildErrorParser.cs: Parser for MSBuild-format error messages.
//
// Author:
// Michael Hutchinson (m.j.hutchinson@gmail.com)
//
// Copyright 2014 Xamarin Inc. (http://www.xamarin.com)
//
// Permission is hereby granted, free of charge, to any person obtaining
// a copy of this software and associated documentation files (the
// "Software"), to deal in the Software without restriction, including
// without limitation the rights to use, copy, modify, merge, publish,
// distribute, sublicense, and/or sell copies of the Software, and to
// permit persons to whom the Software is furnished to do so, subject to
// the following conditions:
//
// The above copyright notice and this permission notice shall be
// included in all copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
// EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
// MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
// NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
// LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
// OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
// WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
using System;
namespace MonoDevelop.Projects
{
//copied from mcs/class/Microsoft.Build.Utilities/Microsoft.Build.Utilities/MSBuildErrorParser.cs
static class MSBuildErrorParser
{
public class Result
{
public string Origin { get; set; }
public int Line { get; set; }
public int Column { get; set; }
public int EndLine { get; set; }
public int EndColumn { get; set; }
public string Subcategory { get; set; }
public bool IsError { get; set; }
public string Code { get; set; }
public string Message { get; set; }
public string HelpKeyword { get; set; }
}
// Parses single-line error message in the standard MSBuild error format:
//
// [origin[(position)]:][subcategory] category code: [message]
//
// Components in [] square brackets are optional.
// Components are as follows:
// origin: tool name or filename, may contain whitespace, no colons except the drive letter
// position: line/col position or range in the file, with one of the following forms:
// (l), (l,c), (l,c-c), (l,c,l,c)
// subcategory: arbitrary text, may contain whitepsace
// code: error code, no whietspace or punctuation
// message: arbitraty text, no restrictions
//
public static Result TryParseLine (string line)
{
int originEnd, originStart = 0;
var result = new Result ();
MoveNextNonSpace (line, ref originStart);
if (originStart >= line.Length)
return null;
//find the origin section
//the filename may include a colon for Windows drive e.g. C:\foo, so ignore colon in first 2 chars
if (line[originStart] != ':') {
if (originStart + 2 >= line.Length)
return null;
if ((originEnd = line.IndexOf (':', originStart + 2) - 1) < 0)
return null;
} else {
originEnd = originStart;
}
int categoryStart = originEnd + 2;
if (categoryStart > line.Length)
return null;
MovePrevNonSpace (line, ref originEnd);
//if there is no origin section, then we can't parse the message
if (originEnd < 0 || originEnd < originStart)
return null;
//find the category section, if there is one
MoveNextNonSpace (line, ref categoryStart);
int categoryEnd = line.IndexOf (':', categoryStart) - 1;
int messageStart = categoryEnd + 2;
if (categoryEnd >= 0) {
MovePrevNonSpace (line, ref categoryEnd);
if (categoryEnd <= categoryStart)
categoryEnd = -1;
}
//if there is a category section and it parses
if (categoryEnd > 0 && ParseCategory (line, categoryStart, categoryEnd, result)) {
//then parse the origin section
if (originEnd > originStart && !ParseOrigin (line, originStart, originEnd, result))
return null;
} else {
//there is no origin, parse the origin section as if it were the category
if (!ParseCategory (line, originStart, originEnd, result))
return null;
messageStart = categoryStart;
}
//read the remaining message
MoveNextNonSpace (line, ref messageStart);
int messageEnd = line.Length - 1;
MovePrevNonSpace (line, ref messageEnd, messageStart);
if (messageEnd > messageStart) {
result.Message = line.Substring (messageStart, messageEnd - messageStart + 1);
} else {
result.Message = "";
}
return result;
}
// filename (line,col) | tool :
static bool ParseOrigin (string line, int start, int end, Result result)
{
// no line/col
if (line [end] != ')') {
result.Origin = line.Substring (start, end - start + 1);
return true;
}
//scan back for matching (, assuming at least one char between them
int posStart = line.LastIndexOf ('(', end - 2, end - start - 2);
if (posStart < 0)
return false;
if (!ParsePosition (line, posStart + 1, end, result)) {
result.Origin = line.Substring (start, end - start + 1);
return true;
}
end = posStart - 1;
MovePrevNonSpace (line, ref end, start);
result.Origin = line.Substring (start, end - start + 1);
return true;
}
static bool ParseLineColVal (string str, out int val)
{
try {
val = int.Parse (str);
return true;
} catch (OverflowException) {
val = 0;
return true;
} catch (FormatException) {
val = 0;
return false;
}
}
// Supported combos:
//
// (SL,SC,EL,EC)
// (SL,SC-EC)
// (SL-EL)
// (SL,SC)
// (SL)
//
// Unexpected patterns of commas/dashes abort parsing, discarding all values.
// Any other characters abort parsing and the (...) gets treated as pert of the filename.
// Overflows are silently treated as zeroes.
//
static bool ParsePosition (string str, int start, int end, Result result)
{
int line = 0, col = 0, endLine = 0, endCol = 0;
var a = str.Substring (start, end - start).Split (',');
if (a.Length > 4 || a.Length == 3)
return true;
if (a.Length == 4) {
bool valid =
ParseLineColVal (a [0], out line) &&
ParseLineColVal (a [1], out col) &&
ParseLineColVal (a [2], out endLine) &&
ParseLineColVal (a [3], out endCol);
if (!valid)
return false;
} else {
var b = a [0].Split ('-');
if (b.Length > 2)
return true;
if (!ParseLineColVal (b [0], out line))
return false;
if (b.Length == 2) {
if (a.Length == 2)
return true;
if (!ParseLineColVal (b [1], out endLine))
return false;
}
if (a.Length == 2) {
var c = a [1].Split ('-');
if (c.Length > 2)
return true;
if (!ParseLineColVal (c [0], out col))
return false;
if (c.Length == 2) {
if (!ParseLineColVal (c [1], out endCol))
return false;
}
}
}
result.Line = line;
result.Column = col;
result.EndLine = endLine;
result.EndColumn = endCol;
return true;
}
static bool ParseCategory (string line, int start, int end, Result result)
{
int idx = end;
MovePrevWordStart (line, ref idx, start);
if (idx < start + 1)
return false;
string code = line.Substring (idx, end - idx + 1);
idx--;
MovePrevNonSpace (line, ref idx, start);
end = idx;
MovePrevWordStart (line, ref idx, start);
if (idx < start)
return false;
string category = line.Substring (idx , end - idx + 1);
if (string.Equals (category, "error", StringComparison.OrdinalIgnoreCase))
result.IsError = true;
else if (!string.Equals (category, "warning", StringComparison.OrdinalIgnoreCase))
return false;
result.Code = code;
idx--;
if (idx > start) {
MovePrevNonSpace (line, ref idx, start);
result.Subcategory = line.Substring (start, idx - start + 1);
} else {
result.Subcategory = "";
}
return true;
}
static void MoveNextNonSpace (string s, ref int idx)
{
while (idx < s.Length && char.IsWhiteSpace (s[idx]))
idx++;
}
static void MovePrevNonSpace (string s, ref int idx, int min = 0)
{
while (idx > min && char.IsWhiteSpace (s[idx]))
idx--;
}
static void MovePrevWordStart (string s, ref int idx, int min = 0)
{
while (idx > min && char.IsLetterOrDigit (s[idx - 1]))
idx--;
}
}
}
|