Merge pull request #2664 from lambdageek/dev/monoerror-mono_rand
[mono.git] / mcs / class / Microsoft.Build.Utilities / Microsoft.Build.Utilities / MSBuildErrorParser.cs
1 //
2 // MSBuildErrorParser.cs: Parser for MSBuild-format error messages.
3 //
4 // Author:
5 //   Michael Hutchinson (m.j.hutchinson@gmail.com)
6 //
7 // Copyright 2014 Xamarin Inc. (http://www.xamarin.com)
8 //
9 // Permission is hereby granted, free of charge, to any person obtaining
10 // a copy of this software and associated documentation files (the
11 // "Software"), to deal in the Software without restriction, including
12 // without limitation the rights to use, copy, modify, merge, publish,
13 // distribute, sublicense, and/or sell copies of the Software, and to
14 // permit persons to whom the Software is furnished to do so, subject to
15 // the following conditions:
16 //
17 // The above copyright notice and this permission notice shall be
18 // included in all copies or substantial portions of the Software.
19 //
20 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
21 // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
22 // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
23 // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
24 // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
25 // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
26 // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
27
28 using System;
29
30 namespace Microsoft.Build.Utilities
31 {
32         static class MSBuildErrorParser
33         {
34                 public class Result
35                 {
36                         public string Origin { get; set; }
37                         public int Line { get; set; }
38                         public int Column { get; set; }
39                         public int EndLine { get; set; }
40                         public int EndColumn { get; set; }
41                         public string Subcategory { get; set; }
42                         public bool IsError { get; set; }
43                         public string Code { get; set; }
44                         public string Message { get; set; }
45                 }
46
47                 // Parses single-line error message in the standard MSBuild error format:
48                 //
49                 // [origin[(position)]:][subcategory] category code: [message]
50                 //
51                 // Components in [] square brackets are optional.
52                 // Components are as follows:
53                 //  origin: tool name or filename, may contain whitespace, no colons except the drive letter
54                 //  position: line/col position or range in the file, with one of the following forms:
55                 //      (l), (l,c), (l,c-c), (l,c,l,c)
56                 //  subcategory: arbitrary text, may contain whitepsace
57                 //  code: error code, no whietspace or punctuation
58                 //  message: arbitraty text, no restrictions
59                 //
60                 public static Result TryParseLine (string line)
61                 {
62                         int originEnd, originStart = 0;
63                         var result = new Result ();
64
65                         MoveNextNonSpace (line, ref originStart);
66
67                         if (originStart >= line.Length)
68                                 return null;
69
70                         //find the origin section
71                         //the filename may include a colon for Windows drive e.g. C:\foo, so ignore colon in first 2 chars
72                         if (line[originStart] != ':') {
73                                 if (originStart + 2 >= line.Length)
74                                         return null;
75
76                                 if ((originEnd = line.IndexOf (':', originStart + 2) - 1) < 0)
77                                         return null;
78                         } else {
79                                 originEnd = originStart;
80                         }
81
82                         int categoryStart = originEnd + 2;
83
84                         if (categoryStart > line.Length)
85                                 return null;
86
87                         MovePrevNonSpace (line, ref originEnd);
88
89                         //if there is no origin section, then we can't parse the message
90                         if (originEnd < 0 || originEnd < originStart)
91                                 return null;
92
93                         //find the category section, if there is one
94                         MoveNextNonSpace (line, ref categoryStart);
95
96                         int categoryEnd = line.IndexOf (':', categoryStart) - 1;
97                         int messageStart = categoryEnd + 2;
98
99                         if (categoryEnd >= 0) {
100                                 MovePrevNonSpace (line, ref categoryEnd);
101                                 if (categoryEnd <= categoryStart)
102                                         categoryEnd = -1;
103                         }
104
105                         //if there is a category section and it parses
106                         if (categoryEnd > 0 && ParseCategory (line, categoryStart, categoryEnd, result)) {
107                                 //then parse the origin section
108                                 if (originEnd > originStart && !ParseOrigin (line, originStart, originEnd, result))
109                                         return null;
110                         } else {
111                                 //there is no origin, parse the origin section as if it were the category
112                                 if (!ParseCategory (line, originStart, originEnd, result))
113                                         return null;
114                                 messageStart = categoryStart;
115                         }
116
117                         //read the remaining message
118                         MoveNextNonSpace (line, ref messageStart);
119                         int messageEnd = line.Length - 1;
120                         MovePrevNonSpace (line, ref messageEnd, messageStart);
121                         if (messageEnd > messageStart) {
122                                 result.Message = line.Substring (messageStart, messageEnd - messageStart + 1);
123                         } else {
124                                 result.Message = "";
125                         }
126
127                         return result;
128                 }
129
130                 // filename (line,col) | tool :
131                 static bool ParseOrigin (string line, int start, int end, Result result)
132                 {
133                         // no line/col
134                         if (line [end] != ')') {
135                                 result.Origin = line.Substring (start, end - start + 1);
136                                 return true;
137                         }
138
139                         //scan back for matching (, assuming at least one char between them
140                         int posStart = line.LastIndexOf ('(', end - 2, end - start - 2);
141                         if (posStart < 0)
142                                 return false;
143
144                         if (!ParsePosition (line, posStart + 1, end, result)) {
145                                 result.Origin = line.Substring (start, end - start + 1);
146                                 return true;
147                         }
148
149                         end = posStart - 1;
150                         MovePrevNonSpace (line, ref end, start);
151
152                         result.Origin = line.Substring (start, end - start + 1);
153                         return true;
154                 }
155
156                 static bool ParseLineColVal (string str, out int val)
157                 {
158                         try {
159                                 val = int.Parse (str);
160                                 return true;
161                         } catch (OverflowException) {
162                                 val = 0;
163                                 return true;
164                         } catch (FormatException) {
165                                 val = 0;
166                                 return false;
167                         }
168                 }
169
170                 // Supported combos:
171                 //
172                 // (SL,SC,EL,EC)
173                 // (SL,SC-EC)
174                 // (SL-EL)
175                 // (SL,SC)
176                 // (SL)
177                 //
178                 // Unexpected patterns of commas/dashes abort parsing, discarding all values.
179                 // Any other characters abort parsing and the (...) gets treated as pert of the filename.
180                 // Overflows are silently treated as zeroes.
181                 //
182                 static bool ParsePosition (string str, int start, int end, Result result)
183                 {
184                         int line = 0, col = 0, endLine = 0, endCol = 0;
185
186                         var a = str.Substring (start, end - start).Split (',');
187
188                         if (a.Length > 4 || a.Length == 3)
189                                 return true;
190
191                         if (a.Length == 4) {
192                                 bool valid =
193                                         ParseLineColVal (a [0], out line) &&
194                                         ParseLineColVal (a [1], out col) &&
195                                         ParseLineColVal (a [2], out endLine) &&
196                                         ParseLineColVal (a [3], out endCol);
197                                 if (!valid)
198                                         return false;
199                         } else {
200                                 var b = a [0].Split ('-');
201                                 if (b.Length > 2)
202                                         return true;
203                                 if (!ParseLineColVal (b [0], out line))
204                                         return false;
205                                 if (b.Length == 2) {
206                                         if (a.Length == 2)
207                                                 return true;
208                                         if (!ParseLineColVal (b [1], out endLine))
209                                                 return false;
210                                 }
211                                 if (a.Length == 2) {
212                                         var c = a [1].Split ('-');
213                                         if (c.Length > 2)
214                                                 return true;
215                                         if (!ParseLineColVal (c [0], out col))
216                                                 return false;
217                                         if (c.Length == 2) {
218                                                 if (!ParseLineColVal (c [1], out endCol))
219                                                         return false;
220                                         }
221                                 }
222                         }
223
224                         result.Line = line;
225                         result.Column = col;
226                         result.EndLine = endLine;
227                         result.EndColumn = endCol;
228                         return true;
229                 }
230
231                 static bool ParseCategory (string line, int start, int end, Result result)
232                 {
233                         int idx = end;
234                         MovePrevWordStart (line, ref idx, start);
235                         if (idx < start + 1)
236                                 return false;
237
238                         string code = line.Substring (idx, end - idx + 1);
239
240                         idx--;
241                         MovePrevNonSpace (line, ref idx, start);
242                         end = idx;
243                         MovePrevWordStart (line, ref idx, start);
244                         if (idx < start)
245                                 return false;
246
247                         string category = line.Substring (idx , end - idx + 1);
248                         if (string.Equals (category, "error", StringComparison.OrdinalIgnoreCase))
249                                 result.IsError = true;
250                         else if (!string.Equals (category, "warning", StringComparison.OrdinalIgnoreCase))
251                                 return false;
252
253                         result.Code = code;
254
255                         idx--;
256                         if (idx > start) {
257                                 MovePrevNonSpace (line, ref idx, start);
258                                 result.Subcategory = line.Substring (start, idx - start + 1);
259                         } else {
260                                 result.Subcategory = "";
261                         }
262
263                         return true;
264                 }
265
266                 static void MoveNextNonSpace (string s, ref int idx)
267                 {
268                         while (idx < s.Length && char.IsWhiteSpace (s[idx]))
269                                 idx++;
270                 }
271
272                 static void MovePrevNonSpace (string s, ref int idx, int min = 0)
273                 {
274                         while (idx > min && char.IsWhiteSpace (s[idx]))
275                                 idx--;
276                 }
277
278                 static void MovePrevWordStart (string s, ref int idx, int min = 0)
279                 {
280                         while (idx > min && char.IsLetterOrDigit (s[idx - 1]))
281                                 idx--;
282                 }
283         }
284 }