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
)
54 // coverity[tainted_string] - build time test tool
65 void process(Tokenrow
* trp
)
71 if (trp
->tp
>= trp
->lp
)
73 trp
->tp
= trp
->lp
= trp
->bp
;
75 // coverity[overrun-buffer-arg: FALSE] - a multiple of trp->max is allocated, not trp->max itself
76 anymacros
|= gettokens(trp
, 1);
79 if (trp
->tp
->type
== END
)
83 if (cursource
->ifdepth
)
84 error(ERROR
, "Unterminated conditional in #include");
86 cursource
->line
+= cursource
->lineinc
;
93 error(ERROR
, "Unterminated #if/#ifdef/#ifndef");
96 if (trp
->tp
->type
== SHARP
)
101 else if (!skipping
&& anymacros
)
102 expandrow(trp
, NULL
);
107 cursource
->line
+= cursource
->lineinc
;
108 if (cursource
->lineinc
> 1 && !Pflag
)
113 void control(Tokenrow
* trp
)
119 if (tp
->type
!= NAME
)
121 if (tp
->type
== NUMBER
)
124 error(ERROR
, "Unidentifiable control line");
125 return; /* else empty line */
128 if (np
== NULL
|| ((np
->flag
& ISKW
) == 0 && !skipping
))
130 error(WARNING
, "Unknown preprocessor control %t", tp
);
138 if (--ifdepth
< skipping
)
140 --cursource
->ifdepth
;
147 if (++ifdepth
>= NIF
)
148 error(FATAL
, "#if too deeply nested");
149 ++cursource
->ifdepth
;
154 if (ifdepth
<= skipping
)
170 if (tp
->type
!= NAME
|| trp
->lp
- trp
->bp
!= 4)
172 error(ERROR
, "Syntax error in #undef");
175 if ((np
= lookup(tp
, 0)) != NULL
)
177 np
->flag
&= ~ISDEFINED
;
182 error(INFO
, "Macro deletion of %s(%r)", np
->name
, np
->ap
);
184 error(INFO
, "Macro deletion of %s", np
->name
);
191 for (tp
= trp
->tp
- 1; ((tp
->type
!= NL
) && (tp
< trp
->lp
)); tp
++)
198 if (++ifdepth
>= NIF
)
199 error(FATAL
, "#if too deeply nested");
200 ++cursource
->ifdepth
;
201 ifsatisfied
[ifdepth
] = 0;
202 if (eval(trp
, np
->val
))
203 ifsatisfied
[ifdepth
] = 1;
211 error(ERROR
, "#elif with no #if");
214 if (ifsatisfied
[ifdepth
] == 2)
215 error(ERROR
, "#elif after #else");
216 if (eval(trp
, np
->val
))
218 if (ifsatisfied
[ifdepth
])
223 ifsatisfied
[ifdepth
] = 1;
231 if (ifdepth
== 0 || cursource
->ifdepth
== 0)
233 error(ERROR
, "#else with no #if");
236 if (ifsatisfied
[ifdepth
] == 2)
237 error(ERROR
, "#else after #else");
238 if (trp
->lp
- trp
->bp
!= 3)
239 error(ERROR
, "Syntax error in #else");
240 skipping
= ifsatisfied
[ifdepth
] ? ifdepth
: 0;
241 ifsatisfied
[ifdepth
] = 2;
245 if (ifdepth
== 0 || cursource
->ifdepth
== 0)
247 error(ERROR
, "#endif with no #if");
251 --cursource
->ifdepth
;
252 if (trp
->lp
- trp
->bp
!= 3)
253 error(WARNING
, "Syntax error in #endif");
258 error(WARNING
, "#error directive: %r", trp
);
263 expandrow(trp
, "<line>");
266 if (tp
+ 1 >= trp
->lp
|| tp
->type
!= NUMBER
|| tp
+ 3 < trp
->lp
267 || (tp
+ 3 == trp
->lp
&& ((tp
+ 1)->type
!= STRING
|| *(tp
+ 1)->t
== 'L')))
269 error(ERROR
, "Syntax error in #line");
272 cursource
->line
= atol((char*)tp
->t
) - 1;
273 if (cursource
->line
< 0 || cursource
->line
>= 32768)
274 error(WARNING
, "#line specifies number out of range");
276 if (tp
+ 1 < trp
->lp
)
277 cursource
->filename
= (char*)newstring(tp
->t
+ 1, tp
->len
- 2, 0);
281 error(ERROR
, "Bad syntax for control line");
285 doinclude(trp
, -1, 1);
290 doinclude(trp
, -1, 0);
295 doinclude(trp
, cursource
->pathdepth
, 0);
304 error(ERROR
, "Preprocessor control `%t' not yet implemented", tp
);
311 void* domalloc(size_t size
)
313 void* p
= malloc(size
);
316 error(FATAL
, "Out of memory from malloc");
320 void dofree(void* p
) { free(p
); }
322 void error(enum errtype type
, char* string
, ...)
331 fprintf(stderr
, "cpp: ");
332 for (s
= cursource
; s
; s
= s
->next
)
334 fprintf(stderr
, "%s:%d ", s
->filename
, s
->line
);
335 va_start(ap
, string
);
336 for (ep
= string
; *ep
; ep
++)
343 c
= (char)va_arg(ap
, int);
344 fprintf(stderr
, "%c", c
);
348 cp
= va_arg(ap
, char*);
349 fprintf(stderr
, "%s", cp
);
354 fprintf(stderr
, "%d", i
);
358 tp
= va_arg(ap
, Token
*);
359 fprintf(stderr
, "%.*s", (int)tp
->len
, tp
->t
);
363 trp
= va_arg(ap
, Tokenrow
*);
364 for (tp
= trp
->tp
; tp
< trp
->lp
&& tp
->type
!= NL
; tp
++)
366 if (tp
> trp
->tp
&& tp
->wslen
)
368 fprintf(stderr
, "%.*s", (int)tp
->len
, tp
->t
);
389 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */