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 .
29 char *outptr
= outbuf
;
32 struct token nltoken
= {NL
, 0, 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 anymacros
|= gettokens(trp
, 1);
80 if (trp
->tp
->type
== END
)
84 if (cursource
->ifdepth
)
86 "Unterminated conditional in #include");
88 cursource
->line
+= cursource
->lineinc
;
95 error(ERROR
, "Unterminated #if/#ifdef/#ifndef");
98 if (trp
->tp
->type
== SHARP
)
104 if (!skipping
&& anymacros
)
105 expandrow(trp
, NULL
);
110 cursource
->line
+= cursource
->lineinc
;
111 if (cursource
->lineinc
> 1)
120 control(Tokenrow
* trp
)
126 if (tp
->type
!= NAME
)
128 if (tp
->type
== NUMBER
)
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
);
144 if (--ifdepth
< skipping
)
146 --cursource
->ifdepth
;
153 if (++ifdepth
>= NIF
)
154 error(FATAL
, "#if too deeply nested");
155 ++cursource
->ifdepth
;
160 if (ifdepth
<= skipping
)
176 if (tp
->type
!= NAME
|| trp
->lp
- trp
->bp
!= 4)
178 error(ERROR
, "Syntax error in #undef");
181 if ((np
= lookup(tp
, 0)) != NULL
)
183 np
->flag
&= ~ISDEFINED
;
188 error(INFO
, "Macro deletion of %s(%r)", np
->name
, np
->ap
);
190 error(INFO
, "Macro deletion of %s", np
->name
);
197 for (tp
= trp
->tp
- 1; ((tp
->type
!= NL
) && (tp
< trp
->lp
)); tp
++)
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;
217 error(ERROR
, "#elif with no #if");
220 if (ifsatisfied
[ifdepth
] == 2)
221 error(ERROR
, "#elif after #else");
222 if (eval(trp
, np
->val
))
224 if (ifsatisfied
[ifdepth
])
229 ifsatisfied
[ifdepth
] = 1;
237 if (ifdepth
== 0 || cursource
->ifdepth
== 0)
239 error(ERROR
, "#else with no #if");
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;
251 if (ifdepth
== 0 || cursource
->ifdepth
== 0)
253 error(ERROR
, "#endif with no #if");
257 --cursource
->ifdepth
;
258 if (trp
->lp
- trp
->bp
!= 3)
259 error(WARNING
, "Syntax error in #endif");
264 error(WARNING
, "#error directive: %r", trp
);
269 expandrow(trp
, "<line>");
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");
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");
283 if (tp
+ 1 < trp
->lp
)
284 cursource
->filename
= (char *) newstring(tp
->t
+ 1, tp
->len
- 2, 0);
288 error(ERROR
, "Bad syntax for control line");
292 doinclude(trp
, -1, 1);
297 doinclude(trp
, -1, 0);
302 doinclude(trp
, cursource
->pathdepth
, 0);
311 error(ERROR
, "Preprocessor control `%t' not yet implemented", tp
);
319 domalloc(size_t size
)
321 void *p
= malloc(size
);
324 error(FATAL
, "Out of memory from malloc");
335 error(enum errtype type
, char *string
,...)
344 fprintf(stderr
, "cpp: ");
345 for (s
= cursource
; s
; s
= s
->next
)
347 fprintf(stderr
, "%s:%d ", s
->filename
, s
->line
);
348 va_start(ap
, string
);
349 for (ep
= string
; *ep
; ep
++)
357 c
= (char) va_arg(ap
, int);
358 fprintf(stderr
, "%c", c
);
362 cp
= va_arg(ap
, char *);
363 fprintf(stderr
, "%s", cp
);
368 fprintf(stderr
, "%d", i
);
372 tp
= va_arg(ap
, Token
*);
373 fprintf(stderr
, "%.*s", (int)tp
->len
, tp
->t
);
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
)
382 fprintf(stderr
, "%.*s", (int)tp
->len
, tp
->t
);
403 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */