home *** CD-ROM | disk | FTP | other *** search
/ Geek Gadgets 1 / ADE-1.bin / ade-dist / binutils-2.7-src.tgz / tar.out / fsf / binutils / gas / messages.c < prev    next >
C/C++ Source or Header  |  1996-09-28  |  12KB  |  540 lines

  1. /* messages.c - error reporter -
  2.    Copyright (C) 1987, 91, 92, 93, 94, 95, 1996 Free Software Foundation, Inc.
  3.  
  4.    This file is part of GAS, the GNU Assembler.
  5.  
  6.    GAS is free software; you can redistribute it and/or modify
  7.    it under the terms of the GNU General Public License as published by
  8.    the Free Software Foundation; either version 2, or (at your option)
  9.    any later version.
  10.  
  11.    GAS is distributed in the hope that it will be useful,
  12.    but WITHOUT ANY WARRANTY; without even the implied warranty of
  13.    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  14.    GNU General Public License for more details.
  15.  
  16.    You should have received a copy of the GNU General Public License
  17.    along with GAS; see the file COPYING.  If not, write to the Free
  18.    Software Foundation, 59 Temple Place - Suite 330, Boston, MA
  19.    02111-1307, USA. */
  20.  
  21. #include "as.h"
  22. #include "libiberty.h"
  23.  
  24. #include <stdio.h>
  25. #ifdef HAVE_ERRNO_H
  26. #include <errno.h>
  27. #endif
  28.  
  29. #ifdef USE_STDARG
  30. #include <stdarg.h>
  31. #endif
  32.  
  33. #ifdef USE_VARARGS
  34. #include <varargs.h>
  35. #endif
  36.  
  37. #if !defined (USE_STDARG) && !defined (USE_VARARGS)
  38. /* Roll our own.  */
  39. #define va_alist REST
  40. #define va_dcl
  41. typedef int * va_list;
  42. #define va_start(ARGS)    ARGS = &REST
  43. #define va_end(ARGS)
  44. #endif
  45.  
  46. extern char *xstrerror PARAMS ((int));    /* from libiberty */
  47.  
  48. static void as_show_where PARAMS ((void));
  49. static void as_warn_internal PARAMS ((char *, unsigned int, char *));
  50. static void as_bad_internal PARAMS ((char *, unsigned int, char *));
  51.  
  52. /*
  53.  * Despite the rest of the comments in this file, (FIXME-SOON),
  54.  * here is the current scheme for error messages etc:
  55.  *
  56.  * as_fatal() is used when gas is quite confused and
  57.  * continuing the assembly is pointless.  In this case we
  58.  * exit immediately with error status.
  59.  *
  60.  * as_bad() is used to mark errors that result in what we
  61.  * presume to be a useless object file.  Say, we ignored
  62.  * something that might have been vital.  If we see any of
  63.  * these, assembly will continue to the end of the source,
  64.  * no object file will be produced, and we will terminate
  65.  * with error status.  The new option, -Z, tells us to
  66.  * produce an object file anyway but we still exit with
  67.  * error status.  The assumption here is that you don't want
  68.  * this object file but we could be wrong.
  69.  *
  70.  * as_warn() is used when we have an error from which we
  71.  * have a plausible error recovery.  eg, masking the top
  72.  * bits of a constant that is longer than will fit in the
  73.  * destination.  In this case we will continue to assemble
  74.  * the source, although we may have made a bad assumption,
  75.  * and we will produce an object file and return normal exit
  76.  * status (ie, no error).  The new option -X tells us to
  77.  * treat all as_warn() errors as as_bad() errors.  That is,
  78.  * no object file will be produced and we will exit with
  79.  * error status.  The idea here is that we don't kill an
  80.  * entire make because of an error that we knew how to
  81.  * correct.  On the other hand, sometimes you might want to
  82.  * stop the make at these points.
  83.  *
  84.  * as_tsktsk() is used when we see a minor error for which
  85.  * our error recovery action is almost certainly correct.
  86.  * In this case, we print a message and then assembly
  87.  * continues as though no error occurred.
  88.  */
  89.  
  90. static void
  91. identify (file)
  92.      char *file;
  93. {
  94.   static int identified;
  95.   if (identified)
  96.     return;
  97.   identified++;
  98.  
  99.   if (!file)
  100.     {
  101.       unsigned int x;
  102.       as_where (&file, &x);
  103.     }
  104.  
  105.   if (file)
  106.     fprintf (stderr, "%s: ", file);
  107.   fprintf (stderr, "Assembler messages:\n");
  108. }
  109.  
  110. static int warning_count;    /* Count of number of warnings issued */
  111.  
  112. int 
  113. had_warnings ()
  114. {
  115.   return (warning_count);
  116. }
  117.  
  118. /* Nonzero if we've hit a 'bad error', and should not write an obj file,
  119.    and exit with a nonzero error code */
  120.  
  121. static int error_count;
  122.  
  123. int 
  124. had_errors ()
  125. {
  126.   return (error_count);
  127. }
  128.  
  129.  
  130. /* Print the current location to stderr.  */
  131.  
  132. static void
  133. as_show_where ()
  134. {
  135.   char *file;
  136.   unsigned int line;
  137.  
  138.   as_where (&file, &line);
  139.   identify (file);
  140.   if (file)
  141.     fprintf (stderr, "%s:%u: ", file, line);
  142. }
  143.  
  144. /*
  145.  *            a s _ p e r r o r
  146.  *
  147.  * Like perror(3), but with more info.
  148.  */
  149.  
  150. void 
  151. as_perror (gripe, filename)
  152.      const char *gripe;        /* Unpunctuated error theme. */
  153.      const char *filename;
  154. {
  155.   const char *errtxt;
  156.  
  157.   as_show_where ();
  158.   fprintf (stderr, gripe, filename);
  159. #ifdef BFD_ASSEMBLER
  160.   errtxt = bfd_errmsg (bfd_get_error ());
  161. #else
  162.   errtxt = xstrerror (errno);
  163. #endif
  164.   fprintf (stderr, ": %s\n", errtxt);
  165.   errno = 0;
  166. #ifdef BFD_ASSEMBLER
  167.   bfd_set_error (bfd_error_no_error);
  168. #endif
  169. }
  170.  
  171. /*
  172.  *            a s _ t s k t s k ()
  173.  *
  174.  * Send to stderr a string as a warning, and locate warning
  175.  * in input file(s).
  176.  * Please only use this for when we have some recovery action.
  177.  * Please explain in string (which may have '\n's) what recovery was done.
  178.  */
  179.  
  180. #ifdef USE_STDARG
  181. void 
  182. as_tsktsk (const char *format,...)
  183. {
  184.   va_list args;
  185.  
  186.   as_show_where ();
  187.   va_start (args, format);
  188.   vfprintf (stderr, format, args);
  189.   va_end (args);
  190.   (void) putc ('\n', stderr);
  191. }                /* as_tsktsk() */
  192. #else
  193. void 
  194. as_tsktsk (format, va_alist)
  195.      const char *format;
  196.      va_dcl
  197. {
  198.   va_list args;
  199.  
  200.   as_show_where ();
  201.   va_start (args);
  202.   vfprintf (stderr, format, args);
  203.   va_end (args);
  204.   (void) putc ('\n', stderr);
  205. }                /* as_tsktsk() */
  206. #endif /* not NO_STDARG */
  207.  
  208. /* The common portion of as_warn and as_warn_where.  */
  209.  
  210. static void
  211. as_warn_internal (file, line, buffer)
  212.      char *file;
  213.      unsigned int line;
  214.      char *buffer;
  215. {
  216.   ++warning_count;
  217.  
  218.   if (file == NULL)
  219.     as_where (&file, &line);
  220.  
  221.   identify (file);
  222.   if (file)
  223.     fprintf (stderr, "%s:%u: ", file, line);
  224.   fprintf (stderr, "Warning: ");
  225.   fputs (buffer, stderr);
  226.   (void) putc ('\n', stderr);
  227. #ifndef NO_LISTING
  228.   listing_warning (buffer);
  229. #endif
  230. }
  231.  
  232. /*
  233.  *            a s _ w a r n ()
  234.  *
  235.  * Send to stderr a string as a warning, and locate warning
  236.  * in input file(s).
  237.  * Please only use this for when we have some recovery action.
  238.  * Please explain in string (which may have '\n's) what recovery was done.
  239.  */
  240.  
  241. #ifdef USE_STDARG
  242. void 
  243. as_warn (const char *format,...)
  244. {
  245.   va_list args;
  246.   char buffer[200];
  247.  
  248.   if (!flag_no_warnings)
  249.     {
  250.       va_start (args, format);
  251.       vsprintf (buffer, format, args);
  252.       va_end (args);
  253.       as_warn_internal ((char *) NULL, 0, buffer);
  254.     }
  255. }                /* as_warn() */
  256. #else
  257. /*VARARGS1 */
  258. void 
  259. as_warn (format, va_alist)
  260.      const char *format;
  261.      va_dcl
  262. {
  263.   va_list args;
  264.   char buffer[200];
  265.  
  266.   if (!flag_no_warnings)
  267.     {
  268.       va_start (args);
  269.       vsprintf (buffer, format, args);
  270.       va_end (args);
  271.       as_warn_internal ((char *) NULL, 0, buffer);
  272.     }
  273. }                /* as_warn() */
  274. #endif /* not NO_STDARG */
  275.  
  276. /* as_warn_where, like as_bad but the file name and line number are
  277.    passed in.  Unfortunately, we have to repeat the function in order
  278.    to handle the varargs correctly and portably.  */
  279.  
  280. #ifdef USE_STDARG
  281. void 
  282. as_warn_where (char *file, unsigned int line, const char *format,...)
  283. {
  284.   va_list args;
  285.   char buffer[200];
  286.  
  287.   if (!flag_no_warnings)
  288.     {
  289.       va_start (args, format);
  290.       vsprintf (buffer, format, args);
  291.       va_end (args);
  292.       as_warn_internal (file, line, buffer);
  293.     }
  294. }                /* as_warn() */
  295. #else
  296. /*VARARGS1 */
  297. void 
  298. as_warn_where (file, line, format, va_alist)
  299.      char *file;
  300.      unsigned int line;
  301.      const char *format;
  302.      va_dcl
  303. {
  304.   va_list args;
  305.   char buffer[200];
  306.  
  307.   if (!flag_no_warnings)
  308.     {
  309.       va_start (args);
  310.       vsprintf (buffer, format, args);
  311.       va_end (args);
  312.       as_warn_internal (file, line, buffer);
  313.     }
  314. }                /* as_warn() */
  315. #endif /* not NO_STDARG */
  316.  
  317. /* The common portion of as_bad and as_bad_where.  */
  318.  
  319. static void
  320. as_bad_internal (file, line, buffer)
  321.      char *file;
  322.      unsigned int line;
  323.      char *buffer;
  324. {
  325.   ++error_count;
  326.  
  327.   if (file == NULL)
  328.     as_where (&file, &line);
  329.  
  330.   identify (file);
  331.   if (file)
  332.     fprintf (stderr, "%s:%u: ", file, line);
  333.   fprintf (stderr, "Error: ");
  334.   fputs (buffer, stderr);
  335.   (void) putc ('\n', stderr);
  336. #ifndef NO_LISTING
  337.   listing_error (buffer);
  338. #endif
  339. }
  340.  
  341. /*
  342.  *            a s _ b a d ()
  343.  *
  344.  * Send to stderr a string as a warning, and locate warning in input file(s).
  345.  * Please us when there is no recovery, but we want to continue processing
  346.  * but not produce an object file.
  347.  * Please explain in string (which may have '\n's) what recovery was done.
  348.  */
  349.  
  350. #ifdef USE_STDARG
  351. void 
  352. as_bad (const char *format,...)
  353. {
  354.   va_list args;
  355.   char buffer[200];
  356.  
  357.   va_start (args, format);
  358.   vsprintf (buffer, format, args);
  359.   va_end (args);
  360.  
  361.   as_bad_internal ((char *) NULL, 0, buffer);
  362. }
  363.  
  364. #else
  365. /*VARARGS1 */
  366. void 
  367. as_bad (format, va_alist)
  368.      const char *format;
  369.      va_dcl
  370. {
  371.   va_list args;
  372.   char buffer[200];
  373.  
  374.   va_start (args);
  375.   vsprintf (buffer, format, args);
  376.   va_end (args);
  377.  
  378.   as_bad_internal ((char *) NULL, 0, buffer);
  379. }
  380. #endif /* not NO_STDARG */
  381.  
  382. /* as_bad_where, like as_bad but the file name and line number are
  383.    passed in.  Unfortunately, we have to repeat the function in order
  384.    to handle the varargs correctly and portably.  */
  385.  
  386. #ifdef USE_STDARG
  387. void 
  388. as_bad_where (char *file, unsigned int line, const char *format,...)
  389. {
  390.   va_list args;
  391.   char buffer[200];
  392.  
  393.   va_start (args, format);
  394.   vsprintf (buffer, format, args);
  395.   va_end (args);
  396.  
  397.   as_bad_internal (file, line, buffer);
  398. }
  399.  
  400. #else
  401. /*VARARGS1 */
  402. void 
  403. as_bad_where (file, line, format, va_alist)
  404.      char *file;
  405.      unsigned int line;
  406.      const char *format;
  407.      va_dcl
  408. {
  409.   va_list args;
  410.   char buffer[200];
  411.  
  412.   va_start (args);
  413.   vsprintf (buffer, format, args);
  414.   va_end (args);
  415.  
  416.   as_bad_internal (file, line, buffer);
  417. }
  418. #endif /* not NO_STDARG */
  419.  
  420. /*
  421.  *            a s _ f a t a l ()
  422.  *
  423.  * Send to stderr a string as a fatal message, and print location of error in
  424.  * input file(s).
  425.  * Please only use this for when we DON'T have some recovery action.
  426.  * It xexit()s with a warning status.
  427.  */
  428.  
  429. #ifdef USE_STDARG
  430. void 
  431. as_fatal (const char *format,...)
  432. {
  433.   va_list args;
  434.  
  435.   as_show_where ();
  436.   va_start (args, format);
  437.   fprintf (stderr, "Fatal error: ");
  438.   vfprintf (stderr, format, args);
  439.   (void) putc ('\n', stderr);
  440.   va_end (args);
  441.   xexit (EXIT_FAILURE);
  442. }                /* as_fatal() */
  443. #else
  444. /*VARARGS1*/
  445. void 
  446. as_fatal (format, va_alist)
  447.      char *format;
  448.      va_dcl
  449. {
  450.   va_list args;
  451.  
  452.   as_show_where ();
  453.   va_start (args);
  454.   fprintf (stderr, "Fatal error: ");
  455.   vfprintf (stderr, format, args);
  456.   (void) putc ('\n', stderr);
  457.   va_end (args);
  458.   xexit (EXIT_FAILURE);
  459. }                /* as_fatal() */
  460. #endif /* not NO_STDARG */
  461.  
  462. /*
  463.  * as_assert: Indicate assertion failure.
  464.  * Arguments: Filename, line number, optional function name.
  465.  */
  466.  
  467. void
  468. as_assert (file, line, fn)
  469.      const char *file, *fn;
  470.      int line;
  471. {
  472.   as_show_where ();
  473.   fprintf (stderr, "Internal error!\n");
  474.   fprintf (stderr, "Assertion failure");
  475.   if (fn)
  476.     fprintf (stderr, " in %s", fn);
  477.   fprintf (stderr, " at %s line %d.\n", file, line);
  478.   fprintf (stderr, "Please report this bug.\n");
  479.   xexit (EXIT_FAILURE);
  480. }
  481.  
  482. /* as_abort: Print a friendly message saying how totally hosed we are,
  483.    and exit without producing a core file.  */
  484. void
  485. as_abort (file, line, fn)
  486.      const char *file, *fn;
  487.      int line;
  488. {
  489.   as_show_where ();
  490.   fprintf (stderr, "Internal error, aborting at %s line %d", file, line);
  491.   if (fn)
  492.     fprintf (stderr, " in %s", fn);
  493.   fprintf (stderr, "\nPlease report this bug.\n");
  494.   xexit (EXIT_FAILURE);
  495. }
  496.  
  497. /* Support routines.  */
  498.  
  499. void
  500. fprint_value (file, val)
  501.      FILE *file;
  502.      valueT val;
  503. {
  504.   if (sizeof (val) <= sizeof (long))
  505.     {
  506.       fprintf (file, "%ld", (long) val);
  507.       return;
  508.     }
  509. #ifdef BFD_ASSEMBLER
  510.   if (sizeof (val) <= sizeof (bfd_vma))
  511.     {
  512.       fprintf_vma (file, val);
  513.       return;
  514.     }
  515. #endif
  516.   abort ();
  517. }
  518.  
  519. void
  520. sprint_value (buf, val)
  521.      char *buf;
  522.      valueT val;
  523. {
  524.   if (sizeof (val) <= sizeof (long))
  525.     {
  526.       sprintf (buf, "%ld", (long) val);
  527.       return;
  528.     }
  529. #ifdef BFD_ASSEMBLER
  530.   if (sizeof (val) <= sizeof (bfd_vma))
  531.     {
  532.       sprintf_vma (buf, val);
  533.       return;
  534.     }
  535. #endif
  536.   abort ();
  537. }
  538.  
  539. /* end of messages.c */
  540.