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); }
272 "restrict" { setTokLength (8); RETURN_TOK (QRESTRICT); }
274 /* some systems expect this! [gack!] */
275 "__const" { setTokLength (7); RETURN_TOK (QCONST); }
277 "extern" { setTokLength (6); RETURN_TOK (QEXTERN); }
278 "auto" { setTokLength (4); RETURN_TOK (QAUTO); }
279 "register" { setTokLength (8); RETURN_TOK (QREGISTER); }
280 "static" { setTokLength (6); RETURN_TOK (QSTATIC); }
282 \"(\\.|[^\\"])*\"([ \t\n]*\"(\\.|[^\\"])*\")* { RETURN_EXPR (processString ()); }
283 L\"(\\.|[^\\"])*\"([ \t\n]*\"(\\.|[^\\"])*\")* { RETURN_EXPR (processWideString ()); }
284 "out" { return (processSpec (QOUT)); }
285 "in" { return (processSpec (QIN)); }
286 "partial" { return (processSpec (QPARTIAL)); }
287 "special" { return (processSpec (QSPECIAL)); }
288 "anytype" { return (processSpec (QANYTYPE)); }
289 "integraltype" { return (processSpec (QINTEGRALTYPE)); }
290 "unsignedintegraltype" { return (processSpec (QUNSIGNEDINTEGRALTYPE)); }
291 "signedintegraltype" { return (processSpec (QSIGNEDINTEGRALTYPE)); }
292 "keep" { return (processSpec (QKEEP)); }
293 "null" { return (processSpec (QNULL)); }
294 "notnull" { return (processSpec (QNOTNULL)); }
295 "isnull" { return (processSpec (QISNULL)); }
296 "truenull" { return (processSpec (QTRUENULL)); }
297 "falsenull" { return (processSpec (QFALSENULL)); }
298 "nullwhentrue" { return (processSpec (QTRUENULL)); }
299 "nullwhenfalse" { return (processSpec (QFALSENULL)); }
300 "relnull" { return (processSpec (QRELNULL)); }
301 "reldef" { return (processSpec (QRELDEF)); }
302 "exposed" { return (processSpec (QEXPOSED)); }
303 "newref" { return (processSpec (QNEWREF)); }
304 "tempref" { return (processSpec (QTEMPREF)); }
305 "killref" { return (processSpec (QKILLREF)); }
306 "refcounted" { return (processSpec (QREFCOUNTED)); }
307 "checked" { return (processSpec (QCHECKED)); }
308 "checkmod" { return (processSpec (QCHECKMOD)); }
309 "checkedstrict" { return (processSpec (QCHECKEDSTRICT)); }
310 "unchecked" { return (processSpec (QUNCHECKED)); }
311 "only" { return (processSpec (QONLY)); }
312 "owned" { return (processSpec (QOWNED)); }
313 "observer" { return (processSpec (QOBSERVER)); }
314 "dependent" { return (processSpec (QDEPENDENT)); }
315 "unused" { return (processSpec (QUNUSED)); }
316 "external" { return (processSpec (QEXTERNAL)); }
317 "sef" { return (processSpec (QSEF)); }
318 "shared" { return (processSpec (QSHARED)); }
319 "yield" { return (processSpec (QYIELD)); }
320 "undef" { return (processSpec (QUNDEF)); }
321 "killed" { return (processSpec (QKILLED)); }
322 "nullterminated" { return (processSpec (QNULLTERMINATED));}
323 "MaxSet" { return (processSpec (QMAXSET));}
324 "MaxRead" { return (processSpec (QMAXREAD));}
325 "maxSet" { return (processSpec (QMAXSET));}
326 "maxRead" { return (processSpec (QMAXREAD));}
328 {Letter}({Letter}|{Digit})* { int tok;
329 context_saveLocation ();
330 setTokLength (longUnsigned_toInt (mstring_length (yytext)));
331 tok = processIdentifier (makeIdentifier (yytext));
337 0[xX]{H}+ { setTokLengthT (mstring_length (yytext));
338 RETURN_INT (ctype_int, processHex ()); /* evs 2000-05-17 was ctype_uint */
340 0[xX]{H}+{L} { setTokLengthT (mstring_length (yytext));
341 RETURN_INT (ctype_lint, processHex ()); }
342 0[xX]{H}+{L}{L} { setTokLengthT (mstring_length (yytext));
343 RETURN_INT (ctype_llint, processHex ()); }
344 0[xX]{H}+{U} { setTokLengthT (mstring_length (yytext));
345 RETURN_INT (ctype_uint, processHex ()); }
346 0[xX]{H}+{ULSuffix} { setTokLengthT (mstring_length (yytext));
347 RETURN_INT (ctype_ulint, processHex ()); }
348 0[xX]{H}+{U}{L}{L} { setTokLengthT (mstring_length (yytext));
349 RETURN_INT (ctype_ullint, processHex ()); }
350 0[xX]{H}+{L}{L}{U} { setTokLengthT (mstring_length (yytext));
351 RETURN_INT (ctype_ullint, processHex ()); }
352 0{Digit}+ { setTokLengthT (mstring_length (yytext));
353 RETURN_INT (ctype_int, processOctal ()); }
354 0{Digit}+{U} { setTokLengthT (mstring_length (yytext));
355 RETURN_INT (ctype_uint, processOctal ()); }
356 0{Digit}+{L} { setTokLengthT (mstring_length (yytext));
357 RETURN_INT (ctype_lint, processOctal ()); }
358 0{Digit}+{L}{L} { setTokLengthT (mstring_length (yytext));
359 RETURN_INT (ctype_llint, processOctal ()); }
360 0{Digit}+{ULSuffix} { setTokLengthT (mstring_length (yytext));
361 RETURN_INT (ctype_ulint, processOctal ()); }
362 0{Digit}+{U}{L}{L} { setTokLengthT (mstring_length (yytext));
363 RETURN_INT (ctype_ullint, processOctal ()); }
364 0{Digit}+{L}{L}{U} { setTokLengthT (mstring_length (yytext));
365 RETURN_INT (ctype_ullint, processOctal ()); }
366 {Digit}+ { setTokLengthT (mstring_length (yytext));
367 RETURN_INT (ctype_int, processDec ()); }
368 {Digit}+{U} { setTokLengthT (mstring_length (yytext));
369 RETURN_INT (ctype_uint, processDec ()); }
370 {Digit}+{L} { setTokLengthT (mstring_length (yytext));
371 RETURN_INT (ctype_lint, processDec ()); }
372 {Digit}+{L}{L} { setTokLengthT (mstring_length (yytext));
373 RETURN_INT (ctype_llint, processDec ()); }
374 {Digit}+{ULSuffix} { setTokLengthT (mstring_length (yytext));
375 RETURN_INT (ctype_ulint, processDec ()); }
376 {Digit}+{U}{L}{L} { setTokLengthT (mstring_length (yytext));
377 RETURN_INT (ctype_ullint, processDec ()); }
378 {Digit}+{L}{L}{U} { setTokLengthT (mstring_length (yytext));
379 RETURN_INT (ctype_ullint, processDec ()); }
380 '(\\.|[^\\'])+' { setTokLengthT (mstring_length (yytext));
381 RETURN_CHAR (processChar ()); }
382 L'(\\.|[^\\'])+' { setTokLengthT (mstring_length (yytext));
383 RETURN_CHAR (processChar ()); }
384 {Digit}+{E}[fF] { setTokLengthT (mstring_length (yytext));
385 RETURN_FLOAT (ctype_float, processFloat ()); }
386 {Digit}+{E}[lL] { setTokLengthT (mstring_length (yytext));
387 RETURN_FLOAT (ctype_ldouble, processFloat ()); }
388 {Digit}+{E} { setTokLengthT (mstring_length (yytext));
389 RETURN_FLOAT (ctype_double, processFloat ()); }
391 {Digit}*"."{Digit}+({E})?[fF] { setTokLengthT (mstring_length (yytext));
392 RETURN_FLOAT (ctype_float, processFloat ()); }
393 {Digit}*"."{Digit}+({E})?[lL] { setTokLengthT (mstring_length (yytext));
394 RETURN_FLOAT (ctype_ldouble, processFloat ()); }
395 {Digit}*"."{Digit}+({E})? { setTokLengthT (mstring_length (yytext));
396 RETURN_FLOAT (ctype_double, processFloat ()); }
398 {Digit}+"."{Digit}*({E})?[fF] { setTokLengthT (mstring_length (yytext));
399 RETURN_FLOAT (ctype_float, processFloat ()); }
400 {Digit}+"."{Digit}*({E})?[lL] { setTokLengthT (mstring_length (yytext));
401 RETURN_FLOAT (ctype_ldouble, processFloat ()); }
402 {Digit}+"."{Digit}*({E})? { setTokLengthT (mstring_length (yytext));
403 RETURN_FLOAT (ctype_double, processFloat ()); }
405 ">>=" { setTokLength (3); RETURN_TOK (RIGHT_ASSIGN); }
406 "<<=" { setTokLength (3); RETURN_TOK (LEFT_ASSIGN); }
407 "+=" { setTokLength (2); RETURN_TOK (ADD_ASSIGN); }
408 "-=" { setTokLength (2); RETURN_TOK (SUB_ASSIGN); }
409 "*=" { setTokLength (2); RETURN_TOK (MUL_ASSIGN); }
410 "/=" { setTokLength (2); RETURN_TOK (DIV_ASSIGN); }
411 "%=" { setTokLength (2); RETURN_TOK (MOD_ASSIGN); }
412 "&=" { setTokLength (2); RETURN_TOK (AND_ASSIGN); }
413 "^=" { setTokLength (2); RETURN_TOK (XOR_ASSIGN); }
414 "|=" { setTokLength (2); RETURN_TOK (OR_ASSIGN); }
415 ">>" { setTokLength (2); RETURN_TOK (RIGHT_OP); }
416 "<<" { setTokLength (2); RETURN_TOK (LEFT_OP); }
417 "++" { setTokLength (2); RETURN_TOK (INC_OP); }
418 "--" { setTokLength (2); RETURN_TOK (DEC_OP); }
419 "->" { setTokLength (2); RETURN_TOK (ARROW_OP); }
420 "&&" { setTokLength (2); RETURN_TOK (AND_OP); }
421 "||" { setTokLength (2); RETURN_TOK (OR_OP); }
422 "<=" { setTokLength (2); RETURN_TOK (LE_OP); }
423 ">=" { setTokLength (2); RETURN_TOK (GE_OP); }
424 "==" { setTokLength (2); RETURN_TOK (EQ_OP); }
425 "!=" { setTokLength (2); RETURN_TOK (NE_OP); }
426 ";" { setTokLength (1); RETURN_TOK (TSEMI); }
427 "{" { setTokLength (1); RETURN_TOK (TLBRACE); }
428 "}" { setTokLength (1); RETURN_TOK (TRBRACE); }
429 "," { setTokLength (1); RETURN_TOK (TCOMMA); }
430 ":" { setTokLength (1); RETURN_TOK (TCOLON); }
431 "=" { setTokLength (1); RETURN_TOK (TASSIGN); }
432 "(" { setTokLength (1); RETURN_TOK (TLPAREN); }
433 ")" { setTokLength (1); RETURN_TOK (TRPAREN); }
434 "[" { setTokLength (1); RETURN_TOK (TLSQBR); }
435 "]" { setTokLength (1); RETURN_TOK (TRSQBR); }
436 "." { setTokLength (1); RETURN_TOK (TDOT); }
437 "&" { setTokLength (1); RETURN_TOK (TAMPERSAND); }
438 "!" { setTokLength (1); RETURN_TOK (TEXCL); }
441 "~" { setTokLength (1); RETURN_TOK (TTILDE); }
442 "-" { setTokLength (1); RETURN_TOK (TMINUS); }
443 "+" { setTokLength (1); RETURN_TOK (TPLUS); }
444 "*" { setTokLength (1); RETURN_TOK (TMULT); }
445 "/" { setTokLength (1); RETURN_TOK (TDIV); }
446 "%" { setTokLength (1); RETURN_TOK (TPERCENT); }
447 "<" { setTokLength (1); RETURN_TOK (TLT); }
448 ">" { setTokLength (1); RETURN_TOK (TGT); }
449 "^" { setTokLength (1); RETURN_TOK (TCIRC); }
450 "|" { setTokLength (1); RETURN_TOK (TBAR); }
451 "?" { setTokLength (1); RETURN_TOK (TQUEST); }
454 "/\\" { setTokLength (1); RETURN_TOK (TCAND); }
457 [ \t\v\f] { incColumn (); }
458 \n { context_incLineno ();
459 if (tokLength != 0) {
461 /* No error to report
464 message ("Likely parse error: token spans multiple lines."),
471 continueLine = FALSE;
475 if (context_inMacro ())
477 /* Don't use RETURN_TOK */
478 yylval.tok = lltok_create (TENDMACRO, fileloc_copy (g_currentloc)); /* !!! evans 2002-03-13 */
479 lastWasString = FALSE;
484 "@@MR@@" { setTokLength (6);
486 if (processMacro ()) {
487 if (context_inIterDef ())
489 RETURN_TOK (LLMACROITER);
491 if (context_inIterEnd ())
493 RETURN_TOK (LLMACROEND);
495 if (context_inMacro ())
497 RETURN_TOK (LLMACRO);
501 "@QLMR" { if (context_inHeader () || context_inFunction ())
507 int nspchar = ninput ();
511 ** This is a hack to get the column number correct.
514 llassert (nspchar >= '0' && nspchar <= '9');
516 nspaces = nspchar - '0';
518 setTokLength (5 + nspaces);
522 DPRINTF (("Here we are: %s", context_unparse ()));
524 if (context_inIterDef ())
526 RETURN_TOK (LLMACROITER);
528 if (context_inIterEnd ())
530 RETURN_TOK (LLMACROEND);
532 if (context_inMacro ())
534 RETURN_TOK (LLMACRO);
539 "@.CT" { setTokLength (4); lldiagmsg (ctype_unparseTable ()); }
540 "@.FA" { setTokLength (4); lldiagmsg (message ("Access types: %q", typeIdSet_unparse (context_fileAccessTypes ()))); }
541 "@.F" { setTokLength (3);
542 lldiagmsg (message ("%q: *** marker ***", fileloc_unparse (g_currentloc)));
544 "@.L" { setTokLength (3); usymtab_printLocal (); }
545 "@.A" { setTokLength (3); lldiagmsg (usymtab_unparseAliases ()); }
546 "@.C" { setTokLength (3); lldiagmsg (context_unparse ()); }
547 "@.W" { setTokLength (3); lldiagmsg (context_unparseClauses ()); }
548 "@.G" { setTokLength (3); usymtab_printGuards (); }
549 "@.S" { setTokLength (3); usymtab_printOut (); }
550 "@.X" { setTokLength (3); usymtab_printAll (); }
551 "@.Z" { setTokLength (3); usymtab_printComplete (); }
552 "@.T" { setTokLength (3); usymtab_printTypes (); }
553 "@.K" { setTokLength (3); lldiagmsg (usymtab_unparseStack ()); }
554 "@.M" { setTokLength (3);
555 lldiagmsg (message ("Can modify: %q",
556 sRefSet_unparse (context_modList ())));
558 "%{" { /* BEFORE_COMMENT_MARKER */
560 incColumn (); incColumn ();
561 tok = handleLlSpecial ();
565 if (tok == CANNOTATION) {
568 /* Beware - this bashes yylval! */
573 "%}" { /* AFTER_COMMENT_MARKER */
576 RETURN_TOK (QENDMACRO); }
577 "\\" { incColumn (); continueLine = TRUE; }
579 if ((int) *yytext == 13 ) {
584 message ("Invalid character (ascii: %d), skipping character",
593 /*@null@*/ /*@observer@*/ char *name;
598 ** These tokens are followed by syntax that is parsed by the
602 struct skeyword s_parsetable[] = {
603 { "modifies", QMODIFIES } ,
604 { "globals", QGLOBALS } ,
607 { "constant", QCONSTANT } ,
608 { "function", QFUNCTION } ,
610 { "defines", QDEFINES } ,
612 { "allocates", QALLOCATES } ,
614 { "releases", QRELEASES } ,
615 { "pre", QPRECLAUSE } ,
616 { "post", QPOSTCLAUSE } ,
617 { "setBufferSize", QSETBUFFERSIZE},
618 { "setStringLength", QSETSTRINGLENGTH},
619 { "testinRange", QTESTINRANGE},
620 { "requires", QPRECLAUSE } ,
621 { "ensures", QPOSTCLAUSE } ,
622 { "invariant", QINVARIANT} ,
627 ** These tokens are either stand-alone tokens, or followed by
628 ** token-specific text.
631 struct skeyword s_keytable[] = {
632 { "anytype", QANYTYPE } ,
633 { "integraltype", QINTEGRALTYPE } ,
634 { "unsignedintegraltype", QUNSIGNEDINTEGRALTYPE } ,
635 { "signedintegraltype", QSIGNEDINTEGRALTYPE } ,
639 { "owned", QOWNED } ,
640 { "dependent", QDEPENDENT } ,
641 { "partial", QPARTIAL } ,
642 { "special", QSPECIAL } ,
643 { "truenull", QTRUENULL } ,
644 { "falsenull", QFALSENULL } ,
645 { "nullwhentrue", QTRUENULL } ,
646 { "falsewhennull", QFALSENULL } ,
649 { "notnull", QNOTNULL } ,
650 { "abstract", QABSTRACT } ,
651 { "concrete", QCONCRETE } ,
652 { "mutable", QMUTABLE } ,
653 { "immutable", QIMMUTABLE } ,
654 { "unused", QUNUSED } ,
655 { "external", QEXTERNAL } ,
657 { "unique", QUNIQUE } ,
658 { "returned", QRETURNED } ,
659 { "exposed", QEXPOSED } ,
660 { "refcounted", QREFCOUNTED } ,
662 { "newref", QNEWREF } ,
663 { "tempref", QTEMPREF } ,
664 { "killref", QKILLREF } ,
666 { "relnull", QRELNULL } ,
667 { "nullterminated", QNULLTERMINATED },
668 { "setBufferSize", QSETBUFFERSIZE },
669 { "testInRange", QTESTINRANGE},
670 { "MaxSet", QMAXSET},
671 { "MaxRead", QMAXREAD},
672 { "reldef", QRELDEF } ,
673 { "observer", QOBSERVER } ,
674 { "exits", QEXITS } ,
675 { "noreturn", QEXITS } ,
676 { "mayexit", QMAYEXIT } ,
677 { "maynotreturn", QMAYEXIT } ,
678 { "trueexit", QTRUEEXIT } ,
679 { "falseexit", QFALSEEXIT } ,
680 { "noreturnwhentrue", QTRUEEXIT } ,
681 { "noreturnwhenfalse", QFALSEEXIT } ,
682 { "neverexit", QNEVEREXIT } ,
683 { "alwaysreturns", QNEVEREXIT } ,
685 { "shared", QSHARED } ,
687 { "unchecked", QUNCHECKED } ,
688 { "checked", QCHECKED } ,
689 { "checkmod", QCHECKMOD } ,
690 { "checkedstrict", QCHECKEDSTRICT } ,
691 { "innercontinue", QINNERCONTINUE } ,
692 { "innerbreak", QINNERBREAK } ,
693 { "loopbreak", QLOOPBREAK } ,
694 { "switchbreak", QSWITCHBREAK } ,
695 { "safebreak", QSAFEBREAK } ,
696 { "fallthrough", QFALLTHROUGH } ,
697 { "l_fallthrou", QLINTFALLTHROUGH } ,
698 { "l_fallth", QLINTFALLTHRU } ,
699 { "notreached", QNOTREACHED } ,
700 { "l_notreach", QLINTNOTREACHED } ,
701 { "printflike", QPRINTFLIKE } ,
702 { "l_printfli", QLINTPRINTFLIKE } ,
703 { "scanflike", QSCANFLIKE } ,
704 { "messagelike", QMESSAGELIKE } ,
705 { "l_argsus", QARGSUSED } ,
710 ** would be better if these weren't hard coded...
713 static bool isArtificial (cstring s)
715 return (cstring_equalLit (s, "modifies")
716 || cstring_equalLit (s, "globals")
717 || cstring_equalLit (s, "warn")
718 || cstring_equalLit (s, "alt"));
721 void swallowMacro (void)
724 bool skipnext = FALSE;
726 while ((i = lminput ()) != EOF)
743 reader_checkUngetc (i, yyin);
751 reader_checkUngetc (i, yyin);
755 static int commentMarkerToken (cstring s)
759 while (s_parsetable[i].name != NULL)
761 DPRINTF (("Try :%s:%s:", s, s_parsetable[i].name));
763 if (cstring_equalLit (s, s_parsetable[i].name))
765 return s_parsetable[i].token;
774 static int tokenMacroCode (cstring s)
778 while (s_keytable[i].name != NULL)
780 if (cstring_equalLit (s, s_keytable[i].name))
782 if (s_keytable[i].token == QLINTFALLTHROUGH)
785 (FLG_WARNLINTCOMMENTS,
787 ("Traditional lint comment /*FALLTHROUGH*/ used. "
788 "Splint interprets this in the same way as most Unix lints, but it is "
789 "preferable to replace it with the /*@fallthrough@*/ "
794 else if (s_keytable[i].token == QLINTFALLTHRU)
797 (FLG_WARNLINTCOMMENTS,
799 ("Traditional lint comment /*FALLTHRU*/ used. "
800 "Splint interprets this in the same way as most Unix lints, but it is "
801 "preferable to replace it with the /*@fallthrough@*/ "
806 else if (s_keytable[i].token == QLINTNOTREACHED)
809 (FLG_WARNLINTCOMMENTS,
811 ("Traditional lint comment /*NOTREACHED*/ used. "
812 "Splint interprets this in the same way as most Unix lints, but it is "
813 "preferable to replace it with the /*@notreached@*/ "
814 "semantic comment."),
819 else if (s_keytable[i].token == QPRINTFLIKE)
821 setSpecialFunction (qual_createPrintfLike ());
824 else if (s_keytable[i].token == QLINTPRINTFLIKE)
827 (FLG_WARNLINTCOMMENTS,
829 ("Traditional lint comment /*PRINTFLIKE*/ used. "
830 "Splint interprets this in the same way as most Unix lints, but it is "
831 "preferable to replace it with either /*@printflike@*/, "
832 "/*@scanflike@*/ or /*@messagelike@*/."),
835 setSpecialFunction (qual_createPrintfLike ());
838 else if (s_keytable[i].token == QSCANFLIKE)
840 setSpecialFunction (qual_createScanfLike ());
843 else if (s_keytable[i].token == QMESSAGELIKE)
845 setSpecialFunction (qual_createMessageLike ());
848 else if (s_keytable[i].token == QARGSUSED)
851 (FLG_WARNLINTCOMMENTS,
853 ("Traditional lint comment /*ARGSUSED*/ used. "
854 "Splint interprets this in the same way as most Unix lints, but it is "
855 "preferable to use /*@unused@*/ annotations on "
856 "the unused parameters."),
863 return s_keytable[i].token;
872 static int lminput ()
874 if (savechar == '\0')
881 int save = (int) savechar;
887 static void lmsavechar (char c)
889 if (savechar == '\0') savechar = c;
892 llbuglit ("lmsavechar: override");
896 static int returnFloat (ctype ct, double f)
898 yylval.expr = exprNode_floatLiteral (f, ct, cstring_fromChars (yytext),
899 fileloc_decColumn (g_currentloc, tokLength));
904 static int returnInt (ctype ct, long i)
908 if (ctype_equal (ct, ctype_int))
912 c = context_typeofZero ();
916 c = context_typeofOne ();
920 yylval.expr = exprNode_numLiteral (c, cstring_fromChars (yytext),
921 fileloc_decColumn (g_currentloc, tokLength), i);
926 static int returnChar (char c)
928 yylval.expr = exprNode_charLiteral (c, cstring_fromChars (yytext),
929 fileloc_decColumn (g_currentloc, tokLength));
938 if (c != EOF && ((char)c == '\n'))
940 context_incLineno ();
946 static char macro_nextChar ()
948 static bool in_quote = FALSE, in_escape = FALSE, in_char = FALSE;
953 c = char_fromInt (ic);
955 if (!in_quote && !in_char && (c == '\\' || c == BEFORE_COMMENT_MARKER[0]))
959 while ((c = char_fromInt (lminput ())) != '\0' && c != '\n')
961 ; /* skip to newline */
964 context_incLineno ();
968 return macro_nextChar ();
975 else /* if (c == '@') */
977 llassert (FALSE); /*@i23@*/
978 if (handleLlSpecial () != BADTOK)
980 llerrorlit (FLG_SYNTAX, "Macro cannot use special syntax");
983 return macro_nextChar ();
986 else if (!in_escape && c == '\"')
988 in_quote = !in_quote;
990 else if (!in_escape && c == '\'')
994 else if ((in_quote || in_char) && c == '\\')
996 in_escape = !in_escape;
998 else if ((in_quote || in_char) && in_escape)
1002 else if (!in_quote && c == '/')
1006 if ((c2 = char_fromInt (lminput ())) == '*')
1010 while ((c2 = char_fromInt (lminput ())) != '\0'
1011 && c2 != '\n' && c2 != '*')
1018 while ((c2 = char_fromInt (lminput ())) != '\0'
1031 llfatalerror (cstring_makeLiteral ("Macro: bad comment!"));
1035 return macro_nextChar ();
1039 /*** putchar does not work! why? puts to stdio...??! ***/
1047 ** keeps semantic comments
1050 static char macro_nextCharC ()
1052 static bool in_quote = FALSE, in_escape = FALSE, in_char = FALSE;
1055 c = char_fromInt (lminput ());
1057 if (!in_quote && !in_char && c == '\\')
1059 while ((c = char_fromInt (lminput ())) != '\0' && c != '\n')
1061 ; /* skip to newline */
1064 context_incLineno ();
1068 return macro_nextCharC ();
1075 else if (!in_escape && c == '\"')
1077 in_quote = !in_quote;
1079 else if (!in_escape && c == '\'')
1083 else if ((in_quote || in_char) && c == '\\')
1085 in_escape = !in_escape;
1087 else if ((in_quote || in_char) && in_escape)
1091 else if (!in_quote && c == '/')
1095 if ((c2 = char_fromInt (lminput ())) == '*')
1099 while ((c2 = char_fromInt (lminput ())) != '\0'
1100 && c2 != '\n' && c2 != '*')
1107 while ((c2 = char_fromInt (lminput ())) != '\0'
1120 llfatalerror (cstring_makeLiteral ("Macro: bad comment!"));
1124 return macro_nextCharC ();
1135 ** skips whitespace (handles line continuations)
1136 ** returns first non-whitespace character
1139 static char skip_whitespace ()
1143 while ((c = macro_nextChar ()) == ' ' || c == '\t')
1151 static void handleMacro ()
1153 cstring mac = cstring_undefined;
1157 while (currentColumn () > 2)
1159 mac = cstring_appendChar (mac, ' ');
1163 c = macro_nextCharC ();
1165 if (c >= '0' && c <= '9')
1169 for (i = 0; i < ((c - '0') + 1); i++)
1171 mac = cstring_appendChar (mac, ' ');
1179 while (((c = macro_nextCharC ()) != '\0') && (c != '\n'))
1181 mac = cstring_appendChar (mac, c);
1185 macrocode = tokenMacroCode (mac);
1187 if (macrocode == BADTOK && !isArtificial (mac))
1189 context_addMacroCache (mac);
1198 context_incLineno ();
1202 static bool processMacro (void)
1207 cstring fname = cstring_undefined;
1209 bool isspecfcn = FALSE;
1210 bool isiter = FALSE;
1211 bool skipparam = FALSE;
1212 bool isenditer = FALSE;
1213 bool unknownm = FALSE;
1214 bool hasParams = FALSE;
1215 bool emptyMacro = FALSE;
1216 char c = skip_whitespace ();
1217 fileloc loc = fileloc_noColumn (g_currentloc);
1219 /* are both of these necessary? what do they mean? */
1220 uentryList specparams = uentryList_undefined;
1221 uentryList pn = uentryList_undefined;
1223 context_resetMacroMissingParams ();
1225 if (c == '\0' || c == '\n')
1227 llcontbug (cstring_makeLiteral ("Bad macro"));
1232 fname = cstring_appendChar (fname, c);
1234 while ((c = macro_nextChar ()) != '(' && c != '\0'
1235 && c != ' ' && c != '\t' && c != '\n')
1237 fname = cstring_appendChar (fname, c);
1240 if (c == ' ' || c == '\t' || c == '\n')
1246 while (c == ' ' || c == '\t')
1248 c = macro_nextChar ();
1262 hasParams = (c == '(');
1264 if (usymtab_exists (fname))
1266 e2 = usymtab_lookupExpose (fname);
1267 ct = uentry_getType (e2);
1269 if (uentry_isCodeDefined (e2)
1270 && fileloc_isUser (uentry_whereDefined (e2)))
1274 message ("Macro %s already defined", fname),
1277 uentry_showWhereDefined (e2);
1278 uentry_clearDefined (e2);
1281 if (uentry_isFunction (e2))
1283 uentry_setType (e2, ctype_unknown);
1286 context_enterUnknownMacro (e2);
1290 context_enterConstantMacro (e2);
1295 if (uentry_isForward (e2) && uentry_isFunction (e2))
1302 ("Parameterized macro has no prototype or specification: %s ",
1307 uentry_setType (e2, ctype_unknown);
1308 uentry_setFunctionDefined (e2, loc);
1309 uentry_setUsed (e2, fileloc_undefined);
1310 context_enterUnknownMacro (e2);
1314 if (uentry_isIter (e2))
1317 specparams = uentry_getParams (e2);
1318 noparams = uentryList_size (specparams);
1319 uentry_setDefined (e2, loc);
1320 context_enterIterDef (e2);
1322 else if (uentry_isEndIter (e2))
1325 uentry_setDefined (e2, loc);
1326 context_enterIterEnd (e2); /* don't care about it now */
1327 /* but should parse like an iter! */
1329 else if (uentry_isConstant (e2))
1335 message ("Constant %s implemented as parameterized macro",
1339 uentry_showWhereSpecified (e2);
1340 uentry_setType (e2, ctype_unknown);
1341 uentry_makeConstantFunction (e2);
1342 uentry_setDefined (e2, g_currentloc);
1343 uentry_setFunctionDefined (e2, g_currentloc);
1344 context_enterUnknownMacro (e2);
1348 if (!uentry_isSpecified (e2))
1350 fileloc oloc = uentry_whereDeclared (e2);
1352 if (fileloc_isLib (oloc))
1356 else if (fileloc_isUndefined (oloc)
1357 || fileloc_isPreproc (oloc))
1362 (FLG_MACROCONSTDECL,
1364 ("Macro constant %q not declared",
1365 uentry_getName (e2)),
1369 else if (!fileloc_withinLines (oloc, loc, 2))
1370 { /* bogus! will give errors if there is too much whitespace */
1374 ("Macro constant name %s does not match name in "
1375 "previous constant declaration. This constant "
1376 "is declared at %q", fname,
1377 fileloc_unparse (oloc)),
1382 context_enterConstantMacro (e2);
1383 cstring_free (fname);
1389 else if (ctype_isFunction (ct))
1392 specparams = ctype_argsFunction (ct);
1393 noparams = uentryList_size (specparams);
1395 uentry_setFunctionDefined (e2, loc);
1396 context_enterMacro (e2);
1398 else if (uentry_isVar (e2))
1404 message ("Variable %s implemented as parameterized macro",
1408 uentry_showWhereSpecified (e2);
1409 uentry_setType (e2, ctype_unknown);
1410 uentry_makeVarFunction (e2);
1411 uentry_setDefined (e2, g_currentloc);
1412 uentry_setFunctionDefined (e2, g_currentloc);
1413 context_enterUnknownMacro (e2);
1417 uentry ucons = uentry_makeConstant (fname,
1420 if (uentry_isExpandedMacro (e2))
1428 message ("Variable %s implemented by a macro",
1432 uentry_showWhereSpecified (e2);
1436 uentry_setDefined (e2, loc);
1437 uentry_setUsed (ucons, loc);
1439 context_enterConstantMacro (ucons);
1440 uentry_markOwned (ucons);
1441 cstring_free (fname);
1447 if (uentry_isDatatype (e2))
1451 message ("Type implemented as macro: %x",
1452 uentry_getName (e2)),
1453 message ("A type is implemented using a macro definition. A "
1454 "typedef should be used instead."),
1458 /* Must exit scope (not sure why a new scope was entered?) */
1459 usymtab_quietExitScope (g_currentloc);
1460 uentry_setDefined (e2, g_currentloc);
1466 (message ("Unexpanded macro not function or constant: %q",
1467 uentry_unparse (e2)));
1468 uentry_setType (e2, ctype_unknown);
1472 uentry_makeVarFunction (e2);
1473 uentry_setDefined (e2, g_currentloc);
1474 uentry_setFunctionDefined (e2, g_currentloc);
1475 context_enterUnknownMacro (e2);
1486 /* evans 2001-09-09 - if it has params, assume a function */
1490 (FLG_MACROMATCHNAME,
1491 message ("Unexpanded macro %s does not match name of a declared "
1492 "function. The name used in the control "
1493 "comment on the previous line should match.",
1497 ce = uentry_makeFunction (fname, ctype_unknown,
1501 warnClause_undefined,
1503 uentry_setUsed (ce, loc); /* perhaps bogus? */
1504 e2 = usymtab_supEntryReturn (ce);
1505 context_enterUnknownMacro (e2);
1510 (FLG_MACROMATCHNAME,
1511 message ("Unexpanded macro %s does not match name of a constant "
1512 "or iter declaration. The name used in the control "
1513 "comment on the previous line should match. "
1514 "(Assuming macro defines a constant.)",
1518 ce = uentry_makeConstant (fname, ctype_unknown, fileloc_undefined);
1519 uentry_setUsed (ce, loc); /* perhaps bogus? */
1520 e2 = usymtab_supEntryReturn (ce);
1522 context_enterConstantMacro (e2);
1523 cstring_free (fname);
1529 /* in macros, ( must follow immediatetly after name */
1535 c = skip_whitespace ();
1537 while (c != ')' && c != '\0')
1540 bool suppress = context_inSuppressRegion ();
1541 cstring paramname = cstring_undefined;
1544 ** save the parameter location
1548 context_saveLocation ();
1551 while (c != ' ' && c != '\t' && c != ',' && c != '\0' && c != ')')
1553 paramname = cstring_appendChar (paramname, c);
1554 c = macro_nextChar ();
1557 if (c == ' ' || c == '\t') c = skip_whitespace ();
1561 c = macro_nextChar ();
1562 if (c == ' ' || c == '\t') c = skip_whitespace ();
1567 llfatalerror (cstring_makeLiteral
1568 ("Bad macro syntax: uentryList"));
1571 if ((isspecfcn || isiter) && (paramno < noparams)
1572 && !uentry_isElipsisMarker (uentryList_getN
1573 (specparams, paramno)))
1575 fileloc sloc = context_getSaveLocation ();
1576 uentry decl = uentryList_getN (specparams, paramno);
1579 param = uentry_nameCopy (paramname, decl);
1581 uentry_setParam (param);
1582 sr = sRef_makeParam (paramno, uentry_getType (param), stateInfo_makeLoc (sloc));
1584 if (sRef_getNullState (sr) == NS_ABSNULL)
1586 ctype pt = ctype_realType (uentry_getType (param));
1588 if (ctype_isUser (pt))
1590 uentry te = usymtab_getTypeEntrySafe (ctype_typeId (pt));
1592 if (uentry_isValid (te))
1594 sRef_setStateFromUentry (sr, te);
1599 sRef_setNullState (sr, NS_UNKNOWN, sloc);
1603 uentry_setSref (param, sr);
1604 uentry_setDeclaredForceOnly (param, sloc);
1606 skipparam = isiter && uentry_isOut (uentryList_getN (specparams, paramno));
1610 fileloc sloc = context_getSaveLocation ();
1612 param = uentry_makeVariableSrefParam
1613 (paramname, ctype_unknown, fileloc_copy (sloc),
1614 sRef_makeParam (paramno, ctype_unknown, stateInfo_makeLoc (sloc)));
1615 DPRINTF (("Unknown param: %s", uentry_unparseFull (param)));
1616 cstring_free (paramname);
1618 sRef_setPosNull (uentry_getSref (param), sloc);
1619 uentry_setDeclaredForce (param, sloc);
1622 fileloc_free (sloc);
1627 llassert (!uentry_isElipsisMarker (param));
1631 sRef_makeUnsafe (uentry_getSref (param));
1634 pn = uentryList_add (pn, uentry_copy (param));
1635 usymtab_supEntry (param);
1639 /* don't add param */
1640 uentry_free (param);
1645 (void) macro_nextChar ();
1646 c = skip_whitespace ();
1654 if (isspecfcn || isiter)
1656 if (paramno != noparams && noparams >= 0)
1662 message ("Macro %s specified with %d args, defined with %d",
1663 fname, noparams, paramno),
1666 uentry_showWhereSpecified (e2);
1667 uentry_resetParams (e2, pn);
1672 uentry_resetParams (e2, pn);
1679 ** the form should be:
1681 ** # define newname oldname
1682 ** where oldname refers to a function matching the specification
1688 sRef_setGlobalScope ();
1689 usymtab_supGlobalEntry (uentry_makeVariableLoc (fname, ctype_unknown));
1690 sRef_clearGlobalScope ();
1694 context_setMacroMissingParams ();
1699 /* context_setuentryList (pn); */
1700 usymtab_enterScope ();
1703 cstring_free (fname);
1708 static bool handleSpecial (char *yyt)
1710 char *l = mstring_create (MAX_NAME_LENGTH);
1718 len_yyt = strlen (yyt +1) ;
1720 l = mstring_copy (yyt + 1);
1722 while ((c = char_fromInt (lminput ())) != '\n' && c != '\0')
1724 l = mstring_append(l, c);
1727 /* Need to safe original l for deallocating. */
1732 olc = cstring_fromChars (ol);
1734 if (cstring_equalPrefixLit (olc, "pragma"))
1736 char *pname = mstring_create (longUnsigned_fromInt (MAX_PRAGMA_LEN));
1737 char *opname = pname;
1738 char *ptr = ol + 6; /* pragma is six characters, plus space */
1742 /* skip whitespace */
1743 while (((c = *ptr) != '\0') && isspace (c))
1749 while (((c = *ptr) != '\0') && !isspace (c))
1753 if (len > MAX_PRAGMA_LEN)
1764 if (len == PRAGMA_LEN_EXPAND
1765 && mstring_equal (opname, PRAGMA_EXPAND))
1767 cstring exname = cstring_undefined;
1771 while (((c = *ptr) != '\0') && !isspace (c))
1773 exname = cstring_appendChar (exname, c);
1778 ue = usymtab_lookupExposeGlob (exname);
1780 if (uentry_isExpandedMacro (ue))
1782 if (fileloc_isPreproc (uentry_whereDefined (ue)))
1784 fileloc_setColumn (g_currentloc, 1);
1785 uentry_setDefined (ue, g_currentloc);
1789 cstring_free (exname);
1792 else if (cstring_equalPrefixLit (olc, "ident"))
1794 /* Some pre-processors will leave these in the code. Ignore rest of line */
1797 ** Yuk...Win32 filenames can have spaces in them...we need to read
1798 ** to the matching end quote.
1800 else if ((sscanf (ol, "line %d \"", &lineno) == 1)
1801 || (sscanf (ol, " %d \"", &lineno) == 1))
1807 /*@access cstring@*/
1808 while (*tmp != '\"' && *tmp != '\0')
1813 llassert (*tmp == '\"');
1819 while (*tmp != '\"' && *tmp != '\0')
1824 llassert (*tmp == '\"');
1828 # if defined(OS2) || defined(MSDOS) || defined(WIN32)
1831 ** DOS-like path delimiters get delivered in pairs, something like
1832 ** \"..\\\\file.h\", so we have to make it normal again. We do NOT
1833 ** remove the pre dirs yet as we usually specify tmp paths relative
1834 ** to the current directory, so tmp files would not get found in
1835 ** the hash table. If this method fails we try it again later.
1842 ** Skip past the drive marker.
1845 if (strchr (stmp, ':') != NULL)
1847 stmp = strchr (stmp, ':') + 1;
1850 while ((stmp = strchr (stmp, CONNECTCHAR)) != NULL )
1852 if (*(stmp+1) == CONNECTCHAR)
1854 memmove (stmp, stmp+1, strlen (stmp));
1860 fid = fileTable_lookupBase (context_fileTable (), fname);
1861 if (!(fileId_isValid (fid)))
1863 fname = removePreDirs (fname);
1864 fid = fileTable_lookupBase (context_fileTable (), fname);
1867 # else /* !defined(OS2) && !defined(MSDOS) */
1868 fname = removePreDirs (fname);
1869 fid = fileTable_lookupBase (context_fileTable (), fname);
1870 # endif /* !defined(OS2) && !defined(MSDOS) */
1872 if (!(fileId_isValid (fid)))
1874 if (context_inXHFile ())
1876 fid = fileTable_addXHFile (context_fileTable (), fname);
1878 else if (isHeaderFile (fname))
1880 fid = fileTable_addHeaderFile (context_fileTable (), fname);
1884 fid = fileTable_addFile (context_fileTable (), fname);
1888 setFileLine (fid, lineno);
1889 /*@noaccess cstring@*/
1891 else if ((sscanf (ol, "line %d", &lineno) == 1)
1892 || (sscanf (ol, " %d", &lineno) == 1))
1894 setLine (lineno); /* next line is <cr> */
1898 if (mstring_equal (ol, "")) {
1899 DPRINTF (("Empty pp command!"));
1901 ** evs 2000-05-16: This is a horrible kludge, to get around a bug (well, difficulty) in the pre-processor.
1902 ** We handle a plain # in the input file, by echoing it, and ignoring it in the post-pp-file.
1908 (FLG_UNRECOGDIRECTIVE,
1909 message ("Unrecognized pre-processor directive: #%s",
1910 cstring_fromChars (ol)),
1915 return FALSE; /* evans 2001-12-30: was: TRUE; */
1922 static int handleLlSpecial ()
1927 char *s = mstring_createEmpty ();
1933 loc = fileloc_copy (g_currentloc);
1934 DPRINTF (("Handle special: %s", fileloc_unparse (loc)));
1936 while (((ic = ninput ()) != 0) && isalpha (ic))
1939 s = mstring_append (s, c);
1943 DPRINTF (("Read: %s / %s", s, fileloc_unparse (g_currentloc)));
1946 if (charsread == 0 && ic == (int) AFTER_COMMENT_MARKER[0])
1950 llassert (ic == AFTER_COMMENT_MARKER[1]);
1956 return QNOMODS; /* special token no modifications token */
1960 DPRINTF (("Coment marker: %s", os));
1961 tok = commentMarkerToken (cstring_fromChars (os));
1965 tokLength = charsread;
1972 DPRINTF (("Not a comment marker..."));
1973 /* Add rest of the comment */
1975 if (ic != 0 && ic != EOF)
1979 s = mstring_append (s, c);
1982 while (((ic = ninput ()) != 0) && (ic != EOF)
1983 && (ic != AFTER_COMMENT_MARKER[0]))
1987 /* evans 2001-09-01 added to prevent assertion failures for uncloses syntactic comments */
1990 hasnl = TRUE; /* This prevents tokLength from being set later. */
1995 message ("Likely parse error: syntactic comment token spans multiple lines: %s",
1996 cstring_fromChars (s)),
2000 s = mstring_append (s, c);
2005 DPRINTF (("Read: %s / %s", s, fileloc_unparse (g_currentloc)));
2007 if (ic == AFTER_COMMENT_MARKER[0])
2010 llassert ((char) nc == AFTER_COMMENT_MARKER[1]);
2016 while (*s == ' ' || *s == '\t' || *s == '\n')
2021 if (*s == '-' || *s == '+' || *s == '=') /* setting flags */
2025 while (c == '-' || c == '+' || c == '=')
2027 ynm set = ynm_fromCodeChar (c);
2032 thisflag = cstring_fromChars (s);
2034 while ((c = *s) != '\0' && (c != '-') && (c != '=')
2035 && (c != '+') && (c != ' ') && (c != '\t') && (c != '\n'))
2042 if (!context_getFlag (FLG_NOCOMMENTS))
2044 cstring flagname = thisflag;
2045 flagcode fflag = flags_identifyFlag (flagname);
2047 if (flagcode_isSkip (fflag))
2051 else if (flagcode_isModeName (fflag))
2053 if (ynm_isMaybe (set))
2058 ("Semantic comment attempts to restore flag %s. "
2059 "A mode flag cannot be restored.",
2064 context_setMode (flagname);
2067 else if (flagcode_isInvalid (fflag))
2070 (FLG_UNRECOGFLAGCOMMENTS,
2071 message ("Unrecognized option in semantic comment: %s",
2075 else if (flagcode_isGlobalFlag (fflag))
2080 ("Semantic comment attempts to set global flag %s. "
2081 "A global flag cannot be set locally.",
2087 context_fileSetFlag (fflag, set, loc);
2089 if (flagcode_hasArgument (fflag))
2091 if (ynm_isMaybe (set))
2096 ("Semantic comment attempts to restore flag %s. "
2097 "A flag for setting a value cannot be restored.",
2102 { /* cut-and-pastied from llmain...blecch */
2103 cstring extra = cstring_undefined;
2109 rest = mstring_copy (s);
2113 while ((rchar = *rest) != '\0'
2114 && (isspace (rchar)))
2120 while ((rchar = *rest) != '\0'
2121 && !isspace (rchar))
2123 extra = cstring_appendChar (extra, rchar);
2130 if (cstring_isUndefined (extra))
2135 ("Flag %s (in semantic comment) must be followed by an argument",
2136 flagcode_unparse (fflag)));
2142 if (flagcode_hasNumber (fflag))
2144 setValueFlag (fflag, extra);
2146 else if (flagcode_hasChar (fflag))
2148 setValueFlag (fflag, extra);
2150 else if (flagcode_hasString (fflag))
2152 setStringFlag (fflag, extra);
2169 while ((c == ' ') || (c == '\t') || (c == '\n'))
2175 if (context_inHeader () && !isArtificial (cstring_fromChars (os)))
2177 DPRINTF (("Here adding comment: %s", os));
2178 context_addComment (cstring_fromCharsNew (os), loc);
2190 annotationInfo ainfo;
2192 while (*s != '\0' && *s != ' ' && *s != '\t' && *s != '\n')
2204 t = cstring_toCharsSafe (cstring_downcase (cstring_fromChars (t)));
2205 macrocode = tokenMacroCode (cstring_fromChars (t));
2207 if (macrocode != BADTOK)
2209 tokLength = hasnl ? 0 : mstring_length (t);
2215 if (macrocode == SKIPTOK)
2223 ainfo = context_lookupAnnotation (cstring_fromChars (os));
2225 if (annotationInfo_isDefined (ainfo)) {
2226 DPRINTF (("Found annotation: %s", annotationInfo_unparse (ainfo)));
2227 /*@i324@*/ yylval.annotation = ainfo;
2235 if (context_inHeader ())
2242 if ((context_inMacro () || context_inGlobalContext ())
2243 && macrocode != SKIPTOK
2244 && !isArtificial (cstring_fromChars (os)))
2246 if (context_processingMacros ())
2248 /* evans 2002-02-24: don't add comments when procssing macros */
2252 context_addComment (cstring_fromCharsNew (os), loc);
2266 if (mstring_equal (t, "ignore"))
2268 if (!context_getFlag (FLG_NOCOMMENTS))
2270 context_enterSuppressRegion (loc);
2273 else if ((*t == 'i' || *t == 't')
2274 && (*(t + 1) == '\0'))
2276 if (!context_getFlag (FLG_NOCOMMENTS)
2277 && (*t == 'i' || context_getFlag (FLG_TMPCOMMENTS)))
2279 context_enterSuppressLine (-1, loc); /* infinite suppression */
2282 else if (((*t == 'i') || (*t == 't'))
2283 && ((*(t + 1) >= '0' && *(t + 1) <= '9')))
2285 bool tmpcomment = (*t == 't');
2287 char *tt = t; /* don't mangle t, since it is free'd */
2290 if (lc >= '0' && lc <= '9')
2292 val = (int)(lc - '0');
2295 while (lc >= '0' && lc <= '9')
2303 if (!context_getFlag (FLG_NOCOMMENTS)
2304 && (!tmpcomment || context_getFlag (FLG_TMPCOMMENTS)))
2306 DPRINTF (("Here: enter suppress: %s", fileloc_unparse (loc)));
2307 context_enterSuppressLine (val, loc);
2310 else if (mstring_equal (t, "end"))
2312 if (!context_getFlag (FLG_NOCOMMENTS))
2314 context_exitSuppressRegion (loc);
2317 else if (mstring_equal (t, "notfunction"))
2319 ; /* handled by pcpp */
2321 else if (mstring_equal (t, "access"))
2327 while ((c = *s) && (c == ' ' || c == '\t' || c == '\n'))
2337 tname = cstring_fromChars (s);
2339 while ((c = *s) != '\0' && c != ' '
2340 && c != '\t' && c != '\n' && c != ',')
2347 DPRINTF (("Access %s", tname));
2349 if (!context_getFlag (FLG_NOCOMMENTS)
2350 && !context_getFlag (FLG_NOACCESS))
2352 if (usymtab_existsType (tname))
2354 typeId uid = usymtab_getTypeId (tname);
2355 uentry ue = usymtab_getTypeEntry (uid);
2357 if (uentry_isAbstractDatatype (ue))
2359 context_addFileAccessType (uid);
2360 DPRINTF (("Adding access to: %s / %d", tname, uid));
2367 ("Non-abstract type %s used in access comment",
2374 if (!(context_inSuppressRegion ()
2375 || context_inSuppressZone (loc)))
2380 ("Unrecognized type %s used in access comment",
2392 if (c != ',' && c != ' ')
2398 else if (mstring_equal (t, "noaccess"))
2405 while ((lc = *s) && (lc == ' ' || lc == '\t' || lc == '\n'))
2415 tname = cstring_fromChars (s);
2417 while ((lc = *s) != '\0' && lc != ' ' && lc != '\t'
2418 && lc != '\n' && lc != ',')
2425 if (!context_getFlag (FLG_NOCOMMENTS)
2426 && !context_getFlag (FLG_NOACCESS))
2428 if (usymtab_existsType (tname))
2430 typeId tuid = usymtab_getTypeId (tname);
2432 if (context_couldHaveAccess (tuid))
2434 DPRINTF (("Removing access: %s", tname));
2435 context_removeFileAccessType (tuid);
2439 if (!(context_inSuppressRegion ()
2440 || context_inSuppressZone (loc)))
2442 uentry ue = usymtab_getTypeEntry (tuid);
2444 if (uentry_isAbstractDatatype (ue))
2449 ("Non-accessible abstract type %s used in noaccess comment",
2458 ("Non-abstract type %s used in noaccess comment",
2467 if (!(context_inSuppressRegion ()
2468 || context_inSuppressZone (loc)))
2473 ("Unrecognized type %s used in noaccess comment",
2485 if (lc != ',' && lc != ' ')
2493 voptgenerror (FLG_UNRECOGCOMMENTS,
2494 message ("Semantic comment unrecognized: %s",
2495 cstring_fromChars (os)),
2507 static /*@only@*/ cstring makeIdentifier (char *s)
2509 char *c = mstring_create (size_toInt (strlen (s)) + 1);
2510 cstring id = cstring_fromChars (c);
2512 while (isalnum (*s) || (*s == '_') || (*s == '$'))
2521 /*@observer@*/ /*@dependent@*/ uentry coerceId (cstring cn)
2523 if (!(usymtab_exists (cn)))
2525 fileloc loc = fileloc_createExternal ();
2528 ** We need to put this in a global scope, otherwise the sRef will be deallocated.
2531 uentry ce = uentry_makeUnrecognized (cn, loc);
2533 if (!context_inIterEnd ())
2537 message ("Unrecognized (possibly system) identifier: %q",
2538 uentry_getName (ce)),
2545 return (usymtab_lookup (cn));
2549 ** like, coerceId, but doesn't supercede for iters
2552 /*@observer@*/ uentry coerceIterId (cstring cn)
2554 if (!(usymtab_exists (cn)))
2556 return uentry_undefined;
2559 return (usymtab_lookup (cn));
2562 /*@observer@*/ cstring LastIdentifier ()
2564 return (lastidprocessed);
2567 static int processIdentifier (cstring id)
2571 if (context_getFlag (FLG_GRAMMAR))
2573 lldiagmsg (message ("Process identifier: %s", id));
2576 context_clearJustPopped ();
2577 lastidprocessed = id;
2579 if (context_inFunctionHeader ())
2581 int tok = commentMarkerToken (id);
2582 DPRINTF (("in function decl..."));
2590 tok = tokenMacroCode (id);
2598 annotationInfo ainfo;
2600 if (expectingMetaStateName)
2602 metaStateInfo msinfo = context_lookupMetaStateInfo (id);
2604 if (metaStateInfo_isDefined (msinfo))
2606 yylval.msinfo = msinfo;
2607 return METASTATE_NAME;
2611 DPRINTF (("Not meta state name: %s", cstring_toCharsSafe (id)));
2615 ainfo = context_lookupAnnotation (id);
2617 if (annotationInfo_isDefined (ainfo))
2619 DPRINTF (("Found annotation: %s", annotationInfo_unparse (ainfo)));
2620 /*@i324@*/ yylval.annotation = ainfo;
2625 DPRINTF (("Not annotation: %s", id));
2631 /* Consider handling: Defined by C99 as static const char __func__[] */
2633 if (context_getFlag (FLG_GNUEXTENSIONS))
2637 if (cstring_equalLit (id, "__stdcall")
2638 || cstring_equalLit (id, "__cdecl")
2639 || cstring_equalLit (id, "__extension__"))
2643 else if (cstring_equalLit (id, "__volatile__"))
2647 else if (cstring_equalLit (id, "__signed"))
2651 else if (cstring_equalLit (id, "__unsigned"))
2655 else if (cstring_equalLit (id, "__const__"))
2659 else if (cstring_equalLit (id, "__alignof__"))
2661 tok = CALIGNOF; /* alignof is parsed like sizeof */
2663 else if (cstring_equalLit (id, "__typeof__"))
2667 else if (cstring_equalLit (id, "typeof"))
2671 else if (cstring_equalLit (id, "__FUNCTION__")
2672 || cstring_equalLit (id, "__PRETTY_FUNCTION__"))
2674 /* These tokens hold the name of the current function as strings */
2675 /* evans 2001-12-30: changed from exprNode_stringLiteral; bug reported by Jim Zelenka. */
2676 yylval.expr = exprNode_makeConstantString (id, fileloc_copy (g_currentloc));
2678 lastWasString = TRUE;
2682 else if (cstring_equalLit (id, "__attribute__")
2683 || cstring_equalLit (id, "__asm__")
2684 || cstring_equalLit (id, "_asm")
2685 || cstring_equalLit (id, "__asm")
2686 || cstring_equalLit (id, "__declspec"))
2689 bool useparens = FALSE;
2690 bool usebraces = FALSE;
2691 bool inquote = FALSE;
2692 bool inescape = FALSE;
2695 while ((ic = input ()) != EOF)
2702 else if (ic == '\\')
2706 else if (ic == '\"')
2742 else if (ic == ')' && useparens)
2745 if (depth == 0) break;
2747 else if (ic == '}' && usebraces)
2750 if (depth == 0) break;
2753 && !usebraces && !useparens
2754 && cstring_equalLit (id, "__asm"))
2757 ** We need this because some MS VC++ include files
2758 ** have __asm mov ... }
2759 ** Its a kludge, but otherwise would need to parse
2768 context_incLineno ();
2770 if (cstring_equalLit (id, "__asm")
2771 && !useparens && !usebraces)
2778 llassert ((useparens && ic == ')')
2779 || (usebraces && ic == '}')
2780 || (!useparens && !usebraces));
2784 else if (cstring_equalLit (id, "inline")
2785 || cstring_equalLit (id, "__inline")
2786 || cstring_equalLit (id, "_inline")
2787 || cstring_equalLit (id, "__inline__"))
2798 le = usymtab_lookupSafe (id);
2800 /*@-dependenttrans@*/
2802 if (uentry_isIter (le))
2804 /*@i32@*/ yylval.entry = le;
2807 else if (uentry_isEndIter (le))
2809 /*@i32@*/ yylval.entry = le;
2810 return (ITER_ENDNAME);
2812 else if (uentry_isUndefined (le))
2816 /* avoid parse errors for certain system built ins */
2818 if (g_expectingTypeName && (cstring_firstChar (id) == '_')
2819 && (cstring_secondChar (id) == '_'))
2821 return (TYPE_NAME_OR_ID);
2824 return (NEW_IDENTIFIER);
2826 else if (!uentry_isDeclared (le) && !uentry_isCodeDefined (le))
2828 if (uentry_isDatatype (le))
2831 return (NEW_IDENTIFIER);
2835 /*@i32@*/ yylval.entry = le;
2836 return (IDENTIFIER);
2839 else if (uentry_isDatatype (le))
2841 if (!g_expectingTypeName)
2845 return (NEW_IDENTIFIER);
2849 yylval.ctyp = uentry_getAbstractType (le);
2851 uentry_setUsed (le, g_currentloc);
2857 /*@i32@*/ yylval.entry = le;
2858 return (IDENTIFIER);
2861 /*@=dependenttrans@*/
2864 static bool processHashIdentifier (/*@only@*/ cstring id)
2866 if (context_inMacro () || context_inIterDef () ||
2867 context_inIterEnd ())
2871 context_clearJustPopped ();
2873 lastidprocessed = id;
2874 le = usymtab_lookupSafe (id);
2876 if (uentry_isParam (le) || uentry_isRefParam (le))
2888 ** Will be handled by handleLlSpecial
2897 static /*@only@*/ exprNode processString ()
2901 char *nl = strchr (yytext, '\n');
2902 cstring ns = cstring_fromCharsNew (yytext);
2906 loc = fileloc_copy (g_currentloc);
2907 addColumn (cstring_length (ns));
2913 loc = fileloc_copy (g_currentloc);
2915 context_incLineno ();
2917 while ((nl = strchr ((nl + 1), '\n')) != NULL)
2919 context_incLineno ();
2925 res = exprNode_stringLiteral (ns, loc);
2930 ** process a wide character string L"...."
2933 static /*@only@*/ exprNode processWideString ()
2937 char *nl = strchr (yytext, '\n');
2940 llassert (*yytext == 'L');
2943 ns = cstring_fromCharsNew (yytext);
2947 loc = fileloc_copy (g_currentloc);
2948 addColumn (cstring_length (ns));
2954 loc = fileloc_copy (g_currentloc);
2956 context_incLineno ();
2958 while ((nl = strchr ((nl + 1), '\n')) != NULL)
2960 context_incLineno ();
2965 res = exprNode_wideStringLiteral (ns, loc);
2975 llassert (*yytext != '\0');
2976 fchar = *(yytext + 1);
2977 if (fchar != '\\') return fchar;
2979 next = *(yytext + 2);
2983 case 'n': return '\n';
2984 case 't': return '\t';
2985 case '\"': return '\"';
2986 case '\'': return '\'';
2987 case '\\': return '\\';
2988 default: return '\0';
2993 double processFloat ()
2995 double ret = atof (yytext);
3006 llassert (yytext[0] == '0'
3007 && (yytext[1] == 'X' || yytext[1] == 'x'));
3009 while (yytext[index] != '\0') {
3011 char c = yytext[index];
3013 if (c >= '0' && c <= '9') {
3014 tval = (int) c - (int) '0';
3015 } else if (c >= 'A' && c <= 'F') {
3016 tval = (int) c - (int) 'A' + 10;
3017 } else if (c >= 'a' && c <= 'f') {
3018 tval = (int) c - (int) 'a' + 10;
3019 } else if (c == 'U' || c == 'L' || c == 'u' || c == 'l') {
3021 while (yytext[index] != '\0') {
3022 if (c == 'U' || c == 'L' || c == 'u' || c == 'l') {
3027 message ("Invalid character (%c) following specifier in hex constant: %s",
3028 c, cstring_fromChars (yytext)),
3038 message ("Invalid character (%c) in hex constant: %s",
3039 c, cstring_fromChars (yytext)),
3044 val = (val * 16) + tval;
3048 DPRINTF (("Hex constant: %s = %ld", yytext, val));
3053 long processOctal ()
3058 llassert (yytext[0] == '0' && yytext[1] != 'X' && yytext[1] != 'x');
3060 while (yytext[index] != '\0') {
3062 char c = yytext[index];
3064 if (c >= '0' && c <= '7') {
3065 tval = (int) c - (int) '0';
3066 } else if (c == 'U' || c == 'L' || c == 'u' || c == 'l') {
3068 while (yytext[index] != '\0') {
3069 if (c == 'U' || c == 'L' || c == 'u' || c == 'l') {
3074 message ("Invalid character (%c) following specifier in octal constant: %s",
3075 c, cstring_fromChars (yytext)),
3085 message ("Invalid character (%c) in octal constant: %s",
3086 c, cstring_fromChars (yytext)),
3091 val = (val * 8) + tval;
3095 DPRINTF (("Octal constant: %s = %ld", yytext, val));
3102 return (atol (yytext));
3106 processSpec (int tok)
3108 size_t length = strlen (yytext);
3112 setTokLengthT (length);
3118 context_saveLocation ();
3119 setTokLengthT (length);
3120 return (processIdentifier (makeIdentifier (yytext)));
3124 void cscanner_expectingMetaStateName ()
3126 llassert (!expectingMetaStateName);
3127 llassert (context_inFunctionHeader ());
3128 expectingMetaStateName = TRUE;
3131 void cscanner_clearExpectingMetaStateName ()
3133 llassert (expectingMetaStateName);
3134 expectingMetaStateName = FALSE;