2 ** Splint - annotation-assisted static program checker
3 ** Copyright (C) 1994-2001 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 lclint: lclint-request@cs.virginia.edu
21 ** To report a bug: lclint-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 LCLint 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 "lclintMacros.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 "This is interpreted by "
781 "LCLint in the same way as most Unix lints, but it is "
782 "preferable to replace it with the /*@fallthrough@*/ "
787 else if (s_keytable[i].token == QLINTFALLTHRU)
790 (FLG_WARNLINTCOMMENTS,
792 ("Traditional lint comment /*FALLTHRU*/ used. "
793 "This is interpreted by "
794 "LCLint in the same way as most Unix lints, but it is "
795 "preferable to replace it with the /*@fallthrough@*/ "
800 else if (s_keytable[i].token == QLINTNOTREACHED)
803 (FLG_WARNLINTCOMMENTS,
805 ("Traditional lint comment /*NOTREACHED*/ used. "
806 "This is interpreted by "
807 "LCLint in the same way as most Unix lints, but it is "
808 "preferable to replace it with the /*@notreached@*/ "
809 "semantic comment."),
814 else if (s_keytable[i].token == QPRINTFLIKE)
816 setSpecialFunction (qual_createPrintfLike ());
819 else if (s_keytable[i].token == QLINTPRINTFLIKE)
822 (FLG_WARNLINTCOMMENTS,
824 ("Traditional lint comment /*PRINTFLIKE*/ used. "
825 "This is interpreted by "
826 "LCLint in the same way as most Unix lints, but it is "
827 "preferable to replace it with either /*@printflike@*/, "
828 "/*@scanflike@*/ or /*@messagelike@*/."),
831 setSpecialFunction (qual_createPrintfLike ());
834 else if (s_keytable[i].token == QSCANFLIKE)
836 setSpecialFunction (qual_createScanfLike ());
839 else if (s_keytable[i].token == QMESSAGELIKE)
841 setSpecialFunction (qual_createMessageLike ());
844 else if (s_keytable[i].token == QARGSUSED)
847 (FLG_WARNLINTCOMMENTS,
849 ("Traditional lint comment /*ARGSUSED*/ used. "
850 "This is interpreted by "
851 "LCLint in the same way as most Unix lints, but it is "
852 "preferable to use /*@unused@*/ annotations on "
853 "the unused parameters."),
860 return s_keytable[i].token;
869 static int lminput ()
871 if (savechar == '\0')
878 int save = (int) savechar;
884 static void lmsavechar (char c)
886 if (savechar == '\0') savechar = c;
889 llbuglit ("lmsavechar: override");
893 static int returnFloat (ctype ct, double f)
895 yylval.expr = exprNode_floatLiteral (f, ct, cstring_fromChars (yytext),
896 fileloc_decColumn (g_currentloc, tokLength));
901 static int returnInt (ctype ct, long i)
905 if (ctype_equal (ct, ctype_int))
909 c = context_typeofZero ();
913 c = context_typeofOne ();
917 yylval.expr = exprNode_numLiteral (c, cstring_fromChars (yytext),
918 fileloc_decColumn (g_currentloc, tokLength), i);
923 static int returnChar (char c)
925 yylval.expr = exprNode_charLiteral (c, cstring_fromChars (yytext),
926 fileloc_decColumn (g_currentloc, tokLength));
935 if (c != EOF && ((char)c == '\n'))
937 context_incLineno ();
943 static char macro_nextChar ()
945 static bool in_quote = FALSE, in_escape = FALSE, in_char = FALSE;
950 c = char_fromInt (ic);
952 if (!in_quote && !in_char && (c == '\\' || c == BEFORE_COMMENT_MARKER[0]))
956 while ((c = char_fromInt (lminput ())) != '\0' && c != '\n')
958 ; /* skip to newline */
961 context_incLineno ();
965 return macro_nextChar ();
972 else /* if (c == '@') */
974 llassert (FALSE); /*@i23@*/
975 if (handleLlSpecial () != BADTOK)
977 llerrorlit (FLG_SYNTAX, "Macro cannot use special syntax");
980 return macro_nextChar ();
983 else if (!in_escape && c == '\"')
985 in_quote = !in_quote;
987 else if (!in_escape && c == '\'')
991 else if ((in_quote || in_char) && c == '\\')
993 in_escape = !in_escape;
995 else if ((in_quote || in_char) && in_escape)
999 else if (!in_quote && c == '/')
1003 if ((c2 = char_fromInt (lminput ())) == '*')
1007 while ((c2 = char_fromInt (lminput ())) != '\0'
1008 && c2 != '\n' && c2 != '*')
1015 while ((c2 = char_fromInt (lminput ())) != '\0'
1028 llfatalerror (cstring_makeLiteral ("Macro: bad comment!"));
1032 return macro_nextChar ();
1036 /*** putchar does not work! why? puts to stdio...??! ***/
1044 ** keeps semantic comments
1047 static char macro_nextCharC ()
1049 static bool in_quote = FALSE, in_escape = FALSE, in_char = FALSE;
1052 c = char_fromInt (lminput ());
1054 if (!in_quote && !in_char && c == '\\')
1056 while ((c = char_fromInt (lminput ())) != '\0' && c != '\n')
1058 ; /* skip to newline */
1061 context_incLineno ();
1065 return macro_nextCharC ();
1072 else if (!in_escape && c == '\"')
1074 in_quote = !in_quote;
1076 else if (!in_escape && c == '\'')
1080 else if ((in_quote || in_char) && c == '\\')
1082 in_escape = !in_escape;
1084 else if ((in_quote || in_char) && in_escape)
1088 else if (!in_quote && c == '/')
1092 if ((c2 = char_fromInt (lminput ())) == '*')
1096 while ((c2 = char_fromInt (lminput ())) != '\0'
1097 && c2 != '\n' && c2 != '*')
1104 while ((c2 = char_fromInt (lminput ())) != '\0'
1117 llfatalerror (cstring_makeLiteral ("Macro: bad comment!"));
1121 return macro_nextCharC ();
1132 ** skips whitespace (handles line continuations)
1133 ** returns first non-whitespace character
1136 static char skip_whitespace ()
1140 while ((c = macro_nextChar ()) == ' ' || c == '\t')
1148 static void handleMacro ()
1150 cstring mac = cstring_undefined;
1154 while (currentColumn () > 2)
1156 mac = cstring_appendChar (mac, ' ');
1160 c = macro_nextCharC ();
1162 if (c >= '0' && c <= '9')
1166 for (i = 0; i < ((c - '0') + 1); i++)
1168 mac = cstring_appendChar (mac, ' ');
1176 while (((c = macro_nextCharC ()) != '\0') && (c != '\n'))
1178 mac = cstring_appendChar (mac, c);
1182 macrocode = tokenMacroCode (mac);
1184 if (macrocode == BADTOK && !isArtificial (mac))
1186 context_addMacroCache (mac);
1195 context_incLineno ();
1199 static bool processMacro (void)
1204 cstring fname = cstring_undefined;
1206 bool isspecfcn = FALSE;
1207 bool isiter = FALSE;
1208 bool skipparam = FALSE;
1209 bool isenditer = FALSE;
1210 bool unknownm = FALSE;
1211 bool hasParams = FALSE;
1212 bool emptyMacro = FALSE;
1213 char c = skip_whitespace ();
1214 fileloc loc = fileloc_noColumn (g_currentloc);
1216 /* are both of these necessary? what do they mean? */
1217 uentryList specparams = uentryList_undefined;
1218 uentryList pn = uentryList_undefined;
1220 context_resetMacroMissingParams ();
1222 if (c == '\0' || c == '\n')
1224 llcontbug (cstring_makeLiteral ("Bad macro"));
1229 fname = cstring_appendChar (fname, c);
1231 while ((c = macro_nextChar ()) != '(' && c != '\0'
1232 && c != ' ' && c != '\t' && c != '\n')
1234 fname = cstring_appendChar (fname, c);
1237 if (c == ' ' || c == '\t' || c == '\n')
1243 while (c == ' ' || c == '\t')
1245 c = macro_nextChar ();
1259 hasParams = (c == '(');
1261 if (usymtab_exists (fname))
1263 e2 = usymtab_lookupExpose (fname);
1264 ct = uentry_getType (e2);
1266 if (uentry_isCodeDefined (e2)
1267 && fileloc_isUser (uentry_whereDefined (e2)))
1271 message ("Macro %s already defined", fname),
1274 uentry_showWhereDefined (e2);
1275 uentry_clearDefined (e2);
1278 if (uentry_isFunction (e2))
1280 uentry_setType (e2, ctype_unknown);
1283 context_enterUnknownMacro (e2);
1287 context_enterConstantMacro (e2);
1292 if (uentry_isForward (e2) && uentry_isFunction (e2))
1299 ("Parameterized macro has no prototype or specification: %s ",
1304 uentry_setType (e2, ctype_unknown);
1305 uentry_setFunctionDefined (e2, loc);
1306 uentry_setUsed (e2, fileloc_undefined);
1307 context_enterUnknownMacro (e2);
1311 if (uentry_isIter (e2))
1314 specparams = uentry_getParams (e2);
1315 noparams = uentryList_size (specparams);
1316 uentry_setDefined (e2, loc);
1317 context_enterIterDef (e2);
1319 else if (uentry_isEndIter (e2))
1322 uentry_setDefined (e2, loc);
1323 context_enterIterEnd (e2); /* don't care about it now */
1324 /* but should parse like an iter! */
1326 else if (uentry_isConstant (e2))
1332 message ("Constant %s implemented as parameterized macro",
1336 uentry_showWhereSpecified (e2);
1337 uentry_setType (e2, ctype_unknown);
1338 uentry_makeConstantFunction (e2);
1339 uentry_setDefined (e2, g_currentloc);
1340 uentry_setFunctionDefined (e2, g_currentloc);
1341 context_enterUnknownMacro (e2);
1345 if (!uentry_isSpecified (e2))
1347 fileloc oloc = uentry_whereDeclared (e2);
1349 if (fileloc_isLib (oloc))
1353 else if (fileloc_isUndefined (oloc)
1354 || fileloc_isPreproc (oloc))
1359 (FLG_MACROCONSTDECL,
1361 ("Macro constant %q not declared",
1362 uentry_getName (e2)),
1366 else if (!fileloc_withinLines (oloc, loc, 2))
1367 { /* bogus! will give errors if there is too much whitespace */
1371 ("Macro constant name %s does not match name in "
1372 "previous constant declaration. This constant "
1373 "is declared at %q", fname,
1374 fileloc_unparse (oloc)),
1379 context_enterConstantMacro (e2);
1380 cstring_free (fname);
1386 else if (ctype_isFunction (ct))
1389 specparams = ctype_argsFunction (ct);
1390 noparams = uentryList_size (specparams);
1392 uentry_setFunctionDefined (e2, loc);
1393 context_enterMacro (e2);
1395 else if (uentry_isVar (e2))
1401 message ("Variable %s implemented as parameterized macro",
1405 uentry_showWhereSpecified (e2);
1406 uentry_setType (e2, ctype_unknown);
1407 uentry_makeVarFunction (e2);
1408 uentry_setDefined (e2, g_currentloc);
1409 uentry_setFunctionDefined (e2, g_currentloc);
1410 context_enterUnknownMacro (e2);
1414 uentry ucons = uentry_makeConstant (fname,
1417 if (uentry_isExpandedMacro (e2))
1425 message ("Variable %s implemented by a macro",
1429 uentry_showWhereSpecified (e2);
1433 uentry_setDefined (e2, loc);
1434 uentry_setUsed (ucons, loc);
1436 context_enterConstantMacro (ucons);
1437 uentry_markOwned (ucons);
1438 cstring_free (fname);
1444 if (uentry_isDatatype (e2))
1448 message ("Type implemented as macro: %x",
1449 uentry_getName (e2)),
1450 message ("A type is implemented using a macro definition. A "
1451 "typedef should be used instead."),
1455 /* Must exit scope (not sure why a new scope was entered?) */
1456 usymtab_quietExitScope (g_currentloc);
1457 uentry_setDefined (e2, g_currentloc);
1463 (message ("Unexpanded macro not function or constant: %q",
1464 uentry_unparse (e2)));
1465 uentry_setType (e2, ctype_unknown);
1469 uentry_makeVarFunction (e2);
1470 uentry_setDefined (e2, g_currentloc);
1471 uentry_setFunctionDefined (e2, g_currentloc);
1472 context_enterUnknownMacro (e2);
1483 /* evans 2001-09-09 - if it has params, assume a function */
1487 (FLG_MACROMATCHNAME,
1488 message ("Unexpanded macro %s does not match name of a declared "
1489 "function. The name used in the control "
1490 "comment on the previous line should match.",
1494 ce = uentry_makeFunction (fname, ctype_unknown,
1498 warnClause_undefined,
1500 uentry_setUsed (ce, loc); /* perhaps bogus? */
1501 e2 = usymtab_supEntryReturn (ce);
1502 context_enterUnknownMacro (e2);
1507 (FLG_MACROMATCHNAME,
1508 message ("Unexpanded macro %s does not match name of a constant "
1509 "or iter declaration. The name used in the control "
1510 "comment on the previous line should match. "
1511 "(Assuming macro defines a constant.)",
1515 ce = uentry_makeConstant (fname, ctype_unknown, fileloc_undefined);
1516 uentry_setUsed (ce, loc); /* perhaps bogus? */
1517 e2 = usymtab_supEntryReturn (ce);
1519 context_enterConstantMacro (e2);
1520 cstring_free (fname);
1526 /* in macros, ( must follow immediatetly after name */
1532 c = skip_whitespace ();
1534 while (c != ')' && c != '\0')
1537 bool suppress = context_inSuppressRegion ();
1538 cstring paramname = cstring_undefined;
1541 ** save the parameter location
1545 context_saveLocation ();
1548 while (c != ' ' && c != '\t' && c != ',' && c != '\0' && c != ')')
1550 paramname = cstring_appendChar (paramname, c);
1551 c = macro_nextChar ();
1554 if (c == ' ' || c == '\t') c = skip_whitespace ();
1558 c = macro_nextChar ();
1559 if (c == ' ' || c == '\t') c = skip_whitespace ();
1564 llfatalerror (cstring_makeLiteral
1565 ("Bad macro syntax: uentryList"));
1568 if ((isspecfcn || isiter) && (paramno < noparams)
1569 && !uentry_isElipsisMarker (uentryList_getN
1570 (specparams, paramno)))
1572 fileloc sloc = context_getSaveLocation ();
1573 uentry decl = uentryList_getN (specparams, paramno);
1576 param = uentry_nameCopy (paramname, decl);
1578 uentry_setParam (param);
1579 sr = sRef_makeParam (paramno, uentry_getType (param), stateInfo_makeLoc (sloc));
1581 if (sRef_getNullState (sr) == NS_ABSNULL)
1583 ctype pt = ctype_realType (uentry_getType (param));
1585 if (ctype_isUser (pt))
1587 uentry te = usymtab_getTypeEntrySafe (ctype_typeId (pt));
1589 if (uentry_isValid (te))
1591 sRef_setStateFromUentry (sr, te);
1596 sRef_setNullState (sr, NS_UNKNOWN, sloc);
1600 uentry_setSref (param, sr);
1601 uentry_setDeclaredForceOnly (param, sloc);
1603 skipparam = isiter && uentry_isOut (uentryList_getN (specparams, paramno));
1607 fileloc sloc = context_getSaveLocation ();
1609 param = uentry_makeVariableSrefParam
1610 (paramname, ctype_unknown, fileloc_copy (sloc),
1611 sRef_makeParam (paramno, ctype_unknown, stateInfo_makeLoc (sloc)));
1612 DPRINTF (("Unknown param: %s", uentry_unparseFull (param)));
1613 cstring_free (paramname);
1615 sRef_setPosNull (uentry_getSref (param), sloc);
1616 uentry_setDeclaredForce (param, sloc);
1619 fileloc_free (sloc);
1624 llassert (!uentry_isElipsisMarker (param));
1628 sRef_makeUnsafe (uentry_getSref (param));
1631 pn = uentryList_add (pn, uentry_copy (param));
1632 usymtab_supEntry (param);
1636 /* don't add param */
1637 uentry_free (param);
1642 (void) macro_nextChar ();
1643 c = skip_whitespace ();
1651 if (isspecfcn || isiter)
1653 if (paramno != noparams && noparams >= 0)
1659 message ("Macro %s specified with %d args, defined with %d",
1660 fname, noparams, paramno),
1663 uentry_showWhereSpecified (e2);
1664 uentry_resetParams (e2, pn);
1669 uentry_resetParams (e2, pn);
1676 ** the form should be:
1678 ** # define newname oldname
1679 ** where oldname refers to a function matching the specification
1685 sRef_setGlobalScope ();
1686 usymtab_supGlobalEntry (uentry_makeVariableLoc (fname, ctype_unknown));
1687 sRef_clearGlobalScope ();
1691 context_setMacroMissingParams ();
1696 /* context_setuentryList (pn); */
1697 usymtab_enterScope ();
1700 cstring_free (fname);
1705 static bool handleSpecial (char *yyt)
1707 char *l = mstring_create (MAX_NAME_LENGTH);
1713 strcpy (l, yyt + 1);
1715 /* Need to safe original l for deallocating. */
1718 l += strlen (yyt) - 1;
1720 while ((c = char_fromInt (lminput ())) != '\n' && c != '\0')
1726 olc = cstring_fromChars (ol);
1728 if (cstring_equalPrefixLit (olc, "pragma"))
1730 char *pname = mstring_create (longUnsigned_fromInt (MAX_PRAGMA_LEN));
1731 char *opname = pname;
1732 char *ptr = ol + 6; /* pragma is six characters, plus space */
1736 /* skip whitespace */
1737 while (((c = *ptr) != '\0') && isspace (c))
1743 while (((c = *ptr) != '\0') && !isspace (c))
1747 if (len > MAX_PRAGMA_LEN)
1758 if (len == PRAGMA_LEN_EXPAND
1759 && mstring_equal (opname, PRAGMA_EXPAND))
1761 cstring exname = cstring_undefined;
1765 while (((c = *ptr) != '\0') && !isspace (c))
1767 exname = cstring_appendChar (exname, c);
1772 ue = usymtab_lookupExposeGlob (exname);
1774 if (uentry_isExpandedMacro (ue))
1776 if (fileloc_isPreproc (uentry_whereDefined (ue)))
1778 fileloc_setColumn (g_currentloc, 1);
1779 uentry_setDefined (ue, g_currentloc);
1783 cstring_free (exname);
1786 else if (cstring_equalPrefixLit (olc, "ident"))
1788 /* Some pre-processors will leave these in the code. Ignore rest of line */
1791 ** Yuk...Win32 filenames can have spaces in them...we need to read
1792 ** to the matching end quote.
1794 else if ((sscanf (ol, "line %d \"", &lineno) == 1)
1795 || (sscanf (ol, " %d \"", &lineno) == 1))
1801 /*@access cstring@*/
1802 while (*tmp != '\"' && *tmp != '\0')
1807 llassert (*tmp == '\"');
1813 while (*tmp != '\"' && *tmp != '\0')
1818 llassert (*tmp == '\"');
1822 # if defined(OS2) || defined(MSDOS) || defined(WIN32)
1825 ** DOS-like path delimiters get delivered in pairs, something like
1826 ** \"..\\\\file.h\", so we have to make it normal again. We do NOT
1827 ** remove the pre dirs yet as we usually specify tmp paths relative
1828 ** to the current directory, so tmp files would not get found in
1829 ** the hash table. If this method fails we try it again later.
1836 ** Skip past the drive marker.
1839 if (strchr (stmp, ':') != NULL)
1841 stmp = strchr (stmp, ':') + 1;
1844 while ((stmp = strchr (stmp, CONNECTCHAR)) != NULL )
1846 if (*(stmp+1) == CONNECTCHAR)
1848 memmove (stmp, stmp+1, strlen (stmp));
1854 fid = fileTable_lookupBase (context_fileTable (), fname);
1855 if (!(fileId_isValid (fid)))
1857 fname = removePreDirs (fname);
1858 fid = fileTable_lookupBase (context_fileTable (), fname);
1861 # else /* !defined(OS2) && !defined(MSDOS) */
1862 fname = removePreDirs (fname);
1863 fid = fileTable_lookupBase (context_fileTable (), fname);
1864 # endif /* !defined(OS2) && !defined(MSDOS) */
1866 if (!(fileId_isValid (fid)))
1868 if (context_inXHFile ())
1870 fid = fileTable_addXHFile (context_fileTable (), fname);
1872 else if (isHeaderFile (fname))
1874 fid = fileTable_addHeaderFile (context_fileTable (), fname);
1878 fid = fileTable_addFile (context_fileTable (), fname);
1882 setFileLine (fid, lineno);
1883 /*@noaccess cstring@*/
1885 else if ((sscanf (ol, "line %d", &lineno) == 1)
1886 || (sscanf (ol, " %d", &lineno) == 1))
1888 setLine (lineno); /* next line is <cr> */
1892 if (mstring_equal (ol, "")) {
1893 DPRINTF (("Empty pp command!"));
1895 ** evs 2000-05-16: This is a horrible kludge, to get around a bug (well, difficulty) in the pre-processor.
1896 ** We handle a plain # in the input file, by echoing it, and ignoring it in the post-pp-file.
1902 (FLG_UNRECOGDIRECTIVE,
1903 message ("Unrecognized pre-processor directive: #%s",
1904 cstring_fromChars (ol)),
1909 return FALSE; /* evans 2001-12-30: was: TRUE; */
1916 static int handleLlSpecial ()
1921 char *s = mstring_createEmpty ();
1927 loc = fileloc_copy (g_currentloc);
1928 DPRINTF (("Handle special: %s", fileloc_unparse (loc)));
1930 while (((ic = ninput ()) != 0) && isalpha (ic))
1933 s = mstring_append (s, c);
1937 DPRINTF (("Read: %s / %s", s, fileloc_unparse (g_currentloc)));
1940 if (charsread == 0 && ic == (int) AFTER_COMMENT_MARKER[0])
1944 llassert (ic == AFTER_COMMENT_MARKER[1]);
1950 return QNOMODS; /* special token no modifications token */
1954 DPRINTF (("Coment marker: %s", os));
1955 tok = commentMarkerToken (cstring_fromChars (os));
1959 tokLength = charsread;
1966 DPRINTF (("Not a comment marker..."));
1967 /* Add rest of the comment */
1969 if (ic != 0 && ic != EOF)
1973 s = mstring_append (s, c);
1976 while (((ic = ninput ()) != 0) && (ic != EOF)
1977 && (ic != AFTER_COMMENT_MARKER[0]))
1981 /* evans 2001-09-01 added to prevent assertion failures for uncloses syntactic comments */
1984 hasnl = TRUE; /* This prevents tokLength from being set later. */
1989 message ("Likely parse error: syntactic comment token spans multiple lines: %s",
1990 cstring_fromChars (s)),
1994 s = mstring_append (s, c);
1999 DPRINTF (("Read: %s / %s", s, fileloc_unparse (g_currentloc)));
2001 if (ic == AFTER_COMMENT_MARKER[0])
2004 llassert ((char) nc == AFTER_COMMENT_MARKER[1]);
2010 while (*s == ' ' || *s == '\t' || *s == '\n')
2015 if (*s == '-' || *s == '+' || *s == '=') /* setting flags */
2019 while (c == '-' || c == '+' || c == '=')
2021 ynm set = ynm_fromCodeChar (c);
2026 thisflag = cstring_fromChars (s);
2028 while ((c = *s) != '\0' && (c != '-') && (c != '=')
2029 && (c != '+') && (c != ' ') && (c != '\t') && (c != '\n'))
2036 if (!context_getFlag (FLG_NOCOMMENTS))
2038 cstring flagname = thisflag;
2039 flagcode fflag = flags_identifyFlag (flagname);
2041 if (flagcode_isSkip (fflag))
2045 else if (flagcode_isInvalid (fflag))
2047 if (isMode (flagname))
2049 if (ynm_isMaybe (set))
2054 ("Semantic comment attempts to restore flag %s. "
2055 "A mode flag cannot be restored.",
2060 context_setMode (flagname);
2066 (FLG_UNRECOGFLAGCOMMENTS,
2067 message ("Unrecognized option in semantic comment: %s",
2072 else if (flagcode_isGlobalFlag (fflag))
2077 ("Semantic comment attempts to set global flag %s. "
2078 "A global flag cannot be set locally.",
2084 context_fileSetFlag (fflag, set);
2086 if (flagcode_hasArgument (fflag))
2088 if (ynm_isMaybe (set))
2093 ("Semantic comment attempts to restore flag %s. "
2094 "A flag for setting a value cannot be restored.",
2099 { /* cut-and-pastied from llmain...blecch */
2100 cstring extra = cstring_undefined;
2106 rest = mstring_copy (s);
2110 while ((rchar = *rest) != '\0'
2111 && (isspace (rchar)))
2117 while ((rchar = *rest) != '\0'
2118 && !isspace (rchar))
2120 extra = cstring_appendChar (extra, rchar);
2127 if (cstring_isUndefined (extra))
2132 ("Flag %s (in semantic comment) must be followed by an argument",
2133 flagcode_unparse (fflag)));
2139 if (flagcode_hasNumber (fflag))
2141 setValueFlag (fflag, extra);
2143 else if (flagcode_hasChar (fflag))
2145 setValueFlag (fflag, extra);
2147 else if (flagcode_hasString (fflag))
2149 setStringFlag (fflag, extra);
2166 while ((c == ' ') || (c == '\t') || (c == '\n'))
2172 if (context_inHeader () && !isArtificial (cstring_fromChars (os)))
2174 DPRINTF (("Here adding comment: %s", os));
2175 context_addComment (cstring_fromCharsNew (os));
2187 annotationInfo ainfo;
2189 while (*s != '\0' && *s != ' ' && *s != '\t' && *s != '\n')
2201 t = cstring_toCharsSafe (cstring_downcase (cstring_fromChars (t)));
2202 macrocode = tokenMacroCode (cstring_fromChars (t));
2204 if (macrocode != BADTOK)
2206 tokLength = hasnl ? 0 : mstring_length (t);
2212 if (macrocode == SKIPTOK)
2220 ainfo = context_lookupAnnotation (cstring_fromChars (os));
2222 if (annotationInfo_isDefined (ainfo)) {
2223 DPRINTF (("Found annotation: %s", annotationInfo_unparse (ainfo)));
2224 /*@i324@*/ yylval.annotation = ainfo;
2232 if (context_inHeader ())
2239 if ((context_inMacro () || context_inGlobalContext ())
2240 && macrocode != SKIPTOK
2241 && !isArtificial (cstring_fromChars (os)))
2243 DPRINTF (("Add comment: %s", os));
2244 context_addComment (cstring_fromCharsNew (os));
2257 if (mstring_equal (t, "ignore"))
2259 if (!context_getFlag (FLG_NOCOMMENTS))
2261 context_enterSuppressRegion ();
2264 else if ((*t == 'i' || *t == 't')
2265 && (*(t + 1) == '\0'))
2267 if (!context_getFlag (FLG_NOCOMMENTS)
2268 && (*t == 'i' || context_getFlag (FLG_TMPCOMMENTS)))
2270 context_enterSuppressLine (-1); /* infinite suppression */
2273 else if (((*t == 'i') || (*t == 't'))
2274 && ((*(t + 1) >= '0' && *(t + 1) <= '9')))
2276 bool tmpcomment = (*t == 't');
2278 char *tt = t; /* don't mangle t, since it is free'd */
2281 if (lc >= '0' && lc <= '9')
2283 val = (int)(lc - '0');
2286 while (lc >= '0' && lc <= '9')
2295 if (!context_getFlag (FLG_NOCOMMENTS)
2296 && (!tmpcomment || context_getFlag (FLG_TMPCOMMENTS)))
2298 context_enterSuppressLine (val);
2301 else if (mstring_equal (t, "end"))
2303 if (!context_getFlag (FLG_NOCOMMENTS))
2305 context_exitSuppressRegion ();
2308 else if (mstring_equal (t, "notfunction"))
2310 ; /* handled by pcpp */
2312 else if (mstring_equal (t, "access"))
2318 while ((c = *s) && (c == ' ' || c == '\t' || c == '\n'))
2328 tname = cstring_fromChars (s);
2330 while ((c = *s) != '\0' && c != ' '
2331 && c != '\t' && c != '\n' && c != ',')
2338 DPRINTF (("Access %s", tname));
2340 if (!context_getFlag (FLG_NOCOMMENTS)
2341 && !context_getFlag (FLG_NOACCESS))
2343 if (usymtab_existsType (tname))
2345 typeId uid = usymtab_getTypeId (tname);
2346 uentry ue = usymtab_getTypeEntry (uid);
2348 if (uentry_isAbstractDatatype (ue))
2350 context_addFileAccessType (uid);
2351 DPRINTF (("Adding access to: %s / %d", tname, uid));
2358 ("Non-abstract type %s used in access comment",
2365 if (!(context_inSuppressRegion ()
2366 || context_inSuppressZone (g_currentloc)))
2371 ("Unrecognized type %s used in access comment",
2383 if (c != ',' && c != ' ')
2389 else if (mstring_equal (t, "noaccess"))
2396 while ((lc = *s) && (lc == ' ' || lc == '\t' || lc == '\n'))
2406 tname = cstring_fromChars (s);
2408 while ((lc = *s) != '\0' && lc != ' ' && lc != '\t'
2409 && lc != '\n' && lc != ',')
2416 if (!context_getFlag (FLG_NOCOMMENTS)
2417 && !context_getFlag (FLG_NOACCESS))
2419 if (usymtab_existsType (tname))
2421 typeId tuid = usymtab_getTypeId (tname);
2423 if (context_couldHaveAccess (tuid))
2425 DPRINTF (("Removing access: %s", tname));
2426 context_removeFileAccessType (tuid);
2430 if (!(context_inSuppressRegion ()
2431 || context_inSuppressZone (g_currentloc)))
2433 uentry ue = usymtab_getTypeEntry (tuid);
2435 if (uentry_isAbstractDatatype (ue))
2440 ("Non-accessible abstract type %s used in noaccess comment",
2449 ("Non-abstract type %s used in noaccess comment",
2458 if (!(context_inSuppressRegion ()
2459 || context_inSuppressZone (g_currentloc)))
2464 ("Unrecognized type %s used in noaccess comment",
2476 if (lc != ',' && lc != ' ')
2484 voptgenerror (FLG_UNRECOGCOMMENTS,
2485 message ("Semantic comment unrecognized: %s",
2486 cstring_fromChars (os)), loc);
2497 static /*@only@*/ cstring makeIdentifier (char *s)
2499 char *c = mstring_create (size_toInt (strlen (s)) + 1);
2500 cstring id = cstring_fromChars (c);
2502 while (isalnum (*s) || (*s == '_') || (*s == '$'))
2511 /*@observer@*/ /*@dependent@*/ uentry coerceId (cstring cn)
2513 if (!(usymtab_exists (cn)))
2515 fileloc loc = fileloc_createExternal ();
2518 ** We need to put this in a global scope, otherwise the sRef will be deallocated.
2521 uentry ce = uentry_makeUnrecognized (cn, loc);
2523 if (!context_inIterEnd ())
2527 message ("Unrecognized (possibly system) identifier: %q",
2528 uentry_getName (ce)),
2535 return (usymtab_lookup (cn));
2539 ** like, coerceId, but doesn't supercede for iters
2542 /*@observer@*/ uentry coerceIterId (cstring cn)
2544 if (!(usymtab_exists (cn)))
2546 return uentry_undefined;
2549 return (usymtab_lookup (cn));
2552 /*@observer@*/ cstring LastIdentifier ()
2554 return (lastidprocessed);
2557 static int processIdentifier (cstring id)
2561 if (context_getFlag (FLG_GRAMMAR))
2563 lldiagmsg (message ("Process identifier: %s", id));
2566 context_clearJustPopped ();
2567 lastidprocessed = id;
2569 if (context_inFunctionHeader ())
2571 int tok = commentMarkerToken (id);
2572 DPRINTF (("in function decl..."));
2580 tok = tokenMacroCode (id);
2588 annotationInfo ainfo;
2590 if (expectingMetaStateName)
2592 metaStateInfo msinfo = context_lookupMetaStateInfo (id);
2594 if (metaStateInfo_isDefined (msinfo))
2596 yylval.msinfo = msinfo;
2597 return METASTATE_NAME;
2601 DPRINTF (("Not meta state name: %s", cstring_toCharsSafe (id)));
2605 ainfo = context_lookupAnnotation (id);
2607 if (annotationInfo_isDefined (ainfo))
2609 DPRINTF (("Found annotation: %s", annotationInfo_unparse (ainfo)));
2610 /*@i324@*/ yylval.annotation = ainfo;
2615 DPRINTF (("Not annotation: %s", id));
2621 /* Consider handling: Defined by C99 as static const char __func__[] */
2623 if (context_getFlag (FLG_GNUEXTENSIONS))
2627 if (cstring_equalLit (id, "__stdcall")
2628 || cstring_equalLit (id, "__cdecl")
2629 || cstring_equalLit (id, "__extension__"))
2633 else if (cstring_equalLit (id, "__volatile__"))
2637 else if (cstring_equalLit (id, "__signed"))
2641 else if (cstring_equalLit (id, "__unsigned"))
2645 else if (cstring_equalLit (id, "__const__"))
2649 else if (cstring_equalLit (id, "__alignof__"))
2651 tok = CALIGNOF; /* alignof is parsed like sizeof */
2653 else if (cstring_equalLit (id, "__FUNCTION__")
2654 || cstring_equalLit (id, "__PRETTY_FUNCTION__"))
2656 /* These tokens hold the name of the current function as strings */
2657 /* evans 2001-12-30: changed from exprNode_stringLiteral; bug reported by Jim Zelenka. */
2658 yylval.expr = exprNode_makeConstantString (id, fileloc_copy (g_currentloc));
2660 lastWasString = TRUE;
2664 else if (cstring_equalLit (id, "__attribute__")
2665 || cstring_equalLit (id, "__asm__")
2666 || cstring_equalLit (id, "_asm")
2667 || cstring_equalLit (id, "__asm")
2668 || cstring_equalLit (id, "__declspec"))
2671 bool useparens = FALSE;
2672 bool usebraces = FALSE;
2673 bool inquote = FALSE;
2674 bool inescape = FALSE;
2677 while ((ic = input ()) != EOF)
2684 else if (ic == '\\')
2688 else if (ic == '\"')
2724 else if (ic == ')' && useparens)
2727 if (depth == 0) break;
2729 else if (ic == '}' && usebraces)
2732 if (depth == 0) break;
2735 && !usebraces && !useparens
2736 && cstring_equalLit (id, "__asm"))
2739 ** We need this because some MS VC++ include files
2740 ** have __asm mov ... }
2741 ** Its a kludge, but otherwise would need to parse
2750 context_incLineno ();
2752 if (cstring_equalLit (id, "__asm")
2753 && !useparens && !usebraces)
2760 llassert ((useparens && ic == ')')
2761 || (usebraces && ic == '}')
2762 || (!useparens && !usebraces));
2766 else if (cstring_equalLit (id, "inline")
2767 || cstring_equalLit (id, "__inline")
2768 || cstring_equalLit (id, "_inline")
2769 || cstring_equalLit (id, "__inline__"))
2780 le = usymtab_lookupSafe (id);
2782 /*@-dependenttrans@*/
2784 if (uentry_isIter (le))
2786 /*@i32@*/ yylval.entry = le;
2789 else if (uentry_isEndIter (le))
2791 /*@i32@*/ yylval.entry = le;
2792 return (ITER_ENDNAME);
2794 else if (uentry_isUndefined (le))
2798 /* avoid parse errors for certain system built ins */
2800 if (g_expectingTypeName && (cstring_firstChar (id) == '_')
2801 && (cstring_secondChar (id) == '_'))
2803 return (TYPE_NAME_OR_ID);
2806 return (NEW_IDENTIFIER);
2808 else if (!uentry_isDeclared (le) && !uentry_isCodeDefined (le))
2810 if (uentry_isDatatype (le))
2813 return (NEW_IDENTIFIER);
2817 /*@i32@*/ yylval.entry = le;
2818 return (IDENTIFIER);
2821 else if (uentry_isDatatype (le))
2823 if (!g_expectingTypeName)
2827 return (NEW_IDENTIFIER);
2831 yylval.ctyp = uentry_getAbstractType (le);
2833 uentry_setUsed (le, g_currentloc);
2839 /*@i32@*/ yylval.entry = le;
2840 return (IDENTIFIER);
2843 /*@=dependenttrans@*/
2846 static bool processHashIdentifier (/*@only@*/ cstring id)
2848 if (context_inMacro () || context_inIterDef () ||
2849 context_inIterEnd ())
2853 context_clearJustPopped ();
2855 lastidprocessed = id;
2856 le = usymtab_lookupSafe (id);
2858 if (uentry_isParam (le) || uentry_isRefParam (le))
2870 ** Will be handled by handleLlSpecial
2879 static /*@only@*/ exprNode processString ()
2883 char *nl = strchr (yytext, '\n');
2884 cstring ns = cstring_fromCharsNew (yytext);
2888 loc = fileloc_copy (g_currentloc);
2889 addColumn (cstring_length (ns));
2895 loc = fileloc_copy (g_currentloc);
2897 context_incLineno ();
2899 while ((nl = strchr ((nl + 1), '\n')) != NULL)
2901 context_incLineno ();
2907 res = exprNode_stringLiteral (ns, loc);
2912 ** process a wide character string L"...."
2915 static /*@only@*/ exprNode processWideString ()
2919 char *nl = strchr (yytext, '\n');
2922 llassert (*yytext == 'L');
2925 ns = cstring_fromCharsNew (yytext);
2929 loc = fileloc_copy (g_currentloc);
2930 addColumn (cstring_length (ns));
2936 loc = fileloc_copy (g_currentloc);
2938 context_incLineno ();
2940 while ((nl = strchr ((nl + 1), '\n')) != NULL)
2942 context_incLineno ();
2947 res = exprNode_wideStringLiteral (ns, loc);
2957 llassert (*yytext != '\0');
2958 fchar = *(yytext + 1);
2959 if (fchar != '\\') return fchar;
2961 next = *(yytext + 2);
2965 case 'n': return '\n';
2966 case 't': return '\t';
2967 case '\"': return '\"';
2968 case '\'': return '\'';
2969 case '\\': return '\\';
2970 default: return '\0';
2975 double processFloat ()
2977 double ret = atof (yytext);
2988 llassert (yytext[0] == '0'
2989 && (yytext[1] == 'X' || yytext[1] == 'x'));
2991 while (yytext[index] != '\0') {
2993 char c = yytext[index];
2995 if (c >= '0' && c <= '9') {
2996 tval = (int) c - (int) '0';
2997 } else if (c >= 'A' && c <= 'F') {
2998 tval = (int) c - (int) 'A' + 10;
2999 } else if (c >= 'a' && c <= 'f') {
3000 tval = (int) c - (int) 'a' + 10;
3001 } else if (c == 'U' || c == 'L' || c == 'u' || c == 'l') {
3003 while (yytext[index] != '\0') {
3004 if (c == 'U' || c == 'L' || c == 'u' || c == 'l') {
3009 message ("Invalid character (%c) following specifier in hex constant: %s",
3010 c, cstring_fromChars (yytext)),
3020 message ("Invalid character (%c) in hex constant: %s",
3021 c, cstring_fromChars (yytext)),
3026 val = (val * 16) + tval;
3030 DPRINTF (("Hex constant: %s = %ld", yytext, val));
3035 long processOctal ()
3040 llassert (yytext[0] == '0' && yytext[1] != 'X' && yytext[1] != 'x');
3042 while (yytext[index] != '\0') {
3044 char c = yytext[index];
3046 if (c >= '0' && c <= '7') {
3047 tval = (int) c - (int) '0';
3048 } else if (c == 'U' || c == 'L' || c == 'u' || c == 'l') {
3050 while (yytext[index] != '\0') {
3051 if (c == 'U' || c == 'L' || c == 'u' || c == 'l') {
3056 message ("Invalid character (%c) following specifier in octal constant: %s",
3057 c, cstring_fromChars (yytext)),
3067 message ("Invalid character (%c) in octal constant: %s",
3068 c, cstring_fromChars (yytext)),
3073 val = (val * 8) + tval;
3077 DPRINTF (("Octal constant: %s = %ld", yytext, val));
3084 return (atol (yytext));
3088 processSpec (int tok)
3090 size_t length = strlen (yytext);
3094 setTokLengthT (length);
3100 context_saveLocation ();
3101 setTokLengthT (length);
3102 return (processIdentifier (makeIdentifier (yytext)));
3106 void cscanner_expectingMetaStateName ()
3108 llassert (!expectingMetaStateName);
3109 llassert (context_inFunctionHeader ());
3110 expectingMetaStateName = TRUE;
3113 void cscanner_clearExpectingMetaStateName ()
3115 llassert (expectingMetaStateName);
3116 expectingMetaStateName = FALSE;