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 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 s_lastidprocessed@*/ ;
111 static bool processHashIdentifier (/*@only@*/ cstring)
112 /*@globals s_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",
592 ** Resets flags set by flex.head.
615 /*@=evalorderuncon@*/
618 /*@=loopswitchbreak@*/
619 /*@=switchswitchbreak@*/
627 /*@=elseifcomplete@*/
631 /*@=immediatetrans@*/
633 /*@=matchanyintegral@*/
645 /*@null@*/ /*@observer@*/ char *name;
650 ** These tokens are followed by syntax that is parsed by the
654 static struct skeyword s_parsetable[] = {
655 { "modifies", QMODIFIES } ,
656 { "globals", QGLOBALS } ,
659 { "constant", QCONSTANT } ,
660 { "function", QFUNCTION } ,
662 { "defines", QDEFINES } ,
664 { "allocates", QALLOCATES } ,
666 { "releases", QRELEASES } ,
667 { "pre", QPRECLAUSE } ,
668 { "post", QPOSTCLAUSE } ,
669 { "setBufferSize", QSETBUFFERSIZE},
670 { "setStringLength", QSETSTRINGLENGTH},
671 { "testinRange", QTESTINRANGE},
672 { "requires", QPRECLAUSE } ,
673 { "ensures", QPOSTCLAUSE } ,
674 { "invariant", QINVARIANT} ,
679 ** These tokens are either stand-alone tokens, or followed by
680 ** token-specific text.
683 static struct skeyword s_keytable[] = {
684 { "anytype", QANYTYPE } ,
685 { "integraltype", QINTEGRALTYPE } ,
686 { "unsignedintegraltype", QUNSIGNEDINTEGRALTYPE } ,
687 { "signedintegraltype", QSIGNEDINTEGRALTYPE } ,
691 { "owned", QOWNED } ,
692 { "dependent", QDEPENDENT } ,
693 { "partial", QPARTIAL } ,
694 { "special", QSPECIAL } ,
695 { "truenull", QTRUENULL } ,
696 { "falsenull", QFALSENULL } ,
697 { "nullwhentrue", QTRUENULL } ,
698 { "falsewhennull", QFALSENULL } ,
701 { "notnull", QNOTNULL } ,
702 { "abstract", QABSTRACT } ,
703 { "concrete", QCONCRETE } ,
704 { "mutable", QMUTABLE } ,
705 { "immutable", QIMMUTABLE } ,
706 { "unused", QUNUSED } ,
707 { "external", QEXTERNAL } ,
709 { "unique", QUNIQUE } ,
710 { "returned", QRETURNED } ,
711 { "exposed", QEXPOSED } ,
712 { "refcounted", QREFCOUNTED } ,
714 { "newref", QNEWREF } ,
715 { "tempref", QTEMPREF } ,
716 { "killref", QKILLREF } ,
718 { "relnull", QRELNULL } ,
719 { "nullterminated", QNULLTERMINATED },
720 { "setBufferSize", QSETBUFFERSIZE },
721 { "testInRange", QTESTINRANGE},
722 { "MaxSet", QMAXSET},
723 { "MaxRead", QMAXREAD},
724 { "reldef", QRELDEF } ,
725 { "observer", QOBSERVER } ,
726 { "exits", QEXITS } ,
727 { "noreturn", QEXITS } ,
728 { "mayexit", QMAYEXIT } ,
729 { "maynotreturn", QMAYEXIT } ,
730 { "trueexit", QTRUEEXIT } ,
731 { "falseexit", QFALSEEXIT } ,
732 { "noreturnwhentrue", QTRUEEXIT } ,
733 { "noreturnwhenfalse", QFALSEEXIT } ,
734 { "neverexit", QNEVEREXIT } ,
735 { "alwaysreturns", QNEVEREXIT } ,
737 { "shared", QSHARED } ,
739 { "unchecked", QUNCHECKED } ,
740 { "checked", QCHECKED } ,
741 { "checkmod", QCHECKMOD } ,
742 { "checkedstrict", QCHECKEDSTRICT } ,
743 { "innercontinue", QINNERCONTINUE } ,
744 { "innerbreak", QINNERBREAK } ,
745 { "loopbreak", QLOOPBREAK } ,
746 { "switchbreak", QSWITCHBREAK } ,
747 { "safebreak", QSAFEBREAK } ,
748 { "fallthrough", QFALLTHROUGH } ,
749 { "l_fallthrou", QLINTFALLTHROUGH } ,
750 { "l_fallth", QLINTFALLTHRU } ,
751 { "notreached", QNOTREACHED } ,
752 { "l_notreach", QLINTNOTREACHED } ,
753 { "printflike", QPRINTFLIKE } ,
754 { "l_printfli", QLINTPRINTFLIKE } ,
755 { "scanflike", QSCANFLIKE } ,
756 { "messagelike", QMESSAGELIKE } ,
757 { "l_argsus", QARGSUSED } ,
762 ** would be better if these weren't hard coded...
765 static bool isArtificial (cstring s)
767 return (cstring_equalLit (s, "modifies")
768 || cstring_equalLit (s, "globals")
769 || cstring_equalLit (s, "warn")
770 || cstring_equalLit (s, "alt"));
773 void cscanner_swallowMacro (void)
776 bool skipnext = FALSE;
778 while ((i = lminput ()) != EOF)
794 reader_checkUngetc (i, yyin);
806 reader_checkUngetc (i, yyin);
810 static int commentMarkerToken (cstring s)
814 while (s_parsetable[i].name != NULL)
816 DPRINTF (("Try :%s:%s:", s, s_parsetable[i].name));
818 if (cstring_equalLit (s, s_parsetable[i].name))
820 return s_parsetable[i].token;
829 static int tokenMacroCode (cstring s)
833 while (s_keytable[i].name != NULL)
835 if (cstring_equalLit (s, s_keytable[i].name))
837 if (s_keytable[i].token == QLINTFALLTHROUGH)
840 (FLG_WARNLINTCOMMENTS,
842 ("Traditional lint comment /*FALLTHROUGH*/ used. "
843 "Splint interprets this in the same way as most Unix lints, but it is "
844 "preferable to replace it with the /*@fallthrough@*/ "
849 else if (s_keytable[i].token == QLINTFALLTHRU)
852 (FLG_WARNLINTCOMMENTS,
854 ("Traditional lint comment /*FALLTHRU*/ used. "
855 "Splint interprets this in the same way as most Unix lints, but it is "
856 "preferable to replace it with the /*@fallthrough@*/ "
861 else if (s_keytable[i].token == QLINTNOTREACHED)
864 (FLG_WARNLINTCOMMENTS,
866 ("Traditional lint comment /*NOTREACHED*/ used. "
867 "Splint interprets this in the same way as most Unix lints, but it is "
868 "preferable to replace it with the /*@notreached@*/ "
869 "semantic comment."),
874 else if (s_keytable[i].token == QPRINTFLIKE)
876 setSpecialFunction (qual_createPrintfLike ());
879 else if (s_keytable[i].token == QLINTPRINTFLIKE)
882 (FLG_WARNLINTCOMMENTS,
884 ("Traditional lint comment /*PRINTFLIKE*/ used. "
885 "Splint interprets this in the same way as most Unix lints, but it is "
886 "preferable to replace it with either /*@printflike@*/, "
887 "/*@scanflike@*/ or /*@messagelike@*/."),
890 setSpecialFunction (qual_createPrintfLike ());
893 else if (s_keytable[i].token == QSCANFLIKE)
895 setSpecialFunction (qual_createScanfLike ());
898 else if (s_keytable[i].token == QMESSAGELIKE)
900 setSpecialFunction (qual_createMessageLike ());
903 else if (s_keytable[i].token == QARGSUSED)
906 (FLG_WARNLINTCOMMENTS,
908 ("Traditional lint comment /*ARGSUSED*/ used. "
909 "Splint interprets this in the same way as most Unix lints, but it is "
910 "preferable to use /*@unused@*/ annotations on "
911 "the unused parameters."),
919 return s_keytable[i].token;
929 static int lminput ()
931 if (savechar == '\0')
938 int save = (int) savechar;
944 static void lmsavechar (char c)
946 if (savechar == '\0') savechar = c;
949 llbuglit ("lmsavechar: override");
953 static int returnFloat (ctype ct, double f)
955 yylval.expr = exprNode_floatLiteral (f, ct, cstring_fromChars (yytext),
956 fileloc_decColumn (g_currentloc, tokLength));
961 static int returnInt (ctype ct, long i)
965 if (ctype_equal (ct, ctype_int))
969 c = context_typeofZero ();
973 c = context_typeofOne ();
981 yylval.expr = exprNode_numLiteral (c, cstring_fromChars (yytext),
982 fileloc_decColumn (g_currentloc, tokLength), i);
987 static int returnChar (char c)
989 yylval.expr = exprNode_charLiteral (c, cstring_fromChars (yytext),
990 fileloc_decColumn (g_currentloc, tokLength));
999 if (c != EOF && ((char)c == '\n'))
1001 context_incLineno ();
1007 static char macro_nextChar (void)
1009 static bool in_quote = FALSE, in_escape = FALSE, in_char = FALSE;
1014 c = char_fromInt (ic);
1016 if (!in_quote && !in_char && (c == '\\' || c == BEFORE_COMMENT_MARKER[0]))
1020 while ((c = char_fromInt (lminput ())) != '\0' && c != '\n')
1022 ; /* skip to newline */
1025 context_incLineno ();
1029 return macro_nextChar ();
1036 else /* if (c == '@') */
1038 llassert (FALSE); /*@i23@*/
1039 if (handleLlSpecial () != BADTOK)
1041 llerrorlit (FLG_SYNTAX, "Macro cannot use special syntax");
1044 return macro_nextChar ();
1047 else if (!in_escape && c == '\"')
1049 in_quote = !in_quote;
1051 else if (!in_escape && c == '\'')
1055 else if ((in_quote || in_char) && c == '\\')
1057 in_escape = !in_escape;
1059 else if ((in_quote || in_char) && in_escape)
1063 else if (!in_quote && c == '/')
1067 if ((c2 = char_fromInt (lminput ())) == '*')
1071 while ((c2 = char_fromInt (lminput ())) != '\0'
1072 && c2 != '\n' && c2 != '*')
1079 while ((c2 = char_fromInt (lminput ())) != '\0'
1092 llfatalerror (cstring_makeLiteral ("Macro: bad comment!"));
1096 return macro_nextChar ();
1100 /*** putchar does not work! why? puts to stdio...??! ***/
1113 ** keeps semantic comments
1116 static char macro_nextCharC (void)
1118 static bool in_quote = FALSE, in_escape = FALSE, in_char = FALSE;
1121 c = char_fromInt (lminput ());
1123 if (!in_quote && !in_char && c == '\\')
1125 while ((c = char_fromInt (lminput ())) != '\0' && c != '\n')
1127 ; /* skip to newline */
1130 context_incLineno ();
1134 return macro_nextCharC ();
1141 else if (!in_escape && c == '\"')
1143 in_quote = !in_quote;
1145 else if (!in_escape && c == '\'')
1149 else if ((in_quote || in_char) && c == '\\')
1151 in_escape = !in_escape;
1153 else if ((in_quote || in_char) && in_escape)
1157 else if (!in_quote && c == '/')
1161 if ((c2 = char_fromInt (lminput ())) == '*')
1165 while ((c2 = char_fromInt (lminput ())) != '\0'
1166 && c2 != '\n' && c2 != '*')
1173 while ((c2 = char_fromInt (lminput ())) != '\0'
1186 llfatalerror (cstring_makeLiteral ("Macro: bad comment!"));
1190 return macro_nextCharC ();
1197 else /* normal character */
1206 ** skips whitespace (handles line continuations)
1207 ** returns first non-whitespace character
1210 static char skip_whitespace (void)
1214 while ((c = macro_nextChar ()) == ' ' || c == '\t')
1222 static void handleMacro ()
1224 cstring mac = cstring_undefined;
1228 while (currentColumn () > 2)
1230 mac = cstring_appendChar (mac, ' ');
1234 c = macro_nextCharC ();
1236 if (c >= '0' && c <= '9')
1240 for (i = 0; i < (((int) (c - '0')) + 1); i++)
1242 mac = cstring_appendChar (mac, ' ');
1250 while (((c = macro_nextCharC ()) != '\0') && (c != '\n'))
1252 mac = cstring_appendChar (mac, c);
1256 macrocode = tokenMacroCode (mac);
1258 if (macrocode == BADTOK && !isArtificial (mac))
1260 context_addMacroCache (mac);
1269 context_incLineno ();
1273 static bool processMacro (void)
1278 cstring fname = cstring_undefined;
1280 bool isspecfcn = FALSE;
1281 bool isiter = FALSE;
1282 bool skipparam = FALSE;
1283 bool isenditer = FALSE;
1284 bool unknownm = FALSE;
1285 bool hasParams = FALSE;
1286 bool emptyMacro = FALSE;
1287 char c = skip_whitespace ();
1288 fileloc loc = fileloc_noColumn (g_currentloc);
1290 /* are both of these necessary? what do they mean? */
1291 uentryList specparams = uentryList_undefined;
1292 uentryList pn = uentryList_undefined;
1294 context_resetMacroMissingParams ();
1296 if (c == '\0' || c == '\n')
1298 llcontbug (cstring_makeLiteral ("Bad macro"));
1303 fname = cstring_appendChar (fname, c);
1305 while ((c = macro_nextChar ()) != '(' && c != '\0'
1306 && c != ' ' && c != '\t' && c != '\n')
1308 fname = cstring_appendChar (fname, c);
1311 if (c == ' ' || c == '\t' || c == '\n')
1317 while (c == ' ' || c == '\t')
1319 c = macro_nextChar ();
1333 hasParams = (c == '(');
1335 if (usymtab_exists (fname))
1337 e2 = usymtab_lookupExpose (fname);
1338 ct = uentry_getType (e2);
1340 if (uentry_isCodeDefined (e2)
1341 && fileloc_isUser (uentry_whereDefined (e2)))
1345 message ("Macro %s already defined", fname),
1348 uentry_showWhereDefined (e2);
1349 uentry_clearDefined (e2);
1352 if (uentry_isFunction (e2))
1354 uentry_setType (e2, ctype_unknown);
1357 context_enterUnknownMacro (e2);
1361 context_enterConstantMacro (e2);
1366 if (uentry_isForward (e2) && uentry_isFunction (e2))
1373 ("Parameterized macro has no prototype or specification: %s ",
1378 uentry_setType (e2, ctype_unknown);
1379 uentry_setFunctionDefined (e2, loc);
1380 uentry_setUsed (e2, fileloc_undefined);
1381 context_enterUnknownMacro (e2);
1385 if (uentry_isIter (e2))
1388 specparams = uentry_getParams (e2);
1389 noparams = uentryList_size (specparams);
1390 uentry_setDefined (e2, loc);
1391 context_enterIterDef (e2);
1393 else if (uentry_isEndIter (e2))
1396 uentry_setDefined (e2, loc);
1397 context_enterIterEnd (e2); /* don't care about it now */
1398 /* but should parse like an iter! */
1400 else if (uentry_isConstant (e2))
1406 message ("Constant %s implemented as parameterized macro",
1410 uentry_showWhereSpecified (e2);
1411 uentry_setType (e2, ctype_unknown);
1412 uentry_makeConstantFunction (e2);
1413 uentry_setDefined (e2, g_currentloc);
1414 uentry_setFunctionDefined (e2, g_currentloc);
1415 context_enterUnknownMacro (e2);
1419 if (!uentry_isSpecified (e2))
1421 fileloc oloc = uentry_whereDeclared (e2);
1423 if (fileloc_isLib (oloc))
1427 else if (fileloc_isUndefined (oloc)
1428 || fileloc_isPreproc (oloc))
1433 (FLG_MACROCONSTDECL,
1435 ("Macro constant %q not declared",
1436 uentry_getName (e2)),
1440 else if (!fileloc_withinLines (oloc, loc, 2))
1441 { /* bogus! will give errors if there is too much whitespace */
1445 ("Macro constant name %s does not match name in "
1446 "previous constant declaration. This constant "
1447 "is declared at %q", fname,
1448 fileloc_unparse (oloc)),
1457 context_enterConstantMacro (e2);
1458 cstring_free (fname);
1464 else if (ctype_isFunction (ct))
1467 specparams = ctype_argsFunction (ct);
1468 noparams = uentryList_size (specparams);
1470 uentry_setFunctionDefined (e2, loc);
1471 context_enterMacro (e2);
1473 else if (uentry_isVar (e2))
1479 message ("Variable %s implemented as parameterized macro",
1483 uentry_showWhereSpecified (e2);
1484 uentry_setType (e2, ctype_unknown);
1485 uentry_makeVarFunction (e2);
1486 uentry_setDefined (e2, g_currentloc);
1487 uentry_setFunctionDefined (e2, g_currentloc);
1488 context_enterUnknownMacro (e2);
1492 uentry ucons = uentry_makeConstant (fname,
1495 if (uentry_isExpandedMacro (e2))
1503 message ("Variable %s implemented by a macro",
1507 uentry_showWhereSpecified (e2);
1511 uentry_setDefined (e2, loc);
1512 uentry_setUsed (ucons, loc);
1514 context_enterConstantMacro (ucons);
1515 uentry_markOwned (ucons);
1516 cstring_free (fname);
1522 if (uentry_isDatatype (e2))
1526 message ("Type implemented as macro: %x",
1527 uentry_getName (e2)),
1528 message ("A type is implemented using a macro definition. A "
1529 "typedef should be used instead."),
1532 cscanner_swallowMacro ();
1533 /* Must exit scope (not sure why a new scope was entered?) */
1534 usymtab_quietExitScope (g_currentloc);
1535 uentry_setDefined (e2, g_currentloc);
1541 (message ("Unexpanded macro not function or constant: %q",
1542 uentry_unparse (e2)));
1543 uentry_setType (e2, ctype_unknown);
1547 uentry_makeVarFunction (e2);
1548 uentry_setDefined (e2, g_currentloc);
1549 uentry_setFunctionDefined (e2, g_currentloc);
1550 context_enterUnknownMacro (e2);
1561 /* evans 2001-09-09 - if it has params, assume a function */
1565 (FLG_MACROMATCHNAME,
1566 message ("Unexpanded macro %s does not match name of a declared "
1567 "function. The name used in the control "
1568 "comment on the previous line should match.",
1572 ce = uentry_makeFunction (fname, ctype_unknown,
1576 warnClause_undefined,
1578 uentry_setUsed (ce, loc); /* perhaps bogus? */
1579 e2 = usymtab_supEntryReturn (ce);
1580 context_enterUnknownMacro (e2);
1585 (FLG_MACROMATCHNAME,
1586 message ("Unexpanded macro %s does not match name of a constant "
1587 "or iter declaration. The name used in the control "
1588 "comment on the previous line should match. "
1589 "(Assuming macro defines a constant.)",
1593 ce = uentry_makeConstant (fname, ctype_unknown, fileloc_undefined);
1594 uentry_setUsed (ce, loc); /* perhaps bogus? */
1595 e2 = usymtab_supEntryReturn (ce);
1597 context_enterConstantMacro (e2);
1598 cstring_free (fname);
1604 /* in macros, ( must follow immediatetly after name */
1610 c = skip_whitespace ();
1612 while (c != ')' && c != '\0')
1615 bool suppress = context_inSuppressRegion ();
1616 cstring paramname = cstring_undefined;
1619 ** save the parameter location
1623 context_saveLocation ();
1626 while (c != ' ' && c != '\t' && c != ',' && c != '\0' && c != ')')
1628 paramname = cstring_appendChar (paramname, c);
1629 c = macro_nextChar ();
1632 if (c == ' ' || c == '\t') c = skip_whitespace ();
1636 c = macro_nextChar ();
1637 if (c == ' ' || c == '\t') c = skip_whitespace ();
1642 llfatalerror (cstring_makeLiteral
1643 ("Bad macro syntax: uentryList"));
1646 if ((isspecfcn || isiter) && (paramno < noparams)
1647 && !uentry_isElipsisMarker (uentryList_getN
1648 (specparams, paramno)))
1650 fileloc sloc = context_getSaveLocation ();
1651 uentry decl = uentryList_getN (specparams, paramno);
1654 param = uentry_nameCopy (paramname, decl);
1656 uentry_setParam (param);
1657 sr = sRef_makeParam (paramno, uentry_getType (param), stateInfo_makeLoc (sloc));
1659 if (sRef_getNullState (sr) == NS_ABSNULL)
1661 ctype pt = ctype_realType (uentry_getType (param));
1663 if (ctype_isUser (pt))
1665 uentry te = usymtab_getTypeEntrySafe (ctype_typeId (pt));
1667 if (uentry_isValid (te))
1669 sRef_setStateFromUentry (sr, te);
1674 sRef_setNullState (sr, NS_UNKNOWN, sloc);
1678 uentry_setSref (param, sr);
1679 uentry_setDeclaredForceOnly (param, sloc);
1681 skipparam = isiter && uentry_isOut (uentryList_getN (specparams, paramno));
1685 fileloc sloc = context_getSaveLocation ();
1687 param = uentry_makeVariableSrefParam
1688 (paramname, ctype_unknown, fileloc_copy (sloc),
1689 sRef_makeParam (paramno, ctype_unknown, stateInfo_makeLoc (sloc)));
1690 DPRINTF (("Unknown param: %s", uentry_unparseFull (param)));
1691 cstring_free (paramname);
1693 sRef_setPosNull (uentry_getSref (param), sloc);
1694 uentry_setDeclaredForce (param, sloc);
1697 fileloc_free (sloc);
1702 llassert (!uentry_isElipsisMarker (param));
1706 sRef_makeUnsafe (uentry_getSref (param));
1709 pn = uentryList_add (pn, uentry_copy (param));
1710 usymtab_supEntry (param);
1714 /* don't add param */
1715 uentry_free (param);
1720 (void) macro_nextChar ();
1721 c = skip_whitespace ();
1729 if (isspecfcn || isiter)
1731 if (paramno != noparams && noparams >= 0)
1737 message ("Macro %s specified with %d args, defined with %d",
1738 fname, noparams, paramno),
1741 uentry_showWhereSpecified (e2);
1742 uentry_resetParams (e2, pn);
1747 uentry_resetParams (e2, pn);
1754 ** the form should be:
1756 ** # define newname oldname
1757 ** where oldname refers to a function matching the specification
1763 sRef_setGlobalScope ();
1764 usymtab_supGlobalEntry (uentry_makeVariableLoc (fname, ctype_unknown));
1765 sRef_clearGlobalScope ();
1769 context_setMacroMissingParams ();
1774 /* context_setuentryList (pn); */
1775 usymtab_enterScope ();
1778 cstring_free (fname);
1783 static bool handleSpecial (char *yyt)
1785 char *l; /* !! = mstring_create (MAX_NAME_LENGTH); */
1792 len_yyt = strlen (yyt +1) ;
1794 l = mstring_copy (yyt + 1);
1796 while ((c = char_fromInt (lminput ())) != '\n' && c != '\0')
1798 l = mstring_append(l, c);
1801 /* Need to safe original l for deallocating. */
1806 olc = cstring_fromChars (ol);
1808 if (cstring_equalPrefixLit (olc, "pragma"))
1810 char *pname = mstring_create (size_fromInt (MAX_PRAGMA_LEN));
1811 char *opname = pname;
1812 char *ptr = ol + 6; /* pragma is six characters, plus space */
1816 /* skip whitespace */
1817 while (((c = *ptr) != '\0') && isspace (c))
1823 while (((c = *ptr) != '\0') && !isspace (c))
1827 if (len > MAX_PRAGMA_LEN)
1838 if (len == PRAGMA_LEN_EXPAND
1839 && mstring_equal (opname, PRAGMA_EXPAND))
1841 cstring exname = cstring_undefined;
1845 while (((c = *ptr) != '\0') && !isspace (c))
1847 exname = cstring_appendChar (exname, c);
1852 ue = usymtab_lookupExposeGlob (exname);
1854 if (uentry_isExpandedMacro (ue))
1856 if (fileloc_isPreproc (uentry_whereDefined (ue)))
1858 fileloc_setColumn (g_currentloc, 1);
1859 uentry_setDefined (ue, g_currentloc);
1863 cstring_free (exname);
1866 else if (cstring_equalPrefixLit (olc, "ident"))
1868 /* Some pre-processors will leave these in the code. Ignore rest of line */
1871 ** Yuk...Win32 filenames can have spaces in them...we need to read
1872 ** to the matching end quote.
1874 else if ((sscanf (ol, "line %d \"", &lineno) == 1)
1875 || (sscanf (ol, " %d \"", &lineno) == 1))
1881 /*@access cstring@*/
1882 while (*tmp != '\"' && *tmp != '\0')
1887 llassert (*tmp == '\"');
1893 while (*tmp != '\"' && *tmp != '\0')
1898 llassert (*tmp == '\"');
1902 # if defined(OS2) || defined(MSDOS) || defined(WIN32)
1905 ** DOS-like path delimiters get delivered in pairs, something like
1906 ** \"..\\\\file.h\", so we have to make it normal again. We do NOT
1907 ** remove the pre dirs yet as we usually specify tmp paths relative
1908 ** to the current directory, so tmp files would not get found in
1909 ** the hash table. If this method fails we try it again later.
1916 ** Skip past the drive marker.
1919 if (strchr (stmp, ':') != NULL)
1921 stmp = strchr (stmp, ':') + 1;
1924 while ((stmp = strchr (stmp, CONNECTCHAR)) != NULL )
1926 if (*(stmp+1) == CONNECTCHAR)
1928 memmove (stmp, stmp+1, strlen (stmp));
1934 fid = fileTable_lookupBase (context_fileTable (), fname);
1935 if (!(fileId_isValid (fid)))
1937 fname = removePreDirs (fname);
1938 fid = fileTable_lookupBase (context_fileTable (), fname);
1941 # else /* !defined(OS2) && !defined(MSDOS) */
1942 fname = removePreDirs (fname);
1943 fid = fileTable_lookupBase (context_fileTable (), fname);
1944 # endif /* !defined(OS2) && !defined(MSDOS) */
1946 if (!(fileId_isValid (fid)))
1948 if (context_inXHFile ())
1950 fid = fileTable_addXHFile (context_fileTable (), fname);
1952 else if (isHeaderFile (fname))
1954 fid = fileTable_addHeaderFile (context_fileTable (), fname);
1958 fid = fileTable_addFile (context_fileTable (), fname);
1962 setFileLine (fid, lineno);
1963 /*@noaccess cstring@*/
1965 else if ((sscanf (ol, "line %d", &lineno) == 1)
1966 || (sscanf (ol, " %d", &lineno) == 1))
1968 setLine (lineno); /* next line is <cr> */
1972 if (mstring_equal (ol, "")) {
1973 DPRINTF (("Empty pp command!"));
1975 ** evs 2000-05-16: This is a horrible kludge, to get around a bug (well, difficulty) in the pre-processor.
1976 ** We handle a plain # in the input file, by echoing it, and ignoring it in the post-pp-file.
1982 (FLG_UNRECOGDIRECTIVE,
1983 message ("Unrecognized pre-processor directive: #%s",
1984 cstring_fromChars (ol)),
1989 return FALSE; /* evans 2001-12-30: was: TRUE; */
1996 static int handleLlSpecial (void)
2001 char *s = mstring_createEmpty ();
2007 loc = fileloc_copy (g_currentloc);
2008 DPRINTF (("Handle special: %s", fileloc_unparse (loc)));
2010 while (((ic = ninput ()) != 0) && isalpha (ic))
2013 s = mstring_append (s, c);
2017 DPRINTF (("Read: %s / %s", s, fileloc_unparse (g_currentloc)));
2020 if (charsread == 0 && ic == (int) AFTER_COMMENT_MARKER[0])
2024 llassert (ic == (int) AFTER_COMMENT_MARKER[1]);
2030 return QNOMODS; /* special token no modifications token */
2034 DPRINTF (("Coment marker: %s", os));
2035 tok = commentMarkerToken (cstring_fromChars (os));
2039 tokLength = charsread;
2046 DPRINTF (("Not a comment marker..."));
2047 /* Add rest of the comment */
2049 if (ic != 0 && ic != EOF)
2053 s = mstring_append (s, c);
2056 while (((ic = ninput ()) != 0) && (ic != EOF)
2057 && (ic != (int) AFTER_COMMENT_MARKER[0]))
2061 /* evans 2001-09-01 added to prevent assertion failures for uncloses syntactic comments */
2064 hasnl = TRUE; /* This prevents tokLength from being set later. */
2069 message ("Likely parse error: syntactic comment token spans multiple lines: %s",
2070 cstring_fromChars (s)),
2074 s = mstring_append (s, c);
2079 DPRINTF (("Read: %s / %s", s, fileloc_unparse (g_currentloc)));
2081 if (ic == (int) AFTER_COMMENT_MARKER[0])
2084 llassert ((char) nc == AFTER_COMMENT_MARKER[1]);
2090 while (*s == ' ' || *s == '\t' || *s == '\n')
2095 if (*s == '-' || *s == '+' || *s == '=') /* setting flags */
2099 while (c == '-' || c == '+' || c == '=')
2101 ynm set = ynm_fromCodeChar (c);
2106 thisflag = cstring_fromChars (s);
2108 while ((c = *s) != '\0' && (c != '-') && (c != '=')
2109 && (c != '+') && (c != ' ') && (c != '\t') && (c != '\n'))
2116 if (!context_getFlag (FLG_NOCOMMENTS))
2118 cstring flagname = thisflag;
2119 flagcode fflag = flags_identifyFlag (flagname);
2121 if (flagcode_isSkip (fflag))
2125 else if (flagcode_isModeName (fflag))
2127 if (ynm_isMaybe (set))
2132 ("Semantic comment attempts to restore flag %s. "
2133 "A mode flag cannot be restored.",
2138 context_setMode (flagname);
2141 else if (flagcode_isInvalid (fflag))
2144 (FLG_UNRECOGFLAGCOMMENTS,
2145 message ("Unrecognized option in semantic comment: %s",
2149 else if (flagcode_isGlobalFlag (fflag))
2154 ("Semantic comment attempts to set global flag %s. "
2155 "A global flag cannot be set locally.",
2161 context_fileSetFlag (fflag, set, loc);
2163 if (flagcode_hasArgument (fflag))
2165 if (ynm_isMaybe (set))
2170 ("Semantic comment attempts to restore flag %s. "
2171 "A flag for setting a value cannot be restored.",
2176 { /* cut-and-pastied from llmain...blecch */
2177 cstring extra = cstring_undefined;
2183 rest = mstring_copy (s);
2187 while ((rchar = *rest) != '\0'
2188 && (isspace (rchar)))
2194 while ((rchar = *rest) != '\0'
2195 && !isspace (rchar))
2197 extra = cstring_appendChar (extra, rchar);
2201 s--; /* evans 2002-07-12: this was previously only in the else branch.
2202 Leads to an invalid read on the true branch.
2207 if (cstring_isUndefined (extra))
2212 ("Flag %s (in semantic comment) must be followed by an argument",
2213 flagcode_unparse (fflag)));
2218 if (flagcode_hasNumber (fflag))
2220 setValueFlag (fflag, extra);
2222 else if (flagcode_hasChar (fflag))
2224 setValueFlag (fflag, extra);
2226 else if (flagcode_hasString (fflag))
2228 setStringFlag (fflag, extra);
2232 cstring_free (extra);
2246 while ((c == ' ') || (c == '\t') || (c == '\n'))
2252 if (context_inHeader () && !isArtificial (cstring_fromChars (os)))
2254 DPRINTF (("Here adding comment: %s", os));
2255 context_addComment (cstring_fromCharsNew (os), loc);
2267 annotationInfo ainfo;
2269 while (*s != '\0' && *s != ' ' && *s != '\t' && *s != '\n')
2281 t = cstring_toCharsSafe (cstring_downcase (cstring_fromChars (t)));
2282 macrocode = tokenMacroCode (cstring_fromChars (t));
2284 if (macrocode != BADTOK)
2286 tokLength = hasnl ? 0 : size_toInt (mstring_length (t));
2292 if (macrocode == SKIPTOK)
2300 ainfo = context_lookupAnnotation (cstring_fromChars (os));
2302 if (annotationInfo_isDefined (ainfo)) {
2303 DPRINTF (("Found annotation: %s", annotationInfo_unparse (ainfo)));
2304 /*@i324@*/ yylval.annotation = ainfo;
2312 if (context_inHeader ())
2319 if ((context_inMacro () || context_inGlobalContext ())
2320 && macrocode != SKIPTOK
2321 && !isArtificial (cstring_fromChars (os)))
2323 if (context_processingMacros ())
2325 /* evans 2002-02-24: don't add comments when procssing macros */
2329 context_addComment (cstring_fromCharsNew (os), loc);
2343 if (mstring_equal (t, "ignore"))
2345 if (!context_getFlag (FLG_NOCOMMENTS))
2347 context_enterSuppressRegion (loc);
2350 else if ((*t == 'i' || *t == 't')
2351 && (*(t + 1) == '\0'))
2353 if (!context_getFlag (FLG_NOCOMMENTS)
2354 && (*t == 'i' || context_getFlag (FLG_TMPCOMMENTS)))
2356 context_enterSuppressLine (-1, loc); /* infinite suppression */
2359 else if (((*t == 'i') || (*t == 't'))
2360 && ((*(t + 1) >= '0' && *(t + 1) <= '9')))
2362 bool tmpcomment = (*t == 't');
2364 char *tt = t; /* don't mangle t, since it is free'd */
2367 if (lc >= '0' && lc <= '9')
2369 val = (int)(lc - '0');
2372 while (lc >= '0' && lc <= '9')
2375 val += (int) (lc - '0');
2380 if (!context_getFlag (FLG_NOCOMMENTS)
2381 && (!tmpcomment || context_getFlag (FLG_TMPCOMMENTS)))
2383 DPRINTF (("Here: enter suppress: %s", fileloc_unparse (loc)));
2384 context_enterSuppressLine (val, loc);
2387 else if (mstring_equal (t, "end"))
2389 if (!context_getFlag (FLG_NOCOMMENTS))
2391 context_exitSuppressRegion (loc);
2394 else if (mstring_equal (t, "notfunction"))
2396 ; /* handled by pcpp */
2398 else if (mstring_equal (t, "access"))
2404 while (((c = *s) != '\0') && (c == ' ' || c == '\t' || c == '\n'))
2414 tname = cstring_fromChars (s);
2416 while ((c = *s) != '\0' && c != ' '
2417 && c != '\t' && c != '\n' && c != ',')
2424 DPRINTF (("Access %s", tname));
2426 if (!context_getFlag (FLG_NOCOMMENTS)
2427 && !context_getFlag (FLG_NOACCESS))
2429 if (usymtab_existsType (tname))
2431 typeId uid = usymtab_getTypeId (tname);
2432 uentry ue = usymtab_getTypeEntry (uid);
2434 if (uentry_isAbstractDatatype (ue))
2436 context_addFileAccessType (uid);
2437 DPRINTF (("Adding access to: %s / %d", tname, uid));
2444 ("Non-abstract type %s used in access comment",
2451 if (!(context_inSuppressRegion ()
2452 || context_inSuppressZone (loc)))
2457 ("Unrecognized type %s used in access comment",
2469 if (c != ',' && c != ' ')
2475 else if (mstring_equal (t, "noaccess"))
2482 while (((lc = *s) != '\0') && (lc == ' ' || lc == '\t' || lc == '\n'))
2492 tname = cstring_fromChars (s);
2494 while ((lc = *s) != '\0' && lc != ' ' && lc != '\t'
2495 && lc != '\n' && lc != ',')
2502 if (!context_getFlag (FLG_NOCOMMENTS)
2503 && !context_getFlag (FLG_NOACCESS))
2505 if (usymtab_existsType (tname))
2507 typeId tuid = usymtab_getTypeId (tname);
2509 if (context_couldHaveAccess (tuid))
2511 DPRINTF (("Removing access: %s", tname));
2512 context_removeFileAccessType (tuid);
2516 if (!(context_inSuppressRegion ()
2517 || context_inSuppressZone (loc)))
2519 uentry ue = usymtab_getTypeEntry (tuid);
2521 if (uentry_isAbstractDatatype (ue))
2526 ("Non-accessible abstract type %s used in noaccess comment",
2535 ("Non-abstract type %s used in noaccess comment",
2544 if (!(context_inSuppressRegion ()
2545 || context_inSuppressZone (loc)))
2550 ("Unrecognized type %s used in noaccess comment",
2562 if (lc != ',' && lc != ' ')
2570 voptgenerror (FLG_UNRECOGCOMMENTS,
2571 message ("Semantic comment unrecognized: %s",
2572 cstring_fromChars (os)),
2584 static /*@only@*/ cstring makeIdentifier (char *s)
2586 char *c = mstring_create (strlen (s) + 1);
2587 cstring id = cstring_fromChars (c);
2589 while (isalnum (*s) || (*s == '_') || (*s == '$'))
2598 /*@observer@*/ /*@dependent@*/ uentry coerceId (cstring cn)
2600 if (!(usymtab_exists (cn)))
2602 fileloc loc = fileloc_createExternal ();
2605 ** We need to put this in a global scope, otherwise the sRef will be deallocated.
2608 uentry ce = uentry_makeUnrecognized (cn, loc);
2610 if (!context_inIterEnd ())
2614 message ("Unrecognized (possibly system) identifier: %q",
2615 uentry_getName (ce)),
2622 return (usymtab_lookup (cn));
2626 ** like, coerceId, but doesn't supercede for iters
2629 /*@observer@*/ uentry coerceIterId (cstring cn)
2631 if (!(usymtab_exists (cn)))
2633 return uentry_undefined;
2636 return (usymtab_lookup (cn));
2640 ** Need to keep this in case there is a declaration that isn't processed until
2641 ** the scope exits. Would be good to rearrange the symbol table so this doesn't
2642 ** happen, and save all the cstring copying.
2645 /*@observer@*/ cstring cscanner_observeLastIdentifier ()
2647 cstring res = s_lastidprocessed;
2651 static void cscanner_setLastIdentifier (/*@keep@*/ cstring id) /*@modifies s_lastidprocessed@*/
2653 if (cstring_isDefined (s_lastidprocessed))
2655 cstring_free (s_lastidprocessed);
2658 s_lastidprocessed = id;
2661 static int processIdentifier (cstring id)
2665 if (context_getFlag (FLG_GRAMMAR))
2667 lldiagmsg (message ("Process identifier: %s", id));
2670 context_clearJustPopped ();
2671 cscanner_setLastIdentifier (id);
2673 if (context_inFunctionHeader ())
2675 int tok = commentMarkerToken (id);
2676 DPRINTF (("in function decl..."));
2684 tok = tokenMacroCode (id);
2692 annotationInfo ainfo;
2694 if (expectingMetaStateName)
2696 metaStateInfo msinfo = context_lookupMetaStateInfo (id);
2698 if (metaStateInfo_isDefined (msinfo))
2700 yylval.msinfo = msinfo;
2701 return METASTATE_NAME;
2705 DPRINTF (("Not meta state name: %s", cstring_toCharsSafe (id)));
2709 ainfo = context_lookupAnnotation (id);
2711 if (annotationInfo_isDefined (ainfo))
2713 DPRINTF (("Found annotation: %s", annotationInfo_unparse (ainfo)));
2714 /*@i324@*/ yylval.annotation = ainfo;
2719 DPRINTF (("Not annotation: %s", id));
2725 /* Consider handling: Defined by C99 as static const char __func__[] */
2727 if (context_getFlag (FLG_GNUEXTENSIONS))
2731 if (cstring_equalLit (id, "__stdcall")
2732 || cstring_equalLit (id, "__cdecl")
2733 || cstring_equalLit (id, "__extension__"))
2737 else if (cstring_equalLit (id, "__volatile__"))
2741 else if (cstring_equalLit (id, "__signed"))
2745 else if (cstring_equalLit (id, "__unsigned"))
2749 else if (cstring_equalLit (id, "__const__"))
2753 else if (cstring_equalLit (id, "__alignof__"))
2755 tok = CALIGNOF; /* alignof is parsed like sizeof */
2757 else if (cstring_equalLit (id, "__typeof__"))
2761 else if (cstring_equalLit (id, "typeof"))
2765 else if (cstring_equalLit (id, "__FUNCTION__")
2766 || cstring_equalLit (id, "__PRETTY_FUNCTION__"))
2768 /* These tokens hold the name of the current function as strings */
2769 /* evans 2001-12-30: changed from exprNode_stringLiteral; bug reported by Jim Zelenka. */
2770 yylval.expr = exprNode_makeConstantString (id, fileloc_copy (g_currentloc));
2772 lastWasString = TRUE;
2776 else if (cstring_equalLit (id, "__attribute__")
2777 || cstring_equalLit (id, "__asm__")
2778 || cstring_equalLit (id, "_asm")
2779 || cstring_equalLit (id, "__asm")
2780 || cstring_equalLit (id, "__declspec"))
2783 bool useparens = FALSE;
2784 bool usebraces = FALSE;
2785 bool inquote = FALSE;
2786 bool inescape = FALSE;
2789 while ((ic = input ()) != EOF)
2791 char cc = (char) ic;
2797 else if (cc == '\\')
2801 else if (cc == '\"')
2837 else if (cc == ')' && useparens)
2840 if (depth == 0) break;
2842 else if (cc == '}' && usebraces)
2845 if (depth == 0) break;
2848 && !usebraces && !useparens
2849 && cstring_equalLit (id, "__asm"))
2852 ** We need this because some MS VC++ include files
2853 ** have __asm mov ... }
2854 ** Its a kludge, but otherwise would need to parse
2871 context_incLineno ();
2873 if (cstring_equalLit (id, "__asm")
2874 && !useparens && !usebraces)
2881 llassert ((useparens && ic == (int) ')')
2882 || (usebraces && ic == (int) '}')
2883 || (!useparens && !usebraces));
2887 else if (cstring_equalLit (id, "inline")
2888 || cstring_equalLit (id, "__inline")
2889 || cstring_equalLit (id, "_inline")
2890 || cstring_equalLit (id, "__inline__"))
2905 le = usymtab_lookupSafe (id);
2907 /*@-dependenttrans@*/
2909 if (uentry_isIter (le))
2911 /*@i32@*/ yylval.entry = le;
2914 else if (uentry_isEndIter (le))
2916 /*@i32@*/ yylval.entry = le;
2917 return (ITER_ENDNAME);
2919 else if (uentry_isUndefined (le))
2921 yylval.cname = cstring_copy (id);
2923 /* avoid parse errors for certain system built ins */
2925 if (g_expectingTypeName && (cstring_firstChar (id) == '_')
2926 && (cstring_secondChar (id) == '_'))
2928 return (TYPE_NAME_OR_ID);
2931 return (NEW_IDENTIFIER);
2933 else if (!uentry_isDeclared (le) && !uentry_isCodeDefined (le))
2935 if (uentry_isDatatype (le))
2937 yylval.cname = cstring_copy (id);
2938 return (NEW_IDENTIFIER);
2942 /*@i32@*/ yylval.entry = le;
2943 return (IDENTIFIER);
2946 else if (uentry_isDatatype (le))
2948 if (!g_expectingTypeName)
2950 yylval.cname = cstring_copy (id);
2952 return (NEW_IDENTIFIER);
2956 yylval.ctyp = uentry_getAbstractType (le);
2958 uentry_setUsed (le, g_currentloc);
2964 /*@i32@*/ yylval.entry = le;
2965 return (IDENTIFIER);
2968 /*@=dependenttrans@*/
2971 static bool processHashIdentifier (/*@only@*/ cstring id)
2973 if (context_inMacro () || context_inIterDef () ||
2974 context_inIterEnd ())
2978 context_clearJustPopped ();
2980 le = usymtab_lookupSafe (id);
2981 cscanner_setLastIdentifier (id);
2983 if (uentry_isParam (le) || uentry_isRefParam (le))
2995 ** Will be handled by handleLlSpecial
3004 static /*@only@*/ exprNode processString (void)
3008 char *nl = strchr (yytext, '\n');
3009 cstring ns = cstring_fromCharsNew (yytext);
3013 loc = fileloc_copy (g_currentloc);
3014 addColumn (size_toInt (cstring_length (ns)));
3020 loc = fileloc_copy (g_currentloc);
3022 context_incLineno ();
3024 while ((nl = strchr ((nl + 1), '\n')) != NULL)
3026 context_incLineno ();
3032 res = exprNode_stringLiteral (ns, loc);
3037 ** process a wide character string L"...."
3040 static /*@only@*/ exprNode processWideString ()
3044 char *nl = strchr (yytext, '\n');
3047 llassert (*yytext == 'L');
3050 ns = cstring_fromCharsNew (yytext);
3054 loc = fileloc_copy (g_currentloc);
3055 addColumn (size_toInt (cstring_length (ns)));
3061 loc = fileloc_copy (g_currentloc);
3063 context_incLineno ();
3065 while ((nl = strchr ((nl + 1), '\n')) != NULL)
3067 context_incLineno ();
3072 res = exprNode_wideStringLiteral (ns, loc);
3082 llassert (*yytext != '\0');
3083 fchar = *(yytext + 1);
3084 if (fchar != '\\') return fchar;
3086 next = *(yytext + 2);
3090 case 'n': return '\n';
3091 case 't': return '\t';
3092 case '\"': return '\"';
3093 case '\'': return '\'';
3094 case '\\': return '\\';
3095 default: return '\0';
3100 double processFloat ()
3102 double ret = atof (yytext);
3113 llassert (yytext[0] == '0'
3114 && (yytext[1] == 'X' || yytext[1] == 'x'));
3116 while (yytext[index] != '\0') {
3118 char c = yytext[index];
3120 if (c >= '0' && c <= '9') {
3121 tval = (int) c - (int) '0';
3122 } else if (c >= 'A' && c <= 'F') {
3123 tval = (int) c - (int) 'A' + 10;
3124 } else if (c >= 'a' && c <= 'f') {
3125 tval = (int) c - (int) 'a' + 10;
3126 } else if (c == 'U' || c == 'L' || c == 'u' || c == 'l') {
3128 while (yytext[index] != '\0') {
3129 if (c == 'U' || c == 'L' || c == 'u' || c == 'l') {
3134 message ("Invalid character (%c) following specifier in hex constant: %s",
3135 c, cstring_fromChars (yytext)),
3145 message ("Invalid character (%c) in hex constant: %s",
3146 c, cstring_fromChars (yytext)),
3151 val = (val * 16) + tval;
3155 DPRINTF (("Hex constant: %s = %ld", yytext, val));
3160 long processOctal ()
3165 llassert (yytext[0] == '0' && yytext[1] != 'X' && yytext[1] != 'x');
3167 while (yytext[index] != '\0') {
3169 char c = yytext[index];
3171 if (c >= '0' && c <= '7') {
3172 tval = (int) c - (int) '0';
3173 } else if (c == 'U' || c == 'L' || c == 'u' || c == 'l') {
3175 while (yytext[index] != '\0') {
3176 if (c == 'U' || c == 'L' || c == 'u' || c == 'l') {
3181 message ("Invalid character (%c) following specifier in octal constant: %s",
3182 c, cstring_fromChars (yytext)),
3192 message ("Invalid character (%c) in octal constant: %s",
3193 c, cstring_fromChars (yytext)),
3198 val = (val * 8) + tval;
3202 DPRINTF (("Octal constant: %s = %ld", yytext, val));
3209 return (atol (yytext));
3213 processSpec (int tok)
3215 size_t length = strlen (yytext);
3219 setTokLengthT (length);
3225 context_saveLocation ();
3226 setTokLengthT (length);
3227 return (processIdentifier (makeIdentifier (yytext)));
3231 void cscanner_expectingMetaStateName ()
3233 llassert (!expectingMetaStateName);
3234 llassert (context_inFunctionHeader ());
3235 expectingMetaStateName = TRUE;
3238 void cscanner_clearExpectingMetaStateName ()
3240 llassert (expectingMetaStateName);
3241 expectingMetaStateName = FALSE;