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 "relnull" { return (processSpec (QRELNULL)); }
292 "reldef" { return (processSpec (QRELDEF)); }
293 "exposed" { return (processSpec (QEXPOSED)); }
294 "newref" { return (processSpec (QNEWREF)); }
295 "tempref" { return (processSpec (QTEMPREF)); }
296 "killref" { return (processSpec (QKILLREF)); }
297 "refcounted" { return (processSpec (QREFCOUNTED)); }
298 "checked" { return (processSpec (QCHECKED)); }
299 "checkmod" { return (processSpec (QCHECKMOD)); }
300 "checkedstrict" { return (processSpec (QCHECKEDSTRICT)); }
301 "unchecked" { return (processSpec (QUNCHECKED)); }
302 "only" { return (processSpec (QONLY)); }
303 "owned" { return (processSpec (QOWNED)); }
304 "observer" { return (processSpec (QOBSERVER)); }
305 "dependent" { return (processSpec (QDEPENDENT)); }
306 "unused" { return (processSpec (QUNUSED)); }
307 "external" { return (processSpec (QEXTERNAL)); }
308 "sef" { return (processSpec (QSEF)); }
309 "shared" { return (processSpec (QSHARED)); }
310 "yield" { return (processSpec (QYIELD)); }
311 "undef" { return (processSpec (QUNDEF)); }
312 "killed" { return (processSpec (QKILLED)); }
313 "nullterminated" { return (processSpec (QNULLTERMINATED));}
314 "MaxSet" { return (processSpec (QMAXSET));}
315 "MaxRead" { return (processSpec (QMAXREAD));}
316 "maxSet" { return (processSpec (QMAXSET));}
317 "maxRead" { return (processSpec (QMAXREAD));}
319 {Letter}({Letter}|{Digit})* { int tok;
320 context_saveLocation ();
321 setTokLength (longUnsigned_toInt (mstring_length (yytext)));
322 tok = processIdentifier (makeIdentifier (yytext));
328 0[xX]{H}+ { setTokLengthT (mstring_length (yytext));
329 RETURN_INT (ctype_int, processHex ()); /* evs 2000-05-17 was ctype_uint */
331 0[xX]{H}+{L} { setTokLengthT (mstring_length (yytext));
332 RETURN_INT (ctype_lint, processHex ()); }
333 0[xX]{H}+{L}{L} { setTokLengthT (mstring_length (yytext));
334 RETURN_INT (ctype_llint, processHex ()); }
335 0[xX]{H}+{U} { setTokLengthT (mstring_length (yytext));
336 RETURN_INT (ctype_uint, processHex ()); }
337 0[xX]{H}+{ULSuffix} { setTokLengthT (mstring_length (yytext));
338 RETURN_INT (ctype_ulint, processHex ()); }
339 0[xX]{H}+{U}{L}{L} { setTokLengthT (mstring_length (yytext));
340 RETURN_INT (ctype_ullint, processHex ()); }
341 0[xX]{H}+{L}{L}{U} { setTokLengthT (mstring_length (yytext));
342 RETURN_INT (ctype_ullint, processHex ()); }
343 0{Digit}+ { setTokLengthT (mstring_length (yytext));
344 RETURN_INT (ctype_int, processOctal ()); }
345 0{Digit}+{U} { setTokLengthT (mstring_length (yytext));
346 RETURN_INT (ctype_uint, processOctal ()); }
347 0{Digit}+{L} { setTokLengthT (mstring_length (yytext));
348 RETURN_INT (ctype_lint, processOctal ()); }
349 0{Digit}+{L}{L} { setTokLengthT (mstring_length (yytext));
350 RETURN_INT (ctype_llint, processOctal ()); }
351 0{Digit}+{ULSuffix} { setTokLengthT (mstring_length (yytext));
352 RETURN_INT (ctype_ulint, processOctal ()); }
353 0{Digit}+{U}{L}{L} { setTokLengthT (mstring_length (yytext));
354 RETURN_INT (ctype_ullint, processOctal ()); }
355 0{Digit}+{L}{L}{U} { setTokLengthT (mstring_length (yytext));
356 RETURN_INT (ctype_ullint, processOctal ()); }
357 {Digit}+ { setTokLengthT (mstring_length (yytext));
358 RETURN_INT (ctype_int, processDec ()); }
359 {Digit}+{U} { setTokLengthT (mstring_length (yytext));
360 RETURN_INT (ctype_uint, processDec ()); }
361 {Digit}+{L} { setTokLengthT (mstring_length (yytext));
362 RETURN_INT (ctype_lint, processDec ()); }
363 {Digit}+{L}{L} { setTokLengthT (mstring_length (yytext));
364 RETURN_INT (ctype_llint, processDec ()); }
365 {Digit}+{ULSuffix} { setTokLengthT (mstring_length (yytext));
366 RETURN_INT (ctype_ulint, processDec ()); }
367 {Digit}+{U}{L}{L} { setTokLengthT (mstring_length (yytext));
368 RETURN_INT (ctype_ullint, processDec ()); }
369 {Digit}+{L}{L}{U} { setTokLengthT (mstring_length (yytext));
370 RETURN_INT (ctype_ullint, processDec ()); }
371 '(\\.|[^\\'])+' { setTokLengthT (mstring_length (yytext));
372 RETURN_CHAR (processChar ()); }
373 L'(\\.|[^\\'])+' { setTokLengthT (mstring_length (yytext));
374 RETURN_CHAR (processChar ()); }
375 {Digit}+{E}[fF] { setTokLengthT (mstring_length (yytext));
376 RETURN_FLOAT (ctype_float, processFloat ()); }
377 {Digit}+{E}[lL] { setTokLengthT (mstring_length (yytext));
378 RETURN_FLOAT (ctype_ldouble, processFloat ()); }
379 {Digit}+{E} { setTokLengthT (mstring_length (yytext));
380 RETURN_FLOAT (ctype_double, processFloat ()); }
382 {Digit}*"."{Digit}+({E})?[fF] { setTokLengthT (mstring_length (yytext));
383 RETURN_FLOAT (ctype_float, processFloat ()); }
384 {Digit}*"."{Digit}+({E})?[lL] { setTokLengthT (mstring_length (yytext));
385 RETURN_FLOAT (ctype_ldouble, processFloat ()); }
386 {Digit}*"."{Digit}+({E})? { setTokLengthT (mstring_length (yytext));
387 RETURN_FLOAT (ctype_double, processFloat ()); }
389 {Digit}+"."{Digit}*({E})?[fF] { setTokLengthT (mstring_length (yytext));
390 RETURN_FLOAT (ctype_float, processFloat ()); }
391 {Digit}+"."{Digit}*({E})?[lL] { setTokLengthT (mstring_length (yytext));
392 RETURN_FLOAT (ctype_ldouble, processFloat ()); }
393 {Digit}+"."{Digit}*({E})? { setTokLengthT (mstring_length (yytext));
394 RETURN_FLOAT (ctype_double, processFloat ()); }
396 ">>=" { setTokLength (3); RETURN_TOK (RIGHT_ASSIGN); }
397 "<<=" { setTokLength (3); RETURN_TOK (LEFT_ASSIGN); }
398 "+=" { setTokLength (2); RETURN_TOK (ADD_ASSIGN); }
399 "-=" { setTokLength (2); RETURN_TOK (SUB_ASSIGN); }
400 "*=" { setTokLength (2); RETURN_TOK (MUL_ASSIGN); }
401 "/=" { setTokLength (2); RETURN_TOK (DIV_ASSIGN); }
402 "%=" { setTokLength (2); RETURN_TOK (MOD_ASSIGN); }
403 "&=" { setTokLength (2); RETURN_TOK (AND_ASSIGN); }
404 "^=" { setTokLength (2); RETURN_TOK (XOR_ASSIGN); }
405 "|=" { setTokLength (2); RETURN_TOK (OR_ASSIGN); }
406 ">>" { setTokLength (2); RETURN_TOK (RIGHT_OP); }
407 "<<" { setTokLength (2); RETURN_TOK (LEFT_OP); }
408 "++" { setTokLength (2); RETURN_TOK (INC_OP); }
409 "--" { setTokLength (2); RETURN_TOK (DEC_OP); }
410 "->" { setTokLength (2); RETURN_TOK (ARROW_OP); }
411 "&&" { setTokLength (2); RETURN_TOK (AND_OP); }
412 "||" { setTokLength (2); RETURN_TOK (OR_OP); }
413 "<=" { setTokLength (2); RETURN_TOK (LE_OP); }
414 ">=" { setTokLength (2); RETURN_TOK (GE_OP); }
415 "==" { setTokLength (2); RETURN_TOK (EQ_OP); }
416 "!=" { setTokLength (2); RETURN_TOK (NE_OP); }
417 ";" { setTokLength (1); RETURN_TOK (TSEMI); }
418 "{" { setTokLength (1); RETURN_TOK (TLBRACE); }
419 "}" { setTokLength (1); RETURN_TOK (TRBRACE); }
420 "," { setTokLength (1); RETURN_TOK (TCOMMA); }
421 ":" { setTokLength (1); RETURN_TOK (TCOLON); }
422 "=" { setTokLength (1); RETURN_TOK (TASSIGN); }
423 "(" { setTokLength (1); RETURN_TOK (TLPAREN); }
424 ")" { setTokLength (1); RETURN_TOK (TRPAREN); }
425 "[" { setTokLength (1); RETURN_TOK (TLSQBR); }
426 "]" { setTokLength (1); RETURN_TOK (TRSQBR); }
427 "." { setTokLength (1); RETURN_TOK (TDOT); }
428 "&" { setTokLength (1); RETURN_TOK (TAMPERSAND); }
429 "!" { setTokLength (1); RETURN_TOK (TEXCL); }
432 "~" { setTokLength (1); RETURN_TOK (TTILDE); }
433 "-" { setTokLength (1); RETURN_TOK (TMINUS); }
434 "+" { setTokLength (1); RETURN_TOK (TPLUS); }
435 "*" { setTokLength (1); RETURN_TOK (TMULT); }
436 "/" { setTokLength (1); RETURN_TOK (TDIV); }
437 "%" { setTokLength (1); RETURN_TOK (TPERCENT); }
438 "<" { setTokLength (1); RETURN_TOK (TLT); }
439 ">" { setTokLength (1); RETURN_TOK (TGT); }
440 "^" { setTokLength (1); RETURN_TOK (TCIRC); }
441 "|" { setTokLength (1); RETURN_TOK (TBAR); }
442 "?" { setTokLength (1); RETURN_TOK (TQUEST); }
445 "/\\" { setTokLength (1); RETURN_TOK (TCAND); }
448 [ \t\v\f] { incColumn (); }
449 \n { context_incLineno ();
450 if (tokLength != 0) {
452 /* No error to report
455 message ("Likely parse error: token spans multiple lines."),
462 continueLine = FALSE;
466 if (context_inMacro ())
468 /* Don't use RETURN_TOK */
469 yylval.tok = lltok_create (TENDMACRO, g_currentloc);
470 lastWasString = FALSE;
475 "@@MR@@" { setTokLength (6);
477 if (processMacro ()) {
478 if (context_inIterDef ())
480 RETURN_TOK (LLMACROITER);
482 if (context_inIterEnd ())
484 RETURN_TOK (LLMACROEND);
486 if (context_inMacro ())
488 RETURN_TOK (LLMACRO);
492 "@QLMR" { if (context_inHeader () || context_inFunction ())
498 int nspchar = ninput ();
502 ** This is a hack to get the column number correct.
505 llassert (nspchar >= '0' && nspchar <= '9');
507 nspaces = nspchar - '0';
509 setTokLength (5 + nspaces);
513 DPRINTF (("Here we are: %s", context_unparse ()));
515 if (context_inIterDef ())
517 RETURN_TOK (LLMACROITER);
519 if (context_inIterEnd ())
521 RETURN_TOK (LLMACROEND);
523 if (context_inMacro ())
525 RETURN_TOK (LLMACRO);
530 "@.CT" { setTokLength (4); lldiagmsg (ctype_unparseTable ()); }
531 "@.FA" { setTokLength (4); lldiagmsg (message ("Access types: %q", typeIdSet_unparse (context_fileAccessTypes ()))); }
532 "@.F" { setTokLength (3);
533 lldiagmsg (message ("%q: *** marker ***", fileloc_unparse (g_currentloc)));
535 "@.L" { setTokLength (3); usymtab_printLocal (); }
536 "@.A" { setTokLength (3); lldiagmsg (usymtab_unparseAliases ()); }
537 "@.C" { setTokLength (3); lldiagmsg (context_unparse ()); }
538 "@.W" { setTokLength (3); lldiagmsg (context_unparseClauses ()); }
539 "@.G" { setTokLength (3); usymtab_printGuards (); }
540 "@.S" { setTokLength (3); usymtab_printOut (); }
541 "@.X" { setTokLength (3); usymtab_printAll (); }
542 "@.Z" { setTokLength (3); usymtab_printComplete (); }
543 "@.T" { setTokLength (3); usymtab_printTypes (); }
544 "@.K" { setTokLength (3); lldiagmsg (usymtab_unparseStack ()); }
545 "@.M" { setTokLength (3);
546 lldiagmsg (message ("Can modify: %q",
547 sRefSet_unparse (context_modList ())));
549 "%{" { /* BEFORE_COMMENT_MARKER */
551 incColumn (); incColumn ();
552 tok = handleLlSpecial ();
556 if (tok == CANNOTATION) {
559 /* Beware - this bashes yylval! */
564 "%}" { /* AFTER_COMMENT_MARKER */
567 RETURN_TOK (QENDMACRO); }
568 "\\" { incColumn (); continueLine = TRUE; }
570 if ((int) *yytext == 13 ) {
575 message ("Invalid character (ascii: %d), skipping character",
584 /*@null@*/ /*@observer@*/ char *name;
589 ** These tokens are followed by syntax that is parsed by the
593 struct skeyword s_parsetable[] = {
594 { "modifies", QMODIFIES } ,
595 { "globals", QGLOBALS } ,
598 { "constant", QCONSTANT } ,
599 { "function", QFUNCTION } ,
601 { "defines", QDEFINES } ,
603 { "allocates", QALLOCATES } ,
605 { "releases", QRELEASES } ,
606 { "pre", QPRECLAUSE } ,
607 { "post", QPOSTCLAUSE } ,
608 { "setBufferSize", QSETBUFFERSIZE},
609 { "setStringLength", QSETSTRINGLENGTH},
610 { "testinRange", QTESTINRANGE},
611 { "requires", QPRECLAUSE } ,
612 { "ensures", QPOSTCLAUSE } ,
617 ** These tokens are either stand-alone tokens, or followed by
618 ** token-specific text.
621 struct skeyword s_keytable[] = {
622 { "anytype", QANYTYPE } ,
623 { "integraltype", QINTEGRALTYPE } ,
624 { "unsignedintegraltype", QUNSIGNEDINTEGRALTYPE } ,
625 { "signedintegraltype", QSIGNEDINTEGRALTYPE } ,
629 { "owned", QOWNED } ,
630 { "dependent", QDEPENDENT } ,
631 { "partial", QPARTIAL } ,
632 { "special", QSPECIAL } ,
633 { "truenull", QTRUENULL } ,
634 { "falsenull", QFALSENULL } ,
637 { "notnull", QNOTNULL } ,
638 { "abstract", QABSTRACT } ,
639 { "concrete", QCONCRETE } ,
640 { "mutable", QMUTABLE } ,
641 { "immutable", QIMMUTABLE } ,
642 { "unused", QUNUSED } ,
643 { "external", QEXTERNAL } ,
645 { "unique", QUNIQUE } ,
646 { "returned", QRETURNED } ,
647 { "exposed", QEXPOSED } ,
648 { "refcounted", QREFCOUNTED } ,
650 { "newref", QNEWREF } ,
651 { "tempref", QTEMPREF } ,
652 { "killref", QKILLREF } ,
654 { "relnull", QRELNULL } ,
655 { "nullterminated", QNULLTERMINATED },
656 { "setBufferSize", QSETBUFFERSIZE },
657 { "testInRange", QTESTINRANGE},
658 { "MaxSet", QMAXSET},
659 { "MaxRead", QMAXREAD},
660 { "reldef", QRELDEF } ,
661 { "observer", QOBSERVER } ,
662 { "exits", QEXITS } ,
663 { "mayexit", QMAYEXIT } ,
664 { "trueexit", QTRUEEXIT } ,
665 { "falseexit", QFALSEEXIT } ,
666 { "neverexit", QNEVEREXIT } ,
668 { "shared", QSHARED } ,
670 { "unchecked", QUNCHECKED } ,
671 { "checked", QCHECKED } ,
672 { "checkmod", QCHECKMOD } ,
673 { "checkedstrict", QCHECKEDSTRICT } ,
674 { "innercontinue", QINNERCONTINUE } ,
675 { "innerbreak", QINNERBREAK } ,
676 { "loopbreak", QLOOPBREAK } ,
677 { "switchbreak", QSWITCHBREAK } ,
678 { "safebreak", QSAFEBREAK } ,
679 { "fallthrough", QFALLTHROUGH } ,
680 { "l_fallthrou", QLINTFALLTHROUGH } ,
681 { "l_fallth", QLINTFALLTHRU } ,
682 { "notreached", QNOTREACHED } ,
683 { "l_notreach", QLINTNOTREACHED } ,
684 { "printflike", QPRINTFLIKE } ,
685 { "l_printfli", QLINTPRINTFLIKE } ,
686 { "scanflike", QSCANFLIKE } ,
687 { "messagelike", QMESSAGELIKE } ,
688 { "l_argsus", QARGSUSED } ,
693 ** would be better if these weren't hard coded...
696 static bool isArtificial (cstring s)
698 return (cstring_equalLit (s, "modifies")
699 || cstring_equalLit (s, "globals")
700 || cstring_equalLit (s, "warn")
701 || cstring_equalLit (s, "alt"));
704 void swallowMacro (void)
707 bool skipnext = FALSE;
709 while ((i = lminput ()) != EOF)
726 reader_checkUngetc (i, yyin);
734 reader_checkUngetc (i, yyin);
738 static int commentMarkerToken (cstring s)
742 while (s_parsetable[i].name != NULL)
744 DPRINTF (("Try :%s:%s:", s, s_parsetable[i].name));
746 if (cstring_equalLit (s, s_parsetable[i].name))
748 return s_parsetable[i].token;
757 static int tokenMacroCode (cstring s)
761 while (s_keytable[i].name != NULL)
763 if (cstring_equalLit (s, s_keytable[i].name))
765 if (s_keytable[i].token == QLINTFALLTHROUGH)
768 (FLG_WARNLINTCOMMENTS,
770 ("Traditional lint comment /*FALLTHROUGH*/ used. "
771 "This is interpreted by "
772 "LCLint in the same way as most Unix lints, but it is "
773 "preferable to replace it with the /*@fallthrough@*/ "
778 else if (s_keytable[i].token == QLINTFALLTHRU)
781 (FLG_WARNLINTCOMMENTS,
783 ("Traditional lint comment /*FALLTHRU*/ used. "
784 "This is interpreted by "
785 "LCLint in the same way as most Unix lints, but it is "
786 "preferable to replace it with the /*@fallthrough@*/ "
791 else if (s_keytable[i].token == QLINTNOTREACHED)
794 (FLG_WARNLINTCOMMENTS,
796 ("Traditional lint comment /*NOTREACHED*/ used. "
797 "This is interpreted by "
798 "LCLint in the same way as most Unix lints, but it is "
799 "preferable to replace it with the /*@notreached@*/ "
800 "semantic comment."),
805 else if (s_keytable[i].token == QPRINTFLIKE)
807 setSpecialFunction (qual_createPrintfLike ());
810 else if (s_keytable[i].token == QLINTPRINTFLIKE)
813 (FLG_WARNLINTCOMMENTS,
815 ("Traditional lint comment /*PRINTFLIKE*/ used. "
816 "This is interpreted by "
817 "LCLint in the same way as most Unix lints, but it is "
818 "preferable to replace it with either /*@printflike@*/, "
819 "/*@scanflike@*/ or /*@messagelike@*/."),
822 setSpecialFunction (qual_createPrintfLike ());
825 else if (s_keytable[i].token == QSCANFLIKE)
827 setSpecialFunction (qual_createScanfLike ());
830 else if (s_keytable[i].token == QMESSAGELIKE)
832 setSpecialFunction (qual_createMessageLike ());
835 else if (s_keytable[i].token == QARGSUSED)
838 (FLG_WARNLINTCOMMENTS,
840 ("Traditional lint comment /*ARGSUSED*/ used. "
841 "This is interpreted by "
842 "LCLint in the same way as most Unix lints, but it is "
843 "preferable to use /*@unused@*/ annotations on "
844 "the unused parameters."),
851 return s_keytable[i].token;
860 static int lminput ()
862 if (savechar == '\0')
869 int save = (int) savechar;
875 static void lmsavechar (char c)
877 if (savechar == '\0') savechar = c;
880 llbuglit ("lmsavechar: override");
884 static int returnFloat (ctype ct, double f)
886 yylval.expr = exprNode_floatLiteral (f, ct, cstring_fromChars (yytext),
887 fileloc_decColumn (g_currentloc, tokLength));
892 static int returnInt (ctype ct, long i)
896 if (ctype_equal (ct, ctype_int))
900 c = context_typeofZero ();
904 c = context_typeofOne ();
908 yylval.expr = exprNode_numLiteral (c, cstring_fromChars (yytext),
909 fileloc_decColumn (g_currentloc, tokLength), i);
914 static int returnChar (char c)
916 yylval.expr = exprNode_charLiteral (c, cstring_fromChars (yytext),
917 fileloc_decColumn (g_currentloc, tokLength));
926 if (c != EOF && ((char)c == '\n'))
928 context_incLineno ();
934 static char macro_nextChar ()
936 static bool in_quote = FALSE, in_escape = FALSE, in_char = FALSE;
941 c = char_fromInt (ic);
943 if (!in_quote && !in_char && (c == '\\' || c == BEFORE_COMMENT_MARKER[0]))
947 while ((c = char_fromInt (lminput ())) != '\0' && c != '\n')
949 ; /* skip to newline */
952 context_incLineno ();
956 return macro_nextChar ();
963 else /* if (c == '@') */
965 llassert (FALSE); /*@i23@*/
966 if (handleLlSpecial () != BADTOK)
968 llerrorlit (FLG_SYNTAX, "Macro cannot use special syntax");
971 return macro_nextChar ();
974 else if (!in_escape && c == '\"')
976 in_quote = !in_quote;
978 else if (!in_escape && c == '\'')
982 else if ((in_quote || in_char) && c == '\\')
984 in_escape = !in_escape;
986 else if ((in_quote || in_char) && in_escape)
990 else if (!in_quote && c == '/')
994 if ((c2 = char_fromInt (lminput ())) == '*')
998 while ((c2 = char_fromInt (lminput ())) != '\0'
999 && c2 != '\n' && c2 != '*')
1006 while ((c2 = char_fromInt (lminput ())) != '\0'
1019 llfatalerror (cstring_makeLiteral ("Macro: bad comment!"));
1023 return macro_nextChar ();
1027 /*** putchar does not work! why? puts to stdio...??! ***/
1035 ** keeps semantic comments
1038 static char macro_nextCharC ()
1040 static bool in_quote = FALSE, in_escape = FALSE, in_char = FALSE;
1043 c = char_fromInt (lminput ());
1045 if (!in_quote && !in_char && c == '\\')
1047 while ((c = char_fromInt (lminput ())) != '\0' && c != '\n')
1049 ; /* skip to newline */
1052 context_incLineno ();
1056 return macro_nextCharC ();
1063 else if (!in_escape && c == '\"')
1065 in_quote = !in_quote;
1067 else if (!in_escape && c == '\'')
1071 else if ((in_quote || in_char) && c == '\\')
1073 in_escape = !in_escape;
1075 else if ((in_quote || in_char) && in_escape)
1079 else if (!in_quote && c == '/')
1083 if ((c2 = char_fromInt (lminput ())) == '*')
1087 while ((c2 = char_fromInt (lminput ())) != '\0'
1088 && c2 != '\n' && c2 != '*')
1095 while ((c2 = char_fromInt (lminput ())) != '\0'
1108 llfatalerror (cstring_makeLiteral ("Macro: bad comment!"));
1112 return macro_nextCharC ();
1123 ** skips whitespace (handles line continuations)
1124 ** returns first non-whitespace character
1127 static char skip_whitespace ()
1131 while ((c = macro_nextChar ()) == ' ' || c == '\t')
1139 static void handleMacro ()
1141 cstring mac = cstring_undefined;
1145 while (currentColumn () > 2)
1147 mac = cstring_appendChar (mac, ' ');
1151 c = macro_nextCharC ();
1153 if (c >= '0' && c <= '9')
1157 for (i = 0; i < ((c - '0') + 1); i++)
1159 mac = cstring_appendChar (mac, ' ');
1167 while (((c = macro_nextCharC ()) != '\0') && (c != '\n'))
1169 mac = cstring_appendChar (mac, c);
1173 macrocode = tokenMacroCode (mac);
1175 if (macrocode == BADTOK && !isArtificial (mac))
1177 context_addMacroCache (mac);
1186 context_incLineno ();
1190 static bool processMacro (void)
1195 cstring fname = cstring_undefined;
1197 bool isspecfcn = FALSE;
1198 bool isiter = FALSE;
1199 bool skipparam = FALSE;
1200 bool isenditer = FALSE;
1201 bool unknownm = FALSE;
1202 bool hasParams = FALSE;
1203 bool emptyMacro = FALSE;
1204 char c = skip_whitespace ();
1205 fileloc loc = fileloc_noColumn (g_currentloc);
1207 /* are both of these necessary? what do they mean? */
1208 uentryList specparams = uentryList_undefined;
1209 uentryList pn = uentryList_undefined;
1211 context_resetMacroMissingParams ();
1213 if (c == '\0' || c == '\n')
1215 llcontbug (cstring_makeLiteral ("Bad macro"));
1220 fname = cstring_appendChar (fname, c);
1222 while ((c = macro_nextChar ()) != '(' && c != '\0'
1223 && c != ' ' && c != '\t' && c != '\n')
1225 fname = cstring_appendChar (fname, c);
1228 if (c == ' ' || c == '\t' || c == '\n')
1234 while (c == ' ' || c == '\t')
1236 c = macro_nextChar ();
1250 hasParams = (c == '(');
1252 if (usymtab_exists (fname))
1254 e2 = usymtab_lookupExpose (fname);
1255 ct = uentry_getType (e2);
1257 if (uentry_isCodeDefined (e2)
1258 && fileloc_isUser (uentry_whereDefined (e2)))
1262 message ("Macro %s already defined", fname),
1265 uentry_showWhereDefined (e2);
1266 uentry_clearDefined (e2);
1269 if (uentry_isFunction (e2))
1271 uentry_setType (e2, ctype_unknown);
1274 context_enterUnknownMacro (e2);
1278 context_enterConstantMacro (e2);
1283 if (uentry_isForward (e2) && uentry_isFunction (e2))
1290 ("Parameterized macro has no prototype or specification: %s ",
1295 uentry_setType (e2, ctype_unknown);
1296 uentry_setFunctionDefined (e2, loc);
1297 uentry_setUsed (e2, fileloc_undefined);
1298 context_enterUnknownMacro (e2);
1302 if (uentry_isIter (e2))
1305 specparams = uentry_getParams (e2);
1306 noparams = uentryList_size (specparams);
1307 uentry_setDefined (e2, loc);
1308 context_enterIterDef (e2);
1310 else if (uentry_isEndIter (e2))
1313 uentry_setDefined (e2, loc);
1314 context_enterIterEnd (e2); /* don't care about it now */
1315 /* but should parse like an iter! */
1317 else if (uentry_isConstant (e2))
1323 message ("Constant %s implemented as parameterized macro",
1327 uentry_showWhereSpecified (e2);
1328 uentry_setType (e2, ctype_unknown);
1329 uentry_makeConstantFunction (e2);
1330 uentry_setDefined (e2, g_currentloc);
1331 uentry_setFunctionDefined (e2, g_currentloc);
1332 context_enterUnknownMacro (e2);
1336 if (!uentry_isSpecified (e2))
1338 fileloc oloc = uentry_whereDeclared (e2);
1340 if (fileloc_isLib (oloc))
1344 else if (fileloc_isUndefined (oloc)
1345 || fileloc_isPreproc (oloc))
1350 (FLG_MACROCONSTDECL,
1352 ("Macro constant %q not declared",
1353 uentry_getName (e2)),
1357 else if (!fileloc_withinLines (oloc, loc, 2))
1358 { /* bogus! will give errors if there is too much whitespace */
1362 ("Macro constant name %s does not match name in "
1363 "previous constant declaration. This constant "
1364 "is declared at %q", fname,
1365 fileloc_unparse (oloc)),
1370 context_enterConstantMacro (e2);
1371 cstring_free (fname);
1377 else if (ctype_isFunction (ct))
1380 specparams = ctype_argsFunction (ct);
1381 noparams = uentryList_size (specparams);
1383 uentry_setFunctionDefined (e2, loc);
1384 context_enterMacro (e2);
1386 else if (uentry_isVar (e2))
1392 message ("Variable %s implemented as parameterized macro",
1396 uentry_showWhereSpecified (e2);
1397 uentry_setType (e2, ctype_unknown);
1398 uentry_makeVarFunction (e2);
1399 uentry_setDefined (e2, g_currentloc);
1400 uentry_setFunctionDefined (e2, g_currentloc);
1401 context_enterUnknownMacro (e2);
1405 uentry ucons = uentry_makeConstant (fname,
1408 if (uentry_isExpandedMacro (e2))
1416 message ("Variable %s implemented by a macro",
1420 uentry_showWhereSpecified (e2);
1424 uentry_setDefined (e2, loc);
1425 uentry_setUsed (ucons, loc);
1427 context_enterConstantMacro (ucons);
1428 uentry_markOwned (ucons);
1429 cstring_free (fname);
1435 if (uentry_isDatatype (e2))
1439 message ("Type implemented as macro: %x",
1440 uentry_getName (e2)),
1441 message ("A type is implemented using a macro definition. A "
1442 "typedef should be used instead."),
1446 /* Must exit scope (not sure why a new scope was entered?) */
1447 usymtab_quietExitScope (g_currentloc);
1448 uentry_setDefined (e2, g_currentloc);
1454 (message ("Unexpanded macro not function or constant: %q",
1455 uentry_unparse (e2)));
1456 uentry_setType (e2, ctype_unknown);
1460 uentry_makeVarFunction (e2);
1461 uentry_setDefined (e2, g_currentloc);
1462 uentry_setFunctionDefined (e2, g_currentloc);
1463 context_enterUnknownMacro (e2);
1474 /* evans 2001-09-09 - if it has params, assume a function */
1478 (FLG_MACROMATCHNAME,
1479 message ("Unexpanded macro %s does not match name of a declared "
1480 "function. The name used in the control "
1481 "comment on the previous line should match.",
1485 ce = uentry_makeFunction (fname, ctype_unknown,
1489 warnClause_undefined,
1491 uentry_setUsed (ce, loc); /* perhaps bogus? */
1492 e2 = usymtab_supEntryReturn (ce);
1493 context_enterUnknownMacro (e2);
1498 (FLG_MACROMATCHNAME,
1499 message ("Unexpanded macro %s does not match name of a constant "
1500 "or iter declaration. The name used in the control "
1501 "comment on the previous line should match. "
1502 "(Assuming macro defines a constant.)",
1506 ce = uentry_makeConstant (fname, ctype_unknown, fileloc_undefined);
1507 uentry_setUsed (ce, loc); /* perhaps bogus? */
1508 e2 = usymtab_supEntryReturn (ce);
1510 context_enterConstantMacro (e2);
1511 cstring_free (fname);
1517 /* in macros, ( must follow immediatetly after name */
1523 c = skip_whitespace ();
1525 while (c != ')' && c != '\0')
1528 bool suppress = context_inSuppressRegion ();
1529 cstring paramname = cstring_undefined;
1532 ** save the parameter location
1536 context_saveLocation ();
1539 while (c != ' ' && c != '\t' && c != ',' && c != '\0' && c != ')')
1541 paramname = cstring_appendChar (paramname, c);
1542 c = macro_nextChar ();
1545 if (c == ' ' || c == '\t') c = skip_whitespace ();
1549 c = macro_nextChar ();
1550 if (c == ' ' || c == '\t') c = skip_whitespace ();
1555 llfatalerror (cstring_makeLiteral
1556 ("Bad macro syntax: uentryList"));
1559 if ((isspecfcn || isiter) && (paramno < noparams)
1560 && !uentry_isElipsisMarker (uentryList_getN
1561 (specparams, paramno)))
1563 fileloc sloc = context_getSaveLocation ();
1564 uentry decl = uentryList_getN (specparams, paramno);
1567 param = uentry_nameCopy (paramname, decl);
1569 uentry_setParam (param);
1570 sr = sRef_makeParam (paramno, uentry_getType (param), stateInfo_makeLoc (sloc));
1572 if (sRef_getNullState (sr) == NS_ABSNULL)
1574 ctype pt = ctype_realType (uentry_getType (param));
1576 if (ctype_isUser (pt))
1578 uentry te = usymtab_getTypeEntrySafe (ctype_typeId (pt));
1580 if (uentry_isValid (te))
1582 sRef_setStateFromUentry (sr, te);
1587 sRef_setNullState (sr, NS_UNKNOWN, sloc);
1591 uentry_setSref (param, sr);
1592 uentry_setDeclaredForceOnly (param, sloc);
1594 skipparam = isiter && uentry_isOut (uentryList_getN (specparams, paramno));
1598 fileloc sloc = context_getSaveLocation ();
1600 param = uentry_makeVariableSrefParam
1601 (paramname, ctype_unknown, fileloc_copy (sloc),
1602 sRef_makeParam (paramno, ctype_unknown, stateInfo_makeLoc (sloc)));
1603 DPRINTF (("Unknown param: %s", uentry_unparseFull (param)));
1604 cstring_free (paramname);
1606 sRef_setPosNull (uentry_getSref (param), sloc);
1607 uentry_setDeclaredForce (param, sloc);
1610 fileloc_free (sloc);
1615 llassert (!uentry_isElipsisMarker (param));
1619 sRef_makeUnsafe (uentry_getSref (param));
1622 pn = uentryList_add (pn, uentry_copy (param));
1623 usymtab_supEntry (param);
1627 /* don't add param */
1628 uentry_free (param);
1633 (void) macro_nextChar ();
1634 c = skip_whitespace ();
1642 if (isspecfcn || isiter)
1644 if (paramno != noparams && noparams >= 0)
1650 message ("Macro %s specified with %d args, defined with %d",
1651 fname, noparams, paramno),
1654 uentry_showWhereSpecified (e2);
1655 uentry_resetParams (e2, pn);
1660 uentry_resetParams (e2, pn);
1667 ** the form should be:
1669 ** # define newname oldname
1670 ** where oldname refers to a function matching the specification
1676 sRef_setGlobalScope ();
1677 usymtab_supGlobalEntry (uentry_makeVariableLoc (fname, ctype_unknown));
1678 sRef_clearGlobalScope ();
1682 context_setMacroMissingParams ();
1687 /* context_setuentryList (pn); */
1688 usymtab_enterScope ();
1691 cstring_free (fname);
1696 static bool handleSpecial (char *yyt)
1698 char *l = mstring_create (MAX_NAME_LENGTH);
1704 strcpy (l, yyt + 1);
1706 /* Need to safe original l for deallocating. */
1709 l += strlen (yyt) - 1;
1711 while ((c = char_fromInt (lminput ())) != '\n' && c != '\0')
1717 olc = cstring_fromChars (ol);
1719 if (cstring_equalPrefixLit (olc, "pragma"))
1721 char *pname = mstring_create (longUnsigned_fromInt (MAX_PRAGMA_LEN));
1722 char *opname = pname;
1723 char *ptr = ol + 6; /* pragma is six characters, plus space */
1727 /* skip whitespace */
1728 while (((c = *ptr) != '\0') && isspace (c))
1734 while (((c = *ptr) != '\0') && !isspace (c))
1738 if (len > MAX_PRAGMA_LEN)
1749 if (len == PRAGMA_LEN_EXPAND
1750 && mstring_equal (opname, PRAGMA_EXPAND))
1752 cstring exname = cstring_undefined;
1756 while (((c = *ptr) != '\0') && !isspace (c))
1758 exname = cstring_appendChar (exname, c);
1763 ue = usymtab_lookupExposeGlob (exname);
1765 if (uentry_isExpandedMacro (ue))
1767 if (fileloc_isPreproc (uentry_whereDefined (ue)))
1769 fileloc_setColumn (g_currentloc, 1);
1770 uentry_setDefined (ue, g_currentloc);
1774 cstring_free (exname);
1777 else if (cstring_equalPrefixLit (olc, "ident"))
1779 /* Some pre-processors will leave these in the code. Ignore rest of line */
1782 ** Yuk...Win32 filenames can have spaces in them...we need to read
1783 ** to the matching end quote.
1785 else if ((sscanf (ol, "line %d \"", &lineno) == 1)
1786 || (sscanf (ol, " %d \"", &lineno) == 1))
1792 /*@access cstring@*/
1793 while (*tmp != '\"' && *tmp != '\0')
1798 llassert (*tmp == '\"');
1804 while (*tmp != '\"' && *tmp != '\0')
1809 llassert (*tmp == '\"');
1813 # if defined(OS2) || defined(MSDOS) || defined(WIN32)
1816 ** DOS-like path delimiters get delivered in pairs, something like
1817 ** \"..\\\\file.h\", so we have to make it normal again. We do NOT
1818 ** remove the pre dirs yet as we usually specify tmp paths relative
1819 ** to the current directory, so tmp files would not get found in
1820 ** the hash table. If this method fails we try it again later.
1827 ** Skip past the drive marker.
1830 if (strchr (stmp, ':') != NULL)
1832 stmp = strchr (stmp, ':') + 1;
1835 while ((stmp = strchr (stmp, CONNECTCHAR)) != NULL )
1837 if (*(stmp+1) == CONNECTCHAR)
1839 memmove (stmp, stmp+1, strlen (stmp));
1845 fid = fileTable_lookupBase (context_fileTable (), fname);
1846 if (!(fileId_isValid (fid)))
1848 fname = removePreDirs (fname);
1849 fid = fileTable_lookupBase (context_fileTable (), fname);
1852 # else /* !defined(OS2) && !defined(MSDOS) */
1853 fname = removePreDirs (fname);
1854 fid = fileTable_lookupBase (context_fileTable (), fname);
1855 # endif /* !defined(OS2) && !defined(MSDOS) */
1857 if (!(fileId_isValid (fid)))
1859 if (context_inXHFile ())
1861 fid = fileTable_addXHFile (context_fileTable (), fname);
1863 else if (isHeaderFile (fname))
1865 fid = fileTable_addHeaderFile (context_fileTable (), fname);
1869 fid = fileTable_addFile (context_fileTable (), fname);
1873 setFileLine (fid, lineno);
1874 /*@noaccess cstring@*/
1876 else if ((sscanf (ol, "line %d", &lineno) == 1)
1877 || (sscanf (ol, " %d", &lineno) == 1))
1879 setLine (lineno); /* next line is <cr> */
1883 if (mstring_equal (ol, "")) {
1884 DPRINTF (("Empty pp command!"));
1886 ** evs 2000-05-16: This is a horrible kludge, to get around a bug (well, difficulty) in the pre-processor.
1887 ** We handle a plain # in the input file, by echoing it, and ignoring it in the post-pp-file.
1893 (FLG_UNRECOGDIRECTIVE,
1894 message ("Unrecognized pre-processor directive: #%s",
1895 cstring_fromChars (ol)),
1900 return FALSE; /* evans 2001-12-30: was: TRUE; */
1907 static int handleLlSpecial ()
1912 char *s = mstring_createEmpty ();
1918 loc = fileloc_copy (g_currentloc);
1919 DPRINTF (("Handle special: %s", fileloc_unparse (loc)));
1921 while (((ic = ninput ()) != 0) && isalpha (ic))
1924 s = mstring_append (s, c);
1928 DPRINTF (("Read: %s / %s", s, fileloc_unparse (g_currentloc)));
1931 if (charsread == 0 && ic == (int) AFTER_COMMENT_MARKER[0])
1935 llassert (ic == AFTER_COMMENT_MARKER[1]);
1941 return QNOMODS; /* special token no modifications token */
1945 DPRINTF (("Coment marker: %s", os));
1946 tok = commentMarkerToken (cstring_fromChars (os));
1950 tokLength = charsread;
1957 DPRINTF (("Not a comment marker..."));
1958 /* Add rest of the comment */
1960 if (ic != 0 && ic != EOF)
1964 s = mstring_append (s, c);
1967 while (((ic = ninput ()) != 0) && (ic != EOF)
1968 && (ic != AFTER_COMMENT_MARKER[0]))
1972 /* evans 2001-09-01 added to prevent assertion failures for uncloses syntactic comments */
1975 hasnl = TRUE; /* This prevents tokLength from being set later. */
1980 message ("Likely parse error: syntactic comment token spans multiple lines: %s",
1981 cstring_fromChars (s)),
1985 s = mstring_append (s, c);
1990 DPRINTF (("Read: %s / %s", s, fileloc_unparse (g_currentloc)));
1992 if (ic == AFTER_COMMENT_MARKER[0])
1995 llassert ((char) nc == AFTER_COMMENT_MARKER[1]);
2001 while (*s == ' ' || *s == '\t' || *s == '\n')
2006 if (*s == '-' || *s == '+' || *s == '=') /* setting flags */
2010 while (c == '-' || c == '+' || c == '=')
2012 ynm set = ynm_fromCodeChar (c);
2017 thisflag = cstring_fromChars (s);
2019 while ((c = *s) != '\0' && (c != '-') && (c != '=')
2020 && (c != '+') && (c != ' ') && (c != '\t') && (c != '\n'))
2027 if (!context_getFlag (FLG_NOCOMMENTS))
2029 cstring flagname = thisflag;
2030 flagcode fflag = identifyFlag (flagname);
2032 if (flagcode_isSkip (fflag))
2036 else if (flagcode_isInvalid (fflag))
2038 if (isMode (flagname))
2040 if (ynm_isMaybe (set))
2045 ("Semantic comment attempts to restore flag %s. "
2046 "A mode flag cannot be restored.",
2051 context_setMode (flagname);
2057 (FLG_UNRECOGFLAGCOMMENTS,
2058 message ("Unrecognized option in semantic comment: %s",
2063 else if (flagcode_isGlobalFlag (fflag))
2068 ("Semantic comment attempts to set global flag %s. "
2069 "A global flag cannot be set locally.",
2075 context_fileSetFlag (fflag, set);
2077 if (flagcode_hasArgument (fflag))
2079 if (ynm_isMaybe (set))
2084 ("Semantic comment attempts to restore flag %s. "
2085 "A flag for setting a value cannot be restored.",
2090 { /* cut-and-pastied from llmain...blecch */
2091 cstring extra = cstring_undefined;
2097 rest = mstring_copy (s);
2101 while ((rchar = *rest) != '\0'
2102 && (isspace (rchar)))
2108 while ((rchar = *rest) != '\0'
2109 && !isspace (rchar))
2111 extra = cstring_appendChar (extra, rchar);
2118 if (cstring_isUndefined (extra))
2123 ("Flag %s (in semantic comment) must be followed by an argument",
2124 flagcode_unparse (fflag)));
2130 if (flagcode_hasValue (fflag))
2132 setValueFlag (fflag, extra);
2134 else if (flagcode_hasString (fflag))
2136 setStringFlag (fflag, extra);
2153 while ((c == ' ') || (c == '\t') || (c == '\n'))
2159 if (context_inHeader () && !isArtificial (cstring_fromChars (os)))
2161 DPRINTF (("Here adding comment: %s", os));
2162 context_addComment (cstring_fromCharsNew (os));
2174 annotationInfo ainfo;
2176 while (*s != '\0' && *s != ' ' && *s != '\t' && *s != '\n')
2188 t = cstring_toCharsSafe (cstring_downcase (cstring_fromChars (t)));
2189 macrocode = tokenMacroCode (cstring_fromChars (t));
2191 if (macrocode != BADTOK)
2193 tokLength = hasnl ? 0 : mstring_length (t);
2199 if (macrocode == SKIPTOK)
2207 ainfo = context_lookupAnnotation (cstring_fromChars (os));
2209 if (annotationInfo_isDefined (ainfo)) {
2210 DPRINTF (("Found annotation: %s", annotationInfo_unparse (ainfo)));
2211 /*@i324@*/ yylval.annotation = ainfo;
2219 if (context_inHeader ())
2226 if ((context_inMacro () || context_inGlobalContext ())
2227 && macrocode != SKIPTOK
2228 && !isArtificial (cstring_fromChars (os)))
2230 DPRINTF (("Add comment: %s", os));
2231 context_addComment (cstring_fromCharsNew (os));
2244 if (mstring_equal (t, "ignore"))
2246 if (!context_getFlag (FLG_NOCOMMENTS))
2248 context_enterSuppressRegion ();
2251 else if ((*t == 'i' || *t == 't')
2252 && (*(t + 1) == '\0'))
2254 if (!context_getFlag (FLG_NOCOMMENTS)
2255 && (*t == 'i' || context_getFlag (FLG_TMPCOMMENTS)))
2257 context_enterSuppressLine (-1); /* infinite suppression */
2260 else if (((*t == 'i') || (*t == 't'))
2261 && ((*(t + 1) >= '0' && *(t + 1) <= '9')))
2263 bool tmpcomment = (*t == 't');
2265 char *tt = t; /* don't mangle t, since it is free'd */
2268 if (lc >= '0' && lc <= '9')
2270 val = (int)(lc - '0');
2273 while (lc >= '0' && lc <= '9')
2282 if (!context_getFlag (FLG_NOCOMMENTS)
2283 && (!tmpcomment || context_getFlag (FLG_TMPCOMMENTS)))
2285 context_enterSuppressLine (val);
2288 else if (mstring_equal (t, "end"))
2290 if (!context_getFlag (FLG_NOCOMMENTS))
2292 context_exitSuppressRegion ();
2295 else if (mstring_equal (t, "notfunction"))
2297 ; /* handled by pcpp */
2299 else if (mstring_equal (t, "access"))
2305 while ((c = *s) && (c == ' ' || c == '\t' || c == '\n'))
2315 tname = cstring_fromChars (s);
2317 while ((c = *s) != '\0' && c != ' '
2318 && c != '\t' && c != '\n' && c != ',')
2325 DPRINTF (("Access %s", tname));
2327 if (!context_getFlag (FLG_NOCOMMENTS)
2328 && !context_getFlag (FLG_NOACCESS))
2330 if (usymtab_existsType (tname))
2332 typeId uid = usymtab_getTypeId (tname);
2333 uentry ue = usymtab_getTypeEntry (uid);
2335 if (uentry_isAbstractDatatype (ue))
2337 context_addFileAccessType (uid);
2338 DPRINTF (("Adding access to: %s / %d", tname, uid));
2345 ("Non-abstract type %s used in access comment",
2352 if (!(context_inSuppressRegion ()
2353 || context_inSuppressZone (g_currentloc)))
2358 ("Unrecognized type %s used in access comment",
2370 if (c != ',' && c != ' ')
2376 else if (mstring_equal (t, "noaccess"))
2383 while ((lc = *s) && (lc == ' ' || lc == '\t' || lc == '\n'))
2393 tname = cstring_fromChars (s);
2395 while ((lc = *s) != '\0' && lc != ' ' && lc != '\t'
2396 && lc != '\n' && lc != ',')
2403 if (!context_getFlag (FLG_NOCOMMENTS)
2404 && !context_getFlag (FLG_NOACCESS))
2406 if (usymtab_existsType (tname))
2408 typeId tuid = usymtab_getTypeId (tname);
2410 if (context_couldHaveAccess (tuid))
2412 DPRINTF (("Removing access: %s", tname));
2413 context_removeFileAccessType (tuid);
2417 if (!(context_inSuppressRegion ()
2418 || context_inSuppressZone (g_currentloc)))
2420 uentry ue = usymtab_getTypeEntry (tuid);
2422 if (uentry_isAbstractDatatype (ue))
2427 ("Non-accessible abstract type %s used in noaccess comment",
2436 ("Non-abstract type %s used in noaccess comment",
2445 if (!(context_inSuppressRegion ()
2446 || context_inSuppressZone (g_currentloc)))
2451 ("Unrecognized type %s used in noaccess comment",
2463 if (lc != ',' && lc != ' ')
2471 voptgenerror (FLG_UNRECOGCOMMENTS,
2472 message ("Semantic comment unrecognized: %s",
2473 cstring_fromChars (os)), loc);
2484 static /*@only@*/ cstring makeIdentifier (char *s)
2486 char *c = mstring_create (size_toInt (strlen (s)) + 1);
2487 cstring id = cstring_fromChars (c);
2489 while (isalnum (*s) || (*s == '_') || (*s == '$'))
2498 /*@observer@*/ /*@dependent@*/ uentry coerceId (cstring cn)
2500 if (!(usymtab_exists (cn)))
2502 fileloc loc = fileloc_createExternal ();
2505 ** We need to put this in a global scope, otherwise the sRef will be deallocated.
2508 uentry ce = uentry_makeUnrecognized (cn, loc);
2510 if (!context_inIterEnd ())
2514 message ("Unrecognized (possibly system) identifier: %q",
2515 uentry_getName (ce)),
2522 return (usymtab_lookup (cn));
2526 ** like, coerceId, but doesn't supercede for iters
2529 /*@observer@*/ uentry coerceIterId (cstring cn)
2531 if (!(usymtab_exists (cn)))
2533 return uentry_undefined;
2536 return (usymtab_lookup (cn));
2539 /*@observer@*/ cstring LastIdentifier ()
2541 return (lastidprocessed);
2544 static int processIdentifier (cstring id)
2548 if (context_getFlag (FLG_GRAMMAR))
2550 lldiagmsg (message ("Process identifier: %s", id));
2553 context_clearJustPopped ();
2554 lastidprocessed = id;
2556 if (context_inFunctionHeader ())
2558 int tok = commentMarkerToken (id);
2559 DPRINTF (("in function decl..."));
2567 tok = tokenMacroCode (id);
2575 annotationInfo ainfo;
2577 if (expectingMetaStateName)
2579 metaStateInfo msinfo = context_lookupMetaStateInfo (id);
2581 if (metaStateInfo_isDefined (msinfo))
2583 yylval.msinfo = msinfo;
2584 return METASTATE_NAME;
2588 DPRINTF (("Not meta state name: %s", cstring_toCharsSafe (id)));
2592 ainfo = context_lookupAnnotation (id);
2594 if (annotationInfo_isDefined (ainfo))
2596 DPRINTF (("Found annotation: %s", annotationInfo_unparse (ainfo)));
2597 /*@i324@*/ yylval.annotation = ainfo;
2602 DPRINTF (("Not annotation: %s", id));
2608 /* Consider handling: Defined by C99 as static const char __func__[] */
2610 if (context_getFlag (FLG_GNUEXTENSIONS))
2614 if (cstring_equalLit (id, "__stdcall")
2615 || cstring_equalLit (id, "__cdecl")
2616 || cstring_equalLit (id, "__extension__"))
2620 else if (cstring_equalLit (id, "__volatile__"))
2624 else if (cstring_equalLit (id, "__signed"))
2628 else if (cstring_equalLit (id, "__unsigned"))
2632 else if (cstring_equalLit (id, "__const__"))
2636 else if (cstring_equalLit (id, "__alignof__"))
2638 tok = CALIGNOF; /* alignof is parsed like sizeof */
2640 else if (cstring_equalLit (id, "__FUNCTION__")
2641 || cstring_equalLit (id, "__PRETTY_FUNCTION__"))
2643 /* These tokens hold the name of the current function as strings */
2644 /* evans 2001-12-30: changed from exprNode_stringLiteral; bug reported by Jim Zelenka. */
2645 yylval.expr = exprNode_makeConstantString (id, fileloc_copy (g_currentloc));
2647 lastWasString = TRUE;
2651 else if (cstring_equalLit (id, "__attribute__")
2652 || cstring_equalLit (id, "__asm__")
2653 || cstring_equalLit (id, "_asm")
2654 || cstring_equalLit (id, "__asm")
2655 || cstring_equalLit (id, "__declspec"))
2658 bool useparens = FALSE;
2659 bool usebraces = FALSE;
2660 bool inquote = FALSE;
2661 bool inescape = FALSE;
2664 while ((ic = input ()) != EOF)
2671 else if (ic == '\\')
2675 else if (ic == '\"')
2711 else if (ic == ')' && useparens)
2714 if (depth == 0) break;
2716 else if (ic == '}' && usebraces)
2719 if (depth == 0) break;
2722 && !usebraces && !useparens
2723 && cstring_equalLit (id, "__asm"))
2726 ** We need this because some MS VC++ include files
2727 ** have __asm mov ... }
2728 ** Its a kludge, but otherwise would need to parse
2737 context_incLineno ();
2739 if (cstring_equalLit (id, "__asm")
2740 && !useparens && !usebraces)
2747 llassert ((useparens && ic == ')')
2748 || (usebraces && ic == '}')
2749 || (!useparens && !usebraces));
2753 else if (cstring_equalLit (id, "inline")
2754 || cstring_equalLit (id, "__inline")
2755 || cstring_equalLit (id, "_inline")
2756 || cstring_equalLit (id, "__inline__"))
2767 le = usymtab_lookupSafe (id);
2769 /*@-dependenttrans@*/
2771 if (uentry_isIter (le))
2773 /*@i32@*/ yylval.entry = le;
2776 else if (uentry_isEndIter (le))
2778 /*@i32@*/ yylval.entry = le;
2779 return (ITER_ENDNAME);
2781 else if (uentry_isUndefined (le))
2785 /* avoid parse errors for certain system built ins */
2787 if (g_expectingTypeName && (cstring_firstChar (id) == '_')
2788 && (cstring_secondChar (id) == '_'))
2790 return (TYPE_NAME_OR_ID);
2793 return (NEW_IDENTIFIER);
2795 else if (!uentry_isDeclared (le) && !uentry_isCodeDefined (le))
2797 if (uentry_isDatatype (le))
2800 return (NEW_IDENTIFIER);
2804 /*@i32@*/ yylval.entry = le;
2805 return (IDENTIFIER);
2808 else if (uentry_isDatatype (le))
2810 if (!g_expectingTypeName)
2814 return (NEW_IDENTIFIER);
2818 yylval.ctyp = uentry_getAbstractType (le);
2820 uentry_setUsed (le, g_currentloc);
2826 /*@i32@*/ yylval.entry = le;
2827 return (IDENTIFIER);
2830 /*@=dependenttrans@*/
2833 static bool processHashIdentifier (/*@only@*/ cstring id)
2835 if (context_inMacro () || context_inIterDef () ||
2836 context_inIterEnd ())
2840 context_clearJustPopped ();
2842 lastidprocessed = id;
2843 le = usymtab_lookupSafe (id);
2845 if (uentry_isParam (le) || uentry_isRefParam (le))
2857 ** Will be handled by handleLlSpecial
2866 static /*@only@*/ exprNode processString ()
2870 char *nl = strchr (yytext, '\n');
2871 cstring ns = cstring_fromCharsNew (yytext);
2875 loc = fileloc_copy (g_currentloc);
2876 addColumn (cstring_length (ns));
2882 loc = fileloc_copy (g_currentloc);
2884 context_incLineno ();
2886 while ((nl = strchr ((nl + 1), '\n')) != NULL)
2888 context_incLineno ();
2894 res = exprNode_stringLiteral (ns, loc);
2899 ** process a wide character string L"...."
2902 static /*@only@*/ exprNode processWideString ()
2906 char *nl = strchr (yytext, '\n');
2909 llassert (*yytext == 'L');
2912 ns = cstring_fromCharsNew (yytext);
2916 loc = fileloc_copy (g_currentloc);
2917 addColumn (cstring_length (ns));
2923 loc = fileloc_copy (g_currentloc);
2925 context_incLineno ();
2927 while ((nl = strchr ((nl + 1), '\n')) != NULL)
2929 context_incLineno ();
2934 res = exprNode_wideStringLiteral (ns, loc);
2944 llassert (*yytext != '\0');
2945 fchar = *(yytext + 1);
2946 if (fchar != '\\') return fchar;
2948 next = *(yytext + 2);
2952 case 'n': return '\n';
2953 case 't': return '\t';
2954 case '\"': return '\"';
2955 case '\'': return '\'';
2956 case '\\': return '\\';
2957 default: return '\0';
2962 double processFloat ()
2964 double ret = atof (yytext);
2975 llassert (yytext[0] == '0'
2976 && (yytext[1] == 'X' || yytext[1] == 'x'));
2978 while (yytext[index] != '\0') {
2980 char c = yytext[index];
2982 if (c >= '0' && c <= '9') {
2983 tval = (int) c - (int) '0';
2984 } else if (c >= 'A' && c <= 'F') {
2985 tval = (int) c - (int) 'A' + 10;
2986 } else if (c >= 'a' && c <= 'f') {
2987 tval = (int) c - (int) 'a' + 10;
2988 } else if (c == 'U' || c == 'L' || c == 'u' || c == 'l') {
2990 while (yytext[index] != '\0') {
2991 if (c == 'U' || c == 'L' || c == 'u' || c == 'l') {
2996 message ("Invalid character (%c) following specifier in hex constant: %s",
2997 c, cstring_fromChars (yytext)),
3007 message ("Invalid character (%c) in hex constant: %s",
3008 c, cstring_fromChars (yytext)),
3013 val = (val * 16) + tval;
3017 DPRINTF (("Hex constant: %s = %ld", yytext, val));
3022 long processOctal ()
3027 llassert (yytext[0] == '0' && yytext[1] != 'X' && yytext[1] != 'x');
3029 while (yytext[index] != '\0') {
3031 char c = yytext[index];
3033 if (c >= '0' && c <= '7') {
3034 tval = (int) c - (int) '0';
3035 } else if (c == 'U' || c == 'L' || c == 'u' || c == 'l') {
3037 while (yytext[index] != '\0') {
3038 if (c == 'U' || c == 'L' || c == 'u' || c == 'l') {
3043 message ("Invalid character (%c) following specifier in octal constant: %s",
3044 c, cstring_fromChars (yytext)),
3054 message ("Invalid character (%c) in octal constant: %s",
3055 c, cstring_fromChars (yytext)),
3060 val = (val * 8) + tval;
3064 DPRINTF (("Octal constant: %s = %ld", yytext, val));
3071 return (atol (yytext));
3075 processSpec (int tok)
3077 size_t length = strlen (yytext);
3081 setTokLengthT (length);
3087 context_saveLocation ();
3088 setTokLengthT (length);
3089 return (processIdentifier (makeIdentifier (yytext)));
3093 void cscanner_expectingMetaStateName ()
3095 llassert (!expectingMetaStateName);
3096 llassert (context_inFunctionHeader ());
3097 expectingMetaStateName = TRUE;
3100 void cscanner_clearExpectingMetaStateName ()
3102 llassert (expectingMetaStateName);
3103 expectingMetaStateName = FALSE;