123 of 158
117 7.3 Error and Warning Messages The compiler will output messages for detected error conditions and warnings to stderr (by default, the console). The message output may be redirected to a file using the I/O redirection capabilities of the operating system or by using a compiler option to specify the error output file (see –-error above). Error messages relating to problems with compiler options will not be affected by the presence of the error output specification. For code-related errors and warnings, the message will contain a reference to the filename and line number of the code corresponding to the error or warning. Although the line number given will generally be correct, code involving line continuations may lead to the line number reported being different than the actual physical line number where the offending code appears. The general format of the error and warning messages may be modified to some extent using the –- error-format option described in the preceding section. This may be useful if you are using the compiler with a different IDE that expects to see error message is a slightly different format for its “jump to the next error” function. 7.3.1 Controlling Warnings The types of warnings that the compiler will issue may be controlled from the command line using the -- warn option. Most of the warning types are enabled by default. It is desirable in some cases to allow the compiler to generate a certain type of warning message except for specific code sequences. This can be accomplished using the #pragma warning compiler directive in your program, the syntax of which is shown below. #pragma warning( <warning id> : <disposition> ) The <warning id> element is either a numeric value (included as part of the warning message) or one of the mnemonic warning identifiers used with the –warn option. The <disposition> element must be one of the keywords On, Off or Default, the effect of which is to enable the warning, disable the warning or set it to the default state. An example of the use of of the warning directive to disable a warning for useless code is shown below. Dim b as Byte Dim i as Integer Sub Main() b = 3 #pragma warning(7 : Off) If (b > 5) Then i = 100 ElseIf (b <= 5) Then i = 10 End If #pragma warning(7 : On) b = 25 End Sub Because of the immediately preceding assignment, the compiler can deduce the Boolean value of the conditions in both the If and ElseIf statements and will issue a “useless-code” warning indicating that that those conditions are always false. The first warning directive disables that warning and the second one unconditionally enables it. Often, it is undesirable to unconditionally enable a warning after disabling it as was done in this example. To avoid enabling a warning that was not previously enabled, two variations of the warning directive are provided to save the current set of enabled warnings (push) and restore them afterward (pop) instead of unconditionally re-enabling a warning. The syntax for the push and pop warning directives is illustrated in the modified example below.