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