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