2 ** Splint - annotation-assisted static program checker
3 ** Copyright (C) 1994-2002 University of Virginia,
4 ** Massachusetts Institute of Technology
6 ** This program is free software; you can redistribute it and/or modify it
7 ** under the terms of the GNU General Public License as published by the
8 ** Free Software Foundation; either version 2 of the License, or (at your
9 ** option) any later version.
11 ** This program is distributed in the hope that it will be useful, but
12 ** WITHOUT ANY WARRANTY; without even the implied warranty of
13 ** MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 ** General Public License for more details.
16 ** The GNU General Public License is available from http://www.gnu.org/ or
17 ** the Free Software Foundation, Inc., 59 Temple Place - Suite 330, Boston,
18 ** MA 02111-1307, USA.
20 ** For information on splint: info@splint.org
21 ** To report a bug: splint-bug@splint.org
22 ** For more information: http://www.splint.org
27 /* CPP main program, using CPP Library.
28 Copyright (C) 1995 Free Software Foundation, Inc.
29 Written by Per Bothner, 1994-95.
31 This program is free software; you can redistribute it and/or modify it
32 under the terms of the GNU General Public License as published by the
33 Free Software Foundation; either version 2, or (at your option) any
36 This program is distributed in the hope that it will be useful,
37 but WITHOUT ANY WARRANTY; without even the implied warranty of
38 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
39 GNU General Public License for more details.
41 You should have received a copy of the GNU General Public License
42 along with this program; if not, write to the Free Software
43 Foundation, 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
45 In other words, you are welcome to use, share and improve this program.
46 You are forbidden to forbid anyone else to use, share and improve
47 what you give them. Help stamp out software-hoarding! */
49 # include "splintMacros.nf"
53 # include "cpperror.h"
61 extern /*@external@*/ /*@observer@*/ char *getenv (const char *);
70 /* More 'friendly' abort that prints the line and file.
71 config.h can #define abort fancy_abort if you like that sort of thing. */
76 fatal ("Internal gcc abort.");
80 void cppReader_initMod ()
82 struct cppOptions *opts = (struct cppOptions *) dmalloc (sizeof (*opts));
84 cpplib_init (&g_cppState);
85 llassert (g_cppState.opts == NULL);
86 g_cppState.opts = opts;
88 cppOptions_init (opts);
89 /*@-compdef@*/ /* g_cppState is not yet innitialized */
92 void cppReader_destroyMod ()
93 /*@globals killed g_cppState@*/
95 cppCleanup (&g_cppState);
98 void cppReader_initialize ()
100 cpplib_initializeReader (&g_cppState);
103 int cppProcess (/*@dependent@*/ cstring infile,
104 /*@dependent@*/ cstring outfile)
107 struct cppOptions *opts = CPPOPTIONS (&g_cppState);
109 opts->out_fname = outfile;
110 opts->in_fname = infile;
111 opts->out_fname = outfile;
113 if (cpplib_fatalErrors (&g_cppState))
118 g_cppState.show_column = TRUE;
120 if (cppReader_startProcess (&g_cppState, opts->in_fname) == 0)
125 ofile = fileTable_createFile (context_fileTable (), outfile);
129 fileTable_noDelete (context_fileTable (), outfile);
131 llfatalerror (message ("Cannot create temporary file for "
132 "pre-processor output. Trying to "
133 "open: %s. Use -tmpdir to change "
134 "the directory for temporary files.",
142 llassert (g_cppState.token_buffer != NULL);
144 if (!opts->no_output)
146 DPRINTF (("Writing: %s", cstring_copyLength (g_cppState.token_buffer, cpplib_getWritten (&g_cppState))));
148 (void) fwrite (g_cppState.token_buffer, (size_t) 1,
149 cpplib_getWritten (&g_cppState), ofile);
152 cppReader_setWritten (&g_cppState, 0);
153 kind = cpplib_getToken (&g_cppState);
159 cppReader_finish (&g_cppState);
160 check (fileTable_closeFile (context_fileTable (), ofile));
162 /* Restore the original definition table. */
164 if (!context_getFlag (FLG_SINGLEINCLUDE))
166 cppReader_restoreHashtab ();
170 /* Undefine everything from this file! */
172 if (g_cppState.errors != 0) {
179 void cppAddIncludeDir (cstring dir)
182 ** Add the -I- code. This code provided by Robin Watts <Robin.Watts@wss.co.uk>
185 DPRINTF (("Adding include: %s", dir));
187 if (cstring_equalLit (dir, "-I-"))
189 struct cppOptions *opts = CPPOPTIONS (&g_cppState);
190 opts->ignore_srcdir = TRUE;
194 /* -I option (Add directory to include path) */
195 struct file_name_list *dirtmp = (struct file_name_list *) dmalloc (sizeof (*dirtmp));
197 llassert (cstring_firstChar (dir) == 'I');
198 dir = cstring_suffix (dir, 1);
200 DPRINTF (("Add include: %s", dir));
202 dirtmp->next = 0; /* New one goes on the end */
203 dirtmp->control_macro = 0;
204 dirtmp->c_system_include_path = FALSE;
206 /* This copy is necessary...but shouldn't be? */
208 dirtmp->fname = cstring_copy (dir);
211 dirtmp->got_name_map = FALSE;
212 cppReader_addIncludeChain (&g_cppState, dirtmp);
216 void cppDoDefine (cstring str)
218 cppBuffer *tbuf = g_cppState.buffer;
220 g_cppState.buffer = NULL;
221 cppReader_define (&g_cppState, cstring_toCharsSafe (str));
222 g_cppState.buffer = tbuf;
225 void cppDoUndefine (cstring str)
229 char *buf = cstring_toCharsSafe (str);
231 sym_length = cppReader_checkMacroName (&g_cppState, buf,
232 cstring_makeLiteralTemp ("macro"));
234 while ((hp = cpphash_lookup (buf, size_toInt (sym_length), -1)) != NULL)
236 /*@-exposetrans@*/ /*@-dependenttrans@*/
237 cppReader_deleteMacro (hp);
238 /*@=exposetrans@*/ /*@=dependenttrans@*/
242 void cppReader_saveDefinitions ()
244 if (!context_getFlag (FLG_SINGLEINCLUDE))
246 cppReader_saveHashtab ();