* platforms/linux.make: Tell users to read INSTALL.txt not the
[mono.git] / mcs / gmcs / report.cs
1 //
2 // report.cs: report errors and warnings.
3 //
4 // Author: Miguel de Icaza (miguel@ximian.com)
5 //
6 // (C) 2001 Ximian, Inc. (http://www.ximian.com)
7 //
8
9 //
10 // FIXME: currently our class library does not support custom number format strings
11 //
12 using System;
13 using System.Text;
14 using System.Collections;
15 using System.Diagnostics;
16
17 namespace Mono.CSharp {
18
19         /// <summary>
20         ///   This class is used to report errors and warnings t te user.
21         /// </summary>
22         public class Report {
23                 /// <summary>  
24                 ///   Errors encountered so far
25                 /// </summary>
26                 static public int Errors;
27
28                 /// <summary>  
29                 ///   Warnings encountered so far
30                 /// </summary>
31                 static public int Warnings;
32
33                 /// <summary>  
34                 ///   Whether errors should be throw an exception
35                 /// </summary>
36                 static public bool Fatal;
37                 
38                 /// <summary>  
39                 ///   Whether warnings should be considered errors
40                 /// </summary>
41                 static public bool WarningsAreErrors;
42
43                 /// <summary>  
44                 ///   Whether to dump a stack trace on errors. 
45                 /// </summary>
46                 static public bool Stacktrace;
47                 
48                 //
49                 // If the 'expected' error code is reported then the
50                 // compilation succeeds.
51                 //
52                 // Used for the test suite to excercise the error codes
53                 //
54                 static int expected_error = 0;
55
56                 //
57                 // Keeps track of the warnings that we are ignoring
58                 //
59                 static Hashtable warning_ignore_table;
60                 
61                 static void Check (int code)
62                 {
63                         if (code == expected_error){
64                                 if (Fatal)
65                                         throw new Exception ();
66                                 
67                                 Environment.Exit (0);
68                         }
69                 }
70                 
71                 static public void RealError (string msg)
72                 {
73                         Errors++;
74                         Console.WriteLine (msg);
75
76                         if (Stacktrace)
77                                 Console.WriteLine (new StackTrace ().ToString ());
78                         if (Fatal)
79                                 throw new Exception (msg);
80                 }
81
82                 static public void Error (int code, Location l, string text)
83                 {
84                         if (code < 0)
85                                 code = 8000-code;
86                         
87                         string msg = String.Format (
88                                 "{0}({1}) error CS{2:0000}: {3}", l.Name, l.Row, code, text);
89 //                              "{0}({1}) error CS{2}: {3}", l.Name, l.Row, code, text);
90                         
91                         RealError (msg);
92                         Check (code);
93                 }
94
95                 static public void Warning (int code, Location l, string text)
96                 {
97                         if (code < 0)
98                                 code = 8000-code;
99                         
100                         if (warning_ignore_table != null){
101                                 if (warning_ignore_table.Contains (code))
102                                         return;
103                         }
104                         
105                         if (WarningsAreErrors)
106                                 Error (code, l, text);
107                         else {
108                                 string row;
109                                 
110                                 if (Location.IsNull (l))
111                                         row = "";
112                                 else
113                                         row = l.Row.ToString ();
114                                 
115                                 Console.WriteLine (String.Format (
116                                         "{0}({1}) warning CS{2:0000}: {3}",
117 //                                      "{0}({1}) warning CS{2}: {3}",
118                                         l.Name,  row, code, text));
119                                 Warnings++;
120                                 Check (code);
121
122                                 if (Stacktrace)
123                                         Console.WriteLine (new StackTrace ().ToString ());
124                         }
125                 }
126                 
127                 static public void Warning (int code, string text)
128                 {
129                         Warning (code, Location.Null, text);
130                 }
131
132                 static public void Warning (int code, int level, string text)
133                 {
134                         if (RootContext.WarningLevel >= level)
135                                 Warning (code, Location.Null, text);
136                 }
137
138                 static public void Warning (int code, int level, Location l, string text)
139                 {
140                         if (RootContext.WarningLevel >= level)
141                                 Warning (code, l, text);
142                 }
143
144                 static public void Error (int code, string text)
145                 {
146                         if (code < 0)
147                                 code = 8000-code;
148                         
149                         string msg = String.Format ("error CS{0:0000}: {1}", code, text);
150 //                      string msg = String.Format ("error CS{0}: {1}", code, text);
151                         
152                         RealError (msg);
153                         Check (code);
154                 }
155
156                 static public void Error (int code, Location loc, string format, params object[] args)
157                 {
158                         Error (code, loc, String.Format (format, args));
159                 }
160
161                 static public void Error (int code, string format, params object[] args)
162                 {
163                         Error (code, String.Format (format, args));
164                 }
165
166                 static public void Warning (int code, Location loc, string format, params object[] args)
167                 {
168                         Warning (code, loc, String.Format (format, args));
169                 }
170
171                 static public void Warning (int code, string format, params object[] args)
172                 {
173                         Warning (code, String.Format (format, args));
174                 }
175
176                 static public void Message (Message m)
177                 {
178                         if (m is ErrorMessage)
179                                 Error (m.code, m.text);
180                         else
181                                 Warning (m.code, m.text);
182                 }
183
184                 static public void SetIgnoreWarning (int code)
185                 {
186                         if (warning_ignore_table == null)
187                                 warning_ignore_table = new Hashtable ();
188
189                         warning_ignore_table [code] = true;
190                 }
191                 
192                 static public int ExpectedError {
193                         set {
194                                 expected_error = value;
195                         }
196                         get {
197                                 return expected_error;
198                         }
199                 }
200
201                 public static int DebugFlags = 0;
202
203                 [Conditional ("MCS_DEBUG")]
204                 static public void Debug (string message, params object[] args)
205                 {
206                         Debug (4, message, args);
207                 }
208                         
209                 [Conditional ("MCS_DEBUG")]
210                 static public void Debug (int category, string message, params object[] args)
211                 {
212                         if ((category & DebugFlags) == 0)
213                                 return;
214
215                         StringBuilder sb = new StringBuilder (message);
216
217                         if ((args != null) && (args.Length > 0)) {
218                                 sb.Append (": ");
219
220                                 bool first = true;
221                                 foreach (object arg in args) {
222                                         if (first)
223                                                 first = false;
224                                         else
225                                                 sb.Append (", ");
226                                         if (arg == null)
227                                                 sb.Append ("null");
228                                         else if (arg is ICollection)
229                                                 sb.Append (PrintCollection ((ICollection) arg));
230                                         else if (arg is IntPtr)
231                                                 sb.Append (String.Format ("IntPtr(0x{0:x})", ((IntPtr) arg).ToInt32 ()));
232                                         else
233                                                 sb.Append (arg);
234                                 }
235                         }
236
237                         Console.WriteLine (sb.ToString ());
238                 }
239
240                 static public string PrintCollection (ICollection collection)
241                 {
242                         StringBuilder sb = new StringBuilder ();
243
244                         sb.Append (collection.GetType ());
245                         sb.Append ("(");
246
247                         bool first = true;
248                         foreach (object o in collection) {
249                                 if (first)
250                                         first = false;
251                                 else
252                                         sb.Append (", ");
253                                 sb.Append (o);
254                         }
255
256                         sb.Append (")");
257                         return sb.ToString ();
258                 }
259         }
260
261         public class Message {
262                 public int code;
263                 public string text;
264                 
265                 public Message (int code, string text)
266                 {
267                         this.code = code;
268                         this.text = text;
269                 }
270         }
271
272         public class WarningMessage : Message {
273                 public WarningMessage (int code, string text) : base (code, text)
274                 {
275                 }
276         }
277
278         public class ErrorMessage : Message {
279                 public ErrorMessage (int code, string text) : base (code, text)
280                 {
281                 }
282
283                 //
284                 // For compatibility reasons with old code.
285                 //
286                 public static void report_error (string error)
287                 {
288                         Console.Write ("ERROR: ");
289                         Console.WriteLine (error);
290                 }
291         }
292
293         public enum TimerType {
294                 FindMembers     = 0,
295                 TcFindMembers   = 1,
296                 MemberLookup    = 2,
297                 CachedLookup    = 3,
298                 CacheInit       = 4,
299                 MiscTimer       = 5,
300                 CountTimers     = 6
301         }
302
303         public enum CounterType {
304                 FindMembers     = 0,
305                 MemberCache     = 1,
306                 MiscCounter     = 2,
307                 CountCounters   = 3
308         }
309
310         public class Timer
311         {
312                 static DateTime[] timer_start;
313                 static TimeSpan[] timers;
314                 static long[] timer_counters;
315                 static long[] counters;
316
317                 static Timer ()
318                 {
319                         timer_start = new DateTime [(int) TimerType.CountTimers];
320                         timers = new TimeSpan [(int) TimerType.CountTimers];
321                         timer_counters = new long [(int) TimerType.CountTimers];
322                         counters = new long [(int) CounterType.CountCounters];
323
324                         for (int i = 0; i < (int) TimerType.CountTimers; i++) {
325                                 timer_start [i] = DateTime.Now;
326                                 timers [i] = TimeSpan.Zero;
327                         }
328                 }
329
330                 [Conditional("TIMER")]
331                 static public void IncrementCounter (CounterType which)
332                 {
333                         ++counters [(int) which];
334                 }
335
336                 [Conditional("TIMER")]
337                 static public void StartTimer (TimerType which)
338                 {
339                         timer_start [(int) which] = DateTime.Now;
340                 }
341
342                 [Conditional("TIMER")]
343                 static public void StopTimer (TimerType which)
344                 {
345                         timers [(int) which] += DateTime.Now - timer_start [(int) which];
346                         ++timer_counters [(int) which];
347                 }
348
349                 [Conditional("TIMER")]
350                 static public void ShowTimers ()
351                 {
352                         ShowTimer (TimerType.FindMembers, "- FindMembers timer");
353                         ShowTimer (TimerType.TcFindMembers, "- TypeContainer.FindMembers timer");
354                         ShowTimer (TimerType.MemberLookup, "- MemberLookup timer");
355                         ShowTimer (TimerType.CachedLookup, "- CachedLookup timer");
356                         ShowTimer (TimerType.CacheInit, "- Cache init");
357                         ShowTimer (TimerType.MiscTimer, "- Misc timer");
358
359                         ShowCounter (CounterType.FindMembers, "- Find members");
360                         ShowCounter (CounterType.MemberCache, "- Member cache");
361                         ShowCounter (CounterType.MiscCounter, "- Misc counter");
362                 }
363
364                 static public void ShowCounter (CounterType which, string msg)
365                 {
366                         Console.WriteLine ("{0} {1}", counters [(int) which], msg);
367                 }
368
369                 static public void ShowTimer (TimerType which, string msg)
370                 {
371                         Console.WriteLine (
372                                 "[{0:00}:{1:000}] {2} (used {3} times)",
373                                 (int) timers [(int) which].TotalSeconds,
374                                 timers [(int) which].Milliseconds, msg,
375                                 timer_counters [(int) which]);
376                 }
377         }
378
379         public class InternalErrorException : Exception {
380                 public InternalErrorException ()
381                         : base ("Internal error")
382                 {
383                 }
384
385                 public InternalErrorException (string message)
386                         : base (message)
387                 {
388                 }
389
390                 public InternalErrorException (string format, params object[] args)
391                         : this (String.Format (format, args))
392                 { }
393         }
394 }