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