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));}
314 "maxSet" { return (processSpec (QMAXSET));}
315 "maxRead" { return (processSpec (QMAXREAD));}
317 {Letter}({Letter}|{Digit})* { int tok;
318 context_saveLocation ();
319 setTokLength (longUnsigned_toInt (mstring_length (yytext)));
320 tok = processIdentifier (makeIdentifier (yytext));
326 0[xX]{H}+ { setTokLengthT (mstring_length (yytext));
327 RETURN_INT (ctype_int, processHex ()); /* evs 2000-05-17 was ctype_uint */
329 0[xX]{H}+{L} { setTokLengthT (mstring_length (yytext));
330 RETURN_INT (ctype_lint, processHex ()); }
331 0[xX]{H}+{L}{L} { setTokLengthT (mstring_length (yytext));
332 RETURN_INT (ctype_llint, processHex ()); }
333 0[xX]{H}+{U} { setTokLengthT (mstring_length (yytext));
334 RETURN_INT (ctype_uint, processHex ()); }
335 0[xX]{H}+{ULSuffix} { setTokLengthT (mstring_length (yytext));
336 RETURN_INT (ctype_ulint, processHex ()); }
337 0[xX]{H}+{U}{L}{L} { setTokLengthT (mstring_length (yytext));
338 RETURN_INT (ctype_ullint, processHex ()); }
339 0[xX]{H}+{L}{L}{U} { setTokLengthT (mstring_length (yytext));
340 RETURN_INT (ctype_ullint, processHex ()); }
341 0{Digit}+ { setTokLengthT (mstring_length (yytext));
342 RETURN_INT (ctype_int, processOctal ()); }
343 0{Digit}+{U} { setTokLengthT (mstring_length (yytext));
344 RETURN_INT (ctype_uint, processOctal ()); }
345 0{Digit}+{L} { setTokLengthT (mstring_length (yytext));
346 RETURN_INT (ctype_lint, processOctal ()); }
347 0{Digit}+{L}{L} { setTokLengthT (mstring_length (yytext));
348 RETURN_INT (ctype_llint, processOctal ()); }
349 0{Digit}+{ULSuffix} { setTokLengthT (mstring_length (yytext));
350 RETURN_INT (ctype_ulint, processOctal ()); }
351 0{Digit}+{U}{L}{L} { setTokLengthT (mstring_length (yytext));
352 RETURN_INT (ctype_ullint, processOctal ()); }
353 0{Digit}+{L}{L}{U} { setTokLengthT (mstring_length (yytext));
354 RETURN_INT (ctype_ullint, processOctal ()); }
355 {Digit}+ { setTokLengthT (mstring_length (yytext));
356 RETURN_INT (ctype_int, processDec ()); }
357 {Digit}+{U} { setTokLengthT (mstring_length (yytext));
358 RETURN_INT (ctype_uint, processDec ()); }
359 {Digit}+{L} { setTokLengthT (mstring_length (yytext));
360 RETURN_INT (ctype_lint, processDec ()); }
361 {Digit}+{L}{L} { setTokLengthT (mstring_length (yytext));
362 RETURN_INT (ctype_llint, processDec ()); }
363 {Digit}+{ULSuffix} { setTokLengthT (mstring_length (yytext));
364 RETURN_INT (ctype_ulint, processDec ()); }
365 {Digit}+{U}{L}{L} { setTokLengthT (mstring_length (yytext));
366 RETURN_INT (ctype_ullint, processDec ()); }
367 {Digit}+{L}{L}{U} { setTokLengthT (mstring_length (yytext));
368 RETURN_INT (ctype_ullint, processDec ()); }
369 '(\\.|[^\\'])+' { setTokLengthT (mstring_length (yytext));
370 RETURN_CHAR (processChar ()); }
371 {Digit}+{E}[fF] { setTokLengthT (mstring_length (yytext));
372 RETURN_FLOAT (ctype_float, processFloat ()); }
373 {Digit}+{E}[lL] { setTokLengthT (mstring_length (yytext));
374 RETURN_FLOAT (ctype_ldouble, processFloat ()); }
375 {Digit}+{E} { setTokLengthT (mstring_length (yytext));
376 RETURN_FLOAT (ctype_double, processFloat ()); }
378 {Digit}*"."{Digit}+({E})?[fF] { setTokLengthT (mstring_length (yytext));
379 RETURN_FLOAT (ctype_float, processFloat ()); }
380 {Digit}*"."{Digit}+({E})?[lL] { setTokLengthT (mstring_length (yytext));
381 RETURN_FLOAT (ctype_ldouble, processFloat ()); }
382 {Digit}*"."{Digit}+({E})? { setTokLengthT (mstring_length (yytext));
383 RETURN_FLOAT (ctype_double, processFloat ()); }
385 {Digit}+"."{Digit}*({E})?[fF] { setTokLengthT (mstring_length (yytext));
386 RETURN_FLOAT (ctype_float, processFloat ()); }
387 {Digit}+"."{Digit}*({E})?[lL] { setTokLengthT (mstring_length (yytext));
388 RETURN_FLOAT (ctype_ldouble, processFloat ()); }
389 {Digit}+"."{Digit}*({E})? { setTokLengthT (mstring_length (yytext));
390 RETURN_FLOAT (ctype_double, processFloat ()); }
392 ">>=" { setTokLength (3); RETURN_TOK (RIGHT_ASSIGN); }
393 "<<=" { setTokLength (3); RETURN_TOK (LEFT_ASSIGN); }
394 "+=" { setTokLength (2); RETURN_TOK (ADD_ASSIGN); }
395 "-=" { setTokLength (2); RETURN_TOK (SUB_ASSIGN); }
396 "*=" { setTokLength (2); RETURN_TOK (MUL_ASSIGN); }
397 "/=" { setTokLength (2); RETURN_TOK (DIV_ASSIGN); }
398 "%=" { setTokLength (2); RETURN_TOK (MOD_ASSIGN); }
399 "&=" { setTokLength (2); RETURN_TOK (AND_ASSIGN); }
400 "^=" { setTokLength (2); RETURN_TOK (XOR_ASSIGN); }
401 "|=" { setTokLength (2); RETURN_TOK (OR_ASSIGN); }
402 ">>" { setTokLength (2); RETURN_TOK (RIGHT_OP); }
403 "<<" { setTokLength (2); RETURN_TOK (LEFT_OP); }
404 "++" { setTokLength (2); RETURN_TOK (INC_OP); }
405 "--" { setTokLength (2); RETURN_TOK (DEC_OP); }
406 "->" { setTokLength (2); RETURN_TOK (ARROW_OP); }
407 "&&" { setTokLength (2); RETURN_TOK (AND_OP); }
408 "||" { setTokLength (2); RETURN_TOK (OR_OP); }
409 "<=" { setTokLength (2); RETURN_TOK (LE_OP); }
410 ">=" { setTokLength (2); RETURN_TOK (GE_OP); }
411 "==" { setTokLength (2); RETURN_TOK (EQ_OP); }
412 "!=" { setTokLength (2); RETURN_TOK (NE_OP); }
413 ";" { setTokLength (1); RETURN_TOK (TSEMI); }
414 "{" { setTokLength (1); RETURN_TOK (TLBRACE); }
415 "}" { setTokLength (1); RETURN_TOK (TRBRACE); }
416 "," { setTokLength (1); RETURN_TOK (TCOMMA); }
417 ":" { setTokLength (1); RETURN_TOK (TCOLON); }
418 "=" { setTokLength (1); RETURN_TOK (TASSIGN); }
419 "(" { setTokLength (1); RETURN_TOK (TLPAREN); }
420 ")" { setTokLength (1); RETURN_TOK (TRPAREN); }
421 "[" { setTokLength (1); RETURN_TOK (TLSQBR); }
422 "]" { setTokLength (1); RETURN_TOK (TRSQBR); }
423 "." { setTokLength (1); RETURN_TOK (TDOT); }
424 "&" { setTokLength (1); RETURN_TOK (TAMPERSAND); }
425 "!" { setTokLength (1); RETURN_TOK (TEXCL); }
428 "~" { setTokLength (1); RETURN_TOK (TTILDE); }
429 "-" { setTokLength (1); RETURN_TOK (TMINUS); }
430 "+" { setTokLength (1); RETURN_TOK (TPLUS); }
431 "*" { setTokLength (1); RETURN_TOK (TMULT); }
432 "/" { setTokLength (1); RETURN_TOK (TDIV); }
433 "%" { setTokLength (1); RETURN_TOK (TPERCENT); }
434 "<" { setTokLength (1); RETURN_TOK (TLT); }
435 ">" { setTokLength (1); RETURN_TOK (TGT); }
436 "^" { setTokLength (1); RETURN_TOK (TCIRC); }
437 "|" { setTokLength (1); RETURN_TOK (TBAR); }
438 "?" { setTokLength (1); RETURN_TOK (TQUEST); }
441 "/\\" { setTokLength (1); RETURN_TOK (TCAND); }
444 [ \t\v\f] { incColumn (); }
445 \n { context_incLineno ();
448 continueLine = FALSE;
452 if (context_inMacro ())
454 /* Don't use RETURN_TOK */
455 yylval.tok = lltok_create (TENDMACRO, g_currentloc);
456 lastWasString = FALSE;
461 "@@MR@@" { setTokLength (6);
463 if (processMacro ()) {
464 if (context_inIterDef ())
466 RETURN_TOK (LLMACROITER);
468 if (context_inIterEnd ())
470 RETURN_TOK (LLMACROEND);
472 if (context_inMacro ())
474 RETURN_TOK (LLMACRO);
478 "@QLMR" { if (context_inHeader () || context_inFunction ())
484 int nspchar = ninput ();
488 ** This is a hack to get the column number correct.
491 llassert (nspchar >= '0' && nspchar <= '9');
493 nspaces = nspchar - '0';
495 setTokLength (5 + nspaces);
499 if (context_inIterDef ())
501 RETURN_TOK (LLMACROITER);
503 if (context_inIterEnd ())
505 RETURN_TOK (LLMACROEND);
507 if (context_inMacro ())
509 RETURN_TOK (LLMACRO);
514 "@.CT" { setTokLength (4); lldiagmsg (ctype_unparseTable ()); }
515 "@.FA" { setTokLength (4); lldiagmsg (message ("Access types: %q", typeIdSet_unparse (context_fileAccessTypes ()))); }
516 "@.F" { setTokLength (3);
517 lldiagmsg (message ("%q: *** marker ***", fileloc_unparse (g_currentloc)));
519 "@.L" { setTokLength (3); usymtab_printLocal (); }
520 "@.A" { setTokLength (3); lldiagmsg (usymtab_unparseAliases ()); }
521 "@.C" { setTokLength (3); lldiagmsg (context_unparse ()); }
522 "@.W" { setTokLength (3); lldiagmsg (context_unparseClauses ()); }
523 "@.G" { setTokLength (3); usymtab_printGuards (); }
524 "@.S" { setTokLength (3); usymtab_printOut (); }
525 "@.X" { setTokLength (3); usymtab_printAll (); }
526 "@.Z" { setTokLength (3); usymtab_printComplete (); }
527 "@.T" { setTokLength (3); usymtab_printTypes (); }
528 "@.K" { setTokLength (3); lldiagmsg (usymtab_unparseStack ()); }
529 "@.M" { setTokLength (3);
530 lldiagmsg (message ("Can modify: %q",
531 sRefSet_unparse (context_modList ())));
533 "%{" { /* BEFORE_COMMENT_MARKER */
535 incColumn (); incColumn ();
536 tok = handleLlSpecial ();
540 if (tok == CANNOTATION) {
543 /* Beware - this bashes yylval! */
548 "%}" { /* AFTER_COMMENT_MARKER */
551 RETURN_TOK (QENDMACRO); }
552 "\\" { incColumn (); continueLine = TRUE; }
554 if ((int) *yytext == 13 ) {
559 message ("Invalid character (ascii: %d), skipping character",
568 /*@null@*/ /*@observer@*/ char *name;
573 ** These tokens are followed by syntax that is parsed by the
577 struct skeyword s_parsetable[] = {
578 { "modifies", QMODIFIES } ,
579 { "globals", QGLOBALS } ,
582 { "constant", QCONSTANT } ,
583 { "function", QFUNCTION } ,
585 { "defines", QDEFINES } ,
587 { "allocates", QALLOCATES } ,
589 { "releases", QRELEASES } ,
590 { "pre", QPRECLAUSE } ,
591 { "post", QPOSTCLAUSE } ,
592 { "setBufferSize", QSETBUFFERSIZE},
593 { "setStringLength", QSETSTRINGLENGTH},
594 { "testinRange", QTESTINRANGE},
595 { "requires", QPRECLAUSE } ,
596 { "ensures", QPOSTCLAUSE } ,
601 ** These tokens are either stand-alone tokens, or followed by
602 ** token-specific text.
605 struct skeyword s_keytable[] = {
606 { "anytype", QANYTYPE } ,
607 { "integraltype", QINTEGRALTYPE } ,
608 { "unsignedintegraltype", QUNSIGNEDINTEGRALTYPE } ,
609 { "signedintegraltype", QSIGNEDINTEGRALTYPE } ,
613 { "owned", QOWNED } ,
614 { "dependent", QDEPENDENT } ,
615 { "partial", QPARTIAL } ,
616 { "special", QSPECIAL } ,
617 { "truenull", QTRUENULL } ,
618 { "falsenull", QFALSENULL } ,
621 { "notnull", QNOTNULL } ,
622 { "abstract", QABSTRACT } ,
623 { "concrete", QCONCRETE } ,
624 { "mutable", QMUTABLE } ,
625 { "immutable", QIMMUTABLE } ,
626 { "unused", QUNUSED } ,
627 { "external", QEXTERNAL } ,
629 { "unique", QUNIQUE } ,
630 { "returned", QRETURNED } ,
631 { "exposed", QEXPOSED } ,
632 { "refcounted", QREFCOUNTED } ,
634 { "newref", QNEWREF } ,
635 { "tempref", QTEMPREF } ,
636 { "killref", QKILLREF } ,
638 { "relnull", QRELNULL } ,
639 { "nullterminated", QNULLTERMINATED },
640 { "setBufferSize", QSETBUFFERSIZE },
641 { "testInRange", QTESTINRANGE},
642 { "MaxSet", QMAXSET},
643 { "MaxRead", QMAXREAD},
644 { "reldef", QRELDEF } ,
645 { "observer", QOBSERVER } ,
646 { "exits", QEXITS } ,
647 { "mayexit", QMAYEXIT } ,
648 { "trueexit", QTRUEEXIT } ,
649 { "falseexit", QFALSEEXIT } ,
650 { "neverexit", QNEVEREXIT } ,
652 { "shared", QSHARED } ,
654 { "unchecked", QUNCHECKED } ,
655 { "checked", QCHECKED } ,
656 { "checkmod", QCHECKMOD } ,
657 { "checkedstrict", QCHECKEDSTRICT } ,
658 { "innercontinue", QINNERCONTINUE } ,
659 { "innerbreak", QINNERBREAK } ,
660 { "loopbreak", QLOOPBREAK } ,
661 { "switchbreak", QSWITCHBREAK } ,
662 { "safebreak", QSAFEBREAK } ,
663 { "fallthrough", QFALLTHROUGH } ,
664 { "l_fallthrou", QLINTFALLTHROUGH } ,
665 { "l_fallth", QLINTFALLTHRU } ,
666 { "notreached", QNOTREACHED } ,
667 { "l_notreach", QLINTNOTREACHED } ,
668 { "printflike", QPRINTFLIKE } ,
669 { "l_printfli", QLINTPRINTFLIKE } ,
670 { "scanflike", QSCANFLIKE } ,
671 { "messagelike", QMESSAGELIKE } ,
672 { "l_argsus", QARGSUSED } ,
677 ** would be better if these weren't hard coded...
680 static bool isArtificial (cstring s)
682 return (cstring_equalLit (s, "modifies")
683 || cstring_equalLit (s, "globals")
684 || cstring_equalLit (s, "warn")
685 || cstring_equalLit (s, "alt"));
688 void swallowMacro (void)
691 bool skipnext = FALSE;
693 while ((i = lminput ()) != EOF)
710 reader_checkUngetc (i, yyin);
718 reader_checkUngetc (i, yyin);
722 static int commentMarkerToken (cstring s)
726 while (s_parsetable[i].name != NULL)
728 DPRINTF (("Try :%s:%s:", s, s_parsetable[i].name));
730 if (cstring_equalLit (s, s_parsetable[i].name))
732 return s_parsetable[i].token;
741 static int tokenMacroCode (cstring s)
745 while (s_keytable[i].name != NULL)
747 if (cstring_equalLit (s, s_keytable[i].name))
749 if (s_keytable[i].token == QLINTFALLTHROUGH)
752 (FLG_WARNLINTCOMMENTS,
754 ("Traditional lint comment /*FALLTHROUGH*/ used. "
755 "This is interpreted by "
756 "LCLint in the same way as most Unix lints, but it is "
757 "preferable to replace it with the /*@fallthrough@*/ "
762 else if (s_keytable[i].token == QLINTFALLTHRU)
765 (FLG_WARNLINTCOMMENTS,
767 ("Traditional lint comment /*FALLTHRU*/ used. "
768 "This is interpreted by "
769 "LCLint in the same way as most Unix lints, but it is "
770 "preferable to replace it with the /*@fallthrough@*/ "
775 else if (s_keytable[i].token == QLINTNOTREACHED)
778 (FLG_WARNLINTCOMMENTS,
780 ("Traditional lint comment /*NOTREACHED*/ used. "
781 "This is interpreted by "
782 "LCLint in the same way as most Unix lints, but it is "
783 "preferable to replace it with the /*@notreached@*/ "
784 "semantic comment."),
789 else if (s_keytable[i].token == QPRINTFLIKE)
791 setSpecialFunction (qual_createPrintfLike ());
794 else if (s_keytable[i].token == QLINTPRINTFLIKE)
797 (FLG_WARNLINTCOMMENTS,
799 ("Traditional lint comment /*PRINTFLIKE*/ used. "
800 "This is interpreted by "
801 "LCLint in the same way as most Unix lints, but it is "
802 "preferable to replace it with either /*@printflike@*/, "
803 "/*@scanflike@*/ or /*@messagelike@*/."),
806 setSpecialFunction (qual_createPrintfLike ());
809 else if (s_keytable[i].token == QSCANFLIKE)
811 setSpecialFunction (qual_createScanfLike ());
814 else if (s_keytable[i].token == QMESSAGELIKE)
816 setSpecialFunction (qual_createMessageLike ());
819 else if (s_keytable[i].token == QARGSUSED)
822 (FLG_WARNLINTCOMMENTS,
824 ("Traditional lint comment /*ARGSUSED*/ used. "
825 "This is interpreted by "
826 "LCLint in the same way as most Unix lints, but it is "
827 "preferable to use /*@unused@*/ annotations on "
828 "the unused parameters."),
835 return s_keytable[i].token;
844 static int lminput ()
846 if (savechar == '\0')
853 int save = (int) savechar;
859 static void lmsavechar (char c)
861 if (savechar == '\0') savechar = c;
864 llbuglit ("lmsavechar: override");
868 static int returnFloat (ctype ct, double f)
870 yylval.expr = exprNode_floatLiteral (f, ct, cstring_fromChars (yytext),
871 fileloc_decColumn (g_currentloc, tokLength));
876 static int returnInt (ctype ct, long i)
880 if (ctype_equal (ct, ctype_int))
884 c = context_typeofZero ();
888 c = context_typeofOne ();
892 yylval.expr = exprNode_numLiteral (c, cstring_fromChars (yytext),
893 fileloc_decColumn (g_currentloc, tokLength), i);
898 static int returnChar (char c)
900 yylval.expr = exprNode_charLiteral (c, cstring_fromChars (yytext),
901 fileloc_decColumn (g_currentloc, tokLength));
910 if (c != EOF && ((char)c == '\n'))
912 context_incLineno ();
918 static char macro_nextChar ()
920 static bool in_quote = FALSE, in_escape = FALSE, in_char = FALSE;
925 c = char_fromInt (ic);
927 if (!in_quote && !in_char && (c == '\\' || c == BEFORE_COMMENT_MARKER[0]))
931 while ((c = char_fromInt (lminput ())) != '\0' && c != '\n')
933 ; /* skip to newline */
936 context_incLineno ();
940 return macro_nextChar ();
947 else /* if (c == '@') */
949 llassert (FALSE); /*@i23@*/
950 if (handleLlSpecial () != BADTOK)
952 llerrorlit (FLG_SYNTAX, "Macro cannot use special syntax");
955 return macro_nextChar ();
958 else if (!in_escape && c == '\"')
960 in_quote = !in_quote;
962 else if (!in_escape && c == '\'')
966 else if ((in_quote || in_char) && c == '\\')
968 in_escape = !in_escape;
970 else if ((in_quote || in_char) && in_escape)
974 else if (!in_quote && c == '/')
978 if ((c2 = char_fromInt (lminput ())) == '*')
982 while ((c2 = char_fromInt (lminput ())) != '\0'
983 && c2 != '\n' && c2 != '*')
990 while ((c2 = char_fromInt (lminput ())) != '\0'
1003 llfatalerror (cstring_makeLiteral ("Macro: bad comment!"));
1007 return macro_nextChar ();
1011 /*** putchar does not work! why? puts to stdio...??! ***/
1019 ** keeps semantic comments
1022 static char macro_nextCharC ()
1024 static bool in_quote = FALSE, in_escape = FALSE, in_char = FALSE;
1027 c = char_fromInt (lminput ());
1029 if (!in_quote && !in_char && c == '\\')
1031 while ((c = char_fromInt (lminput ())) != '\0' && c != '\n')
1033 ; /* skip to newline */
1036 context_incLineno ();
1040 return macro_nextCharC ();
1047 else if (!in_escape && c == '\"')
1049 in_quote = !in_quote;
1051 else if (!in_escape && c == '\'')
1055 else if ((in_quote || in_char) && c == '\\')
1057 in_escape = !in_escape;
1059 else if ((in_quote || in_char) && in_escape)
1063 else if (!in_quote && c == '/')
1067 if ((c2 = char_fromInt (lminput ())) == '*')
1071 while ((c2 = char_fromInt (lminput ())) != '\0'
1072 && c2 != '\n' && c2 != '*')
1079 while ((c2 = char_fromInt (lminput ())) != '\0'
1092 llfatalerror (cstring_makeLiteral ("Macro: bad comment!"));
1096 return macro_nextCharC ();
1107 ** skips whitespace (handles line continuations)
1108 ** returns first non-whitespace character
1111 static char skip_whitespace ()
1115 while ((c = macro_nextChar ()) == ' ' || c == '\t')
1123 static void handleMacro ()
1125 cstring mac = cstring_undefined;
1129 while (currentColumn () > 2)
1131 mac = cstring_appendChar (mac, ' ');
1135 c = macro_nextCharC ();
1137 if (c >= '0' && c <= '9')
1141 for (i = 0; i < ((c - '0') + 1); i++)
1143 mac = cstring_appendChar (mac, ' ');
1151 while (((c = macro_nextCharC ()) != '\0') && (c != '\n'))
1153 mac = cstring_appendChar (mac, c);
1157 macrocode = tokenMacroCode (mac);
1159 if (macrocode == BADTOK && !isArtificial (mac))
1161 context_addMacroCache (mac);
1170 context_incLineno ();
1174 static bool processMacro (void)
1179 cstring fname = cstring_undefined;
1181 bool isspecfcn = FALSE;
1182 bool isiter = FALSE;
1183 bool skipparam = FALSE;
1184 bool isenditer = FALSE;
1185 bool unknownm = FALSE;
1186 bool hasParams = FALSE;
1187 bool emptyMacro = FALSE;
1188 char c = skip_whitespace ();
1189 fileloc loc = fileloc_noColumn (g_currentloc);
1191 /* are both of these necessary? what do they mean? */
1192 uentryList specparams = uentryList_undefined;
1193 uentryList pn = uentryList_undefined;
1195 context_resetMacroMissingParams ();
1197 if (c == '\0' || c == '\n')
1199 llcontbug (cstring_makeLiteral ("Bad macro"));
1204 fname = cstring_appendChar (fname, c);
1206 while ((c = macro_nextChar ()) != '(' && c != '\0'
1207 && c != ' ' && c != '\t' && c != '\n')
1209 fname = cstring_appendChar (fname, c);
1212 if (c == ' ' || c == '\t' || c == '\n')
1218 while (c == ' ' || c == '\t')
1220 c = macro_nextChar ();
1234 hasParams = (c == '(');
1237 if (usymtab_exists (fname))
1239 e2 = usymtab_lookupExpose (fname);
1240 ct = uentry_getType (e2);
1243 if (uentry_isCodeDefined (e2)
1244 && fileloc_isUser (uentry_whereDefined (e2)))
1248 message ("Macro %s already defined", fname),
1251 uentry_showWhereDefined (e2);
1252 uentry_clearDefined (e2);
1255 if (uentry_isFunction (e2))
1257 uentry_setType (e2, ctype_unknown);
1260 context_enterUnknownMacro (e2);
1264 context_enterConstantMacro (e2);
1269 if (uentry_isForward (e2) && uentry_isFunction (e2))
1276 ("Parameterized macro has no prototype or specification: %s ",
1281 uentry_setType (e2, ctype_unknown);
1282 uentry_setFunctionDefined (e2, loc);
1283 uentry_setUsed (e2, fileloc_undefined);
1284 context_enterUnknownMacro (e2);
1288 if (uentry_isIter (e2))
1291 specparams = uentry_getParams (e2);
1292 noparams = uentryList_size (specparams);
1293 uentry_setDefined (e2, loc);
1294 context_enterIterDef (e2);
1296 else if (uentry_isEndIter (e2))
1299 uentry_setDefined (e2, loc);
1300 context_enterIterEnd (e2); /* don't care about it now */
1301 /* but should parse like an iter! */
1303 else if (uentry_isConstant (e2))
1309 message ("Constant %s implemented as parameterized macro",
1313 uentry_showWhereSpecified (e2);
1314 uentry_setType (e2, ctype_unknown);
1315 uentry_makeVarFunction (e2);
1316 uentry_setDefined (e2, g_currentloc);
1317 uentry_setFunctionDefined (e2, g_currentloc);
1318 context_enterUnknownMacro (e2);
1322 if (!uentry_isSpecified (e2))
1324 fileloc oloc = uentry_whereDeclared (e2);
1326 if (fileloc_isLib (oloc))
1330 else if (fileloc_isUndefined (oloc)
1331 || fileloc_isPreproc (oloc))
1336 (FLG_MACROCONSTDECL,
1338 ("Macro constant %q not declared",
1339 uentry_getName (e2)),
1343 else if (!fileloc_withinLines (oloc, loc, 2))
1344 { /* bogus! will give errors if there is too much whitespace */
1348 ("Macro constant name %s does not match name in "
1349 "previous constant declaration. This constant "
1350 "is declared at %q", fname,
1351 fileloc_unparse (oloc)),
1356 context_enterConstantMacro (e2);
1357 cstring_free (fname);
1363 else if (ctype_isFunction (ct))
1366 specparams = ctype_argsFunction (ct);
1367 noparams = uentryList_size (specparams);
1369 uentry_setFunctionDefined (e2, loc);
1370 context_enterMacro (e2);
1372 else if (uentry_isVar (e2))
1378 message ("Variable %s implemented as parameterized macro",
1382 uentry_showWhereSpecified (e2);
1383 uentry_setType (e2, ctype_unknown);
1384 uentry_makeVarFunction (e2);
1385 uentry_setDefined (e2, g_currentloc);
1386 uentry_setFunctionDefined (e2, g_currentloc);
1387 context_enterUnknownMacro (e2);
1391 uentry ucons = uentry_makeConstant (fname,
1394 if (uentry_isExpandedMacro (e2))
1402 message ("Variable %s implemented by a macro",
1406 uentry_showWhereSpecified (e2);
1410 uentry_setDefined (e2, loc);
1411 uentry_setUsed (ucons, loc);
1413 context_enterConstantMacro (ucons);
1414 uentry_markOwned (ucons);
1415 cstring_free (fname);
1421 if (uentry_isDatatype (e2))
1425 message ("Type implemented as macro: %x",
1426 uentry_getName (e2)),
1427 message ("A type is implemented using a macro definition. A "
1428 "typedef should be used instead."),
1432 /* Must exit scope (not sure why a new scope was entered?) */
1433 usymtab_quietExitScope (g_currentloc);
1434 uentry_setDefined (e2, g_currentloc);
1440 (message ("Unexpanded macro not function or constant: %q",
1441 uentry_unparse (e2)));
1442 uentry_setType (e2, ctype_unknown);
1446 uentry_makeVarFunction (e2);
1447 uentry_setDefined (e2, g_currentloc);
1448 uentry_setFunctionDefined (e2, g_currentloc);
1449 context_enterUnknownMacro (e2);
1461 (FLG_MACROMATCHNAME,
1462 message ("Unexpanded macro %s does not match name of a constant "
1463 "or iter declaration. The name used in the control "
1464 "comment on the previous line should match. "
1465 "(Assuming macro defines a constant.)",
1470 ce = uentry_makeConstant (fname, ctype_unknown, fileloc_undefined);
1471 uentry_setUsed (ce, loc); /* perhaps bogus? */
1472 e2 = usymtab_supEntryReturn (ce);
1474 context_enterConstantMacro (e2);
1475 cstring_free (fname);
1480 /* in macros, ( must follow immediatetly after name */
1486 c = skip_whitespace ();
1488 while (c != ')' && c != '\0')
1491 bool suppress = context_inSuppressRegion ();
1492 cstring paramname = cstring_undefined;
1495 ** save the parameter location
1499 context_saveLocation ();
1502 while (c != ' ' && c != '\t' && c != ',' && c != '\0' && c != ')')
1504 paramname = cstring_appendChar (paramname, c);
1505 c = macro_nextChar ();
1508 if (c == ' ' || c == '\t') c = skip_whitespace ();
1512 c = macro_nextChar ();
1513 if (c == ' ' || c == '\t') c = skip_whitespace ();
1518 llfatalerror (cstring_makeLiteral
1519 ("Bad macro syntax: uentryList"));
1522 if ((isspecfcn || isiter) && (paramno < noparams)
1523 && !uentry_isElipsisMarker (uentryList_getN
1524 (specparams, paramno)))
1526 uentry decl = uentryList_getN (specparams, paramno);
1529 param = uentry_nameCopy (paramname, decl);
1532 uentry_setParam (param);
1533 sr = sRef_makeParam (paramno, uentry_getType (param));
1535 if (sRef_getNullState (sr) == NS_ABSNULL)
1537 ctype pt = ctype_realType (uentry_getType (param));
1539 if (ctype_isUser (pt))
1541 uentry te = usymtab_getTypeEntrySafe (ctype_typeId (pt));
1543 if (uentry_isValid (te))
1545 sRef_setStateFromUentry (sr, te);
1550 sRef_setNullState (sr, NS_UNKNOWN, g_currentloc);
1554 uentry_setSref (param, sr);
1555 uentry_setDeclaredForceOnly (param, context_getSaveLocation ());
1557 skipparam = isiter && uentry_isOut (uentryList_getN (specparams, paramno));
1561 fileloc sloc = context_getSaveLocation ();
1563 param = uentry_makeVariableSrefParam
1564 (paramname, ctype_unknown, sRef_makeParam (paramno, ctype_unknown));
1565 cstring_free (paramname);
1567 sRef_setPosNull (uentry_getSref (param), sloc);
1569 uentry_setDeclaredForce (param, sloc);
1572 fileloc_free (sloc);
1577 llassert (!uentry_isElipsisMarker (param));
1581 sRef_makeUnsafe (uentry_getSref (param));
1584 pn = uentryList_add (pn, uentry_copy (param));
1585 usymtab_supEntry (param);
1589 /* don't add param */
1590 uentry_free (param);
1595 (void) macro_nextChar ();
1596 c = skip_whitespace ();
1604 if (isspecfcn || isiter)
1606 if (paramno != noparams && noparams >= 0)
1612 message ("Macro %s specified with %d args, defined with %d",
1613 fname, noparams, paramno),
1616 uentry_showWhereSpecified (e2);
1617 uentry_resetParams (e2, pn);
1622 uentry_resetParams (e2, pn);
1629 ** the form should be:
1631 ** # define newname oldname
1632 ** where oldname refers to a function matching the specification
1638 sRef_setGlobalScope ();
1639 usymtab_supGlobalEntry (uentry_makeVariableLoc (fname, ctype_unknown));
1640 sRef_clearGlobalScope ();
1644 context_setMacroMissingParams ();
1649 /* context_setuentryList (pn); */
1650 usymtab_enterScope ();
1653 cstring_free (fname);
1658 static bool handleSpecial (char *yyt)
1660 char *l = mstring_create (MAX_NAME_LENGTH);
1661 static bool reportcpp = FALSE;
1667 strcpy (l, yyt + 1);
1669 /* Need to safe original l for deallocating. */
1672 l += strlen (yyt) - 1;
1674 while ((c = char_fromInt (lminput ())) != '\n' && c != '\0')
1680 olc = cstring_fromChars (ol);
1682 if (cstring_equalPrefix (olc, "pragma"))
1684 char *pname = mstring_create (longUnsigned_fromInt (MAX_PRAGMA_LEN));
1685 char *opname = pname;
1686 char *ptr = ol + 6; /* pragma is six characters, plus space */
1690 /* skip whitespace */
1691 while (((c = *ptr) != '\0') && isspace (c))
1697 while (((c = *ptr) != '\0') && !isspace (c))
1701 if (len > MAX_PRAGMA_LEN)
1712 if (len == PRAGMA_LEN_EXPAND
1713 && mstring_equal (opname, PRAGMA_EXPAND))
1715 cstring exname = cstring_undefined;
1719 while (((c = *ptr) != '\0') && !isspace (c))
1721 exname = cstring_appendChar (exname, c);
1726 ue = usymtab_lookupExposeGlob (exname);
1728 if (uentry_isExpandedMacro (ue))
1730 if (fileloc_isPreproc (uentry_whereDefined (ue)))
1732 fileloc_setColumn (g_currentloc, 1);
1733 uentry_setDefined (ue, g_currentloc);
1737 cstring_free (exname);
1740 else if (cstring_equalPrefix (olc, "ident"))
1742 /* Some pre-processors will leave these in the code. Ignore rest of line */
1745 ** Yuk...Win32 filenames can have spaces in them...we need to read
1746 ** to the matching end quote.
1748 else if ((sscanf (ol, "line %d \"", &lineno) == 1)
1749 || (sscanf (ol, " %d \"", &lineno) == 1))
1755 /*@access cstring@*/
1756 while (*tmp != '\"' && *tmp != '\0')
1761 llassert (*tmp == '\"');
1767 while (*tmp != '\"' && *tmp != '\0')
1772 llassert (*tmp == '\"');
1776 # if defined(OS2) || defined(MSDOS) || defined(WIN32)
1779 ** DOS-like path delimiters get delivered in pairs, something like
1780 ** \"..\\\\file.h\", so we have to make it normal again. We do NOT
1781 ** remove the pre dirs yet as we usually specify tmp paths relative
1782 ** to the current directory, so tmp files would not get found in
1783 ** the hash table. If this method fails we try it again later.
1790 ** Skip past the drive marker.
1793 if (strchr (stmp, ':') != NULL)
1795 stmp = strchr (stmp, ':') + 1;
1798 while ((stmp = strchr (stmp, CONNECTCHAR)) != NULL )
1800 if (*(stmp+1) == CONNECTCHAR)
1802 memmove (stmp, stmp+1, strlen (stmp));
1808 fid = fileTable_lookupBase (context_fileTable (), fname);
1809 if (!(fileId_isValid (fid)))
1811 fname = removePreDirs (fname);
1812 fid = fileTable_lookupBase (context_fileTable (), fname);
1815 # else /* !defined(OS2) && !defined(MSDOS) */
1816 fname = removePreDirs (fname);
1817 fid = fileTable_lookupBase (context_fileTable (), fname);
1818 # endif /* !defined(OS2) && !defined(MSDOS) */
1820 if (!(fileId_isValid (fid)))
1822 if (isHeaderFile (fname))
1824 fid = fileTable_addHeaderFile (context_fileTable (), fname);
1828 fid = fileTable_addFile (context_fileTable (), fname);
1832 setFileLine (fid, lineno);
1833 /*@noaccess cstring@*/
1835 else if ((sscanf (ol, "line %d", &lineno) == 1)
1836 || (sscanf (ol, " %d", &lineno) == 1))
1838 setLine (lineno); /* next line is <cr> */
1842 if (mstring_equal (ol, "")) {
1843 DPRINTF (("Empty pp command!"));
1845 ** evs 2000-05-16: This is a horrible kludge, to get around a bug (well, difficulty) in the pre-processor.
1846 ** We handle a plain # in the input file, by echoing it, and ignoring it in the post-pp-file.
1855 llbug (message ("File contains preprocessor command: #%s",
1856 cstring_fromChars (ol)));
1869 static int handleLlSpecial ()
1873 char *s = mstring_createEmpty ();
1879 loc = fileloc_copy (g_currentloc);
1880 DPRINTF (("Handle special: %s", fileloc_unparse (loc)));
1882 while (((ic = ninput ()) != 0) && isalpha (ic))
1885 s = mstring_append (s, c);
1889 DPRINTF (("Read: %s / %s", s, fileloc_unparse (g_currentloc)));
1892 if (charsread == 0 && ic == (int) AFTER_COMMENT_MARKER[0])
1896 llassert (ic == AFTER_COMMENT_MARKER[1]);
1902 return QNOMODS; /* special token no modifications token */
1906 DPRINTF (("Coment marker: %s", os));
1907 tok = commentMarkerToken (cstring_fromChars (os));
1911 tokLength = charsread;
1918 DPRINTF (("Not a comment marker..."));
1919 /* Add rest of the comment */
1921 if (ic != 0 && ic != EOF)
1925 s = mstring_append (s, c);
1928 while (((ic = ninput ()) != 0) && (ic != EOF)
1929 && (ic != AFTER_COMMENT_MARKER[0]))
1932 s = mstring_append (s, c);
1937 DPRINTF (("Read: %s / %s", s, fileloc_unparse (g_currentloc)));
1939 if (ic == AFTER_COMMENT_MARKER[0])
1942 llassert ((char) nc == AFTER_COMMENT_MARKER[1]);
1948 while (*s == ' ' || *s == '\t' || *s == '\n')
1953 if (*s == '-' || *s == '+' || *s == '=') /* setting flags */
1957 while (c == '-' || c == '+' || c == '=')
1959 ynm set = ynm_fromCodeChar (c);
1964 thisflag = cstring_fromChars (s);
1966 while ((c = *s) != '\0' && (c != '-') && (c != '=')
1967 && (c != '+') && (c != ' ') && (c != '\t') && (c != '\n'))
1974 if (!context_getFlag (FLG_NOCOMMENTS))
1976 cstring flagname = thisflag;
1977 flagcode fflag = identifyFlag (flagname);
1979 if (flagcode_isSkip (fflag))
1983 else if (flagcode_isInvalid (fflag))
1985 if (isMode (flagname))
1987 if (ynm_isMaybe (set))
1992 ("Semantic comment attempts to restore flag %s. "
1993 "A mode flag cannot be restored.",
1998 context_setMode (flagname);
2004 (FLG_UNRECOGFLAGCOMMENTS,
2005 message ("Unrecognized option in semantic comment: %s",
2010 else if (flagcode_isGlobalFlag (fflag))
2015 ("Semantic comment attempts to set global flag %s. "
2016 "A global flag cannot be set locally.",
2022 context_fileSetFlag (fflag, set);
2024 if (flagcode_hasArgument (fflag))
2026 if (ynm_isMaybe (set))
2031 ("Semantic comment attempts to restore flag %s. "
2032 "A flag for setting a value cannot be restored.",
2037 { /* cut-and-pastied from llmain...blecch */
2038 cstring extra = cstring_undefined;
2044 rest = mstring_copy (s);
2048 while ((rchar = *rest) != '\0'
2049 && (isspace (rchar)))
2055 while ((rchar = *rest) != '\0'
2056 && !isspace (rchar))
2058 extra = cstring_appendChar (extra, rchar);
2065 if (cstring_isUndefined (extra))
2070 ("Flag %s (in semantic comment) must be followed by an argument",
2071 flagcode_unparse (fflag)));
2077 if (flagcode_hasValue (fflag))
2079 setValueFlag (fflag, extra);
2081 else if (flagcode_hasString (fflag))
2083 setStringFlag (fflag, extra);
2100 while ((c == ' ') || (c == '\t') || (c == '\n'))
2106 if (context_inHeader () && !isArtificial (cstring_fromChars (os)))
2108 DPRINTF (("Here adding comment: %s", os));
2109 context_addComment (cstring_fromCharsNew (os));
2121 annotationInfo ainfo;
2123 while (*s != '\0' && *s != ' ' && *s != '\t' && *s != '\n')
2135 t = cstring_toCharsSafe (cstring_downcase (cstring_fromChars (t)));
2136 macrocode = tokenMacroCode (cstring_fromChars (t));
2138 if (macrocode != BADTOK)
2140 tokLength = mstring_length (t);
2146 if (macrocode == SKIPTOK)
2154 ainfo = context_lookupAnnotation (cstring_fromChars (os));
2156 if (annotationInfo_isDefined (ainfo)) {
2157 DPRINTF (("Found annotation: %s", annotationInfo_unparse (ainfo)));
2158 /*@i324@*/ yylval.annotation = ainfo;
2166 if (context_inHeader ())
2173 if ((context_inMacro () || context_inGlobalContext ())
2174 && macrocode != SKIPTOK
2175 && !isArtificial (cstring_fromChars (os)))
2177 DPRINTF (("Add comment: %s", os));
2178 context_addComment (cstring_fromCharsNew (os));
2191 if (mstring_equal (t, "ignore"))
2193 if (!context_getFlag (FLG_NOCOMMENTS))
2195 context_enterSuppressRegion ();
2198 else if ((*t == 'i' || *t == 't')
2199 && (*(t + 1) == '\0'))
2201 if (!context_getFlag (FLG_NOCOMMENTS)
2202 && (*t == 'i' || context_getFlag (FLG_TMPCOMMENTS)))
2204 context_enterSuppressLine (-1); /* infinite suppression */
2207 else if (((*t == 'i') || (*t == 't'))
2208 && ((*(t + 1) >= '0' && *(t + 1) <= '9')))
2210 bool tmpcomment = (*t == 't');
2212 char *tt = t; /* don't mangle t, since it is free'd */
2215 if (lc >= '0' && lc <= '9')
2217 val = (int)(lc - '0');
2220 while (lc >= '0' && lc <= '9')
2229 if (!context_getFlag (FLG_NOCOMMENTS)
2230 && (!tmpcomment || context_getFlag (FLG_TMPCOMMENTS)))
2232 context_enterSuppressLine (val);
2235 else if (mstring_equal (t, "end"))
2237 if (!context_getFlag (FLG_NOCOMMENTS))
2239 context_exitSuppressRegion ();
2242 else if (mstring_equal (t, "notfunction"))
2244 ; /* handled by pcpp */
2246 else if (mstring_equal (t, "access"))
2252 while ((c = *s) && (c == ' ' || c == '\t' || c == '\n'))
2262 tname = cstring_fromChars (s);
2264 while ((c = *s) != '\0' && c != ' '
2265 && c != '\t' && c != '\n' && c != ',')
2272 DPRINTF (("Access %s", tname));
2274 if (!context_getFlag (FLG_NOCOMMENTS)
2275 && !context_getFlag (FLG_NOACCESS))
2277 if (usymtab_existsType (tname))
2279 typeId uid = usymtab_getTypeId (tname);
2280 uentry ue = usymtab_getTypeEntry (uid);
2282 if (uentry_isAbstractDatatype (ue))
2284 context_addFileAccessType (uid);
2285 DPRINTF (("Adding access to: %s / %d", tname, uid));
2292 ("Non-abstract type %s used in access comment",
2299 if (!(context_inSuppressRegion ()
2300 || context_inSuppressZone (g_currentloc)))
2305 ("Unrecognized type %s used in access comment",
2317 if (c != ',' && c != ' ')
2323 else if (mstring_equal (t, "noaccess"))
2330 while ((lc = *s) && (lc == ' ' || lc == '\t' || lc == '\n'))
2340 tname = cstring_fromChars (s);
2342 while ((lc = *s) != '\0' && lc != ' ' && lc != '\t'
2343 && lc != '\n' && lc != ',')
2350 if (!context_getFlag (FLG_NOCOMMENTS)
2351 && !context_getFlag (FLG_NOACCESS))
2353 if (usymtab_existsType (tname))
2355 typeId tuid = usymtab_getTypeId (tname);
2357 if (context_couldHaveAccess (tuid))
2359 DPRINTF (("Removing access: %s", tname));
2360 context_removeFileAccessType (tuid);
2364 if (!(context_inSuppressRegion ()
2365 || context_inSuppressZone (g_currentloc)))
2367 uentry ue = usymtab_getTypeEntry (tuid);
2369 if (uentry_isAbstractDatatype (ue))
2374 ("Non-accessible abstract type %s used in noaccess comment",
2383 ("Non-abstract type %s used in noaccess comment",
2392 if (!(context_inSuppressRegion ()
2393 || context_inSuppressZone (g_currentloc)))
2398 ("Unrecognized type %s used in noaccess comment",
2410 if (lc != ',' && lc != ' ')
2418 voptgenerror (FLG_UNRECOGCOMMENTS,
2419 message ("Semantic comment unrecognized: %s",
2420 cstring_fromChars (os)), loc);
2431 static /*@only@*/ cstring makeIdentifier (char *s)
2433 char *c = mstring_create (size_toInt (strlen (s)) + 1);
2434 cstring id = cstring_fromChars (c);
2436 while (isalnum (*s) || (*s == '_') || (*s == '$'))
2445 /*@observer@*/ /*@dependent@*/ uentry coerceId (cstring cn)
2447 if (!(usymtab_exists (cn)))
2449 fileloc loc = fileloc_createExternal ();
2452 ** We need to put this in a global scope, otherwise the sRef will be deallocated.
2455 uentry ce = uentry_makeUnrecognized (cn, loc);
2457 if (!context_inIterEnd ())
2461 message ("Unrecognized (possibly system) identifier: %q",
2462 uentry_getName (ce)),
2469 return (usymtab_lookup (cn));
2473 ** like, coerceId, but doesn't supercede for iters
2476 /*@observer@*/ uentry coerceIterId (cstring cn)
2478 if (!(usymtab_exists (cn)))
2480 return uentry_undefined;
2483 return (usymtab_lookup (cn));
2486 /*@observer@*/ cstring LastIdentifier ()
2488 return (lastidprocessed);
2491 static int processIdentifier (cstring id)
2495 DPRINTF (("Process identifier: %s / %s / %s", id, fileloc_unparse (g_currentloc),
2496 bool_unparse (context_inFunctionHeader ())));
2498 context_clearJustPopped ();
2499 lastidprocessed = id;
2501 if (context_inFunctionHeader ())
2503 int tok = commentMarkerToken (id);
2504 DPRINTF (("in function decl..."));
2512 tok = tokenMacroCode (id);
2520 annotationInfo ainfo;
2522 if (expectingMetaStateName)
2524 metaStateInfo msinfo = context_lookupMetaStateInfo (id);
2526 if (metaStateInfo_isDefined (msinfo))
2528 yylval.msinfo = msinfo;
2529 return METASTATE_NAME;
2533 DPRINTF (("Not meta state name: %s", cstring_toCharsSafe (id)));
2537 ainfo = context_lookupAnnotation (id);
2539 if (annotationInfo_isDefined (ainfo))
2541 DPRINTF (("Found annotation: %s", annotationInfo_unparse (ainfo)));
2542 /*@i324@*/ yylval.annotation = ainfo;
2547 DPRINTF (("Not annotation: %s", id));
2553 /* Consider handling: Defined by C99 as static const char __func__[] */
2555 if (context_getFlag (FLG_GNUEXTENSIONS))
2559 if (cstring_equalLit (id, "__stdcall")
2560 || cstring_equalLit (id, "__cdecl")
2561 || cstring_equalLit (id, "__extension__"))
2565 else if (cstring_equalLit (id, "__volatile__"))
2569 else if (cstring_equalLit (id, "__signed"))
2573 else if (cstring_equalLit (id, "__unsigned"))
2577 else if (cstring_equalLit (id, "__const__"))
2581 else if (cstring_equalLit (id, "__alignof__"))
2583 tok = CALIGNOF; /* alignof is parsed like sizeof */
2585 else if (cstring_equalLit (id, "__FUNCTION__")
2586 || cstring_equalLit (id, "__PRETTY_FUNCTION__"))
2588 /* These tokens hold the name of the current function as strings */
2589 yylval.expr = exprNode_stringLiteral (id, fileloc_copy (g_currentloc));
2591 lastWasString = TRUE;
2595 else if (cstring_equalLit (id, "__attribute__")
2596 || cstring_equalLit (id, "__asm__")
2597 || cstring_equalLit (id, "_asm")
2598 || cstring_equalLit (id, "__asm")
2599 || cstring_equalLit (id, "__declspec"))
2602 bool useparens = FALSE;
2603 bool usebraces = FALSE;
2604 bool inquote = FALSE;
2605 bool inescape = FALSE;
2608 while ((ic = input ()) != EOF)
2615 else if (ic == '\\')
2619 else if (ic == '\"')
2655 else if (ic == ')' && useparens)
2658 if (depth == 0) break;
2660 else if (ic == '}' && usebraces)
2663 if (depth == 0) break;
2666 && !usebraces && !useparens
2667 && cstring_equalLit (id, "__asm"))
2670 ** We need this because some MS VC++ include files
2671 ** have __asm mov ... }
2672 ** Its a kludge, but otherwise would need to parse
2681 context_incLineno ();
2683 if (cstring_equalLit (id, "__asm")
2684 && !useparens && !usebraces)
2691 llassert ((useparens && ic == ')')
2692 || (usebraces && ic == '}')
2693 || (!useparens && !usebraces));
2697 else if (cstring_equalLit (id, "inline")
2698 || cstring_equalLit (id, "__inline")
2699 || cstring_equalLit (id, "_inline")
2700 || cstring_equalLit (id, "__inline__"))
2711 le = usymtab_lookupSafe (id);
2713 /*@-dependenttrans@*/
2715 if (uentry_isIter (le))
2717 /*@i32@*/ yylval.entry = le;
2720 else if (uentry_isEndIter (le))
2722 /*@i32@*/ yylval.entry = le;
2723 return (ITER_ENDNAME);
2725 else if (uentry_isUndefined (le))
2729 /* avoid parse errors for certain system built ins */
2731 if (g_expectingTypeName && (cstring_firstChar (id) == '_')
2732 && (cstring_secondChar (id) == '_'))
2734 return (TYPE_NAME_OR_ID);
2737 return (NEW_IDENTIFIER);
2739 else if (!uentry_isDeclared (le) && !uentry_isCodeDefined (le))
2741 if (uentry_isDatatype (le))
2744 return (NEW_IDENTIFIER);
2748 /*@i32@*/ yylval.entry = le;
2749 return (IDENTIFIER);
2752 else if (uentry_isDatatype (le))
2754 if (!g_expectingTypeName)
2758 return (NEW_IDENTIFIER);
2762 yylval.ctyp = uentry_getAbstractType (le);
2764 uentry_setUsed (le, g_currentloc);
2770 /*@i32@*/ yylval.entry = le;
2771 return (IDENTIFIER);
2774 /*@=dependenttrans@*/
2777 static bool processHashIdentifier (/*@only@*/ cstring id)
2779 if (context_inMacro () || context_inIterDef () ||
2780 context_inIterEnd ())
2784 context_clearJustPopped ();
2786 lastidprocessed = id;
2787 le = usymtab_lookupSafe (id);
2789 if (uentry_isParam (le) || uentry_isRefParam (le))
2806 static /*@only@*/ exprNode processString ()
2810 char *nl = strchr (yytext, '\n');
2811 cstring ns = cstring_fromCharsNew (yytext);
2815 loc = fileloc_copy (g_currentloc);
2816 addColumn (cstring_length (ns));
2822 loc = fileloc_copy (g_currentloc);
2824 context_incLineno ();
2826 while ((nl = strchr ((nl + 1), '\n')) != NULL)
2828 context_incLineno ();
2834 res = exprNode_stringLiteral (ns, loc);
2844 llassert (*yytext != '\0');
2845 fchar = *(yytext + 1);
2846 if (fchar != '\\') return fchar;
2848 next = *(yytext + 2);
2852 case 'n': return '\n';
2853 case 't': return '\t';
2854 case '\"': return '\"';
2855 case '\'': return '\'';
2856 case '\\': return '\\';
2857 default: return '\0';
2862 double processFloat ()
2864 double ret = atof (yytext);
2875 llassert (yytext[0] == '0'
2876 && (yytext[1] == 'X' || yytext[1] == 'x'));
2878 while (yytext[index] != '\0') {
2880 char c = yytext[index];
2882 if (c >= '0' && c <= '9') {
2883 tval = (int) c - (int) '0';
2884 } else if (c >= 'A' && c <= 'F') {
2885 tval = (int) c - (int) 'A' + 10;
2886 } else if (c >= 'a' && c <= 'f') {
2887 tval = (int) c - (int) 'a' + 10;
2888 } else if (c == 'U' || c == 'L' || c == 'u' || c == 'l') {
2890 while (yytext[index] != '\0') {
2891 if (c == 'U' || c == 'L' || c == 'u' || c == 'l') {
2896 message ("Invalid character (%c) following specifier in hex constant: %s",
2897 c, cstring_fromChars (yytext)),
2907 message ("Invalid character (%c) in hex constant: %s",
2908 c, cstring_fromChars (yytext)),
2913 val = (val * 16) + tval;
2917 DPRINTF (("Hex constant: %s = %ld", yytext, val));
2922 long processOctal ()
2927 llassert (yytext[0] == '0' && yytext[1] != 'X' && yytext[1] != 'x');
2929 while (yytext[index] != '\0') {
2931 char c = yytext[index];
2933 if (c >= '0' && c <= '7') {
2934 tval = (int) c - (int) '0';
2938 message ("Invalid character (%c) in octal constant: %s",
2939 c, cstring_fromChars (yytext)),
2944 val = (val * 8) + tval;
2948 DPRINTF (("Octal constant: %s = %ld", yytext, val));
2955 return (atol (yytext));
2959 processSpec (int tok)
2961 size_t length = strlen (yytext);
2966 setTokLengthT (length);
2972 context_saveLocation ();
2973 setTokLengthT (length);
2974 return (processIdentifier (makeIdentifier (yytext)));
2978 void cscanner_expectingMetaStateName ()
2980 llassert (!expectingMetaStateName);
2981 llassert (context_inFunctionHeader ());
2982 expectingMetaStateName = TRUE;
2985 void cscanner_clearExpectingMetaStateName ()
2987 llassert (expectingMetaStateName);
2988 expectingMetaStateName = FALSE;