]> andersk Git - splint.git/blame - src/cppmain.c
Updated README version number. (Testing sourceforge)
[splint.git] / src / cppmain.c
CommitLineData
616915dd 1/*
2** LCLint - annotation-assisted static program checker
28bf4b0b 3** Copyright (C) 1994-2001 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**
20** For information on lclint: lclint-request@cs.virginia.edu
21** To report a bug: lclint-bug@cs.virginia.edu
22** For more information: http://lclint.cs.virginia.edu
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
49# include "lclintMacros.nf"
50# include "llbasic.h"
616915dd 51# include "cpplib.h"
52# include "cpphash.h"
53# include "cpperror.h"
54# include "llmain.h"
55
56# include <stdio.h>
57
58# ifdef WIN32
59/*@-ansireserved@*/
60extern /*@external@*/ /*@observer@*/ char *getenv (const char *);
61/*@=ansireserved@*/
62# endif
63
64/* char *progname; */
65
66cppReader g_cppState;
67
68#ifdef abort
69/* More 'friendly' abort that prints the line and file.
70 config.h can #define abort fancy_abort if you like that sort of thing. */
71
72void
73fancy_abort ()
74{
75 fatal ("Internal gcc abort.");
76}
77#endif
78
79void cppReader_initMod ()
80{
81 struct cppOptions *opts = (struct cppOptions *) dmalloc (sizeof (*opts));
82
83 cppReader_init (&g_cppState);
84 llassert (g_cppState.opts == NULL);
85 g_cppState.opts = opts;
86
87 cppOptions_init (opts);
88 /*@-compdef@*/ /* g_cppState is not yet innitialized */
89} /*@=compdef@*/
90
91void cppReader_initialize ()
92{
93 cppReader_initializeReader (&g_cppState);
94}
95
96int cppProcess (/*@dependent@*/ cstring infile,
97 /*@dependent@*/ cstring outfile)
98{
99 FILE *ofile;
100 struct cppOptions *opts = CPPOPTIONS (&g_cppState);
101
102 opts->out_fname = outfile;
103 opts->in_fname = infile;
104 opts->out_fname = outfile;
105
106 if (cppFatalErrors (&g_cppState))
107 {
108 llexit (LLFAILURE);
109 }
110
111 g_cppState.show_column = TRUE;
112
113 if (cppReader_startProcess (&g_cppState, opts->in_fname) == 0)
114 {
115 llexit (LLFAILURE);
116 }
117
dfd82dce 118 ofile = fileTable_openFile (context_fileTable (), outfile, "w");
616915dd 119
120 if (ofile == NULL)
121 {
122 fileTable_noDelete (context_fileTable (), outfile);
123 llfatalerror (message ("Cannot create temporary file for "
124 "pre-processor output. Trying to "
125 "open: %s. Use -tmpdir to change "
126 "the directory for temporary files.",
127 outfile));
128 }
129
130 for (;;)
131 {
132 enum cpp_token kind;
133
134 llassert (g_cppState.token_buffer != NULL);
135
136 if (!opts->no_output)
137 {
138 (void) fwrite (g_cppState.token_buffer, (size_t) 1,
139 cppReader_getWritten (&g_cppState), ofile);
140 }
141
142 cppReader_setWritten (&g_cppState, 0);
143 kind = cppGetToken (&g_cppState);
144
145 if (kind == CPP_EOF)
146 break;
147 }
148
149 cppReader_finish (&g_cppState);
dfd82dce 150 check (fileTable_closeFile (context_fileTable (), ofile));
616915dd 151
b37cf05e 152 /* Restore the original definition table. */
616915dd 153
154 if (!context_getFlag (FLG_SINGLEINCLUDE))
155 {
156 cppReader_restoreHashtab ();
157 }
158
159
160 /* Undefine everything from this file! */
161
162 if (g_cppState.errors != 0) {
163 return -1;
164 }
165
166 return 0;
167}
168
1d239d69 169void cppAddIncludeDir (cstring dir)
616915dd 170{
1d239d69 171 /* evans 2001-08-26
172 ** Add the -I- code. This code provided by Robin Watts <Robin.Watts@wss.co.uk>
173 */
616915dd 174
1d239d69 175 if (cstring_equalLit (dir, "-I-"))
176 {
177 struct cppOptions *opts = CPPOPTIONS (&g_cppState);
178 opts->ignore_srcdir = TRUE;
179 }
180 else
181 {
182 /* -I option (Add directory to include path) */
183 struct file_name_list *dirtmp = (struct file_name_list *) dmalloc (sizeof (*dirtmp));
184
185 DPRINTF (("Add include: %s", dir));
186
187 dirtmp->next = 0; /* New one goes on the end */
188 dirtmp->control_macro = 0;
189 dirtmp->c_system_include_path = FALSE;
190
191 /* This copy is necessary...but shouldn't be? */
192 /*@-onlytrans@*/
193 dirtmp->fname = cstring_copy (dir);
194 /*@=onlytrans@*/
195
196 dirtmp->got_name_map = FALSE;
197 cppReader_addIncludeChain (&g_cppState, dirtmp);
198 }
616915dd 199}
200
201void cppDoDefine (cstring str)
202{
203 cppBuffer *tbuf = g_cppState.buffer;
204
205 g_cppState.buffer = NULL;
206 cppReader_define (&g_cppState, cstring_toCharsSafe (str));
207 g_cppState.buffer = tbuf;
208}
209
210void cppDoUndefine (cstring str)
211{
212 int sym_length;
cd7d9b17 213 hashNode hp;
616915dd 214 char *buf = cstring_toCharsSafe (str);
215
216 sym_length = cppReader_checkMacroName (&g_cppState, buf,
217 cstring_makeLiteralTemp ("macro"));
218
219 while ((hp = cppReader_lookup (buf, sym_length, -1)) != NULL)
220 {
221 /*@-exposetrans@*/ /*@-dependenttrans@*/
222 cppReader_deleteMacro (hp);
223 /*@=exposetrans@*/ /*@=dependenttrans@*/
224 }
225}
226
227void cppReader_saveDefinitions ()
228{
229 if (!context_getFlag (FLG_SINGLEINCLUDE))
230 {
231 cppReader_saveHashtab ();
232 }
233}
This page took 0.072428 seconds and 5 git commands to generate.