]> andersk Git - splint.git/blame - src/cppmain.c
noexpand always false.
[splint.git] / src / cppmain.c
CommitLineData
616915dd 1/*
11db3170 2** Splint - annotation-assisted static program checker
c59f5181 3** Copyright (C) 1994-2003 University of Virginia,
616915dd 4** Massachusetts Institute of Technology
5**
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.
10**
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.
15**
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.
19**
155af98d 20** For information on splint: info@splint.org
21** To report a bug: splint-bug@splint.org
11db3170 22** For more information: http://www.splint.org
616915dd 23*/
24/*
25** cppmain.c
26*/
27/* CPP main program, using CPP Library.
28 Copyright (C) 1995 Free Software Foundation, Inc.
29 Written by Per Bothner, 1994-95.
30
31This program is free software; you can redistribute it and/or modify it
32under the terms of the GNU General Public License as published by the
33Free Software Foundation; either version 2, or (at your option) any
34later version.
35
36This program is distributed in the hope that it will be useful,
37but WITHOUT ANY WARRANTY; without even the implied warranty of
38MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
39GNU General Public License for more details.
40
41You should have received a copy of the GNU General Public License
42along with this program; if not, write to the Free Software
43Foundation, 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
44
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! */
48
1b8ae690 49# include "splintMacros.nf"
b73d1009 50# include "basic.h"
616915dd 51# include "cpplib.h"
52# include "cpphash.h"
53# include "cpperror.h"
54# include "llmain.h"
4dd72714 55# include "osd.h"
616915dd 56
57# include <stdio.h>
58
59# ifdef WIN32
60/*@-ansireserved@*/
61extern /*@external@*/ /*@observer@*/ char *getenv (const char *);
62/*@=ansireserved@*/
63# endif
64
65/* char *progname; */
66
67cppReader g_cppState;
68
69#ifdef abort
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. */
72
73void
74fancy_abort ()
75{
76 fatal ("Internal gcc abort.");
77}
78#endif
79
80void cppReader_initMod ()
81{
82 struct cppOptions *opts = (struct cppOptions *) dmalloc (sizeof (*opts));
83
3e3ec469 84 cpplib_init (&g_cppState);
616915dd 85 llassert (g_cppState.opts == NULL);
86 g_cppState.opts = opts;
87
88 cppOptions_init (opts);
89 /*@-compdef@*/ /* g_cppState is not yet innitialized */
90} /*@=compdef@*/
91
6fcd0b1e 92void cppReader_destroyMod ()
93 /*@globals killed g_cppState@*/
94{
95 cppCleanup (&g_cppState);
96}
97
616915dd 98void cppReader_initialize ()
99{
3e3ec469 100 cpplib_initializeReader (&g_cppState);
616915dd 101}
102
103int cppProcess (/*@dependent@*/ cstring infile,
104 /*@dependent@*/ cstring outfile)
105{
106 FILE *ofile;
107 struct cppOptions *opts = CPPOPTIONS (&g_cppState);
108
109 opts->out_fname = outfile;
110 opts->in_fname = infile;
111 opts->out_fname = outfile;
112
3e3ec469 113 if (cpplib_fatalErrors (&g_cppState))
616915dd 114 {
115 llexit (LLFAILURE);
116 }
117
118 g_cppState.show_column = TRUE;
119
120 if (cppReader_startProcess (&g_cppState, opts->in_fname) == 0)
121 {
122 llexit (LLFAILURE);
123 }
124
d5047b91 125 ofile = fileTable_createFile (context_fileTable (), outfile);
616915dd 126
127 if (ofile == NULL)
128 {
129 fileTable_noDelete (context_fileTable (), outfile);
4dd72714 130 osd_setTempError ();
616915dd 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.",
135 outfile));
136 }
137
138 for (;;)
139 {
140 enum cpp_token kind;
141
142 llassert (g_cppState.token_buffer != NULL);
143
144 if (!opts->no_output)
145 {
ce7034f0 146 DPRINTF (("Writing: %s", cstring_copyLength (g_cppState.token_buffer, cpplib_getWritten (&g_cppState))));
147
616915dd 148 (void) fwrite (g_cppState.token_buffer, (size_t) 1,
3e3ec469 149 cpplib_getWritten (&g_cppState), ofile);
616915dd 150 }
151
152 cppReader_setWritten (&g_cppState, 0);
3e3ec469 153 kind = cpplib_getToken (&g_cppState);
616915dd 154
155 if (kind == CPP_EOF)
156 break;
157 }
158
159 cppReader_finish (&g_cppState);
dfd82dce 160 check (fileTable_closeFile (context_fileTable (), ofile));
616915dd 161
b37cf05e 162 /* Restore the original definition table. */
616915dd 163
164 if (!context_getFlag (FLG_SINGLEINCLUDE))
165 {
166 cppReader_restoreHashtab ();
167 }
168
169
170 /* Undefine everything from this file! */
171
172 if (g_cppState.errors != 0) {
173 return -1;
174 }
175
176 return 0;
177}
178
1d239d69 179void cppAddIncludeDir (cstring dir)
616915dd 180{
1d239d69 181 /* evans 2001-08-26
182 ** Add the -I- code. This code provided by Robin Watts <Robin.Watts@wss.co.uk>
183 */
616915dd 184
140c27a8 185 DPRINTF (("Adding include: %s", dir));
186
1d239d69 187 if (cstring_equalLit (dir, "-I-"))
188 {
189 struct cppOptions *opts = CPPOPTIONS (&g_cppState);
190 opts->ignore_srcdir = TRUE;
191 }
192 else
193 {
1d239d69 194 struct file_name_list *dirtmp = (struct file_name_list *) dmalloc (sizeof (*dirtmp));
195
196 DPRINTF (("Add include: %s", dir));
140c27a8 197
1d239d69 198 dirtmp->next = 0; /* New one goes on the end */
199 dirtmp->control_macro = 0;
200 dirtmp->c_system_include_path = FALSE;
201
202 /* This copy is necessary...but shouldn't be? */
203 /*@-onlytrans@*/
204 dirtmp->fname = cstring_copy (dir);
205 /*@=onlytrans@*/
206
207 dirtmp->got_name_map = FALSE;
208 cppReader_addIncludeChain (&g_cppState, dirtmp);
209 }
616915dd 210}
211
212void cppDoDefine (cstring str)
213{
214 cppBuffer *tbuf = g_cppState.buffer;
215
216 g_cppState.buffer = NULL;
217 cppReader_define (&g_cppState, cstring_toCharsSafe (str));
218 g_cppState.buffer = tbuf;
219}
220
221void cppDoUndefine (cstring str)
222{
abd7f895 223 size_t sym_length;
cd7d9b17 224 hashNode hp;
616915dd 225 char *buf = cstring_toCharsSafe (str);
226
227 sym_length = cppReader_checkMacroName (&g_cppState, buf,
abd7f895 228 cstring_makeLiteralTemp ("macro"));
616915dd 229
abd7f895 230 while ((hp = cpphash_lookup (buf, size_toInt (sym_length), -1)) != NULL)
616915dd 231 {
232 /*@-exposetrans@*/ /*@-dependenttrans@*/
233 cppReader_deleteMacro (hp);
234 /*@=exposetrans@*/ /*@=dependenttrans@*/
235 }
236}
237
238void cppReader_saveDefinitions ()
239{
240 if (!context_getFlag (FLG_SINGLEINCLUDE))
241 {
242 cppReader_saveHashtab ();
243 }
244}
This page took 0.82705 seconds and 5 git commands to generate.