2001-11-05 Miguel de Icaza <miguel@ximian.com>
[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
11 namespace CIR {
12
13         public class Report {
14                 static int errors;
15                 static int warnings;
16
17                 //
18                 // whether errors are fatal (they throw an exception), useful
19                 // for debugging the compiler
20                 //
21                 static bool fatal;
22
23                 //
24                 // If the error code is reported on the given line,
25                 // then the process exits with a unique error code.
26                 //
27                 // Used for the test suite to excercise the error codes
28                 //
29                 static int probe_error = 0;
30                 static int probe_line = 0;
31                 
32                 static void Check (int code)
33                 {
34                         if (code ==  probe_error){
35                                 Environment.Exit (123);
36                         }
37                 }
38                 
39                 static public void RealError (string msg)
40                 {
41                         errors++;
42                         Console.WriteLine (msg);
43
44                         if (fatal)
45                                 throw new Exception (msg);
46                 }
47                        
48                 static public void Error (int code, Location l, string text)
49                 {
50                         string msg = l.Name + "(" + l.Row + 
51                                 "): Error CS"+code+": " + text;
52
53                         RealError (msg);
54                         Check (code);
55                 }
56
57                 static public void Warning (int code, Location l, string text)
58                 {
59                         Console.WriteLine (l.Name + "(" + l.Row + 
60                                            "): Warning CS"+code+": " + text);
61                         warnings++;
62                         Check (code);
63                 }
64                 
65                 static public void Error (int code, string text)
66                 {
67                         string msg = "Error CS"+code+": "+text;
68
69                         RealError (msg);
70                         Check (code);
71                 }
72
73                 static public void Warning (int code, string text)
74                 {
75                         Console.WriteLine ("Warning CS"+code+": "+text);
76                         warnings++;
77                         Check (code);
78                 }
79
80                 static public void Message (Message m)
81                 {
82                         if (m is ErrorMessage)
83                                 Error (m.code, m.text);
84                         else
85                                 Warning (m.code, m.text);
86                 }
87
88                 static public void SetProbe (int code, int line)
89                 {
90                         probe_error = code;
91                         probe_line = line;
92                 }
93
94                 static public int ProbeCode {
95                         get {
96                                 return probe_error;
97                         }
98                 }
99                 
100                 static public int Errors {
101                         get {
102                                 return errors;
103                         }
104                 }
105
106                 static public int Warnings {
107                         get {
108                                 return warnings;
109                         }
110                 }
111
112                 static public bool Fatal {
113                         set {
114                                 fatal = true;
115                         }
116
117                         get {
118                                 return fatal;
119                         }
120                 }
121         }
122
123         public class Message {
124                 public int code;
125                 public string text;
126                 
127                 public Message (int code, string text)
128                 {
129                         this.code = code;
130                         this.text = text;
131                 }
132         }
133
134         public class WarningMessage : Message {
135                 public WarningMessage (int code, string text) : base (code, text)
136                 {
137                 }
138         }
139
140         public class ErrorMessage : Message {
141                 public ErrorMessage (int code, string text) : base (code, text)
142                 {
143                 }
144
145                 //
146                 // For compatibility reasons with old code.
147                 //
148                 public static void report_error (string error)
149                 {
150                         Console.Write ("ERROR: ");
151                         Console.WriteLine (error);
152                 }
153         }
154 }
155
156