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"
66 # include "cgrammar.h"
67 # include "cgrammar_tokens.h"
69 # include "fileIdList.h"
72 # if defined(OS2) && defined(__IBMC__)
73 /* needed for isatty()... */
77 static bool lastWasString = FALSE;
78 static char savechar = '\0';
81 # define yyinput() (incColumn (), getc (yyin))
83 static /*@owned@*/ cstring lastidprocessed = cstring_undefined;
84 static int lminput (void);
85 static int tokLength = 0;
86 static bool inSpecPart = FALSE;
87 static bool continueLine = FALSE;
89 static int ninput (void);
90 static char processChar (void);
91 static double processFloat (void);
92 static /*@only@*/ exprNode processString (void);
93 static long processDec (void);
94 static long processHex (void);
95 static long processOctal (void);
96 static int processIdentifier (/*@only@*/ cstring)
97 /*@globals undef lastidprocessed@*/ ;
98 static bool processHashIdentifier (/*@only@*/ cstring)
99 /*@globals undef lastidprocessed@*/ ;
101 static int processSpec (int);
102 static bool handleSpecial (char *);
103 static int handleLlSpecial (void);
104 static void handleMacro (void);
105 static bool processMacro (void);
106 static /*@only@*/ cstring makeIdentifier (char *);
108 /* yes, this is exported! */
109 bool g_expectingTypeName = TRUE; /* beginning of file can be type name! */
111 static bool expectingMetaStateName = FALSE;
113 static int returnInt (ctype, long);
114 static int returnFloat (ctype, double);
115 static int returnChar (char);
116 static void setTokLength (int) /*@modifies g_currentloc@*/ ;
117 static void setTokLengthT (size_t) /*@modifies g_currentloc@*/ ;
119 static void advanceLine (void)
126 # define RETURN_INT(c,i) \
127 do { lastWasString = FALSE; \
128 return (returnInt (c, i)); } while (FALSE)
130 # define RETURN_FLOAT(c,f) \
131 do { lastWasString = FALSE; \
132 return (returnFloat (c, f)); \
135 # define RETURN_CHAR(c) \
136 do { lastWasString = FALSE; \
137 return (returnChar (c)); \
140 # define RETURN_TOK(t) \
141 do { yylval.tok = lltok_create (t, fileloc_decColumn (g_currentloc, tokLength)); \
143 lastWasString = FALSE; \
144 return (t); } while (FALSE)
146 # define RETURN_TYPE(t, ct) \
147 do { yylval.ctyp = ct; tokLength = 0; return (t); } while (FALSE)
149 /* don't fileloc_decColumn (g_currentloc, tokLength));
150 the string could have \n's in it!
153 # define RETURN_STRING(c) \
154 do { yylval.expr = exprNode_stringLiteral (c, fileloc_decColumn (g_currentloc, tokLength)); \
156 lastWasString = TRUE; \
157 return (CCONSTANT); } while (FALSE)
159 # define RETURN_EXPR(e) \
160 do { yylval.expr = e; \
162 lastWasString = TRUE; \
163 return (CCONSTANT); } while (FALSE)
167 static void setTokLength (int len)
171 DPRINTF (("Set tok length: %d", len));
174 static void setTokLengthT (size_t len)
176 setTokLength (size_toInt (len));
179 # include "flex.head"
181 /*@-unrecog@*/ /*@i5343@*/
187 "/*" { llfatalbug (cstring_makeLiteral ("Comment in pre-processor output")); }
189 "#"{Letter}({Letter}|{Digit})* {
190 context_saveLocation ();
191 setTokLength (longUnsigned_toInt (mstring_length (yytext)));
193 if (processHashIdentifier (makeIdentifier (yytext + 1)))
197 /* was nothing! */ /*@i32@*/
198 RETURN_STRING (cstring_makeLiteral ("\"\""));
202 RETURN_STRING (cstring_makeLiteral ("\"\""));
207 if (handleSpecial (yytext))
214 "#" { if (handleSpecial (yytext))
216 setTokLength (1); RETURN_TOK (0);
219 "..." { setTokLength (3); RETURN_TOK (CTOK_ELIPSIS); }
220 "break" { setTokLength (5); RETURN_TOK (BREAK); }
221 "case" { setTokLength (4); RETURN_TOK (CASE); }
222 "continue" { setTokLength (8); RETURN_TOK (CONTINUE); }
223 "default" { setTokLength (7); RETURN_TOK (DEFAULT); }
224 "do" { setTokLength (2); RETURN_TOK (DO); }
225 "else" { setTokLength (4); RETURN_TOK (CELSE); }
226 "for" { setTokLength (3); RETURN_TOK (CFOR); }
227 "goto" { setTokLength (4); RETURN_TOK (GOTO); }
228 "if" { setTokLength (2); RETURN_TOK (CIF); }
229 "return" { setTokLength (6); RETURN_TOK (RETURN); }
230 "sizeof" { setTokLength (6); RETURN_TOK (CSIZEOF); }
231 "offsetof" { setTokLength (8); RETURN_TOK (COFFSETOF); }
232 "switch" { setTokLength (6); RETURN_TOK (SWITCH); }
233 "while" { setTokLength (5); RETURN_TOK (WHILE); }
234 "va_arg" { setTokLength (6); RETURN_TOK (VA_ARG); }
235 "va_dcl" { setTokLength (6); RETURN_TOK (VA_DCL); }
237 /* gcc extension...this might not be appropriate */
238 setTokLength (6); RETURN_TOK (QINLINE); }
240 "struct" { setTokLength (6); RETURN_TOK (CSTRUCT); }
241 "typedef" { setTokLength (7); RETURN_TOK (CTYPEDEF); }
243 "union" { setTokLength (5); RETURN_TOK (CUNION); }
244 "enum" { setTokLength (4); RETURN_TOK (CENUM); }
246 "void" { setTokLength (4); RETURN_TYPE (CVOID, ctype_void); }
247 "int" { setTokLength (3); RETURN_TYPE (CINT, ctype_int); }
248 "double" { setTokLength (6); RETURN_TYPE (CDOUBLE, ctype_double); }
249 "char" { setTokLength (4); RETURN_TYPE (CGCHAR, ctype_char); }
250 "float" { setTokLength (5); RETURN_TYPE (CGFLOAT, ctype_float); }
252 "long" { setTokLength (4); RETURN_TOK (QLONG); }
253 "short" { setTokLength (5); RETURN_TOK (QSHORT); }
254 "unsigned" { setTokLength (8); RETURN_TOK (QUNSIGNED); }
255 "signed" { setTokLength (6); RETURN_TOK (QSIGNED); }
257 "volatile" { setTokLength (8); RETURN_TOK (QVOLATILE); }
258 "const" { setTokLength (5); RETURN_TOK (QCONST); }
260 /* some systems expect this! [gack!] */
261 "__const" { setTokLength (7); RETURN_TOK (QCONST); }
263 "extern" { setTokLength (6); RETURN_TOK (QEXTERN); }
264 "auto" { setTokLength (4); RETURN_TOK (QAUTO); }
265 "register" { setTokLength (8); RETURN_TOK (QREGISTER); }
266 "static" { setTokLength (6); RETURN_TOK (QSTATIC); }
268 \"(\\.|[^\\"])*\"([ \t\n]*\"(\\.|[^\\"])*\")* { RETURN_EXPR (processString ()); }
269 L\"(\\.|[^\\"])*\"([ \t\n]*\"(\\.|[^\\"])*\")* { RETURN_EXPR (processString ()); }
270 "out" { return (processSpec (QOUT)); }
271 "in" { return (processSpec (QIN)); }
272 "partial" { return (processSpec (QPARTIAL)); }
273 "special" { return (processSpec (QSPECIAL)); }
274 "anytype" { return (processSpec (QANYTYPE)); }
275 "integraltype" { return (processSpec (QINTEGRALTYPE)); }
276 "unsignedintegraltype" { return (processSpec (QUNSIGNEDINTEGRALTYPE)); }
277 "signedintegraltype" { return (processSpec (QSIGNEDINTEGRALTYPE)); }
278 "keep" { return (processSpec (QKEEP)); }
279 "null" { return (processSpec (QNULL)); }
280 "notnull" { return (processSpec (QNOTNULL)); }
281 "isnull" { return (processSpec (QISNULL)); }
282 "truenull" { return (processSpec (QTRUENULL)); }
283 "falsenull" { return (processSpec (QFALSENULL)); }
284 "relnull" { return (processSpec (QRELNULL)); }
285 "reldef" { return (processSpec (QRELDEF)); }
286 "exposed" { return (processSpec (QEXPOSED)); }
287 "newref" { return (processSpec (QNEWREF)); }
288 "tempref" { return (processSpec (QTEMPREF)); }
289 "killref" { return (processSpec (QKILLREF)); }
290 "refcounted" { return (processSpec (QREFCOUNTED)); }
291 "checked" { return (processSpec (QCHECKED)); }
292 "checkmod" { return (processSpec (QCHECKMOD)); }
293 "checkedstrict" { return (processSpec (QCHECKEDSTRICT)); }
294 "unchecked" { return (processSpec (QUNCHECKED)); }
295 "only" { return (processSpec (QONLY)); }
296 "owned" { return (processSpec (QOWNED)); }
297 "observer" { return (processSpec (QOBSERVER)); }
298 "dependent" { return (processSpec (QDEPENDENT)); }
299 "unused" { return (processSpec (QUNUSED)); }
300 "external" { return (processSpec (QEXTERNAL)); }
301 "sef" { return (processSpec (QSEF)); }
302 "shared" { return (processSpec (QSHARED)); }
303 "yield" { return (processSpec (QYIELD)); }
304 "undef" { return (processSpec (QUNDEF)); }
305 "killed" { return (processSpec (QKILLED)); }
306 "nullterminated" { return (processSpec (QNULLTERMINATED));}
307 "MaxSet" { return (processSpec (QMAXSET));}
308 "MaxRead" { return (processSpec (QMAXREAD));}
309 "maxSet" { return (processSpec (QMAXSET));}
310 "maxRead" { return (processSpec (QMAXREAD));}
312 {Letter}({Letter}|{Digit})* { int tok;
313 context_saveLocation ();
314 setTokLength (longUnsigned_toInt (mstring_length (yytext)));
315 tok = processIdentifier (makeIdentifier (yytext));
321 0[xX]{H}+ { setTokLengthT (mstring_length (yytext));
322 RETURN_INT (ctype_int, processHex ()); /* evs 2000-05-17 was ctype_uint */
324 0[xX]{H}+{L} { setTokLengthT (mstring_length (yytext));
325 RETURN_INT (ctype_lint, processHex ()); }
326 0[xX]{H}+{L}{L} { setTokLengthT (mstring_length (yytext));
327 RETURN_INT (ctype_llint, processHex ()); }
328 0[xX]{H}+{U} { setTokLengthT (mstring_length (yytext));
329 RETURN_INT (ctype_uint, processHex ()); }
330 0[xX]{H}+{ULSuffix} { setTokLengthT (mstring_length (yytext));
331 RETURN_INT (ctype_ulint, processHex ()); }
332 0[xX]{H}+{U}{L}{L} { setTokLengthT (mstring_length (yytext));
333 RETURN_INT (ctype_ullint, processHex ()); }
334 0[xX]{H}+{L}{L}{U} { setTokLengthT (mstring_length (yytext));
335 RETURN_INT (ctype_ullint, processHex ()); }
336 0{Digit}+ { setTokLengthT (mstring_length (yytext));
337 RETURN_INT (ctype_int, processOctal ()); }
338 0{Digit}+{U} { setTokLengthT (mstring_length (yytext));
339 RETURN_INT (ctype_uint, processOctal ()); }
340 0{Digit}+{L} { setTokLengthT (mstring_length (yytext));
341 RETURN_INT (ctype_lint, processOctal ()); }
342 0{Digit}+{L}{L} { setTokLengthT (mstring_length (yytext));
343 RETURN_INT (ctype_llint, processOctal ()); }
344 0{Digit}+{ULSuffix} { setTokLengthT (mstring_length (yytext));
345 RETURN_INT (ctype_ulint, processOctal ()); }
346 0{Digit}+{U}{L}{L} { setTokLengthT (mstring_length (yytext));
347 RETURN_INT (ctype_ullint, processOctal ()); }
348 0{Digit}+{L}{L}{U} { setTokLengthT (mstring_length (yytext));
349 RETURN_INT (ctype_ullint, processOctal ()); }
350 {Digit}+ { setTokLengthT (mstring_length (yytext));
351 RETURN_INT (ctype_int, processDec ()); }
352 {Digit}+{U} { setTokLengthT (mstring_length (yytext));
353 RETURN_INT (ctype_uint, processDec ()); }
354 {Digit}+{L} { setTokLengthT (mstring_length (yytext));
355 RETURN_INT (ctype_lint, processDec ()); }
356 {Digit}+{L}{L} { setTokLengthT (mstring_length (yytext));
357 RETURN_INT (ctype_llint, processDec ()); }
358 {Digit}+{ULSuffix} { setTokLengthT (mstring_length (yytext));
359 RETURN_INT (ctype_ulint, processDec ()); }
360 {Digit}+{U}{L}{L} { setTokLengthT (mstring_length (yytext));
361 RETURN_INT (ctype_ullint, processDec ()); }
362 {Digit}+{L}{L}{U} { setTokLengthT (mstring_length (yytext));
363 RETURN_INT (ctype_ullint, processDec ()); }
364 '(\\.|[^\\'])+' { setTokLengthT (mstring_length (yytext));
365 RETURN_CHAR (processChar ()); }
366 L'(\\.|[^\\'])+' { setTokLengthT (mstring_length (yytext));
367 RETURN_CHAR (processChar ()); }
368 {Digit}+{E}[fF] { setTokLengthT (mstring_length (yytext));
369 RETURN_FLOAT (ctype_float, processFloat ()); }
370 {Digit}+{E}[lL] { setTokLengthT (mstring_length (yytext));
371 RETURN_FLOAT (ctype_ldouble, processFloat ()); }
372 {Digit}+{E} { setTokLengthT (mstring_length (yytext));
373 RETURN_FLOAT (ctype_double, processFloat ()); }
375 {Digit}*"."{Digit}+({E})?[fF] { setTokLengthT (mstring_length (yytext));
376 RETURN_FLOAT (ctype_float, processFloat ()); }
377 {Digit}*"."{Digit}+({E})?[lL] { setTokLengthT (mstring_length (yytext));
378 RETURN_FLOAT (ctype_ldouble, processFloat ()); }
379 {Digit}*"."{Digit}+({E})? { setTokLengthT (mstring_length (yytext));
380 RETURN_FLOAT (ctype_double, processFloat ()); }
382 {Digit}+"."{Digit}*({E})?[fF] { setTokLengthT (mstring_length (yytext));
383 RETURN_FLOAT (ctype_float, processFloat ()); }
384 {Digit}+"."{Digit}*({E})?[lL] { setTokLengthT (mstring_length (yytext));
385 RETURN_FLOAT (ctype_ldouble, processFloat ()); }
386 {Digit}+"."{Digit}*({E})? { setTokLengthT (mstring_length (yytext));
387 RETURN_FLOAT (ctype_double, processFloat ()); }
389 ">>=" { setTokLength (3); RETURN_TOK (RIGHT_ASSIGN); }
390 "<<=" { setTokLength (3); RETURN_TOK (LEFT_ASSIGN); }
391 "+=" { setTokLength (2); RETURN_TOK (ADD_ASSIGN); }
392 "-=" { setTokLength (2); RETURN_TOK (SUB_ASSIGN); }
393 "*=" { setTokLength (2); RETURN_TOK (MUL_ASSIGN); }
394 "/=" { setTokLength (2); RETURN_TOK (DIV_ASSIGN); }
395 "%=" { setTokLength (2); RETURN_TOK (MOD_ASSIGN); }
396 "&=" { setTokLength (2); RETURN_TOK (AND_ASSIGN); }
397 "^=" { setTokLength (2); RETURN_TOK (XOR_ASSIGN); }
398 "|=" { setTokLength (2); RETURN_TOK (OR_ASSIGN); }
399 ">>" { setTokLength (2); RETURN_TOK (RIGHT_OP); }
400 "<<" { setTokLength (2); RETURN_TOK (LEFT_OP); }
401 "++" { setTokLength (2); RETURN_TOK (INC_OP); }
402 "--" { setTokLength (2); RETURN_TOK (DEC_OP); }
403 "->" { setTokLength (2); RETURN_TOK (ARROW_OP); }
404 "&&" { setTokLength (2); RETURN_TOK (AND_OP); }
405 "||" { setTokLength (2); RETURN_TOK (OR_OP); }
406 "<=" { setTokLength (2); RETURN_TOK (LE_OP); }
407 ">=" { setTokLength (2); RETURN_TOK (GE_OP); }
408 "==" { setTokLength (2); RETURN_TOK (EQ_OP); }
409 "!=" { setTokLength (2); RETURN_TOK (NE_OP); }
410 ";" { setTokLength (1); RETURN_TOK (TSEMI); }
411 "{" { setTokLength (1); RETURN_TOK (TLBRACE); }
412 "}" { setTokLength (1); RETURN_TOK (TRBRACE); }
413 "," { setTokLength (1); RETURN_TOK (TCOMMA); }
414 ":" { setTokLength (1); RETURN_TOK (TCOLON); }
415 "=" { setTokLength (1); RETURN_TOK (TASSIGN); }
416 "(" { setTokLength (1); RETURN_TOK (TLPAREN); }
417 ")" { setTokLength (1); RETURN_TOK (TRPAREN); }
418 "[" { setTokLength (1); RETURN_TOK (TLSQBR); }
419 "]" { setTokLength (1); RETURN_TOK (TRSQBR); }
420 "." { setTokLength (1); RETURN_TOK (TDOT); }
421 "&" { setTokLength (1); RETURN_TOK (TAMPERSAND); }
422 "!" { setTokLength (1); RETURN_TOK (TEXCL); }
425 "~" { setTokLength (1); RETURN_TOK (TTILDE); }
426 "-" { setTokLength (1); RETURN_TOK (TMINUS); }
427 "+" { setTokLength (1); RETURN_TOK (TPLUS); }
428 "*" { setTokLength (1); RETURN_TOK (TMULT); }
429 "/" { setTokLength (1); RETURN_TOK (TDIV); }
430 "%" { setTokLength (1); RETURN_TOK (TPERCENT); }
431 "<" { setTokLength (1); RETURN_TOK (TLT); }
432 ">" { setTokLength (1); RETURN_TOK (TGT); }
433 "^" { setTokLength (1); RETURN_TOK (TCIRC); }
434 "|" { setTokLength (1); RETURN_TOK (TBAR); }
435 "?" { setTokLength (1); RETURN_TOK (TQUEST); }
438 "/\\" { setTokLength (1); RETURN_TOK (TCAND); }
441 [ \t\v\f] { incColumn (); }
442 \n { context_incLineno ();
443 if (tokLength != 0) {
445 /* No error to report
448 message ("Likely parse error: token spans multiple lines."),
455 continueLine = FALSE;
459 if (context_inMacro ())
461 /* Don't use RETURN_TOK */
462 yylval.tok = lltok_create (TENDMACRO, g_currentloc);
463 lastWasString = FALSE;
468 "@@MR@@" { setTokLength (6);
470 if (processMacro ()) {
471 if (context_inIterDef ())
473 RETURN_TOK (LLMACROITER);
475 if (context_inIterEnd ())
477 RETURN_TOK (LLMACROEND);
479 if (context_inMacro ())
481 RETURN_TOK (LLMACRO);
485 "@QLMR" { if (context_inHeader () || context_inFunction ())
491 int nspchar = ninput ();
495 ** This is a hack to get the column number correct.
498 llassert (nspchar >= '0' && nspchar <= '9');
500 nspaces = nspchar - '0';
502 setTokLength (5 + nspaces);
506 if (context_inIterDef ())
508 RETURN_TOK (LLMACROITER);
510 if (context_inIterEnd ())
512 RETURN_TOK (LLMACROEND);
514 if (context_inMacro ())
516 RETURN_TOK (LLMACRO);
521 "@.CT" { setTokLength (4); lldiagmsg (ctype_unparseTable ()); }
522 "@.FA" { setTokLength (4); lldiagmsg (message ("Access types: %q", typeIdSet_unparse (context_fileAccessTypes ()))); }
523 "@.F" { setTokLength (3);
524 lldiagmsg (message ("%q: *** marker ***", fileloc_unparse (g_currentloc)));
526 "@.L" { setTokLength (3); usymtab_printLocal (); }
527 "@.A" { setTokLength (3); lldiagmsg (usymtab_unparseAliases ()); }
528 "@.C" { setTokLength (3); lldiagmsg (context_unparse ()); }
529 "@.W" { setTokLength (3); lldiagmsg (context_unparseClauses ()); }
530 "@.G" { setTokLength (3); usymtab_printGuards (); }
531 "@.S" { setTokLength (3); usymtab_printOut (); }
532 "@.X" { setTokLength (3); usymtab_printAll (); }
533 "@.Z" { setTokLength (3); usymtab_printComplete (); }
534 "@.T" { setTokLength (3); usymtab_printTypes (); }
535 "@.K" { setTokLength (3); lldiagmsg (usymtab_unparseStack ()); }
536 "@.M" { setTokLength (3);
537 lldiagmsg (message ("Can modify: %q",
538 sRefSet_unparse (context_modList ())));
540 "%{" { /* BEFORE_COMMENT_MARKER */
542 incColumn (); incColumn ();
543 tok = handleLlSpecial ();
547 if (tok == CANNOTATION) {
550 /* Beware - this bashes yylval! */
555 "%}" { /* AFTER_COMMENT_MARKER */
558 RETURN_TOK (QENDMACRO); }
559 "\\" { incColumn (); continueLine = TRUE; }
561 if ((int) *yytext == 13 ) {
566 message ("Invalid character (ascii: %d), skipping character",
575 /*@null@*/ /*@observer@*/ char *name;
580 ** These tokens are followed by syntax that is parsed by the
584 struct skeyword s_parsetable[] = {
585 { "modifies", QMODIFIES } ,
586 { "globals", QGLOBALS } ,
589 { "constant", QCONSTANT } ,
590 { "function", QFUNCTION } ,
592 { "defines", QDEFINES } ,
594 { "allocates", QALLOCATES } ,
596 { "releases", QRELEASES } ,
597 { "pre", QPRECLAUSE } ,
598 { "post", QPOSTCLAUSE } ,
599 { "setBufferSize", QSETBUFFERSIZE},
600 { "setStringLength", QSETSTRINGLENGTH},
601 { "testinRange", QTESTINRANGE},
602 { "requires", QPRECLAUSE } ,
603 { "ensures", QPOSTCLAUSE } ,
608 ** These tokens are either stand-alone tokens, or followed by
609 ** token-specific text.
612 struct skeyword s_keytable[] = {
613 { "anytype", QANYTYPE } ,
614 { "integraltype", QINTEGRALTYPE } ,
615 { "unsignedintegraltype", QUNSIGNEDINTEGRALTYPE } ,
616 { "signedintegraltype", QSIGNEDINTEGRALTYPE } ,
620 { "owned", QOWNED } ,
621 { "dependent", QDEPENDENT } ,
622 { "partial", QPARTIAL } ,
623 { "special", QSPECIAL } ,
624 { "truenull", QTRUENULL } ,
625 { "falsenull", QFALSENULL } ,
628 { "notnull", QNOTNULL } ,
629 { "abstract", QABSTRACT } ,
630 { "concrete", QCONCRETE } ,
631 { "mutable", QMUTABLE } ,
632 { "immutable", QIMMUTABLE } ,
633 { "unused", QUNUSED } ,
634 { "external", QEXTERNAL } ,
636 { "unique", QUNIQUE } ,
637 { "returned", QRETURNED } ,
638 { "exposed", QEXPOSED } ,
639 { "refcounted", QREFCOUNTED } ,
641 { "newref", QNEWREF } ,
642 { "tempref", QTEMPREF } ,
643 { "killref", QKILLREF } ,
645 { "relnull", QRELNULL } ,
646 { "nullterminated", QNULLTERMINATED },
647 { "setBufferSize", QSETBUFFERSIZE },
648 { "testInRange", QTESTINRANGE},
649 { "MaxSet", QMAXSET},
650 { "MaxRead", QMAXREAD},
651 { "reldef", QRELDEF } ,
652 { "observer", QOBSERVER } ,
653 { "exits", QEXITS } ,
654 { "mayexit", QMAYEXIT } ,
655 { "trueexit", QTRUEEXIT } ,
656 { "falseexit", QFALSEEXIT } ,
657 { "neverexit", QNEVEREXIT } ,
659 { "shared", QSHARED } ,
661 { "unchecked", QUNCHECKED } ,
662 { "checked", QCHECKED } ,
663 { "checkmod", QCHECKMOD } ,
664 { "checkedstrict", QCHECKEDSTRICT } ,
665 { "innercontinue", QINNERCONTINUE } ,
666 { "innerbreak", QINNERBREAK } ,
667 { "loopbreak", QLOOPBREAK } ,
668 { "switchbreak", QSWITCHBREAK } ,
669 { "safebreak", QSAFEBREAK } ,
670 { "fallthrough", QFALLTHROUGH } ,
671 { "l_fallthrou", QLINTFALLTHROUGH } ,
672 { "l_fallth", QLINTFALLTHRU } ,
673 { "notreached", QNOTREACHED } ,
674 { "l_notreach", QLINTNOTREACHED } ,
675 { "printflike", QPRINTFLIKE } ,
676 { "l_printfli", QLINTPRINTFLIKE } ,
677 { "scanflike", QSCANFLIKE } ,
678 { "messagelike", QMESSAGELIKE } ,
679 { "l_argsus", QARGSUSED } ,
684 ** would be better if these weren't hard coded...
687 static bool isArtificial (cstring s)
689 return (cstring_equalLit (s, "modifies")
690 || cstring_equalLit (s, "globals")
691 || cstring_equalLit (s, "warn")
692 || cstring_equalLit (s, "alt"));
695 void swallowMacro (void)
698 bool skipnext = FALSE;
700 while ((i = lminput ()) != EOF)
717 reader_checkUngetc (i, yyin);
725 reader_checkUngetc (i, yyin);
729 static int commentMarkerToken (cstring s)
733 while (s_parsetable[i].name != NULL)
735 DPRINTF (("Try :%s:%s:", s, s_parsetable[i].name));
737 if (cstring_equalLit (s, s_parsetable[i].name))
739 return s_parsetable[i].token;
748 static int tokenMacroCode (cstring s)
752 while (s_keytable[i].name != NULL)
754 if (cstring_equalLit (s, s_keytable[i].name))
756 if (s_keytable[i].token == QLINTFALLTHROUGH)
759 (FLG_WARNLINTCOMMENTS,
761 ("Traditional lint comment /*FALLTHROUGH*/ used. "
762 "This is interpreted by "
763 "LCLint in the same way as most Unix lints, but it is "
764 "preferable to replace it with the /*@fallthrough@*/ "
769 else if (s_keytable[i].token == QLINTFALLTHRU)
772 (FLG_WARNLINTCOMMENTS,
774 ("Traditional lint comment /*FALLTHRU*/ used. "
775 "This is interpreted by "
776 "LCLint in the same way as most Unix lints, but it is "
777 "preferable to replace it with the /*@fallthrough@*/ "
782 else if (s_keytable[i].token == QLINTNOTREACHED)
785 (FLG_WARNLINTCOMMENTS,
787 ("Traditional lint comment /*NOTREACHED*/ used. "
788 "This is interpreted by "
789 "LCLint in the same way as most Unix lints, but it is "
790 "preferable to replace it with the /*@notreached@*/ "
791 "semantic comment."),
796 else if (s_keytable[i].token == QPRINTFLIKE)
798 setSpecialFunction (qual_createPrintfLike ());
801 else if (s_keytable[i].token == QLINTPRINTFLIKE)
804 (FLG_WARNLINTCOMMENTS,
806 ("Traditional lint comment /*PRINTFLIKE*/ used. "
807 "This is interpreted by "
808 "LCLint in the same way as most Unix lints, but it is "
809 "preferable to replace it with either /*@printflike@*/, "
810 "/*@scanflike@*/ or /*@messagelike@*/."),
813 setSpecialFunction (qual_createPrintfLike ());
816 else if (s_keytable[i].token == QSCANFLIKE)
818 setSpecialFunction (qual_createScanfLike ());
821 else if (s_keytable[i].token == QMESSAGELIKE)
823 setSpecialFunction (qual_createMessageLike ());
826 else if (s_keytable[i].token == QARGSUSED)
829 (FLG_WARNLINTCOMMENTS,
831 ("Traditional lint comment /*ARGSUSED*/ used. "
832 "This is interpreted by "
833 "LCLint in the same way as most Unix lints, but it is "
834 "preferable to use /*@unused@*/ annotations on "
835 "the unused parameters."),
842 return s_keytable[i].token;
851 static int lminput ()
853 if (savechar == '\0')
860 int save = (int) savechar;
866 static void lmsavechar (char c)
868 if (savechar == '\0') savechar = c;
871 llbuglit ("lmsavechar: override");
875 static int returnFloat (ctype ct, double f)
877 yylval.expr = exprNode_floatLiteral (f, ct, cstring_fromChars (yytext),
878 fileloc_decColumn (g_currentloc, tokLength));
883 static int returnInt (ctype ct, long i)
887 if (ctype_equal (ct, ctype_int))
891 c = context_typeofZero ();
895 c = context_typeofOne ();
899 yylval.expr = exprNode_numLiteral (c, cstring_fromChars (yytext),
900 fileloc_decColumn (g_currentloc, tokLength), i);
905 static int returnChar (char c)
907 yylval.expr = exprNode_charLiteral (c, cstring_fromChars (yytext),
908 fileloc_decColumn (g_currentloc, tokLength));
917 if (c != EOF && ((char)c == '\n'))
919 context_incLineno ();
925 static char macro_nextChar ()
927 static bool in_quote = FALSE, in_escape = FALSE, in_char = FALSE;
932 c = char_fromInt (ic);
934 if (!in_quote && !in_char && (c == '\\' || c == BEFORE_COMMENT_MARKER[0]))
938 while ((c = char_fromInt (lminput ())) != '\0' && c != '\n')
940 ; /* skip to newline */
943 context_incLineno ();
947 return macro_nextChar ();
954 else /* if (c == '@') */
956 llassert (FALSE); /*@i23@*/
957 if (handleLlSpecial () != BADTOK)
959 llerrorlit (FLG_SYNTAX, "Macro cannot use special syntax");
962 return macro_nextChar ();
965 else if (!in_escape && c == '\"')
967 in_quote = !in_quote;
969 else if (!in_escape && c == '\'')
973 else if ((in_quote || in_char) && c == '\\')
975 in_escape = !in_escape;
977 else if ((in_quote || in_char) && in_escape)
981 else if (!in_quote && c == '/')
985 if ((c2 = char_fromInt (lminput ())) == '*')
989 while ((c2 = char_fromInt (lminput ())) != '\0'
990 && c2 != '\n' && c2 != '*')
997 while ((c2 = char_fromInt (lminput ())) != '\0'
1010 llfatalerror (cstring_makeLiteral ("Macro: bad comment!"));
1014 return macro_nextChar ();
1018 /*** putchar does not work! why? puts to stdio...??! ***/
1026 ** keeps semantic comments
1029 static char macro_nextCharC ()
1031 static bool in_quote = FALSE, in_escape = FALSE, in_char = FALSE;
1034 c = char_fromInt (lminput ());
1036 if (!in_quote && !in_char && c == '\\')
1038 while ((c = char_fromInt (lminput ())) != '\0' && c != '\n')
1040 ; /* skip to newline */
1043 context_incLineno ();
1047 return macro_nextCharC ();
1054 else if (!in_escape && c == '\"')
1056 in_quote = !in_quote;
1058 else if (!in_escape && c == '\'')
1062 else if ((in_quote || in_char) && c == '\\')
1064 in_escape = !in_escape;
1066 else if ((in_quote || in_char) && in_escape)
1070 else if (!in_quote && c == '/')
1074 if ((c2 = char_fromInt (lminput ())) == '*')
1078 while ((c2 = char_fromInt (lminput ())) != '\0'
1079 && c2 != '\n' && c2 != '*')
1086 while ((c2 = char_fromInt (lminput ())) != '\0'
1099 llfatalerror (cstring_makeLiteral ("Macro: bad comment!"));
1103 return macro_nextCharC ();
1114 ** skips whitespace (handles line continuations)
1115 ** returns first non-whitespace character
1118 static char skip_whitespace ()
1122 while ((c = macro_nextChar ()) == ' ' || c == '\t')
1130 static void handleMacro ()
1132 cstring mac = cstring_undefined;
1136 while (currentColumn () > 2)
1138 mac = cstring_appendChar (mac, ' ');
1142 c = macro_nextCharC ();
1144 if (c >= '0' && c <= '9')
1148 for (i = 0; i < ((c - '0') + 1); i++)
1150 mac = cstring_appendChar (mac, ' ');
1158 while (((c = macro_nextCharC ()) != '\0') && (c != '\n'))
1160 mac = cstring_appendChar (mac, c);
1164 macrocode = tokenMacroCode (mac);
1166 if (macrocode == BADTOK && !isArtificial (mac))
1168 context_addMacroCache (mac);
1177 context_incLineno ();
1181 static bool processMacro (void)
1186 cstring fname = cstring_undefined;
1188 bool isspecfcn = FALSE;
1189 bool isiter = FALSE;
1190 bool skipparam = FALSE;
1191 bool isenditer = FALSE;
1192 bool unknownm = FALSE;
1193 bool hasParams = FALSE;
1194 bool emptyMacro = FALSE;
1195 char c = skip_whitespace ();
1196 fileloc loc = fileloc_noColumn (g_currentloc);
1198 /* are both of these necessary? what do they mean? */
1199 uentryList specparams = uentryList_undefined;
1200 uentryList pn = uentryList_undefined;
1202 context_resetMacroMissingParams ();
1204 if (c == '\0' || c == '\n')
1206 llcontbug (cstring_makeLiteral ("Bad macro"));
1211 fname = cstring_appendChar (fname, c);
1213 while ((c = macro_nextChar ()) != '(' && c != '\0'
1214 && c != ' ' && c != '\t' && c != '\n')
1216 fname = cstring_appendChar (fname, c);
1219 if (c == ' ' || c == '\t' || c == '\n')
1225 while (c == ' ' || c == '\t')
1227 c = macro_nextChar ();
1241 hasParams = (c == '(');
1243 if (usymtab_exists (fname))
1245 e2 = usymtab_lookupExpose (fname);
1246 ct = uentry_getType (e2);
1248 if (uentry_isCodeDefined (e2)
1249 && fileloc_isUser (uentry_whereDefined (e2)))
1253 message ("Macro %s already defined", fname),
1256 uentry_showWhereDefined (e2);
1257 uentry_clearDefined (e2);
1260 if (uentry_isFunction (e2))
1262 uentry_setType (e2, ctype_unknown);
1265 context_enterUnknownMacro (e2);
1269 context_enterConstantMacro (e2);
1274 if (uentry_isForward (e2) && uentry_isFunction (e2))
1281 ("Parameterized macro has no prototype or specification: %s ",
1286 uentry_setType (e2, ctype_unknown);
1287 uentry_setFunctionDefined (e2, loc);
1288 uentry_setUsed (e2, fileloc_undefined);
1289 context_enterUnknownMacro (e2);
1293 if (uentry_isIter (e2))
1296 specparams = uentry_getParams (e2);
1297 noparams = uentryList_size (specparams);
1298 uentry_setDefined (e2, loc);
1299 context_enterIterDef (e2);
1301 else if (uentry_isEndIter (e2))
1304 uentry_setDefined (e2, loc);
1305 context_enterIterEnd (e2); /* don't care about it now */
1306 /* but should parse like an iter! */
1308 else if (uentry_isConstant (e2))
1314 message ("Constant %s implemented as parameterized macro",
1318 uentry_showWhereSpecified (e2);
1319 uentry_setType (e2, ctype_unknown);
1320 uentry_makeConstantFunction (e2);
1321 uentry_setDefined (e2, g_currentloc);
1322 uentry_setFunctionDefined (e2, g_currentloc);
1323 context_enterUnknownMacro (e2);
1327 if (!uentry_isSpecified (e2))
1329 fileloc oloc = uentry_whereDeclared (e2);
1331 if (fileloc_isLib (oloc))
1335 else if (fileloc_isUndefined (oloc)
1336 || fileloc_isPreproc (oloc))
1341 (FLG_MACROCONSTDECL,
1343 ("Macro constant %q not declared",
1344 uentry_getName (e2)),
1348 else if (!fileloc_withinLines (oloc, loc, 2))
1349 { /* bogus! will give errors if there is too much whitespace */
1353 ("Macro constant name %s does not match name in "
1354 "previous constant declaration. This constant "
1355 "is declared at %q", fname,
1356 fileloc_unparse (oloc)),
1361 context_enterConstantMacro (e2);
1362 cstring_free (fname);
1368 else if (ctype_isFunction (ct))
1371 specparams = ctype_argsFunction (ct);
1372 noparams = uentryList_size (specparams);
1374 uentry_setFunctionDefined (e2, loc);
1375 context_enterMacro (e2);
1377 else if (uentry_isVar (e2))
1383 message ("Variable %s implemented as parameterized macro",
1387 uentry_showWhereSpecified (e2);
1388 uentry_setType (e2, ctype_unknown);
1389 uentry_makeVarFunction (e2);
1390 uentry_setDefined (e2, g_currentloc);
1391 uentry_setFunctionDefined (e2, g_currentloc);
1392 context_enterUnknownMacro (e2);
1396 uentry ucons = uentry_makeConstant (fname,
1399 if (uentry_isExpandedMacro (e2))
1407 message ("Variable %s implemented by a macro",
1411 uentry_showWhereSpecified (e2);
1415 uentry_setDefined (e2, loc);
1416 uentry_setUsed (ucons, loc);
1418 context_enterConstantMacro (ucons);
1419 uentry_markOwned (ucons);
1420 cstring_free (fname);
1426 if (uentry_isDatatype (e2))
1430 message ("Type implemented as macro: %x",
1431 uentry_getName (e2)),
1432 message ("A type is implemented using a macro definition. A "
1433 "typedef should be used instead."),
1437 /* Must exit scope (not sure why a new scope was entered?) */
1438 usymtab_quietExitScope (g_currentloc);
1439 uentry_setDefined (e2, g_currentloc);
1445 (message ("Unexpanded macro not function or constant: %q",
1446 uentry_unparse (e2)));
1447 uentry_setType (e2, ctype_unknown);
1451 uentry_makeVarFunction (e2);
1452 uentry_setDefined (e2, g_currentloc);
1453 uentry_setFunctionDefined (e2, g_currentloc);
1454 context_enterUnknownMacro (e2);
1465 /* evans 2001-09-09 - if it has params, assume a function */
1469 (FLG_MACROMATCHNAME,
1470 message ("Unexpanded macro %s does not match name of a declared "
1471 "function. The name used in the control "
1472 "comment on the previous line should match.",
1476 ce = uentry_makeFunction (fname, ctype_unknown,
1480 warnClause_undefined,
1482 uentry_setUsed (ce, loc); /* perhaps bogus? */
1483 e2 = usymtab_supEntryReturn (ce);
1484 context_enterUnknownMacro (e2);
1489 (FLG_MACROMATCHNAME,
1490 message ("Unexpanded macro %s does not match name of a constant "
1491 "or iter declaration. The name used in the control "
1492 "comment on the previous line should match. "
1493 "(Assuming macro defines a constant.)",
1497 ce = uentry_makeConstant (fname, ctype_unknown, fileloc_undefined);
1498 uentry_setUsed (ce, loc); /* perhaps bogus? */
1499 e2 = usymtab_supEntryReturn (ce);
1501 context_enterConstantMacro (e2);
1502 cstring_free (fname);
1508 /* in macros, ( must follow immediatetly after name */
1514 c = skip_whitespace ();
1516 while (c != ')' && c != '\0')
1519 bool suppress = context_inSuppressRegion ();
1520 cstring paramname = cstring_undefined;
1523 ** save the parameter location
1527 context_saveLocation ();
1530 while (c != ' ' && c != '\t' && c != ',' && c != '\0' && c != ')')
1532 paramname = cstring_appendChar (paramname, c);
1533 c = macro_nextChar ();
1536 if (c == ' ' || c == '\t') c = skip_whitespace ();
1540 c = macro_nextChar ();
1541 if (c == ' ' || c == '\t') c = skip_whitespace ();
1546 llfatalerror (cstring_makeLiteral
1547 ("Bad macro syntax: uentryList"));
1550 if ((isspecfcn || isiter) && (paramno < noparams)
1551 && !uentry_isElipsisMarker (uentryList_getN
1552 (specparams, paramno)))
1554 fileloc sloc = context_getSaveLocation ();
1555 uentry decl = uentryList_getN (specparams, paramno);
1558 param = uentry_nameCopy (paramname, decl);
1560 uentry_setParam (param);
1561 sr = sRef_makeParam (paramno, uentry_getType (param), stateInfo_makeLoc (sloc));
1563 if (sRef_getNullState (sr) == NS_ABSNULL)
1565 ctype pt = ctype_realType (uentry_getType (param));
1567 if (ctype_isUser (pt))
1569 uentry te = usymtab_getTypeEntrySafe (ctype_typeId (pt));
1571 if (uentry_isValid (te))
1573 sRef_setStateFromUentry (sr, te);
1578 sRef_setNullState (sr, NS_UNKNOWN, sloc);
1582 uentry_setSref (param, sr);
1583 uentry_setDeclaredForceOnly (param, sloc);
1585 skipparam = isiter && uentry_isOut (uentryList_getN (specparams, paramno));
1589 fileloc sloc = context_getSaveLocation ();
1591 param = uentry_makeVariableSrefParam
1592 (paramname, ctype_unknown, fileloc_copy (sloc),
1593 sRef_makeParam (paramno, ctype_unknown, stateInfo_makeLoc (sloc)));
1594 DPRINTF (("Unknown param: %s", uentry_unparseFull (param)));
1595 cstring_free (paramname);
1597 sRef_setPosNull (uentry_getSref (param), sloc);
1598 uentry_setDeclaredForce (param, sloc);
1601 fileloc_free (sloc);
1606 llassert (!uentry_isElipsisMarker (param));
1610 sRef_makeUnsafe (uentry_getSref (param));
1613 pn = uentryList_add (pn, uentry_copy (param));
1614 usymtab_supEntry (param);
1618 /* don't add param */
1619 uentry_free (param);
1624 (void) macro_nextChar ();
1625 c = skip_whitespace ();
1633 if (isspecfcn || isiter)
1635 if (paramno != noparams && noparams >= 0)
1641 message ("Macro %s specified with %d args, defined with %d",
1642 fname, noparams, paramno),
1645 uentry_showWhereSpecified (e2);
1646 uentry_resetParams (e2, pn);
1651 uentry_resetParams (e2, pn);
1658 ** the form should be:
1660 ** # define newname oldname
1661 ** where oldname refers to a function matching the specification
1667 sRef_setGlobalScope ();
1668 usymtab_supGlobalEntry (uentry_makeVariableLoc (fname, ctype_unknown));
1669 sRef_clearGlobalScope ();
1673 context_setMacroMissingParams ();
1678 /* context_setuentryList (pn); */
1679 usymtab_enterScope ();
1682 cstring_free (fname);
1687 static bool handleSpecial (char *yyt)
1689 char *l = mstring_create (MAX_NAME_LENGTH);
1690 static bool reportcpp = FALSE;
1696 strcpy (l, yyt + 1);
1698 /* Need to safe original l for deallocating. */
1701 l += strlen (yyt) - 1;
1703 while ((c = char_fromInt (lminput ())) != '\n' && c != '\0')
1709 olc = cstring_fromChars (ol);
1711 if (cstring_equalPrefixLit (olc, "pragma"))
1713 char *pname = mstring_create (longUnsigned_fromInt (MAX_PRAGMA_LEN));
1714 char *opname = pname;
1715 char *ptr = ol + 6; /* pragma is six characters, plus space */
1719 /* skip whitespace */
1720 while (((c = *ptr) != '\0') && isspace (c))
1726 while (((c = *ptr) != '\0') && !isspace (c))
1730 if (len > MAX_PRAGMA_LEN)
1741 if (len == PRAGMA_LEN_EXPAND
1742 && mstring_equal (opname, PRAGMA_EXPAND))
1744 cstring exname = cstring_undefined;
1748 while (((c = *ptr) != '\0') && !isspace (c))
1750 exname = cstring_appendChar (exname, c);
1755 ue = usymtab_lookupExposeGlob (exname);
1757 if (uentry_isExpandedMacro (ue))
1759 if (fileloc_isPreproc (uentry_whereDefined (ue)))
1761 fileloc_setColumn (g_currentloc, 1);
1762 uentry_setDefined (ue, g_currentloc);
1766 cstring_free (exname);
1769 else if (cstring_equalPrefixLit (olc, "ident"))
1771 /* Some pre-processors will leave these in the code. Ignore rest of line */
1774 ** Yuk...Win32 filenames can have spaces in them...we need to read
1775 ** to the matching end quote.
1777 else if ((sscanf (ol, "line %d \"", &lineno) == 1)
1778 || (sscanf (ol, " %d \"", &lineno) == 1))
1784 /*@access cstring@*/
1785 while (*tmp != '\"' && *tmp != '\0')
1790 llassert (*tmp == '\"');
1796 while (*tmp != '\"' && *tmp != '\0')
1801 llassert (*tmp == '\"');
1805 # if defined(OS2) || defined(MSDOS) || defined(WIN32)
1808 ** DOS-like path delimiters get delivered in pairs, something like
1809 ** \"..\\\\file.h\", so we have to make it normal again. We do NOT
1810 ** remove the pre dirs yet as we usually specify tmp paths relative
1811 ** to the current directory, so tmp files would not get found in
1812 ** the hash table. If this method fails we try it again later.
1819 ** Skip past the drive marker.
1822 if (strchr (stmp, ':') != NULL)
1824 stmp = strchr (stmp, ':') + 1;
1827 while ((stmp = strchr (stmp, CONNECTCHAR)) != NULL )
1829 if (*(stmp+1) == CONNECTCHAR)
1831 memmove (stmp, stmp+1, strlen (stmp));
1837 fid = fileTable_lookupBase (context_fileTable (), fname);
1838 if (!(fileId_isValid (fid)))
1840 fname = removePreDirs (fname);
1841 fid = fileTable_lookupBase (context_fileTable (), fname);
1844 # else /* !defined(OS2) && !defined(MSDOS) */
1845 fname = removePreDirs (fname);
1846 fid = fileTable_lookupBase (context_fileTable (), fname);
1847 # endif /* !defined(OS2) && !defined(MSDOS) */
1849 if (!(fileId_isValid (fid)))
1851 if (context_inXHFile ())
1853 fid = fileTable_addXHFile (context_fileTable (), fname);
1855 else if (isHeaderFile (fname))
1857 fid = fileTable_addHeaderFile (context_fileTable (), fname);
1861 fid = fileTable_addFile (context_fileTable (), fname);
1865 setFileLine (fid, lineno);
1866 /*@noaccess cstring@*/
1868 else if ((sscanf (ol, "line %d", &lineno) == 1)
1869 || (sscanf (ol, " %d", &lineno) == 1))
1871 setLine (lineno); /* next line is <cr> */
1875 if (mstring_equal (ol, "")) {
1876 DPRINTF (("Empty pp command!"));
1878 ** evs 2000-05-16: This is a horrible kludge, to get around a bug (well, difficulty) in the pre-processor.
1879 ** We handle a plain # in the input file, by echoing it, and ignoring it in the post-pp-file.
1888 llbug (message ("File contains preprocessor command: #%s",
1889 cstring_fromChars (ol)));
1902 static int handleLlSpecial ()
1907 char *s = mstring_createEmpty ();
1913 loc = fileloc_copy (g_currentloc);
1914 DPRINTF (("Handle special: %s", fileloc_unparse (loc)));
1916 while (((ic = ninput ()) != 0) && isalpha (ic))
1919 s = mstring_append (s, c);
1923 DPRINTF (("Read: %s / %s", s, fileloc_unparse (g_currentloc)));
1926 if (charsread == 0 && ic == (int) AFTER_COMMENT_MARKER[0])
1930 llassert (ic == AFTER_COMMENT_MARKER[1]);
1936 return QNOMODS; /* special token no modifications token */
1940 DPRINTF (("Coment marker: %s", os));
1941 tok = commentMarkerToken (cstring_fromChars (os));
1945 tokLength = charsread;
1952 DPRINTF (("Not a comment marker..."));
1953 /* Add rest of the comment */
1955 if (ic != 0 && ic != EOF)
1959 s = mstring_append (s, c);
1962 while (((ic = ninput ()) != 0) && (ic != EOF)
1963 && (ic != AFTER_COMMENT_MARKER[0]))
1967 /* evans 2001-09-01 added to prevent assertion failures for uncloses syntactic comments */
1970 hasnl = TRUE; /* This prevents tokLength from being set later. */
1975 message ("Likely parse error: syntactic comment token spans multiple lines: %s",
1976 cstring_fromChars (s)),
1980 s = mstring_append (s, c);
1985 DPRINTF (("Read: %s / %s", s, fileloc_unparse (g_currentloc)));
1987 if (ic == AFTER_COMMENT_MARKER[0])
1990 llassert ((char) nc == AFTER_COMMENT_MARKER[1]);
1996 while (*s == ' ' || *s == '\t' || *s == '\n')
2001 if (*s == '-' || *s == '+' || *s == '=') /* setting flags */
2005 while (c == '-' || c == '+' || c == '=')
2007 ynm set = ynm_fromCodeChar (c);
2012 thisflag = cstring_fromChars (s);
2014 while ((c = *s) != '\0' && (c != '-') && (c != '=')
2015 && (c != '+') && (c != ' ') && (c != '\t') && (c != '\n'))
2022 if (!context_getFlag (FLG_NOCOMMENTS))
2024 cstring flagname = thisflag;
2025 flagcode fflag = identifyFlag (flagname);
2027 if (flagcode_isSkip (fflag))
2031 else if (flagcode_isInvalid (fflag))
2033 if (isMode (flagname))
2035 if (ynm_isMaybe (set))
2040 ("Semantic comment attempts to restore flag %s. "
2041 "A mode flag cannot be restored.",
2046 context_setMode (flagname);
2052 (FLG_UNRECOGFLAGCOMMENTS,
2053 message ("Unrecognized option in semantic comment: %s",
2058 else if (flagcode_isGlobalFlag (fflag))
2063 ("Semantic comment attempts to set global flag %s. "
2064 "A global flag cannot be set locally.",
2070 context_fileSetFlag (fflag, set);
2072 if (flagcode_hasArgument (fflag))
2074 if (ynm_isMaybe (set))
2079 ("Semantic comment attempts to restore flag %s. "
2080 "A flag for setting a value cannot be restored.",
2085 { /* cut-and-pastied from llmain...blecch */
2086 cstring extra = cstring_undefined;
2092 rest = mstring_copy (s);
2096 while ((rchar = *rest) != '\0'
2097 && (isspace (rchar)))
2103 while ((rchar = *rest) != '\0'
2104 && !isspace (rchar))
2106 extra = cstring_appendChar (extra, rchar);
2113 if (cstring_isUndefined (extra))
2118 ("Flag %s (in semantic comment) must be followed by an argument",
2119 flagcode_unparse (fflag)));
2125 if (flagcode_hasValue (fflag))
2127 setValueFlag (fflag, extra);
2129 else if (flagcode_hasString (fflag))
2131 setStringFlag (fflag, extra);
2148 while ((c == ' ') || (c == '\t') || (c == '\n'))
2154 if (context_inHeader () && !isArtificial (cstring_fromChars (os)))
2156 DPRINTF (("Here adding comment: %s", os));
2157 context_addComment (cstring_fromCharsNew (os));
2169 annotationInfo ainfo;
2171 while (*s != '\0' && *s != ' ' && *s != '\t' && *s != '\n')
2183 t = cstring_toCharsSafe (cstring_downcase (cstring_fromChars (t)));
2184 macrocode = tokenMacroCode (cstring_fromChars (t));
2186 if (macrocode != BADTOK)
2188 tokLength = hasnl ? 0 : mstring_length (t);
2194 if (macrocode == SKIPTOK)
2202 ainfo = context_lookupAnnotation (cstring_fromChars (os));
2204 if (annotationInfo_isDefined (ainfo)) {
2205 DPRINTF (("Found annotation: %s", annotationInfo_unparse (ainfo)));
2206 /*@i324@*/ yylval.annotation = ainfo;
2214 if (context_inHeader ())
2221 if ((context_inMacro () || context_inGlobalContext ())
2222 && macrocode != SKIPTOK
2223 && !isArtificial (cstring_fromChars (os)))
2225 DPRINTF (("Add comment: %s", os));
2226 context_addComment (cstring_fromCharsNew (os));
2239 if (mstring_equal (t, "ignore"))
2241 if (!context_getFlag (FLG_NOCOMMENTS))
2243 context_enterSuppressRegion ();
2246 else if ((*t == 'i' || *t == 't')
2247 && (*(t + 1) == '\0'))
2249 if (!context_getFlag (FLG_NOCOMMENTS)
2250 && (*t == 'i' || context_getFlag (FLG_TMPCOMMENTS)))
2252 context_enterSuppressLine (-1); /* infinite suppression */
2255 else if (((*t == 'i') || (*t == 't'))
2256 && ((*(t + 1) >= '0' && *(t + 1) <= '9')))
2258 bool tmpcomment = (*t == 't');
2260 char *tt = t; /* don't mangle t, since it is free'd */
2263 if (lc >= '0' && lc <= '9')
2265 val = (int)(lc - '0');
2268 while (lc >= '0' && lc <= '9')
2277 if (!context_getFlag (FLG_NOCOMMENTS)
2278 && (!tmpcomment || context_getFlag (FLG_TMPCOMMENTS)))
2280 context_enterSuppressLine (val);
2283 else if (mstring_equal (t, "end"))
2285 if (!context_getFlag (FLG_NOCOMMENTS))
2287 context_exitSuppressRegion ();
2290 else if (mstring_equal (t, "notfunction"))
2292 ; /* handled by pcpp */
2294 else if (mstring_equal (t, "access"))
2300 while ((c = *s) && (c == ' ' || c == '\t' || c == '\n'))
2310 tname = cstring_fromChars (s);
2312 while ((c = *s) != '\0' && c != ' '
2313 && c != '\t' && c != '\n' && c != ',')
2320 DPRINTF (("Access %s", tname));
2322 if (!context_getFlag (FLG_NOCOMMENTS)
2323 && !context_getFlag (FLG_NOACCESS))
2325 if (usymtab_existsType (tname))
2327 typeId uid = usymtab_getTypeId (tname);
2328 uentry ue = usymtab_getTypeEntry (uid);
2330 if (uentry_isAbstractDatatype (ue))
2332 context_addFileAccessType (uid);
2333 DPRINTF (("Adding access to: %s / %d", tname, uid));
2340 ("Non-abstract type %s used in access comment",
2347 if (!(context_inSuppressRegion ()
2348 || context_inSuppressZone (g_currentloc)))
2353 ("Unrecognized type %s used in access comment",
2365 if (c != ',' && c != ' ')
2371 else if (mstring_equal (t, "noaccess"))
2378 while ((lc = *s) && (lc == ' ' || lc == '\t' || lc == '\n'))
2388 tname = cstring_fromChars (s);
2390 while ((lc = *s) != '\0' && lc != ' ' && lc != '\t'
2391 && lc != '\n' && lc != ',')
2398 if (!context_getFlag (FLG_NOCOMMENTS)
2399 && !context_getFlag (FLG_NOACCESS))
2401 if (usymtab_existsType (tname))
2403 typeId tuid = usymtab_getTypeId (tname);
2405 if (context_couldHaveAccess (tuid))
2407 DPRINTF (("Removing access: %s", tname));
2408 context_removeFileAccessType (tuid);
2412 if (!(context_inSuppressRegion ()
2413 || context_inSuppressZone (g_currentloc)))
2415 uentry ue = usymtab_getTypeEntry (tuid);
2417 if (uentry_isAbstractDatatype (ue))
2422 ("Non-accessible abstract type %s used in noaccess comment",
2431 ("Non-abstract type %s used in noaccess comment",
2440 if (!(context_inSuppressRegion ()
2441 || context_inSuppressZone (g_currentloc)))
2446 ("Unrecognized type %s used in noaccess comment",
2458 if (lc != ',' && lc != ' ')
2466 voptgenerror (FLG_UNRECOGCOMMENTS,
2467 message ("Semantic comment unrecognized: %s",
2468 cstring_fromChars (os)), loc);
2479 static /*@only@*/ cstring makeIdentifier (char *s)
2481 char *c = mstring_create (size_toInt (strlen (s)) + 1);
2482 cstring id = cstring_fromChars (c);
2484 while (isalnum (*s) || (*s == '_') || (*s == '$'))
2493 /*@observer@*/ /*@dependent@*/ uentry coerceId (cstring cn)
2495 if (!(usymtab_exists (cn)))
2497 fileloc loc = fileloc_createExternal ();
2500 ** We need to put this in a global scope, otherwise the sRef will be deallocated.
2503 uentry ce = uentry_makeUnrecognized (cn, loc);
2505 if (!context_inIterEnd ())
2509 message ("Unrecognized (possibly system) identifier: %q",
2510 uentry_getName (ce)),
2517 return (usymtab_lookup (cn));
2521 ** like, coerceId, but doesn't supercede for iters
2524 /*@observer@*/ uentry coerceIterId (cstring cn)
2526 if (!(usymtab_exists (cn)))
2528 return uentry_undefined;
2531 return (usymtab_lookup (cn));
2534 /*@observer@*/ cstring LastIdentifier ()
2536 return (lastidprocessed);
2539 static int processIdentifier (cstring id)
2543 if (context_getFlag (FLG_GRAMMAR))
2545 lldiagmsg (message ("Process identifier: %s", id));
2548 context_clearJustPopped ();
2549 lastidprocessed = id;
2551 if (context_inFunctionHeader ())
2553 int tok = commentMarkerToken (id);
2554 DPRINTF (("in function decl..."));
2562 tok = tokenMacroCode (id);
2570 annotationInfo ainfo;
2572 if (expectingMetaStateName)
2574 metaStateInfo msinfo = context_lookupMetaStateInfo (id);
2576 if (metaStateInfo_isDefined (msinfo))
2578 yylval.msinfo = msinfo;
2579 return METASTATE_NAME;
2583 DPRINTF (("Not meta state name: %s", cstring_toCharsSafe (id)));
2587 ainfo = context_lookupAnnotation (id);
2589 if (annotationInfo_isDefined (ainfo))
2591 DPRINTF (("Found annotation: %s", annotationInfo_unparse (ainfo)));
2592 /*@i324@*/ yylval.annotation = ainfo;
2597 DPRINTF (("Not annotation: %s", id));
2603 /* Consider handling: Defined by C99 as static const char __func__[] */
2605 if (context_getFlag (FLG_GNUEXTENSIONS))
2609 if (cstring_equalLit (id, "__stdcall")
2610 || cstring_equalLit (id, "__cdecl")
2611 || cstring_equalLit (id, "__extension__"))
2615 else if (cstring_equalLit (id, "__volatile__"))
2619 else if (cstring_equalLit (id, "__signed"))
2623 else if (cstring_equalLit (id, "__unsigned"))
2627 else if (cstring_equalLit (id, "__const__"))
2631 else if (cstring_equalLit (id, "__alignof__"))
2633 tok = CALIGNOF; /* alignof is parsed like sizeof */
2635 else if (cstring_equalLit (id, "__FUNCTION__")
2636 || cstring_equalLit (id, "__PRETTY_FUNCTION__"))
2638 /* These tokens hold the name of the current function as strings */
2639 yylval.expr = exprNode_stringLiteral (id, fileloc_copy (g_currentloc));
2641 lastWasString = TRUE;
2645 else if (cstring_equalLit (id, "__attribute__")
2646 || cstring_equalLit (id, "__asm__")
2647 || cstring_equalLit (id, "_asm")
2648 || cstring_equalLit (id, "__asm")
2649 || cstring_equalLit (id, "__declspec"))
2652 bool useparens = FALSE;
2653 bool usebraces = FALSE;
2654 bool inquote = FALSE;
2655 bool inescape = FALSE;
2658 while ((ic = input ()) != EOF)
2665 else if (ic == '\\')
2669 else if (ic == '\"')
2705 else if (ic == ')' && useparens)
2708 if (depth == 0) break;
2710 else if (ic == '}' && usebraces)
2713 if (depth == 0) break;
2716 && !usebraces && !useparens
2717 && cstring_equalLit (id, "__asm"))
2720 ** We need this because some MS VC++ include files
2721 ** have __asm mov ... }
2722 ** Its a kludge, but otherwise would need to parse
2731 context_incLineno ();
2733 if (cstring_equalLit (id, "__asm")
2734 && !useparens && !usebraces)
2741 llassert ((useparens && ic == ')')
2742 || (usebraces && ic == '}')
2743 || (!useparens && !usebraces));
2747 else if (cstring_equalLit (id, "inline")
2748 || cstring_equalLit (id, "__inline")
2749 || cstring_equalLit (id, "_inline")
2750 || cstring_equalLit (id, "__inline__"))
2761 le = usymtab_lookupSafe (id);
2763 /*@-dependenttrans@*/
2765 if (uentry_isIter (le))
2767 /*@i32@*/ yylval.entry = le;
2770 else if (uentry_isEndIter (le))
2772 /*@i32@*/ yylval.entry = le;
2773 return (ITER_ENDNAME);
2775 else if (uentry_isUndefined (le))
2779 /* avoid parse errors for certain system built ins */
2781 if (g_expectingTypeName && (cstring_firstChar (id) == '_')
2782 && (cstring_secondChar (id) == '_'))
2784 return (TYPE_NAME_OR_ID);
2787 return (NEW_IDENTIFIER);
2789 else if (!uentry_isDeclared (le) && !uentry_isCodeDefined (le))
2791 if (uentry_isDatatype (le))
2794 return (NEW_IDENTIFIER);
2798 /*@i32@*/ yylval.entry = le;
2799 return (IDENTIFIER);
2802 else if (uentry_isDatatype (le))
2804 if (!g_expectingTypeName)
2808 return (NEW_IDENTIFIER);
2812 yylval.ctyp = uentry_getAbstractType (le);
2814 uentry_setUsed (le, g_currentloc);
2820 /*@i32@*/ yylval.entry = le;
2821 return (IDENTIFIER);
2824 /*@=dependenttrans@*/
2827 static bool processHashIdentifier (/*@only@*/ cstring id)
2829 if (context_inMacro () || context_inIterDef () ||
2830 context_inIterEnd ())
2834 context_clearJustPopped ();
2836 lastidprocessed = id;
2837 le = usymtab_lookupSafe (id);
2839 if (uentry_isParam (le) || uentry_isRefParam (le))
2856 static /*@only@*/ exprNode processString ()
2860 char *nl = strchr (yytext, '\n');
2861 cstring ns = cstring_fromCharsNew (yytext);
2865 loc = fileloc_copy (g_currentloc);
2866 addColumn (cstring_length (ns));
2872 loc = fileloc_copy (g_currentloc);
2874 context_incLineno ();
2876 while ((nl = strchr ((nl + 1), '\n')) != NULL)
2878 context_incLineno ();
2884 res = exprNode_stringLiteral (ns, loc);
2894 llassert (*yytext != '\0');
2895 fchar = *(yytext + 1);
2896 if (fchar != '\\') return fchar;
2898 next = *(yytext + 2);
2902 case 'n': return '\n';
2903 case 't': return '\t';
2904 case '\"': return '\"';
2905 case '\'': return '\'';
2906 case '\\': return '\\';
2907 default: return '\0';
2912 double processFloat ()
2914 double ret = atof (yytext);
2925 llassert (yytext[0] == '0'
2926 && (yytext[1] == 'X' || yytext[1] == 'x'));
2928 while (yytext[index] != '\0') {
2930 char c = yytext[index];
2932 if (c >= '0' && c <= '9') {
2933 tval = (int) c - (int) '0';
2934 } else if (c >= 'A' && c <= 'F') {
2935 tval = (int) c - (int) 'A' + 10;
2936 } else if (c >= 'a' && c <= 'f') {
2937 tval = (int) c - (int) 'a' + 10;
2938 } else if (c == 'U' || c == 'L' || c == 'u' || c == 'l') {
2940 while (yytext[index] != '\0') {
2941 if (c == 'U' || c == 'L' || c == 'u' || c == 'l') {
2946 message ("Invalid character (%c) following specifier in hex constant: %s",
2947 c, cstring_fromChars (yytext)),
2957 message ("Invalid character (%c) in hex constant: %s",
2958 c, cstring_fromChars (yytext)),
2963 val = (val * 16) + tval;
2967 DPRINTF (("Hex constant: %s = %ld", yytext, val));
2972 long processOctal ()
2977 llassert (yytext[0] == '0' && yytext[1] != 'X' && yytext[1] != 'x');
2979 while (yytext[index] != '\0') {
2981 char c = yytext[index];
2983 if (c >= '0' && c <= '7') {
2984 tval = (int) c - (int) '0';
2988 message ("Invalid character (%c) in octal constant: %s",
2989 c, cstring_fromChars (yytext)),
2994 val = (val * 8) + tval;
2998 DPRINTF (("Octal constant: %s = %ld", yytext, val));
3005 return (atol (yytext));
3009 processSpec (int tok)
3011 size_t length = strlen (yytext);
3015 setTokLengthT (length);
3021 context_saveLocation ();
3022 setTokLengthT (length);
3023 return (processIdentifier (makeIdentifier (yytext)));
3027 void cscanner_expectingMetaStateName ()
3029 llassert (!expectingMetaStateName);
3030 llassert (context_inFunctionHeader ());
3031 expectingMetaStateName = TRUE;
3034 void cscanner_clearExpectingMetaStateName ()
3036 llassert (expectingMetaStateName);
3037 expectingMetaStateName = FALSE;