2 ** Copyright (c) Massachusetts Institute of Technology 1994-1998.
3 ** All Rights Reserved.
4 ** Unpublished rights reserved under the copyright laws of
7 ** THIS MATERIAL IS PROVIDED AS IS, WITH ABSOLUTELY NO WARRANTY EXPRESSED
8 ** OR IMPLIED. ANY USE IS AT YOUR OWN RISK.
10 ** This code is distributed freely and may be used freely under the
11 ** following conditions:
13 ** 1. This notice may not be removed or altered.
15 ** 2. Works derived from this code are not distributed for
16 ** commercial gain without explicit permission from MIT
17 ** (for permission contact lclint-request@sds.lcs.mit.edu).
20 * Modified by Herbert 08/19/97:
21 * - added #include for IBM's OS/2 compiler.
22 * - fixed weird bug with lookup of tmp files (OS/2 and MSDOS only).
26 * Modified by Mike Smith
27 * Corrected missing 'line' in scanf() calls in handleSpecial().
28 * Without this, I get an error when LCLint hits a '#line' directive
29 * in the pre-pre-processed source files. For safety, I have made these
30 * conditional on OS2 and MSDOS because I don't understand why noone else
31 * has seen this problem.
33 * Modified by Mike Smith, 4th June 1997
34 * Finally resolved the #line problem. The scanf() calls have been fixed to
35 * allow the following #line forms:-
37 * #line 123 "filename"
42 * The last two are generated by the GNU pre-processor, apparently
53 ULSuffix ({U}{L}|{L}{U})
57 ** based on original C lexer by Nate Osgood
58 ** from hacrat@catfish.lcs.mit.edu Mon Jun 14 13:06:32 1993
62 # include "lclintMacros.nf"
65 # include "cgrammar.h"
66 # include "cgrammar_tokens.h"
68 # include "fileIdList.h"
71 # if defined(OS2) && defined(__IBMC__)
72 /* needed for isatty()... */
76 static bool lastWasString = FALSE;
77 static char savechar = '\0';
80 # define yyinput() (incColumn (), getc (yyin))
84 extern /*@external@*/ int read ();
88 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 long processDec (void);
100 static long processHex (void);
101 static long processOctal (void);
102 static int processIdentifier (/*@only@*/ cstring)
103 /*@globals undef lastidprocessed@*/ ;
104 static bool processHashIdentifier (/*@only@*/ cstring)
105 /*@globals undef lastidprocessed@*/ ;
107 static int processSpec (int);
108 static bool handleSpecial (char *);
109 static int handleLlSpecial (void);
110 static void handleMacro (void);
111 static bool processMacro (void);
112 static /*@only@*/ cstring makeIdentifier (char *);
114 /* yes, this is exported! */
115 bool g_expectingTypeName = TRUE; /* beginning of file can be type name! */
117 static bool expectingMetaStateName = FALSE;
119 static int returnInt (ctype, long);
120 static int returnFloat (ctype, double);
121 static int returnChar (char);
122 static void setTokLength (int) /*@modifies g_currentloc@*/ ;
123 static void setTokLengthT (size_t) /*@modifies g_currentloc@*/ ;
125 static void advanceLine (void)
132 # define RETURN_INT(c,i) \
133 do { lastWasString = FALSE; \
134 return (returnInt (c, i)); } while (FALSE)
136 # define RETURN_FLOAT(c,f) \
137 do { lastWasString = FALSE; \
138 return (returnFloat (c, f)); \
141 # define RETURN_CHAR(c) \
142 do { lastWasString = FALSE; \
143 return (returnChar (c)); \
146 # define RETURN_TOK(t) \
147 do { yylval.tok = lltok_create (t, fileloc_decColumn (g_currentloc, tokLength)); \
149 lastWasString = FALSE; \
150 return (t); } while (FALSE)
152 # define RETURN_TYPE(t, ct) \
153 do { yylval.ctyp = ct; tokLength = 0; return (t); } while (FALSE)
155 /* don't fileloc_decColumn (g_currentloc, tokLength));
156 the string could have \n's in it!
159 # define RETURN_STRING(c) \
160 do { yylval.expr = exprNode_stringLiteral (c, fileloc_decColumn (g_currentloc, tokLength)); \
162 lastWasString = TRUE; \
163 return (CCONSTANT); } while (FALSE)
165 # define RETURN_EXPR(e) \
166 do { yylval.expr = e; \
168 lastWasString = TRUE; \
169 return (CCONSTANT); } while (FALSE)
173 static void setTokLength (int len)
179 static void setTokLengthT (size_t len)
181 setTokLength (size_toInt (len));
184 # include "flex.head"
186 /*@-unrecog@*/ /*@i5343@*/
192 "/*" { llfatalbug (cstring_makeLiteral ("Comment in pre-processor output")); }
194 "#"{Letter}({Letter}|{Digit})* {
195 context_saveLocation ();
196 setTokLength (longUnsigned_toInt (mstring_length (yytext)));
198 if (processHashIdentifier (makeIdentifier (yytext + 1)))
202 /* was nothing! */ /*@i32@*/
203 RETURN_STRING (cstring_makeLiteral ("\"\""));
207 RETURN_STRING (cstring_makeLiteral ("\"\""));
212 if (handleSpecial (yytext))
219 "#" { if (handleSpecial (yytext))
221 setTokLength (1); RETURN_TOK (0);
224 "..." { setTokLength (3); RETURN_TOK (CTOK_ELIPSIS); }
225 "break" { setTokLength (5); RETURN_TOK (BREAK); }
226 "case" { setTokLength (4); RETURN_TOK (CASE); }
227 "continue" { setTokLength (8); RETURN_TOK (CONTINUE); }
228 "default" { setTokLength (7); RETURN_TOK (DEFAULT); }
229 "do" { setTokLength (2); RETURN_TOK (DO); }
230 "else" { setTokLength (4); RETURN_TOK (CELSE); }
231 "for" { setTokLength (3); RETURN_TOK (CFOR); }
232 "goto" { setTokLength (4); RETURN_TOK (GOTO); }
233 "if" { setTokLength (2); RETURN_TOK (CIF); }
234 "return" { setTokLength (6); RETURN_TOK (RETURN); }
235 "sizeof" { setTokLength (6); RETURN_TOK (CSIZEOF); }
236 "offsetof" { setTokLength (8); RETURN_TOK (COFFSETOF); }
237 "switch" { setTokLength (6); RETURN_TOK (SWITCH); }
238 "while" { setTokLength (5); RETURN_TOK (WHILE); }
239 "va_arg" { setTokLength (6); RETURN_TOK (VA_ARG); }
240 "va_dcl" { setTokLength (6); RETURN_TOK (VA_DCL); }
242 /* gcc extension...this might not be appropriate */
243 setTokLength (6); RETURN_TOK (QINLINE); }
245 "struct" { setTokLength (6); RETURN_TOK (CSTRUCT); }
246 "typedef" { setTokLength (7); RETURN_TOK (CTYPEDEF); }
248 "union" { setTokLength (5); RETURN_TOK (CUNION); }
249 "enum" { setTokLength (4); RETURN_TOK (CENUM); }
251 "void" { setTokLength (4); RETURN_TYPE (CVOID, ctype_void); }
252 "int" { setTokLength (3); RETURN_TYPE (CINT, ctype_int); }
253 "double" { setTokLength (6); RETURN_TYPE (CDOUBLE, ctype_double); }
254 "char" { setTokLength (4); RETURN_TYPE (CGCHAR, ctype_char); }
255 "float" { setTokLength (5); RETURN_TYPE (CGFLOAT, ctype_float); }
257 "long" { setTokLength (4); RETURN_TOK (QLONG); }
258 "short" { setTokLength (5); RETURN_TOK (QSHORT); }
259 "unsigned" { setTokLength (8); RETURN_TOK (QUNSIGNED); }
260 "signed" { setTokLength (6); RETURN_TOK (QSIGNED); }
262 "volatile" { setTokLength (8); RETURN_TOK (QVOLATILE); }
263 "const" { setTokLength (5); RETURN_TOK (QCONST); }
265 /* some systems expect this! [gack!] */
266 "__const" { setTokLength (7); RETURN_TOK (QCONST); }
268 "extern" { setTokLength (6); RETURN_TOK (QEXTERN); }
269 "auto" { setTokLength (4); RETURN_TOK (QAUTO); }
270 "register" { setTokLength (8); RETURN_TOK (QREGISTER); }
271 "static" { setTokLength (6); RETURN_TOK (QSTATIC); }
273 \"(\\.|[^\\"])*\"([ \t\n]*\"(\\.|[^\\"])*\")* { RETURN_EXPR (processString ()); }
274 L\"(\\.|[^\\"])*\"([ \t\n]*\"(\\.|[^\\"])*\")* { RETURN_EXPR (processString ()); }
275 "out" { return (processSpec (QOUT)); }
276 "in" { return (processSpec (QIN)); }
277 "partial" { return (processSpec (QPARTIAL)); }
278 "special" { return (processSpec (QSPECIAL)); }
279 "anytype" { return (processSpec (QANYTYPE)); }
280 "integraltype" { return (processSpec (QINTEGRALTYPE)); }
281 "unsignedintegraltype" { return (processSpec (QUNSIGNEDINTEGRALTYPE)); }
282 "signedintegraltype" { return (processSpec (QSIGNEDINTEGRALTYPE)); }
283 "keep" { return (processSpec (QKEEP)); }
284 "null" { return (processSpec (QNULL)); }
285 "notnull" { return (processSpec (QNOTNULL)); }
286 "isnull" { return (processSpec (QISNULL)); }
287 "truenull" { return (processSpec (QTRUENULL)); }
288 "falsenull" { return (processSpec (QFALSENULL)); }
289 "relnull" { return (processSpec (QRELNULL)); }
290 "reldef" { return (processSpec (QRELDEF)); }
291 "exposed" { return (processSpec (QEXPOSED)); }
292 "newref" { return (processSpec (QNEWREF)); }
293 "tempref" { return (processSpec (QTEMPREF)); }
294 "killref" { return (processSpec (QKILLREF)); }
295 "refcounted" { return (processSpec (QREFCOUNTED)); }
296 "checked" { return (processSpec (QCHECKED)); }
297 "checkmod" { return (processSpec (QCHECKMOD)); }
298 "checkedstrict" { return (processSpec (QCHECKEDSTRICT)); }
299 "unchecked" { return (processSpec (QUNCHECKED)); }
300 "only" { return (processSpec (QONLY)); }
301 "owned" { return (processSpec (QOWNED)); }
302 "observer" { return (processSpec (QOBSERVER)); }
303 "dependent" { return (processSpec (QDEPENDENT)); }
304 "unused" { return (processSpec (QUNUSED)); }
305 "external" { return (processSpec (QEXTERNAL)); }
306 "sef" { return (processSpec (QSEF)); }
307 "shared" { return (processSpec (QSHARED)); }
308 "yield" { return (processSpec (QYIELD)); }
309 "undef" { return (processSpec (QUNDEF)); }
310 "killed" { return (processSpec (QKILLED)); }
311 "nullterminated" { return (processSpec (QNULLTERMINATED));}
312 "MaxSet" { return (processSpec (QMAXSET));}
313 "MaxRead" { return (processSpec (QMAXREAD));}
315 {Letter}({Letter}|{Digit})* { int tok;
316 context_saveLocation ();
317 setTokLength (longUnsigned_toInt (mstring_length (yytext)));
318 tok = processIdentifier (makeIdentifier (yytext));
324 0[xX]{H}+ { setTokLengthT (mstring_length (yytext));
325 RETURN_INT (ctype_int, processHex ()); /* evs 2000-05-17 was ctype_uint */
327 0[xX]{H}+{L} { setTokLengthT (mstring_length (yytext));
328 RETURN_INT (ctype_lint, processHex ()); }
329 0[xX]{H}+{L}{L} { setTokLengthT (mstring_length (yytext));
330 RETURN_INT (ctype_llint, processHex ()); }
331 0[xX]{H}+{U} { setTokLengthT (mstring_length (yytext));
332 RETURN_INT (ctype_uint, processHex ()); }
333 0[xX]{H}+{ULSuffix} { setTokLengthT (mstring_length (yytext));
334 RETURN_INT (ctype_ulint, processHex ()); }
335 0[xX]{H}+{U}{L}{L} { setTokLengthT (mstring_length (yytext));
336 RETURN_INT (ctype_ullint, processHex ()); }
337 0[xX]{H}+{L}{L}{U} { setTokLengthT (mstring_length (yytext));
338 RETURN_INT (ctype_ullint, processHex ()); }
339 0{Digit}+ { setTokLengthT (mstring_length (yytext));
340 RETURN_INT (ctype_int, processOctal ()); }
341 0{Digit}+{U} { setTokLengthT (mstring_length (yytext));
342 RETURN_INT (ctype_uint, processOctal ()); }
343 0{Digit}+{L} { setTokLengthT (mstring_length (yytext));
344 RETURN_INT (ctype_lint, processOctal ()); }
345 0{Digit}+{L}{L} { setTokLengthT (mstring_length (yytext));
346 RETURN_INT (ctype_llint, processOctal ()); }
347 0{Digit}+{ULSuffix} { setTokLengthT (mstring_length (yytext));
348 RETURN_INT (ctype_ulint, processOctal ()); }
349 0{Digit}+{U}{L}{L} { setTokLengthT (mstring_length (yytext));
350 RETURN_INT (ctype_ullint, processOctal ()); }
351 0{Digit}+{L}{L}{U} { setTokLengthT (mstring_length (yytext));
352 RETURN_INT (ctype_ullint, processOctal ()); }
353 {Digit}+ { setTokLengthT (mstring_length (yytext));
354 RETURN_INT (ctype_int, processDec ()); }
355 {Digit}+{U} { setTokLengthT (mstring_length (yytext));
356 RETURN_INT (ctype_uint, processDec ()); }
357 {Digit}+{L} { setTokLengthT (mstring_length (yytext));
358 RETURN_INT (ctype_lint, processDec ()); }
359 {Digit}+{L}{L} { setTokLengthT (mstring_length (yytext));
360 RETURN_INT (ctype_llint, processDec ()); }
361 {Digit}+{ULSuffix} { setTokLengthT (mstring_length (yytext));
362 RETURN_INT (ctype_ulint, processDec ()); }
363 {Digit}+{U}{L}{L} { setTokLengthT (mstring_length (yytext));
364 RETURN_INT (ctype_ullint, processDec ()); }
365 {Digit}+{L}{L}{U} { setTokLengthT (mstring_length (yytext));
366 RETURN_INT (ctype_ullint, processDec ()); }
367 '(\\.|[^\\'])+' { setTokLengthT (mstring_length (yytext));
368 RETURN_CHAR (processChar ()); }
369 {Digit}+{E}[fF] { setTokLengthT (mstring_length (yytext));
370 RETURN_FLOAT (ctype_float, processFloat ()); }
371 {Digit}+{E}[lL] { setTokLengthT (mstring_length (yytext));
372 RETURN_FLOAT (ctype_ldouble, processFloat ()); }
373 {Digit}+{E} { setTokLengthT (mstring_length (yytext));
374 RETURN_FLOAT (ctype_double, processFloat ()); }
376 {Digit}*"."{Digit}+({E})?[fF] { setTokLengthT (mstring_length (yytext));
377 RETURN_FLOAT (ctype_float, processFloat ()); }
378 {Digit}*"."{Digit}+({E})?[lL] { setTokLengthT (mstring_length (yytext));
379 RETURN_FLOAT (ctype_ldouble, processFloat ()); }
380 {Digit}*"."{Digit}+({E})? { setTokLengthT (mstring_length (yytext));
381 RETURN_FLOAT (ctype_double, processFloat ()); }
383 {Digit}+"."{Digit}*({E})?[fF] { setTokLengthT (mstring_length (yytext));
384 RETURN_FLOAT (ctype_float, processFloat ()); }
385 {Digit}+"."{Digit}*({E})?[lL] { setTokLengthT (mstring_length (yytext));
386 RETURN_FLOAT (ctype_ldouble, processFloat ()); }
387 {Digit}+"."{Digit}*({E})? { setTokLengthT (mstring_length (yytext));
388 RETURN_FLOAT (ctype_double, processFloat ()); }
390 ">>=" { setTokLength (3); RETURN_TOK (RIGHT_ASSIGN); }
391 "<<=" { setTokLength (3); RETURN_TOK (LEFT_ASSIGN); }
392 "+=" { setTokLength (2); RETURN_TOK (ADD_ASSIGN); }
393 "-=" { setTokLength (2); RETURN_TOK (SUB_ASSIGN); }
394 "*=" { setTokLength (2); RETURN_TOK (MUL_ASSIGN); }
395 "/=" { setTokLength (2); RETURN_TOK (DIV_ASSIGN); }
396 "%=" { setTokLength (2); RETURN_TOK (MOD_ASSIGN); }
397 "&=" { setTokLength (2); RETURN_TOK (AND_ASSIGN); }
398 "^=" { setTokLength (2); RETURN_TOK (XOR_ASSIGN); }
399 "|=" { setTokLength (2); RETURN_TOK (OR_ASSIGN); }
400 ">>" { setTokLength (2); RETURN_TOK (RIGHT_OP); }
401 "<<" { setTokLength (2); RETURN_TOK (LEFT_OP); }
402 "++" { setTokLength (2); RETURN_TOK (INC_OP); }
403 "--" { setTokLength (2); RETURN_TOK (DEC_OP); }
404 "->" { setTokLength (2); RETURN_TOK (ARROW_OP); }
405 "&&" { setTokLength (2); RETURN_TOK (AND_OP); }
406 "||" { setTokLength (2); RETURN_TOK (OR_OP); }
407 "<=" { setTokLength (2); RETURN_TOK (LE_OP); }
408 ">=" { setTokLength (2); RETURN_TOK (GE_OP); }
409 "==" { setTokLength (2); RETURN_TOK (EQ_OP); }
410 "!=" { setTokLength (2); RETURN_TOK (NE_OP); }
411 ";" { setTokLength (1); RETURN_TOK (TSEMI); }
412 "{" { setTokLength (1); RETURN_TOK (TLBRACE); }
413 "}" { setTokLength (1); RETURN_TOK (TRBRACE); }
414 "," { setTokLength (1); RETURN_TOK (TCOMMA); }
415 ":" { setTokLength (1); RETURN_TOK (TCOLON); }
416 "=" { setTokLength (1); RETURN_TOK (TASSIGN); }
417 "(" { setTokLength (1); RETURN_TOK (TLPAREN); }
418 ")" { setTokLength (1); RETURN_TOK (TRPAREN); }
419 "[" { setTokLength (1); RETURN_TOK (TLSQBR); }
420 "]" { setTokLength (1); RETURN_TOK (TRSQBR); }
421 "." { setTokLength (1); RETURN_TOK (TDOT); }
422 "&" { setTokLength (1); RETURN_TOK (TAMPERSAND); }
423 "!" { setTokLength (1); RETURN_TOK (TEXCL); }
426 "~" { setTokLength (1); RETURN_TOK (TTILDE); }
427 "-" { setTokLength (1); RETURN_TOK (TMINUS); }
428 "+" { setTokLength (1); RETURN_TOK (TPLUS); }
429 "*" { setTokLength (1); RETURN_TOK (TMULT); }
430 "/" { setTokLength (1); RETURN_TOK (TDIV); }
431 "%" { setTokLength (1); RETURN_TOK (TPERCENT); }
432 "<" { setTokLength (1); RETURN_TOK (TLT); }
433 ">" { setTokLength (1); RETURN_TOK (TGT); }
434 "^" { setTokLength (1); RETURN_TOK (TCIRC); }
435 "|" { setTokLength (1); RETURN_TOK (TBAR); }
436 "?" { setTokLength (1); RETURN_TOK (TQUEST); }
439 "/\\" { setTokLength (1); RETURN_TOK (TCAND); }
442 [ \t\v\f] { incColumn (); }
443 \n { context_incLineno ();
446 continueLine = FALSE;
450 if (context_inMacro ())
452 /* Don't use RETURN_TOK */
453 yylval.tok = lltok_create (TENDMACRO, g_currentloc);
454 lastWasString = FALSE;
459 "@@MR@@" { setTokLength (6);
461 if (processMacro ()) {
462 if (context_inIterDef ())
464 RETURN_TOK (LLMACROITER);
466 if (context_inIterEnd ())
468 RETURN_TOK (LLMACROEND);
470 if (context_inMacro ())
472 RETURN_TOK (LLMACRO);
476 "@QLMR" { if (context_inHeader () || context_inFunction ())
482 int nspchar = ninput ();
486 ** This is a hack to get the column number correct.
489 llassert (nspchar >= '0' && nspchar <= '9');
491 nspaces = nspchar - '0';
493 setTokLength (5 + nspaces);
497 if (context_inIterDef ())
499 RETURN_TOK (LLMACROITER);
501 if (context_inIterEnd ())
503 RETURN_TOK (LLMACROEND);
505 if (context_inMacro ())
507 RETURN_TOK (LLMACRO);
512 "@.CT" { setTokLength (4); lldiagmsg (ctype_unparseTable ()); }
513 "@.FA" { setTokLength (4); lldiagmsg (message ("Access types: %q", typeIdSet_unparse (context_fileAccessTypes ()))); }
514 "@.F" { setTokLength (3);
515 lldiagmsg (message ("%q: *** marker ***", fileloc_unparse (g_currentloc)));
517 "@.L" { setTokLength (3); usymtab_printLocal (); }
518 "@.A" { setTokLength (3); lldiagmsg (usymtab_unparseAliases ()); }
519 "@.C" { setTokLength (3); lldiagmsg (context_unparse ()); }
520 "@.W" { setTokLength (3); lldiagmsg (context_unparseClauses ()); }
521 "@.G" { setTokLength (3); usymtab_printGuards (); }
522 "@.S" { setTokLength (3); usymtab_printOut (); }
523 "@.X" { setTokLength (3); usymtab_printAll (); }
524 "@.Z" { setTokLength (3); usymtab_printComplete (); }
525 "@.T" { setTokLength (3); usymtab_printTypes (); }
526 "@.K" { setTokLength (3); lldiagmsg (usymtab_unparseStack ()); }
527 "@.M" { setTokLength (3);
528 lldiagmsg (message ("Can modify: %q",
529 sRefSet_unparse (context_modList ())));
531 "%{" { /* BEFORE_COMMENT_MARKER */
533 incColumn (); incColumn ();
534 tok = handleLlSpecial ();
538 if (tok == CANNOTATION) {
541 /* Beware - this bashes yylval! */
546 "%}" { /* AFTER_COMMENT_MARKER */
549 RETURN_TOK (QENDMACRO); }
550 "\\" { incColumn (); continueLine = TRUE; }
552 if ((int) *yytext == 13 ) {
557 message ("Invalid character (ascii: %d), skipping character",
566 /*@null@*/ /*@observer@*/ char *name;
571 ** These tokens are followed by syntax that is parsed by the
575 struct skeyword s_parsetable[] = {
576 { "modifies", QMODIFIES } ,
577 { "globals", QGLOBALS } ,
580 { "constant", QCONSTANT } ,
581 { "function", QFUNCTION } ,
583 { "defines", QDEFINES } ,
585 { "allocates", QALLOCATES } ,
587 { "releases", QRELEASES } ,
588 { "pre", QPRECLAUSE } ,
589 { "post", QPOSTCLAUSE } ,
590 { "setBufferSize", QSETBUFFERSIZE},
591 { "setStringLength", QSETSTRINGLENGTH},
592 { "testinRange", QTESTINRANGE},
593 { "requires", QPRECLAUSE } ,
594 { "ensures", QPOSTCLAUSE } ,
599 ** These tokens are either stand-alone tokens, or followed by
600 ** token-specific text.
603 struct skeyword s_keytable[] = {
604 { "anytype", QANYTYPE } ,
605 { "integraltype", QINTEGRALTYPE } ,
606 { "unsignedintegraltype", QUNSIGNEDINTEGRALTYPE } ,
607 { "signedintegraltype", QSIGNEDINTEGRALTYPE } ,
611 { "owned", QOWNED } ,
612 { "dependent", QDEPENDENT } ,
613 { "partial", QPARTIAL } ,
614 { "special", QSPECIAL } ,
615 { "truenull", QTRUENULL } ,
616 { "falsenull", QFALSENULL } ,
619 { "notnull", QNOTNULL } ,
620 { "abstract", QABSTRACT } ,
621 { "concrete", QCONCRETE } ,
622 { "mutable", QMUTABLE } ,
623 { "immutable", QIMMUTABLE } ,
624 { "unused", QUNUSED } ,
625 { "external", QEXTERNAL } ,
627 { "unique", QUNIQUE } ,
628 { "returned", QRETURNED } ,
629 { "exposed", QEXPOSED } ,
630 { "refcounted", QREFCOUNTED } ,
632 { "newref", QNEWREF } ,
633 { "tempref", QTEMPREF } ,
634 { "killref", QKILLREF } ,
636 { "relnull", QRELNULL } ,
637 { "nullterminated", QNULLTERMINATED },
638 { "setBufferSize", QSETBUFFERSIZE },
639 { "testInRange", QTESTINRANGE},
640 { "MaxSet", QMAXSET},
641 { "MaxRead", QMAXREAD},
642 { "reldef", QRELDEF } ,
643 { "observer", QOBSERVER } ,
644 { "exits", QEXITS } ,
645 { "mayexit", QMAYEXIT } ,
646 { "trueexit", QTRUEEXIT } ,
647 { "falseexit", QFALSEEXIT } ,
648 { "neverexit", QNEVEREXIT } ,
650 { "shared", QSHARED } ,
652 { "unchecked", QUNCHECKED } ,
653 { "checked", QCHECKED } ,
654 { "checkmod", QCHECKMOD } ,
655 { "checkedstrict", QCHECKEDSTRICT } ,
656 { "innercontinue", QINNERCONTINUE } ,
657 { "innerbreak", QINNERBREAK } ,
658 { "loopbreak", QLOOPBREAK } ,
659 { "switchbreak", QSWITCHBREAK } ,
660 { "safebreak", QSAFEBREAK } ,
661 { "fallthrough", QFALLTHROUGH } ,
662 { "l_fallthrou", QLINTFALLTHROUGH } ,
663 { "l_fallth", QLINTFALLTHRU } ,
664 { "notreached", QNOTREACHED } ,
665 { "l_notreach", QLINTNOTREACHED } ,
666 { "printflike", QPRINTFLIKE } ,
667 { "l_printfli", QLINTPRINTFLIKE } ,
668 { "scanflike", QSCANFLIKE } ,
669 { "messagelike", QMESSAGELIKE } ,
670 { "l_argsus", QARGSUSED } ,
675 ** would be better if these weren't hard coded...
678 static bool isArtificial (cstring s)
680 return (cstring_equalLit (s, "modifies")
681 || cstring_equalLit (s, "globals")
682 || cstring_equalLit (s, "warn")
683 || cstring_equalLit (s, "alt"));
686 void swallowMacro (void)
689 bool skipnext = FALSE;
691 while ((i = lminput ()) != EOF)
708 reader_checkUngetc (i, yyin);
716 reader_checkUngetc (i, yyin);
720 static int commentMarkerToken (cstring s)
724 while (s_parsetable[i].name != NULL)
726 DPRINTF (("Try :%s:%s:", s, s_parsetable[i].name));
728 if (cstring_equalLit (s, s_parsetable[i].name))
730 return s_parsetable[i].token;
739 static int tokenMacroCode (cstring s)
743 while (s_keytable[i].name != NULL)
745 if (cstring_equalLit (s, s_keytable[i].name))
747 if (s_keytable[i].token == QLINTFALLTHROUGH)
750 (FLG_WARNLINTCOMMENTS,
752 ("Traditional lint comment /*FALLTHROUGH*/ used. "
753 "This is interpreted by "
754 "LCLint in the same way as most Unix lints, but it is "
755 "preferable to replace it with the /*@fallthrough@*/ "
760 else if (s_keytable[i].token == QLINTFALLTHRU)
763 (FLG_WARNLINTCOMMENTS,
765 ("Traditional lint comment /*FALLTHRU*/ used. "
766 "This is interpreted by "
767 "LCLint in the same way as most Unix lints, but it is "
768 "preferable to replace it with the /*@fallthrough@*/ "
773 else if (s_keytable[i].token == QLINTNOTREACHED)
776 (FLG_WARNLINTCOMMENTS,
778 ("Traditional lint comment /*NOTREACHED*/ used. "
779 "This is interpreted by "
780 "LCLint in the same way as most Unix lints, but it is "
781 "preferable to replace it with the /*@notreached@*/ "
782 "semantic comment."),
787 else if (s_keytable[i].token == QPRINTFLIKE)
789 setSpecialFunction (qual_createPrintfLike ());
792 else if (s_keytable[i].token == QLINTPRINTFLIKE)
795 (FLG_WARNLINTCOMMENTS,
797 ("Traditional lint comment /*PRINTFLIKE*/ used. "
798 "This is interpreted by "
799 "LCLint in the same way as most Unix lints, but it is "
800 "preferable to replace it with either /*@printflike@*/, "
801 "/*@scanflike@*/ or /*@messagelike@*/."),
804 setSpecialFunction (qual_createPrintfLike ());
807 else if (s_keytable[i].token == QSCANFLIKE)
809 setSpecialFunction (qual_createScanfLike ());
812 else if (s_keytable[i].token == QMESSAGELIKE)
814 setSpecialFunction (qual_createMessageLike ());
817 else if (s_keytable[i].token == QARGSUSED)
820 (FLG_WARNLINTCOMMENTS,
822 ("Traditional lint comment /*ARGSUSED*/ used. "
823 "This is interpreted by "
824 "LCLint in the same way as most Unix lints, but it is "
825 "preferable to use /*@unused@*/ annotations on "
826 "the unused parameters."),
833 return s_keytable[i].token;
842 static int lminput ()
844 if (savechar == '\0')
851 int save = (int) savechar;
857 static void lmsavechar (char c)
859 if (savechar == '\0') savechar = c;
862 llbuglit ("lmsavechar: override");
866 static int returnFloat (ctype ct, double f)
868 yylval.expr = exprNode_floatLiteral (f, ct, cstring_fromChars (yytext),
869 fileloc_decColumn (g_currentloc, tokLength));
874 static int returnInt (ctype ct, long i)
878 if (ctype_equal (ct, ctype_int))
882 c = context_typeofZero ();
886 c = context_typeofOne ();
890 yylval.expr = exprNode_numLiteral (c, cstring_fromChars (yytext),
891 fileloc_decColumn (g_currentloc, tokLength), i);
896 static int returnChar (char c)
898 yylval.expr = exprNode_charLiteral (c, cstring_fromChars (yytext),
899 fileloc_decColumn (g_currentloc, tokLength));
908 if (c != EOF && ((char)c == '\n'))
910 context_incLineno ();
916 static char macro_nextChar ()
918 static bool in_quote = FALSE, in_escape = FALSE, in_char = FALSE;
923 c = char_fromInt (ic);
925 if (!in_quote && !in_char && (c == '\\' || c == BEFORE_COMMENT_MARKER[0]))
929 while ((c = char_fromInt (lminput ())) != '\0' && c != '\n')
931 ; /* skip to newline */
934 context_incLineno ();
938 return macro_nextChar ();
945 else /* if (c == '@') */
947 llassert (FALSE); /*@i23@*/
948 if (handleLlSpecial () != BADTOK)
950 llerrorlit (FLG_SYNTAX, "Macro cannot use special syntax");
953 return macro_nextChar ();
956 else if (!in_escape && c == '\"')
958 in_quote = !in_quote;
960 else if (!in_escape && c == '\'')
964 else if ((in_quote || in_char) && c == '\\')
966 in_escape = !in_escape;
968 else if ((in_quote || in_char) && in_escape)
972 else if (!in_quote && c == '/')
976 if ((c2 = char_fromInt (lminput ())) == '*')
980 while ((c2 = char_fromInt (lminput ())) != '\0'
981 && c2 != '\n' && c2 != '*')
988 while ((c2 = char_fromInt (lminput ())) != '\0'
1001 llfatalerror (cstring_makeLiteral ("Macro: bad comment!"));
1005 return macro_nextChar ();
1009 /*** putchar does not work! why? puts to stdio...??! ***/
1017 ** keeps semantic comments
1020 static char macro_nextCharC ()
1022 static bool in_quote = FALSE, in_escape = FALSE, in_char = FALSE;
1025 c = char_fromInt (lminput ());
1027 if (!in_quote && !in_char && c == '\\')
1029 while ((c = char_fromInt (lminput ())) != '\0' && c != '\n')
1031 ; /* skip to newline */
1034 context_incLineno ();
1038 return macro_nextCharC ();
1045 else if (!in_escape && c == '\"')
1047 in_quote = !in_quote;
1049 else if (!in_escape && c == '\'')
1053 else if ((in_quote || in_char) && c == '\\')
1055 in_escape = !in_escape;
1057 else if ((in_quote || in_char) && in_escape)
1061 else if (!in_quote && c == '/')
1065 if ((c2 = char_fromInt (lminput ())) == '*')
1069 while ((c2 = char_fromInt (lminput ())) != '\0'
1070 && c2 != '\n' && c2 != '*')
1077 while ((c2 = char_fromInt (lminput ())) != '\0'
1090 llfatalerror (cstring_makeLiteral ("Macro: bad comment!"));
1094 return macro_nextCharC ();
1105 ** skips whitespace (handles line continuations)
1106 ** returns first non-whitespace character
1109 static char skip_whitespace ()
1113 while ((c = macro_nextChar ()) == ' ' || c == '\t')
1121 static void handleMacro ()
1123 cstring mac = cstring_undefined;
1127 while (currentColumn () > 2)
1129 mac = cstring_appendChar (mac, ' ');
1133 c = macro_nextCharC ();
1135 if (c >= '0' && c <= '9')
1139 for (i = 0; i < ((c - '0') + 1); i++)
1141 mac = cstring_appendChar (mac, ' ');
1149 while (((c = macro_nextCharC ()) != '\0') && (c != '\n'))
1151 mac = cstring_appendChar (mac, c);
1155 macrocode = tokenMacroCode (mac);
1157 if (macrocode == BADTOK && !isArtificial (mac))
1159 context_addMacroCache (mac);
1168 context_incLineno ();
1172 static bool processMacro (void)
1177 cstring fname = cstring_undefined;
1179 bool isspecfcn = FALSE;
1180 bool isiter = FALSE;
1181 bool skipparam = FALSE;
1182 bool isenditer = FALSE;
1183 bool unknownm = FALSE;
1184 bool hasParams = FALSE;
1185 bool emptyMacro = FALSE;
1186 char c = skip_whitespace ();
1187 fileloc loc = fileloc_noColumn (g_currentloc);
1189 /* are both of these necessary? what do they mean? */
1190 uentryList specparams = uentryList_undefined;
1191 uentryList pn = uentryList_undefined;
1193 context_resetMacroMissingParams ();
1195 if (c == '\0' || c == '\n')
1197 llcontbug (cstring_makeLiteral ("Bad macro"));
1202 fname = cstring_appendChar (fname, c);
1204 while ((c = macro_nextChar ()) != '(' && c != '\0'
1205 && c != ' ' && c != '\t' && c != '\n')
1207 fname = cstring_appendChar (fname, c);
1210 if (c == ' ' || c == '\t' || c == '\n')
1216 while (c == ' ' || c == '\t')
1218 c = macro_nextChar ();
1232 hasParams = (c == '(');
1235 if (usymtab_exists (fname))
1237 e2 = usymtab_lookupExpose (fname);
1238 ct = uentry_getType (e2);
1241 if (uentry_isCodeDefined (e2)
1242 && fileloc_isUser (uentry_whereDefined (e2)))
1246 message ("Macro %s already defined", fname),
1249 uentry_showWhereDefined (e2);
1250 uentry_clearDefined (e2);
1253 if (uentry_isFunction (e2))
1255 uentry_setType (e2, ctype_unknown);
1258 context_enterUnknownMacro (e2);
1262 context_enterConstantMacro (e2);
1267 if (uentry_isForward (e2) && uentry_isFunction (e2))
1274 ("Parameterized macro has no prototype or specification: %s ",
1279 uentry_setType (e2, ctype_unknown);
1280 uentry_setFunctionDefined (e2, loc);
1281 uentry_setUsed (e2, fileloc_undefined);
1282 context_enterUnknownMacro (e2);
1286 if (uentry_isIter (e2))
1289 specparams = uentry_getParams (e2);
1290 noparams = uentryList_size (specparams);
1291 uentry_setDefined (e2, loc);
1292 context_enterIterDef (e2);
1294 else if (uentry_isEndIter (e2))
1297 uentry_setDefined (e2, loc);
1298 context_enterIterEnd (e2); /* don't care about it now */
1299 /* but should parse like an iter! */
1301 else if (uentry_isConstant (e2))
1307 message ("Constant %s implemented as parameterized macro",
1311 uentry_showWhereSpecified (e2);
1312 uentry_setType (e2, ctype_unknown);
1313 uentry_makeVarFunction (e2);
1314 uentry_setDefined (e2, g_currentloc);
1315 uentry_setFunctionDefined (e2, g_currentloc);
1316 context_enterUnknownMacro (e2);
1320 if (!uentry_isSpecified (e2))
1322 fileloc oloc = uentry_whereDeclared (e2);
1324 if (fileloc_isLib (oloc))
1328 else if (fileloc_isUndefined (oloc)
1329 || fileloc_isPreproc (oloc))
1334 (FLG_MACROCONSTDECL,
1336 ("Macro constant %q not declared",
1337 uentry_getName (e2)),
1341 else if (!fileloc_withinLines (oloc, loc, 2))
1342 { /* bogus! will give errors if there is too much whitespace */
1346 ("Macro constant name %s does not match name in "
1347 "previous constant declaration. This constant "
1348 "is declared at %q", fname,
1349 fileloc_unparse (oloc)),
1354 context_enterConstantMacro (e2);
1355 cstring_free (fname);
1361 else if (ctype_isFunction (ct))
1364 specparams = ctype_argsFunction (ct);
1365 noparams = uentryList_size (specparams);
1367 uentry_setFunctionDefined (e2, loc);
1368 context_enterMacro (e2);
1370 else if (uentry_isVar (e2))
1376 message ("Variable %s implemented as parameterized macro",
1380 uentry_showWhereSpecified (e2);
1381 uentry_setType (e2, ctype_unknown);
1382 uentry_makeVarFunction (e2);
1383 uentry_setDefined (e2, g_currentloc);
1384 uentry_setFunctionDefined (e2, g_currentloc);
1385 context_enterUnknownMacro (e2);
1389 uentry ucons = uentry_makeConstant (fname,
1392 if (uentry_isExpandedMacro (e2))
1400 message ("Variable %s implemented by a macro",
1404 uentry_showWhereSpecified (e2);
1408 uentry_setDefined (e2, loc);
1409 uentry_setUsed (ucons, loc);
1411 context_enterConstantMacro (ucons);
1412 uentry_markOwned (ucons);
1413 cstring_free (fname);
1419 if (uentry_isDatatype (e2))
1423 message ("Type implemented as macro: %x",
1424 uentry_getName (e2)),
1425 message ("A type is implemented using a macro definition. A "
1426 "typedef should be used instead."),
1430 /* Must exit scope (not sure why a new scope was entered?) */
1431 usymtab_quietExitScope (g_currentloc);
1432 uentry_setDefined (e2, g_currentloc);
1438 (message ("Unexpanded macro not function or constant: %q",
1439 uentry_unparse (e2)));
1440 uentry_setType (e2, ctype_unknown);
1444 uentry_makeVarFunction (e2);
1445 uentry_setDefined (e2, g_currentloc);
1446 uentry_setFunctionDefined (e2, g_currentloc);
1447 context_enterUnknownMacro (e2);
1459 (FLG_MACROMATCHNAME,
1460 message ("Unexpanded macro %s does not match name of a constant "
1461 "or iter declaration. The name used in the control "
1462 "comment on the previous line should match. "
1463 "(Assuming macro defines a constant.)",
1468 ce = uentry_makeConstant (fname, ctype_unknown, fileloc_undefined);
1469 uentry_setUsed (ce, loc); /* perhaps bogus? */
1470 e2 = usymtab_supEntryReturn (ce);
1472 context_enterConstantMacro (e2);
1473 cstring_free (fname);
1478 /* in macros, ( must follow immediatetly after name */
1484 c = skip_whitespace ();
1486 while (c != ')' && c != '\0')
1489 bool suppress = context_inSuppressRegion ();
1490 cstring paramname = cstring_undefined;
1493 ** save the parameter location
1497 context_saveLocation ();
1500 while (c != ' ' && c != '\t' && c != ',' && c != '\0' && c != ')')
1502 paramname = cstring_appendChar (paramname, c);
1503 c = macro_nextChar ();
1506 if (c == ' ' || c == '\t') c = skip_whitespace ();
1510 c = macro_nextChar ();
1511 if (c == ' ' || c == '\t') c = skip_whitespace ();
1516 llfatalerror (cstring_makeLiteral
1517 ("Bad macro syntax: uentryList"));
1520 if ((isspecfcn || isiter) && (paramno < noparams)
1521 && !uentry_isElipsisMarker (uentryList_getN
1522 (specparams, paramno)))
1524 uentry decl = uentryList_getN (specparams, paramno);
1527 param = uentry_nameCopy (paramname, decl);
1530 uentry_setParam (param);
1531 sr = sRef_makeParam (paramno, uentry_getType (param));
1533 if (sRef_getNullState (sr) == NS_ABSNULL)
1535 ctype pt = ctype_realType (uentry_getType (param));
1537 if (ctype_isUser (pt))
1539 uentry te = usymtab_getTypeEntrySafe (ctype_typeId (pt));
1541 if (uentry_isValid (te))
1543 sRef_setStateFromUentry (sr, te);
1548 sRef_setNullState (sr, NS_UNKNOWN, g_currentloc);
1552 uentry_setSref (param, sr);
1553 uentry_setDeclaredForceOnly (param, context_getSaveLocation ());
1555 skipparam = isiter && uentry_isOut (uentryList_getN (specparams, paramno));
1559 fileloc sloc = context_getSaveLocation ();
1561 param = uentry_makeVariableSrefParam
1562 (paramname, ctype_unknown, sRef_makeParam (paramno, ctype_unknown));
1563 cstring_free (paramname);
1565 sRef_setPosNull (uentry_getSref (param), sloc);
1567 uentry_setDeclaredForce (param, sloc);
1570 fileloc_free (sloc);
1575 llassert (!uentry_isElipsisMarker (param));
1579 sRef_makeUnsafe (uentry_getSref (param));
1582 pn = uentryList_add (pn, uentry_copy (param));
1583 usymtab_supEntry (param);
1587 /* don't add param */
1588 uentry_free (param);
1593 (void) macro_nextChar ();
1594 c = skip_whitespace ();
1602 if (isspecfcn || isiter)
1604 if (paramno != noparams && noparams >= 0)
1610 message ("Macro %s specified with %d args, defined with %d",
1611 fname, noparams, paramno),
1614 uentry_showWhereSpecified (e2);
1615 uentry_resetParams (e2, pn);
1620 uentry_resetParams (e2, pn);
1627 ** the form should be:
1629 ** # define newname oldname
1630 ** where oldname refers to a function matching the specification
1636 sRef_setGlobalScope ();
1637 usymtab_supGlobalEntry (uentry_makeVariableLoc (fname, ctype_unknown));
1638 sRef_clearGlobalScope ();
1642 context_setMacroMissingParams ();
1647 /* context_setuentryList (pn); */
1648 usymtab_enterScope ();
1651 cstring_free (fname);
1656 static bool handleSpecial (char *yyt)
1658 char *l = mstring_create (MAX_NAME_LENGTH);
1659 static bool reportcpp = FALSE;
1665 strcpy (l, yyt + 1);
1667 /* Need to safe original l for deallocating. */
1670 l += strlen (yyt) - 1;
1672 while ((c = char_fromInt (lminput ())) != '\n' && c != '\0')
1678 olc = cstring_fromChars (ol);
1680 if (cstring_equalPrefix (olc, "pragma"))
1682 char *pname = mstring_create (longUnsigned_fromInt (MAX_PRAGMA_LEN));
1683 char *opname = pname;
1684 char *ptr = ol + 6; /* pragma is six characters, plus space */
1688 /* skip whitespace */
1689 while (((c = *ptr) != '\0') && isspace (c))
1695 while (((c = *ptr) != '\0') && !isspace (c))
1699 if (len > MAX_PRAGMA_LEN)
1710 if (len == PRAGMA_LEN_EXPAND
1711 && mstring_equal (opname, PRAGMA_EXPAND))
1713 cstring exname = cstring_undefined;
1717 while (((c = *ptr) != '\0') && !isspace (c))
1719 exname = cstring_appendChar (exname, c);
1724 ue = usymtab_lookupExposeGlob (exname);
1726 if (uentry_isExpandedMacro (ue))
1728 if (fileloc_isPreproc (uentry_whereDefined (ue)))
1730 fileloc_setColumn (g_currentloc, 1);
1731 uentry_setDefined (ue, g_currentloc);
1735 cstring_free (exname);
1738 else if (cstring_equalPrefix (olc, "ident"))
1740 /* Some pre-processors will leave these in the code. Ignore rest of line */
1743 ** Yuk...Win32 filenames can have spaces in them...we need to read
1744 ** to the matching end quote.
1746 else if ((sscanf (ol, "line %d \"", &lineno) == 1)
1747 || (sscanf (ol, " %d \"", &lineno) == 1))
1753 /*@access cstring@*/
1754 while (*tmp != '\"' && *tmp != '\0')
1759 llassert (*tmp == '\"');
1765 while (*tmp != '\"' && *tmp != '\0')
1770 llassert (*tmp == '\"');
1774 # if defined(OS2) || defined(MSDOS) || defined(WIN32)
1777 ** DOS-like path delimiters get delivered in pairs, something like
1778 ** \"..\\\\file.h\", so we have to make it normal again. We do NOT
1779 ** remove the pre dirs yet as we usually specify tmp paths relative
1780 ** to the current directory, so tmp files would not get found in
1781 ** the hash table. If this method fails we try it again later.
1788 ** Skip past the drive marker.
1791 if (strchr (stmp, ':') != NULL)
1793 stmp = strchr (stmp, ':') + 1;
1796 while ((stmp = strchr (stmp, CONNECTCHAR)) != NULL )
1798 if (*(stmp+1) == CONNECTCHAR)
1800 memmove (stmp, stmp+1, strlen (stmp));
1806 fid = fileTable_lookupBase (context_fileTable (), fname);
1807 if (!(fileId_isValid (fid)))
1809 fname = removePreDirs (fname);
1810 fid = fileTable_lookupBase (context_fileTable (), fname);
1813 # else /* !defined(OS2) && !defined(MSDOS) */
1814 fname = removePreDirs (fname);
1815 fid = fileTable_lookupBase (context_fileTable (), fname);
1816 # endif /* !defined(OS2) && !defined(MSDOS) */
1818 if (!(fileId_isValid (fid)))
1820 if (isHeaderFile (fname))
1822 fid = fileTable_addHeaderFile (context_fileTable (), fname);
1826 fid = fileTable_addFile (context_fileTable (), fname);
1830 setFileLine (fid, lineno);
1831 /*@noaccess cstring@*/
1833 else if ((sscanf (ol, "line %d", &lineno) == 1)
1834 || (sscanf (ol, " %d", &lineno) == 1))
1836 setLine (lineno); /* next line is <cr> */
1840 if (mstring_equal (ol, "")) {
1841 DPRINTF (("Empty pp command!"));
1843 ** evs 2000-05-16: This is a horrible kludge, to get around a bug (well, difficulty) in the pre-processor.
1844 ** We handle a plain # in the input file, by echoing it, and ignoring it in the post-pp-file.
1853 llbug (message ("File contains preprocessor command: #%s",
1854 cstring_fromChars (ol)));
1867 static int handleLlSpecial ()
1871 char *s = mstring_createEmpty ();
1877 loc = fileloc_copy (g_currentloc);
1878 DPRINTF (("Handle special: %s", fileloc_unparse (loc)));
1880 while (((ic = ninput ()) != 0) && isalpha (ic))
1883 s = mstring_append (s, c);
1887 DPRINTF (("Read: %s / %s", s, fileloc_unparse (g_currentloc)));
1890 if (charsread == 0 && ic == (int) AFTER_COMMENT_MARKER[0])
1894 llassert (ic == AFTER_COMMENT_MARKER[1]);
1900 return QNOMODS; /* special token no modifications token */
1904 DPRINTF (("Coment marker: %s", os));
1905 tok = commentMarkerToken (cstring_fromChars (os));
1909 tokLength = charsread;
1916 DPRINTF (("Not a comment marker..."));
1917 /* Add rest of the comment */
1919 if (ic != 0 && ic != EOF)
1923 s = mstring_append (s, c);
1926 while (((ic = ninput ()) != 0) && (ic != EOF)
1927 && (ic != AFTER_COMMENT_MARKER[0]))
1930 s = mstring_append (s, c);
1935 DPRINTF (("Read: %s / %s", s, fileloc_unparse (g_currentloc)));
1937 if (ic == AFTER_COMMENT_MARKER[0])
1940 llassert ((char) nc == AFTER_COMMENT_MARKER[1]);
1946 while (*s == ' ' || *s == '\t' || *s == '\n')
1951 if (*s == '-' || *s == '+' || *s == '=') /* setting flags */
1955 while (c == '-' || c == '+' || c == '=')
1957 ynm set = ynm_fromCodeChar (c);
1962 thisflag = cstring_fromChars (s);
1964 while ((c = *s) != '\0' && (c != '-') && (c != '=')
1965 && (c != '+') && (c != ' ') && (c != '\t') && (c != '\n'))
1972 if (!context_getFlag (FLG_NOCOMMENTS))
1974 cstring flagname = thisflag;
1975 flagcode fflag = identifyFlag (flagname);
1977 if (flagcode_isSkip (fflag))
1981 else if (flagcode_isInvalid (fflag))
1983 if (isMode (flagname))
1985 if (ynm_isMaybe (set))
1990 ("Semantic comment attempts to restore flag %s. "
1991 "A mode flag cannot be restored.",
1996 context_setMode (flagname);
2002 (FLG_UNRECOGFLAGCOMMENTS,
2003 message ("Unrecognized option in semantic comment: %s",
2008 else if (flagcode_isGlobalFlag (fflag))
2013 ("Semantic comment attempts to set global flag %s. "
2014 "A global flag cannot be set locally.",
2020 context_fileSetFlag (fflag, set);
2022 if (flagcode_hasArgument (fflag))
2024 if (ynm_isMaybe (set))
2029 ("Semantic comment attempts to restore flag %s. "
2030 "A flag for setting a value cannot be restored.",
2035 { /* cut-and-pastied from llmain...blecch */
2036 cstring extra = cstring_undefined;
2042 rest = mstring_copy (s);
2046 while ((rchar = *rest) != '\0'
2047 && (isspace (rchar)))
2053 while ((rchar = *rest) != '\0'
2054 && !isspace (rchar))
2056 extra = cstring_appendChar (extra, rchar);
2063 if (cstring_isUndefined (extra))
2068 ("Flag %s (in semantic comment) must be followed by an argument",
2069 flagcode_unparse (fflag)));
2075 if (flagcode_hasValue (fflag))
2077 setValueFlag (fflag, extra);
2079 else if (flagcode_hasString (fflag))
2081 setStringFlag (fflag, extra);
2098 while ((c == ' ') || (c == '\t') || (c == '\n'))
2104 if (context_inHeader () && !isArtificial (cstring_fromChars (os)))
2106 DPRINTF (("Here adding comment: %s", os));
2107 context_addComment (cstring_fromCharsNew (os));
2119 annotationInfo ainfo;
2121 while (*s != '\0' && *s != ' ' && *s != '\t' && *s != '\n')
2133 t = cstring_toCharsSafe (cstring_downcase (cstring_fromChars (t)));
2134 macrocode = tokenMacroCode (cstring_fromChars (t));
2136 if (macrocode != BADTOK)
2138 tokLength = mstring_length (t);
2144 if (macrocode == SKIPTOK)
2152 ainfo = context_lookupAnnotation (cstring_fromChars (os));
2154 if (annotationInfo_isDefined (ainfo)) {
2155 DPRINTF (("Found annotation: %s", annotationInfo_unparse (ainfo)));
2156 /*@i324@*/ yylval.annotation = ainfo;
2164 if (context_inHeader ())
2171 if ((context_inMacro () || context_inGlobalContext ())
2172 && macrocode != SKIPTOK
2173 && !isArtificial (cstring_fromChars (os)))
2175 DPRINTF (("Add comment: %s", os));
2176 context_addComment (cstring_fromCharsNew (os));
2189 if (mstring_equal (t, "ignore"))
2191 if (!context_getFlag (FLG_NOCOMMENTS))
2193 context_enterSuppressRegion ();
2196 else if ((*t == 'i' || *t == 't')
2197 && (*(t + 1) == '\0'))
2199 if (!context_getFlag (FLG_NOCOMMENTS)
2200 && (*t == 'i' || context_getFlag (FLG_TMPCOMMENTS)))
2202 context_enterSuppressLine (-1); /* infinite suppression */
2205 else if (((*t == 'i') || (*t == 't'))
2206 && ((*(t + 1) >= '0' && *(t + 1) <= '9')))
2208 bool tmpcomment = (*t == 't');
2210 char *tt = t; /* don't mangle t, since it is free'd */
2213 if (lc >= '0' && lc <= '9')
2215 val = (int)(lc - '0');
2218 while (lc >= '0' && lc <= '9')
2227 if (!context_getFlag (FLG_NOCOMMENTS)
2228 && (!tmpcomment || context_getFlag (FLG_TMPCOMMENTS)))
2230 context_enterSuppressLine (val);
2233 else if (mstring_equal (t, "end"))
2235 if (!context_getFlag (FLG_NOCOMMENTS))
2237 context_exitSuppressRegion ();
2240 else if (mstring_equal (t, "notfunction"))
2242 ; /* handled by pcpp */
2244 else if (mstring_equal (t, "access"))
2250 while ((c = *s) && (c == ' ' || c == '\t' || c == '\n'))
2260 tname = cstring_fromChars (s);
2262 while ((c = *s) != '\0' && c != ' '
2263 && c != '\t' && c != '\n' && c != ',')
2270 DPRINTF (("Access %s", tname));
2272 if (!context_getFlag (FLG_NOCOMMENTS)
2273 && !context_getFlag (FLG_NOACCESS))
2275 if (usymtab_existsType (tname))
2277 typeId uid = usymtab_getTypeId (tname);
2278 uentry ue = usymtab_getTypeEntry (uid);
2280 if (uentry_isAbstractDatatype (ue))
2282 context_addFileAccessType (uid);
2283 DPRINTF (("Adding access to: %s / %d", tname, uid));
2290 ("Non-abstract type %s used in access comment",
2297 if (!(context_inSuppressRegion ()
2298 || context_inSuppressZone (g_currentloc)))
2303 ("Unrecognized type %s used in access comment",
2315 if (c != ',' && c != ' ')
2321 else if (mstring_equal (t, "noaccess"))
2328 while ((lc = *s) && (lc == ' ' || lc == '\t' || lc == '\n'))
2338 tname = cstring_fromChars (s);
2340 while ((lc = *s) != '\0' && lc != ' ' && lc != '\t'
2341 && lc != '\n' && lc != ',')
2348 if (!context_getFlag (FLG_NOCOMMENTS)
2349 && !context_getFlag (FLG_NOACCESS))
2351 if (usymtab_existsType (tname))
2353 typeId tuid = usymtab_getTypeId (tname);
2355 if (context_couldHaveAccess (tuid))
2357 DPRINTF (("Removing access: %s", tname));
2358 context_removeFileAccessType (tuid);
2362 if (!(context_inSuppressRegion ()
2363 || context_inSuppressZone (g_currentloc)))
2365 uentry ue = usymtab_getTypeEntry (tuid);
2367 if (uentry_isAbstractDatatype (ue))
2372 ("Non-accessible abstract type %s used in noaccess comment",
2381 ("Non-abstract type %s used in noaccess comment",
2390 if (!(context_inSuppressRegion ()
2391 || context_inSuppressZone (g_currentloc)))
2396 ("Unrecognized type %s used in noaccess comment",
2408 if (lc != ',' && lc != ' ')
2416 voptgenerror (FLG_UNRECOGCOMMENTS,
2417 message ("Semantic comment unrecognized: %s",
2418 cstring_fromChars (os)), loc);
2429 static /*@only@*/ cstring makeIdentifier (char *s)
2431 char *c = mstring_create (size_toInt (strlen (s)) + 1);
2432 cstring id = cstring_fromChars (c);
2434 while (isalnum (*s) || (*s == '_') || (*s == '$'))
2443 /*@observer@*/ /*@dependent@*/ uentry coerceId (cstring cn)
2445 if (!(usymtab_exists (cn)))
2447 fileloc loc = fileloc_createExternal ();
2450 ** We need to put this in a global scope, otherwise the sRef will be deallocated.
2453 uentry ce = uentry_makeUnrecognized (cn, loc);
2455 if (!context_inIterEnd ())
2459 message ("Unrecognized (possibly system) identifier: %q",
2460 uentry_getName (ce)),
2467 return (usymtab_lookup (cn));
2471 ** like, coerceId, but doesn't supercede for iters
2474 /*@observer@*/ uentry coerceIterId (cstring cn)
2476 if (!(usymtab_exists (cn)))
2478 return uentry_undefined;
2481 return (usymtab_lookup (cn));
2484 /*@observer@*/ cstring LastIdentifier ()
2486 return (lastidprocessed);
2489 static int processIdentifier (cstring id)
2493 DPRINTF (("Process identifier: %s / %s / %s", id, fileloc_unparse (g_currentloc),
2494 bool_unparse (context_inFunctionHeader ())));
2496 context_clearJustPopped ();
2497 lastidprocessed = id;
2499 if (context_inFunctionHeader ())
2501 int tok = commentMarkerToken (id);
2502 DPRINTF (("in function decl..."));
2510 tok = tokenMacroCode (id);
2518 annotationInfo ainfo;
2520 if (expectingMetaStateName)
2522 metaStateInfo msinfo = context_lookupMetaStateInfo (id);
2524 if (metaStateInfo_isDefined (msinfo))
2526 yylval.msinfo = msinfo;
2527 return METASTATE_NAME;
2531 TPRINTF (("Not meta state name: %s", cstring_toCharsSafe (id)));
2535 ainfo = context_lookupAnnotation (id);
2537 if (annotationInfo_isDefined (ainfo))
2539 DPRINTF (("Found annotation: %s", annotationInfo_unparse (ainfo)));
2540 /*@i324@*/ yylval.annotation = ainfo;
2545 DPRINTF (("Not annotation: %s", id));
2551 /* Consider handling: Defined by C99 as static const char __func__[] */
2553 if (context_getFlag (FLG_GNUEXTENSIONS))
2557 if (cstring_equalLit (id, "__stdcall")
2558 || cstring_equalLit (id, "__cdecl")
2559 || cstring_equalLit (id, "__extension__"))
2563 else if (cstring_equalLit (id, "__volatile__"))
2567 else if (cstring_equalLit (id, "__signed"))
2571 else if (cstring_equalLit (id, "__unsigned"))
2575 else if (cstring_equalLit (id, "__const__"))
2579 else if (cstring_equalLit (id, "__alignof__"))
2581 tok = CALIGNOF; /* alignof is parsed like sizeof */
2583 else if (cstring_equalLit (id, "__FUNCTION__")
2584 || cstring_equalLit (id, "__PRETTY_FUNCTION__"))
2586 /* These tokens hold the name of the current function as strings */
2587 yylval.expr = exprNode_stringLiteral (id, fileloc_copy (g_currentloc));
2589 lastWasString = TRUE;
2593 else if (cstring_equalLit (id, "__attribute__")
2594 || cstring_equalLit (id, "__asm__")
2595 || cstring_equalLit (id, "_asm")
2596 || cstring_equalLit (id, "__asm")
2597 || cstring_equalLit (id, "__declspec"))
2600 bool useparens = FALSE;
2601 bool usebraces = FALSE;
2602 bool inquote = FALSE;
2603 bool inescape = FALSE;
2606 while ((ic = input ()) != EOF)
2613 else if (ic == '\\')
2617 else if (ic == '\"')
2653 else if (ic == ')' && useparens)
2656 if (depth == 0) break;
2658 else if (ic == '}' && usebraces)
2661 if (depth == 0) break;
2664 && !usebraces && !useparens
2665 && cstring_equalLit (id, "__asm"))
2668 ** We need this because some MS VC++ include files
2669 ** have __asm mov ... }
2670 ** Its a kludge, but otherwise would need to parse
2679 context_incLineno ();
2681 if (cstring_equalLit (id, "__asm")
2682 && !useparens && !usebraces)
2689 llassert ((useparens && ic == ')')
2690 || (usebraces && ic == '}')
2691 || (!useparens && !usebraces));
2695 else if (cstring_equalLit (id, "inline")
2696 || cstring_equalLit (id, "__inline")
2697 || cstring_equalLit (id, "_inline")
2698 || cstring_equalLit (id, "__inline__"))
2709 le = usymtab_lookupSafe (id);
2711 /*@-dependenttrans@*/
2713 if (uentry_isIter (le))
2715 /*@i32@*/ yylval.entry = le;
2718 else if (uentry_isEndIter (le))
2720 /*@i32@*/ yylval.entry = le;
2721 return (ITER_ENDNAME);
2723 else if (uentry_isUndefined (le))
2727 /* avoid parse errors for certain system built ins */
2729 if (g_expectingTypeName && (cstring_firstChar (id) == '_')
2730 && (cstring_secondChar (id) == '_'))
2732 return (TYPE_NAME_OR_ID);
2735 return (NEW_IDENTIFIER);
2737 else if (!uentry_isDeclared (le) && !uentry_isCodeDefined (le))
2739 if (uentry_isDatatype (le))
2742 return (NEW_IDENTIFIER);
2746 /*@i32@*/ yylval.entry = le;
2747 return (IDENTIFIER);
2750 else if (uentry_isDatatype (le))
2752 if (!g_expectingTypeName)
2756 return (NEW_IDENTIFIER);
2760 yylval.ctyp = uentry_getAbstractType (le);
2762 uentry_setUsed (le, g_currentloc);
2768 /*@i32@*/ yylval.entry = le;
2769 return (IDENTIFIER);
2772 /*@=dependenttrans@*/
2775 static bool processHashIdentifier (/*@only@*/ cstring id)
2777 if (context_inMacro () || context_inIterDef () ||
2778 context_inIterEnd ())
2782 context_clearJustPopped ();
2784 lastidprocessed = id;
2785 le = usymtab_lookupSafe (id);
2787 if (uentry_isParam (le) || uentry_isRefParam (le))
2804 static /*@only@*/ exprNode processString ()
2808 char *nl = strchr (yytext, '\n');
2809 cstring ns = cstring_fromCharsNew (yytext);
2813 loc = fileloc_copy (g_currentloc);
2814 addColumn (cstring_length (ns));
2820 loc = fileloc_copy (g_currentloc);
2822 context_incLineno ();
2824 while ((nl = strchr ((nl + 1), '\n')) != NULL)
2826 context_incLineno ();
2832 res = exprNode_stringLiteral (ns, loc);
2842 llassert (*yytext != '\0');
2843 fchar = *(yytext + 1);
2844 if (fchar != '\\') return fchar;
2846 next = *(yytext + 2);
2850 case 'n': return '\n';
2851 case 't': return '\t';
2852 case '\"': return '\"';
2853 case '\'': return '\'';
2854 case '\\': return '\\';
2855 default: return '\0';
2860 double processFloat ()
2862 double ret = atof (yytext);
2873 llassert (yytext[0] == '0'
2874 && (yytext[1] == 'X' || yytext[1] == 'x'));
2876 while (yytext[index] != '\0') {
2878 char c = yytext[index];
2880 if (c >= '0' && c <= '9') {
2881 tval = (int) c - (int) '0';
2882 } else if (c >= 'A' && c <= 'F') {
2883 tval = (int) c - (int) 'A' + 10;
2884 } else if (c >= 'a' && c <= 'f') {
2885 tval = (int) c - (int) 'a' + 10;
2886 } else if (c == 'U' || c == 'L' || c == 'u' || c == 'l') {
2888 while (yytext[index] != '\0') {
2889 if (c == 'U' || c == 'L' || c == 'u' || c == 'l') {
2894 message ("Invalid character (%c) following specifier in hex constant: %s",
2895 c, cstring_fromChars (yytext)),
2905 message ("Invalid character (%c) in hex constant: %s",
2906 c, cstring_fromChars (yytext)),
2911 val = (val * 16) + tval;
2915 DPRINTF (("Hex constant: %s = %ld", yytext, val));
2920 long processOctal ()
2925 llassert (yytext[0] == '0' && yytext[1] != 'X' && yytext[1] != 'x');
2927 while (yytext[index] != '\0') {
2929 char c = yytext[index];
2931 if (c >= '0' && c <= '7') {
2932 tval = (int) c - (int) '0';
2936 message ("Invalid character (%c) in octal constant: %s",
2937 c, cstring_fromChars (yytext)),
2942 val = (val * 8) + tval;
2946 DPRINTF (("Octal constant: %s = %ld", yytext, val));
2953 return (atol (yytext));
2957 processSpec (int tok)
2959 size_t length = strlen (yytext);
2964 setTokLengthT (length);
2970 context_saveLocation ();
2971 setTokLengthT (length);
2972 return (processIdentifier (makeIdentifier (yytext)));
2976 void cscanner_expectingMetaStateName ()
2978 llassert (!expectingMetaStateName);
2979 llassert (context_inFunctionHeader ());
2980 expectingMetaStateName = TRUE;
2983 void cscanner_clearExpectingMetaStateName ()
2985 llassert (expectingMetaStateName);
2986 expectingMetaStateName = FALSE;