Main Page   Alphabetical List   Compound List   File List   Compound Members   File Members  

cpperror.c

Go to the documentation of this file.
00001 /*
00002 ** LCLint - annotation-assisted static program checker
00003 ** Copyright (C) 1994-2000 University of Virginia,
00004 **         Massachusetts Institute of Technology
00005 **
00006 ** This program is free software; you can redistribute it and/or modify it
00007 ** under the terms of the GNU General Public License as published by the
00008 ** Free Software Foundation; either version 2 of the License, or (at your
00009 ** option) any later version.
00010 ** 
00011 ** This program is distributed in the hope that it will be useful, but
00012 ** WITHOUT ANY WARRANTY; without even the implied warranty of
00013 ** MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
00014 ** General Public License for more details.
00015 ** 
00016 ** The GNU General Public License is available from http://www.gnu.org/ or
00017 ** the Free Software Foundation, Inc., 59 Temple Place - Suite 330, Boston,
00018 ** MA 02111-1307, USA.
00019 **
00020 ** For information on lclint: lclint-request@cs.virginia.edu
00021 ** To report a bug: lclint-bug@cs.virginia.edu
00022 ** For more information: http://lclint.cs.virginia.edu
00023 */
00024 /*
00025 ** cpperror.c
00026 */
00027 /* Default error handlers for CPP Library.
00028    Copyright (C) 1986, 87, 89, 92, 93, 94, 1995 Free Software Foundation, Inc.
00029    Written by Per Bothner, 1994.
00030    Based on CCCP program by by Paul Rubin, June 1986
00031    Adapted to ANSI C, Richard Stallman, Jan 1987
00032 
00033 This program is free software; you can redistribute it and/or modify it
00034 under the terms of the GNU General Public License as published by the
00035 Free Software Foundation; either version 2, or (at your option) any
00036 later version.
00037 
00038 This program is distributed in the hope that it will be useful,
00039 but WITHOUT ANY WARRANTY; without even the implied warranty of
00040 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00041 GNU General Public License for more details.
00042 
00043 You should have received a copy of the GNU General Public License
00044 along with this program; if not, write to the Free Software
00045 Foundation, 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
00046 
00047  In other words, you are welcome to use, share and improve this program.
00048  You are forbidden to forbid anyone else to use, share and improve
00049  what you give them.   Help stamp out software-hoarding!  */
00050 
00051 /*@-macroconstdecl@*/ 
00052 /*@-macrofcndecl@*/
00053 /*@-evalorderuncon@*/
00054 /*@+ptrnegate@*/
00055 /*@-memchecks@*/
00056 /*@+charintliteral@*/
00057 /*@-infloopsuncon@*/
00058 /*@-loopswitchbreak@*/
00059 /*@-switchloopbreak@*/
00060 /*@-whileblock@*/
00061 /*@-forblock@*/
00062 /*@-elseifcomplete@*/
00063 /*@-abstract@*/
00064 /*@-usedef@*/
00065 /*@-retvalint@*/
00066 /*@-macroparams@*/
00067 /*@-readonlytrans@*/
00068 /*@-boolops@*/
00069 /*@-sizeoftype@*/
00070 /*@-predboolint@*/
00071 /*@-predboolptr@*/
00072 /*@+boolint@*/
00073 /*@+charint@*/
00074 /*@+ignorequals@*/
00075 /*@+ignoresigns@*/
00076 /*@+matchanyintegral@*/
00077 /*@-shiftsigned@*/
00078 /*@-onlyunqglobaltrans@*/
00079 /*@-macroconstdecl@*/
00080 
00081 # include <stdlib.h>
00082 # include <errno.h>
00083 # include <stdio.h>
00084 
00085 # define FATAL_EXIT_CODE EXIT_FAILURE
00086 
00087 # include "lclintMacros.nf"
00088 # include "llbasic.h"
00089 # include "cpp.h"
00090 # include "cpplib.h"
00091 # include "cpperror.h"
00092 
00093 static void cppReader_printFileAndLine (cppReader *p_pfile);
00094 static void cppReader_warningWithLine (cppReader *p_pfile, 
00095                                        int p_line, int p_column, 
00096                                        /*@only@*/ cstring p_msg);
00097 
00098 /* Print the file names and line numbers of the #include
00099    commands which led to the current file.  */
00100 
00101 static void cppReader_printContainingFiles (cppReader *pfile)
00102 {
00103   cppBuffer *ip;
00104   int first = 1;
00105 
00106   if (pfile == NULL) {
00107     /* Error processing command line. */
00108     return;
00109   }
00110 
00111   /* If stack of files hasn't changed since we last printed
00112      this info, don't repeat it.  */
00113   if (pfile->input_stack_listing_current)
00114     {
00115       return;
00116     }
00117 
00118   ip = cppReader_fileBuffer (pfile);
00119 
00120   /* Give up if we don't find a source file.  */
00121   if (ip == NULL)
00122     {
00123       return;
00124     }
00125 
00126   /* Find the other, outer source files.  */
00127   while ((ip = cppBuffer_prevBuffer (ip)), 
00128          ip != cppReader_nullBuffer (pfile))
00129     {
00130       int line, col;
00131       cstring temps; 
00132       
00133       cppBuffer_lineAndColumn (ip, &line, &col);
00134       if (ip->fname != NULL)
00135         {
00136           if (first)
00137             {
00138               first = 0;
00139               fprintf (stderr, "In file included");
00140             }
00141           else
00142             fprintf (stderr, ",\n                ");
00143         }
00144 
00145       fprintf (stderr, "  from %s", 
00146                cstring_toCharsSafe (temps = fileloc_unparseRaw (ip->nominal_fname, line)));
00147 
00148       cstring_free (temps);
00149     }
00150   
00151   if (!first)
00152     {
00153       fprintf (stderr, ":\n");
00154     }
00155 
00156   /* Record we have printed the status as of this time.  */
00157   pfile->input_stack_listing_current = 1;
00158 }
00159 
00160 static void
00161 cppReader_fileLineForMessage (cstring filename, long line, long column)
00162 {
00163   if (column > 0)
00164     {
00165       cstring temps;
00166 
00167       if (filename != NULL)
00168         {
00169           fprintf (stderr, "%s: ",
00170                    cstring_toCharsSafe (temps = fileloc_unparseRawCol (filename, (int) line, (int) column)));
00171         }
00172       else 
00173         {
00174           fprintf (stderr, "%s: ",
00175                    cstring_toCharsSafe (temps = fileloc_unparseRawCol (cstring_makeLiteralTemp ("<no file>"),
00176                                                                        (int) line, (int) column)));
00177         }
00178 
00179       cstring_free (temps);
00180     }
00181   else
00182     {
00183       cstring temps;
00184 
00185       if (filename != NULL) 
00186         {
00187           fprintf (stderr, "%s: ", 
00188                    cstring_toCharsSafe (temps = fileloc_unparseRaw (filename, (int) line)));
00189           
00190         }
00191       else
00192         {
00193           fprintf (stderr, "%s: ",
00194                    cstring_toCharsSafe (temps = fileloc_unparseRaw (cstring_makeLiteralTemp ("<no file>"),
00195                                                                     (int) line)));
00196         }
00197 
00198       cstring_free (temps);
00199     }
00200 }
00201 
00202 /* IS_ERROR is 2 for "fatal" error, 1 for error, 0 for warning */
00203 
00204 static void
00205 cppReader_message (cppReader *pfile, int is_error, /*@only@*/ cstring msg)
00206 {
00207   if (!is_error)
00208     {
00209       /* fprintf (stderr, "warning: "); */
00210     }
00211   else if (is_error == 2)
00212     {
00213       pfile->errors = cppReader_fatalErrorLimit;
00214     }
00215   else if (pfile->errors < cppReader_fatalErrorLimit)
00216     {
00217       pfile->errors++;
00218     }
00219   else
00220     {
00221       ;
00222     }
00223 
00224   fprintf (stderr, "%s", cstring_toCharsSafe (msg));
00225   fprintf (stderr, "\n");
00226 }
00227 
00228 /* Same as cppReader_error, except we consider the error to be "fatal",
00229    such as inconsistent options.  I.e. there is little point in continuing.
00230    (We do not exit, to support use of cpplib as a library.
00231    Instead, it is the caller's responsibility to check
00232    cppFatalErrors.  */
00233 
00234 void
00235 cppReader_fatalError (cppReader *pfile, /*@only@*/ cstring str)
00236 {
00237   fprintf (stderr, "preprocessor: ");
00238   cppReader_message (pfile, 2, str);
00239 }
00240 
00241 void
00242 cppReader_pfatalWithName (cppReader *pfile, cstring name)
00243 {
00244   cppReader_perrorWithName (pfile, name);
00245   exit (FATAL_EXIT_CODE);
00246 }
00247 
00248 void
00249 cppReader_printFileAndLine (cppReader *pfile)
00250 {
00251   cppBuffer *ip = cppReader_fileBuffer (pfile);
00252 
00253   if (ip != NULL)
00254     {
00255       int line, col;
00256 
00257       cppBuffer_lineAndColumn (ip, &line, &col);
00258       cppReader_fileLineForMessage (ip->nominal_fname,
00259                                     line, pfile->show_column ? col : -1);
00260     }
00261   else
00262     {
00263       fprintf (stderr, "Command Line: ");
00264     }
00265 }
00266 
00267 void
00268 cppReader_errorLit (cppReader *pfile, /*@observer@*/ cstring msg)
00269 {
00270   cppReader_error (pfile, cstring_copy (msg));
00271 }
00272 
00273 void
00274 cppReader_error (cppReader *pfile, /*@only@*/ cstring msg)
00275 {
00276   prepareMessage ();
00277   cppReader_printContainingFiles (pfile);
00278   cppReader_printFileAndLine (pfile);
00279   cppReader_message (pfile, 1, msg);
00280   closeMessage ();
00281 }
00282 
00283 /* Print error message but don't count it.  */
00284 
00285 void
00286 cppReader_warningLit (cppReader *pfile, cstring msg)
00287 {
00288   cppReader_warning (pfile, cstring_copy (msg));
00289 }
00290 
00291 void
00292 cppReader_warning (cppReader *pfile, /*@only@*/ cstring msg)
00293 {
00294   if (CPPOPTIONS (pfile)->inhibit_warnings)
00295     return;
00296 
00297   if (CPPOPTIONS (pfile)->warnings_are_errors)
00298     pfile->errors++;
00299 
00300   prepareMessage ();
00301   cppReader_printContainingFiles (pfile);
00302   cppReader_printFileAndLine (pfile);
00303   cppReader_message (pfile, 0, msg);
00304   closeMessage ();
00305 }
00306 
00307 /* Print an error message and maybe count it.  */
00308 
00309 extern void
00310 cppReader_pedwarnLit (cppReader *pfile, /*@observer@*/ cstring msg)
00311 {
00312   cppReader_pedwarn (pfile, cstring_copy (msg));
00313 }
00314 
00315 extern void
00316 cppReader_pedwarn (cppReader *pfile, /*@only@*/ cstring msg)
00317 {
00318   if (CPPOPTIONS (pfile)->pedantic_errors)
00319     {
00320       cppReader_error (pfile, msg);
00321     }
00322   else
00323     {
00324       cppReader_warning (pfile, msg);
00325     }
00326 }
00327 
00328 void
00329 cppReader_errorWithLine (cppReader *pfile, long line, long column, 
00330                      /*@only@*/ cstring msg)
00331 {
00332   cppBuffer *ip = cppReader_fileBuffer (pfile);
00333 
00334   prepareMessage ();
00335   cppReader_printContainingFiles (pfile);
00336 
00337   if (ip != NULL)
00338     cppReader_fileLineForMessage (ip->nominal_fname, line, column);
00339 
00340   cppReader_message (pfile, 1, msg);
00341   closeMessage ();
00342 }
00343 
00344 void
00345 cppReader_warningWithLine (cppReader *pfile, 
00346                        int line, int column, 
00347                        /*@only@*/ cstring msg)
00348 {
00349   cppBuffer *ip;
00350 
00351   if (CPPOPTIONS (pfile)->inhibit_warnings)
00352     return;
00353 
00354   if (CPPOPTIONS (pfile)->warnings_are_errors)
00355     pfile->errors++;
00356 
00357   prepareMessage ();
00358 
00359   cppReader_printContainingFiles (pfile);
00360 
00361   ip = cppReader_fileBuffer (pfile);
00362 
00363   if (ip != NULL)
00364     {
00365       cppReader_fileLineForMessage (ip->nominal_fname, line, column);
00366     }
00367 
00368   cppReader_message (pfile, 0, msg);
00369   closeMessage ();
00370 }
00371 
00372 void
00373 cppReader_pedwarnWithLine (cppReader *pfile, int line, int column,
00374                        /*@only@*/ cstring msg)
00375 {
00376   if (CPPOPTIONS (pfile)->pedantic_errors)
00377     {
00378       cppReader_errorWithLine (pfile, column, line, msg);
00379     }
00380   else
00381     {
00382       cppReader_warningWithLine (pfile, line, column, msg);
00383     }
00384 }
00385 
00386 void cppReader_perrorWithName (cppReader *pfile, cstring name)
00387 {
00388   cppReader_message (pfile, 1, 
00389                      message ("preprocessing: %s: %s",
00390                               name, lldecodeerror (errno)));
00391 }
00392 
00393 
00394 
00395 
00396 
00397 
00398 
00399 

Generated at Fri Nov 3 18:57:39 2000 for LCLint by doxygen1.2.3 written by Dimitri van Heesch, © 1997-2000