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"
88 # include "fileIdList.h"
91 static bool lastWasString = FALSE;
92 static char savechar = '\0';
95 # define yyinput() (incColumn (), getc (yyin))
97 static /*@owned@*/ cstring lastidprocessed = cstring_undefined;
98 static int lminput (void);
99 static int tokLength = 0;
100 static bool inSpecPart = FALSE;
101 static bool continueLine = FALSE;
103 static int ninput (void);
104 static char processChar (void);
105 static double processFloat (void);
106 static /*@only@*/ exprNode processString (void) ;
107 static /*@only@*/ exprNode processWideString (void) ;
108 static long processDec (void);
109 static long processHex (void);
110 static long processOctal (void);
111 static int processIdentifier (/*@only@*/ cstring)
112 /*@globals undef lastidprocessed@*/ ;
113 static bool processHashIdentifier (/*@only@*/ cstring)
114 /*@globals undef lastidprocessed@*/ ;
116 static int processSpec (int);
117 static bool handleSpecial (char *);
118 static int handleLlSpecial (void);
119 static void handleMacro (void);
120 static bool processMacro (void);
121 static /*@only@*/ cstring makeIdentifier (char *);
123 /* yes, this is exported! */
124 bool g_expectingTypeName = TRUE; /* beginning of file can be type name! */
126 static bool expectingMetaStateName = FALSE;
128 static int returnInt (ctype, long);
129 static int returnFloat (ctype, double);
130 static int returnChar (char);
131 static void setTokLength (int) /*@modifies g_currentloc@*/ ;
132 static void setTokLengthT (size_t) /*@modifies g_currentloc@*/ ;
134 static void advanceLine (void)
141 # define RETURN_INT(c,i) \
142 do { lastWasString = FALSE; \
143 return (returnInt (c, i)); } while (FALSE)
145 # define RETURN_FLOAT(c,f) \
146 do { lastWasString = FALSE; \
147 return (returnFloat (c, f)); \
150 # define RETURN_CHAR(c) \
151 do { lastWasString = FALSE; \
152 return (returnChar (c)); \
155 # define RETURN_TOK(t) \
156 do { yylval.tok = lltok_create (t, fileloc_decColumn (g_currentloc, tokLength)); \
158 lastWasString = FALSE; \
159 return (t); } while (FALSE)
161 # define RETURN_TYPE(t, ct) \
162 do { yylval.ctyp = ct; tokLength = 0; return (t); } while (FALSE)
164 /* don't fileloc_decColumn (g_currentloc, tokLength));
165 the string could have \n's in it!
168 # define RETURN_STRING(c) \
169 do { yylval.expr = exprNode_stringLiteral (c, fileloc_decColumn (g_currentloc, tokLength)); \
171 lastWasString = TRUE; \
172 return (CCONSTANT); } while (FALSE)
174 # define RETURN_EXPR(e) \
175 do { yylval.expr = e; \
177 lastWasString = TRUE; \
178 return (CCONSTANT); } while (FALSE)
182 static void setTokLength (int len)
186 DPRINTF (("Set tok length: %d", len));
189 static void setTokLengthT (size_t len)
191 setTokLength (size_toInt (len));
194 # include "flex.head"
196 /*@-unrecog@*/ /*@i5343@*/
202 "/*" { llfatalbug (cstring_makeLiteral ("Comment in pre-processor output")); }
204 "#"{Letter}({Letter}|{Digit})* {
205 context_saveLocation ();
206 setTokLength (longUnsigned_toInt (mstring_length (yytext)));
208 if (processHashIdentifier (makeIdentifier (yytext + 1)))
212 /* was nothing! */ /*@i32@*/
213 RETURN_STRING (cstring_makeLiteral ("\"\""));
217 RETURN_STRING (cstring_makeLiteral ("\"\""));
222 if (handleSpecial (yytext))
229 "#" { if (handleSpecial (yytext))
231 setTokLength (1); RETURN_TOK (0);
234 "..." { setTokLength (3); RETURN_TOK (CTOK_ELIPSIS); }
235 "break" { setTokLength (5); RETURN_TOK (BREAK); }
236 "case" { setTokLength (4); RETURN_TOK (CASE); }
237 "continue" { setTokLength (8); RETURN_TOK (CONTINUE); }
238 "default" { setTokLength (7); RETURN_TOK (DEFAULT); }
239 "do" { setTokLength (2); RETURN_TOK (DO); }
240 "else" { setTokLength (4); RETURN_TOK (CELSE); }
241 "for" { setTokLength (3); RETURN_TOK (CFOR); }
242 "goto" { setTokLength (4); RETURN_TOK (GOTO); }
243 "if" { setTokLength (2); RETURN_TOK (CIF); }
244 "return" { setTokLength (6); RETURN_TOK (RETURN); }
245 "sizeof" { setTokLength (6); RETURN_TOK (CSIZEOF); }
246 "offsetof" { setTokLength (8); RETURN_TOK (COFFSETOF); }
247 "switch" { setTokLength (6); RETURN_TOK (SWITCH); }
248 "while" { setTokLength (5); RETURN_TOK (WHILE); }
249 "va_arg" { setTokLength (6); RETURN_TOK (VA_ARG); }
250 "va_dcl" { setTokLength (6); RETURN_TOK (VA_DCL); }
252 /* gcc extension...this might not be appropriate */
253 setTokLength (6); RETURN_TOK (QINLINE); }
255 "struct" { setTokLength (6); RETURN_TOK (CSTRUCT); }
256 "typedef" { setTokLength (7); RETURN_TOK (CTYPEDEF); }
258 "union" { setTokLength (5); RETURN_TOK (CUNION); }
259 "enum" { setTokLength (4); RETURN_TOK (CENUM); }
261 "void" { setTokLength (4); RETURN_TYPE (CVOID, ctype_void); }
262 "int" { setTokLength (3); RETURN_TYPE (CINT, ctype_int); }
263 "double" { setTokLength (6); RETURN_TYPE (CDOUBLE, ctype_double); }
264 "char" { setTokLength (4); RETURN_TYPE (CGCHAR, ctype_char); }
265 "float" { setTokLength (5); RETURN_TYPE (CGFLOAT, ctype_float); }
267 "long" { setTokLength (4); RETURN_TOK (QLONG); }
268 "short" { setTokLength (5); RETURN_TOK (QSHORT); }
269 "unsigned" { setTokLength (8); RETURN_TOK (QUNSIGNED); }
270 "signed" { setTokLength (6); RETURN_TOK (QSIGNED); }
272 "volatile" { setTokLength (8); RETURN_TOK (QVOLATILE); }
273 "const" { setTokLength (5); RETURN_TOK (QCONST); }
275 /* some systems expect this! [gack!] */
276 "__const" { setTokLength (7); RETURN_TOK (QCONST); }
278 "extern" { setTokLength (6); RETURN_TOK (QEXTERN); }
279 "auto" { setTokLength (4); RETURN_TOK (QAUTO); }
280 "register" { setTokLength (8); RETURN_TOK (QREGISTER); }
281 "static" { setTokLength (6); RETURN_TOK (QSTATIC); }
283 \"(\\.|[^\\"])*\"([ \t\n]*\"(\\.|[^\\"])*\")* { RETURN_EXPR (processString ()); }
284 L\"(\\.|[^\\"])*\"([ \t\n]*\"(\\.|[^\\"])*\")* { RETURN_EXPR (processWideString ()); }
285 "out" { return (processSpec (QOUT)); }
286 "in" { return (processSpec (QIN)); }
287 "partial" { return (processSpec (QPARTIAL)); }
288 "special" { return (processSpec (QSPECIAL)); }
289 "anytype" { return (processSpec (QANYTYPE)); }
290 "integraltype" { return (processSpec (QINTEGRALTYPE)); }
291 "unsignedintegraltype" { return (processSpec (QUNSIGNEDINTEGRALTYPE)); }
292 "signedintegraltype" { return (processSpec (QSIGNEDINTEGRALTYPE)); }
293 "keep" { return (processSpec (QKEEP)); }
294 "null" { return (processSpec (QNULL)); }
295 "notnull" { return (processSpec (QNOTNULL)); }
296 "isnull" { return (processSpec (QISNULL)); }
297 "truenull" { return (processSpec (QTRUENULL)); }
298 "falsenull" { return (processSpec (QFALSENULL)); }
299 "nullwhentrue" { return (processSpec (QTRUENULL)); }
300 "nullwhenfalse" { return (processSpec (QFALSENULL)); }
301 "relnull" { return (processSpec (QRELNULL)); }
302 "reldef" { return (processSpec (QRELDEF)); }
303 "exposed" { return (processSpec (QEXPOSED)); }
304 "newref" { return (processSpec (QNEWREF)); }
305 "tempref" { return (processSpec (QTEMPREF)); }
306 "killref" { return (processSpec (QKILLREF)); }
307 "refcounted" { return (processSpec (QREFCOUNTED)); }
308 "checked" { return (processSpec (QCHECKED)); }
309 "checkmod" { return (processSpec (QCHECKMOD)); }
310 "checkedstrict" { return (processSpec (QCHECKEDSTRICT)); }
311 "unchecked" { return (processSpec (QUNCHECKED)); }
312 "only" { return (processSpec (QONLY)); }
313 "owned" { return (processSpec (QOWNED)); }
314 "observer" { return (processSpec (QOBSERVER)); }
315 "dependent" { return (processSpec (QDEPENDENT)); }
316 "unused" { return (processSpec (QUNUSED)); }
317 "external" { return (processSpec (QEXTERNAL)); }
318 "sef" { return (processSpec (QSEF)); }
319 "shared" { return (processSpec (QSHARED)); }
320 "yield" { return (processSpec (QYIELD)); }
321 "undef" { return (processSpec (QUNDEF)); }
322 "killed" { return (processSpec (QKILLED)); }
323 "nullterminated" { return (processSpec (QNULLTERMINATED));}
324 "MaxSet" { return (processSpec (QMAXSET));}
325 "MaxRead" { return (processSpec (QMAXREAD));}
326 "maxSet" { return (processSpec (QMAXSET));}
327 "maxRead" { return (processSpec (QMAXREAD));}
329 {Letter}({Letter}|{Digit})* { int tok;
330 context_saveLocation ();
331 setTokLength (longUnsigned_toInt (mstring_length (yytext)));
332 tok = processIdentifier (makeIdentifier (yytext));
338 0[xX]{H}+ { setTokLengthT (mstring_length (yytext));
339 RETURN_INT (ctype_int, processHex ()); /* evs 2000-05-17 was ctype_uint */
341 0[xX]{H}+{L} { setTokLengthT (mstring_length (yytext));
342 RETURN_INT (ctype_lint, processHex ()); }
343 0[xX]{H}+{L}{L} { setTokLengthT (mstring_length (yytext));
344 RETURN_INT (ctype_llint, processHex ()); }
345 0[xX]{H}+{U} { setTokLengthT (mstring_length (yytext));
346 RETURN_INT (ctype_uint, processHex ()); }
347 0[xX]{H}+{ULSuffix} { setTokLengthT (mstring_length (yytext));
348 RETURN_INT (ctype_ulint, processHex ()); }
349 0[xX]{H}+{U}{L}{L} { setTokLengthT (mstring_length (yytext));
350 RETURN_INT (ctype_ullint, processHex ()); }
351 0[xX]{H}+{L}{L}{U} { setTokLengthT (mstring_length (yytext));
352 RETURN_INT (ctype_ullint, processHex ()); }
353 0{Digit}+ { setTokLengthT (mstring_length (yytext));
354 RETURN_INT (ctype_int, processOctal ()); }
355 0{Digit}+{U} { setTokLengthT (mstring_length (yytext));
356 RETURN_INT (ctype_uint, processOctal ()); }
357 0{Digit}+{L} { setTokLengthT (mstring_length (yytext));
358 RETURN_INT (ctype_lint, processOctal ()); }
359 0{Digit}+{L}{L} { setTokLengthT (mstring_length (yytext));
360 RETURN_INT (ctype_llint, processOctal ()); }
361 0{Digit}+{ULSuffix} { setTokLengthT (mstring_length (yytext));
362 RETURN_INT (ctype_ulint, processOctal ()); }
363 0{Digit}+{U}{L}{L} { setTokLengthT (mstring_length (yytext));
364 RETURN_INT (ctype_ullint, processOctal ()); }
365 0{Digit}+{L}{L}{U} { setTokLengthT (mstring_length (yytext));
366 RETURN_INT (ctype_ullint, processOctal ()); }
367 {Digit}+ { setTokLengthT (mstring_length (yytext));
368 RETURN_INT (ctype_int, processDec ()); }
369 {Digit}+{U} { setTokLengthT (mstring_length (yytext));
370 RETURN_INT (ctype_uint, processDec ()); }
371 {Digit}+{L} { setTokLengthT (mstring_length (yytext));
372 RETURN_INT (ctype_lint, processDec ()); }
373 {Digit}+{L}{L} { setTokLengthT (mstring_length (yytext));
374 RETURN_INT (ctype_llint, processDec ()); }
375 {Digit}+{ULSuffix} { setTokLengthT (mstring_length (yytext));
376 RETURN_INT (ctype_ulint, processDec ()); }
377 {Digit}+{U}{L}{L} { setTokLengthT (mstring_length (yytext));
378 RETURN_INT (ctype_ullint, processDec ()); }
379 {Digit}+{L}{L}{U} { setTokLengthT (mstring_length (yytext));
380 RETURN_INT (ctype_ullint, processDec ()); }
381 '(\\.|[^\\'])+' { setTokLengthT (mstring_length (yytext));
382 RETURN_CHAR (processChar ()); }
383 L'(\\.|[^\\'])+' { setTokLengthT (mstring_length (yytext));
384 RETURN_CHAR (processChar ()); }
385 {Digit}+{E}[fF] { setTokLengthT (mstring_length (yytext));
386 RETURN_FLOAT (ctype_float, processFloat ()); }
387 {Digit}+{E}[lL] { setTokLengthT (mstring_length (yytext));
388 RETURN_FLOAT (ctype_ldouble, processFloat ()); }
389 {Digit}+{E} { setTokLengthT (mstring_length (yytext));
390 RETURN_FLOAT (ctype_double, processFloat ()); }
392 {Digit}*"."{Digit}+({E})?[fF] { setTokLengthT (mstring_length (yytext));
393 RETURN_FLOAT (ctype_float, processFloat ()); }
394 {Digit}*"."{Digit}+({E})?[lL] { setTokLengthT (mstring_length (yytext));
395 RETURN_FLOAT (ctype_ldouble, processFloat ()); }
396 {Digit}*"."{Digit}+({E})? { setTokLengthT (mstring_length (yytext));
397 RETURN_FLOAT (ctype_double, processFloat ()); }
399 {Digit}+"."{Digit}*({E})?[fF] { setTokLengthT (mstring_length (yytext));
400 RETURN_FLOAT (ctype_float, processFloat ()); }
401 {Digit}+"."{Digit}*({E})?[lL] { setTokLengthT (mstring_length (yytext));
402 RETURN_FLOAT (ctype_ldouble, processFloat ()); }
403 {Digit}+"."{Digit}*({E})? { setTokLengthT (mstring_length (yytext));
404 RETURN_FLOAT (ctype_double, processFloat ()); }
406 ">>=" { setTokLength (3); RETURN_TOK (RIGHT_ASSIGN); }
407 "<<=" { setTokLength (3); RETURN_TOK (LEFT_ASSIGN); }
408 "+=" { setTokLength (2); RETURN_TOK (ADD_ASSIGN); }
409 "-=" { setTokLength (2); RETURN_TOK (SUB_ASSIGN); }
410 "*=" { setTokLength (2); RETURN_TOK (MUL_ASSIGN); }
411 "/=" { setTokLength (2); RETURN_TOK (DIV_ASSIGN); }
412 "%=" { setTokLength (2); RETURN_TOK (MOD_ASSIGN); }
413 "&=" { setTokLength (2); RETURN_TOK (AND_ASSIGN); }
414 "^=" { setTokLength (2); RETURN_TOK (XOR_ASSIGN); }
415 "|=" { setTokLength (2); RETURN_TOK (OR_ASSIGN); }
416 ">>" { setTokLength (2); RETURN_TOK (RIGHT_OP); }
417 "<<" { setTokLength (2); RETURN_TOK (LEFT_OP); }
418 "++" { setTokLength (2); RETURN_TOK (INC_OP); }
419 "--" { setTokLength (2); RETURN_TOK (DEC_OP); }
420 "->" { setTokLength (2); RETURN_TOK (ARROW_OP); }
421 "&&" { setTokLength (2); RETURN_TOK (AND_OP); }
422 "||" { setTokLength (2); RETURN_TOK (OR_OP); }
423 "<=" { setTokLength (2); RETURN_TOK (LE_OP); }
424 ">=" { setTokLength (2); RETURN_TOK (GE_OP); }
425 "==" { setTokLength (2); RETURN_TOK (EQ_OP); }
426 "!=" { setTokLength (2); RETURN_TOK (NE_OP); }
427 ";" { setTokLength (1); RETURN_TOK (TSEMI); }
428 "{" { setTokLength (1); RETURN_TOK (TLBRACE); }
429 "}" { setTokLength (1); RETURN_TOK (TRBRACE); }
430 "," { setTokLength (1); RETURN_TOK (TCOMMA); }
431 ":" { setTokLength (1); RETURN_TOK (TCOLON); }
432 "=" { setTokLength (1); RETURN_TOK (TASSIGN); }
433 "(" { setTokLength (1); RETURN_TOK (TLPAREN); }
434 ")" { setTokLength (1); RETURN_TOK (TRPAREN); }
435 "[" { setTokLength (1); RETURN_TOK (TLSQBR); }
436 "]" { setTokLength (1); RETURN_TOK (TRSQBR); }
437 "." { setTokLength (1); RETURN_TOK (TDOT); }
438 "&" { setTokLength (1); RETURN_TOK (TAMPERSAND); }
439 "!" { setTokLength (1); RETURN_TOK (TEXCL); }
442 "~" { setTokLength (1); RETURN_TOK (TTILDE); }
443 "-" { setTokLength (1); RETURN_TOK (TMINUS); }
444 "+" { setTokLength (1); RETURN_TOK (TPLUS); }
445 "*" { setTokLength (1); RETURN_TOK (TMULT); }
446 "/" { setTokLength (1); RETURN_TOK (TDIV); }
447 "%" { setTokLength (1); RETURN_TOK (TPERCENT); }
448 "<" { setTokLength (1); RETURN_TOK (TLT); }
449 ">" { setTokLength (1); RETURN_TOK (TGT); }
450 "^" { setTokLength (1); RETURN_TOK (TCIRC); }
451 "|" { setTokLength (1); RETURN_TOK (TBAR); }
452 "?" { setTokLength (1); RETURN_TOK (TQUEST); }
455 "/\\" { setTokLength (1); RETURN_TOK (TCAND); }
458 [ \t\v\f] { incColumn (); }
459 \n { context_incLineno ();
460 if (tokLength != 0) {
462 /* No error to report
465 message ("Likely parse error: token spans multiple lines."),
472 continueLine = FALSE;
476 if (context_inMacro ())
478 /* Don't use RETURN_TOK */
479 yylval.tok = lltok_create (TENDMACRO, fileloc_copy (g_currentloc)); /* !!! evans 2002-03-13 */
480 lastWasString = FALSE;
485 "@@MR@@" { setTokLength (6);
487 if (processMacro ()) {
488 if (context_inIterDef ())
490 RETURN_TOK (LLMACROITER);
492 if (context_inIterEnd ())
494 RETURN_TOK (LLMACROEND);
496 if (context_inMacro ())
498 RETURN_TOK (LLMACRO);
502 "@QLMR" { if (context_inHeader () || context_inFunction ())
508 int nspchar = ninput ();
512 ** This is a hack to get the column number correct.
515 llassert (nspchar >= '0' && nspchar <= '9');
517 nspaces = nspchar - '0';
519 setTokLength (5 + nspaces);
523 DPRINTF (("Here we are: %s", context_unparse ()));
525 if (context_inIterDef ())
527 RETURN_TOK (LLMACROITER);
529 if (context_inIterEnd ())
531 RETURN_TOK (LLMACROEND);
533 if (context_inMacro ())
535 RETURN_TOK (LLMACRO);
540 "@.CT" { setTokLength (4); lldiagmsg (ctype_unparseTable ()); }
541 "@.FA" { setTokLength (4); lldiagmsg (message ("Access types: %q", typeIdSet_unparse (context_fileAccessTypes ()))); }
542 "@.F" { setTokLength (3);
543 lldiagmsg (message ("%q: *** marker ***", fileloc_unparse (g_currentloc)));
545 "@.L" { setTokLength (3); usymtab_printLocal (); }
546 "@.A" { setTokLength (3); lldiagmsg (usymtab_unparseAliases ()); }
547 "@.C" { setTokLength (3); lldiagmsg (context_unparse ()); }
548 "@.W" { setTokLength (3); lldiagmsg (context_unparseClauses ()); }
549 "@.G" { setTokLength (3); usymtab_printGuards (); }
550 "@.S" { setTokLength (3); usymtab_printOut (); }
551 "@.X" { setTokLength (3); usymtab_printAll (); }
552 "@.Z" { setTokLength (3); usymtab_printComplete (); }
553 "@.T" { setTokLength (3); usymtab_printTypes (); }
554 "@.K" { setTokLength (3); lldiagmsg (usymtab_unparseStack ()); }
555 "@.M" { setTokLength (3);
556 lldiagmsg (message ("Can modify: %q",
557 sRefSet_unparse (context_modList ())));
559 "%{" { /* BEFORE_COMMENT_MARKER */
561 incColumn (); incColumn ();
562 tok = handleLlSpecial ();
566 if (tok == CANNOTATION) {
569 /* Beware - this bashes yylval! */
574 "%}" { /* AFTER_COMMENT_MARKER */
577 RETURN_TOK (QENDMACRO); }
578 "\\" { incColumn (); continueLine = TRUE; }
580 if ((int) *yytext == 13 ) {
585 message ("Invalid character (ascii: %d), skipping character",
594 /*@null@*/ /*@observer@*/ char *name;
599 ** These tokens are followed by syntax that is parsed by the
603 struct skeyword s_parsetable[] = {
604 { "modifies", QMODIFIES } ,
605 { "globals", QGLOBALS } ,
608 { "constant", QCONSTANT } ,
609 { "function", QFUNCTION } ,
611 { "defines", QDEFINES } ,
613 { "allocates", QALLOCATES } ,
615 { "releases", QRELEASES } ,
616 { "pre", QPRECLAUSE } ,
617 { "post", QPOSTCLAUSE } ,
618 { "setBufferSize", QSETBUFFERSIZE},
619 { "setStringLength", QSETSTRINGLENGTH},
620 { "testinRange", QTESTINRANGE},
621 { "requires", QPRECLAUSE } ,
622 { "ensures", QPOSTCLAUSE } ,
623 { "invariant", QINVARIANT} ,
628 ** These tokens are either stand-alone tokens, or followed by
629 ** token-specific text.
632 struct skeyword s_keytable[] = {
633 { "anytype", QANYTYPE } ,
634 { "integraltype", QINTEGRALTYPE } ,
635 { "unsignedintegraltype", QUNSIGNEDINTEGRALTYPE } ,
636 { "signedintegraltype", QSIGNEDINTEGRALTYPE } ,
640 { "owned", QOWNED } ,
641 { "dependent", QDEPENDENT } ,
642 { "partial", QPARTIAL } ,
643 { "special", QSPECIAL } ,
644 { "truenull", QTRUENULL } ,
645 { "falsenull", QFALSENULL } ,
646 { "nullwhentrue", QTRUENULL } ,
647 { "falsewhennull", QFALSENULL } ,
650 { "notnull", QNOTNULL } ,
651 { "abstract", QABSTRACT } ,
652 { "concrete", QCONCRETE } ,
653 { "mutable", QMUTABLE } ,
654 { "immutable", QIMMUTABLE } ,
655 { "unused", QUNUSED } ,
656 { "external", QEXTERNAL } ,
658 { "unique", QUNIQUE } ,
659 { "returned", QRETURNED } ,
660 { "exposed", QEXPOSED } ,
661 { "refcounted", QREFCOUNTED } ,
663 { "newref", QNEWREF } ,
664 { "tempref", QTEMPREF } ,
665 { "killref", QKILLREF } ,
667 { "relnull", QRELNULL } ,
668 { "nullterminated", QNULLTERMINATED },
669 { "setBufferSize", QSETBUFFERSIZE },
670 { "testInRange", QTESTINRANGE},
671 { "MaxSet", QMAXSET},
672 { "MaxRead", QMAXREAD},
673 { "reldef", QRELDEF } ,
674 { "observer", QOBSERVER } ,
675 { "exits", QEXITS } ,
676 { "noreturn", QEXITS } ,
677 { "mayexit", QMAYEXIT } ,
678 { "maynotreturn", QMAYEXIT } ,
679 { "trueexit", QTRUEEXIT } ,
680 { "falseexit", QFALSEEXIT } ,
681 { "noreturnwhentrue", QTRUEEXIT } ,
682 { "noreturnwhenfalse", QFALSEEXIT } ,
683 { "neverexit", QNEVEREXIT } ,
684 { "alwaysreturns", QNEVEREXIT } ,
686 { "shared", QSHARED } ,
688 { "unchecked", QUNCHECKED } ,
689 { "checked", QCHECKED } ,
690 { "checkmod", QCHECKMOD } ,
691 { "checkedstrict", QCHECKEDSTRICT } ,
692 { "innercontinue", QINNERCONTINUE } ,
693 { "innerbreak", QINNERBREAK } ,
694 { "loopbreak", QLOOPBREAK } ,
695 { "switchbreak", QSWITCHBREAK } ,
696 { "safebreak", QSAFEBREAK } ,
697 { "fallthrough", QFALLTHROUGH } ,
698 { "l_fallthrou", QLINTFALLTHROUGH } ,
699 { "l_fallth", QLINTFALLTHRU } ,
700 { "notreached", QNOTREACHED } ,
701 { "l_notreach", QLINTNOTREACHED } ,
702 { "printflike", QPRINTFLIKE } ,
703 { "l_printfli", QLINTPRINTFLIKE } ,
704 { "scanflike", QSCANFLIKE } ,
705 { "messagelike", QMESSAGELIKE } ,
706 { "l_argsus", QARGSUSED } ,
711 ** would be better if these weren't hard coded...
714 static bool isArtificial (cstring s)
716 return (cstring_equalLit (s, "modifies")
717 || cstring_equalLit (s, "globals")
718 || cstring_equalLit (s, "warn")
719 || cstring_equalLit (s, "alt"));
722 void swallowMacro (void)
725 bool skipnext = FALSE;
727 while ((i = lminput ()) != EOF)
744 reader_checkUngetc (i, yyin);
752 reader_checkUngetc (i, yyin);
756 static int commentMarkerToken (cstring s)
760 while (s_parsetable[i].name != NULL)
762 DPRINTF (("Try :%s:%s:", s, s_parsetable[i].name));
764 if (cstring_equalLit (s, s_parsetable[i].name))
766 return s_parsetable[i].token;
775 static int tokenMacroCode (cstring s)
779 while (s_keytable[i].name != NULL)
781 if (cstring_equalLit (s, s_keytable[i].name))
783 if (s_keytable[i].token == QLINTFALLTHROUGH)
786 (FLG_WARNLINTCOMMENTS,
788 ("Traditional lint comment /*FALLTHROUGH*/ used. "
789 "Splint interprets this in the same way as most Unix lints, but it is "
790 "preferable to replace it with the /*@fallthrough@*/ "
795 else if (s_keytable[i].token == QLINTFALLTHRU)
798 (FLG_WARNLINTCOMMENTS,
800 ("Traditional lint comment /*FALLTHRU*/ used. "
801 "Splint interprets this in the same way as most Unix lints, but it is "
802 "preferable to replace it with the /*@fallthrough@*/ "
807 else if (s_keytable[i].token == QLINTNOTREACHED)
810 (FLG_WARNLINTCOMMENTS,
812 ("Traditional lint comment /*NOTREACHED*/ used. "
813 "Splint interprets this in the same way as most Unix lints, but it is "
814 "preferable to replace it with the /*@notreached@*/ "
815 "semantic comment."),
820 else if (s_keytable[i].token == QPRINTFLIKE)
822 setSpecialFunction (qual_createPrintfLike ());
825 else if (s_keytable[i].token == QLINTPRINTFLIKE)
828 (FLG_WARNLINTCOMMENTS,
830 ("Traditional lint comment /*PRINTFLIKE*/ used. "
831 "Splint interprets this in the same way as most Unix lints, but it is "
832 "preferable to replace it with either /*@printflike@*/, "
833 "/*@scanflike@*/ or /*@messagelike@*/."),
836 setSpecialFunction (qual_createPrintfLike ());
839 else if (s_keytable[i].token == QSCANFLIKE)
841 setSpecialFunction (qual_createScanfLike ());
844 else if (s_keytable[i].token == QMESSAGELIKE)
846 setSpecialFunction (qual_createMessageLike ());
849 else if (s_keytable[i].token == QARGSUSED)
852 (FLG_WARNLINTCOMMENTS,
854 ("Traditional lint comment /*ARGSUSED*/ used. "
855 "Splint interprets this in the same way as most Unix lints, but it is "
856 "preferable to use /*@unused@*/ annotations on "
857 "the unused parameters."),
864 return s_keytable[i].token;
873 static int lminput ()
875 if (savechar == '\0')
882 int save = (int) savechar;
888 static void lmsavechar (char c)
890 if (savechar == '\0') savechar = c;
893 llbuglit ("lmsavechar: override");
897 static int returnFloat (ctype ct, double f)
899 yylval.expr = exprNode_floatLiteral (f, ct, cstring_fromChars (yytext),
900 fileloc_decColumn (g_currentloc, tokLength));
905 static int returnInt (ctype ct, long i)
909 if (ctype_equal (ct, ctype_int))
913 c = context_typeofZero ();
917 c = context_typeofOne ();
921 yylval.expr = exprNode_numLiteral (c, cstring_fromChars (yytext),
922 fileloc_decColumn (g_currentloc, tokLength), i);
927 static int returnChar (char c)
929 yylval.expr = exprNode_charLiteral (c, cstring_fromChars (yytext),
930 fileloc_decColumn (g_currentloc, tokLength));
939 if (c != EOF && ((char)c == '\n'))
941 context_incLineno ();
947 static char macro_nextChar ()
949 static bool in_quote = FALSE, in_escape = FALSE, in_char = FALSE;
954 c = char_fromInt (ic);
956 if (!in_quote && !in_char && (c == '\\' || c == BEFORE_COMMENT_MARKER[0]))
960 while ((c = char_fromInt (lminput ())) != '\0' && c != '\n')
962 ; /* skip to newline */
965 context_incLineno ();
969 return macro_nextChar ();
976 else /* if (c == '@') */
978 llassert (FALSE); /*@i23@*/
979 if (handleLlSpecial () != BADTOK)
981 llerrorlit (FLG_SYNTAX, "Macro cannot use special syntax");
984 return macro_nextChar ();
987 else if (!in_escape && c == '\"')
989 in_quote = !in_quote;
991 else if (!in_escape && c == '\'')
995 else if ((in_quote || in_char) && c == '\\')
997 in_escape = !in_escape;
999 else if ((in_quote || in_char) && in_escape)
1003 else if (!in_quote && c == '/')
1007 if ((c2 = char_fromInt (lminput ())) == '*')
1011 while ((c2 = char_fromInt (lminput ())) != '\0'
1012 && c2 != '\n' && c2 != '*')
1019 while ((c2 = char_fromInt (lminput ())) != '\0'
1032 llfatalerror (cstring_makeLiteral ("Macro: bad comment!"));
1036 return macro_nextChar ();
1040 /*** putchar does not work! why? puts to stdio...??! ***/
1048 ** keeps semantic comments
1051 static char macro_nextCharC ()
1053 static bool in_quote = FALSE, in_escape = FALSE, in_char = FALSE;
1056 c = char_fromInt (lminput ());
1058 if (!in_quote && !in_char && c == '\\')
1060 while ((c = char_fromInt (lminput ())) != '\0' && c != '\n')
1062 ; /* skip to newline */
1065 context_incLineno ();
1069 return macro_nextCharC ();
1076 else if (!in_escape && c == '\"')
1078 in_quote = !in_quote;
1080 else if (!in_escape && c == '\'')
1084 else if ((in_quote || in_char) && c == '\\')
1086 in_escape = !in_escape;
1088 else if ((in_quote || in_char) && in_escape)
1092 else if (!in_quote && c == '/')
1096 if ((c2 = char_fromInt (lminput ())) == '*')
1100 while ((c2 = char_fromInt (lminput ())) != '\0'
1101 && c2 != '\n' && c2 != '*')
1108 while ((c2 = char_fromInt (lminput ())) != '\0'
1121 llfatalerror (cstring_makeLiteral ("Macro: bad comment!"));
1125 return macro_nextCharC ();
1136 ** skips whitespace (handles line continuations)
1137 ** returns first non-whitespace character
1140 static char skip_whitespace ()
1144 while ((c = macro_nextChar ()) == ' ' || c == '\t')
1152 static void handleMacro ()
1154 cstring mac = cstring_undefined;
1158 while (currentColumn () > 2)
1160 mac = cstring_appendChar (mac, ' ');
1164 c = macro_nextCharC ();
1166 if (c >= '0' && c <= '9')
1170 for (i = 0; i < ((c - '0') + 1); i++)
1172 mac = cstring_appendChar (mac, ' ');
1180 while (((c = macro_nextCharC ()) != '\0') && (c != '\n'))
1182 mac = cstring_appendChar (mac, c);
1186 macrocode = tokenMacroCode (mac);
1188 if (macrocode == BADTOK && !isArtificial (mac))
1190 context_addMacroCache (mac);
1199 context_incLineno ();
1203 static bool processMacro (void)
1208 cstring fname = cstring_undefined;
1210 bool isspecfcn = FALSE;
1211 bool isiter = FALSE;
1212 bool skipparam = FALSE;
1213 bool isenditer = FALSE;
1214 bool unknownm = FALSE;
1215 bool hasParams = FALSE;
1216 bool emptyMacro = FALSE;
1217 char c = skip_whitespace ();
1218 fileloc loc = fileloc_noColumn (g_currentloc);
1220 /* are both of these necessary? what do they mean? */
1221 uentryList specparams = uentryList_undefined;
1222 uentryList pn = uentryList_undefined;
1224 context_resetMacroMissingParams ();
1226 if (c == '\0' || c == '\n')
1228 llcontbug (cstring_makeLiteral ("Bad macro"));
1233 fname = cstring_appendChar (fname, c);
1235 while ((c = macro_nextChar ()) != '(' && c != '\0'
1236 && c != ' ' && c != '\t' && c != '\n')
1238 fname = cstring_appendChar (fname, c);
1241 if (c == ' ' || c == '\t' || c == '\n')
1247 while (c == ' ' || c == '\t')
1249 c = macro_nextChar ();
1263 hasParams = (c == '(');
1265 if (usymtab_exists (fname))
1267 e2 = usymtab_lookupExpose (fname);
1268 ct = uentry_getType (e2);
1270 if (uentry_isCodeDefined (e2)
1271 && fileloc_isUser (uentry_whereDefined (e2)))
1275 message ("Macro %s already defined", fname),
1278 uentry_showWhereDefined (e2);
1279 uentry_clearDefined (e2);
1282 if (uentry_isFunction (e2))
1284 uentry_setType (e2, ctype_unknown);
1287 context_enterUnknownMacro (e2);
1291 context_enterConstantMacro (e2);
1296 if (uentry_isForward (e2) && uentry_isFunction (e2))
1303 ("Parameterized macro has no prototype or specification: %s ",
1308 uentry_setType (e2, ctype_unknown);
1309 uentry_setFunctionDefined (e2, loc);
1310 uentry_setUsed (e2, fileloc_undefined);
1311 context_enterUnknownMacro (e2);
1315 if (uentry_isIter (e2))
1318 specparams = uentry_getParams (e2);
1319 noparams = uentryList_size (specparams);
1320 uentry_setDefined (e2, loc);
1321 context_enterIterDef (e2);
1323 else if (uentry_isEndIter (e2))
1326 uentry_setDefined (e2, loc);
1327 context_enterIterEnd (e2); /* don't care about it now */
1328 /* but should parse like an iter! */
1330 else if (uentry_isConstant (e2))
1336 message ("Constant %s implemented as parameterized macro",
1340 uentry_showWhereSpecified (e2);
1341 uentry_setType (e2, ctype_unknown);
1342 uentry_makeConstantFunction (e2);
1343 uentry_setDefined (e2, g_currentloc);
1344 uentry_setFunctionDefined (e2, g_currentloc);
1345 context_enterUnknownMacro (e2);
1349 if (!uentry_isSpecified (e2))
1351 fileloc oloc = uentry_whereDeclared (e2);
1353 if (fileloc_isLib (oloc))
1357 else if (fileloc_isUndefined (oloc)
1358 || fileloc_isPreproc (oloc))
1363 (FLG_MACROCONSTDECL,
1365 ("Macro constant %q not declared",
1366 uentry_getName (e2)),
1370 else if (!fileloc_withinLines (oloc, loc, 2))
1371 { /* bogus! will give errors if there is too much whitespace */
1375 ("Macro constant name %s does not match name in "
1376 "previous constant declaration. This constant "
1377 "is declared at %q", fname,
1378 fileloc_unparse (oloc)),
1383 context_enterConstantMacro (e2);
1384 cstring_free (fname);
1390 else if (ctype_isFunction (ct))
1393 specparams = ctype_argsFunction (ct);
1394 noparams = uentryList_size (specparams);
1396 uentry_setFunctionDefined (e2, loc);
1397 context_enterMacro (e2);
1399 else if (uentry_isVar (e2))
1405 message ("Variable %s implemented as parameterized macro",
1409 uentry_showWhereSpecified (e2);
1410 uentry_setType (e2, ctype_unknown);
1411 uentry_makeVarFunction (e2);
1412 uentry_setDefined (e2, g_currentloc);
1413 uentry_setFunctionDefined (e2, g_currentloc);
1414 context_enterUnknownMacro (e2);
1418 uentry ucons = uentry_makeConstant (fname,
1421 if (uentry_isExpandedMacro (e2))
1429 message ("Variable %s implemented by a macro",
1433 uentry_showWhereSpecified (e2);
1437 uentry_setDefined (e2, loc);
1438 uentry_setUsed (ucons, loc);
1440 context_enterConstantMacro (ucons);
1441 uentry_markOwned (ucons);
1442 cstring_free (fname);
1448 if (uentry_isDatatype (e2))
1452 message ("Type implemented as macro: %x",
1453 uentry_getName (e2)),
1454 message ("A type is implemented using a macro definition. A "
1455 "typedef should be used instead."),
1459 /* Must exit scope (not sure why a new scope was entered?) */
1460 usymtab_quietExitScope (g_currentloc);
1461 uentry_setDefined (e2, g_currentloc);
1467 (message ("Unexpanded macro not function or constant: %q",
1468 uentry_unparse (e2)));
1469 uentry_setType (e2, ctype_unknown);
1473 uentry_makeVarFunction (e2);
1474 uentry_setDefined (e2, g_currentloc);
1475 uentry_setFunctionDefined (e2, g_currentloc);
1476 context_enterUnknownMacro (e2);
1487 /* evans 2001-09-09 - if it has params, assume a function */
1491 (FLG_MACROMATCHNAME,
1492 message ("Unexpanded macro %s does not match name of a declared "
1493 "function. The name used in the control "
1494 "comment on the previous line should match.",
1498 ce = uentry_makeFunction (fname, ctype_unknown,
1502 warnClause_undefined,
1504 uentry_setUsed (ce, loc); /* perhaps bogus? */
1505 e2 = usymtab_supEntryReturn (ce);
1506 context_enterUnknownMacro (e2);
1511 (FLG_MACROMATCHNAME,
1512 message ("Unexpanded macro %s does not match name of a constant "
1513 "or iter declaration. The name used in the control "
1514 "comment on the previous line should match. "
1515 "(Assuming macro defines a constant.)",
1519 ce = uentry_makeConstant (fname, ctype_unknown, fileloc_undefined);
1520 uentry_setUsed (ce, loc); /* perhaps bogus? */
1521 e2 = usymtab_supEntryReturn (ce);
1523 context_enterConstantMacro (e2);
1524 cstring_free (fname);
1530 /* in macros, ( must follow immediatetly after name */
1536 c = skip_whitespace ();
1538 while (c != ')' && c != '\0')
1541 bool suppress = context_inSuppressRegion ();
1542 cstring paramname = cstring_undefined;
1545 ** save the parameter location
1549 context_saveLocation ();
1552 while (c != ' ' && c != '\t' && c != ',' && c != '\0' && c != ')')
1554 paramname = cstring_appendChar (paramname, c);
1555 c = macro_nextChar ();
1558 if (c == ' ' || c == '\t') c = skip_whitespace ();
1562 c = macro_nextChar ();
1563 if (c == ' ' || c == '\t') c = skip_whitespace ();
1568 llfatalerror (cstring_makeLiteral
1569 ("Bad macro syntax: uentryList"));
1572 if ((isspecfcn || isiter) && (paramno < noparams)
1573 && !uentry_isElipsisMarker (uentryList_getN
1574 (specparams, paramno)))
1576 fileloc sloc = context_getSaveLocation ();
1577 uentry decl = uentryList_getN (specparams, paramno);
1580 param = uentry_nameCopy (paramname, decl);
1582 uentry_setParam (param);
1583 sr = sRef_makeParam (paramno, uentry_getType (param), stateInfo_makeLoc (sloc));
1585 if (sRef_getNullState (sr) == NS_ABSNULL)
1587 ctype pt = ctype_realType (uentry_getType (param));
1589 if (ctype_isUser (pt))
1591 uentry te = usymtab_getTypeEntrySafe (ctype_typeId (pt));
1593 if (uentry_isValid (te))
1595 sRef_setStateFromUentry (sr, te);
1600 sRef_setNullState (sr, NS_UNKNOWN, sloc);
1604 uentry_setSref (param, sr);
1605 uentry_setDeclaredForceOnly (param, sloc);
1607 skipparam = isiter && uentry_isOut (uentryList_getN (specparams, paramno));
1611 fileloc sloc = context_getSaveLocation ();
1613 param = uentry_makeVariableSrefParam
1614 (paramname, ctype_unknown, fileloc_copy (sloc),
1615 sRef_makeParam (paramno, ctype_unknown, stateInfo_makeLoc (sloc)));
1616 DPRINTF (("Unknown param: %s", uentry_unparseFull (param)));
1617 cstring_free (paramname);
1619 sRef_setPosNull (uentry_getSref (param), sloc);
1620 uentry_setDeclaredForce (param, sloc);
1623 fileloc_free (sloc);
1628 llassert (!uentry_isElipsisMarker (param));
1632 sRef_makeUnsafe (uentry_getSref (param));
1635 pn = uentryList_add (pn, uentry_copy (param));
1636 usymtab_supEntry (param);
1640 /* don't add param */
1641 uentry_free (param);
1646 (void) macro_nextChar ();
1647 c = skip_whitespace ();
1655 if (isspecfcn || isiter)
1657 if (paramno != noparams && noparams >= 0)
1663 message ("Macro %s specified with %d args, defined with %d",
1664 fname, noparams, paramno),
1667 uentry_showWhereSpecified (e2);
1668 uentry_resetParams (e2, pn);
1673 uentry_resetParams (e2, pn);
1680 ** the form should be:
1682 ** # define newname oldname
1683 ** where oldname refers to a function matching the specification
1689 sRef_setGlobalScope ();
1690 usymtab_supGlobalEntry (uentry_makeVariableLoc (fname, ctype_unknown));
1691 sRef_clearGlobalScope ();
1695 context_setMacroMissingParams ();
1700 /* context_setuentryList (pn); */
1701 usymtab_enterScope ();
1704 cstring_free (fname);
1709 static bool handleSpecial (char *yyt)
1711 char *l = mstring_create (MAX_NAME_LENGTH);
1719 len_yyt = strlen (yyt +1) ;
1721 l = mstring_copy (yyt + 1);
1723 while ((c = char_fromInt (lminput ())) != '\n' && c != '\0')
1725 l = mstring_append(l, c);
1728 /* Need to safe original l for deallocating. */
1733 olc = cstring_fromChars (ol);
1735 if (cstring_equalPrefixLit (olc, "pragma"))
1737 char *pname = mstring_create (longUnsigned_fromInt (MAX_PRAGMA_LEN));
1738 char *opname = pname;
1739 char *ptr = ol + 6; /* pragma is six characters, plus space */
1743 /* skip whitespace */
1744 while (((c = *ptr) != '\0') && isspace (c))
1750 while (((c = *ptr) != '\0') && !isspace (c))
1754 if (len > MAX_PRAGMA_LEN)
1765 if (len == PRAGMA_LEN_EXPAND
1766 && mstring_equal (opname, PRAGMA_EXPAND))
1768 cstring exname = cstring_undefined;
1772 while (((c = *ptr) != '\0') && !isspace (c))
1774 exname = cstring_appendChar (exname, c);
1779 ue = usymtab_lookupExposeGlob (exname);
1781 if (uentry_isExpandedMacro (ue))
1783 if (fileloc_isPreproc (uentry_whereDefined (ue)))
1785 fileloc_setColumn (g_currentloc, 1);
1786 uentry_setDefined (ue, g_currentloc);
1790 cstring_free (exname);
1793 else if (cstring_equalPrefixLit (olc, "ident"))
1795 /* Some pre-processors will leave these in the code. Ignore rest of line */
1798 ** Yuk...Win32 filenames can have spaces in them...we need to read
1799 ** to the matching end quote.
1801 else if ((sscanf (ol, "line %d \"", &lineno) == 1)
1802 || (sscanf (ol, " %d \"", &lineno) == 1))
1808 /*@access cstring@*/
1809 while (*tmp != '\"' && *tmp != '\0')
1814 llassert (*tmp == '\"');
1820 while (*tmp != '\"' && *tmp != '\0')
1825 llassert (*tmp == '\"');
1829 # if defined(OS2) || defined(MSDOS) || defined(WIN32)
1832 ** DOS-like path delimiters get delivered in pairs, something like
1833 ** \"..\\\\file.h\", so we have to make it normal again. We do NOT
1834 ** remove the pre dirs yet as we usually specify tmp paths relative
1835 ** to the current directory, so tmp files would not get found in
1836 ** the hash table. If this method fails we try it again later.
1843 ** Skip past the drive marker.
1846 if (strchr (stmp, ':') != NULL)
1848 stmp = strchr (stmp, ':') + 1;
1851 while ((stmp = strchr (stmp, CONNECTCHAR)) != NULL )
1853 if (*(stmp+1) == CONNECTCHAR)
1855 memmove (stmp, stmp+1, strlen (stmp));
1861 fid = fileTable_lookupBase (context_fileTable (), fname);
1862 if (!(fileId_isValid (fid)))
1864 fname = removePreDirs (fname);
1865 fid = fileTable_lookupBase (context_fileTable (), fname);
1868 # else /* !defined(OS2) && !defined(MSDOS) */
1869 fname = removePreDirs (fname);
1870 fid = fileTable_lookupBase (context_fileTable (), fname);
1871 # endif /* !defined(OS2) && !defined(MSDOS) */
1873 if (!(fileId_isValid (fid)))
1875 if (context_inXHFile ())
1877 fid = fileTable_addXHFile (context_fileTable (), fname);
1879 else if (isHeaderFile (fname))
1881 fid = fileTable_addHeaderFile (context_fileTable (), fname);
1885 fid = fileTable_addFile (context_fileTable (), fname);
1889 setFileLine (fid, lineno);
1890 /*@noaccess cstring@*/
1892 else if ((sscanf (ol, "line %d", &lineno) == 1)
1893 || (sscanf (ol, " %d", &lineno) == 1))
1895 setLine (lineno); /* next line is <cr> */
1899 if (mstring_equal (ol, "")) {
1900 DPRINTF (("Empty pp command!"));
1902 ** evs 2000-05-16: This is a horrible kludge, to get around a bug (well, difficulty) in the pre-processor.
1903 ** We handle a plain # in the input file, by echoing it, and ignoring it in the post-pp-file.
1909 (FLG_UNRECOGDIRECTIVE,
1910 message ("Unrecognized pre-processor directive: #%s",
1911 cstring_fromChars (ol)),
1916 return FALSE; /* evans 2001-12-30: was: TRUE; */
1923 static int handleLlSpecial ()
1928 char *s = mstring_createEmpty ();
1934 loc = fileloc_copy (g_currentloc);
1935 DPRINTF (("Handle special: %s", fileloc_unparse (loc)));
1937 while (((ic = ninput ()) != 0) && isalpha (ic))
1940 s = mstring_append (s, c);
1944 DPRINTF (("Read: %s / %s", s, fileloc_unparse (g_currentloc)));
1947 if (charsread == 0 && ic == (int) AFTER_COMMENT_MARKER[0])
1951 llassert (ic == AFTER_COMMENT_MARKER[1]);
1957 return QNOMODS; /* special token no modifications token */
1961 DPRINTF (("Coment marker: %s", os));
1962 tok = commentMarkerToken (cstring_fromChars (os));
1966 tokLength = charsread;
1973 DPRINTF (("Not a comment marker..."));
1974 /* Add rest of the comment */
1976 if (ic != 0 && ic != EOF)
1980 s = mstring_append (s, c);
1983 while (((ic = ninput ()) != 0) && (ic != EOF)
1984 && (ic != AFTER_COMMENT_MARKER[0]))
1988 /* evans 2001-09-01 added to prevent assertion failures for uncloses syntactic comments */
1991 hasnl = TRUE; /* This prevents tokLength from being set later. */
1996 message ("Likely parse error: syntactic comment token spans multiple lines: %s",
1997 cstring_fromChars (s)),
2001 s = mstring_append (s, c);
2006 DPRINTF (("Read: %s / %s", s, fileloc_unparse (g_currentloc)));
2008 if (ic == AFTER_COMMENT_MARKER[0])
2011 llassert ((char) nc == AFTER_COMMENT_MARKER[1]);
2017 while (*s == ' ' || *s == '\t' || *s == '\n')
2022 if (*s == '-' || *s == '+' || *s == '=') /* setting flags */
2026 while (c == '-' || c == '+' || c == '=')
2028 ynm set = ynm_fromCodeChar (c);
2033 thisflag = cstring_fromChars (s);
2035 while ((c = *s) != '\0' && (c != '-') && (c != '=')
2036 && (c != '+') && (c != ' ') && (c != '\t') && (c != '\n'))
2043 if (!context_getFlag (FLG_NOCOMMENTS))
2045 cstring flagname = thisflag;
2046 flagcode fflag = flags_identifyFlag (flagname);
2048 if (flagcode_isSkip (fflag))
2052 else if (flagcode_isInvalid (fflag))
2054 if (isMode (flagname))
2056 if (ynm_isMaybe (set))
2061 ("Semantic comment attempts to restore flag %s. "
2062 "A mode flag cannot be restored.",
2067 context_setMode (flagname);
2073 (FLG_UNRECOGFLAGCOMMENTS,
2074 message ("Unrecognized option in semantic comment: %s",
2079 else if (flagcode_isGlobalFlag (fflag))
2084 ("Semantic comment attempts to set global flag %s. "
2085 "A global flag cannot be set locally.",
2091 context_fileSetFlag (fflag, set, loc);
2093 if (flagcode_hasArgument (fflag))
2095 if (ynm_isMaybe (set))
2100 ("Semantic comment attempts to restore flag %s. "
2101 "A flag for setting a value cannot be restored.",
2106 { /* cut-and-pastied from llmain...blecch */
2107 cstring extra = cstring_undefined;
2113 rest = mstring_copy (s);
2117 while ((rchar = *rest) != '\0'
2118 && (isspace (rchar)))
2124 while ((rchar = *rest) != '\0'
2125 && !isspace (rchar))
2127 extra = cstring_appendChar (extra, rchar);
2134 if (cstring_isUndefined (extra))
2139 ("Flag %s (in semantic comment) must be followed by an argument",
2140 flagcode_unparse (fflag)));
2146 if (flagcode_hasNumber (fflag))
2148 setValueFlag (fflag, extra);
2150 else if (flagcode_hasChar (fflag))
2152 setValueFlag (fflag, extra);
2154 else if (flagcode_hasString (fflag))
2156 setStringFlag (fflag, extra);
2173 while ((c == ' ') || (c == '\t') || (c == '\n'))
2179 if (context_inHeader () && !isArtificial (cstring_fromChars (os)))
2181 DPRINTF (("Here adding comment: %s", os));
2182 context_addComment (cstring_fromCharsNew (os), loc);
2194 annotationInfo ainfo;
2196 while (*s != '\0' && *s != ' ' && *s != '\t' && *s != '\n')
2208 t = cstring_toCharsSafe (cstring_downcase (cstring_fromChars (t)));
2209 macrocode = tokenMacroCode (cstring_fromChars (t));
2211 if (macrocode != BADTOK)
2213 tokLength = hasnl ? 0 : mstring_length (t);
2219 if (macrocode == SKIPTOK)
2227 ainfo = context_lookupAnnotation (cstring_fromChars (os));
2229 if (annotationInfo_isDefined (ainfo)) {
2230 DPRINTF (("Found annotation: %s", annotationInfo_unparse (ainfo)));
2231 /*@i324@*/ yylval.annotation = ainfo;
2239 if (context_inHeader ())
2246 if ((context_inMacro () || context_inGlobalContext ())
2247 && macrocode != SKIPTOK
2248 && !isArtificial (cstring_fromChars (os)))
2250 if (context_processingMacros ())
2252 /* evans 2002-02-24: don't add comments when procssing macros */
2256 context_addComment (cstring_fromCharsNew (os), loc);
2270 if (mstring_equal (t, "ignore"))
2272 if (!context_getFlag (FLG_NOCOMMENTS))
2274 context_enterSuppressRegion (loc);
2277 else if ((*t == 'i' || *t == 't')
2278 && (*(t + 1) == '\0'))
2280 if (!context_getFlag (FLG_NOCOMMENTS)
2281 && (*t == 'i' || context_getFlag (FLG_TMPCOMMENTS)))
2283 context_enterSuppressLine (-1, loc); /* infinite suppression */
2286 else if (((*t == 'i') || (*t == 't'))
2287 && ((*(t + 1) >= '0' && *(t + 1) <= '9')))
2289 bool tmpcomment = (*t == 't');
2291 char *tt = t; /* don't mangle t, since it is free'd */
2294 if (lc >= '0' && lc <= '9')
2296 val = (int)(lc - '0');
2299 while (lc >= '0' && lc <= '9')
2307 if (!context_getFlag (FLG_NOCOMMENTS)
2308 && (!tmpcomment || context_getFlag (FLG_TMPCOMMENTS)))
2310 DPRINTF (("Here: enter suppress: %s", fileloc_unparse (loc)));
2311 context_enterSuppressLine (val, loc);
2314 else if (mstring_equal (t, "end"))
2316 if (!context_getFlag (FLG_NOCOMMENTS))
2318 context_exitSuppressRegion (loc);
2321 else if (mstring_equal (t, "notfunction"))
2323 ; /* handled by pcpp */
2325 else if (mstring_equal (t, "access"))
2331 while ((c = *s) && (c == ' ' || c == '\t' || c == '\n'))
2341 tname = cstring_fromChars (s);
2343 while ((c = *s) != '\0' && c != ' '
2344 && c != '\t' && c != '\n' && c != ',')
2351 DPRINTF (("Access %s", tname));
2353 if (!context_getFlag (FLG_NOCOMMENTS)
2354 && !context_getFlag (FLG_NOACCESS))
2356 if (usymtab_existsType (tname))
2358 typeId uid = usymtab_getTypeId (tname);
2359 uentry ue = usymtab_getTypeEntry (uid);
2361 if (uentry_isAbstractDatatype (ue))
2363 context_addFileAccessType (uid);
2364 DPRINTF (("Adding access to: %s / %d", tname, uid));
2371 ("Non-abstract type %s used in access comment",
2378 if (!(context_inSuppressRegion ()
2379 || context_inSuppressZone (loc)))
2384 ("Unrecognized type %s used in access comment",
2396 if (c != ',' && c != ' ')
2402 else if (mstring_equal (t, "noaccess"))
2409 while ((lc = *s) && (lc == ' ' || lc == '\t' || lc == '\n'))
2419 tname = cstring_fromChars (s);
2421 while ((lc = *s) != '\0' && lc != ' ' && lc != '\t'
2422 && lc != '\n' && lc != ',')
2429 if (!context_getFlag (FLG_NOCOMMENTS)
2430 && !context_getFlag (FLG_NOACCESS))
2432 if (usymtab_existsType (tname))
2434 typeId tuid = usymtab_getTypeId (tname);
2436 if (context_couldHaveAccess (tuid))
2438 DPRINTF (("Removing access: %s", tname));
2439 context_removeFileAccessType (tuid);
2443 if (!(context_inSuppressRegion ()
2444 || context_inSuppressZone (loc)))
2446 uentry ue = usymtab_getTypeEntry (tuid);
2448 if (uentry_isAbstractDatatype (ue))
2453 ("Non-accessible abstract type %s used in noaccess comment",
2462 ("Non-abstract type %s used in noaccess comment",
2471 if (!(context_inSuppressRegion ()
2472 || context_inSuppressZone (loc)))
2477 ("Unrecognized type %s used in noaccess comment",
2489 if (lc != ',' && lc != ' ')
2497 voptgenerror (FLG_UNRECOGCOMMENTS,
2498 message ("Semantic comment unrecognized: %s",
2499 cstring_fromChars (os)),
2511 static /*@only@*/ cstring makeIdentifier (char *s)
2513 char *c = mstring_create (size_toInt (strlen (s)) + 1);
2514 cstring id = cstring_fromChars (c);
2516 while (isalnum (*s) || (*s == '_') || (*s == '$'))
2525 /*@observer@*/ /*@dependent@*/ uentry coerceId (cstring cn)
2527 if (!(usymtab_exists (cn)))
2529 fileloc loc = fileloc_createExternal ();
2532 ** We need to put this in a global scope, otherwise the sRef will be deallocated.
2535 uentry ce = uentry_makeUnrecognized (cn, loc);
2537 if (!context_inIterEnd ())
2541 message ("Unrecognized (possibly system) identifier: %q",
2542 uentry_getName (ce)),
2549 return (usymtab_lookup (cn));
2553 ** like, coerceId, but doesn't supercede for iters
2556 /*@observer@*/ uentry coerceIterId (cstring cn)
2558 if (!(usymtab_exists (cn)))
2560 return uentry_undefined;
2563 return (usymtab_lookup (cn));
2566 /*@observer@*/ cstring LastIdentifier ()
2568 return (lastidprocessed);
2571 static int processIdentifier (cstring id)
2575 if (context_getFlag (FLG_GRAMMAR))
2577 lldiagmsg (message ("Process identifier: %s", id));
2580 context_clearJustPopped ();
2581 lastidprocessed = id;
2583 if (context_inFunctionHeader ())
2585 int tok = commentMarkerToken (id);
2586 DPRINTF (("in function decl..."));
2594 tok = tokenMacroCode (id);
2602 annotationInfo ainfo;
2604 if (expectingMetaStateName)
2606 metaStateInfo msinfo = context_lookupMetaStateInfo (id);
2608 if (metaStateInfo_isDefined (msinfo))
2610 yylval.msinfo = msinfo;
2611 return METASTATE_NAME;
2615 DPRINTF (("Not meta state name: %s", cstring_toCharsSafe (id)));
2619 ainfo = context_lookupAnnotation (id);
2621 if (annotationInfo_isDefined (ainfo))
2623 DPRINTF (("Found annotation: %s", annotationInfo_unparse (ainfo)));
2624 /*@i324@*/ yylval.annotation = ainfo;
2629 DPRINTF (("Not annotation: %s", id));
2635 /* Consider handling: Defined by C99 as static const char __func__[] */
2637 if (context_getFlag (FLG_GNUEXTENSIONS))
2641 if (cstring_equalLit (id, "__stdcall")
2642 || cstring_equalLit (id, "__cdecl")
2643 || cstring_equalLit (id, "__extension__"))
2647 else if (cstring_equalLit (id, "__volatile__"))
2651 else if (cstring_equalLit (id, "__signed"))
2655 else if (cstring_equalLit (id, "__unsigned"))
2659 else if (cstring_equalLit (id, "__const__"))
2663 else if (cstring_equalLit (id, "__alignof__"))
2665 tok = CALIGNOF; /* alignof is parsed like sizeof */
2667 else if (cstring_equalLit (id, "__typeof__"))
2671 else if (cstring_equalLit (id, "typeof"))
2675 else if (cstring_equalLit (id, "__FUNCTION__")
2676 || cstring_equalLit (id, "__PRETTY_FUNCTION__"))
2678 /* These tokens hold the name of the current function as strings */
2679 /* evans 2001-12-30: changed from exprNode_stringLiteral; bug reported by Jim Zelenka. */
2680 yylval.expr = exprNode_makeConstantString (id, fileloc_copy (g_currentloc));
2682 lastWasString = TRUE;
2686 else if (cstring_equalLit (id, "__attribute__")
2687 || cstring_equalLit (id, "__asm__")
2688 || cstring_equalLit (id, "_asm")
2689 || cstring_equalLit (id, "__asm")
2690 || cstring_equalLit (id, "__declspec"))
2693 bool useparens = FALSE;
2694 bool usebraces = FALSE;
2695 bool inquote = FALSE;
2696 bool inescape = FALSE;
2699 while ((ic = input ()) != EOF)
2706 else if (ic == '\\')
2710 else if (ic == '\"')
2746 else if (ic == ')' && useparens)
2749 if (depth == 0) break;
2751 else if (ic == '}' && usebraces)
2754 if (depth == 0) break;
2757 && !usebraces && !useparens
2758 && cstring_equalLit (id, "__asm"))
2761 ** We need this because some MS VC++ include files
2762 ** have __asm mov ... }
2763 ** Its a kludge, but otherwise would need to parse
2772 context_incLineno ();
2774 if (cstring_equalLit (id, "__asm")
2775 && !useparens && !usebraces)
2782 llassert ((useparens && ic == ')')
2783 || (usebraces && ic == '}')
2784 || (!useparens && !usebraces));
2788 else if (cstring_equalLit (id, "inline")
2789 || cstring_equalLit (id, "__inline")
2790 || cstring_equalLit (id, "_inline")
2791 || cstring_equalLit (id, "__inline__"))
2802 le = usymtab_lookupSafe (id);
2804 /*@-dependenttrans@*/
2806 if (uentry_isIter (le))
2808 /*@i32@*/ yylval.entry = le;
2811 else if (uentry_isEndIter (le))
2813 /*@i32@*/ yylval.entry = le;
2814 return (ITER_ENDNAME);
2816 else if (uentry_isUndefined (le))
2820 /* avoid parse errors for certain system built ins */
2822 if (g_expectingTypeName && (cstring_firstChar (id) == '_')
2823 && (cstring_secondChar (id) == '_'))
2825 return (TYPE_NAME_OR_ID);
2828 return (NEW_IDENTIFIER);
2830 else if (!uentry_isDeclared (le) && !uentry_isCodeDefined (le))
2832 if (uentry_isDatatype (le))
2835 return (NEW_IDENTIFIER);
2839 /*@i32@*/ yylval.entry = le;
2840 return (IDENTIFIER);
2843 else if (uentry_isDatatype (le))
2845 if (!g_expectingTypeName)
2849 return (NEW_IDENTIFIER);
2853 yylval.ctyp = uentry_getAbstractType (le);
2855 uentry_setUsed (le, g_currentloc);
2861 /*@i32@*/ yylval.entry = le;
2862 return (IDENTIFIER);
2865 /*@=dependenttrans@*/
2868 static bool processHashIdentifier (/*@only@*/ cstring id)
2870 if (context_inMacro () || context_inIterDef () ||
2871 context_inIterEnd ())
2875 context_clearJustPopped ();
2877 lastidprocessed = id;
2878 le = usymtab_lookupSafe (id);
2880 if (uentry_isParam (le) || uentry_isRefParam (le))
2892 ** Will be handled by handleLlSpecial
2901 static /*@only@*/ exprNode processString ()
2905 char *nl = strchr (yytext, '\n');
2906 cstring ns = cstring_fromCharsNew (yytext);
2910 loc = fileloc_copy (g_currentloc);
2911 addColumn (cstring_length (ns));
2917 loc = fileloc_copy (g_currentloc);
2919 context_incLineno ();
2921 while ((nl = strchr ((nl + 1), '\n')) != NULL)
2923 context_incLineno ();
2929 res = exprNode_stringLiteral (ns, loc);
2934 ** process a wide character string L"...."
2937 static /*@only@*/ exprNode processWideString ()
2941 char *nl = strchr (yytext, '\n');
2944 llassert (*yytext == 'L');
2947 ns = cstring_fromCharsNew (yytext);
2951 loc = fileloc_copy (g_currentloc);
2952 addColumn (cstring_length (ns));
2958 loc = fileloc_copy (g_currentloc);
2960 context_incLineno ();
2962 while ((nl = strchr ((nl + 1), '\n')) != NULL)
2964 context_incLineno ();
2969 res = exprNode_wideStringLiteral (ns, loc);
2979 llassert (*yytext != '\0');
2980 fchar = *(yytext + 1);
2981 if (fchar != '\\') return fchar;
2983 next = *(yytext + 2);
2987 case 'n': return '\n';
2988 case 't': return '\t';
2989 case '\"': return '\"';
2990 case '\'': return '\'';
2991 case '\\': return '\\';
2992 default: return '\0';
2997 double processFloat ()
2999 double ret = atof (yytext);
3010 llassert (yytext[0] == '0'
3011 && (yytext[1] == 'X' || yytext[1] == 'x'));
3013 while (yytext[index] != '\0') {
3015 char c = yytext[index];
3017 if (c >= '0' && c <= '9') {
3018 tval = (int) c - (int) '0';
3019 } else if (c >= 'A' && c <= 'F') {
3020 tval = (int) c - (int) 'A' + 10;
3021 } else if (c >= 'a' && c <= 'f') {
3022 tval = (int) c - (int) 'a' + 10;
3023 } else if (c == 'U' || c == 'L' || c == 'u' || c == 'l') {
3025 while (yytext[index] != '\0') {
3026 if (c == 'U' || c == 'L' || c == 'u' || c == 'l') {
3031 message ("Invalid character (%c) following specifier in hex constant: %s",
3032 c, cstring_fromChars (yytext)),
3042 message ("Invalid character (%c) in hex constant: %s",
3043 c, cstring_fromChars (yytext)),
3048 val = (val * 16) + tval;
3052 DPRINTF (("Hex constant: %s = %ld", yytext, val));
3057 long processOctal ()
3062 llassert (yytext[0] == '0' && yytext[1] != 'X' && yytext[1] != 'x');
3064 while (yytext[index] != '\0') {
3066 char c = yytext[index];
3068 if (c >= '0' && c <= '7') {
3069 tval = (int) c - (int) '0';
3070 } else if (c == 'U' || c == 'L' || c == 'u' || c == 'l') {
3072 while (yytext[index] != '\0') {
3073 if (c == 'U' || c == 'L' || c == 'u' || c == 'l') {
3078 message ("Invalid character (%c) following specifier in octal constant: %s",
3079 c, cstring_fromChars (yytext)),
3089 message ("Invalid character (%c) in octal constant: %s",
3090 c, cstring_fromChars (yytext)),
3095 val = (val * 8) + tval;
3099 DPRINTF (("Octal constant: %s = %ld", yytext, val));
3106 return (atol (yytext));
3110 processSpec (int tok)
3112 size_t length = strlen (yytext);
3116 setTokLengthT (length);
3122 context_saveLocation ();
3123 setTokLengthT (length);
3124 return (processIdentifier (makeIdentifier (yytext)));
3128 void cscanner_expectingMetaStateName ()
3130 llassert (!expectingMetaStateName);
3131 llassert (context_inFunctionHeader ());
3132 expectingMetaStateName = TRUE;
3135 void cscanner_clearExpectingMetaStateName ()
3137 llassert (expectingMetaStateName);
3138 expectingMetaStateName = FALSE;