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()... */
77 # include "cgrammar.h"
78 # include "cgrammar_tokens.h"
80 # include "fileIdList.h"
83 static bool lastWasString = FALSE;
84 static char savechar = '\0';
87 # define yyinput() (incColumn (), getc (yyin))
89 static /*@owned@*/ cstring lastidprocessed = cstring_undefined;
90 static int lminput (void);
91 static int tokLength = 0;
92 static bool inSpecPart = FALSE;
93 static bool continueLine = FALSE;
95 static int ninput (void);
96 static char processChar (void);
97 static double processFloat (void);
98 static /*@only@*/ exprNode processString (void) ;
99 static /*@only@*/ exprNode processWideString (void) ;
100 static long processDec (void);
101 static long processHex (void);
102 static long processOctal (void);
103 static int processIdentifier (/*@only@*/ cstring)
104 /*@globals undef lastidprocessed@*/ ;
105 static bool processHashIdentifier (/*@only@*/ cstring)
106 /*@globals undef lastidprocessed@*/ ;
108 static int processSpec (int);
109 static bool handleSpecial (char *);
110 static int handleLlSpecial (void);
111 static void handleMacro (void);
112 static bool processMacro (void);
113 static /*@only@*/ cstring makeIdentifier (char *);
115 /* yes, this is exported! */
116 bool g_expectingTypeName = TRUE; /* beginning of file can be type name! */
118 static bool expectingMetaStateName = FALSE;
120 static int returnInt (ctype, long);
121 static int returnFloat (ctype, double);
122 static int returnChar (char);
123 static void setTokLength (int) /*@modifies g_currentloc@*/ ;
124 static void setTokLengthT (size_t) /*@modifies g_currentloc@*/ ;
126 static void advanceLine (void)
133 # define RETURN_INT(c,i) \
134 do { lastWasString = FALSE; \
135 return (returnInt (c, i)); } while (FALSE)
137 # define RETURN_FLOAT(c,f) \
138 do { lastWasString = FALSE; \
139 return (returnFloat (c, f)); \
142 # define RETURN_CHAR(c) \
143 do { lastWasString = FALSE; \
144 return (returnChar (c)); \
147 # define RETURN_TOK(t) \
148 do { yylval.tok = lltok_create (t, fileloc_decColumn (g_currentloc, tokLength)); \
150 lastWasString = FALSE; \
151 return (t); } while (FALSE)
153 # define RETURN_TYPE(t, ct) \
154 do { yylval.ctyp = ct; tokLength = 0; return (t); } while (FALSE)
156 /* don't fileloc_decColumn (g_currentloc, tokLength));
157 the string could have \n's in it!
160 # define RETURN_STRING(c) \
161 do { yylval.expr = exprNode_stringLiteral (c, fileloc_decColumn (g_currentloc, tokLength)); \
163 lastWasString = TRUE; \
164 return (CCONSTANT); } while (FALSE)
166 # define RETURN_EXPR(e) \
167 do { yylval.expr = e; \
169 lastWasString = TRUE; \
170 return (CCONSTANT); } while (FALSE)
174 static void setTokLength (int len)
178 DPRINTF (("Set tok length: %d", len));
181 static void setTokLengthT (size_t len)
183 setTokLength (size_toInt (len));
186 # include "flex.head"
188 /*@-unrecog@*/ /*@i5343@*/
194 "/*" { llfatalbug (cstring_makeLiteral ("Comment in pre-processor output")); }
196 "#"{Letter}({Letter}|{Digit})* {
197 context_saveLocation ();
198 setTokLength (longUnsigned_toInt (mstring_length (yytext)));
200 if (processHashIdentifier (makeIdentifier (yytext + 1)))
204 /* was nothing! */ /*@i32@*/
205 RETURN_STRING (cstring_makeLiteral ("\"\""));
209 RETURN_STRING (cstring_makeLiteral ("\"\""));
214 if (handleSpecial (yytext))
221 "#" { if (handleSpecial (yytext))
223 setTokLength (1); RETURN_TOK (0);
226 "..." { setTokLength (3); RETURN_TOK (CTOK_ELIPSIS); }
227 "break" { setTokLength (5); RETURN_TOK (BREAK); }
228 "case" { setTokLength (4); RETURN_TOK (CASE); }
229 "continue" { setTokLength (8); RETURN_TOK (CONTINUE); }
230 "default" { setTokLength (7); RETURN_TOK (DEFAULT); }
231 "do" { setTokLength (2); RETURN_TOK (DO); }
232 "else" { setTokLength (4); RETURN_TOK (CELSE); }
233 "for" { setTokLength (3); RETURN_TOK (CFOR); }
234 "goto" { setTokLength (4); RETURN_TOK (GOTO); }
235 "if" { setTokLength (2); RETURN_TOK (CIF); }
236 "return" { setTokLength (6); RETURN_TOK (RETURN); }
237 "sizeof" { setTokLength (6); RETURN_TOK (CSIZEOF); }
238 "offsetof" { setTokLength (8); RETURN_TOK (COFFSETOF); }
239 "switch" { setTokLength (6); RETURN_TOK (SWITCH); }
240 "while" { setTokLength (5); RETURN_TOK (WHILE); }
241 "va_arg" { setTokLength (6); RETURN_TOK (VA_ARG); }
242 "va_dcl" { setTokLength (6); RETURN_TOK (VA_DCL); }
244 /* gcc extension...this might not be appropriate */
245 setTokLength (6); RETURN_TOK (QINLINE); }
247 "struct" { setTokLength (6); RETURN_TOK (CSTRUCT); }
248 "typedef" { setTokLength (7); RETURN_TOK (CTYPEDEF); }
250 "union" { setTokLength (5); RETURN_TOK (CUNION); }
251 "enum" { setTokLength (4); RETURN_TOK (CENUM); }
253 "void" { setTokLength (4); RETURN_TYPE (CVOID, ctype_void); }
254 "int" { setTokLength (3); RETURN_TYPE (CINT, ctype_int); }
255 "double" { setTokLength (6); RETURN_TYPE (CDOUBLE, ctype_double); }
256 "char" { setTokLength (4); RETURN_TYPE (CGCHAR, ctype_char); }
257 "float" { setTokLength (5); RETURN_TYPE (CGFLOAT, ctype_float); }
259 "long" { setTokLength (4); RETURN_TOK (QLONG); }
260 "short" { setTokLength (5); RETURN_TOK (QSHORT); }
261 "unsigned" { setTokLength (8); RETURN_TOK (QUNSIGNED); }
262 "signed" { setTokLength (6); RETURN_TOK (QSIGNED); }
264 "volatile" { setTokLength (8); RETURN_TOK (QVOLATILE); }
265 "const" { setTokLength (5); RETURN_TOK (QCONST); }
267 /* some systems expect this! [gack!] */
268 "__const" { setTokLength (7); RETURN_TOK (QCONST); }
270 "extern" { setTokLength (6); RETURN_TOK (QEXTERN); }
271 "auto" { setTokLength (4); RETURN_TOK (QAUTO); }
272 "register" { setTokLength (8); RETURN_TOK (QREGISTER); }
273 "static" { setTokLength (6); RETURN_TOK (QSTATIC); }
275 \"(\\.|[^\\"])*\"([ \t\n]*\"(\\.|[^\\"])*\")* { RETURN_EXPR (processString ()); }
276 L\"(\\.|[^\\"])*\"([ \t\n]*\"(\\.|[^\\"])*\")* { RETURN_EXPR (processWideString ()); }
277 "out" { return (processSpec (QOUT)); }
278 "in" { return (processSpec (QIN)); }
279 "partial" { return (processSpec (QPARTIAL)); }
280 "special" { return (processSpec (QSPECIAL)); }
281 "anytype" { return (processSpec (QANYTYPE)); }
282 "integraltype" { return (processSpec (QINTEGRALTYPE)); }
283 "unsignedintegraltype" { return (processSpec (QUNSIGNEDINTEGRALTYPE)); }
284 "signedintegraltype" { return (processSpec (QSIGNEDINTEGRALTYPE)); }
285 "keep" { return (processSpec (QKEEP)); }
286 "null" { return (processSpec (QNULL)); }
287 "notnull" { return (processSpec (QNOTNULL)); }
288 "isnull" { return (processSpec (QISNULL)); }
289 "truenull" { return (processSpec (QTRUENULL)); }
290 "falsenull" { return (processSpec (QFALSENULL)); }
291 "nullwhentrue" { return (processSpec (QTRUENULL)); }
292 "nullwhenfalse" { return (processSpec (QFALSENULL)); }
293 "relnull" { return (processSpec (QRELNULL)); }
294 "reldef" { return (processSpec (QRELDEF)); }
295 "exposed" { return (processSpec (QEXPOSED)); }
296 "newref" { return (processSpec (QNEWREF)); }
297 "tempref" { return (processSpec (QTEMPREF)); }
298 "killref" { return (processSpec (QKILLREF)); }
299 "refcounted" { return (processSpec (QREFCOUNTED)); }
300 "checked" { return (processSpec (QCHECKED)); }
301 "checkmod" { return (processSpec (QCHECKMOD)); }
302 "checkedstrict" { return (processSpec (QCHECKEDSTRICT)); }
303 "unchecked" { return (processSpec (QUNCHECKED)); }
304 "only" { return (processSpec (QONLY)); }
305 "owned" { return (processSpec (QOWNED)); }
306 "observer" { return (processSpec (QOBSERVER)); }
307 "dependent" { return (processSpec (QDEPENDENT)); }
308 "unused" { return (processSpec (QUNUSED)); }
309 "external" { return (processSpec (QEXTERNAL)); }
310 "sef" { return (processSpec (QSEF)); }
311 "shared" { return (processSpec (QSHARED)); }
312 "yield" { return (processSpec (QYIELD)); }
313 "undef" { return (processSpec (QUNDEF)); }
314 "killed" { return (processSpec (QKILLED)); }
315 "nullterminated" { return (processSpec (QNULLTERMINATED));}
316 "MaxSet" { return (processSpec (QMAXSET));}
317 "MaxRead" { return (processSpec (QMAXREAD));}
318 "maxSet" { return (processSpec (QMAXSET));}
319 "maxRead" { return (processSpec (QMAXREAD));}
321 {Letter}({Letter}|{Digit})* { int tok;
322 context_saveLocation ();
323 setTokLength (longUnsigned_toInt (mstring_length (yytext)));
324 tok = processIdentifier (makeIdentifier (yytext));
330 0[xX]{H}+ { setTokLengthT (mstring_length (yytext));
331 RETURN_INT (ctype_int, processHex ()); /* evs 2000-05-17 was ctype_uint */
333 0[xX]{H}+{L} { setTokLengthT (mstring_length (yytext));
334 RETURN_INT (ctype_lint, processHex ()); }
335 0[xX]{H}+{L}{L} { setTokLengthT (mstring_length (yytext));
336 RETURN_INT (ctype_llint, processHex ()); }
337 0[xX]{H}+{U} { setTokLengthT (mstring_length (yytext));
338 RETURN_INT (ctype_uint, processHex ()); }
339 0[xX]{H}+{ULSuffix} { setTokLengthT (mstring_length (yytext));
340 RETURN_INT (ctype_ulint, processHex ()); }
341 0[xX]{H}+{U}{L}{L} { setTokLengthT (mstring_length (yytext));
342 RETURN_INT (ctype_ullint, processHex ()); }
343 0[xX]{H}+{L}{L}{U} { setTokLengthT (mstring_length (yytext));
344 RETURN_INT (ctype_ullint, processHex ()); }
345 0{Digit}+ { setTokLengthT (mstring_length (yytext));
346 RETURN_INT (ctype_int, processOctal ()); }
347 0{Digit}+{U} { setTokLengthT (mstring_length (yytext));
348 RETURN_INT (ctype_uint, processOctal ()); }
349 0{Digit}+{L} { setTokLengthT (mstring_length (yytext));
350 RETURN_INT (ctype_lint, processOctal ()); }
351 0{Digit}+{L}{L} { setTokLengthT (mstring_length (yytext));
352 RETURN_INT (ctype_llint, processOctal ()); }
353 0{Digit}+{ULSuffix} { setTokLengthT (mstring_length (yytext));
354 RETURN_INT (ctype_ulint, processOctal ()); }
355 0{Digit}+{U}{L}{L} { setTokLengthT (mstring_length (yytext));
356 RETURN_INT (ctype_ullint, processOctal ()); }
357 0{Digit}+{L}{L}{U} { setTokLengthT (mstring_length (yytext));
358 RETURN_INT (ctype_ullint, processOctal ()); }
359 {Digit}+ { setTokLengthT (mstring_length (yytext));
360 RETURN_INT (ctype_int, processDec ()); }
361 {Digit}+{U} { setTokLengthT (mstring_length (yytext));
362 RETURN_INT (ctype_uint, processDec ()); }
363 {Digit}+{L} { setTokLengthT (mstring_length (yytext));
364 RETURN_INT (ctype_lint, processDec ()); }
365 {Digit}+{L}{L} { setTokLengthT (mstring_length (yytext));
366 RETURN_INT (ctype_llint, processDec ()); }
367 {Digit}+{ULSuffix} { setTokLengthT (mstring_length (yytext));
368 RETURN_INT (ctype_ulint, processDec ()); }
369 {Digit}+{U}{L}{L} { setTokLengthT (mstring_length (yytext));
370 RETURN_INT (ctype_ullint, processDec ()); }
371 {Digit}+{L}{L}{U} { setTokLengthT (mstring_length (yytext));
372 RETURN_INT (ctype_ullint, processDec ()); }
373 '(\\.|[^\\'])+' { setTokLengthT (mstring_length (yytext));
374 RETURN_CHAR (processChar ()); }
375 L'(\\.|[^\\'])+' { setTokLengthT (mstring_length (yytext));
376 RETURN_CHAR (processChar ()); }
377 {Digit}+{E}[fF] { setTokLengthT (mstring_length (yytext));
378 RETURN_FLOAT (ctype_float, processFloat ()); }
379 {Digit}+{E}[lL] { setTokLengthT (mstring_length (yytext));
380 RETURN_FLOAT (ctype_ldouble, processFloat ()); }
381 {Digit}+{E} { setTokLengthT (mstring_length (yytext));
382 RETURN_FLOAT (ctype_double, processFloat ()); }
384 {Digit}*"."{Digit}+({E})?[fF] { setTokLengthT (mstring_length (yytext));
385 RETURN_FLOAT (ctype_float, processFloat ()); }
386 {Digit}*"."{Digit}+({E})?[lL] { setTokLengthT (mstring_length (yytext));
387 RETURN_FLOAT (ctype_ldouble, processFloat ()); }
388 {Digit}*"."{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 ">>=" { setTokLength (3); RETURN_TOK (RIGHT_ASSIGN); }
399 "<<=" { setTokLength (3); RETURN_TOK (LEFT_ASSIGN); }
400 "+=" { setTokLength (2); RETURN_TOK (ADD_ASSIGN); }
401 "-=" { setTokLength (2); RETURN_TOK (SUB_ASSIGN); }
402 "*=" { setTokLength (2); RETURN_TOK (MUL_ASSIGN); }
403 "/=" { setTokLength (2); RETURN_TOK (DIV_ASSIGN); }
404 "%=" { setTokLength (2); RETURN_TOK (MOD_ASSIGN); }
405 "&=" { setTokLength (2); RETURN_TOK (AND_ASSIGN); }
406 "^=" { setTokLength (2); RETURN_TOK (XOR_ASSIGN); }
407 "|=" { setTokLength (2); RETURN_TOK (OR_ASSIGN); }
408 ">>" { setTokLength (2); RETURN_TOK (RIGHT_OP); }
409 "<<" { setTokLength (2); RETURN_TOK (LEFT_OP); }
410 "++" { setTokLength (2); RETURN_TOK (INC_OP); }
411 "--" { setTokLength (2); RETURN_TOK (DEC_OP); }
412 "->" { setTokLength (2); RETURN_TOK (ARROW_OP); }
413 "&&" { setTokLength (2); RETURN_TOK (AND_OP); }
414 "||" { setTokLength (2); RETURN_TOK (OR_OP); }
415 "<=" { setTokLength (2); RETURN_TOK (LE_OP); }
416 ">=" { setTokLength (2); RETURN_TOK (GE_OP); }
417 "==" { setTokLength (2); RETURN_TOK (EQ_OP); }
418 "!=" { setTokLength (2); RETURN_TOK (NE_OP); }
419 ";" { setTokLength (1); RETURN_TOK (TSEMI); }
420 "{" { setTokLength (1); RETURN_TOK (TLBRACE); }
421 "}" { setTokLength (1); RETURN_TOK (TRBRACE); }
422 "," { setTokLength (1); RETURN_TOK (TCOMMA); }
423 ":" { setTokLength (1); RETURN_TOK (TCOLON); }
424 "=" { setTokLength (1); RETURN_TOK (TASSIGN); }
425 "(" { setTokLength (1); RETURN_TOK (TLPAREN); }
426 ")" { setTokLength (1); RETURN_TOK (TRPAREN); }
427 "[" { setTokLength (1); RETURN_TOK (TLSQBR); }
428 "]" { setTokLength (1); RETURN_TOK (TRSQBR); }
429 "." { setTokLength (1); RETURN_TOK (TDOT); }
430 "&" { setTokLength (1); RETURN_TOK (TAMPERSAND); }
431 "!" { setTokLength (1); RETURN_TOK (TEXCL); }
434 "~" { setTokLength (1); RETURN_TOK (TTILDE); }
435 "-" { setTokLength (1); RETURN_TOK (TMINUS); }
436 "+" { setTokLength (1); RETURN_TOK (TPLUS); }
437 "*" { setTokLength (1); RETURN_TOK (TMULT); }
438 "/" { setTokLength (1); RETURN_TOK (TDIV); }
439 "%" { setTokLength (1); RETURN_TOK (TPERCENT); }
440 "<" { setTokLength (1); RETURN_TOK (TLT); }
441 ">" { setTokLength (1); RETURN_TOK (TGT); }
442 "^" { setTokLength (1); RETURN_TOK (TCIRC); }
443 "|" { setTokLength (1); RETURN_TOK (TBAR); }
444 "?" { setTokLength (1); RETURN_TOK (TQUEST); }
447 "/\\" { setTokLength (1); RETURN_TOK (TCAND); }
450 [ \t\v\f] { incColumn (); }
451 \n { context_incLineno ();
452 if (tokLength != 0) {
454 /* No error to report
457 message ("Likely parse error: token spans multiple lines."),
464 continueLine = FALSE;
468 if (context_inMacro ())
470 /* Don't use RETURN_TOK */
471 yylval.tok = lltok_create (TENDMACRO, g_currentloc);
472 lastWasString = FALSE;
477 "@@MR@@" { setTokLength (6);
479 if (processMacro ()) {
480 if (context_inIterDef ())
482 RETURN_TOK (LLMACROITER);
484 if (context_inIterEnd ())
486 RETURN_TOK (LLMACROEND);
488 if (context_inMacro ())
490 RETURN_TOK (LLMACRO);
494 "@QLMR" { if (context_inHeader () || context_inFunction ())
500 int nspchar = ninput ();
504 ** This is a hack to get the column number correct.
507 llassert (nspchar >= '0' && nspchar <= '9');
509 nspaces = nspchar - '0';
511 setTokLength (5 + nspaces);
515 DPRINTF (("Here we are: %s", context_unparse ()));
517 if (context_inIterDef ())
519 RETURN_TOK (LLMACROITER);
521 if (context_inIterEnd ())
523 RETURN_TOK (LLMACROEND);
525 if (context_inMacro ())
527 RETURN_TOK (LLMACRO);
532 "@.CT" { setTokLength (4); lldiagmsg (ctype_unparseTable ()); }
533 "@.FA" { setTokLength (4); lldiagmsg (message ("Access types: %q", typeIdSet_unparse (context_fileAccessTypes ()))); }
534 "@.F" { setTokLength (3);
535 lldiagmsg (message ("%q: *** marker ***", fileloc_unparse (g_currentloc)));
537 "@.L" { setTokLength (3); usymtab_printLocal (); }
538 "@.A" { setTokLength (3); lldiagmsg (usymtab_unparseAliases ()); }
539 "@.C" { setTokLength (3); lldiagmsg (context_unparse ()); }
540 "@.W" { setTokLength (3); lldiagmsg (context_unparseClauses ()); }
541 "@.G" { setTokLength (3); usymtab_printGuards (); }
542 "@.S" { setTokLength (3); usymtab_printOut (); }
543 "@.X" { setTokLength (3); usymtab_printAll (); }
544 "@.Z" { setTokLength (3); usymtab_printComplete (); }
545 "@.T" { setTokLength (3); usymtab_printTypes (); }
546 "@.K" { setTokLength (3); lldiagmsg (usymtab_unparseStack ()); }
547 "@.M" { setTokLength (3);
548 lldiagmsg (message ("Can modify: %q",
549 sRefSet_unparse (context_modList ())));
551 "%{" { /* BEFORE_COMMENT_MARKER */
553 incColumn (); incColumn ();
554 tok = handleLlSpecial ();
558 if (tok == CANNOTATION) {
561 /* Beware - this bashes yylval! */
566 "%}" { /* AFTER_COMMENT_MARKER */
569 RETURN_TOK (QENDMACRO); }
570 "\\" { incColumn (); continueLine = TRUE; }
572 if ((int) *yytext == 13 ) {
577 message ("Invalid character (ascii: %d), skipping character",
586 /*@null@*/ /*@observer@*/ char *name;
591 ** These tokens are followed by syntax that is parsed by the
595 struct skeyword s_parsetable[] = {
596 { "modifies", QMODIFIES } ,
597 { "globals", QGLOBALS } ,
600 { "constant", QCONSTANT } ,
601 { "function", QFUNCTION } ,
603 { "defines", QDEFINES } ,
605 { "allocates", QALLOCATES } ,
607 { "releases", QRELEASES } ,
608 { "pre", QPRECLAUSE } ,
609 { "post", QPOSTCLAUSE } ,
610 { "setBufferSize", QSETBUFFERSIZE},
611 { "setStringLength", QSETSTRINGLENGTH},
612 { "testinRange", QTESTINRANGE},
613 { "requires", QPRECLAUSE } ,
614 { "ensures", QPOSTCLAUSE } ,
619 ** These tokens are either stand-alone tokens, or followed by
620 ** token-specific text.
623 struct skeyword s_keytable[] = {
624 { "anytype", QANYTYPE } ,
625 { "integraltype", QINTEGRALTYPE } ,
626 { "unsignedintegraltype", QUNSIGNEDINTEGRALTYPE } ,
627 { "signedintegraltype", QSIGNEDINTEGRALTYPE } ,
631 { "owned", QOWNED } ,
632 { "dependent", QDEPENDENT } ,
633 { "partial", QPARTIAL } ,
634 { "special", QSPECIAL } ,
635 { "truenull", QTRUENULL } ,
636 { "falsenull", QFALSENULL } ,
637 { "nullwhentrue", QTRUENULL } ,
638 { "falsewhennull", QFALSENULL } ,
641 { "notnull", QNOTNULL } ,
642 { "abstract", QABSTRACT } ,
643 { "concrete", QCONCRETE } ,
644 { "mutable", QMUTABLE } ,
645 { "immutable", QIMMUTABLE } ,
646 { "unused", QUNUSED } ,
647 { "external", QEXTERNAL } ,
649 { "unique", QUNIQUE } ,
650 { "returned", QRETURNED } ,
651 { "exposed", QEXPOSED } ,
652 { "refcounted", QREFCOUNTED } ,
654 { "newref", QNEWREF } ,
655 { "tempref", QTEMPREF } ,
656 { "killref", QKILLREF } ,
658 { "relnull", QRELNULL } ,
659 { "nullterminated", QNULLTERMINATED },
660 { "setBufferSize", QSETBUFFERSIZE },
661 { "testInRange", QTESTINRANGE},
662 { "MaxSet", QMAXSET},
663 { "MaxRead", QMAXREAD},
664 { "reldef", QRELDEF } ,
665 { "observer", QOBSERVER } ,
666 { "exits", QEXITS } ,
667 { "noreturn", QEXITS } ,
668 { "mayexit", QMAYEXIT } ,
669 { "maynotreturn", QMAYEXIT } ,
670 { "trueexit", QTRUEEXIT } ,
671 { "falseexit", QFALSEEXIT } ,
672 { "noreturnwhentrue", QTRUEEXIT } ,
673 { "noreturnwhenfalse", QFALSEEXIT } ,
674 { "neverexit", QNEVEREXIT } ,
675 { "alwaysreturns", QNEVEREXIT } ,
677 { "shared", QSHARED } ,
679 { "unchecked", QUNCHECKED } ,
680 { "checked", QCHECKED } ,
681 { "checkmod", QCHECKMOD } ,
682 { "checkedstrict", QCHECKEDSTRICT } ,
683 { "innercontinue", QINNERCONTINUE } ,
684 { "innerbreak", QINNERBREAK } ,
685 { "loopbreak", QLOOPBREAK } ,
686 { "switchbreak", QSWITCHBREAK } ,
687 { "safebreak", QSAFEBREAK } ,
688 { "fallthrough", QFALLTHROUGH } ,
689 { "l_fallthrou", QLINTFALLTHROUGH } ,
690 { "l_fallth", QLINTFALLTHRU } ,
691 { "notreached", QNOTREACHED } ,
692 { "l_notreach", QLINTNOTREACHED } ,
693 { "printflike", QPRINTFLIKE } ,
694 { "l_printfli", QLINTPRINTFLIKE } ,
695 { "scanflike", QSCANFLIKE } ,
696 { "messagelike", QMESSAGELIKE } ,
697 { "l_argsus", QARGSUSED } ,
702 ** would be better if these weren't hard coded...
705 static bool isArtificial (cstring s)
707 return (cstring_equalLit (s, "modifies")
708 || cstring_equalLit (s, "globals")
709 || cstring_equalLit (s, "warn")
710 || cstring_equalLit (s, "alt"));
713 void swallowMacro (void)
716 bool skipnext = FALSE;
718 while ((i = lminput ()) != EOF)
735 reader_checkUngetc (i, yyin);
743 reader_checkUngetc (i, yyin);
747 static int commentMarkerToken (cstring s)
751 while (s_parsetable[i].name != NULL)
753 DPRINTF (("Try :%s:%s:", s, s_parsetable[i].name));
755 if (cstring_equalLit (s, s_parsetable[i].name))
757 return s_parsetable[i].token;
766 static int tokenMacroCode (cstring s)
770 while (s_keytable[i].name != NULL)
772 if (cstring_equalLit (s, s_keytable[i].name))
774 if (s_keytable[i].token == QLINTFALLTHROUGH)
777 (FLG_WARNLINTCOMMENTS,
779 ("Traditional lint comment /*FALLTHROUGH*/ used. "
780 "Splint interprets this in the same way as most Unix lints, but it is "
781 "preferable to replace it with the /*@fallthrough@*/ "
786 else if (s_keytable[i].token == QLINTFALLTHRU)
789 (FLG_WARNLINTCOMMENTS,
791 ("Traditional lint comment /*FALLTHRU*/ used. "
792 "Splint interprets this in the same way as most Unix lints, but it is "
793 "preferable to replace it with the /*@fallthrough@*/ "
798 else if (s_keytable[i].token == QLINTNOTREACHED)
801 (FLG_WARNLINTCOMMENTS,
803 ("Traditional lint comment /*NOTREACHED*/ used. "
804 "Splint interprets this in the same way as most Unix lints, but it is "
805 "preferable to replace it with the /*@notreached@*/ "
806 "semantic comment."),
811 else if (s_keytable[i].token == QPRINTFLIKE)
813 setSpecialFunction (qual_createPrintfLike ());
816 else if (s_keytable[i].token == QLINTPRINTFLIKE)
819 (FLG_WARNLINTCOMMENTS,
821 ("Traditional lint comment /*PRINTFLIKE*/ used. "
822 "Splint interprets this in the same way as most Unix lints, but it is "
823 "preferable to replace it with either /*@printflike@*/, "
824 "/*@scanflike@*/ or /*@messagelike@*/."),
827 setSpecialFunction (qual_createPrintfLike ());
830 else if (s_keytable[i].token == QSCANFLIKE)
832 setSpecialFunction (qual_createScanfLike ());
835 else if (s_keytable[i].token == QMESSAGELIKE)
837 setSpecialFunction (qual_createMessageLike ());
840 else if (s_keytable[i].token == QARGSUSED)
843 (FLG_WARNLINTCOMMENTS,
845 ("Traditional lint comment /*ARGSUSED*/ used. "
846 "Splint interprets this in the same way as most Unix lints, but it is "
847 "preferable to use /*@unused@*/ annotations on "
848 "the unused parameters."),
855 return s_keytable[i].token;
864 static int lminput ()
866 if (savechar == '\0')
873 int save = (int) savechar;
879 static void lmsavechar (char c)
881 if (savechar == '\0') savechar = c;
884 llbuglit ("lmsavechar: override");
888 static int returnFloat (ctype ct, double f)
890 yylval.expr = exprNode_floatLiteral (f, ct, cstring_fromChars (yytext),
891 fileloc_decColumn (g_currentloc, tokLength));
896 static int returnInt (ctype ct, long i)
900 if (ctype_equal (ct, ctype_int))
904 c = context_typeofZero ();
908 c = context_typeofOne ();
912 yylval.expr = exprNode_numLiteral (c, cstring_fromChars (yytext),
913 fileloc_decColumn (g_currentloc, tokLength), i);
918 static int returnChar (char c)
920 yylval.expr = exprNode_charLiteral (c, cstring_fromChars (yytext),
921 fileloc_decColumn (g_currentloc, tokLength));
930 if (c != EOF && ((char)c == '\n'))
932 context_incLineno ();
938 static char macro_nextChar ()
940 static bool in_quote = FALSE, in_escape = FALSE, in_char = FALSE;
945 c = char_fromInt (ic);
947 if (!in_quote && !in_char && (c == '\\' || c == BEFORE_COMMENT_MARKER[0]))
951 while ((c = char_fromInt (lminput ())) != '\0' && c != '\n')
953 ; /* skip to newline */
956 context_incLineno ();
960 return macro_nextChar ();
967 else /* if (c == '@') */
969 llassert (FALSE); /*@i23@*/
970 if (handleLlSpecial () != BADTOK)
972 llerrorlit (FLG_SYNTAX, "Macro cannot use special syntax");
975 return macro_nextChar ();
978 else if (!in_escape && c == '\"')
980 in_quote = !in_quote;
982 else if (!in_escape && c == '\'')
986 else if ((in_quote || in_char) && c == '\\')
988 in_escape = !in_escape;
990 else if ((in_quote || in_char) && in_escape)
994 else if (!in_quote && c == '/')
998 if ((c2 = char_fromInt (lminput ())) == '*')
1002 while ((c2 = char_fromInt (lminput ())) != '\0'
1003 && c2 != '\n' && c2 != '*')
1010 while ((c2 = char_fromInt (lminput ())) != '\0'
1023 llfatalerror (cstring_makeLiteral ("Macro: bad comment!"));
1027 return macro_nextChar ();
1031 /*** putchar does not work! why? puts to stdio...??! ***/
1039 ** keeps semantic comments
1042 static char macro_nextCharC ()
1044 static bool in_quote = FALSE, in_escape = FALSE, in_char = FALSE;
1047 c = char_fromInt (lminput ());
1049 if (!in_quote && !in_char && c == '\\')
1051 while ((c = char_fromInt (lminput ())) != '\0' && c != '\n')
1053 ; /* skip to newline */
1056 context_incLineno ();
1060 return macro_nextCharC ();
1067 else if (!in_escape && c == '\"')
1069 in_quote = !in_quote;
1071 else if (!in_escape && c == '\'')
1075 else if ((in_quote || in_char) && c == '\\')
1077 in_escape = !in_escape;
1079 else if ((in_quote || in_char) && in_escape)
1083 else if (!in_quote && c == '/')
1087 if ((c2 = char_fromInt (lminput ())) == '*')
1091 while ((c2 = char_fromInt (lminput ())) != '\0'
1092 && c2 != '\n' && c2 != '*')
1099 while ((c2 = char_fromInt (lminput ())) != '\0'
1112 llfatalerror (cstring_makeLiteral ("Macro: bad comment!"));
1116 return macro_nextCharC ();
1127 ** skips whitespace (handles line continuations)
1128 ** returns first non-whitespace character
1131 static char skip_whitespace ()
1135 while ((c = macro_nextChar ()) == ' ' || c == '\t')
1143 static void handleMacro ()
1145 cstring mac = cstring_undefined;
1149 while (currentColumn () > 2)
1151 mac = cstring_appendChar (mac, ' ');
1155 c = macro_nextCharC ();
1157 if (c >= '0' && c <= '9')
1161 for (i = 0; i < ((c - '0') + 1); i++)
1163 mac = cstring_appendChar (mac, ' ');
1171 while (((c = macro_nextCharC ()) != '\0') && (c != '\n'))
1173 mac = cstring_appendChar (mac, c);
1177 macrocode = tokenMacroCode (mac);
1179 if (macrocode == BADTOK && !isArtificial (mac))
1181 context_addMacroCache (mac);
1190 context_incLineno ();
1194 static bool processMacro (void)
1199 cstring fname = cstring_undefined;
1201 bool isspecfcn = FALSE;
1202 bool isiter = FALSE;
1203 bool skipparam = FALSE;
1204 bool isenditer = FALSE;
1205 bool unknownm = FALSE;
1206 bool hasParams = FALSE;
1207 bool emptyMacro = FALSE;
1208 char c = skip_whitespace ();
1209 fileloc loc = fileloc_noColumn (g_currentloc);
1211 /* are both of these necessary? what do they mean? */
1212 uentryList specparams = uentryList_undefined;
1213 uentryList pn = uentryList_undefined;
1215 context_resetMacroMissingParams ();
1217 if (c == '\0' || c == '\n')
1219 llcontbug (cstring_makeLiteral ("Bad macro"));
1224 fname = cstring_appendChar (fname, c);
1226 while ((c = macro_nextChar ()) != '(' && c != '\0'
1227 && c != ' ' && c != '\t' && c != '\n')
1229 fname = cstring_appendChar (fname, c);
1232 if (c == ' ' || c == '\t' || c == '\n')
1238 while (c == ' ' || c == '\t')
1240 c = macro_nextChar ();
1254 hasParams = (c == '(');
1256 if (usymtab_exists (fname))
1258 e2 = usymtab_lookupExpose (fname);
1259 ct = uentry_getType (e2);
1261 if (uentry_isCodeDefined (e2)
1262 && fileloc_isUser (uentry_whereDefined (e2)))
1266 message ("Macro %s already defined", fname),
1269 uentry_showWhereDefined (e2);
1270 uentry_clearDefined (e2);
1273 if (uentry_isFunction (e2))
1275 uentry_setType (e2, ctype_unknown);
1278 context_enterUnknownMacro (e2);
1282 context_enterConstantMacro (e2);
1287 if (uentry_isForward (e2) && uentry_isFunction (e2))
1294 ("Parameterized macro has no prototype or specification: %s ",
1299 uentry_setType (e2, ctype_unknown);
1300 uentry_setFunctionDefined (e2, loc);
1301 uentry_setUsed (e2, fileloc_undefined);
1302 context_enterUnknownMacro (e2);
1306 if (uentry_isIter (e2))
1309 specparams = uentry_getParams (e2);
1310 noparams = uentryList_size (specparams);
1311 uentry_setDefined (e2, loc);
1312 context_enterIterDef (e2);
1314 else if (uentry_isEndIter (e2))
1317 uentry_setDefined (e2, loc);
1318 context_enterIterEnd (e2); /* don't care about it now */
1319 /* but should parse like an iter! */
1321 else if (uentry_isConstant (e2))
1327 message ("Constant %s implemented as parameterized macro",
1331 uentry_showWhereSpecified (e2);
1332 uentry_setType (e2, ctype_unknown);
1333 uentry_makeConstantFunction (e2);
1334 uentry_setDefined (e2, g_currentloc);
1335 uentry_setFunctionDefined (e2, g_currentloc);
1336 context_enterUnknownMacro (e2);
1340 if (!uentry_isSpecified (e2))
1342 fileloc oloc = uentry_whereDeclared (e2);
1344 if (fileloc_isLib (oloc))
1348 else if (fileloc_isUndefined (oloc)
1349 || fileloc_isPreproc (oloc))
1354 (FLG_MACROCONSTDECL,
1356 ("Macro constant %q not declared",
1357 uentry_getName (e2)),
1361 else if (!fileloc_withinLines (oloc, loc, 2))
1362 { /* bogus! will give errors if there is too much whitespace */
1366 ("Macro constant name %s does not match name in "
1367 "previous constant declaration. This constant "
1368 "is declared at %q", fname,
1369 fileloc_unparse (oloc)),
1374 context_enterConstantMacro (e2);
1375 cstring_free (fname);
1381 else if (ctype_isFunction (ct))
1384 specparams = ctype_argsFunction (ct);
1385 noparams = uentryList_size (specparams);
1387 uentry_setFunctionDefined (e2, loc);
1388 context_enterMacro (e2);
1390 else if (uentry_isVar (e2))
1396 message ("Variable %s implemented as parameterized macro",
1400 uentry_showWhereSpecified (e2);
1401 uentry_setType (e2, ctype_unknown);
1402 uentry_makeVarFunction (e2);
1403 uentry_setDefined (e2, g_currentloc);
1404 uentry_setFunctionDefined (e2, g_currentloc);
1405 context_enterUnknownMacro (e2);
1409 uentry ucons = uentry_makeConstant (fname,
1412 if (uentry_isExpandedMacro (e2))
1420 message ("Variable %s implemented by a macro",
1424 uentry_showWhereSpecified (e2);
1428 uentry_setDefined (e2, loc);
1429 uentry_setUsed (ucons, loc);
1431 context_enterConstantMacro (ucons);
1432 uentry_markOwned (ucons);
1433 cstring_free (fname);
1439 if (uentry_isDatatype (e2))
1443 message ("Type implemented as macro: %x",
1444 uentry_getName (e2)),
1445 message ("A type is implemented using a macro definition. A "
1446 "typedef should be used instead."),
1450 /* Must exit scope (not sure why a new scope was entered?) */
1451 usymtab_quietExitScope (g_currentloc);
1452 uentry_setDefined (e2, g_currentloc);
1458 (message ("Unexpanded macro not function or constant: %q",
1459 uentry_unparse (e2)));
1460 uentry_setType (e2, ctype_unknown);
1464 uentry_makeVarFunction (e2);
1465 uentry_setDefined (e2, g_currentloc);
1466 uentry_setFunctionDefined (e2, g_currentloc);
1467 context_enterUnknownMacro (e2);
1478 /* evans 2001-09-09 - if it has params, assume a function */
1482 (FLG_MACROMATCHNAME,
1483 message ("Unexpanded macro %s does not match name of a declared "
1484 "function. The name used in the control "
1485 "comment on the previous line should match.",
1489 ce = uentry_makeFunction (fname, ctype_unknown,
1493 warnClause_undefined,
1495 uentry_setUsed (ce, loc); /* perhaps bogus? */
1496 e2 = usymtab_supEntryReturn (ce);
1497 context_enterUnknownMacro (e2);
1502 (FLG_MACROMATCHNAME,
1503 message ("Unexpanded macro %s does not match name of a constant "
1504 "or iter declaration. The name used in the control "
1505 "comment on the previous line should match. "
1506 "(Assuming macro defines a constant.)",
1510 ce = uentry_makeConstant (fname, ctype_unknown, fileloc_undefined);
1511 uentry_setUsed (ce, loc); /* perhaps bogus? */
1512 e2 = usymtab_supEntryReturn (ce);
1514 context_enterConstantMacro (e2);
1515 cstring_free (fname);
1521 /* in macros, ( must follow immediatetly after name */
1527 c = skip_whitespace ();
1529 while (c != ')' && c != '\0')
1532 bool suppress = context_inSuppressRegion ();
1533 cstring paramname = cstring_undefined;
1536 ** save the parameter location
1540 context_saveLocation ();
1543 while (c != ' ' && c != '\t' && c != ',' && c != '\0' && c != ')')
1545 paramname = cstring_appendChar (paramname, c);
1546 c = macro_nextChar ();
1549 if (c == ' ' || c == '\t') c = skip_whitespace ();
1553 c = macro_nextChar ();
1554 if (c == ' ' || c == '\t') c = skip_whitespace ();
1559 llfatalerror (cstring_makeLiteral
1560 ("Bad macro syntax: uentryList"));
1563 if ((isspecfcn || isiter) && (paramno < noparams)
1564 && !uentry_isElipsisMarker (uentryList_getN
1565 (specparams, paramno)))
1567 fileloc sloc = context_getSaveLocation ();
1568 uentry decl = uentryList_getN (specparams, paramno);
1571 param = uentry_nameCopy (paramname, decl);
1573 uentry_setParam (param);
1574 sr = sRef_makeParam (paramno, uentry_getType (param), stateInfo_makeLoc (sloc));
1576 if (sRef_getNullState (sr) == NS_ABSNULL)
1578 ctype pt = ctype_realType (uentry_getType (param));
1580 if (ctype_isUser (pt))
1582 uentry te = usymtab_getTypeEntrySafe (ctype_typeId (pt));
1584 if (uentry_isValid (te))
1586 sRef_setStateFromUentry (sr, te);
1591 sRef_setNullState (sr, NS_UNKNOWN, sloc);
1595 uentry_setSref (param, sr);
1596 uentry_setDeclaredForceOnly (param, sloc);
1598 skipparam = isiter && uentry_isOut (uentryList_getN (specparams, paramno));
1602 fileloc sloc = context_getSaveLocation ();
1604 param = uentry_makeVariableSrefParam
1605 (paramname, ctype_unknown, fileloc_copy (sloc),
1606 sRef_makeParam (paramno, ctype_unknown, stateInfo_makeLoc (sloc)));
1607 DPRINTF (("Unknown param: %s", uentry_unparseFull (param)));
1608 cstring_free (paramname);
1610 sRef_setPosNull (uentry_getSref (param), sloc);
1611 uentry_setDeclaredForce (param, sloc);
1614 fileloc_free (sloc);
1619 llassert (!uentry_isElipsisMarker (param));
1623 sRef_makeUnsafe (uentry_getSref (param));
1626 pn = uentryList_add (pn, uentry_copy (param));
1627 usymtab_supEntry (param);
1631 /* don't add param */
1632 uentry_free (param);
1637 (void) macro_nextChar ();
1638 c = skip_whitespace ();
1646 if (isspecfcn || isiter)
1648 if (paramno != noparams && noparams >= 0)
1654 message ("Macro %s specified with %d args, defined with %d",
1655 fname, noparams, paramno),
1658 uentry_showWhereSpecified (e2);
1659 uentry_resetParams (e2, pn);
1664 uentry_resetParams (e2, pn);
1671 ** the form should be:
1673 ** # define newname oldname
1674 ** where oldname refers to a function matching the specification
1680 sRef_setGlobalScope ();
1681 usymtab_supGlobalEntry (uentry_makeVariableLoc (fname, ctype_unknown));
1682 sRef_clearGlobalScope ();
1686 context_setMacroMissingParams ();
1691 /* context_setuentryList (pn); */
1692 usymtab_enterScope ();
1695 cstring_free (fname);
1700 static bool handleSpecial (char *yyt)
1702 char *l = mstring_create (MAX_NAME_LENGTH);
1708 strcpy (l, yyt + 1);
1710 /* Need to safe original l for deallocating. */
1713 l += strlen (yyt) - 1;
1715 while ((c = char_fromInt (lminput ())) != '\n' && c != '\0')
1721 olc = cstring_fromChars (ol);
1723 if (cstring_equalPrefixLit (olc, "pragma"))
1725 char *pname = mstring_create (longUnsigned_fromInt (MAX_PRAGMA_LEN));
1726 char *opname = pname;
1727 char *ptr = ol + 6; /* pragma is six characters, plus space */
1731 /* skip whitespace */
1732 while (((c = *ptr) != '\0') && isspace (c))
1738 while (((c = *ptr) != '\0') && !isspace (c))
1742 if (len > MAX_PRAGMA_LEN)
1753 if (len == PRAGMA_LEN_EXPAND
1754 && mstring_equal (opname, PRAGMA_EXPAND))
1756 cstring exname = cstring_undefined;
1760 while (((c = *ptr) != '\0') && !isspace (c))
1762 exname = cstring_appendChar (exname, c);
1767 ue = usymtab_lookupExposeGlob (exname);
1769 if (uentry_isExpandedMacro (ue))
1771 if (fileloc_isPreproc (uentry_whereDefined (ue)))
1773 fileloc_setColumn (g_currentloc, 1);
1774 uentry_setDefined (ue, g_currentloc);
1778 cstring_free (exname);
1781 else if (cstring_equalPrefixLit (olc, "ident"))
1783 /* Some pre-processors will leave these in the code. Ignore rest of line */
1786 ** Yuk...Win32 filenames can have spaces in them...we need to read
1787 ** to the matching end quote.
1789 else if ((sscanf (ol, "line %d \"", &lineno) == 1)
1790 || (sscanf (ol, " %d \"", &lineno) == 1))
1796 /*@access cstring@*/
1797 while (*tmp != '\"' && *tmp != '\0')
1802 llassert (*tmp == '\"');
1808 while (*tmp != '\"' && *tmp != '\0')
1813 llassert (*tmp == '\"');
1817 # if defined(OS2) || defined(MSDOS) || defined(WIN32)
1820 ** DOS-like path delimiters get delivered in pairs, something like
1821 ** \"..\\\\file.h\", so we have to make it normal again. We do NOT
1822 ** remove the pre dirs yet as we usually specify tmp paths relative
1823 ** to the current directory, so tmp files would not get found in
1824 ** the hash table. If this method fails we try it again later.
1831 ** Skip past the drive marker.
1834 if (strchr (stmp, ':') != NULL)
1836 stmp = strchr (stmp, ':') + 1;
1839 while ((stmp = strchr (stmp, CONNECTCHAR)) != NULL )
1841 if (*(stmp+1) == CONNECTCHAR)
1843 memmove (stmp, stmp+1, strlen (stmp));
1849 fid = fileTable_lookupBase (context_fileTable (), fname);
1850 if (!(fileId_isValid (fid)))
1852 fname = removePreDirs (fname);
1853 fid = fileTable_lookupBase (context_fileTable (), fname);
1856 # else /* !defined(OS2) && !defined(MSDOS) */
1857 fname = removePreDirs (fname);
1858 fid = fileTable_lookupBase (context_fileTable (), fname);
1859 # endif /* !defined(OS2) && !defined(MSDOS) */
1861 if (!(fileId_isValid (fid)))
1863 if (context_inXHFile ())
1865 fid = fileTable_addXHFile (context_fileTable (), fname);
1867 else if (isHeaderFile (fname))
1869 fid = fileTable_addHeaderFile (context_fileTable (), fname);
1873 fid = fileTable_addFile (context_fileTable (), fname);
1877 setFileLine (fid, lineno);
1878 /*@noaccess cstring@*/
1880 else if ((sscanf (ol, "line %d", &lineno) == 1)
1881 || (sscanf (ol, " %d", &lineno) == 1))
1883 setLine (lineno); /* next line is <cr> */
1887 if (mstring_equal (ol, "")) {
1888 DPRINTF (("Empty pp command!"));
1890 ** evs 2000-05-16: This is a horrible kludge, to get around a bug (well, difficulty) in the pre-processor.
1891 ** We handle a plain # in the input file, by echoing it, and ignoring it in the post-pp-file.
1897 (FLG_UNRECOGDIRECTIVE,
1898 message ("Unrecognized pre-processor directive: #%s",
1899 cstring_fromChars (ol)),
1904 return FALSE; /* evans 2001-12-30: was: TRUE; */
1911 static int handleLlSpecial ()
1916 char *s = mstring_createEmpty ();
1922 loc = fileloc_copy (g_currentloc);
1923 DPRINTF (("Handle special: %s", fileloc_unparse (loc)));
1925 while (((ic = ninput ()) != 0) && isalpha (ic))
1928 s = mstring_append (s, c);
1932 DPRINTF (("Read: %s / %s", s, fileloc_unparse (g_currentloc)));
1935 if (charsread == 0 && ic == (int) AFTER_COMMENT_MARKER[0])
1939 llassert (ic == AFTER_COMMENT_MARKER[1]);
1945 return QNOMODS; /* special token no modifications token */
1949 DPRINTF (("Coment marker: %s", os));
1950 tok = commentMarkerToken (cstring_fromChars (os));
1954 tokLength = charsread;
1961 DPRINTF (("Not a comment marker..."));
1962 /* Add rest of the comment */
1964 if (ic != 0 && ic != EOF)
1968 s = mstring_append (s, c);
1971 while (((ic = ninput ()) != 0) && (ic != EOF)
1972 && (ic != AFTER_COMMENT_MARKER[0]))
1976 /* evans 2001-09-01 added to prevent assertion failures for uncloses syntactic comments */
1979 hasnl = TRUE; /* This prevents tokLength from being set later. */
1984 message ("Likely parse error: syntactic comment token spans multiple lines: %s",
1985 cstring_fromChars (s)),
1989 s = mstring_append (s, c);
1994 DPRINTF (("Read: %s / %s", s, fileloc_unparse (g_currentloc)));
1996 if (ic == AFTER_COMMENT_MARKER[0])
1999 llassert ((char) nc == AFTER_COMMENT_MARKER[1]);
2005 while (*s == ' ' || *s == '\t' || *s == '\n')
2010 if (*s == '-' || *s == '+' || *s == '=') /* setting flags */
2014 while (c == '-' || c == '+' || c == '=')
2016 ynm set = ynm_fromCodeChar (c);
2021 thisflag = cstring_fromChars (s);
2023 while ((c = *s) != '\0' && (c != '-') && (c != '=')
2024 && (c != '+') && (c != ' ') && (c != '\t') && (c != '\n'))
2031 if (!context_getFlag (FLG_NOCOMMENTS))
2033 cstring flagname = thisflag;
2034 flagcode fflag = flags_identifyFlag (flagname);
2036 if (flagcode_isSkip (fflag))
2040 else if (flagcode_isInvalid (fflag))
2042 if (isMode (flagname))
2044 if (ynm_isMaybe (set))
2049 ("Semantic comment attempts to restore flag %s. "
2050 "A mode flag cannot be restored.",
2055 context_setMode (flagname);
2061 (FLG_UNRECOGFLAGCOMMENTS,
2062 message ("Unrecognized option in semantic comment: %s",
2067 else if (flagcode_isGlobalFlag (fflag))
2072 ("Semantic comment attempts to set global flag %s. "
2073 "A global flag cannot be set locally.",
2079 context_fileSetFlag (fflag, set);
2081 if (flagcode_hasArgument (fflag))
2083 if (ynm_isMaybe (set))
2088 ("Semantic comment attempts to restore flag %s. "
2089 "A flag for setting a value cannot be restored.",
2094 { /* cut-and-pastied from llmain...blecch */
2095 cstring extra = cstring_undefined;
2101 rest = mstring_copy (s);
2105 while ((rchar = *rest) != '\0'
2106 && (isspace (rchar)))
2112 while ((rchar = *rest) != '\0'
2113 && !isspace (rchar))
2115 extra = cstring_appendChar (extra, rchar);
2122 if (cstring_isUndefined (extra))
2127 ("Flag %s (in semantic comment) must be followed by an argument",
2128 flagcode_unparse (fflag)));
2134 if (flagcode_hasNumber (fflag))
2136 setValueFlag (fflag, extra);
2138 else if (flagcode_hasChar (fflag))
2140 setValueFlag (fflag, extra);
2142 else if (flagcode_hasString (fflag))
2144 setStringFlag (fflag, extra);
2161 while ((c == ' ') || (c == '\t') || (c == '\n'))
2167 if (context_inHeader () && !isArtificial (cstring_fromChars (os)))
2169 DPRINTF (("Here adding comment: %s", os));
2170 context_addComment (cstring_fromCharsNew (os));
2182 annotationInfo ainfo;
2184 while (*s != '\0' && *s != ' ' && *s != '\t' && *s != '\n')
2196 t = cstring_toCharsSafe (cstring_downcase (cstring_fromChars (t)));
2197 macrocode = tokenMacroCode (cstring_fromChars (t));
2199 if (macrocode != BADTOK)
2201 tokLength = hasnl ? 0 : mstring_length (t);
2207 if (macrocode == SKIPTOK)
2215 ainfo = context_lookupAnnotation (cstring_fromChars (os));
2217 if (annotationInfo_isDefined (ainfo)) {
2218 DPRINTF (("Found annotation: %s", annotationInfo_unparse (ainfo)));
2219 /*@i324@*/ yylval.annotation = ainfo;
2227 if (context_inHeader ())
2234 if ((context_inMacro () || context_inGlobalContext ())
2235 && macrocode != SKIPTOK
2236 && !isArtificial (cstring_fromChars (os)))
2238 DPRINTF (("Add comment: %s", os));
2239 context_addComment (cstring_fromCharsNew (os));
2252 if (mstring_equal (t, "ignore"))
2254 if (!context_getFlag (FLG_NOCOMMENTS))
2256 context_enterSuppressRegion ();
2259 else if ((*t == 'i' || *t == 't')
2260 && (*(t + 1) == '\0'))
2262 if (!context_getFlag (FLG_NOCOMMENTS)
2263 && (*t == 'i' || context_getFlag (FLG_TMPCOMMENTS)))
2265 context_enterSuppressLine (-1); /* infinite suppression */
2268 else if (((*t == 'i') || (*t == 't'))
2269 && ((*(t + 1) >= '0' && *(t + 1) <= '9')))
2271 bool tmpcomment = (*t == 't');
2273 char *tt = t; /* don't mangle t, since it is free'd */
2276 if (lc >= '0' && lc <= '9')
2278 val = (int)(lc - '0');
2281 while (lc >= '0' && lc <= '9')
2290 if (!context_getFlag (FLG_NOCOMMENTS)
2291 && (!tmpcomment || context_getFlag (FLG_TMPCOMMENTS)))
2293 context_enterSuppressLine (val);
2296 else if (mstring_equal (t, "end"))
2298 if (!context_getFlag (FLG_NOCOMMENTS))
2300 context_exitSuppressRegion ();
2303 else if (mstring_equal (t, "notfunction"))
2305 ; /* handled by pcpp */
2307 else if (mstring_equal (t, "access"))
2313 while ((c = *s) && (c == ' ' || c == '\t' || c == '\n'))
2323 tname = cstring_fromChars (s);
2325 while ((c = *s) != '\0' && c != ' '
2326 && c != '\t' && c != '\n' && c != ',')
2333 DPRINTF (("Access %s", tname));
2335 if (!context_getFlag (FLG_NOCOMMENTS)
2336 && !context_getFlag (FLG_NOACCESS))
2338 if (usymtab_existsType (tname))
2340 typeId uid = usymtab_getTypeId (tname);
2341 uentry ue = usymtab_getTypeEntry (uid);
2343 if (uentry_isAbstractDatatype (ue))
2345 context_addFileAccessType (uid);
2346 DPRINTF (("Adding access to: %s / %d", tname, uid));
2353 ("Non-abstract type %s used in access comment",
2360 if (!(context_inSuppressRegion ()
2361 || context_inSuppressZone (g_currentloc)))
2366 ("Unrecognized type %s used in access comment",
2378 if (c != ',' && c != ' ')
2384 else if (mstring_equal (t, "noaccess"))
2391 while ((lc = *s) && (lc == ' ' || lc == '\t' || lc == '\n'))
2401 tname = cstring_fromChars (s);
2403 while ((lc = *s) != '\0' && lc != ' ' && lc != '\t'
2404 && lc != '\n' && lc != ',')
2411 if (!context_getFlag (FLG_NOCOMMENTS)
2412 && !context_getFlag (FLG_NOACCESS))
2414 if (usymtab_existsType (tname))
2416 typeId tuid = usymtab_getTypeId (tname);
2418 if (context_couldHaveAccess (tuid))
2420 DPRINTF (("Removing access: %s", tname));
2421 context_removeFileAccessType (tuid);
2425 if (!(context_inSuppressRegion ()
2426 || context_inSuppressZone (g_currentloc)))
2428 uentry ue = usymtab_getTypeEntry (tuid);
2430 if (uentry_isAbstractDatatype (ue))
2435 ("Non-accessible abstract type %s used in noaccess comment",
2444 ("Non-abstract type %s used in noaccess comment",
2453 if (!(context_inSuppressRegion ()
2454 || context_inSuppressZone (g_currentloc)))
2459 ("Unrecognized type %s used in noaccess comment",
2471 if (lc != ',' && lc != ' ')
2479 voptgenerror (FLG_UNRECOGCOMMENTS,
2480 message ("Semantic comment unrecognized: %s",
2481 cstring_fromChars (os)), loc);
2492 static /*@only@*/ cstring makeIdentifier (char *s)
2494 char *c = mstring_create (size_toInt (strlen (s)) + 1);
2495 cstring id = cstring_fromChars (c);
2497 while (isalnum (*s) || (*s == '_') || (*s == '$'))
2506 /*@observer@*/ /*@dependent@*/ uentry coerceId (cstring cn)
2508 if (!(usymtab_exists (cn)))
2510 fileloc loc = fileloc_createExternal ();
2513 ** We need to put this in a global scope, otherwise the sRef will be deallocated.
2516 uentry ce = uentry_makeUnrecognized (cn, loc);
2518 if (!context_inIterEnd ())
2522 message ("Unrecognized (possibly system) identifier: %q",
2523 uentry_getName (ce)),
2530 return (usymtab_lookup (cn));
2534 ** like, coerceId, but doesn't supercede for iters
2537 /*@observer@*/ uentry coerceIterId (cstring cn)
2539 if (!(usymtab_exists (cn)))
2541 return uentry_undefined;
2544 return (usymtab_lookup (cn));
2547 /*@observer@*/ cstring LastIdentifier ()
2549 return (lastidprocessed);
2552 static int processIdentifier (cstring id)
2556 if (context_getFlag (FLG_GRAMMAR))
2558 lldiagmsg (message ("Process identifier: %s", id));
2561 context_clearJustPopped ();
2562 lastidprocessed = id;
2564 if (context_inFunctionHeader ())
2566 int tok = commentMarkerToken (id);
2567 DPRINTF (("in function decl..."));
2575 tok = tokenMacroCode (id);
2583 annotationInfo ainfo;
2585 if (expectingMetaStateName)
2587 metaStateInfo msinfo = context_lookupMetaStateInfo (id);
2589 if (metaStateInfo_isDefined (msinfo))
2591 yylval.msinfo = msinfo;
2592 return METASTATE_NAME;
2596 DPRINTF (("Not meta state name: %s", cstring_toCharsSafe (id)));
2600 ainfo = context_lookupAnnotation (id);
2602 if (annotationInfo_isDefined (ainfo))
2604 DPRINTF (("Found annotation: %s", annotationInfo_unparse (ainfo)));
2605 /*@i324@*/ yylval.annotation = ainfo;
2610 DPRINTF (("Not annotation: %s", id));
2616 /* Consider handling: Defined by C99 as static const char __func__[] */
2618 if (context_getFlag (FLG_GNUEXTENSIONS))
2622 if (cstring_equalLit (id, "__stdcall")
2623 || cstring_equalLit (id, "__cdecl")
2624 || cstring_equalLit (id, "__extension__"))
2628 else if (cstring_equalLit (id, "__volatile__"))
2632 else if (cstring_equalLit (id, "__signed"))
2636 else if (cstring_equalLit (id, "__unsigned"))
2640 else if (cstring_equalLit (id, "__const__"))
2644 else if (cstring_equalLit (id, "__alignof__"))
2646 tok = CALIGNOF; /* alignof is parsed like sizeof */
2648 else if (cstring_equalLit (id, "__FUNCTION__")
2649 || cstring_equalLit (id, "__PRETTY_FUNCTION__"))
2651 /* These tokens hold the name of the current function as strings */
2652 /* evans 2001-12-30: changed from exprNode_stringLiteral; bug reported by Jim Zelenka. */
2653 yylval.expr = exprNode_makeConstantString (id, fileloc_copy (g_currentloc));
2655 lastWasString = TRUE;
2659 else if (cstring_equalLit (id, "__attribute__")
2660 || cstring_equalLit (id, "__asm__")
2661 || cstring_equalLit (id, "_asm")
2662 || cstring_equalLit (id, "__asm")
2663 || cstring_equalLit (id, "__declspec"))
2666 bool useparens = FALSE;
2667 bool usebraces = FALSE;
2668 bool inquote = FALSE;
2669 bool inescape = FALSE;
2672 while ((ic = input ()) != EOF)
2679 else if (ic == '\\')
2683 else if (ic == '\"')
2719 else if (ic == ')' && useparens)
2722 if (depth == 0) break;
2724 else if (ic == '}' && usebraces)
2727 if (depth == 0) break;
2730 && !usebraces && !useparens
2731 && cstring_equalLit (id, "__asm"))
2734 ** We need this because some MS VC++ include files
2735 ** have __asm mov ... }
2736 ** Its a kludge, but otherwise would need to parse
2745 context_incLineno ();
2747 if (cstring_equalLit (id, "__asm")
2748 && !useparens && !usebraces)
2755 llassert ((useparens && ic == ')')
2756 || (usebraces && ic == '}')
2757 || (!useparens && !usebraces));
2761 else if (cstring_equalLit (id, "inline")
2762 || cstring_equalLit (id, "__inline")
2763 || cstring_equalLit (id, "_inline")
2764 || cstring_equalLit (id, "__inline__"))
2775 le = usymtab_lookupSafe (id);
2777 /*@-dependenttrans@*/
2779 if (uentry_isIter (le))
2781 /*@i32@*/ yylval.entry = le;
2784 else if (uentry_isEndIter (le))
2786 /*@i32@*/ yylval.entry = le;
2787 return (ITER_ENDNAME);
2789 else if (uentry_isUndefined (le))
2793 /* avoid parse errors for certain system built ins */
2795 if (g_expectingTypeName && (cstring_firstChar (id) == '_')
2796 && (cstring_secondChar (id) == '_'))
2798 return (TYPE_NAME_OR_ID);
2801 return (NEW_IDENTIFIER);
2803 else if (!uentry_isDeclared (le) && !uentry_isCodeDefined (le))
2805 if (uentry_isDatatype (le))
2808 return (NEW_IDENTIFIER);
2812 /*@i32@*/ yylval.entry = le;
2813 return (IDENTIFIER);
2816 else if (uentry_isDatatype (le))
2818 if (!g_expectingTypeName)
2822 return (NEW_IDENTIFIER);
2826 yylval.ctyp = uentry_getAbstractType (le);
2828 uentry_setUsed (le, g_currentloc);
2834 /*@i32@*/ yylval.entry = le;
2835 return (IDENTIFIER);
2838 /*@=dependenttrans@*/
2841 static bool processHashIdentifier (/*@only@*/ cstring id)
2843 if (context_inMacro () || context_inIterDef () ||
2844 context_inIterEnd ())
2848 context_clearJustPopped ();
2850 lastidprocessed = id;
2851 le = usymtab_lookupSafe (id);
2853 if (uentry_isParam (le) || uentry_isRefParam (le))
2865 ** Will be handled by handleLlSpecial
2874 static /*@only@*/ exprNode processString ()
2878 char *nl = strchr (yytext, '\n');
2879 cstring ns = cstring_fromCharsNew (yytext);
2883 loc = fileloc_copy (g_currentloc);
2884 addColumn (cstring_length (ns));
2890 loc = fileloc_copy (g_currentloc);
2892 context_incLineno ();
2894 while ((nl = strchr ((nl + 1), '\n')) != NULL)
2896 context_incLineno ();
2902 res = exprNode_stringLiteral (ns, loc);
2907 ** process a wide character string L"...."
2910 static /*@only@*/ exprNode processWideString ()
2914 char *nl = strchr (yytext, '\n');
2917 llassert (*yytext == 'L');
2920 ns = cstring_fromCharsNew (yytext);
2924 loc = fileloc_copy (g_currentloc);
2925 addColumn (cstring_length (ns));
2931 loc = fileloc_copy (g_currentloc);
2933 context_incLineno ();
2935 while ((nl = strchr ((nl + 1), '\n')) != NULL)
2937 context_incLineno ();
2942 res = exprNode_wideStringLiteral (ns, loc);
2952 llassert (*yytext != '\0');
2953 fchar = *(yytext + 1);
2954 if (fchar != '\\') return fchar;
2956 next = *(yytext + 2);
2960 case 'n': return '\n';
2961 case 't': return '\t';
2962 case '\"': return '\"';
2963 case '\'': return '\'';
2964 case '\\': return '\\';
2965 default: return '\0';
2970 double processFloat ()
2972 double ret = atof (yytext);
2983 llassert (yytext[0] == '0'
2984 && (yytext[1] == 'X' || yytext[1] == 'x'));
2986 while (yytext[index] != '\0') {
2988 char c = yytext[index];
2990 if (c >= '0' && c <= '9') {
2991 tval = (int) c - (int) '0';
2992 } else if (c >= 'A' && c <= 'F') {
2993 tval = (int) c - (int) 'A' + 10;
2994 } else if (c >= 'a' && c <= 'f') {
2995 tval = (int) c - (int) 'a' + 10;
2996 } else if (c == 'U' || c == 'L' || c == 'u' || c == 'l') {
2998 while (yytext[index] != '\0') {
2999 if (c == 'U' || c == 'L' || c == 'u' || c == 'l') {
3004 message ("Invalid character (%c) following specifier in hex constant: %s",
3005 c, cstring_fromChars (yytext)),
3015 message ("Invalid character (%c) in hex constant: %s",
3016 c, cstring_fromChars (yytext)),
3021 val = (val * 16) + tval;
3025 DPRINTF (("Hex constant: %s = %ld", yytext, val));
3030 long processOctal ()
3035 llassert (yytext[0] == '0' && yytext[1] != 'X' && yytext[1] != 'x');
3037 while (yytext[index] != '\0') {
3039 char c = yytext[index];
3041 if (c >= '0' && c <= '7') {
3042 tval = (int) c - (int) '0';
3043 } else if (c == 'U' || c == 'L' || c == 'u' || c == 'l') {
3045 while (yytext[index] != '\0') {
3046 if (c == 'U' || c == 'L' || c == 'u' || c == 'l') {
3051 message ("Invalid character (%c) following specifier in octal constant: %s",
3052 c, cstring_fromChars (yytext)),
3062 message ("Invalid character (%c) in octal constant: %s",
3063 c, cstring_fromChars (yytext)),
3068 val = (val * 8) + tval;
3072 DPRINTF (("Octal constant: %s = %ld", yytext, val));
3079 return (atol (yytext));
3083 processSpec (int tok)
3085 size_t length = strlen (yytext);
3089 setTokLengthT (length);
3095 context_saveLocation ();
3096 setTokLengthT (length);
3097 return (processIdentifier (makeIdentifier (yytext)));
3101 void cscanner_expectingMetaStateName ()
3103 llassert (!expectingMetaStateName);
3104 llassert (context_inFunctionHeader ());
3105 expectingMetaStateName = TRUE;
3108 void cscanner_clearExpectingMetaStateName ()
3110 llassert (expectingMetaStateName);
3111 expectingMetaStateName = FALSE;