Bug fixes and a couple of optimizations (used a nice profiler to find
[mono.git] / mcs / mcs / 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.Collections;
14 using System.Diagnostics;
15
16 namespace Mono.CSharp {
17
18         /// <summary>
19         ///   This class is used to report errors and warnings t te user.
20         /// </summary>
21         public class Report {
22                 /// <summary>  
23                 ///   Errors encountered so far
24                 /// </summary>
25                 static public int Errors;
26
27                 /// <summary>  
28                 ///   Warnings encountered so far
29                 /// </summary>
30                 static public int Warnings;
31
32                 /// <summary>  
33                 ///   Whether errors should be throw an exception
34                 /// </summary>
35                 static public bool Fatal;
36                 
37                 /// <summary>  
38                 ///   Whether warnings should be considered errors
39                 /// </summary>
40                 static public bool WarningsAreErrors;
41
42                 /// <summary>  
43                 ///   Whether to dump a stack trace on errors. 
44                 /// </summary>
45                 static public bool Stacktrace;
46                 
47                 //
48                 // If the error code is reported on the given line,
49                 // then the process exits with a unique error code.
50                 //
51                 // Used for the test suite to excercise the error codes
52                 //
53                 static int probe_error = 0;
54
55                 //
56                 // Keeps track of the warnings that we are ignoring
57                 //
58                 static Hashtable warning_ignore_table;
59                 
60                 static void Check (int code)
61                 {
62                         if (code ==  probe_error){
63                                 Environment.Exit (123);
64                         }
65                 }
66                 
67                 static public void RealError (string msg)
68                 {
69                         Errors++;
70                         Console.WriteLine (msg);
71
72                         if (Stacktrace)
73                                 Console.WriteLine (new StackTrace ().ToString ());
74                         if (Fatal)
75                                 throw new Exception (msg);
76                 }
77
78                 static public void Error (int code, Location l, string text)
79                 {
80                         string msg = String.Format (
81 //                              "{0}({1}) error CS{2:0000}: {3}", l.Name, l.Row, code, text);
82                                 "{0}({1}) error CS{2}: {3}", l.Name, l.Row, code, text);
83                         
84                         RealError (msg);
85                         Check (code);
86                 }
87
88                 static public void Warning (int code, Location l, string text)
89                 {
90                         if (warning_ignore_table != null){
91                                 if (warning_ignore_table.Contains (code))
92                                         return;
93                         }
94                         
95                         if (WarningsAreErrors)
96                                 Error (code, l, text);
97                         else {
98                                 string row;
99                                 
100                                 if (Location.IsNull (l))
101                                         row = "";
102                                 else
103                                         row = l.Row.ToString ();
104                                 
105                                 Console.WriteLine (String.Format (
106 //                                      "{0}({1}) warning CS{2:0000}: {3}",
107                                         "{0}({1}) warning CS{2}: {3}",
108                                         l.Name,  row, code, text));
109                                 Warnings++;
110                                 Check (code);
111                         }
112                 }
113                 
114                 static public void Warning (int code, string text)
115                 {
116                         Warning (code, Location.Null, text);
117                 }
118
119                 static public void Error (int code, string text)
120                 {
121 //                      string msg = String.Format ("error CS{0:0000}: {1}", code, text);
122                         string msg = String.Format ("error CS{0}: {1}", code, text);
123                         
124                         RealError (msg);
125                         Check (code);
126                 }
127
128                 static public void Message (Message m)
129                 {
130                         if (m is ErrorMessage)
131                                 Error (m.code, m.text);
132                         else
133                                 Warning (m.code, m.text);
134                 }
135
136                 static public void SetIgnoreWarning (int code)
137                 {
138                         if (warning_ignore_table == null)
139                                 warning_ignore_table = new Hashtable ();
140
141                         warning_ignore_table [code] = true;
142                 }
143                 
144                 static public void SetProbe (int code)
145                 {
146                         probe_error = code;
147                 }
148
149                 static public int ProbeCode {
150                         get {
151                                 return probe_error;
152                         }
153                 }
154         }
155
156         public class Message {
157                 public int code;
158                 public string text;
159                 
160                 public Message (int code, string text)
161                 {
162                         this.code = code;
163                         this.text = text;
164                 }
165         }
166
167         public class WarningMessage : Message {
168                 public WarningMessage (int code, string text) : base (code, text)
169                 {
170                 }
171         }
172
173         public class ErrorMessage : Message {
174                 public ErrorMessage (int code, string text) : base (code, text)
175                 {
176                 }
177
178                 //
179                 // For compatibility reasons with old code.
180                 //
181                 public static void report_error (string error)
182                 {
183                         Console.Write ("ERROR: ");
184                         Console.WriteLine (error);
185                 }
186         }
187 }
188
189