bump product version to 5.0.4.1
[LibreOffice.git] / soltools / cpp / _cpp.c
blob08520f4a8a7a7bca9681f81090d770d93c6414b8
1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2 /*
3 * This file is part of the LibreOffice project.
5 * This Source Code Form is subject to the terms of the Mozilla Public
6 * License, v. 2.0. If a copy of the MPL was not distributed with this
7 * file, You can obtain one at http://mozilla.org/MPL/2.0/.
9 * This file incorporates work covered by the following license notice:
11 * Licensed to the Apache Software Foundation (ASF) under one or more
12 * contributor license agreements. See the NOTICE file distributed
13 * with this work for additional information regarding copyright
14 * ownership. The ASF licenses this file to you under the Apache
15 * License, Version 2.0 (the "License"); you may not use this file
16 * except in compliance with the License. You may obtain a copy of
17 * the License at http://www.apache.org/licenses/LICENSE-2.0 .
20 #include <stdio.h>
21 #include <stdlib.h>
22 #include <string.h>
23 #include <time.h>
24 #include <stdarg.h>
25 #include "cpp.h"
27 #define OUTS 16384
28 char outbuf[OUTS];
29 char *outptr = outbuf;
30 Source *cursource;
31 int nerrs;
32 struct token nltoken = {NL, 0, 0, 1, (uchar *) "\n", 0};
33 char *curtime;
34 int incdepth;
35 int ifdepth;
36 int ifsatisfied[NIF];
37 int skipping;
39 int
40 #ifdef _WIN32
41 __cdecl
42 #endif // _WIN32
43 main(int argc, char **argv)
46 Tokenrow tr;
47 time_t t;
48 char ebuf[BUFSIZ];
50 setbuf(stderr, ebuf);
51 t = time(NULL);
52 curtime = ctime(&t);
53 maketokenrow(3, &tr);
54 expandlex();
55 // coverity[tainted_string] - build time test tool
56 setup(argc, argv);
57 fixlex();
58 if (!Pflag)
59 genline();
60 process(&tr);
61 flushout();
62 fflush(stderr);
63 exit(nerrs > 0);
66 void
67 process(Tokenrow * trp)
69 int anymacros = 0;
71 for (;;)
73 if (trp->tp >= trp->lp)
75 trp->tp = trp->lp = trp->bp;
76 outptr = outbuf;
77 anymacros |= gettokens(trp, 1);
78 trp->tp = trp->bp;
80 if (trp->tp->type == END)
82 if (--incdepth >= 0)
84 if (cursource->ifdepth)
85 error(ERROR,
86 "Unterminated conditional in #include");
87 unsetsource();
88 cursource->line += cursource->lineinc;
89 trp->tp = trp->lp;
90 if (!Pflag)
91 genline();
92 continue;
94 if (ifdepth)
95 error(ERROR, "Unterminated #if/#ifdef/#ifndef");
96 break;
98 if (trp->tp->type == SHARP)
100 trp->tp += 1;
101 control(trp);
103 else
104 if (!skipping && anymacros)
105 expandrow(trp, NULL);
106 if (skipping)
107 setempty(trp);
108 puttokens(trp);
109 anymacros = 0;
110 cursource->line += cursource->lineinc;
111 if (cursource->lineinc > 1)
113 if (!Pflag)
114 genline();
119 void
120 control(Tokenrow * trp)
122 Nlist *np;
123 Token *tp;
125 tp = trp->tp;
126 if (tp->type != NAME)
128 if (tp->type == NUMBER)
129 goto kline;
130 if (tp->type != NL)
131 error(ERROR, "Unidentifiable control line");
132 return; /* else empty line */
134 if ((np = lookup(tp, 0)) == NULL || ((np->flag & ISKW) == 0 && !skipping))
136 error(WARNING, "Unknown preprocessor control %t", tp);
137 return;
139 if (skipping)
141 switch (np->val)
143 case KENDIF:
144 if (--ifdepth < skipping)
145 skipping = 0;
146 --cursource->ifdepth;
147 setempty(trp);
148 return;
150 case KIFDEF:
151 case KIFNDEF:
152 case KIF:
153 if (++ifdepth >= NIF)
154 error(FATAL, "#if too deeply nested");
155 ++cursource->ifdepth;
156 return;
158 case KELIF:
159 case KELSE:
160 if (ifdepth <= skipping)
161 break;
162 return;
164 default:
165 return;
168 switch (np->val)
170 case KDEFINE:
171 dodefine(trp);
172 break;
174 case KUNDEF:
175 tp += 1;
176 if (tp->type != NAME || trp->lp - trp->bp != 4)
178 error(ERROR, "Syntax error in #undef");
179 break;
181 if ((np = lookup(tp, 0)) != NULL)
183 np->flag &= ~ISDEFINED;
185 if (Mflag)
187 if (np->ap)
188 error(INFO, "Macro deletion of %s(%r)", np->name, np->ap);
189 else
190 error(INFO, "Macro deletion of %s", np->name);
193 break;
195 case KPRAGMA:
196 case KIDENT:
197 for (tp = trp->tp - 1; ((tp->type != NL) && (tp < trp->lp)); tp++)
198 tp->type = UNCLASS;
199 return;
201 case KIFDEF:
202 case KIFNDEF:
203 case KIF:
204 if (++ifdepth >= NIF)
205 error(FATAL, "#if too deeply nested");
206 ++cursource->ifdepth;
207 ifsatisfied[ifdepth] = 0;
208 if (eval(trp, np->val))
209 ifsatisfied[ifdepth] = 1;
210 else
211 skipping = ifdepth;
212 break;
214 case KELIF:
215 if (ifdepth == 0)
217 error(ERROR, "#elif with no #if");
218 return;
220 if (ifsatisfied[ifdepth] == 2)
221 error(ERROR, "#elif after #else");
222 if (eval(trp, np->val))
224 if (ifsatisfied[ifdepth])
225 skipping = ifdepth;
226 else
228 skipping = 0;
229 ifsatisfied[ifdepth] = 1;
232 else
233 skipping = ifdepth;
234 break;
236 case KELSE:
237 if (ifdepth == 0 || cursource->ifdepth == 0)
239 error(ERROR, "#else with no #if");
240 return;
242 if (ifsatisfied[ifdepth] == 2)
243 error(ERROR, "#else after #else");
244 if (trp->lp - trp->bp != 3)
245 error(ERROR, "Syntax error in #else");
246 skipping = ifsatisfied[ifdepth] ? ifdepth : 0;
247 ifsatisfied[ifdepth] = 2;
248 break;
250 case KENDIF:
251 if (ifdepth == 0 || cursource->ifdepth == 0)
253 error(ERROR, "#endif with no #if");
254 return;
256 --ifdepth;
257 --cursource->ifdepth;
258 if (trp->lp - trp->bp != 3)
259 error(WARNING, "Syntax error in #endif");
260 break;
262 case KERROR:
263 trp->tp = tp + 1;
264 error(WARNING, "#error directive: %r", trp);
265 break;
267 case KLINE:
268 trp->tp = tp + 1;
269 expandrow(trp, "<line>");
270 tp = trp->bp + 2;
271 kline:
272 if (tp + 1 >= trp->lp || tp->type != NUMBER || tp + 3 < trp->lp
273 || (tp + 3 == trp->lp
274 && ((tp + 1)->type != STRING || *(tp + 1)->t == 'L')))
276 error(ERROR, "Syntax error in #line");
277 return;
279 cursource->line = atol((char *) tp->t) - 1;
280 if (cursource->line < 0 || cursource->line >= 32768)
281 error(WARNING, "#line specifies number out of range");
282 tp = tp + 1;
283 if (tp + 1 < trp->lp)
284 cursource->filename = (char *) newstring(tp->t + 1, tp->len - 2, 0);
285 return;
287 case KDEFINED:
288 error(ERROR, "Bad syntax for control line");
289 break;
291 case KIMPORT:
292 doinclude(trp, -1, 1);
293 trp->lp = trp->bp;
294 return;
296 case KINCLUDE:
297 doinclude(trp, -1, 0);
298 trp->lp = trp->bp;
299 return;
301 case KINCLUDENEXT:
302 doinclude(trp, cursource->pathdepth, 0);
303 trp->lp = trp->bp;
304 return;
306 case KEVAL:
307 eval(trp, np->val);
308 break;
310 default:
311 error(ERROR, "Preprocessor control `%t' not yet implemented", tp);
312 break;
314 setempty(trp);
315 return;
318 void *
319 domalloc(size_t size)
321 void *p = malloc(size);
323 if (p == NULL)
324 error(FATAL, "Out of memory from malloc");
325 return p;
328 void
329 dofree(void *p)
331 free(p);
334 void
335 error(enum errtype type, char *string,...)
337 va_list ap;
338 char c, *cp, *ep;
339 Token *tp;
340 Tokenrow *trp;
341 Source *s;
342 int i;
344 fprintf(stderr, "cpp: ");
345 for (s = cursource; s; s = s->next)
346 if (*s->filename)
347 fprintf(stderr, "%s:%d ", s->filename, s->line);
348 va_start(ap, string);
349 for (ep = string; *ep; ep++)
351 if (*ep == '%')
353 switch (*++ep)
356 case 'c':
357 c = (char) va_arg(ap, int);
358 fprintf(stderr, "%c", c);
359 break;
361 case 's':
362 cp = va_arg(ap, char *);
363 fprintf(stderr, "%s", cp);
364 break;
366 case 'd':
367 i = va_arg(ap, int);
368 fprintf(stderr, "%d", i);
369 break;
371 case 't':
372 tp = va_arg(ap, Token *);
373 fprintf(stderr, "%.*s", (int)tp->len, tp->t);
374 break;
376 case 'r':
377 trp = va_arg(ap, Tokenrow *);
378 for (tp = trp->tp; tp < trp->lp && tp->type != NL; tp++)
380 if (tp > trp->tp && tp->wslen)
381 fputc(' ', stderr);
382 fprintf(stderr, "%.*s", (int)tp->len, tp->t);
384 break;
386 default:
387 fputc(*ep, stderr);
388 break;
391 else
392 fputc(*ep, stderr);
394 va_end(ap);
395 fputc('\n', stderr);
396 if (type == FATAL)
397 exit(1);
398 if (type != WARNING)
399 nerrs = 1;
400 fflush(stderr);
403 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */