Back to index

cell-binutils  2.17cvs20070401
messages.c
Go to the documentation of this file.
00001 /* messages.c - error reporter -
00002    Copyright 1987, 1991, 1992, 1993, 1994, 1995, 1996, 1998, 2000, 2001,
00003    2003, 2004, 2005, 2006
00004    Free Software Foundation, Inc.
00005    This file is part of GAS, the GNU Assembler.
00006 
00007    GAS is free software; you can redistribute it and/or modify
00008    it under the terms of the GNU General Public License as published by
00009    the Free Software Foundation; either version 2, or (at your option)
00010    any later version.
00011 
00012    GAS is distributed in the hope that it will be useful,
00013    but WITHOUT ANY WARRANTY; without even the implied warranty of
00014    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00015    GNU General Public License for more details.
00016 
00017    You should have received a copy of the GNU General Public License
00018    along with GAS; see the file COPYING.  If not, write to the Free
00019    Software Foundation, 51 Franklin Street - Fifth Floor, Boston, MA
00020    02110-1301, USA.  */
00021 
00022 #include "as.h"
00023 
00024 static void identify (char *);
00025 static void as_show_where (void);
00026 static void as_warn_internal (char *, unsigned int, char *);
00027 static void as_bad_internal (char *, unsigned int, char *);
00028 
00029 /* Despite the rest of the comments in this file, (FIXME-SOON),
00030    here is the current scheme for error messages etc:
00031 
00032    as_fatal() is used when gas is quite confused and
00033    continuing the assembly is pointless.  In this case we
00034    exit immediately with error status.
00035 
00036    as_bad() is used to mark errors that result in what we
00037    presume to be a useless object file.  Say, we ignored
00038    something that might have been vital.  If we see any of
00039    these, assembly will continue to the end of the source,
00040    no object file will be produced, and we will terminate
00041    with error status.  The new option, -Z, tells us to
00042    produce an object file anyway but we still exit with
00043    error status.  The assumption here is that you don't want
00044    this object file but we could be wrong.
00045 
00046    as_warn() is used when we have an error from which we
00047    have a plausible error recovery.  eg, masking the top
00048    bits of a constant that is longer than will fit in the
00049    destination.  In this case we will continue to assemble
00050    the source, although we may have made a bad assumption,
00051    and we will produce an object file and return normal exit
00052    status (ie, no error).  The new option -X tells us to
00053    treat all as_warn() errors as as_bad() errors.  That is,
00054    no object file will be produced and we will exit with
00055    error status.  The idea here is that we don't kill an
00056    entire make because of an error that we knew how to
00057    correct.  On the other hand, sometimes you might want to
00058    stop the make at these points.
00059 
00060    as_tsktsk() is used when we see a minor error for which
00061    our error recovery action is almost certainly correct.
00062    In this case, we print a message and then assembly
00063    continues as though no error occurred.  */
00064 
00065 static void
00066 identify (char *file)
00067 {
00068   static int identified;
00069 
00070   if (identified)
00071     return;
00072   identified++;
00073 
00074   if (!file)
00075     {
00076       unsigned int x;
00077       as_where (&file, &x);
00078     }
00079 
00080   if (file)
00081     fprintf (stderr, "%s: ", file);
00082   fprintf (stderr, _("Assembler messages:\n"));
00083 }
00084 
00085 /* The number of warnings issued.  */
00086 static int warning_count;
00087 
00088 int
00089 had_warnings (void)
00090 {
00091   return warning_count;
00092 }
00093 
00094 /* Nonzero if we've hit a 'bad error', and should not write an obj file,
00095    and exit with a nonzero error code.  */
00096 
00097 static int error_count;
00098 
00099 int
00100 had_errors (void)
00101 {
00102   return error_count;
00103 }
00104 
00105 /* Print the current location to stderr.  */
00106 
00107 static void
00108 as_show_where (void)
00109 {
00110   char *file;
00111   unsigned int line;
00112 
00113   as_where (&file, &line);
00114   identify (file);
00115   if (file)
00116     fprintf (stderr, "%s:%u: ", file, line);
00117 }
00118 
00119 /* Send to stderr a string as a warning, and locate warning
00120    in input file(s).
00121    Please only use this for when we have some recovery action.
00122    Please explain in string (which may have '\n's) what recovery was
00123    done.  */
00124 
00125 #ifdef USE_STDARG
00126 void
00127 as_tsktsk (const char *format, ...)
00128 {
00129   va_list args;
00130 
00131   as_show_where ();
00132   va_start (args, format);
00133   vfprintf (stderr, format, args);
00134   va_end (args);
00135   (void) putc ('\n', stderr);
00136 }
00137 #else
00138 void
00139 as_tsktsk (format, va_alist)
00140      const char *format;
00141      va_dcl
00142 {
00143   va_list args;
00144 
00145   as_show_where ();
00146   va_start (args);
00147   vfprintf (stderr, format, args);
00148   va_end (args);
00149   (void) putc ('\n', stderr);
00150 }
00151 #endif /* not NO_STDARG */
00152 
00153 /* The common portion of as_warn and as_warn_where.  */
00154 
00155 static void
00156 as_warn_internal (char *file, unsigned int line, char *buffer)
00157 {
00158   ++warning_count;
00159 
00160   if (file == NULL)
00161     as_where (&file, &line);
00162 
00163   identify (file);
00164   if (file)
00165     fprintf (stderr, "%s:%u: ", file, line);
00166   fprintf (stderr, _("Warning: "));
00167   fputs (buffer, stderr);
00168   (void) putc ('\n', stderr);
00169 #ifndef NO_LISTING
00170   listing_warning (buffer);
00171 #endif
00172 }
00173 
00174 /* Send to stderr a string as a warning, and locate warning
00175    in input file(s).
00176    Please only use this for when we have some recovery action.
00177    Please explain in string (which may have '\n's) what recovery was
00178    done.  */
00179 
00180 #ifdef USE_STDARG
00181 void
00182 as_warn (const char *format, ...)
00183 {
00184   va_list args;
00185   char buffer[2000];
00186 
00187   if (!flag_no_warnings)
00188     {
00189       va_start (args, format);
00190       vsnprintf (buffer, sizeof (buffer), format, args);
00191       va_end (args);
00192       as_warn_internal ((char *) NULL, 0, buffer);
00193     }
00194 }
00195 #else
00196 void
00197 as_warn (format, va_alist)
00198      const char *format;
00199      va_dcl
00200 {
00201   va_list args;
00202   char buffer[2000];
00203 
00204   if (!flag_no_warnings)
00205     {
00206       va_start (args);
00207       vsnprintf (buffer, sizeof (buffer), format, args);
00208       va_end (args);
00209       as_warn_internal ((char *) NULL, 0, buffer);
00210     }
00211 }
00212 #endif /* not NO_STDARG */
00213 
00214 /* Like as_bad but the file name and line number are passed in.
00215    Unfortunately, we have to repeat the function in order to handle
00216    the varargs correctly and portably.  */
00217 
00218 #ifdef USE_STDARG
00219 void
00220 as_warn_where (char *file, unsigned int line, const char *format, ...)
00221 {
00222   va_list args;
00223   char buffer[2000];
00224 
00225   if (!flag_no_warnings)
00226     {
00227       va_start (args, format);
00228       vsnprintf (buffer, sizeof (buffer), format, args);
00229       va_end (args);
00230       as_warn_internal (file, line, buffer);
00231     }
00232 }
00233 #else
00234 void
00235 as_warn_where (file, line, format, va_alist)
00236      char *file;
00237      unsigned int line;
00238      const char *format;
00239      va_dcl
00240 {
00241   va_list args;
00242   char buffer[2000];
00243 
00244   if (!flag_no_warnings)
00245     {
00246       va_start (args);
00247       vsnprintf (buffer, sizeof (buffer), format, args);
00248       va_end (args);
00249       as_warn_internal (file, line, buffer);
00250     }
00251 }
00252 #endif /* not NO_STDARG */
00253 
00254 /* The common portion of as_bad and as_bad_where.  */
00255 
00256 static void
00257 as_bad_internal (char *file, unsigned int line, char *buffer)
00258 {
00259   ++error_count;
00260 
00261   if (file == NULL)
00262     as_where (&file, &line);
00263 
00264   identify (file);
00265   if (file)
00266     fprintf (stderr, "%s:%u: ", file, line);
00267   fprintf (stderr, _("Error: "));
00268   fputs (buffer, stderr);
00269   (void) putc ('\n', stderr);
00270 #ifndef NO_LISTING
00271   listing_error (buffer);
00272 #endif
00273 }
00274 
00275 /* Send to stderr a string as a warning, and locate warning in input
00276    file(s).  Please us when there is no recovery, but we want to
00277    continue processing but not produce an object file.
00278    Please explain in string (which may have '\n's) what recovery was
00279    done.  */
00280 
00281 #ifdef USE_STDARG
00282 void
00283 as_bad (const char *format, ...)
00284 {
00285   va_list args;
00286   char buffer[2000];
00287 
00288   va_start (args, format);
00289   vsnprintf (buffer, sizeof (buffer), format, args);
00290   va_end (args);
00291 
00292   as_bad_internal ((char *) NULL, 0, buffer);
00293 }
00294 
00295 #else
00296 void
00297 as_bad (format, va_alist)
00298      const char *format;
00299      va_dcl
00300 {
00301   va_list args;
00302   char buffer[2000];
00303 
00304   va_start (args);
00305   vsnprintf (buffer, sizeof (buffer), format, args);
00306   va_end (args);
00307 
00308   as_bad_internal ((char *) NULL, 0, buffer);
00309 }
00310 #endif /* not NO_STDARG */
00311 
00312 /* Like as_bad but the file name and line number are passed in.
00313    Unfortunately, we have to repeat the function in order to handle
00314    the varargs correctly and portably.  */
00315 
00316 #ifdef USE_STDARG
00317 void
00318 as_bad_where (char *file, unsigned int line, const char *format, ...)
00319 {
00320   va_list args;
00321   char buffer[2000];
00322 
00323   va_start (args, format);
00324   vsnprintf (buffer, sizeof (buffer), format, args);
00325   va_end (args);
00326 
00327   as_bad_internal (file, line, buffer);
00328 }
00329 
00330 #else
00331 void
00332 as_bad_where (file, line, format, va_alist)
00333      char *file;
00334      unsigned int line;
00335      const char *format;
00336      va_dcl
00337 {
00338   va_list args;
00339   char buffer[2000];
00340 
00341   va_start (args);
00342   vsnprintf (buffer, sizeof (buffer), format, args);
00343   va_end (args);
00344 
00345   as_bad_internal (file, line, buffer);
00346 }
00347 #endif /* not NO_STDARG */
00348 
00349 /* Send to stderr a string as a fatal message, and print location of
00350    error in input file(s).
00351    Please only use this for when we DON'T have some recovery action.
00352    It xexit()s with a warning status.  */
00353 
00354 #ifdef USE_STDARG
00355 void
00356 as_fatal (const char *format, ...)
00357 {
00358   va_list args;
00359 
00360   as_show_where ();
00361   va_start (args, format);
00362   fprintf (stderr, _("Fatal error: "));
00363   vfprintf (stderr, format, args);
00364   (void) putc ('\n', stderr);
00365   va_end (args);
00366   /* Delete the output file, if it exists.  This will prevent make from
00367      thinking that a file was created and hence does not need rebuilding.  */
00368   if (out_file_name != NULL)
00369     unlink_if_ordinary (out_file_name);
00370   xexit (EXIT_FAILURE);
00371 }
00372 #else
00373 void
00374 as_fatal (format, va_alist)
00375      char *format;
00376      va_dcl
00377 {
00378   va_list args;
00379 
00380   as_show_where ();
00381   va_start (args);
00382   fprintf (stderr, _("Fatal error: "));
00383   vfprintf (stderr, format, args);
00384   (void) putc ('\n', stderr);
00385   va_end (args);
00386   xexit (EXIT_FAILURE);
00387 }
00388 #endif /* not NO_STDARG */
00389 
00390 /* Indicate assertion failure.
00391    Arguments: Filename, line number, optional function name.  */
00392 
00393 void
00394 as_assert (const char *file, int line, const char *fn)
00395 {
00396   as_show_where ();
00397   fprintf (stderr, _("Internal error!\n"));
00398   if (fn)
00399     fprintf (stderr, _("Assertion failure in %s at %s line %d.\n"),
00400             fn, file, line);
00401   else
00402     fprintf (stderr, _("Assertion failure at %s line %d.\n"), file, line);
00403   fprintf (stderr, _("Please report this bug.\n"));
00404   xexit (EXIT_FAILURE);
00405 }
00406 
00407 /* as_abort: Print a friendly message saying how totally hosed we are,
00408    and exit without producing a core file.  */
00409 
00410 void
00411 as_abort (const char *file, int line, const char *fn)
00412 {
00413   as_show_where ();
00414   if (fn)
00415     fprintf (stderr, _("Internal error, aborting at %s line %d in %s\n"),
00416             file, line, fn);
00417   else
00418     fprintf (stderr, _("Internal error, aborting at %s line %d\n"),
00419             file, line);
00420   fprintf (stderr, _("Please report this bug.\n"));
00421   xexit (EXIT_FAILURE);
00422 }
00423 
00424 /* Support routines.  */
00425 
00426 void
00427 sprint_value (char *buf, valueT val)
00428 {
00429   if (sizeof (val) <= sizeof (long))
00430     {
00431       sprintf (buf, "%ld", (long) val);
00432       return;
00433     }
00434   if (sizeof (val) <= sizeof (bfd_vma))
00435     {
00436       sprintf_vma (buf, val);
00437       return;
00438     }
00439   abort ();
00440 }
00441 
00442 #define HEX_MAX_THRESHOLD   1024
00443 #define HEX_MIN_THRESHOLD   -(HEX_MAX_THRESHOLD)
00444 
00445 static void
00446 as_internal_value_out_of_range (char *    prefix,
00447                             offsetT   val,
00448                             offsetT   min,
00449                             offsetT   max,
00450                             char *    file,
00451                             unsigned  line,
00452                             int       bad)
00453 {
00454   const char * err;
00455 
00456   if (prefix == NULL)
00457     prefix = "";
00458 
00459   if (   val < HEX_MAX_THRESHOLD
00460       && min < HEX_MAX_THRESHOLD
00461       && max < HEX_MAX_THRESHOLD
00462       && val > HEX_MIN_THRESHOLD
00463       && min > HEX_MIN_THRESHOLD
00464       && max > HEX_MIN_THRESHOLD)
00465     {
00466       /* xgettext:c-format  */
00467       err = _("%s out of range (%d is not between %d and %d)");
00468 
00469       if (bad)
00470        as_bad_where (file, line, err,
00471                     prefix, (int) val, (int) min, (int) max);
00472       else
00473        as_warn_where (file, line, err,
00474                      prefix, (int) val, (int) min, (int) max);
00475     }
00476   else
00477     {
00478       char val_buf [sizeof (val) * 3 + 2];
00479       char min_buf [sizeof (val) * 3 + 2];
00480       char max_buf [sizeof (val) * 3 + 2];
00481 
00482       if (sizeof (val) > sizeof (bfd_vma))
00483        abort ();
00484 
00485       sprintf_vma (val_buf, val);
00486       sprintf_vma (min_buf, min);
00487       sprintf_vma (max_buf, max);
00488 
00489       /* xgettext:c-format.  */
00490       err = _("%s out of range (0x%s is not between 0x%s and 0x%s)");
00491 
00492       if (bad)
00493        as_bad_where (file, line, err, prefix, val_buf, min_buf, max_buf);
00494       else
00495        as_warn_where (file, line, err, prefix, val_buf, min_buf, max_buf);
00496     }
00497 }
00498 
00499 void
00500 as_warn_value_out_of_range (char *   prefix,
00501                         offsetT  value,
00502                         offsetT  min,
00503                         offsetT  max,
00504                         char *   file,
00505                         unsigned line)
00506 {
00507   as_internal_value_out_of_range (prefix, value, min, max, file, line, 0);
00508 }
00509 
00510 void
00511 as_bad_value_out_of_range (char *   prefix,
00512                         offsetT  value,
00513                         offsetT  min,
00514                         offsetT  max,
00515                         char *   file,
00516                         unsigned line)
00517 {
00518   as_internal_value_out_of_range (prefix, value, min, max, file, line, 1);
00519 }