2 ** Splint - annotation-assisted static program checker
3 ** Copyright (C) 1994-2002 University of Virginia,
4 ** Massachusetts Institute of Technology
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.
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.
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.
20 ** For information on splint: splint@cs.virginia.edu
21 ** To report a bug: splint-bug@cs.virginia.edu
22 ** For more information: http://www.splint.org
28 ** Based on a C lexer by Nate Osgood
29 ** from hacrat@catfish.lcs.mit.edu Mon Jun 14 13:06:32 1993
32 ** Modified by Herbert 08/19/97:
33 ** - added #include for IBM's OS/2 compiler.
34 ** - fixed weird bug with lookup of tmp files (OS/2 and MSDOS only).
37 ** Modified by Mike Smith
38 ** Corrected missing 'line' in scanf() calls in handleSpecial().
39 ** Without this, I get an error when Splint hits a '#line' directive
40 ** in the pre-pre-processed source files. For safety, I have made these
41 ** conditional on OS2 and MSDOS because I don't understand why noone else
42 ** has seen this problem.
44 ** Modified by Mike Smith, 4th June 1997
45 ** Finally resolved the #line problem. The scanf() calls have been fixed to
46 ** allow the following #line forms:-
48 ** #line 123 "filename"
53 ** The last two are generated by the GNU pre-processor, apparently
64 ULSuffix ({U}{L}|{L}{U})
67 # include "splintMacros.nf"
68 # if defined(OS2) && defined(__IBMC__)
69 /* needed for isatty()... */
74 ** Win32 doesn't have unistd.h
85 # include "cgrammar.h"
86 # include "cgrammar_tokens.h"
89 static bool lastWasString = FALSE;
90 static char savechar = '\0';
93 # define yyinput() (incColumn (), getc (yyin))
95 static /*@owned@*/ cstring lastidprocessed = cstring_undefined;
96 static int lminput (void);
97 static int tokLength = 0;
98 static bool inSpecPart = FALSE;
99 static bool continueLine = FALSE;
101 static int ninput (void);
102 static char processChar (void);
103 static double processFloat (void);
104 static /*@only@*/ exprNode processString (void) ;
105 static /*@only@*/ exprNode processWideString (void) ;
106 static long processDec (void);
107 static long processHex (void);
108 static long processOctal (void);
109 static int processIdentifier (/*@only@*/ cstring)
110 /*@globals undef lastidprocessed@*/ ;
111 static bool processHashIdentifier (/*@only@*/ cstring)
112 /*@globals undef lastidprocessed@*/ ;
114 static int processSpec (int);
115 static bool handleSpecial (char *);
116 static int handleLlSpecial (void);
117 static void handleMacro (void);
118 static bool processMacro (void);
119 static /*@only@*/ cstring makeIdentifier (char *);
121 /* yes, this is exported! */
122 bool g_expectingTypeName = TRUE; /* beginning of file can be type name! */
124 static bool expectingMetaStateName = FALSE;
126 static int returnInt (ctype, long);
127 static int returnFloat (ctype, double);
128 static int returnChar (char);
129 static void setTokLength (int) /*@modifies g_currentloc@*/ ;
130 static void setTokLengthT (size_t) /*@modifies g_currentloc@*/ ;
132 static void advanceLine (void)
139 # define RETURN_INT(c,i) \
140 do { lastWasString = FALSE; \
141 return (returnInt (c, i)); } while (FALSE)
143 # define RETURN_FLOAT(c,f) \
144 do { lastWasString = FALSE; \
145 return (returnFloat (c, f)); \
148 # define RETURN_CHAR(c) \
149 do { lastWasString = FALSE; \
150 return (returnChar (c)); \
153 # define RETURN_TOK(t) \
154 do { yylval.tok = lltok_create (t, fileloc_decColumn (g_currentloc, tokLength)); \
156 lastWasString = FALSE; \
157 return (t); } while (FALSE)
159 # define RETURN_TYPE(t, ct) \
160 do { yylval.ctyp = ct; tokLength = 0; return (t); } while (FALSE)
162 /* don't fileloc_decColumn (g_currentloc, tokLength));
163 the string could have \n's in it!
166 # define RETURN_STRING(c) \
167 do { yylval.expr = exprNode_stringLiteral (c, fileloc_decColumn (g_currentloc, tokLength)); \
169 lastWasString = TRUE; \
170 return (CCONSTANT); } while (FALSE)
172 # define RETURN_EXPR(e) \
173 do { yylval.expr = e; \
175 lastWasString = TRUE; \
176 return (CCONSTANT); } while (FALSE)
180 static void setTokLength (int len)
184 DPRINTF (("Set tok length: %d", len));
187 static void setTokLengthT (size_t len)
189 setTokLength (size_toInt (len));
192 # include "flex.head"
194 /*@-unrecog@*/ /*@i5343@*/
200 "/*" { llfatalerror (cstring_makeLiteral ("Comment in pre-processor output")); }
202 "#"{Letter}({Letter}|{Digit})* {
203 context_saveLocation ();
204 setTokLength (longUnsigned_toInt (mstring_length (yytext)));
206 if (processHashIdentifier (makeIdentifier (yytext + 1)))
210 /* was nothing! */ /*@i32@*/
211 RETURN_STRING (cstring_makeLiteral ("\"\""));
215 RETURN_STRING (cstring_makeLiteral ("\"\""));
220 if (handleSpecial (yytext))
227 "#" { if (handleSpecial (yytext))
229 setTokLength (1); RETURN_TOK (0);
232 "..." { setTokLength (3); RETURN_TOK (CTOK_ELIPSIS); }
233 "break" { setTokLength (5); RETURN_TOK (BREAK); }
234 "case" { setTokLength (4); RETURN_TOK (CASE); }
235 "continue" { setTokLength (8); RETURN_TOK (CONTINUE); }
236 "default" { setTokLength (7); RETURN_TOK (DEFAULT); }
237 "do" { setTokLength (2); RETURN_TOK (DO); }
238 "else" { setTokLength (4); RETURN_TOK (CELSE); }
239 "for" { setTokLength (3); RETURN_TOK (CFOR); }
240 "goto" { setTokLength (4); RETURN_TOK (GOTO); }
241 "if" { setTokLength (2); RETURN_TOK (CIF); }
242 "return" { setTokLength (6); RETURN_TOK (RETURN); }
243 "sizeof" { setTokLength (6); RETURN_TOK (CSIZEOF); }
244 "offsetof" { setTokLength (8); RETURN_TOK (COFFSETOF); }
245 "switch" { setTokLength (6); RETURN_TOK (SWITCH); }
246 "while" { setTokLength (5); RETURN_TOK (WHILE); }
247 "va_arg" { setTokLength (6); RETURN_TOK (VA_ARG); }
248 "va_dcl" { setTokLength (6); RETURN_TOK (VA_DCL); }
250 /* gcc extension...this might not be appropriate */
251 setTokLength (6); RETURN_TOK (QINLINE); }
253 "struct" { setTokLength (6); RETURN_TOK (CSTRUCT); }
254 "typedef" { setTokLength (7); RETURN_TOK (CTYPEDEF); }
256 "union" { setTokLength (5); RETURN_TOK (CUNION); }
257 "enum" { setTokLength (4); RETURN_TOK (CENUM); }
259 "void" { setTokLength (4); RETURN_TYPE (CVOID, ctype_void); }
260 "int" { setTokLength (3); RETURN_TYPE (CINT, ctype_int); }
261 "double" { setTokLength (6); RETURN_TYPE (CDOUBLE, ctype_double); }
262 "char" { setTokLength (4); RETURN_TYPE (CGCHAR, ctype_char); }
263 "float" { setTokLength (5); RETURN_TYPE (CGFLOAT, ctype_float); }
265 "long" { setTokLength (4); RETURN_TOK (QLONG); }
266 "short" { setTokLength (5); RETURN_TOK (QSHORT); }
267 "unsigned" { setTokLength (8); RETURN_TOK (QUNSIGNED); }
268 "signed" { setTokLength (6); RETURN_TOK (QSIGNED); }
270 "volatile" { setTokLength (8); RETURN_TOK (QVOLATILE); }
271 "const" { setTokLength (5); RETURN_TOK (QCONST); }
273 /* some systems expect this! [gack!] */
274 "__const" { setTokLength (7); RETURN_TOK (QCONST); }
276 "extern" { setTokLength (6); RETURN_TOK (QEXTERN); }
277 "auto" { setTokLength (4); RETURN_TOK (QAUTO); }
278 "register" { setTokLength (8); RETURN_TOK (QREGISTER); }
279 "static" { setTokLength (6); RETURN_TOK (QSTATIC); }
281 \"(\\.|[^\\"])*\"([ \t\n]*\"(\\.|[^\\"])*\")* { RETURN_EXPR (processString ()); }
282 L\"(\\.|[^\\"])*\"([ \t\n]*\"(\\.|[^\\"])*\")* { RETURN_EXPR (processWideString ()); }
283 "out" { return (processSpec (QOUT)); }
284 "in" { return (processSpec (QIN)); }
285 "partial" { return (processSpec (QPARTIAL)); }
286 "special" { return (processSpec (QSPECIAL)); }
287 "anytype" { return (processSpec (QANYTYPE)); }
288 "integraltype" { return (processSpec (QINTEGRALTYPE)); }
289 "unsignedintegraltype" { return (processSpec (QUNSIGNEDINTEGRALTYPE)); }
290 "signedintegraltype" { return (processSpec (QSIGNEDINTEGRALTYPE)); }
291 "keep" { return (processSpec (QKEEP)); }
292 "null" { return (processSpec (QNULL)); }
293 "notnull" { return (processSpec (QNOTNULL)); }
294 "isnull" { return (processSpec (QISNULL)); }
295 "truenull" { return (processSpec (QTRUENULL)); }
296 "falsenull" { return (processSpec (QFALSENULL)); }
297 "nullwhentrue" { return (processSpec (QTRUENULL)); }
298 "nullwhenfalse" { return (processSpec (QFALSENULL)); }
299 "relnull" { return (processSpec (QRELNULL)); }
300 "reldef" { return (processSpec (QRELDEF)); }
301 "exposed" { return (processSpec (QEXPOSED)); }
302 "newref" { return (processSpec (QNEWREF)); }
303 "tempref" { return (processSpec (QTEMPREF)); }
304 "killref" { return (processSpec (QKILLREF)); }
305 "refcounted" { return (processSpec (QREFCOUNTED)); }
306 "checked" { return (processSpec (QCHECKED)); }
307 "checkmod" { return (processSpec (QCHECKMOD)); }
308 "checkedstrict" { return (processSpec (QCHECKEDSTRICT)); }
309 "unchecked" { return (processSpec (QUNCHECKED)); }
310 "only" { return (processSpec (QONLY)); }
311 "owned" { return (processSpec (QOWNED)); }
312 "observer" { return (processSpec (QOBSERVER)); }
313 "dependent" { return (processSpec (QDEPENDENT)); }
314 "unused" { return (processSpec (QUNUSED)); }
315 "external" { return (processSpec (QEXTERNAL)); }
316 "sef" { return (processSpec (QSEF)); }
317 "shared" { return (processSpec (QSHARED)); }
318 "yield" { return (processSpec (QYIELD)); }
319 "undef" { return (processSpec (QUNDEF)); }
320 "killed" { return (processSpec (QKILLED)); }
321 "nullterminated" { return (processSpec (QNULLTERMINATED));}
322 "MaxSet" { return (processSpec (QMAXSET));}
323 "MaxRead" { return (processSpec (QMAXREAD));}
324 "maxSet" { return (processSpec (QMAXSET));}
325 "maxRead" { return (processSpec (QMAXREAD));}
327 {Letter}({Letter}|{Digit})* { int tok;
328 context_saveLocation ();
329 setTokLength (longUnsigned_toInt (mstring_length (yytext)));
330 tok = processIdentifier (makeIdentifier (yytext));
336 0[xX]{H}+ { setTokLengthT (mstring_length (yytext));
337 RETURN_INT (ctype_int, processHex ()); /* evs 2000-05-17 was ctype_uint */
339 0[xX]{H}+{L} { setTokLengthT (mstring_length (yytext));
340 RETURN_INT (ctype_lint, processHex ()); }
341 0[xX]{H}+{L}{L} { setTokLengthT (mstring_length (yytext));
342 RETURN_INT (ctype_llint, processHex ()); }
343 0[xX]{H}+{U} { setTokLengthT (mstring_length (yytext));
344 RETURN_INT (ctype_uint, processHex ()); }
345 0[xX]{H}+{ULSuffix} { setTokLengthT (mstring_length (yytext));
346 RETURN_INT (ctype_ulint, processHex ()); }
347 0[xX]{H}+{U}{L}{L} { setTokLengthT (mstring_length (yytext));
348 RETURN_INT (ctype_ullint, processHex ()); }
349 0[xX]{H}+{L}{L}{U} { setTokLengthT (mstring_length (yytext));
350 RETURN_INT (ctype_ullint, processHex ()); }
351 0{Digit}+ { setTokLengthT (mstring_length (yytext));
352 RETURN_INT (ctype_int, processOctal ()); }
353 0{Digit}+{U} { setTokLengthT (mstring_length (yytext));
354 RETURN_INT (ctype_uint, processOctal ()); }
355 0{Digit}+{L} { setTokLengthT (mstring_length (yytext));
356 RETURN_INT (ctype_lint, processOctal ()); }
357 0{Digit}+{L}{L} { setTokLengthT (mstring_length (yytext));
358 RETURN_INT (ctype_llint, processOctal ()); }
359 0{Digit}+{ULSuffix} { setTokLengthT (mstring_length (yytext));
360 RETURN_INT (ctype_ulint, processOctal ()); }
361 0{Digit}+{U}{L}{L} { setTokLengthT (mstring_length (yytext));
362 RETURN_INT (ctype_ullint, processOctal ()); }
363 0{Digit}+{L}{L}{U} { setTokLengthT (mstring_length (yytext));
364 RETURN_INT (ctype_ullint, processOctal ()); }
365 {Digit}+ { setTokLengthT (mstring_length (yytext));
366 RETURN_INT (ctype_int, processDec ()); }
367 {Digit}+{U} { setTokLengthT (mstring_length (yytext));
368 RETURN_INT (ctype_uint, processDec ()); }
369 {Digit}+{L} { setTokLengthT (mstring_length (yytext));
370 RETURN_INT (ctype_lint, processDec ()); }
371 {Digit}+{L}{L} { setTokLengthT (mstring_length (yytext));
372 RETURN_INT (ctype_llint, processDec ()); }
373 {Digit}+{ULSuffix} { setTokLengthT (mstring_length (yytext));
374 RETURN_INT (ctype_ulint, processDec ()); }
375 {Digit}+{U}{L}{L} { setTokLengthT (mstring_length (yytext));
376 RETURN_INT (ctype_ullint, processDec ()); }
377 {Digit}+{L}{L}{U} { setTokLengthT (mstring_length (yytext));
378 RETURN_INT (ctype_ullint, processDec ()); }
379 '(\\.|[^\\'])+' { setTokLengthT (mstring_length (yytext));
380 RETURN_CHAR (processChar ()); }
381 L'(\\.|[^\\'])+' { setTokLengthT (mstring_length (yytext));
382 RETURN_CHAR (processChar ()); }
383 {Digit}+{E}[fF] { setTokLengthT (mstring_length (yytext));
384 RETURN_FLOAT (ctype_float, processFloat ()); }
385 {Digit}+{E}[lL] { setTokLengthT (mstring_length (yytext));
386 RETURN_FLOAT (ctype_ldouble, processFloat ()); }
387 {Digit}+{E} { setTokLengthT (mstring_length (yytext));
388 RETURN_FLOAT (ctype_double, processFloat ()); }
390 {Digit}*"."{Digit}+({E})?[fF] { setTokLengthT (mstring_length (yytext));
391 RETURN_FLOAT (ctype_float, processFloat ()); }
392 {Digit}*"."{Digit}+({E})?[lL] { setTokLengthT (mstring_length (yytext));
393 RETURN_FLOAT (ctype_ldouble, processFloat ()); }
394 {Digit}*"."{Digit}+({E})? { setTokLengthT (mstring_length (yytext));
395 RETURN_FLOAT (ctype_double, processFloat ()); }
397 {Digit}+"."{Digit}*({E})?[fF] { setTokLengthT (mstring_length (yytext));
398 RETURN_FLOAT (ctype_float, processFloat ()); }
399 {Digit}+"."{Digit}*({E})?[lL] { setTokLengthT (mstring_length (yytext));
400 RETURN_FLOAT (ctype_ldouble, processFloat ()); }
401 {Digit}+"."{Digit}*({E})? { setTokLengthT (mstring_length (yytext));
402 RETURN_FLOAT (ctype_double, processFloat ()); }
404 ">>=" { setTokLength (3); RETURN_TOK (RIGHT_ASSIGN); }
405 "<<=" { setTokLength (3); RETURN_TOK (LEFT_ASSIGN); }
406 "+=" { setTokLength (2); RETURN_TOK (ADD_ASSIGN); }
407 "-=" { setTokLength (2); RETURN_TOK (SUB_ASSIGN); }
408 "*=" { setTokLength (2); RETURN_TOK (MUL_ASSIGN); }
409 "/=" { setTokLength (2); RETURN_TOK (DIV_ASSIGN); }
410 "%=" { setTokLength (2); RETURN_TOK (MOD_ASSIGN); }
411 "&=" { setTokLength (2); RETURN_TOK (AND_ASSIGN); }
412 "^=" { setTokLength (2); RETURN_TOK (XOR_ASSIGN); }
413 "|=" { setTokLength (2); RETURN_TOK (OR_ASSIGN); }
414 ">>" { setTokLength (2); RETURN_TOK (RIGHT_OP); }
415 "<<" { setTokLength (2); RETURN_TOK (LEFT_OP); }
416 "++" { setTokLength (2); RETURN_TOK (INC_OP); }
417 "--" { setTokLength (2); RETURN_TOK (DEC_OP); }
418 "->" { setTokLength (2); RETURN_TOK (ARROW_OP); }
419 "&&" { setTokLength (2); RETURN_TOK (AND_OP); }
420 "||" { setTokLength (2); RETURN_TOK (OR_OP); }
421 "<=" { setTokLength (2); RETURN_TOK (LE_OP); }
422 ">=" { setTokLength (2); RETURN_TOK (GE_OP); }
423 "==" { setTokLength (2); RETURN_TOK (EQ_OP); }
424 "!=" { setTokLength (2); RETURN_TOK (NE_OP); }
425 ";" { setTokLength (1); RETURN_TOK (TSEMI); }
426 "{" { setTokLength (1); RETURN_TOK (TLBRACE); }
427 "}" { setTokLength (1); RETURN_TOK (TRBRACE); }
428 "," { setTokLength (1); RETURN_TOK (TCOMMA); }
429 ":" { setTokLength (1); RETURN_TOK (TCOLON); }
430 "=" { setTokLength (1); RETURN_TOK (TASSIGN); }
431 "(" { setTokLength (1); RETURN_TOK (TLPAREN); }
432 ")" { setTokLength (1); RETURN_TOK (TRPAREN); }
433 "[" { setTokLength (1); RETURN_TOK (TLSQBR); }
434 "]" { setTokLength (1); RETURN_TOK (TRSQBR); }
435 "." { setTokLength (1); RETURN_TOK (TDOT); }
436 "&" { setTokLength (1); RETURN_TOK (TAMPERSAND); }
437 "!" { setTokLength (1); RETURN_TOK (TEXCL); }
440 "~" { setTokLength (1); RETURN_TOK (TTILDE); }
441 "-" { setTokLength (1); RETURN_TOK (TMINUS); }
442 "+" { setTokLength (1); RETURN_TOK (TPLUS); }
443 "*" { setTokLength (1); RETURN_TOK (TMULT); }
444 "/" { setTokLength (1); RETURN_TOK (TDIV); }
445 "%" { setTokLength (1); RETURN_TOK (TPERCENT); }
446 "<" { setTokLength (1); RETURN_TOK (TLT); }
447 ">" { setTokLength (1); RETURN_TOK (TGT); }
448 "^" { setTokLength (1); RETURN_TOK (TCIRC); }
449 "|" { setTokLength (1); RETURN_TOK (TBAR); }
450 "?" { setTokLength (1); RETURN_TOK (TQUEST); }
453 "/\\" { setTokLength (1); RETURN_TOK (TCAND); }
456 [ \t\v\f] { incColumn (); }
457 \n { context_incLineno ();
458 if (tokLength != 0) {
460 /* No error to report
463 message ("Likely parse error: token spans multiple lines."),
470 continueLine = FALSE;
474 if (context_inMacro ())
476 /* Don't use RETURN_TOK */
477 yylval.tok = lltok_create (TENDMACRO, fileloc_copy (g_currentloc)); /* !!! evans 2002-03-13 */
478 lastWasString = FALSE;
483 "@@MR@@" { setTokLength (6);
485 if (processMacro ()) {
486 if (context_inIterDef ())
488 RETURN_TOK (LLMACROITER);
490 if (context_inIterEnd ())
492 RETURN_TOK (LLMACROEND);
494 if (context_inMacro ())
496 RETURN_TOK (LLMACRO);
500 "@QLMR" { if (context_inHeader () || context_inFunction ())
506 int nspchar = ninput ();
510 ** This is a hack to get the column number correct.
513 llassert (nspchar >= '0' && nspchar <= '9');
515 nspaces = nspchar - '0';
517 setTokLength (5 + nspaces);
521 DPRINTF (("Here we are: %s", context_unparse ()));
523 if (context_inIterDef ())
525 RETURN_TOK (LLMACROITER);
527 if (context_inIterEnd ())
529 RETURN_TOK (LLMACROEND);
531 if (context_inMacro ())
533 RETURN_TOK (LLMACRO);
538 "@.CT" { setTokLength (4); lldiagmsg (ctype_unparseTable ()); }
539 "@.FA" { setTokLength (4); lldiagmsg (message ("Access types: %q", typeIdSet_unparse (context_fileAccessTypes ()))); }
540 "@.F" { setTokLength (3);
541 lldiagmsg (message ("%q: *** marker ***", fileloc_unparse (g_currentloc)));
543 "@.L" { setTokLength (3); usymtab_printLocal (); }
544 "@.A" { setTokLength (3); lldiagmsg (usymtab_unparseAliases ()); }
545 "@.C" { setTokLength (3); lldiagmsg (context_unparse ()); }
546 "@.W" { setTokLength (3); lldiagmsg (context_unparseClauses ()); }
547 "@.G" { setTokLength (3); usymtab_printGuards (); }
548 "@.S" { setTokLength (3); usymtab_printOut (); }
549 "@.X" { setTokLength (3); usymtab_printAll (); }
550 "@.Z" { setTokLength (3); usymtab_printComplete (); }
551 "@.T" { setTokLength (3); usymtab_printTypes (); }
552 "@.K" { setTokLength (3); lldiagmsg (usymtab_unparseStack ()); }
553 "@.M" { setTokLength (3);
554 lldiagmsg (message ("Can modify: %q",
555 sRefSet_unparse (context_modList ())));
557 "%{" { /* BEFORE_COMMENT_MARKER */
559 incColumn (); incColumn ();
560 tok = handleLlSpecial ();
564 if (tok == CANNOTATION) {
567 /* Beware - this bashes yylval! */
572 "%}" { /* AFTER_COMMENT_MARKER */
575 RETURN_TOK (QENDMACRO); }
576 "\\" { incColumn (); continueLine = TRUE; }
578 if ((int) *yytext == 13 ) {
583 message ("Invalid character (ascii: %d), skipping character",
592 /*@null@*/ /*@observer@*/ char *name;
597 ** These tokens are followed by syntax that is parsed by the
601 struct skeyword s_parsetable[] = {
602 { "modifies", QMODIFIES } ,
603 { "globals", QGLOBALS } ,
606 { "constant", QCONSTANT } ,
607 { "function", QFUNCTION } ,
609 { "defines", QDEFINES } ,
611 { "allocates", QALLOCATES } ,
613 { "releases", QRELEASES } ,
614 { "pre", QPRECLAUSE } ,
615 { "post", QPOSTCLAUSE } ,
616 { "setBufferSize", QSETBUFFERSIZE},
617 { "setStringLength", QSETSTRINGLENGTH},
618 { "testinRange", QTESTINRANGE},
619 { "requires", QPRECLAUSE } ,
620 { "ensures", QPOSTCLAUSE } ,
621 { "invariant", QINVARIANT} ,
626 ** These tokens are either stand-alone tokens, or followed by
627 ** token-specific text.
630 struct skeyword s_keytable[] = {
631 { "anytype", QANYTYPE } ,
632 { "integraltype", QINTEGRALTYPE } ,
633 { "unsignedintegraltype", QUNSIGNEDINTEGRALTYPE } ,
634 { "signedintegraltype", QSIGNEDINTEGRALTYPE } ,
638 { "owned", QOWNED } ,
639 { "dependent", QDEPENDENT } ,
640 { "partial", QPARTIAL } ,
641 { "special", QSPECIAL } ,
642 { "truenull", QTRUENULL } ,
643 { "falsenull", QFALSENULL } ,
644 { "nullwhentrue", QTRUENULL } ,
645 { "falsewhennull", QFALSENULL } ,
648 { "notnull", QNOTNULL } ,
649 { "abstract", QABSTRACT } ,
650 { "concrete", QCONCRETE } ,
651 { "mutable", QMUTABLE } ,
652 { "immutable", QIMMUTABLE } ,
653 { "unused", QUNUSED } ,
654 { "external", QEXTERNAL } ,
656 { "unique", QUNIQUE } ,
657 { "returned", QRETURNED } ,
658 { "exposed", QEXPOSED } ,
659 { "refcounted", QREFCOUNTED } ,
661 { "newref", QNEWREF } ,
662 { "tempref", QTEMPREF } ,
663 { "killref", QKILLREF } ,
665 { "relnull", QRELNULL } ,
666 { "nullterminated", QNULLTERMINATED },
667 { "setBufferSize", QSETBUFFERSIZE },
668 { "testInRange", QTESTINRANGE},
669 { "MaxSet", QMAXSET},
670 { "MaxRead", QMAXREAD},
671 { "reldef", QRELDEF } ,
672 { "observer", QOBSERVER } ,
673 { "exits", QEXITS } ,
674 { "noreturn", QEXITS } ,
675 { "mayexit", QMAYEXIT } ,
676 { "maynotreturn", QMAYEXIT } ,
677 { "trueexit", QTRUEEXIT } ,
678 { "falseexit", QFALSEEXIT } ,
679 { "noreturnwhentrue", QTRUEEXIT } ,
680 { "noreturnwhenfalse", QFALSEEXIT } ,
681 { "neverexit", QNEVEREXIT } ,
682 { "alwaysreturns", QNEVEREXIT } ,
684 { "shared", QSHARED } ,
686 { "unchecked", QUNCHECKED } ,
687 { "checked", QCHECKED } ,
688 { "checkmod", QCHECKMOD } ,
689 { "checkedstrict", QCHECKEDSTRICT } ,
690 { "innercontinue", QINNERCONTINUE } ,
691 { "innerbreak", QINNERBREAK } ,
692 { "loopbreak", QLOOPBREAK } ,
693 { "switchbreak", QSWITCHBREAK } ,
694 { "safebreak", QSAFEBREAK } ,
695 { "fallthrough", QFALLTHROUGH } ,
696 { "l_fallthrou", QLINTFALLTHROUGH } ,
697 { "l_fallth", QLINTFALLTHRU } ,
698 { "notreached", QNOTREACHED } ,
699 { "l_notreach", QLINTNOTREACHED } ,
700 { "printflike", QPRINTFLIKE } ,
701 { "l_printfli", QLINTPRINTFLIKE } ,
702 { "scanflike", QSCANFLIKE } ,
703 { "messagelike", QMESSAGELIKE } ,
704 { "l_argsus", QARGSUSED } ,
709 ** would be better if these weren't hard coded...
712 static bool isArtificial (cstring s)
714 return (cstring_equalLit (s, "modifies")
715 || cstring_equalLit (s, "globals")
716 || cstring_equalLit (s, "warn")
717 || cstring_equalLit (s, "alt"));
720 void swallowMacro (void)
723 bool skipnext = FALSE;
725 while ((i = lminput ()) != EOF)
742 reader_checkUngetc (i, yyin);
750 reader_checkUngetc (i, yyin);
754 static int commentMarkerToken (cstring s)
758 while (s_parsetable[i].name != NULL)
760 DPRINTF (("Try :%s:%s:", s, s_parsetable[i].name));
762 if (cstring_equalLit (s, s_parsetable[i].name))
764 return s_parsetable[i].token;
773 static int tokenMacroCode (cstring s)
777 while (s_keytable[i].name != NULL)
779 if (cstring_equalLit (s, s_keytable[i].name))
781 if (s_keytable[i].token == QLINTFALLTHROUGH)
784 (FLG_WARNLINTCOMMENTS,
786 ("Traditional lint comment /*FALLTHROUGH*/ used. "
787 "Splint interprets this in the same way as most Unix lints, but it is "
788 "preferable to replace it with the /*@fallthrough@*/ "
793 else if (s_keytable[i].token == QLINTFALLTHRU)
796 (FLG_WARNLINTCOMMENTS,
798 ("Traditional lint comment /*FALLTHRU*/ used. "
799 "Splint interprets this in the same way as most Unix lints, but it is "
800 "preferable to replace it with the /*@fallthrough@*/ "
805 else if (s_keytable[i].token == QLINTNOTREACHED)
808 (FLG_WARNLINTCOMMENTS,
810 ("Traditional lint comment /*NOTREACHED*/ used. "
811 "Splint interprets this in the same way as most Unix lints, but it is "
812 "preferable to replace it with the /*@notreached@*/ "
813 "semantic comment."),
818 else if (s_keytable[i].token == QPRINTFLIKE)
820 setSpecialFunction (qual_createPrintfLike ());
823 else if (s_keytable[i].token == QLINTPRINTFLIKE)
826 (FLG_WARNLINTCOMMENTS,
828 ("Traditional lint comment /*PRINTFLIKE*/ used. "
829 "Splint interprets this in the same way as most Unix lints, but it is "
830 "preferable to replace it with either /*@printflike@*/, "
831 "/*@scanflike@*/ or /*@messagelike@*/."),
834 setSpecialFunction (qual_createPrintfLike ());
837 else if (s_keytable[i].token == QSCANFLIKE)
839 setSpecialFunction (qual_createScanfLike ());
842 else if (s_keytable[i].token == QMESSAGELIKE)
844 setSpecialFunction (qual_createMessageLike ());
847 else if (s_keytable[i].token == QARGSUSED)
850 (FLG_WARNLINTCOMMENTS,
852 ("Traditional lint comment /*ARGSUSED*/ used. "
853 "Splint interprets this in the same way as most Unix lints, but it is "
854 "preferable to use /*@unused@*/ annotations on "
855 "the unused parameters."),
862 return s_keytable[i].token;
871 static int lminput ()
873 if (savechar == '\0')
880 int save = (int) savechar;
886 static void lmsavechar (char c)
888 if (savechar == '\0') savechar = c;
891 llbuglit ("lmsavechar: override");
895 static int returnFloat (ctype ct, double f)
897 yylval.expr = exprNode_floatLiteral (f, ct, cstring_fromChars (yytext),
898 fileloc_decColumn (g_currentloc, tokLength));
903 static int returnInt (ctype ct, long i)
907 if (ctype_equal (ct, ctype_int))
911 c = context_typeofZero ();
915 c = context_typeofOne ();
919 yylval.expr = exprNode_numLiteral (c, cstring_fromChars (yytext),
920 fileloc_decColumn (g_currentloc, tokLength), i);
925 static int returnChar (char c)
927 yylval.expr = exprNode_charLiteral (c, cstring_fromChars (yytext),
928 fileloc_decColumn (g_currentloc, tokLength));
937 if (c != EOF && ((char)c == '\n'))
939 context_incLineno ();
945 static char macro_nextChar ()
947 static bool in_quote = FALSE, in_escape = FALSE, in_char = FALSE;
952 c = char_fromInt (ic);
954 if (!in_quote && !in_char && (c == '\\' || c == BEFORE_COMMENT_MARKER[0]))
958 while ((c = char_fromInt (lminput ())) != '\0' && c != '\n')
960 ; /* skip to newline */
963 context_incLineno ();
967 return macro_nextChar ();
974 else /* if (c == '@') */
976 llassert (FALSE); /*@i23@*/
977 if (handleLlSpecial () != BADTOK)
979 llerrorlit (FLG_SYNTAX, "Macro cannot use special syntax");
982 return macro_nextChar ();
985 else if (!in_escape && c == '\"')
987 in_quote = !in_quote;
989 else if (!in_escape && c == '\'')
993 else if ((in_quote || in_char) && c == '\\')
995 in_escape = !in_escape;
997 else if ((in_quote || in_char) && in_escape)
1001 else if (!in_quote && c == '/')
1005 if ((c2 = char_fromInt (lminput ())) == '*')
1009 while ((c2 = char_fromInt (lminput ())) != '\0'
1010 && c2 != '\n' && c2 != '*')
1017 while ((c2 = char_fromInt (lminput ())) != '\0'
1030 llfatalerror (cstring_makeLiteral ("Macro: bad comment!"));
1034 return macro_nextChar ();
1038 /*** putchar does not work! why? puts to stdio...??! ***/
1046 ** keeps semantic comments
1049 static char macro_nextCharC ()
1051 static bool in_quote = FALSE, in_escape = FALSE, in_char = FALSE;
1054 c = char_fromInt (lminput ());
1056 if (!in_quote && !in_char && c == '\\')
1058 while ((c = char_fromInt (lminput ())) != '\0' && c != '\n')
1060 ; /* skip to newline */
1063 context_incLineno ();
1067 return macro_nextCharC ();
1074 else if (!in_escape && c == '\"')
1076 in_quote = !in_quote;
1078 else if (!in_escape && c == '\'')
1082 else if ((in_quote || in_char) && c == '\\')
1084 in_escape = !in_escape;
1086 else if ((in_quote || in_char) && in_escape)
1090 else if (!in_quote && c == '/')
1094 if ((c2 = char_fromInt (lminput ())) == '*')
1098 while ((c2 = char_fromInt (lminput ())) != '\0'
1099 && c2 != '\n' && c2 != '*')
1106 while ((c2 = char_fromInt (lminput ())) != '\0'
1119 llfatalerror (cstring_makeLiteral ("Macro: bad comment!"));
1123 return macro_nextCharC ();
1134 ** skips whitespace (handles line continuations)
1135 ** returns first non-whitespace character
1138 static char skip_whitespace ()
1142 while ((c = macro_nextChar ()) == ' ' || c == '\t')
1150 static void handleMacro ()
1152 cstring mac = cstring_undefined;
1156 while (currentColumn () > 2)
1158 mac = cstring_appendChar (mac, ' ');
1162 c = macro_nextCharC ();
1164 if (c >= '0' && c <= '9')
1168 for (i = 0; i < ((c - '0') + 1); i++)
1170 mac = cstring_appendChar (mac, ' ');
1178 while (((c = macro_nextCharC ()) != '\0') && (c != '\n'))
1180 mac = cstring_appendChar (mac, c);
1184 macrocode = tokenMacroCode (mac);
1186 if (macrocode == BADTOK && !isArtificial (mac))
1188 context_addMacroCache (mac);
1197 context_incLineno ();
1201 static bool processMacro (void)
1206 cstring fname = cstring_undefined;
1208 bool isspecfcn = FALSE;
1209 bool isiter = FALSE;
1210 bool skipparam = FALSE;
1211 bool isenditer = FALSE;
1212 bool unknownm = FALSE;
1213 bool hasParams = FALSE;
1214 bool emptyMacro = FALSE;
1215 char c = skip_whitespace ();
1216 fileloc loc = fileloc_noColumn (g_currentloc);
1218 /* are both of these necessary? what do they mean? */
1219 uentryList specparams = uentryList_undefined;
1220 uentryList pn = uentryList_undefined;
1222 context_resetMacroMissingParams ();
1224 if (c == '\0' || c == '\n')
1226 llcontbug (cstring_makeLiteral ("Bad macro"));
1231 fname = cstring_appendChar (fname, c);
1233 while ((c = macro_nextChar ()) != '(' && c != '\0'
1234 && c != ' ' && c != '\t' && c != '\n')
1236 fname = cstring_appendChar (fname, c);
1239 if (c == ' ' || c == '\t' || c == '\n')
1245 while (c == ' ' || c == '\t')
1247 c = macro_nextChar ();
1261 hasParams = (c == '(');
1263 if (usymtab_exists (fname))
1265 e2 = usymtab_lookupExpose (fname);
1266 ct = uentry_getType (e2);
1268 if (uentry_isCodeDefined (e2)
1269 && fileloc_isUser (uentry_whereDefined (e2)))
1273 message ("Macro %s already defined", fname),
1276 uentry_showWhereDefined (e2);
1277 uentry_clearDefined (e2);
1280 if (uentry_isFunction (e2))
1282 uentry_setType (e2, ctype_unknown);
1285 context_enterUnknownMacro (e2);
1289 context_enterConstantMacro (e2);
1294 if (uentry_isForward (e2) && uentry_isFunction (e2))
1301 ("Parameterized macro has no prototype or specification: %s ",
1306 uentry_setType (e2, ctype_unknown);
1307 uentry_setFunctionDefined (e2, loc);
1308 uentry_setUsed (e2, fileloc_undefined);
1309 context_enterUnknownMacro (e2);
1313 if (uentry_isIter (e2))
1316 specparams = uentry_getParams (e2);
1317 noparams = uentryList_size (specparams);
1318 uentry_setDefined (e2, loc);
1319 context_enterIterDef (e2);
1321 else if (uentry_isEndIter (e2))
1324 uentry_setDefined (e2, loc);
1325 context_enterIterEnd (e2); /* don't care about it now */
1326 /* but should parse like an iter! */
1328 else if (uentry_isConstant (e2))
1334 message ("Constant %s implemented as parameterized macro",
1338 uentry_showWhereSpecified (e2);
1339 uentry_setType (e2, ctype_unknown);
1340 uentry_makeConstantFunction (e2);
1341 uentry_setDefined (e2, g_currentloc);
1342 uentry_setFunctionDefined (e2, g_currentloc);
1343 context_enterUnknownMacro (e2);
1347 if (!uentry_isSpecified (e2))
1349 fileloc oloc = uentry_whereDeclared (e2);
1351 if (fileloc_isLib (oloc))
1355 else if (fileloc_isUndefined (oloc)
1356 || fileloc_isPreproc (oloc))
1361 (FLG_MACROCONSTDECL,
1363 ("Macro constant %q not declared",
1364 uentry_getName (e2)),
1368 else if (!fileloc_withinLines (oloc, loc, 2))
1369 { /* bogus! will give errors if there is too much whitespace */
1373 ("Macro constant name %s does not match name in "
1374 "previous constant declaration. This constant "
1375 "is declared at %q", fname,
1376 fileloc_unparse (oloc)),
1381 context_enterConstantMacro (e2);
1382 cstring_free (fname);
1388 else if (ctype_isFunction (ct))
1391 specparams = ctype_argsFunction (ct);
1392 noparams = uentryList_size (specparams);
1394 uentry_setFunctionDefined (e2, loc);
1395 context_enterMacro (e2);
1397 else if (uentry_isVar (e2))
1403 message ("Variable %s implemented as parameterized macro",
1407 uentry_showWhereSpecified (e2);
1408 uentry_setType (e2, ctype_unknown);
1409 uentry_makeVarFunction (e2);
1410 uentry_setDefined (e2, g_currentloc);
1411 uentry_setFunctionDefined (e2, g_currentloc);
1412 context_enterUnknownMacro (e2);
1416 uentry ucons = uentry_makeConstant (fname,
1419 if (uentry_isExpandedMacro (e2))
1427 message ("Variable %s implemented by a macro",
1431 uentry_showWhereSpecified (e2);
1435 uentry_setDefined (e2, loc);
1436 uentry_setUsed (ucons, loc);
1438 context_enterConstantMacro (ucons);
1439 uentry_markOwned (ucons);
1440 cstring_free (fname);
1446 if (uentry_isDatatype (e2))
1450 message ("Type implemented as macro: %x",
1451 uentry_getName (e2)),
1452 message ("A type is implemented using a macro definition. A "
1453 "typedef should be used instead."),
1457 /* Must exit scope (not sure why a new scope was entered?) */
1458 usymtab_quietExitScope (g_currentloc);
1459 uentry_setDefined (e2, g_currentloc);
1465 (message ("Unexpanded macro not function or constant: %q",
1466 uentry_unparse (e2)));
1467 uentry_setType (e2, ctype_unknown);
1471 uentry_makeVarFunction (e2);
1472 uentry_setDefined (e2, g_currentloc);
1473 uentry_setFunctionDefined (e2, g_currentloc);
1474 context_enterUnknownMacro (e2);
1485 /* evans 2001-09-09 - if it has params, assume a function */
1489 (FLG_MACROMATCHNAME,
1490 message ("Unexpanded macro %s does not match name of a declared "
1491 "function. The name used in the control "
1492 "comment on the previous line should match.",
1496 ce = uentry_makeFunction (fname, ctype_unknown,
1500 warnClause_undefined,
1502 uentry_setUsed (ce, loc); /* perhaps bogus? */
1503 e2 = usymtab_supEntryReturn (ce);
1504 context_enterUnknownMacro (e2);
1509 (FLG_MACROMATCHNAME,
1510 message ("Unexpanded macro %s does not match name of a constant "
1511 "or iter declaration. The name used in the control "
1512 "comment on the previous line should match. "
1513 "(Assuming macro defines a constant.)",
1517 ce = uentry_makeConstant (fname, ctype_unknown, fileloc_undefined);
1518 uentry_setUsed (ce, loc); /* perhaps bogus? */
1519 e2 = usymtab_supEntryReturn (ce);
1521 context_enterConstantMacro (e2);
1522 cstring_free (fname);
1528 /* in macros, ( must follow immediatetly after name */
1534 c = skip_whitespace ();
1536 while (c != ')' && c != '\0')
1539 bool suppress = context_inSuppressRegion ();
1540 cstring paramname = cstring_undefined;
1543 ** save the parameter location
1547 context_saveLocation ();
1550 while (c != ' ' && c != '\t' && c != ',' && c != '\0' && c != ')')
1552 paramname = cstring_appendChar (paramname, c);
1553 c = macro_nextChar ();
1556 if (c == ' ' || c == '\t') c = skip_whitespace ();
1560 c = macro_nextChar ();
1561 if (c == ' ' || c == '\t') c = skip_whitespace ();
1566 llfatalerror (cstring_makeLiteral
1567 ("Bad macro syntax: uentryList"));
1570 if ((isspecfcn || isiter) && (paramno < noparams)
1571 && !uentry_isElipsisMarker (uentryList_getN
1572 (specparams, paramno)))
1574 fileloc sloc = context_getSaveLocation ();
1575 uentry decl = uentryList_getN (specparams, paramno);
1578 param = uentry_nameCopy (paramname, decl);
1580 uentry_setParam (param);
1581 sr = sRef_makeParam (paramno, uentry_getType (param), stateInfo_makeLoc (sloc));
1583 if (sRef_getNullState (sr) == NS_ABSNULL)
1585 ctype pt = ctype_realType (uentry_getType (param));
1587 if (ctype_isUser (pt))
1589 uentry te = usymtab_getTypeEntrySafe (ctype_typeId (pt));
1591 if (uentry_isValid (te))
1593 sRef_setStateFromUentry (sr, te);
1598 sRef_setNullState (sr, NS_UNKNOWN, sloc);
1602 uentry_setSref (param, sr);
1603 uentry_setDeclaredForceOnly (param, sloc);
1605 skipparam = isiter && uentry_isOut (uentryList_getN (specparams, paramno));
1609 fileloc sloc = context_getSaveLocation ();
1611 param = uentry_makeVariableSrefParam
1612 (paramname, ctype_unknown, fileloc_copy (sloc),
1613 sRef_makeParam (paramno, ctype_unknown, stateInfo_makeLoc (sloc)));
1614 DPRINTF (("Unknown param: %s", uentry_unparseFull (param)));
1615 cstring_free (paramname);
1617 sRef_setPosNull (uentry_getSref (param), sloc);
1618 uentry_setDeclaredForce (param, sloc);
1621 fileloc_free (sloc);
1626 llassert (!uentry_isElipsisMarker (param));
1630 sRef_makeUnsafe (uentry_getSref (param));
1633 pn = uentryList_add (pn, uentry_copy (param));
1634 usymtab_supEntry (param);
1638 /* don't add param */
1639 uentry_free (param);
1644 (void) macro_nextChar ();
1645 c = skip_whitespace ();
1653 if (isspecfcn || isiter)
1655 if (paramno != noparams && noparams >= 0)
1661 message ("Macro %s specified with %d args, defined with %d",
1662 fname, noparams, paramno),
1665 uentry_showWhereSpecified (e2);
1666 uentry_resetParams (e2, pn);
1671 uentry_resetParams (e2, pn);
1678 ** the form should be:
1680 ** # define newname oldname
1681 ** where oldname refers to a function matching the specification
1687 sRef_setGlobalScope ();
1688 usymtab_supGlobalEntry (uentry_makeVariableLoc (fname, ctype_unknown));
1689 sRef_clearGlobalScope ();
1693 context_setMacroMissingParams ();
1698 /* context_setuentryList (pn); */
1699 usymtab_enterScope ();
1702 cstring_free (fname);
1707 static bool handleSpecial (char *yyt)
1709 char *l = mstring_create (MAX_NAME_LENGTH);
1717 len_yyt = strlen (yyt +1) ;
1719 l = mstring_copy (yyt + 1);
1721 while ((c = char_fromInt (lminput ())) != '\n' && c != '\0')
1723 l = mstring_append(l, c);
1726 /* Need to safe original l for deallocating. */
1731 olc = cstring_fromChars (ol);
1733 if (cstring_equalPrefixLit (olc, "pragma"))
1735 char *pname = mstring_create (longUnsigned_fromInt (MAX_PRAGMA_LEN));
1736 char *opname = pname;
1737 char *ptr = ol + 6; /* pragma is six characters, plus space */
1741 /* skip whitespace */
1742 while (((c = *ptr) != '\0') && isspace (c))
1748 while (((c = *ptr) != '\0') && !isspace (c))
1752 if (len > MAX_PRAGMA_LEN)
1763 if (len == PRAGMA_LEN_EXPAND
1764 && mstring_equal (opname, PRAGMA_EXPAND))
1766 cstring exname = cstring_undefined;
1770 while (((c = *ptr) != '\0') && !isspace (c))
1772 exname = cstring_appendChar (exname, c);
1777 ue = usymtab_lookupExposeGlob (exname);
1779 if (uentry_isExpandedMacro (ue))
1781 if (fileloc_isPreproc (uentry_whereDefined (ue)))
1783 fileloc_setColumn (g_currentloc, 1);
1784 uentry_setDefined (ue, g_currentloc);
1788 cstring_free (exname);
1791 else if (cstring_equalPrefixLit (olc, "ident"))
1793 /* Some pre-processors will leave these in the code. Ignore rest of line */
1796 ** Yuk...Win32 filenames can have spaces in them...we need to read
1797 ** to the matching end quote.
1799 else if ((sscanf (ol, "line %d \"", &lineno) == 1)
1800 || (sscanf (ol, " %d \"", &lineno) == 1))
1806 /*@access cstring@*/
1807 while (*tmp != '\"' && *tmp != '\0')
1812 llassert (*tmp == '\"');
1818 while (*tmp != '\"' && *tmp != '\0')
1823 llassert (*tmp == '\"');
1827 # if defined(OS2) || defined(MSDOS) || defined(WIN32)
1830 ** DOS-like path delimiters get delivered in pairs, something like
1831 ** \"..\\\\file.h\", so we have to make it normal again. We do NOT
1832 ** remove the pre dirs yet as we usually specify tmp paths relative
1833 ** to the current directory, so tmp files would not get found in
1834 ** the hash table. If this method fails we try it again later.
1841 ** Skip past the drive marker.
1844 if (strchr (stmp, ':') != NULL)
1846 stmp = strchr (stmp, ':') + 1;
1849 while ((stmp = strchr (stmp, CONNECTCHAR)) != NULL )
1851 if (*(stmp+1) == CONNECTCHAR)
1853 memmove (stmp, stmp+1, strlen (stmp));
1859 fid = fileTable_lookupBase (context_fileTable (), fname);
1860 if (!(fileId_isValid (fid)))
1862 fname = removePreDirs (fname);
1863 fid = fileTable_lookupBase (context_fileTable (), fname);
1866 # else /* !defined(OS2) && !defined(MSDOS) */
1867 fname = removePreDirs (fname);
1868 fid = fileTable_lookupBase (context_fileTable (), fname);
1869 # endif /* !defined(OS2) && !defined(MSDOS) */
1871 if (!(fileId_isValid (fid)))
1873 if (context_inXHFile ())
1875 fid = fileTable_addXHFile (context_fileTable (), fname);
1877 else if (isHeaderFile (fname))
1879 fid = fileTable_addHeaderFile (context_fileTable (), fname);
1883 fid = fileTable_addFile (context_fileTable (), fname);
1887 setFileLine (fid, lineno);
1888 /*@noaccess cstring@*/
1890 else if ((sscanf (ol, "line %d", &lineno) == 1)
1891 || (sscanf (ol, " %d", &lineno) == 1))
1893 setLine (lineno); /* next line is <cr> */
1897 if (mstring_equal (ol, "")) {
1898 DPRINTF (("Empty pp command!"));
1900 ** evs 2000-05-16: This is a horrible kludge, to get around a bug (well, difficulty) in the pre-processor.
1901 ** We handle a plain # in the input file, by echoing it, and ignoring it in the post-pp-file.
1907 (FLG_UNRECOGDIRECTIVE,
1908 message ("Unrecognized pre-processor directive: #%s",
1909 cstring_fromChars (ol)),
1914 return FALSE; /* evans 2001-12-30: was: TRUE; */
1921 static int handleLlSpecial ()
1926 char *s = mstring_createEmpty ();
1932 loc = fileloc_copy (g_currentloc);
1933 DPRINTF (("Handle special: %s", fileloc_unparse (loc)));
1935 while (((ic = ninput ()) != 0) && isalpha (ic))
1938 s = mstring_append (s, c);
1942 DPRINTF (("Read: %s / %s", s, fileloc_unparse (g_currentloc)));
1945 if (charsread == 0 && ic == (int) AFTER_COMMENT_MARKER[0])
1949 llassert (ic == AFTER_COMMENT_MARKER[1]);
1955 return QNOMODS; /* special token no modifications token */
1959 DPRINTF (("Coment marker: %s", os));
1960 tok = commentMarkerToken (cstring_fromChars (os));
1964 tokLength = charsread;
1971 DPRINTF (("Not a comment marker..."));
1972 /* Add rest of the comment */
1974 if (ic != 0 && ic != EOF)
1978 s = mstring_append (s, c);
1981 while (((ic = ninput ()) != 0) && (ic != EOF)
1982 && (ic != AFTER_COMMENT_MARKER[0]))
1986 /* evans 2001-09-01 added to prevent assertion failures for uncloses syntactic comments */
1989 hasnl = TRUE; /* This prevents tokLength from being set later. */
1994 message ("Likely parse error: syntactic comment token spans multiple lines: %s",
1995 cstring_fromChars (s)),
1999 s = mstring_append (s, c);
2004 DPRINTF (("Read: %s / %s", s, fileloc_unparse (g_currentloc)));
2006 if (ic == AFTER_COMMENT_MARKER[0])
2009 llassert ((char) nc == AFTER_COMMENT_MARKER[1]);
2015 while (*s == ' ' || *s == '\t' || *s == '\n')
2020 if (*s == '-' || *s == '+' || *s == '=') /* setting flags */
2024 while (c == '-' || c == '+' || c == '=')
2026 ynm set = ynm_fromCodeChar (c);
2031 thisflag = cstring_fromChars (s);
2033 while ((c = *s) != '\0' && (c != '-') && (c != '=')
2034 && (c != '+') && (c != ' ') && (c != '\t') && (c != '\n'))
2041 if (!context_getFlag (FLG_NOCOMMENTS))
2043 cstring flagname = thisflag;
2044 flagcode fflag = flags_identifyFlag (flagname);
2046 if (flagcode_isSkip (fflag))
2050 else if (flagcode_isModeName (fflag))
2052 if (ynm_isMaybe (set))
2057 ("Semantic comment attempts to restore flag %s. "
2058 "A mode flag cannot be restored.",
2063 context_setMode (flagname);
2066 else if (flagcode_isInvalid (fflag))
2069 (FLG_UNRECOGFLAGCOMMENTS,
2070 message ("Unrecognized option in semantic comment: %s",
2074 else if (flagcode_isGlobalFlag (fflag))
2079 ("Semantic comment attempts to set global flag %s. "
2080 "A global flag cannot be set locally.",
2086 context_fileSetFlag (fflag, set, loc);
2088 if (flagcode_hasArgument (fflag))
2090 if (ynm_isMaybe (set))
2095 ("Semantic comment attempts to restore flag %s. "
2096 "A flag for setting a value cannot be restored.",
2101 { /* cut-and-pastied from llmain...blecch */
2102 cstring extra = cstring_undefined;
2108 rest = mstring_copy (s);
2112 while ((rchar = *rest) != '\0'
2113 && (isspace (rchar)))
2119 while ((rchar = *rest) != '\0'
2120 && !isspace (rchar))
2122 extra = cstring_appendChar (extra, rchar);
2129 if (cstring_isUndefined (extra))
2134 ("Flag %s (in semantic comment) must be followed by an argument",
2135 flagcode_unparse (fflag)));
2141 if (flagcode_hasNumber (fflag))
2143 setValueFlag (fflag, extra);
2145 else if (flagcode_hasChar (fflag))
2147 setValueFlag (fflag, extra);
2149 else if (flagcode_hasString (fflag))
2151 setStringFlag (fflag, extra);
2168 while ((c == ' ') || (c == '\t') || (c == '\n'))
2174 if (context_inHeader () && !isArtificial (cstring_fromChars (os)))
2176 DPRINTF (("Here adding comment: %s", os));
2177 context_addComment (cstring_fromCharsNew (os), loc);
2189 annotationInfo ainfo;
2191 while (*s != '\0' && *s != ' ' && *s != '\t' && *s != '\n')
2203 t = cstring_toCharsSafe (cstring_downcase (cstring_fromChars (t)));
2204 macrocode = tokenMacroCode (cstring_fromChars (t));
2206 if (macrocode != BADTOK)
2208 tokLength = hasnl ? 0 : mstring_length (t);
2214 if (macrocode == SKIPTOK)
2222 ainfo = context_lookupAnnotation (cstring_fromChars (os));
2224 if (annotationInfo_isDefined (ainfo)) {
2225 DPRINTF (("Found annotation: %s", annotationInfo_unparse (ainfo)));
2226 /*@i324@*/ yylval.annotation = ainfo;
2234 if (context_inHeader ())
2241 if ((context_inMacro () || context_inGlobalContext ())
2242 && macrocode != SKIPTOK
2243 && !isArtificial (cstring_fromChars (os)))
2245 if (context_processingMacros ())
2247 /* evans 2002-02-24: don't add comments when procssing macros */
2251 context_addComment (cstring_fromCharsNew (os), loc);
2265 if (mstring_equal (t, "ignore"))
2267 if (!context_getFlag (FLG_NOCOMMENTS))
2269 context_enterSuppressRegion (loc);
2272 else if ((*t == 'i' || *t == 't')
2273 && (*(t + 1) == '\0'))
2275 if (!context_getFlag (FLG_NOCOMMENTS)
2276 && (*t == 'i' || context_getFlag (FLG_TMPCOMMENTS)))
2278 context_enterSuppressLine (-1, loc); /* infinite suppression */
2281 else if (((*t == 'i') || (*t == 't'))
2282 && ((*(t + 1) >= '0' && *(t + 1) <= '9')))
2284 bool tmpcomment = (*t == 't');
2286 char *tt = t; /* don't mangle t, since it is free'd */
2289 if (lc >= '0' && lc <= '9')
2291 val = (int)(lc - '0');
2294 while (lc >= '0' && lc <= '9')
2302 if (!context_getFlag (FLG_NOCOMMENTS)
2303 && (!tmpcomment || context_getFlag (FLG_TMPCOMMENTS)))
2305 DPRINTF (("Here: enter suppress: %s", fileloc_unparse (loc)));
2306 context_enterSuppressLine (val, loc);
2309 else if (mstring_equal (t, "end"))
2311 if (!context_getFlag (FLG_NOCOMMENTS))
2313 context_exitSuppressRegion (loc);
2316 else if (mstring_equal (t, "notfunction"))
2318 ; /* handled by pcpp */
2320 else if (mstring_equal (t, "access"))
2326 while ((c = *s) && (c == ' ' || c == '\t' || c == '\n'))
2336 tname = cstring_fromChars (s);
2338 while ((c = *s) != '\0' && c != ' '
2339 && c != '\t' && c != '\n' && c != ',')
2346 DPRINTF (("Access %s", tname));
2348 if (!context_getFlag (FLG_NOCOMMENTS)
2349 && !context_getFlag (FLG_NOACCESS))
2351 if (usymtab_existsType (tname))
2353 typeId uid = usymtab_getTypeId (tname);
2354 uentry ue = usymtab_getTypeEntry (uid);
2356 if (uentry_isAbstractDatatype (ue))
2358 context_addFileAccessType (uid);
2359 DPRINTF (("Adding access to: %s / %d", tname, uid));
2366 ("Non-abstract type %s used in access comment",
2373 if (!(context_inSuppressRegion ()
2374 || context_inSuppressZone (loc)))
2379 ("Unrecognized type %s used in access comment",
2391 if (c != ',' && c != ' ')
2397 else if (mstring_equal (t, "noaccess"))
2404 while ((lc = *s) && (lc == ' ' || lc == '\t' || lc == '\n'))
2414 tname = cstring_fromChars (s);
2416 while ((lc = *s) != '\0' && lc != ' ' && lc != '\t'
2417 && lc != '\n' && lc != ',')
2424 if (!context_getFlag (FLG_NOCOMMENTS)
2425 && !context_getFlag (FLG_NOACCESS))
2427 if (usymtab_existsType (tname))
2429 typeId tuid = usymtab_getTypeId (tname);
2431 if (context_couldHaveAccess (tuid))
2433 DPRINTF (("Removing access: %s", tname));
2434 context_removeFileAccessType (tuid);
2438 if (!(context_inSuppressRegion ()
2439 || context_inSuppressZone (loc)))
2441 uentry ue = usymtab_getTypeEntry (tuid);
2443 if (uentry_isAbstractDatatype (ue))
2448 ("Non-accessible abstract type %s used in noaccess comment",
2457 ("Non-abstract type %s used in noaccess comment",
2466 if (!(context_inSuppressRegion ()
2467 || context_inSuppressZone (loc)))
2472 ("Unrecognized type %s used in noaccess comment",
2484 if (lc != ',' && lc != ' ')
2492 voptgenerror (FLG_UNRECOGCOMMENTS,
2493 message ("Semantic comment unrecognized: %s",
2494 cstring_fromChars (os)),
2506 static /*@only@*/ cstring makeIdentifier (char *s)
2508 char *c = mstring_create (size_toInt (strlen (s)) + 1);
2509 cstring id = cstring_fromChars (c);
2511 while (isalnum (*s) || (*s == '_') || (*s == '$'))
2520 /*@observer@*/ /*@dependent@*/ uentry coerceId (cstring cn)
2522 if (!(usymtab_exists (cn)))
2524 fileloc loc = fileloc_createExternal ();
2527 ** We need to put this in a global scope, otherwise the sRef will be deallocated.
2530 uentry ce = uentry_makeUnrecognized (cn, loc);
2532 if (!context_inIterEnd ())
2536 message ("Unrecognized (possibly system) identifier: %q",
2537 uentry_getName (ce)),
2544 return (usymtab_lookup (cn));
2548 ** like, coerceId, but doesn't supercede for iters
2551 /*@observer@*/ uentry coerceIterId (cstring cn)
2553 if (!(usymtab_exists (cn)))
2555 return uentry_undefined;
2558 return (usymtab_lookup (cn));
2561 /*@observer@*/ cstring LastIdentifier ()
2563 return (lastidprocessed);
2566 static int processIdentifier (cstring id)
2570 if (context_getFlag (FLG_GRAMMAR))
2572 lldiagmsg (message ("Process identifier: %s", id));
2575 context_clearJustPopped ();
2576 lastidprocessed = id;
2578 if (context_inFunctionHeader ())
2580 int tok = commentMarkerToken (id);
2581 DPRINTF (("in function decl..."));
2589 tok = tokenMacroCode (id);
2597 annotationInfo ainfo;
2599 if (expectingMetaStateName)
2601 metaStateInfo msinfo = context_lookupMetaStateInfo (id);
2603 if (metaStateInfo_isDefined (msinfo))
2605 yylval.msinfo = msinfo;
2606 return METASTATE_NAME;
2610 DPRINTF (("Not meta state name: %s", cstring_toCharsSafe (id)));
2614 ainfo = context_lookupAnnotation (id);
2616 if (annotationInfo_isDefined (ainfo))
2618 DPRINTF (("Found annotation: %s", annotationInfo_unparse (ainfo)));
2619 /*@i324@*/ yylval.annotation = ainfo;
2624 DPRINTF (("Not annotation: %s", id));
2630 /* Consider handling: Defined by C99 as static const char __func__[] */
2632 if (context_getFlag (FLG_GNUEXTENSIONS))
2636 if (cstring_equalLit (id, "__stdcall")
2637 || cstring_equalLit (id, "__cdecl")
2638 || cstring_equalLit (id, "__extension__"))
2642 else if (cstring_equalLit (id, "__volatile__"))
2646 else if (cstring_equalLit (id, "__signed"))
2650 else if (cstring_equalLit (id, "__unsigned"))
2654 else if (cstring_equalLit (id, "__const__"))
2658 else if (cstring_equalLit (id, "__alignof__"))
2660 tok = CALIGNOF; /* alignof is parsed like sizeof */
2662 else if (cstring_equalLit (id, "__typeof__"))
2666 else if (cstring_equalLit (id, "typeof"))
2670 else if (cstring_equalLit (id, "__FUNCTION__")
2671 || cstring_equalLit (id, "__PRETTY_FUNCTION__"))
2673 /* These tokens hold the name of the current function as strings */
2674 /* evans 2001-12-30: changed from exprNode_stringLiteral; bug reported by Jim Zelenka. */
2675 yylval.expr = exprNode_makeConstantString (id, fileloc_copy (g_currentloc));
2677 lastWasString = TRUE;
2681 else if (cstring_equalLit (id, "__attribute__")
2682 || cstring_equalLit (id, "__asm__")
2683 || cstring_equalLit (id, "_asm")
2684 || cstring_equalLit (id, "__asm")
2685 || cstring_equalLit (id, "__declspec"))
2688 bool useparens = FALSE;
2689 bool usebraces = FALSE;
2690 bool inquote = FALSE;
2691 bool inescape = FALSE;
2694 while ((ic = input ()) != EOF)
2701 else if (ic == '\\')
2705 else if (ic == '\"')
2741 else if (ic == ')' && useparens)
2744 if (depth == 0) break;
2746 else if (ic == '}' && usebraces)
2749 if (depth == 0) break;
2752 && !usebraces && !useparens
2753 && cstring_equalLit (id, "__asm"))
2756 ** We need this because some MS VC++ include files
2757 ** have __asm mov ... }
2758 ** Its a kludge, but otherwise would need to parse
2767 context_incLineno ();
2769 if (cstring_equalLit (id, "__asm")
2770 && !useparens && !usebraces)
2777 llassert ((useparens && ic == ')')
2778 || (usebraces && ic == '}')
2779 || (!useparens && !usebraces));
2783 else if (cstring_equalLit (id, "inline")
2784 || cstring_equalLit (id, "__inline")
2785 || cstring_equalLit (id, "_inline")
2786 || cstring_equalLit (id, "__inline__"))
2797 le = usymtab_lookupSafe (id);
2799 /*@-dependenttrans@*/
2801 if (uentry_isIter (le))
2803 /*@i32@*/ yylval.entry = le;
2806 else if (uentry_isEndIter (le))
2808 /*@i32@*/ yylval.entry = le;
2809 return (ITER_ENDNAME);
2811 else if (uentry_isUndefined (le))
2815 /* avoid parse errors for certain system built ins */
2817 if (g_expectingTypeName && (cstring_firstChar (id) == '_')
2818 && (cstring_secondChar (id) == '_'))
2820 return (TYPE_NAME_OR_ID);
2823 return (NEW_IDENTIFIER);
2825 else if (!uentry_isDeclared (le) && !uentry_isCodeDefined (le))
2827 if (uentry_isDatatype (le))
2830 return (NEW_IDENTIFIER);
2834 /*@i32@*/ yylval.entry = le;
2835 return (IDENTIFIER);
2838 else if (uentry_isDatatype (le))
2840 if (!g_expectingTypeName)
2844 return (NEW_IDENTIFIER);
2848 yylval.ctyp = uentry_getAbstractType (le);
2850 uentry_setUsed (le, g_currentloc);
2856 /*@i32@*/ yylval.entry = le;
2857 return (IDENTIFIER);
2860 /*@=dependenttrans@*/
2863 static bool processHashIdentifier (/*@only@*/ cstring id)
2865 if (context_inMacro () || context_inIterDef () ||
2866 context_inIterEnd ())
2870 context_clearJustPopped ();
2872 lastidprocessed = id;
2873 le = usymtab_lookupSafe (id);
2875 if (uentry_isParam (le) || uentry_isRefParam (le))
2887 ** Will be handled by handleLlSpecial
2896 static /*@only@*/ exprNode processString ()
2900 char *nl = strchr (yytext, '\n');
2901 cstring ns = cstring_fromCharsNew (yytext);
2905 loc = fileloc_copy (g_currentloc);
2906 addColumn (cstring_length (ns));
2912 loc = fileloc_copy (g_currentloc);
2914 context_incLineno ();
2916 while ((nl = strchr ((nl + 1), '\n')) != NULL)
2918 context_incLineno ();
2924 res = exprNode_stringLiteral (ns, loc);
2929 ** process a wide character string L"...."
2932 static /*@only@*/ exprNode processWideString ()
2936 char *nl = strchr (yytext, '\n');
2939 llassert (*yytext == 'L');
2942 ns = cstring_fromCharsNew (yytext);
2946 loc = fileloc_copy (g_currentloc);
2947 addColumn (cstring_length (ns));
2953 loc = fileloc_copy (g_currentloc);
2955 context_incLineno ();
2957 while ((nl = strchr ((nl + 1), '\n')) != NULL)
2959 context_incLineno ();
2964 res = exprNode_wideStringLiteral (ns, loc);
2974 llassert (*yytext != '\0');
2975 fchar = *(yytext + 1);
2976 if (fchar != '\\') return fchar;
2978 next = *(yytext + 2);
2982 case 'n': return '\n';
2983 case 't': return '\t';
2984 case '\"': return '\"';
2985 case '\'': return '\'';
2986 case '\\': return '\\';
2987 default: return '\0';
2992 double processFloat ()
2994 double ret = atof (yytext);
3005 llassert (yytext[0] == '0'
3006 && (yytext[1] == 'X' || yytext[1] == 'x'));
3008 while (yytext[index] != '\0') {
3010 char c = yytext[index];
3012 if (c >= '0' && c <= '9') {
3013 tval = (int) c - (int) '0';
3014 } else if (c >= 'A' && c <= 'F') {
3015 tval = (int) c - (int) 'A' + 10;
3016 } else if (c >= 'a' && c <= 'f') {
3017 tval = (int) c - (int) 'a' + 10;
3018 } else if (c == 'U' || c == 'L' || c == 'u' || c == 'l') {
3020 while (yytext[index] != '\0') {
3021 if (c == 'U' || c == 'L' || c == 'u' || c == 'l') {
3026 message ("Invalid character (%c) following specifier in hex constant: %s",
3027 c, cstring_fromChars (yytext)),
3037 message ("Invalid character (%c) in hex constant: %s",
3038 c, cstring_fromChars (yytext)),
3043 val = (val * 16) + tval;
3047 DPRINTF (("Hex constant: %s = %ld", yytext, val));
3052 long processOctal ()
3057 llassert (yytext[0] == '0' && yytext[1] != 'X' && yytext[1] != 'x');
3059 while (yytext[index] != '\0') {
3061 char c = yytext[index];
3063 if (c >= '0' && c <= '7') {
3064 tval = (int) c - (int) '0';
3065 } else if (c == 'U' || c == 'L' || c == 'u' || c == 'l') {
3067 while (yytext[index] != '\0') {
3068 if (c == 'U' || c == 'L' || c == 'u' || c == 'l') {
3073 message ("Invalid character (%c) following specifier in octal constant: %s",
3074 c, cstring_fromChars (yytext)),
3084 message ("Invalid character (%c) in octal constant: %s",
3085 c, cstring_fromChars (yytext)),
3090 val = (val * 8) + tval;
3094 DPRINTF (("Octal constant: %s = %ld", yytext, val));
3101 return (atol (yytext));
3105 processSpec (int tok)
3107 size_t length = strlen (yytext);
3111 setTokLengthT (length);
3117 context_saveLocation ();
3118 setTokLengthT (length);
3119 return (processIdentifier (makeIdentifier (yytext)));
3123 void cscanner_expectingMetaStateName ()
3125 llassert (!expectingMetaStateName);
3126 llassert (context_inFunctionHeader ());
3127 expectingMetaStateName = TRUE;
3130 void cscanner_clearExpectingMetaStateName ()
3132 llassert (expectingMetaStateName);
3133 expectingMetaStateName = FALSE;