2 // report.cs: report errors and warnings.
4 // Author: Miguel de Icaza (miguel@ximian.com)
5 // Marek Safar (marek.safar@seznam.cz)
7 // (C) 2001 Ximian, Inc. (http://www.ximian.com)
13 using System
.Collections
;
14 using System
.Collections
.Specialized
;
15 using System
.Diagnostics
;
16 using System
.Reflection
;
17 using System
.Reflection
.Emit
;
19 namespace Mono
.CSharp
{
22 /// This class is used to report errors and warnings t te user.
26 /// Errors encountered so far
28 static public int Errors
;
31 /// Warnings encountered so far
33 static public int Warnings
;
36 /// Whether errors should be throw an exception
38 static public bool Fatal
;
41 /// Whether warnings should be considered errors
43 static public bool WarningsAreErrors
;
46 /// Whether to dump a stack trace on errors.
48 static public bool Stacktrace
;
50 static public TextWriter Stderr
= Console
.Error
;
53 // If the 'expected' error code is reported then the
54 // compilation succeeds.
56 // Used for the test suite to excercise the error codes
58 static int expected_error
= 0;
61 // Keeps track of the warnings that we are ignoring
63 public static Hashtable warning_ignore_table
;
65 static Hashtable warning_regions_table
;
68 /// List of symbols related to reported error/warning. You have to fill it before error/warning is reported.
70 static StringCollection extra_information
= new StringCollection ();
73 // IF YOU ADD A NEW WARNING YOU HAVE TO ADD ITS ID HERE
75 public static readonly int[] AllWarnings
= new int[] {
77 105, 108, 109, 114, 162, 164, 168, 169, 183, 184, 197,
78 219, 251, 252, 253, 282,
79 419, 420, 429, 436, 440, 465,
80 612, 618, 626, 628, 642, 649, 652, 658, 659, 660, 661, 665, 672,
82 1522, 1570, 1571, 1572, 1573, 1574, 1580, 1581, 1584, 1587, 1589, 1590, 1591, 1592,
83 1616, 1633, 1634, 1635, 1690, 1691, 1692,
87 3005, 3012, 3019, 3021, 3022, 3023, 3026, 3027
92 // Just to be sure that binary search is working
93 Array
.Sort (AllWarnings
);
96 public static void Reset ()
98 Errors
= Warnings
= 0;
99 WarningsAreErrors
= false;
100 warning_ignore_table
= null;
101 warning_regions_table
= null;
104 abstract class AbstractMessage
{
106 static void Check (int code
)
108 if (code
== expected_error
) {
109 Environment
.Exit (0);
113 public abstract bool IsWarning { get; }
115 public abstract string MessageType { get; }
117 public virtual void Print (int code
, string location
, string text
)
122 StringBuilder msg
= new StringBuilder ();
123 if (location
.Length
!= 0) {
124 msg
.Append (location
);
127 msg
.AppendFormat ("{0} CS{1:0000}: {2}", MessageType
, code
, text
);
128 Stderr
.WriteLine (msg
.ToString ());
130 foreach (string s
in extra_information
)
131 Stderr
.WriteLine (s
+ MessageType
);
133 extra_information
.Clear ();
136 Console
.WriteLine (FriendlyStackTrace (new StackTrace (true)));
139 if (!IsWarning
|| WarningsAreErrors
)
140 throw new Exception (text
);
146 public virtual void Print (int code
, Location location
, string text
)
148 Print (code
, location
.IsNull
? "" : location
.ToString (), text
);
152 sealed class WarningMessage
: AbstractMessage
{
153 Location loc
= Location
.Null
;
156 public WarningMessage (int level
)
161 public override bool IsWarning
{
165 bool IsEnabled (int code
)
167 if (RootContext
.WarningLevel
< Level
)
170 if (warning_ignore_table
!= null) {
171 if (warning_ignore_table
.Contains (code
)) {
176 if (warning_regions_table
== null || loc
.Equals (Location
.Null
))
179 WarningRegions regions
= (WarningRegions
)warning_regions_table
[loc
.Name
];
183 return regions
.IsWarningEnabled (code
, loc
.Row
);
186 public override void Print(int code
, string location
, string text
)
188 if (!IsEnabled (code
)) {
189 extra_information
.Clear ();
193 if (WarningsAreErrors
) {
194 new ErrorMessage ().Print (code
, location
, text
);
199 base.Print (code
, location
, text
);
202 public override void Print(int code
, Location location
, string text
)
205 base.Print (code
, location
, text
);
208 public override string MessageType
{
215 sealed class ErrorMessage
: AbstractMessage
{
217 public override void Print(int code
, string location
, string text
)
220 base.Print (code
, location
, text
);
223 public override bool IsWarning
{
224 get { return false; }
227 public override string MessageType
{
235 public static void FeatureIsNotStandardized (Location loc
, string feature
)
237 Report
.Error (1644, loc
, "Feature `{0}' cannot be used because it is not part of the standardized ISO C# language specification", feature
);
240 public static string FriendlyStackTrace (Exception e
)
242 return FriendlyStackTrace (new StackTrace (e
, true));
245 static string FriendlyStackTrace (StackTrace t
)
247 StringBuilder sb
= new StringBuilder ();
249 bool foundUserCode
= false;
251 for (int i
= 0; i
< t
.FrameCount
; i
++) {
252 StackFrame f
= t
.GetFrame (i
);
253 MethodBase mb
= f
.GetMethod ();
255 if (!foundUserCode
&& mb
.ReflectedType
== typeof (Report
))
258 foundUserCode
= true;
262 if (f
.GetFileLineNumber () > 0)
263 sb
.AppendFormat ("(at {0}:{1}) ", f
.GetFileName (), f
.GetFileLineNumber ());
265 sb
.AppendFormat ("{0}.{1} (", mb
.ReflectedType
.Name
, mb
.Name
);
268 foreach (ParameterInfo pi
in mb
.GetParameters ()) {
273 sb
.Append (TypeManager
.CSharpName (pi
.ParameterType
));
278 return sb
.ToString ();
281 public static void StackTrace ()
283 Console
.WriteLine (FriendlyStackTrace (new StackTrace (true)));
286 public static bool IsValidWarning (int code
)
288 return Array
.BinarySearch (AllWarnings
, code
) >= 0;
291 static public void RuntimeMissingSupport (Location loc
, string feature
)
293 Report
.Error (-88, loc
, "Your .NET Runtime does not support `{0}'. Please use the latest Mono runtime instead.", feature
);
297 /// In most error cases is very useful to have information about symbol that caused the error.
298 /// Call this method before you call Report.Error when it makes sense.
300 static public void SymbolRelatedToPreviousError (Location loc
, string symbol
)
302 SymbolRelatedToPreviousError (loc
.ToString (), symbol
);
305 static public void SymbolRelatedToPreviousError (MemberInfo mi
)
307 TypeContainer temp_ds
= TypeManager
.LookupTypeContainer (mi
.DeclaringType
);
308 if (temp_ds
== null) {
309 SymbolRelatedToPreviousError (mi
.DeclaringType
.Assembly
.Location
, TypeManager
.GetFullNameSignature (mi
));
311 if (mi
is MethodBase
) {
312 IMethodData md
= TypeManager
.GetMethod ((MethodBase
)mi
);
313 SymbolRelatedToPreviousError (md
.Location
, md
.GetSignatureForError ());
317 MemberCore mc
= temp_ds
.GetDefinition (mi
.Name
);
318 SymbolRelatedToPreviousError (mc
);
322 static public void SymbolRelatedToPreviousError (MemberCore mc
)
324 SymbolRelatedToPreviousError (mc
.Location
, mc
.GetSignatureForError ());
327 static public void SymbolRelatedToPreviousError (Type type
)
329 if (type
is TypeBuilder
) {
330 DeclSpace temp_ds
= TypeManager
.LookupDeclSpace (type
);
331 SymbolRelatedToPreviousError (temp_ds
.Location
, TypeManager
.CSharpName (type
));
332 } else if (type
.HasElementType
) {
333 SymbolRelatedToPreviousError (type
.GetElementType ());
335 SymbolRelatedToPreviousError (type
.Assembly
.Location
, TypeManager
.CSharpName (type
));
339 static void SymbolRelatedToPreviousError (string loc
, string symbol
)
341 extra_information
.Add (String
.Format ("{0}: `{1}', name of symbol related to previous ", loc
, symbol
));
344 public static void ExtraInformation (Location loc
, string msg
)
346 extra_information
.Add (String
.Format ("{0} {1}", loc
, msg
));
349 public static WarningRegions
RegisterWarningRegion (Location location
)
351 if (warning_regions_table
== null)
352 warning_regions_table
= new Hashtable ();
354 WarningRegions regions
= (WarningRegions
)warning_regions_table
[location
.Name
];
355 if (regions
== null) {
356 regions
= new WarningRegions ();
357 warning_regions_table
.Add (location
.Name
, regions
);
362 static public void Warning (int code
, int level
, Location loc
, string message
)
364 WarningMessage w
= new WarningMessage (level
);
365 w
.Print (code
, loc
, message
);
368 static public void Warning (int code
, int level
, Location loc
, string format
, string arg
)
370 WarningMessage w
= new WarningMessage (level
);
371 w
.Print (code
, loc
, String
.Format (format
, arg
));
374 static public void Warning (int code
, int level
, Location loc
, string format
, string arg1
, string arg2
)
376 WarningMessage w
= new WarningMessage (level
);
377 w
.Print (code
, loc
, String
.Format (format
, arg1
, arg2
));
380 static public void Warning (int code
, int level
, Location loc
, string format
, params string[] args
)
382 WarningMessage w
= new WarningMessage (level
);
383 w
.Print (code
, loc
, String
.Format (format
, args
));
386 static public void Warning (int code
, int level
, string message
)
388 Warning (code
, level
, Location
.Null
, message
);
391 static public void Warning (int code
, int level
, string format
, string arg
)
393 Warning (code
, level
, Location
.Null
, format
, arg
);
396 static public void Warning (int code
, int level
, string format
, string arg1
, string arg2
)
398 Warning (code
, level
, Location
.Null
, format
, arg1
, arg2
);
401 static public void Warning (int code
, int level
, string format
, params string[] args
)
403 Warning (code
, level
, Location
.Null
, String
.Format (format
, args
));
406 static public void Error (int code
, Location loc
, string error
)
408 new ErrorMessage ().Print (code
, loc
, error
);
411 static public void Error (int code
, Location loc
, string format
, string arg
)
413 new ErrorMessage ().Print (code
, loc
, String
.Format (format
, arg
));
416 static public void Error (int code
, Location loc
, string format
, string arg1
, string arg2
)
418 new ErrorMessage ().Print (code
, loc
, String
.Format (format
, arg1
, arg2
));
421 static public void Error (int code
, Location loc
, string format
, params string[] args
)
423 Error (code
, loc
, String
.Format (format
, args
));
426 static public void Error (int code
, string error
)
428 Error (code
, Location
.Null
, error
);
431 static public void Error (int code
, string format
, string arg
)
433 Error (code
, Location
.Null
, format
, arg
);
436 static public void Error (int code
, string format
, string arg1
, string arg2
)
438 Error (code
, Location
.Null
, format
, arg1
, arg2
);
441 static public void Error (int code
, string format
, params string[] args
)
443 Error (code
, Location
.Null
, String
.Format (format
, args
));
446 static public void SetIgnoreWarning (int code
)
448 if (warning_ignore_table
== null)
449 warning_ignore_table
= new Hashtable ();
451 warning_ignore_table
[code
] = true;
454 static public int ExpectedError
{
456 expected_error
= value;
459 return expected_error
;
463 public static int DebugFlags
= 0;
465 [Conditional ("MCS_DEBUG")]
466 static public void Debug (string message
, params object[] args
)
468 Debug (4, message
, args
);
471 [Conditional ("MCS_DEBUG")]
472 static public void Debug (int category
, string message
, params object[] args
)
474 if ((category
& DebugFlags
) == 0)
477 StringBuilder sb
= new StringBuilder (message
);
479 if ((args
!= null) && (args
.Length
> 0)) {
483 foreach (object arg
in args
) {
490 else if (arg
is ICollection
)
491 sb
.Append (PrintCollection ((ICollection
) arg
));
497 Console
.WriteLine (sb
.ToString ());
500 static public string PrintCollection (ICollection collection
)
502 StringBuilder sb
= new StringBuilder ();
504 sb
.Append (collection
.GetType ());
508 foreach (object o
in collection
) {
517 return sb
.ToString ();
521 public enum TimerType
{
531 public enum CounterType
{
540 static DateTime
[] timer_start
;
541 static TimeSpan
[] timers
;
542 static long[] timer_counters
;
543 static long[] counters
;
547 timer_start
= new DateTime
[(int) TimerType
.CountTimers
];
548 timers
= new TimeSpan
[(int) TimerType
.CountTimers
];
549 timer_counters
= new long [(int) TimerType
.CountTimers
];
550 counters
= new long [(int) CounterType
.CountCounters
];
552 for (int i
= 0; i
< (int) TimerType
.CountTimers
; i
++) {
553 timer_start
[i
] = DateTime
.Now
;
554 timers
[i
] = TimeSpan
.Zero
;
558 [Conditional("TIMER")]
559 static public void IncrementCounter (CounterType which
)
561 ++counters
[(int) which
];
564 [Conditional("TIMER")]
565 static public void StartTimer (TimerType which
)
567 timer_start
[(int) which
] = DateTime
.Now
;
570 [Conditional("TIMER")]
571 static public void StopTimer (TimerType which
)
573 timers
[(int) which
] += DateTime
.Now
- timer_start
[(int) which
];
574 ++timer_counters
[(int) which
];
577 [Conditional("TIMER")]
578 static public void ShowTimers ()
580 ShowTimer (TimerType
.FindMembers
, "- FindMembers timer");
581 ShowTimer (TimerType
.TcFindMembers
, "- TypeContainer.FindMembers timer");
582 ShowTimer (TimerType
.MemberLookup
, "- MemberLookup timer");
583 ShowTimer (TimerType
.CachedLookup
, "- CachedLookup timer");
584 ShowTimer (TimerType
.CacheInit
, "- Cache init");
585 ShowTimer (TimerType
.MiscTimer
, "- Misc timer");
587 ShowCounter (CounterType
.FindMembers
, "- Find members");
588 ShowCounter (CounterType
.MemberCache
, "- Member cache");
589 ShowCounter (CounterType
.MiscCounter
, "- Misc counter");
592 static public void ShowCounter (CounterType which
, string msg
)
594 Console
.WriteLine ("{0} {1}", counters
[(int) which
], msg
);
597 static public void ShowTimer (TimerType which
, string msg
)
600 "[{0:00}:{1:000}] {2} (used {3} times)",
601 (int) timers
[(int) which
].TotalSeconds
,
602 timers
[(int) which
].Milliseconds
, msg
,
603 timer_counters
[(int) which
]);
607 public class InternalErrorException
: Exception
{
608 public InternalErrorException (Location loc
, string text
, Exception e
)
609 : base (loc
+ " " + text
, e
)
613 public InternalErrorException ()
614 : base ("Internal error")
618 public InternalErrorException (string message
)
625 /// Handles #pragma warning
627 public class WarningRegions
{
629 abstract class PragmaCmd
633 protected PragmaCmd (int line
)
638 public abstract bool IsEnabled (int code
, bool previous
);
641 class Disable
: PragmaCmd
644 public Disable (int line
, int code
)
650 public override bool IsEnabled (int code
, bool previous
)
652 return this.code
== code
? false : previous
;
656 class DisableAll
: PragmaCmd
658 public DisableAll (int line
)
661 public override bool IsEnabled(int code
, bool previous
)
667 class Enable
: PragmaCmd
670 public Enable (int line
, int code
)
676 public override bool IsEnabled(int code
, bool previous
)
678 return this.code
== code
? true : previous
;
682 class EnableAll
: PragmaCmd
684 public EnableAll (int line
)
687 public override bool IsEnabled(int code
, bool previous
)
694 ArrayList regions
= new ArrayList ();
696 public void WarningDisable (int line
)
698 regions
.Add (new DisableAll (line
));
701 public void WarningDisable (Location location
, int code
)
703 if (CheckWarningCode (code
, location
))
704 regions
.Add (new Disable (location
.Row
, code
));
707 public void WarningEnable (int line
)
709 regions
.Add (new EnableAll (line
));
712 public void WarningEnable (Location location
, int code
)
714 if (CheckWarningCode (code
, location
))
715 regions
.Add (new Enable (location
.Row
, code
));
718 public bool IsWarningEnabled (int code
, int src_line
)
721 foreach (PragmaCmd pragma
in regions
) {
722 if (src_line
< pragma
.Line
)
725 result
= pragma
.IsEnabled (code
, result
);
730 static bool CheckWarningCode (int code
, Location loc
)
732 if (Report
.IsValidWarning (code
))
735 Report
.Warning (1691, 1, loc
, "`{0}' is not a valid warning number", code
.ToString ());