1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
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 .
28 static char outbuf
[OUTS
];
29 char *outptr
= outbuf
;
32 struct token nltoken
= {NL
, 0, 1, (uchar
*) "\n", 0};
43 main(int argc
, char **argv
)
55 // coverity[tainted_string] - build time test tool
67 process(Tokenrow
* trp
)
73 if (trp
->tp
>= trp
->lp
)
75 trp
->tp
= trp
->lp
= trp
->bp
;
77 // coverity[overrun-buffer-arg: FALSE] - a multiple of trp->max is allocated, not trp->max itself
78 anymacros
|= gettokens(trp
, 1);
81 if (trp
->tp
->type
== END
)
85 if (cursource
->ifdepth
)
87 "Unterminated conditional in #include");
89 cursource
->line
+= cursource
->lineinc
;
96 error(ERROR
, "Unterminated #if/#ifdef/#ifndef");
99 if (trp
->tp
->type
== SHARP
)
105 if (!skipping
&& anymacros
)
106 expandrow(trp
, NULL
);
111 cursource
->line
+= cursource
->lineinc
;
112 if (cursource
->lineinc
> 1 && !Pflag
)
118 control(Tokenrow
* trp
)
124 if (tp
->type
!= NAME
)
126 if (tp
->type
== NUMBER
)
129 error(ERROR
, "Unidentifiable control line");
130 return; /* else empty line */
132 if ((np
= lookup(tp
, 0)) == NULL
|| ((np
->flag
& ISKW
) == 0 && !skipping
))
134 error(WARNING
, "Unknown preprocessor control %t", tp
);
142 if (--ifdepth
< skipping
)
144 --cursource
->ifdepth
;
151 if (++ifdepth
>= NIF
)
152 error(FATAL
, "#if too deeply nested");
153 ++cursource
->ifdepth
;
158 if (ifdepth
<= skipping
)
174 if (tp
->type
!= NAME
|| trp
->lp
- trp
->bp
!= 4)
176 error(ERROR
, "Syntax error in #undef");
179 if ((np
= lookup(tp
, 0)) != NULL
)
181 np
->flag
&= ~ISDEFINED
;
186 error(INFO
, "Macro deletion of %s(%r)", np
->name
, np
->ap
);
188 error(INFO
, "Macro deletion of %s", np
->name
);
195 for (tp
= trp
->tp
- 1; ((tp
->type
!= NL
) && (tp
< trp
->lp
)); tp
++)
202 if (++ifdepth
>= NIF
)
203 error(FATAL
, "#if too deeply nested");
204 ++cursource
->ifdepth
;
205 ifsatisfied
[ifdepth
] = 0;
206 if (eval(trp
, np
->val
))
207 ifsatisfied
[ifdepth
] = 1;
215 error(ERROR
, "#elif with no #if");
218 if (ifsatisfied
[ifdepth
] == 2)
219 error(ERROR
, "#elif after #else");
220 if (eval(trp
, np
->val
))
222 if (ifsatisfied
[ifdepth
])
227 ifsatisfied
[ifdepth
] = 1;
235 if (ifdepth
== 0 || cursource
->ifdepth
== 0)
237 error(ERROR
, "#else with no #if");
240 if (ifsatisfied
[ifdepth
] == 2)
241 error(ERROR
, "#else after #else");
242 if (trp
->lp
- trp
->bp
!= 3)
243 error(ERROR
, "Syntax error in #else");
244 skipping
= ifsatisfied
[ifdepth
] ? ifdepth
: 0;
245 ifsatisfied
[ifdepth
] = 2;
249 if (ifdepth
== 0 || cursource
->ifdepth
== 0)
251 error(ERROR
, "#endif with no #if");
255 --cursource
->ifdepth
;
256 if (trp
->lp
- trp
->bp
!= 3)
257 error(WARNING
, "Syntax error in #endif");
262 error(WARNING
, "#error directive: %r", trp
);
267 expandrow(trp
, "<line>");
270 if (tp
+ 1 >= trp
->lp
|| tp
->type
!= NUMBER
|| tp
+ 3 < trp
->lp
271 || (tp
+ 3 == trp
->lp
272 && ((tp
+ 1)->type
!= STRING
|| *(tp
+ 1)->t
== 'L')))
274 error(ERROR
, "Syntax error in #line");
277 cursource
->line
= atol((char *) tp
->t
) - 1;
278 if (cursource
->line
< 0 || cursource
->line
>= 32768)
279 error(WARNING
, "#line specifies number out of range");
281 if (tp
+ 1 < trp
->lp
)
282 cursource
->filename
= (char *) newstring(tp
->t
+ 1, tp
->len
- 2, 0);
286 error(ERROR
, "Bad syntax for control line");
290 doinclude(trp
, -1, 1);
295 doinclude(trp
, -1, 0);
300 doinclude(trp
, cursource
->pathdepth
, 0);
309 error(ERROR
, "Preprocessor control `%t' not yet implemented", tp
);
317 domalloc(size_t size
)
319 void *p
= malloc(size
);
322 error(FATAL
, "Out of memory from malloc");
333 error(enum errtype type
, char *string
,...)
342 fprintf(stderr
, "cpp: ");
343 for (s
= cursource
; s
; s
= s
->next
)
345 fprintf(stderr
, "%s:%d ", s
->filename
, s
->line
);
346 va_start(ap
, string
);
347 for (ep
= string
; *ep
; ep
++)
355 c
= (char) va_arg(ap
, int);
356 fprintf(stderr
, "%c", c
);
360 cp
= va_arg(ap
, char *);
361 fprintf(stderr
, "%s", cp
);
366 fprintf(stderr
, "%d", i
);
370 tp
= va_arg(ap
, Token
*);
371 fprintf(stderr
, "%.*s", (int)tp
->len
, tp
->t
);
375 trp
= va_arg(ap
, Tokenrow
*);
376 for (tp
= trp
->tp
; tp
< trp
->lp
&& tp
->type
!= NL
; tp
++)
378 if (tp
> trp
->tp
&& tp
->wslen
)
380 fprintf(stderr
, "%.*s", (int)tp
->len
, tp
->t
);
401 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */