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 s_lastidprocessed = cstring_undefined;
96 static int lminput (void);
97 static int tokLength = 0;
98 static bool s_inSpecPart = FALSE;
99 static int s_whichSpecPart;
100 static bool continueLine = FALSE;
102 static int ninput (void);
103 static char processChar (void);
104 static double processFloat (void);
105 static /*@only@*/ exprNode processString (void) ;
106 static /*@only@*/ exprNode processWideString (void) ;
107 static long processDec (void);
108 static long processHex (void);
109 static long processOctal (void);
110 static int processIdentifier (/*@only@*/ cstring)
111 /*@globals s_lastidprocessed@*/ ;
112 static bool processHashIdentifier (/*@only@*/ cstring)
113 /*@globals s_lastidprocessed@*/ ;
115 static int processSpec (int);
116 static bool handleSpecial (char *);
117 static int handleLlSpecial (void);
118 static void handleMacro (void);
119 static bool processMacro (void);
120 static /*@only@*/ cstring makeIdentifier (char *);
122 /* yes, this is exported! */
123 bool g_expectingTypeName = TRUE; /* beginning of file can be type name! */
125 static bool expectingMetaStateName = FALSE;
127 static int returnInt (ctype, long);
128 static int returnFloat (ctype, double);
129 static int returnChar (char);
130 static void setTokLength (int) /*@modifies g_currentloc@*/ ;
131 static void setTokLengthT (size_t) /*@modifies g_currentloc@*/ ;
133 static void advanceLine (void)
140 # define RETURN_INT(c,i) \
141 do { lastWasString = FALSE; \
142 return (returnInt (c, i)); } while (FALSE)
144 # define RETURN_FLOAT(c,f) \
145 do { lastWasString = FALSE; \
146 return (returnFloat (c, f)); \
149 # define RETURN_CHAR(c) \
150 do { lastWasString = FALSE; \
151 return (returnChar (c)); \
154 # define RETURN_TOK(t) \
155 do { yylval.tok = lltok_create (t, fileloc_decColumn (g_currentloc, tokLength)); \
157 lastWasString = FALSE; \
158 return (t); } while (FALSE)
160 # define RETURN_TYPE(t, ct) \
161 do { yylval.ctyp = ct; tokLength = 0; return (t); } while (FALSE)
163 /* don't fileloc_decColumn (g_currentloc, tokLength));
164 the string could have \n's in it!
167 # define RETURN_STRING(c) \
168 do { yylval.expr = exprNode_stringLiteral (c, fileloc_decColumn (g_currentloc, tokLength)); \
170 lastWasString = TRUE; \
171 return (CCONSTANT); } while (FALSE)
173 # define RETURN_EXPR(e) \
174 do { yylval.expr = e; \
176 lastWasString = TRUE; \
177 return (CCONSTANT); } while (FALSE)
181 static void setTokLength (int len)
185 DPRINTF (("Set tok length: %d", len));
188 static void setTokLengthT (size_t len)
190 setTokLength (size_toInt (len));
193 # include "flex.head"
195 /*@-unrecog@*/ /*@i5343@*/
201 "/*" { llfatalerror (cstring_makeLiteral ("Comment in pre-processor output")); }
203 "#"{Letter}({Letter}|{Digit})* {
204 context_saveLocation ();
205 setTokLength (longUnsigned_toInt (mstring_length (yytext)));
207 if (processHashIdentifier (makeIdentifier (yytext + 1)))
211 /* was nothing! */ /*@i32@*/
212 RETURN_STRING (cstring_makeLiteral ("\"\""));
216 RETURN_STRING (cstring_makeLiteral ("\"\""));
221 if (handleSpecial (yytext))
228 "#" { if (handleSpecial (yytext))
230 setTokLength (1); RETURN_TOK (0);
233 "..." { setTokLength (3); RETURN_TOK (CTOK_ELIPSIS); }
234 "break" { setTokLength (5); RETURN_TOK (BREAK); }
235 "case" { setTokLength (4); RETURN_TOK (CASE); }
236 "continue" { setTokLength (8); RETURN_TOK (CONTINUE); }
237 "default" { setTokLength (7); RETURN_TOK (DEFAULT); }
238 "do" { setTokLength (2); RETURN_TOK (DO); }
239 "else" { setTokLength (4); RETURN_TOK (CELSE); }
240 "for" { setTokLength (3); RETURN_TOK (CFOR); }
241 "goto" { setTokLength (4); RETURN_TOK (GOTO); }
242 "if" { setTokLength (2); RETURN_TOK (CIF); }
243 "return" { setTokLength (6); RETURN_TOK (RETURN); }
244 "sizeof" { setTokLength (6); RETURN_TOK (CSIZEOF); }
245 "offsetof" { setTokLength (8); RETURN_TOK (COFFSETOF); }
246 "switch" { setTokLength (6); RETURN_TOK (SWITCH); }
247 "while" { setTokLength (5); RETURN_TOK (WHILE); }
248 "va_arg" { setTokLength (6); RETURN_TOK (VA_ARG); }
249 "va_dcl" { setTokLength (6); RETURN_TOK (VA_DCL); }
251 /* gcc extension...this might not be appropriate */
252 setTokLength (6); RETURN_TOK (QINLINE); }
254 "struct" { setTokLength (6); RETURN_TOK (CSTRUCT); }
255 "typedef" { setTokLength (7); RETURN_TOK (CTYPEDEF); }
257 "union" { setTokLength (5); RETURN_TOK (CUNION); }
258 "enum" { setTokLength (4); RETURN_TOK (CENUM); }
260 "void" { setTokLength (4); RETURN_TYPE (CVOID, ctype_void); }
261 "int" { setTokLength (3); RETURN_TYPE (CINT, ctype_int); }
262 "double" { setTokLength (6); RETURN_TYPE (CDOUBLE, ctype_double); }
263 "char" { setTokLength (4); RETURN_TYPE (CGCHAR, ctype_char); }
264 "float" { setTokLength (5); RETURN_TYPE (CGFLOAT, ctype_float); }
266 "long" { setTokLength (4); RETURN_TOK (QLONG); }
267 "short" { setTokLength (5); RETURN_TOK (QSHORT); }
268 "unsigned" { setTokLength (8); RETURN_TOK (QUNSIGNED); }
269 "signed" { setTokLength (6); RETURN_TOK (QSIGNED); }
271 "volatile" { setTokLength (8); RETURN_TOK (QVOLATILE); }
272 "const" { setTokLength (5); RETURN_TOK (QCONST); }
273 "restrict" { setTokLength (8); RETURN_TOK (QRESTRICT); }
275 /* some systems expect this! [gack!] */
276 "__const" { setTokLength (7); RETURN_TOK (QCONST); }
278 "extern" { setTokLength (6); RETURN_TOK (QEXTERN); }
279 "auto" { setTokLength (4); RETURN_TOK (QAUTO); }
280 "register" { setTokLength (8); RETURN_TOK (QREGISTER); }
281 "static" { setTokLength (6); RETURN_TOK (QSTATIC); }
283 \"(\\.|[^\\"])*\"([ \t\n]*\"(\\.|[^\\"])*\")* { RETURN_EXPR (processString ()); }
284 L\"(\\.|[^\\"])*\"([ \t\n]*\"(\\.|[^\\"])*\")* { RETURN_EXPR (processWideString ()); }
285 "out" { return (processSpec (QOUT)); }
286 "in" { return (processSpec (QIN)); }
287 "partial" { return (processSpec (QPARTIAL)); }
288 "special" { return (processSpec (QSPECIAL)); }
289 "anytype" { return (processSpec (QANYTYPE)); }
290 "integraltype" { return (processSpec (QINTEGRALTYPE)); }
291 "unsignedintegraltype" { return (processSpec (QUNSIGNEDINTEGRALTYPE)); }
292 "signedintegraltype" { return (processSpec (QSIGNEDINTEGRALTYPE)); }
293 "keep" { return (processSpec (QKEEP)); }
294 "null" { return (processSpec (QNULL)); }
295 "notnull" { return (processSpec (QNOTNULL)); }
296 "isnull" { return (processSpec (QISNULL)); }
297 "truenull" { return (processSpec (QTRUENULL)); }
298 "falsenull" { return (processSpec (QFALSENULL)); }
299 "nullwhentrue" { return (processSpec (QTRUENULL)); }
300 "nullwhenfalse" { return (processSpec (QFALSENULL)); }
301 "relnull" { return (processSpec (QRELNULL)); }
302 "reldef" { return (processSpec (QRELDEF)); }
303 "exposed" { return (processSpec (QEXPOSED)); }
304 "newref" { return (processSpec (QNEWREF)); }
305 "tempref" { return (processSpec (QTEMPREF)); }
306 "killref" { return (processSpec (QKILLREF)); }
307 "refcounted" { return (processSpec (QREFCOUNTED)); }
308 "checked" { return (processSpec (QCHECKED)); }
309 "checkmod" { return (processSpec (QCHECKMOD)); }
310 "checkedstrict" { return (processSpec (QCHECKEDSTRICT)); }
311 "unchecked" { return (processSpec (QUNCHECKED)); }
312 "only" { return (processSpec (QONLY)); }
313 "owned" { return (processSpec (QOWNED)); }
314 "observer" { return (processSpec (QOBSERVER)); }
315 "dependent" { return (processSpec (QDEPENDENT)); }
316 "unused" { return (processSpec (QUNUSED)); }
317 "external" { return (processSpec (QEXTERNAL)); }
318 "sef" { return (processSpec (QSEF)); }
319 "shared" { return (processSpec (QSHARED)); }
320 "yield" { return (processSpec (QYIELD)); }
321 "undef" { return (processSpec (QUNDEF)); }
322 "killed" { return (processSpec (QKILLED)); }
323 "nullterminated" { return (processSpec (QNULLTERMINATED));}
324 "MaxSet" { return (processSpec (QMAXSET));}
325 "MaxRead" { return (processSpec (QMAXREAD));}
326 "maxSet" { return (processSpec (QMAXSET));}
327 "maxRead" { return (processSpec (QMAXREAD));}
329 {Letter}({Letter}|{Digit})* { int tok;
330 context_saveLocation ();
331 setTokLength (longUnsigned_toInt (mstring_length (yytext)));
332 tok = processIdentifier (makeIdentifier (yytext));
338 0[xX]{H}+ { setTokLengthT (mstring_length (yytext));
339 RETURN_INT (ctype_int, processHex ()); /* evs 2000-05-17 was ctype_uint */
341 0[xX]{H}+{L} { setTokLengthT (mstring_length (yytext));
342 RETURN_INT (ctype_lint, processHex ()); }
343 0[xX]{H}+{L}{L} { setTokLengthT (mstring_length (yytext));
344 RETURN_INT (ctype_llint, processHex ()); }
345 0[xX]{H}+{U} { setTokLengthT (mstring_length (yytext));
346 RETURN_INT (ctype_uint, processHex ()); }
347 0[xX]{H}+{ULSuffix} { setTokLengthT (mstring_length (yytext));
348 RETURN_INT (ctype_ulint, processHex ()); }
349 0[xX]{H}+{U}{L}{L} { setTokLengthT (mstring_length (yytext));
350 RETURN_INT (ctype_ullint, processHex ()); }
351 0[xX]{H}+{L}{L}{U} { setTokLengthT (mstring_length (yytext));
352 RETURN_INT (ctype_ullint, processHex ()); }
353 0{Digit}+ { setTokLengthT (mstring_length (yytext));
354 RETURN_INT (ctype_int, processOctal ()); }
355 0{Digit}+{U} { setTokLengthT (mstring_length (yytext));
356 RETURN_INT (ctype_uint, processOctal ()); }
357 0{Digit}+{L} { setTokLengthT (mstring_length (yytext));
358 RETURN_INT (ctype_lint, processOctal ()); }
359 0{Digit}+{L}{L} { setTokLengthT (mstring_length (yytext));
360 RETURN_INT (ctype_llint, processOctal ()); }
361 0{Digit}+{ULSuffix} { setTokLengthT (mstring_length (yytext));
362 RETURN_INT (ctype_ulint, processOctal ()); }
363 0{Digit}+{U}{L}{L} { setTokLengthT (mstring_length (yytext));
364 RETURN_INT (ctype_ullint, processOctal ()); }
365 0{Digit}+{L}{L}{U} { setTokLengthT (mstring_length (yytext));
366 RETURN_INT (ctype_ullint, processOctal ()); }
367 {Digit}+ { setTokLengthT (mstring_length (yytext));
368 RETURN_INT (ctype_int, processDec ()); }
369 {Digit}+{U} { setTokLengthT (mstring_length (yytext));
370 RETURN_INT (ctype_uint, processDec ()); }
371 {Digit}+{L} { setTokLengthT (mstring_length (yytext));
372 RETURN_INT (ctype_lint, processDec ()); }
373 {Digit}+{L}{L} { setTokLengthT (mstring_length (yytext));
374 RETURN_INT (ctype_llint, processDec ()); }
375 {Digit}+{ULSuffix} { setTokLengthT (mstring_length (yytext));
376 RETURN_INT (ctype_ulint, processDec ()); }
377 {Digit}+{U}{L}{L} { setTokLengthT (mstring_length (yytext));
378 RETURN_INT (ctype_ullint, processDec ()); }
379 {Digit}+{L}{L}{U} { setTokLengthT (mstring_length (yytext));
380 RETURN_INT (ctype_ullint, processDec ()); }
381 '(\\.|[^\\'])+' { setTokLengthT (mstring_length (yytext));
382 RETURN_CHAR (processChar ()); }
383 L'(\\.|[^\\'])+' { setTokLengthT (mstring_length (yytext));
384 RETURN_CHAR (processChar ()); }
385 {Digit}+{E}[fF] { setTokLengthT (mstring_length (yytext));
386 RETURN_FLOAT (ctype_float, processFloat ()); }
387 {Digit}+{E}[lL] { setTokLengthT (mstring_length (yytext));
388 RETURN_FLOAT (ctype_ldouble, processFloat ()); }
389 {Digit}+{E} { setTokLengthT (mstring_length (yytext));
390 RETURN_FLOAT (ctype_double, processFloat ()); }
392 {Digit}*"."{Digit}+({E})?[fF] { setTokLengthT (mstring_length (yytext));
393 RETURN_FLOAT (ctype_float, processFloat ()); }
394 {Digit}*"."{Digit}+({E})?[lL] { setTokLengthT (mstring_length (yytext));
395 RETURN_FLOAT (ctype_ldouble, processFloat ()); }
396 {Digit}*"."{Digit}+({E})? { setTokLengthT (mstring_length (yytext));
397 RETURN_FLOAT (ctype_double, processFloat ()); }
399 {Digit}+"."{Digit}*({E})?[fF] { setTokLengthT (mstring_length (yytext));
400 RETURN_FLOAT (ctype_float, processFloat ()); }
401 {Digit}+"."{Digit}*({E})?[lL] { setTokLengthT (mstring_length (yytext));
402 RETURN_FLOAT (ctype_ldouble, processFloat ()); }
403 {Digit}+"."{Digit}*({E})? { setTokLengthT (mstring_length (yytext));
404 RETURN_FLOAT (ctype_double, processFloat ()); }
406 ">>=" { setTokLength (3); RETURN_TOK (RIGHT_ASSIGN); }
407 "<<=" { setTokLength (3); RETURN_TOK (LEFT_ASSIGN); }
408 "+=" { setTokLength (2); RETURN_TOK (ADD_ASSIGN); }
409 "-=" { setTokLength (2); RETURN_TOK (SUB_ASSIGN); }
410 "*=" { setTokLength (2); RETURN_TOK (MUL_ASSIGN); }
411 "/=" { setTokLength (2); RETURN_TOK (DIV_ASSIGN); }
412 "%=" { setTokLength (2); RETURN_TOK (MOD_ASSIGN); }
413 "&=" { setTokLength (2); RETURN_TOK (AND_ASSIGN); }
414 "^=" { setTokLength (2); RETURN_TOK (XOR_ASSIGN); }
415 "|=" { setTokLength (2); RETURN_TOK (OR_ASSIGN); }
416 ">>" { setTokLength (2); RETURN_TOK (RIGHT_OP); }
417 "<<" { setTokLength (2); RETURN_TOK (LEFT_OP); }
418 "++" { setTokLength (2); RETURN_TOK (INC_OP); }
419 "--" { setTokLength (2); RETURN_TOK (DEC_OP); }
420 "->" { setTokLength (2); RETURN_TOK (ARROW_OP); }
421 "&&" { setTokLength (2); RETURN_TOK (AND_OP); }
422 "||" { setTokLength (2); RETURN_TOK (OR_OP); }
423 "<=" { setTokLength (2); RETURN_TOK (LE_OP); }
424 ">=" { setTokLength (2); RETURN_TOK (GE_OP); }
425 "==" { setTokLength (2); RETURN_TOK (EQ_OP); }
426 "!=" { setTokLength (2); RETURN_TOK (NE_OP); }
427 ";" { setTokLength (1); RETURN_TOK (TSEMI); }
428 "{" { setTokLength (1); RETURN_TOK (TLBRACE); }
429 "}" { setTokLength (1); RETURN_TOK (TRBRACE); }
430 "," { setTokLength (1); RETURN_TOK (TCOMMA); }
431 ":" { setTokLength (1); RETURN_TOK (TCOLON); }
432 "=" { setTokLength (1); RETURN_TOK (TASSIGN); }
433 "(" { setTokLength (1); RETURN_TOK (TLPAREN); }
434 ")" { setTokLength (1); RETURN_TOK (TRPAREN); }
435 "[" { setTokLength (1); RETURN_TOK (TLSQBR); }
436 "]" { setTokLength (1); RETURN_TOK (TRSQBR); }
437 "." { setTokLength (1); RETURN_TOK (TDOT); }
438 "&" { setTokLength (1); RETURN_TOK (TAMPERSAND); }
439 "!" { setTokLength (1); RETURN_TOK (TEXCL); }
442 "~" { setTokLength (1); RETURN_TOK (TTILDE); }
443 "-" { setTokLength (1); RETURN_TOK (TMINUS); }
444 "+" { setTokLength (1); RETURN_TOK (TPLUS); }
445 "*" { setTokLength (1); RETURN_TOK (TMULT); }
446 "/" { setTokLength (1); RETURN_TOK (TDIV); }
447 "%" { setTokLength (1); RETURN_TOK (TPERCENT); }
448 "<" { setTokLength (1); RETURN_TOK (TLT); }
449 ">" { setTokLength (1); RETURN_TOK (TGT); }
450 "^" { setTokLength (1); RETURN_TOK (TCIRC); }
451 "|" { setTokLength (1); RETURN_TOK (TBAR); }
452 "?" { setTokLength (1); RETURN_TOK (TQUEST); }
455 "/\\" { setTokLength (1); RETURN_TOK (TCAND); }
458 [ \t\v\f] { incColumn (); }
459 \n { context_incLineno ();
460 if (tokLength != 0) {
462 /* No error to report
465 message ("Likely parse error: token spans multiple lines."),
472 continueLine = FALSE;
476 if (context_inMacro ())
478 /* Don't use RETURN_TOK */
479 yylval.tok = lltok_create (TENDMACRO, fileloc_copy (g_currentloc)); /* !!! evans 2002-03-13 */
480 lastWasString = FALSE;
485 "@@MR@@" { setTokLength (6);
487 if (processMacro ()) {
488 if (context_inIterDef ())
490 RETURN_TOK (LLMACROITER);
492 if (context_inIterEnd ())
494 RETURN_TOK (LLMACROEND);
496 if (context_inMacro ())
498 RETURN_TOK (LLMACRO);
502 "@QLMR" { if (context_inHeader () || context_inFunction ())
508 int nspchar = ninput ();
512 ** This is a hack to get the column number correct.
515 llassert (nspchar >= '0' && nspchar <= '9');
517 nspaces = nspchar - '0';
519 setTokLength (5 + nspaces);
523 DPRINTF (("Here we are: %s", context_unparse ()));
525 if (context_inIterDef ())
527 RETURN_TOK (LLMACROITER);
529 if (context_inIterEnd ())
531 RETURN_TOK (LLMACROEND);
533 if (context_inMacro ())
535 RETURN_TOK (LLMACRO);
540 "@.CT" { setTokLength (4); lldiagmsg (ctype_unparseTable ()); }
541 "@.FA" { setTokLength (4); lldiagmsg (message ("Access types: %q", typeIdSet_unparse (context_fileAccessTypes ()))); }
542 "@.F" { setTokLength (3);
543 lldiagmsg (message ("%q: *** marker ***", fileloc_unparse (g_currentloc)));
545 "@.L" { setTokLength (3); usymtab_printLocal (); }
546 "@.A" { setTokLength (3); lldiagmsg (usymtab_unparseAliases ()); }
547 "@.C" { setTokLength (3); lldiagmsg (context_unparse ()); }
548 "@.W" { setTokLength (3); lldiagmsg (context_unparseClauses ()); }
549 "@.G" { setTokLength (3); usymtab_printGuards (); }
550 "@.S" { setTokLength (3); usymtab_printOut (); }
551 "@.X" { setTokLength (3); usymtab_printAll (); }
552 "@.Z" { setTokLength (3); usymtab_printComplete (); }
553 "@.T" { setTokLength (3); usymtab_printTypes (); }
554 "@.K" { setTokLength (3); lldiagmsg (usymtab_unparseStack ()); }
555 "@.M" { setTokLength (3);
556 lldiagmsg (message ("Can modify: %q",
557 sRefSet_unparse (context_modList ())));
559 "%{" { /* BEFORE_COMMENT_MARKER */
561 incColumn (); incColumn ();
562 tok = handleLlSpecial ();
566 if (tok == CANNOTATION) {
569 /* Beware - this bashes yylval! */
574 "%}" { /* AFTER_COMMENT_MARKER */
576 s_inSpecPart = FALSE;
577 s_whichSpecPart = BADTOK;
578 RETURN_TOK (QENDMACRO); }
579 "\\" { incColumn (); continueLine = TRUE; }
581 if ((int) *yytext == 13 ) {
586 message ("Invalid character (ascii: %d), skipping character",
594 ** Resets flags set by flex.head.
617 /*@=evalorderuncon@*/
620 /*@=loopswitchbreak@*/
621 /*@=switchswitchbreak@*/
629 /*@=elseifcomplete@*/
633 /*@=immediatetrans@*/
635 /*@=matchanyintegral@*/
647 /*@null@*/ /*@observer@*/ char *name;
652 ** These tokens are followed by syntax that is parsed by the
656 static struct skeyword s_parsetable[] = {
657 { "modifies", QMODIFIES } ,
658 { "globals", QGLOBALS } ,
661 { "constant", QCONSTANT } ,
662 { "function", QFUNCTION } ,
664 { "defines", QDEFINES } ,
666 { "allocates", QALLOCATES } ,
668 { "releases", QRELEASES } ,
669 { "pre", QPRECLAUSE } ,
670 { "post", QPOSTCLAUSE } ,
671 { "setBufferSize", QSETBUFFERSIZE},
672 { "setStringLength", QSETSTRINGLENGTH},
673 { "testinRange", QTESTINRANGE},
674 { "requires", QPRECLAUSE } ,
675 { "ensures", QPOSTCLAUSE } ,
676 { "invariant", QINVARIANT} ,
681 ** These tokens are either stand-alone tokens, or followed by
682 ** token-specific text.
685 static struct skeyword s_keytable[] = {
686 { "anytype", QANYTYPE } ,
687 { "integraltype", QINTEGRALTYPE } ,
688 { "unsignedintegraltype", QUNSIGNEDINTEGRALTYPE } ,
689 { "signedintegraltype", QSIGNEDINTEGRALTYPE } ,
693 { "owned", QOWNED } ,
694 { "dependent", QDEPENDENT } ,
695 { "partial", QPARTIAL } ,
696 { "special", QSPECIAL } ,
697 { "truenull", QTRUENULL } ,
698 { "falsenull", QFALSENULL } ,
699 { "nullwhentrue", QTRUENULL } ,
700 { "falsewhennull", QFALSENULL } ,
703 { "notnull", QNOTNULL } ,
704 { "abstract", QABSTRACT } ,
705 { "concrete", QCONCRETE } ,
706 { "mutable", QMUTABLE } ,
707 { "immutable", QIMMUTABLE } ,
708 { "unused", QUNUSED } ,
709 { "external", QEXTERNAL } ,
711 { "unique", QUNIQUE } ,
712 { "returned", QRETURNED } ,
713 { "exposed", QEXPOSED } ,
714 { "refcounted", QREFCOUNTED } ,
716 { "newref", QNEWREF } ,
717 { "tempref", QTEMPREF } ,
718 { "killref", QKILLREF } ,
720 { "relnull", QRELNULL } ,
721 { "nullterminated", QNULLTERMINATED },
722 { "setBufferSize", QSETBUFFERSIZE },
723 { "testInRange", QTESTINRANGE},
724 { "isnull", QISNULL },
725 { "MaxSet", QMAXSET},
726 { "MaxRead", QMAXREAD},
727 { "maxSet", QMAXSET},
728 { "maxRead", QMAXREAD},
729 { "reldef", QRELDEF } ,
730 { "observer", QOBSERVER } ,
731 { "exits", QEXITS } ,
732 { "noreturn", QEXITS } ,
733 { "mayexit", QMAYEXIT } ,
734 { "maynotreturn", QMAYEXIT } ,
735 { "trueexit", QTRUEEXIT } ,
736 { "falseexit", QFALSEEXIT } ,
737 { "noreturnwhentrue", QTRUEEXIT } ,
738 { "noreturnwhenfalse", QFALSEEXIT } ,
739 { "neverexit", QNEVEREXIT } ,
740 { "alwaysreturns", QNEVEREXIT } ,
742 { "shared", QSHARED } ,
744 { "unchecked", QUNCHECKED } ,
745 { "checked", QCHECKED } ,
746 { "checkmod", QCHECKMOD } ,
747 { "checkedstrict", QCHECKEDSTRICT } ,
748 { "innercontinue", QINNERCONTINUE } ,
749 { "innerbreak", QINNERBREAK } ,
750 { "loopbreak", QLOOPBREAK } ,
751 { "switchbreak", QSWITCHBREAK } ,
752 { "safebreak", QSAFEBREAK } ,
753 { "fallthrough", QFALLTHROUGH } ,
754 { "l_fallthrou", QLINTFALLTHROUGH } ,
755 { "l_fallth", QLINTFALLTHRU } ,
756 { "notreached", QNOTREACHED } ,
757 { "l_notreach", QLINTNOTREACHED } ,
758 { "printflike", QPRINTFLIKE } ,
759 { "l_printfli", QLINTPRINTFLIKE } ,
760 { "scanflike", QSCANFLIKE } ,
761 { "messagelike", QMESSAGELIKE } ,
762 { "l_argsus", QARGSUSED } ,
767 ** would be better if these weren't hard coded...
770 static bool isArtificial (cstring s)
772 return (cstring_equalLit (s, "modifies")
773 || cstring_equalLit (s, "globals")
774 || cstring_equalLit (s, "warn")
775 || cstring_equalLit (s, "alt"));
778 void cscanner_swallowMacro (void)
781 bool skipnext = FALSE;
783 while ((i = lminput ()) != EOF)
799 reader_checkUngetc (i, yyin);
811 reader_checkUngetc (i, yyin);
815 static int commentMarkerToken (cstring s)
819 while (s_parsetable[i].name != NULL)
821 DPRINTF (("Try :%s:%s:", s, s_parsetable[i].name));
823 if (cstring_equalLit (s, s_parsetable[i].name))
825 return s_parsetable[i].token;
834 static int tokenMacroCode (cstring s)
838 while (s_keytable[i].name != NULL)
840 if (cstring_equalLit (s, s_keytable[i].name))
842 if (s_keytable[i].token == QLINTFALLTHROUGH)
845 (FLG_WARNLINTCOMMENTS,
847 ("Traditional lint comment /*FALLTHROUGH*/ used. "
848 "Splint interprets this in the same way as most Unix lints, but it is "
849 "preferable to replace it with the /*@fallthrough@*/ "
854 else if (s_keytable[i].token == QLINTFALLTHRU)
857 (FLG_WARNLINTCOMMENTS,
859 ("Traditional lint comment /*FALLTHRU*/ used. "
860 "Splint interprets this in the same way as most Unix lints, but it is "
861 "preferable to replace it with the /*@fallthrough@*/ "
866 else if (s_keytable[i].token == QLINTNOTREACHED)
869 (FLG_WARNLINTCOMMENTS,
871 ("Traditional lint comment /*NOTREACHED*/ used. "
872 "Splint interprets this in the same way as most Unix lints, but it is "
873 "preferable to replace it with the /*@notreached@*/ "
874 "semantic comment."),
879 else if (s_keytable[i].token == QPRINTFLIKE)
881 setSpecialFunction (qual_createPrintfLike ());
884 else if (s_keytable[i].token == QLINTPRINTFLIKE)
887 (FLG_WARNLINTCOMMENTS,
889 ("Traditional lint comment /*PRINTFLIKE*/ used. "
890 "Splint interprets this in the same way as most Unix lints, but it is "
891 "preferable to replace it with either /*@printflike@*/, "
892 "/*@scanflike@*/ or /*@messagelike@*/."),
895 setSpecialFunction (qual_createPrintfLike ());
898 else if (s_keytable[i].token == QSCANFLIKE)
900 setSpecialFunction (qual_createScanfLike ());
903 else if (s_keytable[i].token == QMESSAGELIKE)
905 setSpecialFunction (qual_createMessageLike ());
908 else if (s_keytable[i].token == QARGSUSED)
911 (FLG_WARNLINTCOMMENTS,
913 ("Traditional lint comment /*ARGSUSED*/ used. "
914 "Splint interprets this in the same way as most Unix lints, but it is "
915 "preferable to use /*@unused@*/ annotations on "
916 "the unused parameters."),
924 return s_keytable[i].token;
934 static int lminput ()
936 if (savechar == '\0')
943 int save = (int) savechar;
949 static void lmsavechar (char c)
951 if (savechar == '\0') savechar = c;
954 llbuglit ("lmsavechar: override");
958 static int returnFloat (ctype ct, double f)
960 yylval.expr = exprNode_floatLiteral (f, ct, cstring_fromChars (yytext),
961 fileloc_decColumn (g_currentloc, tokLength));
966 static int returnInt (ctype ct, long i)
970 if (ctype_equal (ct, ctype_int))
974 c = context_typeofZero ();
978 c = context_typeofOne ();
986 yylval.expr = exprNode_numLiteral (c, cstring_fromChars (yytext),
987 fileloc_decColumn (g_currentloc, tokLength), i);
992 static int returnChar (char c)
994 yylval.expr = exprNode_charLiteral (c, cstring_fromChars (yytext),
995 fileloc_decColumn (g_currentloc, tokLength));
1000 static int ninput ()
1004 if (c != EOF && ((char)c == '\n'))
1006 context_incLineno ();
1012 static char macro_nextChar (void)
1014 static bool in_quote = FALSE, in_escape = FALSE, in_char = FALSE;
1019 c = char_fromInt (ic);
1021 if (!in_quote && !in_char && (c == '\\' || c == BEFORE_COMMENT_MARKER[0]))
1025 while ((c = char_fromInt (lminput ())) != '\0' && c != '\n')
1027 ; /* skip to newline */
1030 context_incLineno ();
1034 return macro_nextChar ();
1041 else /* if (c == '@') */
1043 llassert (FALSE); /*@i23@*/
1044 if (handleLlSpecial () != BADTOK)
1046 llerrorlit (FLG_SYNTAX, "Macro cannot use special syntax");
1049 return macro_nextChar ();
1052 else if (!in_escape && c == '\"')
1054 in_quote = !in_quote;
1056 else if (!in_escape && c == '\'')
1060 else if ((in_quote || in_char) && c == '\\')
1062 in_escape = !in_escape;
1064 else if ((in_quote || in_char) && in_escape)
1068 else if (!in_quote && c == '/')
1072 if ((c2 = char_fromInt (lminput ())) == '*')
1076 while ((c2 = char_fromInt (lminput ())) != '\0'
1077 && c2 != '\n' && c2 != '*')
1084 while ((c2 = char_fromInt (lminput ())) != '\0'
1097 llfatalerror (cstring_makeLiteral ("Macro: bad comment!"));
1101 return macro_nextChar ();
1105 /*** putchar does not work! why? puts to stdio...??! ***/
1118 ** keeps semantic comments
1121 static char macro_nextCharC (void)
1123 static bool in_quote = FALSE, in_escape = FALSE, in_char = FALSE;
1126 c = char_fromInt (lminput ());
1128 if (!in_quote && !in_char && c == '\\')
1130 while ((c = char_fromInt (lminput ())) != '\0' && c != '\n')
1132 ; /* skip to newline */
1135 context_incLineno ();
1139 return macro_nextCharC ();
1146 else if (!in_escape && c == '\"')
1148 in_quote = !in_quote;
1150 else if (!in_escape && c == '\'')
1154 else if ((in_quote || in_char) && c == '\\')
1156 in_escape = !in_escape;
1158 else if ((in_quote || in_char) && in_escape)
1162 else if (!in_quote && c == '/')
1166 if ((c2 = char_fromInt (lminput ())) == '*')
1170 while ((c2 = char_fromInt (lminput ())) != '\0'
1171 && c2 != '\n' && c2 != '*')
1178 while ((c2 = char_fromInt (lminput ())) != '\0'
1191 llfatalerror (cstring_makeLiteral ("Macro: bad comment!"));
1195 return macro_nextCharC ();
1202 else /* normal character */
1211 ** skips whitespace (handles line continuations)
1212 ** returns first non-whitespace character
1215 static char skip_whitespace (void)
1219 while ((c = macro_nextChar ()) == ' ' || c == '\t')
1227 static void handleMacro ()
1229 cstring mac = cstring_undefined;
1233 while (currentColumn () > 2)
1235 mac = cstring_appendChar (mac, ' ');
1239 c = macro_nextCharC ();
1241 if (c >= '0' && c <= '9')
1245 for (i = 0; i < (((int) (c - '0')) + 1); i++)
1247 mac = cstring_appendChar (mac, ' ');
1255 while (((c = macro_nextCharC ()) != '\0') && (c != '\n'))
1257 mac = cstring_appendChar (mac, c);
1261 macrocode = tokenMacroCode (mac);
1263 if (macrocode == BADTOK && !isArtificial (mac))
1265 context_addMacroCache (mac);
1274 context_incLineno ();
1278 static bool processMacro (void)
1283 cstring fname = cstring_undefined;
1285 bool isspecfcn = FALSE;
1286 bool isiter = FALSE;
1287 bool skipparam = FALSE;
1288 bool isenditer = FALSE;
1289 bool unknownm = FALSE;
1290 bool hasParams = FALSE;
1291 bool emptyMacro = FALSE;
1292 char c = skip_whitespace ();
1293 fileloc loc = fileloc_noColumn (g_currentloc);
1295 /* are both of these necessary? what do they mean? */
1296 uentryList specparams = uentryList_undefined;
1297 uentryList pn = uentryList_undefined;
1299 context_resetMacroMissingParams ();
1301 if (c == '\0' || c == '\n')
1303 llcontbug (cstring_makeLiteral ("Bad macro"));
1308 fname = cstring_appendChar (fname, c);
1310 while ((c = macro_nextChar ()) != '(' && c != '\0'
1311 && c != ' ' && c != '\t' && c != '\n')
1313 fname = cstring_appendChar (fname, c);
1316 if (c == ' ' || c == '\t' || c == '\n')
1322 while (c == ' ' || c == '\t')
1324 c = macro_nextChar ();
1338 hasParams = (c == '(');
1340 if (usymtab_exists (fname))
1342 e2 = usymtab_lookupExpose (fname);
1343 ct = uentry_getType (e2);
1345 if (uentry_isCodeDefined (e2)
1346 && fileloc_isUser (uentry_whereDefined (e2)))
1350 message ("Macro %s already defined", fname),
1353 uentry_showWhereDefined (e2);
1354 uentry_clearDefined (e2);
1357 if (uentry_isFunction (e2))
1359 uentry_setType (e2, ctype_unknown);
1362 context_enterUnknownMacro (e2);
1366 context_enterConstantMacro (e2);
1371 if (uentry_isForward (e2) && uentry_isFunction (e2))
1378 ("Parameterized macro has no prototype or specification: %s ",
1383 uentry_setType (e2, ctype_unknown);
1384 uentry_setFunctionDefined (e2, loc);
1385 uentry_setUsed (e2, fileloc_undefined);
1386 context_enterUnknownMacro (e2);
1390 if (uentry_isIter (e2))
1393 specparams = uentry_getParams (e2);
1394 noparams = uentryList_size (specparams);
1395 uentry_setDefined (e2, loc);
1396 context_enterIterDef (e2);
1398 else if (uentry_isEndIter (e2))
1401 uentry_setDefined (e2, loc);
1402 context_enterIterEnd (e2); /* don't care about it now */
1403 /* but should parse like an iter! */
1405 else if (uentry_isConstant (e2))
1411 message ("Constant %s implemented as parameterized macro",
1415 uentry_showWhereSpecified (e2);
1416 uentry_setType (e2, ctype_unknown);
1417 uentry_makeConstantFunction (e2);
1418 uentry_setDefined (e2, g_currentloc);
1419 uentry_setFunctionDefined (e2, g_currentloc);
1420 context_enterUnknownMacro (e2);
1424 if (!uentry_isSpecified (e2))
1426 fileloc oloc = uentry_whereDeclared (e2);
1428 if (fileloc_isLib (oloc))
1432 else if (fileloc_isUndefined (oloc)
1433 || fileloc_isPreproc (oloc))
1438 (FLG_MACROCONSTDECL,
1440 ("Macro constant %q not declared",
1441 uentry_getName (e2)),
1445 else if (!fileloc_withinLines (oloc, loc, 2))
1446 { /* bogus! will give errors if there is too much whitespace */
1450 ("Macro constant name %s does not match name in "
1451 "previous constant declaration. This constant "
1452 "is declared at %q", fname,
1453 fileloc_unparse (oloc)),
1462 context_enterConstantMacro (e2);
1463 cstring_free (fname);
1469 else if (ctype_isFunction (ct))
1472 specparams = ctype_argsFunction (ct);
1473 noparams = uentryList_size (specparams);
1475 uentry_setFunctionDefined (e2, loc);
1476 context_enterMacro (e2);
1478 else if (uentry_isVar (e2))
1484 message ("Variable %s implemented as parameterized macro",
1488 uentry_showWhereSpecified (e2);
1489 uentry_setType (e2, ctype_unknown);
1490 uentry_makeVarFunction (e2);
1491 uentry_setDefined (e2, g_currentloc);
1492 uentry_setFunctionDefined (e2, g_currentloc);
1493 context_enterUnknownMacro (e2);
1497 uentry ucons = uentry_makeConstant (fname,
1500 if (uentry_isExpandedMacro (e2))
1508 message ("Variable %s implemented by a macro",
1512 uentry_showWhereSpecified (e2);
1516 uentry_setDefined (e2, loc);
1517 uentry_setUsed (ucons, loc);
1519 context_enterConstantMacro (ucons);
1520 uentry_markOwned (ucons);
1521 cstring_free (fname);
1527 if (uentry_isDatatype (e2))
1531 message ("Type implemented as macro: %x",
1532 uentry_getName (e2)),
1533 message ("A type is implemented using a macro definition. A "
1534 "typedef should be used instead."),
1537 cscanner_swallowMacro ();
1538 /* Must exit scope (not sure why a new scope was entered?) */
1539 usymtab_quietExitScope (g_currentloc);
1540 uentry_setDefined (e2, g_currentloc);
1546 (message ("Unexpanded macro not function or constant: %q",
1547 uentry_unparse (e2)));
1548 uentry_setType (e2, ctype_unknown);
1552 uentry_makeVarFunction (e2);
1553 uentry_setDefined (e2, g_currentloc);
1554 uentry_setFunctionDefined (e2, g_currentloc);
1555 context_enterUnknownMacro (e2);
1566 /* evans 2001-09-09 - if it has params, assume a function */
1570 (FLG_MACROMATCHNAME,
1571 message ("Unexpanded macro %s does not match name of a declared "
1572 "function. The name used in the control "
1573 "comment on the previous line should match.",
1577 ce = uentry_makeFunction (fname, ctype_unknown,
1581 warnClause_undefined,
1583 uentry_setUsed (ce, loc); /* perhaps bogus? */
1584 e2 = usymtab_supEntryReturn (ce);
1585 context_enterUnknownMacro (e2);
1590 (FLG_MACROMATCHNAME,
1591 message ("Unexpanded macro %s does not match name of a constant "
1592 "or iter declaration. The name used in the control "
1593 "comment on the previous line should match. "
1594 "(Assuming macro defines a constant.)",
1598 ce = uentry_makeConstant (fname, ctype_unknown, fileloc_undefined);
1599 uentry_setUsed (ce, loc); /* perhaps bogus? */
1600 e2 = usymtab_supEntryReturn (ce);
1602 context_enterConstantMacro (e2);
1603 cstring_free (fname);
1609 /* in macros, ( must follow immediatetly after name */
1615 c = skip_whitespace ();
1617 while (c != ')' && c != '\0')
1620 bool suppress = context_inSuppressRegion ();
1621 cstring paramname = cstring_undefined;
1624 ** save the parameter location
1628 context_saveLocation ();
1631 while (c != ' ' && c != '\t' && c != ',' && c != '\0' && c != ')')
1633 paramname = cstring_appendChar (paramname, c);
1634 c = macro_nextChar ();
1637 if (c == ' ' || c == '\t') c = skip_whitespace ();
1641 c = macro_nextChar ();
1642 if (c == ' ' || c == '\t') c = skip_whitespace ();
1647 llfatalerror (cstring_makeLiteral
1648 ("Bad macro syntax: uentryList"));
1651 if ((isspecfcn || isiter) && (paramno < noparams)
1652 && !uentry_isElipsisMarker (uentryList_getN
1653 (specparams, paramno)))
1655 fileloc sloc = context_getSaveLocation ();
1656 uentry decl = uentryList_getN (specparams, paramno);
1659 param = uentry_nameCopy (paramname, decl);
1661 uentry_setParam (param);
1662 sr = sRef_makeParam (paramno, uentry_getType (param), stateInfo_makeLoc (sloc));
1664 if (sRef_getNullState (sr) == NS_ABSNULL)
1666 ctype pt = ctype_realType (uentry_getType (param));
1668 if (ctype_isUser (pt))
1670 uentry te = usymtab_getTypeEntrySafe (ctype_typeId (pt));
1672 if (uentry_isValid (te))
1674 sRef_setStateFromUentry (sr, te);
1679 sRef_setNullState (sr, NS_UNKNOWN, sloc);
1683 uentry_setSref (param, sr);
1684 uentry_setDeclaredForceOnly (param, sloc);
1686 skipparam = isiter && uentry_isOut (uentryList_getN (specparams, paramno));
1690 fileloc sloc = context_getSaveLocation ();
1692 param = uentry_makeVariableSrefParam
1693 (paramname, ctype_unknown, fileloc_copy (sloc),
1694 sRef_makeParam (paramno, ctype_unknown, stateInfo_makeLoc (sloc)));
1695 DPRINTF (("Unknown param: %s", uentry_unparseFull (param)));
1696 cstring_free (paramname);
1698 sRef_setPosNull (uentry_getSref (param), sloc);
1699 uentry_setDeclaredForce (param, sloc);
1702 fileloc_free (sloc);
1707 llassert (!uentry_isElipsisMarker (param));
1711 sRef_makeUnsafe (uentry_getSref (param));
1714 pn = uentryList_add (pn, uentry_copy (param));
1715 usymtab_supEntry (param);
1719 /* don't add param */
1720 uentry_free (param);
1725 (void) macro_nextChar ();
1726 c = skip_whitespace ();
1734 if (isspecfcn || isiter)
1736 if (paramno != noparams && noparams >= 0)
1742 message ("Macro %s specified with %d args, defined with %d",
1743 fname, noparams, paramno),
1746 uentry_showWhereSpecified (e2);
1747 uentry_resetParams (e2, pn);
1752 uentry_resetParams (e2, pn);
1759 ** the form should be:
1761 ** # define newname oldname
1762 ** where oldname refers to a function matching the specification
1768 sRef_setGlobalScope ();
1769 usymtab_supGlobalEntry (uentry_makeVariableLoc (fname, ctype_unknown));
1770 sRef_clearGlobalScope ();
1774 context_setMacroMissingParams ();
1779 /* context_setuentryList (pn); */
1780 usymtab_enterScope ();
1783 cstring_free (fname);
1788 static bool handleSpecial (char *yyt)
1790 char *l; /* !! = mstring_create (MAX_NAME_LENGTH); */
1797 len_yyt = strlen (yyt +1) ;
1799 l = mstring_copy (yyt + 1);
1801 while ((c = char_fromInt (lminput ())) != '\n' && c != '\0')
1803 l = mstring_append(l, c);
1806 /* Need to safe original l for deallocating. */
1811 olc = cstring_fromChars (ol);
1813 if (cstring_equalPrefixLit (olc, "pragma"))
1815 char *pname = mstring_create (size_fromInt (MAX_PRAGMA_LEN));
1816 char *opname = pname;
1817 char *ptr = ol + 6; /* pragma is six characters, plus space */
1821 /* skip whitespace */
1822 while (((c = *ptr) != '\0') && isspace (c))
1828 while (((c = *ptr) != '\0') && !isspace (c))
1832 if (len > MAX_PRAGMA_LEN)
1843 if (len == PRAGMA_LEN_EXPAND
1844 && mstring_equal (opname, PRAGMA_EXPAND))
1846 cstring exname = cstring_undefined;
1850 while (((c = *ptr) != '\0') && !isspace (c))
1852 exname = cstring_appendChar (exname, c);
1857 ue = usymtab_lookupExposeGlob (exname);
1859 if (uentry_isExpandedMacro (ue))
1861 if (fileloc_isPreproc (uentry_whereDefined (ue)))
1863 fileloc_setColumn (g_currentloc, 1);
1864 uentry_setDefined (ue, g_currentloc);
1868 cstring_free (exname);
1871 else if (cstring_equalPrefixLit (olc, "ident"))
1873 /* Some pre-processors will leave these in the code. Ignore rest of line */
1876 ** Yuk...Win32 filenames can have spaces in them...we need to read
1877 ** to the matching end quote.
1879 else if ((sscanf (ol, "line %d \"", &lineno) == 1)
1880 || (sscanf (ol, " %d \"", &lineno) == 1))
1886 /*@access cstring@*/
1887 while (*tmp != '\"' && *tmp != '\0')
1892 llassert (*tmp == '\"');
1898 while (*tmp != '\"' && *tmp != '\0')
1903 llassert (*tmp == '\"');
1907 # if defined(OS2) || defined(MSDOS) || defined(WIN32)
1910 ** DOS-like path delimiters get delivered in pairs, something like
1911 ** \"..\\\\file.h\", so we have to make it normal again. We do NOT
1912 ** remove the pre dirs yet as we usually specify tmp paths relative
1913 ** to the current directory, so tmp files would not get found in
1914 ** the hash table. If this method fails we try it again later.
1921 ** Skip past the drive marker.
1924 if (strchr (stmp, ':') != NULL)
1926 stmp = strchr (stmp, ':') + 1;
1929 while ((stmp = strchr (stmp, CONNECTCHAR)) != NULL )
1931 if (*(stmp+1) == CONNECTCHAR)
1933 memmove (stmp, stmp+1, strlen (stmp));
1939 fid = fileTable_lookupBase (context_fileTable (), fname);
1940 if (!(fileId_isValid (fid)))
1942 fname = removePreDirs (fname);
1943 fid = fileTable_lookupBase (context_fileTable (), fname);
1946 # else /* !defined(OS2) && !defined(MSDOS) */
1947 fname = removePreDirs (fname);
1948 fid = fileTable_lookupBase (context_fileTable (), fname);
1949 # endif /* !defined(OS2) && !defined(MSDOS) */
1951 if (!(fileId_isValid (fid)))
1953 if (context_inXHFile ())
1955 fid = fileTable_addXHFile (context_fileTable (), fname);
1957 else if (isHeaderFile (fname))
1959 fid = fileTable_addHeaderFile (context_fileTable (), fname);
1963 fid = fileTable_addFile (context_fileTable (), fname);
1967 setFileLine (fid, lineno);
1968 /*@noaccess cstring@*/
1970 else if ((sscanf (ol, "line %d", &lineno) == 1)
1971 || (sscanf (ol, " %d", &lineno) == 1))
1973 setLine (lineno); /* next line is <cr> */
1977 if (mstring_equal (ol, "")) {
1978 DPRINTF (("Empty pp command!"));
1980 ** evs 2000-05-16: This is a horrible kludge, to get around a bug (well, difficulty) in the pre-processor.
1981 ** We handle a plain # in the input file, by echoing it, and ignoring it in the post-pp-file.
1987 (FLG_UNRECOGDIRECTIVE,
1988 message ("Unrecognized pre-processor directive: #%s",
1989 cstring_fromChars (ol)),
1994 return FALSE; /* evans 2001-12-30: was: TRUE; */
2001 static int handleLlSpecial (void)
2006 char *s = mstring_createEmpty ();
2012 loc = fileloc_copy (g_currentloc);
2013 DPRINTF (("Handle special: %s", fileloc_unparse (loc)));
2015 while (((ic = ninput ()) != 0) && isalpha (ic))
2018 s = mstring_append (s, c);
2022 DPRINTF (("Read: %s / %s", s, fileloc_unparse (g_currentloc)));
2025 if (charsread == 0 && ic == (int) AFTER_COMMENT_MARKER[0])
2029 llassert (ic == (int) AFTER_COMMENT_MARKER[1]);
2035 return QNOMODS; /* special token no modifications token */
2039 DPRINTF (("Coment marker: %s", os));
2040 tok = commentMarkerToken (cstring_fromChars (os));
2044 tokLength = charsread;
2046 s_inSpecPart = TRUE;
2047 s_whichSpecPart = tok;
2052 DPRINTF (("Not a comment marker..."));
2053 /* Add rest of the comment */
2055 if (ic != 0 && ic != EOF)
2059 s = mstring_append (s, c);
2062 while (((ic = ninput ()) != 0) && (ic != EOF)
2063 && (ic != (int) AFTER_COMMENT_MARKER[0]))
2067 /* evans 2001-09-01 added to prevent assertion failures for uncloses syntactic comments */
2070 hasnl = TRUE; /* This prevents tokLength from being set later. */
2075 message ("Likely parse error: syntactic comment token spans multiple lines: %s",
2076 cstring_fromChars (s)),
2080 s = mstring_append (s, c);
2085 DPRINTF (("Read: %s / %s", s, fileloc_unparse (g_currentloc)));
2087 if (ic == (int) AFTER_COMMENT_MARKER[0])
2090 llassert ((char) nc == AFTER_COMMENT_MARKER[1]);
2096 while (*s == ' ' || *s == '\t' || *s == '\n')
2101 if (*s == '-' || *s == '+' || *s == '=') /* setting flags */
2105 while (c == '-' || c == '+' || c == '=')
2107 ynm set = ynm_fromCodeChar (c);
2112 thisflag = cstring_fromChars (s);
2114 while ((c = *s) != '\0' && (c != '-') && (c != '=')
2115 && (c != '+') && (c != ' ') && (c != '\t') && (c != '\n'))
2122 if (!context_getFlag (FLG_NOCOMMENTS))
2124 cstring flagname = thisflag;
2125 flagcode fflag = flags_identifyFlag (flagname);
2127 if (flagcode_isSkip (fflag))
2131 else if (flagcode_isModeName (fflag))
2133 if (ynm_isMaybe (set))
2138 ("Semantic comment attempts to restore flag %s. "
2139 "A mode flag cannot be restored.",
2144 context_setMode (flagname);
2147 else if (flagcode_isInvalid (fflag))
2150 (FLG_UNRECOGFLAGCOMMENTS,
2151 message ("Unrecognized option in semantic comment: %s",
2155 else if (flagcode_isGlobalFlag (fflag))
2160 ("Semantic comment attempts to set global flag %s. "
2161 "A global flag cannot be set locally.",
2167 context_fileSetFlag (fflag, set, loc);
2169 if (flagcode_hasArgument (fflag))
2171 if (ynm_isMaybe (set))
2176 ("Semantic comment attempts to restore flag %s. "
2177 "A flag for setting a value cannot be restored.",
2182 { /* cut-and-pastied from llmain...blecch */
2183 cstring extra = cstring_undefined;
2189 rest = mstring_copy (s);
2193 while ((rchar = *rest) != '\0'
2194 && (isspace (rchar)))
2200 while ((rchar = *rest) != '\0'
2201 && !isspace (rchar))
2203 extra = cstring_appendChar (extra, rchar);
2207 s--; /* evans 2002-07-12: this was previously only in the else branch.
2208 Leads to an invalid read on the true branch.
2213 if (cstring_isUndefined (extra))
2218 ("Flag %s (in semantic comment) must be followed by an argument",
2219 flagcode_unparse (fflag)));
2224 if (flagcode_hasNumber (fflag))
2226 setValueFlag (fflag, extra);
2228 else if (flagcode_hasChar (fflag))
2230 setValueFlag (fflag, extra);
2232 else if (flagcode_hasString (fflag))
2234 setStringFlag (fflag, extra);
2238 cstring_free (extra);
2252 while ((c == ' ') || (c == '\t') || (c == '\n'))
2258 if (context_inHeader () && !isArtificial (cstring_fromChars (os)))
2260 DPRINTF (("Here adding comment: %s", os));
2261 context_addComment (cstring_fromCharsNew (os), loc);
2273 annotationInfo ainfo;
2275 while (*s != '\0' && *s != ' ' && *s != '\t' && *s != '\n')
2287 t = cstring_toCharsSafe (cstring_downcase (cstring_fromChars (t)));
2288 macrocode = tokenMacroCode (cstring_fromChars (t));
2290 if (macrocode != BADTOK)
2292 tokLength = hasnl ? 0 : size_toInt (mstring_length (t));
2298 if (macrocode == SKIPTOK)
2306 ainfo = context_lookupAnnotation (cstring_fromChars (os));
2308 if (annotationInfo_isDefined (ainfo)) {
2309 DPRINTF (("Found annotation: %s", annotationInfo_unparse (ainfo)));
2310 /*@i324@*/ yylval.annotation = ainfo;
2318 if (context_inHeader ())
2325 if ((context_inMacro () || context_inGlobalContext ())
2326 && macrocode != SKIPTOK
2327 && !isArtificial (cstring_fromChars (os)))
2329 if (context_processingMacros ())
2331 /* evans 2002-02-24: don't add comments when procssing macros */
2335 context_addComment (cstring_fromCharsNew (os), loc);
2349 if (mstring_equal (t, "ignore"))
2351 if (!context_getFlag (FLG_NOCOMMENTS))
2353 context_enterSuppressRegion (loc);
2356 else if ((*t == 'i' || *t == 't')
2357 && (*(t + 1) == '\0'))
2359 if (!context_getFlag (FLG_NOCOMMENTS)
2360 && (*t == 'i' || context_getFlag (FLG_TMPCOMMENTS)))
2362 context_enterSuppressLine (-1, loc); /* infinite suppression */
2365 else if (((*t == 'i') || (*t == 't'))
2366 && ((*(t + 1) >= '0' && *(t + 1) <= '9')))
2368 bool tmpcomment = (*t == 't');
2370 char *tt = t; /* don't mangle t, since it is free'd */
2373 if (lc >= '0' && lc <= '9')
2375 val = (int)(lc - '0');
2378 while (lc >= '0' && lc <= '9')
2381 val += (int) (lc - '0');
2386 if (!context_getFlag (FLG_NOCOMMENTS)
2387 && (!tmpcomment || context_getFlag (FLG_TMPCOMMENTS)))
2389 DPRINTF (("Here: enter suppress: %s", fileloc_unparse (loc)));
2390 context_enterSuppressLine (val, loc);
2393 else if (mstring_equal (t, "end"))
2395 if (!context_getFlag (FLG_NOCOMMENTS))
2397 context_exitSuppressRegion (loc);
2400 else if (mstring_equal (t, "notfunction"))
2402 ; /* handled by pcpp */
2404 else if (mstring_equal (t, "access"))
2410 while (((c = *s) != '\0') && (c == ' ' || c == '\t' || c == '\n'))
2420 tname = cstring_fromChars (s);
2422 while ((c = *s) != '\0' && c != ' '
2423 && c != '\t' && c != '\n' && c != ',')
2430 DPRINTF (("Access %s", tname));
2432 if (!context_getFlag (FLG_NOCOMMENTS)
2433 && !context_getFlag (FLG_NOACCESS))
2435 if (usymtab_existsType (tname))
2437 typeId uid = usymtab_getTypeId (tname);
2438 uentry ue = usymtab_getTypeEntry (uid);
2440 if (uentry_isAbstractDatatype (ue))
2442 context_addFileAccessType (uid);
2443 DPRINTF (("Adding access to: %s / %d", tname, uid));
2450 ("Non-abstract type %s used in access comment",
2457 if (!(context_inSuppressRegion ()
2458 || context_inSuppressZone (loc)))
2463 ("Unrecognized type %s used in access comment",
2475 if (c != ',' && c != ' ')
2481 else if (mstring_equal (t, "noaccess"))
2488 while (((lc = *s) != '\0') && (lc == ' ' || lc == '\t' || lc == '\n'))
2498 tname = cstring_fromChars (s);
2500 while ((lc = *s) != '\0' && lc != ' ' && lc != '\t'
2501 && lc != '\n' && lc != ',')
2508 if (!context_getFlag (FLG_NOCOMMENTS)
2509 && !context_getFlag (FLG_NOACCESS))
2511 if (usymtab_existsType (tname))
2513 typeId tuid = usymtab_getTypeId (tname);
2515 if (context_couldHaveAccess (tuid))
2517 DPRINTF (("Removing access: %s", tname));
2518 context_removeFileAccessType (tuid);
2522 if (!(context_inSuppressRegion ()
2523 || context_inSuppressZone (loc)))
2525 uentry ue = usymtab_getTypeEntry (tuid);
2527 if (uentry_isAbstractDatatype (ue))
2532 ("Non-accessible abstract type %s used in noaccess comment",
2541 ("Non-abstract type %s used in noaccess comment",
2550 if (!(context_inSuppressRegion ()
2551 || context_inSuppressZone (loc)))
2556 ("Unrecognized type %s used in noaccess comment",
2568 if (lc != ',' && lc != ' ')
2576 voptgenerror (FLG_UNRECOGCOMMENTS,
2577 message ("Semantic comment unrecognized: %s",
2578 cstring_fromChars (os)),
2590 static /*@only@*/ cstring makeIdentifier (char *s)
2592 char *c = mstring_create (strlen (s) + 1);
2593 cstring id = cstring_fromChars (c);
2595 while (isalnum (*s) || (*s == '_') || (*s == '$'))
2604 /*@observer@*/ /*@dependent@*/ uentry coerceId (cstring cn)
2606 if (!(usymtab_exists (cn)))
2608 fileloc loc = fileloc_createExternal ();
2611 ** We need to put this in a global scope, otherwise the sRef will be deallocated.
2614 uentry ce = uentry_makeUnrecognized (cn, loc);
2616 if (!context_inIterEnd ())
2620 message ("Unrecognized (possibly system) identifier: %q",
2621 uentry_getName (ce)),
2628 return (usymtab_lookup (cn));
2632 ** like, coerceId, but doesn't supercede for iters
2635 /*@observer@*/ uentry coerceIterId (cstring cn)
2637 if (!(usymtab_exists (cn)))
2639 return uentry_undefined;
2642 return (usymtab_lookup (cn));
2646 ** Need to keep this in case there is a declaration that isn't processed until
2647 ** the scope exits. Would be good to rearrange the symbol table so this doesn't
2648 ** happen, and save all the cstring copying.
2651 /*@observer@*/ cstring cscanner_observeLastIdentifier ()
2653 cstring res = s_lastidprocessed;
2657 static void cscanner_setLastIdentifier (/*@keep@*/ cstring id) /*@modifies s_lastidprocessed@*/
2659 if (cstring_isDefined (s_lastidprocessed))
2661 cstring_free (s_lastidprocessed);
2664 s_lastidprocessed = id;
2667 static int processIdentifier (cstring id)
2671 if (context_getFlag (FLG_GRAMMAR))
2673 lldiagmsg (message ("Process identifier: %s", id));
2676 context_clearJustPopped ();
2677 cscanner_setLastIdentifier (id);
2679 DPRINTF (("Context: %s", context_unparse ()));
2681 if (context_inFunctionHeader ())
2683 int tok = commentMarkerToken (id);
2684 DPRINTF (("in function decl: %s", id));
2692 tok = tokenMacroCode (id);
2700 annotationInfo ainfo;
2702 if (expectingMetaStateName)
2704 metaStateInfo msinfo = context_lookupMetaStateInfo (id);
2706 if (metaStateInfo_isDefined (msinfo))
2708 yylval.msinfo = msinfo;
2709 return METASTATE_NAME;
2713 DPRINTF (("Not meta state name: %s", cstring_toCharsSafe (id)));
2717 ainfo = context_lookupAnnotation (id);
2719 if (annotationInfo_isDefined (ainfo))
2721 DPRINTF (("Found annotation: %s", annotationInfo_unparse (ainfo)));
2722 /*@i324@*/ yylval.annotation = ainfo;
2727 DPRINTF (("Not annotation: %s", id));
2733 DPRINTF (("Here!"));
2735 /* Consider handling: Defined by C99 as static const char __func__[] */
2737 if (context_getFlag (FLG_GNUEXTENSIONS))
2741 if (cstring_equalLit (id, "__stdcall")
2742 || cstring_equalLit (id, "__cdecl")
2743 || cstring_equalLit (id, "__extension__"))
2747 else if (cstring_equalLit (id, "__volatile__"))
2751 else if (cstring_equalLit (id, "__signed"))
2755 else if (cstring_equalLit (id, "__unsigned"))
2759 else if (cstring_equalLit (id, "__const__"))
2763 else if (cstring_equalLit (id, "__alignof__"))
2765 tok = CALIGNOF; /* alignof is parsed like sizeof */
2767 else if (cstring_equalLit (id, "__typeof__"))
2771 else if (cstring_equalLit (id, "typeof"))
2775 else if (cstring_equalLit (id, "__FUNCTION__")
2776 || cstring_equalLit (id, "__PRETTY_FUNCTION__"))
2778 /* These tokens hold the name of the current function as strings */
2779 /* evans 2001-12-30: changed from exprNode_stringLiteral; bug reported by Jim Zelenka. */
2780 yylval.expr = exprNode_makeConstantString (id, fileloc_copy (g_currentloc));
2782 lastWasString = TRUE;
2786 else if (cstring_equalLit (id, "__attribute__")
2787 || cstring_equalLit (id, "__asm__")
2788 || cstring_equalLit (id, "_asm")
2789 || cstring_equalLit (id, "__asm")
2790 || cstring_equalLit (id, "__declspec"))
2793 bool useparens = FALSE;
2794 bool usebraces = FALSE;
2795 bool inquote = FALSE;
2796 bool inescape = FALSE;
2799 while ((ic = input ()) != EOF)
2801 char cc = (char) ic;
2807 else if (cc == '\\')
2811 else if (cc == '\"')
2847 else if (cc == ')' && useparens)
2850 if (depth == 0) break;
2852 else if (cc == '}' && usebraces)
2855 if (depth == 0) break;
2858 && !usebraces && !useparens
2859 && cstring_equalLit (id, "__asm"))
2862 ** We need this because some MS VC++ include files
2863 ** have __asm mov ... }
2864 ** Its a kludge, but otherwise would need to parse
2881 context_incLineno ();
2883 if (cstring_equalLit (id, "__asm")
2884 && !useparens && !usebraces)
2891 llassert ((useparens && ic == (int) ')')
2892 || (usebraces && ic == (int) '}')
2893 || (!useparens && !usebraces));
2897 else if (cstring_equalLit (id, "inline")
2898 || cstring_equalLit (id, "__inline")
2899 || cstring_equalLit (id, "_inline")
2900 || cstring_equalLit (id, "__inline__"))
2915 le = usymtab_lookupSafe (id);
2917 /*@-dependenttrans@*/
2919 if (uentry_isIter (le))
2921 /*@i32@*/ yylval.entry = le;
2924 else if (uentry_isEndIter (le))
2926 /*@i32@*/ yylval.entry = le;
2927 return (ITER_ENDNAME);
2929 else if (uentry_isUndefined (le))
2931 yylval.cname = cstring_copy (id);
2933 /* avoid parse errors for certain system built ins */
2935 if (g_expectingTypeName && (cstring_firstChar (id) == '_')
2936 && (cstring_secondChar (id) == '_'))
2938 return (TYPE_NAME_OR_ID);
2941 return (NEW_IDENTIFIER);
2943 else if (!uentry_isDeclared (le) && !uentry_isCodeDefined (le))
2945 if (uentry_isDatatype (le))
2947 yylval.cname = cstring_copy (id);
2948 return (NEW_IDENTIFIER);
2952 /*@i32@*/ yylval.entry = le;
2953 return (IDENTIFIER);
2956 else if (uentry_isDatatype (le))
2958 if (!g_expectingTypeName)
2960 yylval.cname = cstring_copy (id);
2962 return (NEW_IDENTIFIER);
2966 yylval.ctyp = uentry_getAbstractType (le);
2968 uentry_setUsed (le, g_currentloc);
2974 /*@i32@*/ yylval.entry = le;
2975 return (IDENTIFIER);
2978 /*@=dependenttrans@*/
2981 static bool processHashIdentifier (/*@only@*/ cstring id)
2983 if (context_inMacro () || context_inIterDef () ||
2984 context_inIterEnd ())
2988 context_clearJustPopped ();
2990 le = usymtab_lookupSafe (id);
2991 cscanner_setLastIdentifier (id);
2993 if (uentry_isParam (le) || uentry_isRefParam (le))
3005 ** Will be handled by handleLlSpecial
3014 static /*@only@*/ exprNode processString (void)
3018 char *nl = strchr (yytext, '\n');
3019 cstring ns = cstring_fromCharsNew (yytext);
3023 loc = fileloc_copy (g_currentloc);
3024 addColumn (size_toInt (cstring_length (ns)));
3030 loc = fileloc_copy (g_currentloc);
3032 context_incLineno ();
3034 while ((nl = strchr ((nl + 1), '\n')) != NULL)
3036 context_incLineno ();
3042 res = exprNode_stringLiteral (ns, loc);
3047 ** process a wide character string L"...."
3050 static /*@only@*/ exprNode processWideString ()
3054 char *nl = strchr (yytext, '\n');
3057 llassert (*yytext == 'L');
3060 ns = cstring_fromCharsNew (yytext);
3064 loc = fileloc_copy (g_currentloc);
3065 addColumn (size_toInt (cstring_length (ns)));
3071 loc = fileloc_copy (g_currentloc);
3073 context_incLineno ();
3075 while ((nl = strchr ((nl + 1), '\n')) != NULL)
3077 context_incLineno ();
3082 res = exprNode_wideStringLiteral (ns, loc);
3092 llassert (*yytext != '\0');
3093 fchar = *(yytext + 1);
3094 if (fchar != '\\') return fchar;
3096 next = *(yytext + 2);
3100 case 'n': return '\n';
3101 case 't': return '\t';
3102 case '\"': return '\"';
3103 case '\'': return '\'';
3104 case '\\': return '\\';
3105 default: return '\0';
3110 double processFloat ()
3112 double ret = atof (yytext);
3123 llassert (yytext[0] == '0'
3124 && (yytext[1] == 'X' || yytext[1] == 'x'));
3126 while (yytext[index] != '\0') {
3128 char c = yytext[index];
3130 if (c >= '0' && c <= '9') {
3131 tval = (int) c - (int) '0';
3132 } else if (c >= 'A' && c <= 'F') {
3133 tval = (int) c - (int) 'A' + 10;
3134 } else if (c >= 'a' && c <= 'f') {
3135 tval = (int) c - (int) 'a' + 10;
3136 } else if (c == 'U' || c == 'L' || c == 'u' || c == 'l') {
3138 while (yytext[index] != '\0') {
3139 if (c == 'U' || c == 'L' || c == 'u' || c == 'l') {
3144 message ("Invalid character (%c) following specifier in hex constant: %s",
3145 c, cstring_fromChars (yytext)),
3155 message ("Invalid character (%c) in hex constant: %s",
3156 c, cstring_fromChars (yytext)),
3161 val = (val * 16) + tval;
3165 DPRINTF (("Hex constant: %s = %ld", yytext, val));
3170 long processOctal ()
3175 llassert (yytext[0] == '0' && yytext[1] != 'X' && yytext[1] != 'x');
3177 while (yytext[index] != '\0') {
3179 char c = yytext[index];
3181 if (c >= '0' && c <= '7') {
3182 tval = (int) c - (int) '0';
3183 } else if (c == 'U' || c == 'L' || c == 'u' || c == 'l') {
3185 while (yytext[index] != '\0') {
3186 if (c == 'U' || c == 'L' || c == 'u' || c == 'l') {
3191 message ("Invalid character (%c) following specifier in octal constant: %s",
3192 c, cstring_fromChars (yytext)),
3202 message ("Invalid character (%c) in octal constant: %s",
3203 c, cstring_fromChars (yytext)),
3208 val = (val * 8) + tval;
3212 DPRINTF (("Octal constant: %s = %ld", yytext, val));
3219 return (atol (yytext));
3223 processSpec (int tok)
3225 size_t length = strlen (yytext);
3232 if (s_whichSpecPart == QMODIFIES
3233 || s_whichSpecPart == QDEFINES
3234 || s_whichSpecPart == QUSES
3235 || s_whichSpecPart == QALLOCATES
3236 || s_whichSpecPart == QSETS
3237 || s_whichSpecPart == QRELEASES
3238 || s_whichSpecPart == QPRECLAUSE
3239 || s_whichSpecPart == QPOSTCLAUSE
3240 || s_whichSpecPart == QINVARIANT)
3242 ; /* Allow specificiation keywords to be used as identifiers in these contexts. */
3249 setTokLengthT (length);
3254 context_saveLocation ();
3255 setTokLengthT (length);
3256 return (processIdentifier (makeIdentifier (yytext)));
3259 void cscanner_expectingMetaStateName ()
3261 llassert (!expectingMetaStateName);
3262 llassert (context_inFunctionHeader ());
3263 expectingMetaStateName = TRUE;
3266 void cscanner_clearExpectingMetaStateName ()
3268 llassert (expectingMetaStateName);
3269 expectingMetaStateName = FALSE;