]>
Commit | Line | Data |
---|---|---|
616915dd | 1 | /* |
11db3170 | 2 | ** Splint - annotation-assisted static program checker |
77d37419 | 3 | ** Copyright (C) 1994-2002 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 | ||
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 | |
34 | later version. | |
35 | ||
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. | |
40 | ||
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. | |
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" |
616915dd | 50 | # include "llbasic.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@*/ | |
61 | extern /*@external@*/ /*@observer@*/ char *getenv (const char *); | |
62 | /*@=ansireserved@*/ | |
63 | # endif | |
64 | ||
65 | /* char *progname; */ | |
66 | ||
67 | cppReader 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 | ||
73 | void | |
74 | fancy_abort () | |
75 | { | |
76 | fatal ("Internal gcc abort."); | |
77 | } | |
78 | #endif | |
79 | ||
80 | void 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 | ||
92 | void cppReader_initialize () | |
93 | { | |
3e3ec469 | 94 | cpplib_initializeReader (&g_cppState); |
616915dd | 95 | } |
96 | ||
97 | int cppProcess (/*@dependent@*/ cstring infile, | |
98 | /*@dependent@*/ cstring outfile) | |
99 | { | |
100 | FILE *ofile; | |
101 | struct cppOptions *opts = CPPOPTIONS (&g_cppState); | |
102 | ||
103 | opts->out_fname = outfile; | |
104 | opts->in_fname = infile; | |
105 | opts->out_fname = outfile; | |
106 | ||
3e3ec469 | 107 | if (cpplib_fatalErrors (&g_cppState)) |
616915dd | 108 | { |
109 | llexit (LLFAILURE); | |
110 | } | |
111 | ||
112 | g_cppState.show_column = TRUE; | |
113 | ||
114 | if (cppReader_startProcess (&g_cppState, opts->in_fname) == 0) | |
115 | { | |
116 | llexit (LLFAILURE); | |
117 | } | |
118 | ||
d5047b91 | 119 | ofile = fileTable_createFile (context_fileTable (), outfile); |
616915dd | 120 | |
121 | if (ofile == NULL) | |
122 | { | |
123 | fileTable_noDelete (context_fileTable (), outfile); | |
4dd72714 | 124 | osd_setTempError (); |
616915dd | 125 | llfatalerror (message ("Cannot create temporary file for " |
126 | "pre-processor output. Trying to " | |
127 | "open: %s. Use -tmpdir to change " | |
128 | "the directory for temporary files.", | |
129 | outfile)); | |
130 | } | |
131 | ||
132 | for (;;) | |
133 | { | |
134 | enum cpp_token kind; | |
135 | ||
136 | llassert (g_cppState.token_buffer != NULL); | |
137 | ||
138 | if (!opts->no_output) | |
139 | { | |
140 | (void) fwrite (g_cppState.token_buffer, (size_t) 1, | |
3e3ec469 | 141 | cpplib_getWritten (&g_cppState), ofile); |
616915dd | 142 | } |
143 | ||
144 | cppReader_setWritten (&g_cppState, 0); | |
3e3ec469 | 145 | kind = cpplib_getToken (&g_cppState); |
616915dd | 146 | |
147 | if (kind == CPP_EOF) | |
148 | break; | |
149 | } | |
150 | ||
151 | cppReader_finish (&g_cppState); | |
dfd82dce | 152 | check (fileTable_closeFile (context_fileTable (), ofile)); |
616915dd | 153 | |
b37cf05e | 154 | /* Restore the original definition table. */ |
616915dd | 155 | |
156 | if (!context_getFlag (FLG_SINGLEINCLUDE)) | |
157 | { | |
158 | cppReader_restoreHashtab (); | |
159 | } | |
160 | ||
161 | ||
162 | /* Undefine everything from this file! */ | |
163 | ||
164 | if (g_cppState.errors != 0) { | |
165 | return -1; | |
166 | } | |
167 | ||
168 | return 0; | |
169 | } | |
170 | ||
1d239d69 | 171 | void cppAddIncludeDir (cstring dir) |
616915dd | 172 | { |
1d239d69 | 173 | /* evans 2001-08-26 |
174 | ** Add the -I- code. This code provided by Robin Watts <Robin.Watts@wss.co.uk> | |
175 | */ | |
616915dd | 176 | |
1d239d69 | 177 | if (cstring_equalLit (dir, "-I-")) |
178 | { | |
179 | struct cppOptions *opts = CPPOPTIONS (&g_cppState); | |
180 | opts->ignore_srcdir = TRUE; | |
181 | } | |
182 | else | |
183 | { | |
184 | /* -I option (Add directory to include path) */ | |
185 | struct file_name_list *dirtmp = (struct file_name_list *) dmalloc (sizeof (*dirtmp)); | |
186 | ||
187 | DPRINTF (("Add include: %s", dir)); | |
188 | ||
189 | dirtmp->next = 0; /* New one goes on the end */ | |
190 | dirtmp->control_macro = 0; | |
191 | dirtmp->c_system_include_path = FALSE; | |
192 | ||
193 | /* This copy is necessary...but shouldn't be? */ | |
194 | /*@-onlytrans@*/ | |
195 | dirtmp->fname = cstring_copy (dir); | |
196 | /*@=onlytrans@*/ | |
197 | ||
198 | dirtmp->got_name_map = FALSE; | |
199 | cppReader_addIncludeChain (&g_cppState, dirtmp); | |
200 | } | |
616915dd | 201 | } |
202 | ||
203 | void cppDoDefine (cstring str) | |
204 | { | |
205 | cppBuffer *tbuf = g_cppState.buffer; | |
206 | ||
207 | g_cppState.buffer = NULL; | |
208 | cppReader_define (&g_cppState, cstring_toCharsSafe (str)); | |
209 | g_cppState.buffer = tbuf; | |
210 | } | |
211 | ||
212 | void cppDoUndefine (cstring str) | |
213 | { | |
abd7f895 | 214 | size_t sym_length; |
cd7d9b17 | 215 | hashNode hp; |
616915dd | 216 | char *buf = cstring_toCharsSafe (str); |
217 | ||
218 | sym_length = cppReader_checkMacroName (&g_cppState, buf, | |
abd7f895 | 219 | cstring_makeLiteralTemp ("macro")); |
616915dd | 220 | |
abd7f895 | 221 | while ((hp = cpphash_lookup (buf, size_toInt (sym_length), -1)) != NULL) |
616915dd | 222 | { |
223 | /*@-exposetrans@*/ /*@-dependenttrans@*/ | |
224 | cppReader_deleteMacro (hp); | |
225 | /*@=exposetrans@*/ /*@=dependenttrans@*/ | |
226 | } | |
227 | } | |
228 | ||
229 | void cppReader_saveDefinitions () | |
230 | { | |
231 | if (!context_getFlag (FLG_SINGLEINCLUDE)) | |
232 | { | |
233 | cppReader_saveHashtab (); | |
234 | } | |
235 | } |