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"
63 # if defined(OS2) && defined(__IBMC__)
64 /* needed for isatty()... */
72 # include "cgrammar.h"
73 # include "cgrammar_tokens.h"
75 # include "fileIdList.h"
78 static bool lastWasString = FALSE;
79 static char savechar = '\0';
82 # define yyinput() (incColumn (), getc (yyin))
84 static /*@owned@*/ cstring lastidprocessed = cstring_undefined;
85 static int lminput (void);
86 static int tokLength = 0;
87 static bool inSpecPart = FALSE;
88 static bool continueLine = FALSE;
90 static int ninput (void);
91 static char processChar (void);
92 static double processFloat (void);
93 static /*@only@*/ exprNode processString (void);
94 static long processDec (void);
95 static long processHex (void);
96 static long processOctal (void);
97 static int processIdentifier (/*@only@*/ cstring)
98 /*@globals undef lastidprocessed@*/ ;
99 static bool processHashIdentifier (/*@only@*/ cstring)
100 /*@globals undef lastidprocessed@*/ ;
102 static int processSpec (int);
103 static bool handleSpecial (char *);
104 static int handleLlSpecial (void);
105 static void handleMacro (void);
106 static bool processMacro (void);
107 static /*@only@*/ cstring makeIdentifier (char *);
109 /* yes, this is exported! */
110 bool g_expectingTypeName = TRUE; /* beginning of file can be type name! */
112 static bool expectingMetaStateName = FALSE;
114 static int returnInt (ctype, long);
115 static int returnFloat (ctype, double);
116 static int returnChar (char);
117 static void setTokLength (int) /*@modifies g_currentloc@*/ ;
118 static void setTokLengthT (size_t) /*@modifies g_currentloc@*/ ;
120 static void advanceLine (void)
127 # define RETURN_INT(c,i) \
128 do { lastWasString = FALSE; \
129 return (returnInt (c, i)); } while (FALSE)
131 # define RETURN_FLOAT(c,f) \
132 do { lastWasString = FALSE; \
133 return (returnFloat (c, f)); \
136 # define RETURN_CHAR(c) \
137 do { lastWasString = FALSE; \
138 return (returnChar (c)); \
141 # define RETURN_TOK(t) \
142 do { yylval.tok = lltok_create (t, fileloc_decColumn (g_currentloc, tokLength)); \
144 lastWasString = FALSE; \
145 return (t); } while (FALSE)
147 # define RETURN_TYPE(t, ct) \
148 do { yylval.ctyp = ct; tokLength = 0; return (t); } while (FALSE)
150 /* don't fileloc_decColumn (g_currentloc, tokLength));
151 the string could have \n's in it!
154 # define RETURN_STRING(c) \
155 do { yylval.expr = exprNode_stringLiteral (c, fileloc_decColumn (g_currentloc, tokLength)); \
157 lastWasString = TRUE; \
158 return (CCONSTANT); } while (FALSE)
160 # define RETURN_EXPR(e) \
161 do { yylval.expr = e; \
163 lastWasString = TRUE; \
164 return (CCONSTANT); } while (FALSE)
168 static void setTokLength (int len)
172 DPRINTF (("Set tok length: %d", len));
175 static void setTokLengthT (size_t len)
177 setTokLength (size_toInt (len));
180 # include "flex.head"
182 /*@-unrecog@*/ /*@i5343@*/
188 "/*" { llfatalbug (cstring_makeLiteral ("Comment in pre-processor output")); }
190 "#"{Letter}({Letter}|{Digit})* {
191 context_saveLocation ();
192 setTokLength (longUnsigned_toInt (mstring_length (yytext)));
194 if (processHashIdentifier (makeIdentifier (yytext + 1)))
198 /* was nothing! */ /*@i32@*/
199 RETURN_STRING (cstring_makeLiteral ("\"\""));
203 RETURN_STRING (cstring_makeLiteral ("\"\""));
208 if (handleSpecial (yytext))
215 "#" { if (handleSpecial (yytext))
217 setTokLength (1); RETURN_TOK (0);
220 "..." { setTokLength (3); RETURN_TOK (CTOK_ELIPSIS); }
221 "break" { setTokLength (5); RETURN_TOK (BREAK); }
222 "case" { setTokLength (4); RETURN_TOK (CASE); }
223 "continue" { setTokLength (8); RETURN_TOK (CONTINUE); }
224 "default" { setTokLength (7); RETURN_TOK (DEFAULT); }
225 "do" { setTokLength (2); RETURN_TOK (DO); }
226 "else" { setTokLength (4); RETURN_TOK (CELSE); }
227 "for" { setTokLength (3); RETURN_TOK (CFOR); }
228 "goto" { setTokLength (4); RETURN_TOK (GOTO); }
229 "if" { setTokLength (2); RETURN_TOK (CIF); }
230 "return" { setTokLength (6); RETURN_TOK (RETURN); }
231 "sizeof" { setTokLength (6); RETURN_TOK (CSIZEOF); }
232 "offsetof" { setTokLength (8); RETURN_TOK (COFFSETOF); }
233 "switch" { setTokLength (6); RETURN_TOK (SWITCH); }
234 "while" { setTokLength (5); RETURN_TOK (WHILE); }
235 "va_arg" { setTokLength (6); RETURN_TOK (VA_ARG); }
236 "va_dcl" { setTokLength (6); RETURN_TOK (VA_DCL); }
238 /* gcc extension...this might not be appropriate */
239 setTokLength (6); RETURN_TOK (QINLINE); }
241 "struct" { setTokLength (6); RETURN_TOK (CSTRUCT); }
242 "typedef" { setTokLength (7); RETURN_TOK (CTYPEDEF); }
244 "union" { setTokLength (5); RETURN_TOK (CUNION); }
245 "enum" { setTokLength (4); RETURN_TOK (CENUM); }
247 "void" { setTokLength (4); RETURN_TYPE (CVOID, ctype_void); }
248 "int" { setTokLength (3); RETURN_TYPE (CINT, ctype_int); }
249 "double" { setTokLength (6); RETURN_TYPE (CDOUBLE, ctype_double); }
250 "char" { setTokLength (4); RETURN_TYPE (CGCHAR, ctype_char); }
251 "float" { setTokLength (5); RETURN_TYPE (CGFLOAT, ctype_float); }
253 "long" { setTokLength (4); RETURN_TOK (QLONG); }
254 "short" { setTokLength (5); RETURN_TOK (QSHORT); }
255 "unsigned" { setTokLength (8); RETURN_TOK (QUNSIGNED); }
256 "signed" { setTokLength (6); RETURN_TOK (QSIGNED); }
258 "volatile" { setTokLength (8); RETURN_TOK (QVOLATILE); }
259 "const" { setTokLength (5); RETURN_TOK (QCONST); }
261 /* some systems expect this! [gack!] */
262 "__const" { setTokLength (7); RETURN_TOK (QCONST); }
264 "extern" { setTokLength (6); RETURN_TOK (QEXTERN); }
265 "auto" { setTokLength (4); RETURN_TOK (QAUTO); }
266 "register" { setTokLength (8); RETURN_TOK (QREGISTER); }
267 "static" { setTokLength (6); RETURN_TOK (QSTATIC); }
269 \"(\\.|[^\\"])*\"([ \t\n]*\"(\\.|[^\\"])*\")* { RETURN_EXPR (processString ()); }
270 L\"(\\.|[^\\"])*\"([ \t\n]*\"(\\.|[^\\"])*\")* { RETURN_EXPR (processString ()); }
271 "out" { return (processSpec (QOUT)); }
272 "in" { return (processSpec (QIN)); }
273 "partial" { return (processSpec (QPARTIAL)); }
274 "special" { return (processSpec (QSPECIAL)); }
275 "anytype" { return (processSpec (QANYTYPE)); }
276 "integraltype" { return (processSpec (QINTEGRALTYPE)); }
277 "unsignedintegraltype" { return (processSpec (QUNSIGNEDINTEGRALTYPE)); }
278 "signedintegraltype" { return (processSpec (QSIGNEDINTEGRALTYPE)); }
279 "keep" { return (processSpec (QKEEP)); }
280 "null" { return (processSpec (QNULL)); }
281 "notnull" { return (processSpec (QNOTNULL)); }
282 "isnull" { return (processSpec (QISNULL)); }
283 "truenull" { return (processSpec (QTRUENULL)); }
284 "falsenull" { return (processSpec (QFALSENULL)); }
285 "relnull" { return (processSpec (QRELNULL)); }
286 "reldef" { return (processSpec (QRELDEF)); }
287 "exposed" { return (processSpec (QEXPOSED)); }
288 "newref" { return (processSpec (QNEWREF)); }
289 "tempref" { return (processSpec (QTEMPREF)); }
290 "killref" { return (processSpec (QKILLREF)); }
291 "refcounted" { return (processSpec (QREFCOUNTED)); }
292 "checked" { return (processSpec (QCHECKED)); }
293 "checkmod" { return (processSpec (QCHECKMOD)); }
294 "checkedstrict" { return (processSpec (QCHECKEDSTRICT)); }
295 "unchecked" { return (processSpec (QUNCHECKED)); }
296 "only" { return (processSpec (QONLY)); }
297 "owned" { return (processSpec (QOWNED)); }
298 "observer" { return (processSpec (QOBSERVER)); }
299 "dependent" { return (processSpec (QDEPENDENT)); }
300 "unused" { return (processSpec (QUNUSED)); }
301 "external" { return (processSpec (QEXTERNAL)); }
302 "sef" { return (processSpec (QSEF)); }
303 "shared" { return (processSpec (QSHARED)); }
304 "yield" { return (processSpec (QYIELD)); }
305 "undef" { return (processSpec (QUNDEF)); }
306 "killed" { return (processSpec (QKILLED)); }
307 "nullterminated" { return (processSpec (QNULLTERMINATED));}
308 "MaxSet" { return (processSpec (QMAXSET));}
309 "MaxRead" { return (processSpec (QMAXREAD));}
310 "maxSet" { return (processSpec (QMAXSET));}
311 "maxRead" { return (processSpec (QMAXREAD));}
313 {Letter}({Letter}|{Digit})* { int tok;
314 context_saveLocation ();
315 setTokLength (longUnsigned_toInt (mstring_length (yytext)));
316 tok = processIdentifier (makeIdentifier (yytext));
322 0[xX]{H}+ { setTokLengthT (mstring_length (yytext));
323 RETURN_INT (ctype_int, processHex ()); /* evs 2000-05-17 was ctype_uint */
325 0[xX]{H}+{L} { setTokLengthT (mstring_length (yytext));
326 RETURN_INT (ctype_lint, processHex ()); }
327 0[xX]{H}+{L}{L} { setTokLengthT (mstring_length (yytext));
328 RETURN_INT (ctype_llint, processHex ()); }
329 0[xX]{H}+{U} { setTokLengthT (mstring_length (yytext));
330 RETURN_INT (ctype_uint, processHex ()); }
331 0[xX]{H}+{ULSuffix} { setTokLengthT (mstring_length (yytext));
332 RETURN_INT (ctype_ulint, processHex ()); }
333 0[xX]{H}+{U}{L}{L} { setTokLengthT (mstring_length (yytext));
334 RETURN_INT (ctype_ullint, processHex ()); }
335 0[xX]{H}+{L}{L}{U} { setTokLengthT (mstring_length (yytext));
336 RETURN_INT (ctype_ullint, processHex ()); }
337 0{Digit}+ { setTokLengthT (mstring_length (yytext));
338 RETURN_INT (ctype_int, processOctal ()); }
339 0{Digit}+{U} { setTokLengthT (mstring_length (yytext));
340 RETURN_INT (ctype_uint, processOctal ()); }
341 0{Digit}+{L} { setTokLengthT (mstring_length (yytext));
342 RETURN_INT (ctype_lint, processOctal ()); }
343 0{Digit}+{L}{L} { setTokLengthT (mstring_length (yytext));
344 RETURN_INT (ctype_llint, processOctal ()); }
345 0{Digit}+{ULSuffix} { setTokLengthT (mstring_length (yytext));
346 RETURN_INT (ctype_ulint, processOctal ()); }
347 0{Digit}+{U}{L}{L} { setTokLengthT (mstring_length (yytext));
348 RETURN_INT (ctype_ullint, processOctal ()); }
349 0{Digit}+{L}{L}{U} { setTokLengthT (mstring_length (yytext));
350 RETURN_INT (ctype_ullint, processOctal ()); }
351 {Digit}+ { setTokLengthT (mstring_length (yytext));
352 RETURN_INT (ctype_int, processDec ()); }
353 {Digit}+{U} { setTokLengthT (mstring_length (yytext));
354 RETURN_INT (ctype_uint, processDec ()); }
355 {Digit}+{L} { setTokLengthT (mstring_length (yytext));
356 RETURN_INT (ctype_lint, processDec ()); }
357 {Digit}+{L}{L} { setTokLengthT (mstring_length (yytext));
358 RETURN_INT (ctype_llint, processDec ()); }
359 {Digit}+{ULSuffix} { setTokLengthT (mstring_length (yytext));
360 RETURN_INT (ctype_ulint, processDec ()); }
361 {Digit}+{U}{L}{L} { setTokLengthT (mstring_length (yytext));
362 RETURN_INT (ctype_ullint, processDec ()); }
363 {Digit}+{L}{L}{U} { setTokLengthT (mstring_length (yytext));
364 RETURN_INT (ctype_ullint, processDec ()); }
365 '(\\.|[^\\'])+' { setTokLengthT (mstring_length (yytext));
366 RETURN_CHAR (processChar ()); }
367 L'(\\.|[^\\'])+' { setTokLengthT (mstring_length (yytext));
368 RETURN_CHAR (processChar ()); }
369 {Digit}+{E}[fF] { setTokLengthT (mstring_length (yytext));
370 RETURN_FLOAT (ctype_float, processFloat ()); }
371 {Digit}+{E}[lL] { setTokLengthT (mstring_length (yytext));
372 RETURN_FLOAT (ctype_ldouble, processFloat ()); }
373 {Digit}+{E} { setTokLengthT (mstring_length (yytext));
374 RETURN_FLOAT (ctype_double, processFloat ()); }
376 {Digit}*"."{Digit}+({E})?[fF] { setTokLengthT (mstring_length (yytext));
377 RETURN_FLOAT (ctype_float, processFloat ()); }
378 {Digit}*"."{Digit}+({E})?[lL] { setTokLengthT (mstring_length (yytext));
379 RETURN_FLOAT (ctype_ldouble, processFloat ()); }
380 {Digit}*"."{Digit}+({E})? { setTokLengthT (mstring_length (yytext));
381 RETURN_FLOAT (ctype_double, processFloat ()); }
383 {Digit}+"."{Digit}*({E})?[fF] { setTokLengthT (mstring_length (yytext));
384 RETURN_FLOAT (ctype_float, processFloat ()); }
385 {Digit}+"."{Digit}*({E})?[lL] { setTokLengthT (mstring_length (yytext));
386 RETURN_FLOAT (ctype_ldouble, processFloat ()); }
387 {Digit}+"."{Digit}*({E})? { setTokLengthT (mstring_length (yytext));
388 RETURN_FLOAT (ctype_double, processFloat ()); }
390 ">>=" { setTokLength (3); RETURN_TOK (RIGHT_ASSIGN); }
391 "<<=" { setTokLength (3); RETURN_TOK (LEFT_ASSIGN); }
392 "+=" { setTokLength (2); RETURN_TOK (ADD_ASSIGN); }
393 "-=" { setTokLength (2); RETURN_TOK (SUB_ASSIGN); }
394 "*=" { setTokLength (2); RETURN_TOK (MUL_ASSIGN); }
395 "/=" { setTokLength (2); RETURN_TOK (DIV_ASSIGN); }
396 "%=" { setTokLength (2); RETURN_TOK (MOD_ASSIGN); }
397 "&=" { setTokLength (2); RETURN_TOK (AND_ASSIGN); }
398 "^=" { setTokLength (2); RETURN_TOK (XOR_ASSIGN); }
399 "|=" { setTokLength (2); RETURN_TOK (OR_ASSIGN); }
400 ">>" { setTokLength (2); RETURN_TOK (RIGHT_OP); }
401 "<<" { setTokLength (2); RETURN_TOK (LEFT_OP); }
402 "++" { setTokLength (2); RETURN_TOK (INC_OP); }
403 "--" { setTokLength (2); RETURN_TOK (DEC_OP); }
404 "->" { setTokLength (2); RETURN_TOK (ARROW_OP); }
405 "&&" { setTokLength (2); RETURN_TOK (AND_OP); }
406 "||" { setTokLength (2); RETURN_TOK (OR_OP); }
407 "<=" { setTokLength (2); RETURN_TOK (LE_OP); }
408 ">=" { setTokLength (2); RETURN_TOK (GE_OP); }
409 "==" { setTokLength (2); RETURN_TOK (EQ_OP); }
410 "!=" { setTokLength (2); RETURN_TOK (NE_OP); }
411 ";" { setTokLength (1); RETURN_TOK (TSEMI); }
412 "{" { setTokLength (1); RETURN_TOK (TLBRACE); }
413 "}" { setTokLength (1); RETURN_TOK (TRBRACE); }
414 "," { setTokLength (1); RETURN_TOK (TCOMMA); }
415 ":" { setTokLength (1); RETURN_TOK (TCOLON); }
416 "=" { setTokLength (1); RETURN_TOK (TASSIGN); }
417 "(" { setTokLength (1); RETURN_TOK (TLPAREN); }
418 ")" { setTokLength (1); RETURN_TOK (TRPAREN); }
419 "[" { setTokLength (1); RETURN_TOK (TLSQBR); }
420 "]" { setTokLength (1); RETURN_TOK (TRSQBR); }
421 "." { setTokLength (1); RETURN_TOK (TDOT); }
422 "&" { setTokLength (1); RETURN_TOK (TAMPERSAND); }
423 "!" { setTokLength (1); RETURN_TOK (TEXCL); }
426 "~" { setTokLength (1); RETURN_TOK (TTILDE); }
427 "-" { setTokLength (1); RETURN_TOK (TMINUS); }
428 "+" { setTokLength (1); RETURN_TOK (TPLUS); }
429 "*" { setTokLength (1); RETURN_TOK (TMULT); }
430 "/" { setTokLength (1); RETURN_TOK (TDIV); }
431 "%" { setTokLength (1); RETURN_TOK (TPERCENT); }
432 "<" { setTokLength (1); RETURN_TOK (TLT); }
433 ">" { setTokLength (1); RETURN_TOK (TGT); }
434 "^" { setTokLength (1); RETURN_TOK (TCIRC); }
435 "|" { setTokLength (1); RETURN_TOK (TBAR); }
436 "?" { setTokLength (1); RETURN_TOK (TQUEST); }
439 "/\\" { setTokLength (1); RETURN_TOK (TCAND); }
442 [ \t\v\f] { incColumn (); }
443 \n { context_incLineno ();
444 if (tokLength != 0) {
446 /* No error to report
449 message ("Likely parse error: token spans multiple lines."),
456 continueLine = FALSE;
460 if (context_inMacro ())
462 /* Don't use RETURN_TOK */
463 yylval.tok = lltok_create (TENDMACRO, g_currentloc);
464 lastWasString = FALSE;
469 "@@MR@@" { setTokLength (6);
471 if (processMacro ()) {
472 if (context_inIterDef ())
474 RETURN_TOK (LLMACROITER);
476 if (context_inIterEnd ())
478 RETURN_TOK (LLMACROEND);
480 if (context_inMacro ())
482 RETURN_TOK (LLMACRO);
486 "@QLMR" { if (context_inHeader () || context_inFunction ())
492 int nspchar = ninput ();
496 ** This is a hack to get the column number correct.
499 llassert (nspchar >= '0' && nspchar <= '9');
501 nspaces = nspchar - '0';
503 setTokLength (5 + nspaces);
507 if (context_inIterDef ())
509 RETURN_TOK (LLMACROITER);
511 if (context_inIterEnd ())
513 RETURN_TOK (LLMACROEND);
515 if (context_inMacro ())
517 RETURN_TOK (LLMACRO);
522 "@.CT" { setTokLength (4); lldiagmsg (ctype_unparseTable ()); }
523 "@.FA" { setTokLength (4); lldiagmsg (message ("Access types: %q", typeIdSet_unparse (context_fileAccessTypes ()))); }
524 "@.F" { setTokLength (3);
525 lldiagmsg (message ("%q: *** marker ***", fileloc_unparse (g_currentloc)));
527 "@.L" { setTokLength (3); usymtab_printLocal (); }
528 "@.A" { setTokLength (3); lldiagmsg (usymtab_unparseAliases ()); }
529 "@.C" { setTokLength (3); lldiagmsg (context_unparse ()); }
530 "@.W" { setTokLength (3); lldiagmsg (context_unparseClauses ()); }
531 "@.G" { setTokLength (3); usymtab_printGuards (); }
532 "@.S" { setTokLength (3); usymtab_printOut (); }
533 "@.X" { setTokLength (3); usymtab_printAll (); }
534 "@.Z" { setTokLength (3); usymtab_printComplete (); }
535 "@.T" { setTokLength (3); usymtab_printTypes (); }
536 "@.K" { setTokLength (3); lldiagmsg (usymtab_unparseStack ()); }
537 "@.M" { setTokLength (3);
538 lldiagmsg (message ("Can modify: %q",
539 sRefSet_unparse (context_modList ())));
541 "%{" { /* BEFORE_COMMENT_MARKER */
543 incColumn (); incColumn ();
544 tok = handleLlSpecial ();
548 if (tok == CANNOTATION) {
551 /* Beware - this bashes yylval! */
556 "%}" { /* AFTER_COMMENT_MARKER */
559 RETURN_TOK (QENDMACRO); }
560 "\\" { incColumn (); continueLine = TRUE; }
562 if ((int) *yytext == 13 ) {
567 message ("Invalid character (ascii: %d), skipping character",
576 /*@null@*/ /*@observer@*/ char *name;
581 ** These tokens are followed by syntax that is parsed by the
585 struct skeyword s_parsetable[] = {
586 { "modifies", QMODIFIES } ,
587 { "globals", QGLOBALS } ,
590 { "constant", QCONSTANT } ,
591 { "function", QFUNCTION } ,
593 { "defines", QDEFINES } ,
595 { "allocates", QALLOCATES } ,
597 { "releases", QRELEASES } ,
598 { "pre", QPRECLAUSE } ,
599 { "post", QPOSTCLAUSE } ,
600 { "setBufferSize", QSETBUFFERSIZE},
601 { "setStringLength", QSETSTRINGLENGTH},
602 { "testinRange", QTESTINRANGE},
603 { "requires", QPRECLAUSE } ,
604 { "ensures", QPOSTCLAUSE } ,
609 ** These tokens are either stand-alone tokens, or followed by
610 ** token-specific text.
613 struct skeyword s_keytable[] = {
614 { "anytype", QANYTYPE } ,
615 { "integraltype", QINTEGRALTYPE } ,
616 { "unsignedintegraltype", QUNSIGNEDINTEGRALTYPE } ,
617 { "signedintegraltype", QSIGNEDINTEGRALTYPE } ,
621 { "owned", QOWNED } ,
622 { "dependent", QDEPENDENT } ,
623 { "partial", QPARTIAL } ,
624 { "special", QSPECIAL } ,
625 { "truenull", QTRUENULL } ,
626 { "falsenull", QFALSENULL } ,
629 { "notnull", QNOTNULL } ,
630 { "abstract", QABSTRACT } ,
631 { "concrete", QCONCRETE } ,
632 { "mutable", QMUTABLE } ,
633 { "immutable", QIMMUTABLE } ,
634 { "unused", QUNUSED } ,
635 { "external", QEXTERNAL } ,
637 { "unique", QUNIQUE } ,
638 { "returned", QRETURNED } ,
639 { "exposed", QEXPOSED } ,
640 { "refcounted", QREFCOUNTED } ,
642 { "newref", QNEWREF } ,
643 { "tempref", QTEMPREF } ,
644 { "killref", QKILLREF } ,
646 { "relnull", QRELNULL } ,
647 { "nullterminated", QNULLTERMINATED },
648 { "setBufferSize", QSETBUFFERSIZE },
649 { "testInRange", QTESTINRANGE},
650 { "MaxSet", QMAXSET},
651 { "MaxRead", QMAXREAD},
652 { "reldef", QRELDEF } ,
653 { "observer", QOBSERVER } ,
654 { "exits", QEXITS } ,
655 { "mayexit", QMAYEXIT } ,
656 { "trueexit", QTRUEEXIT } ,
657 { "falseexit", QFALSEEXIT } ,
658 { "neverexit", QNEVEREXIT } ,
660 { "shared", QSHARED } ,
662 { "unchecked", QUNCHECKED } ,
663 { "checked", QCHECKED } ,
664 { "checkmod", QCHECKMOD } ,
665 { "checkedstrict", QCHECKEDSTRICT } ,
666 { "innercontinue", QINNERCONTINUE } ,
667 { "innerbreak", QINNERBREAK } ,
668 { "loopbreak", QLOOPBREAK } ,
669 { "switchbreak", QSWITCHBREAK } ,
670 { "safebreak", QSAFEBREAK } ,
671 { "fallthrough", QFALLTHROUGH } ,
672 { "l_fallthrou", QLINTFALLTHROUGH } ,
673 { "l_fallth", QLINTFALLTHRU } ,
674 { "notreached", QNOTREACHED } ,
675 { "l_notreach", QLINTNOTREACHED } ,
676 { "printflike", QPRINTFLIKE } ,
677 { "l_printfli", QLINTPRINTFLIKE } ,
678 { "scanflike", QSCANFLIKE } ,
679 { "messagelike", QMESSAGELIKE } ,
680 { "l_argsus", QARGSUSED } ,
685 ** would be better if these weren't hard coded...
688 static bool isArtificial (cstring s)
690 return (cstring_equalLit (s, "modifies")
691 || cstring_equalLit (s, "globals")
692 || cstring_equalLit (s, "warn")
693 || cstring_equalLit (s, "alt"));
696 void swallowMacro (void)
699 bool skipnext = FALSE;
701 while ((i = lminput ()) != EOF)
718 reader_checkUngetc (i, yyin);
726 reader_checkUngetc (i, yyin);
730 static int commentMarkerToken (cstring s)
734 while (s_parsetable[i].name != NULL)
736 DPRINTF (("Try :%s:%s:", s, s_parsetable[i].name));
738 if (cstring_equalLit (s, s_parsetable[i].name))
740 return s_parsetable[i].token;
749 static int tokenMacroCode (cstring s)
753 while (s_keytable[i].name != NULL)
755 if (cstring_equalLit (s, s_keytable[i].name))
757 if (s_keytable[i].token == QLINTFALLTHROUGH)
760 (FLG_WARNLINTCOMMENTS,
762 ("Traditional lint comment /*FALLTHROUGH*/ used. "
763 "This is interpreted by "
764 "LCLint in the same way as most Unix lints, but it is "
765 "preferable to replace it with the /*@fallthrough@*/ "
770 else if (s_keytable[i].token == QLINTFALLTHRU)
773 (FLG_WARNLINTCOMMENTS,
775 ("Traditional lint comment /*FALLTHRU*/ used. "
776 "This is interpreted by "
777 "LCLint in the same way as most Unix lints, but it is "
778 "preferable to replace it with the /*@fallthrough@*/ "
783 else if (s_keytable[i].token == QLINTNOTREACHED)
786 (FLG_WARNLINTCOMMENTS,
788 ("Traditional lint comment /*NOTREACHED*/ used. "
789 "This is interpreted by "
790 "LCLint in the same way as most Unix lints, but it is "
791 "preferable to replace it with the /*@notreached@*/ "
792 "semantic comment."),
797 else if (s_keytable[i].token == QPRINTFLIKE)
799 setSpecialFunction (qual_createPrintfLike ());
802 else if (s_keytable[i].token == QLINTPRINTFLIKE)
805 (FLG_WARNLINTCOMMENTS,
807 ("Traditional lint comment /*PRINTFLIKE*/ used. "
808 "This is interpreted by "
809 "LCLint in the same way as most Unix lints, but it is "
810 "preferable to replace it with either /*@printflike@*/, "
811 "/*@scanflike@*/ or /*@messagelike@*/."),
814 setSpecialFunction (qual_createPrintfLike ());
817 else if (s_keytable[i].token == QSCANFLIKE)
819 setSpecialFunction (qual_createScanfLike ());
822 else if (s_keytable[i].token == QMESSAGELIKE)
824 setSpecialFunction (qual_createMessageLike ());
827 else if (s_keytable[i].token == QARGSUSED)
830 (FLG_WARNLINTCOMMENTS,
832 ("Traditional lint comment /*ARGSUSED*/ used. "
833 "This is interpreted by "
834 "LCLint in the same way as most Unix lints, but it is "
835 "preferable to use /*@unused@*/ annotations on "
836 "the unused parameters."),
843 return s_keytable[i].token;
852 static int lminput ()
854 if (savechar == '\0')
861 int save = (int) savechar;
867 static void lmsavechar (char c)
869 if (savechar == '\0') savechar = c;
872 llbuglit ("lmsavechar: override");
876 static int returnFloat (ctype ct, double f)
878 yylval.expr = exprNode_floatLiteral (f, ct, cstring_fromChars (yytext),
879 fileloc_decColumn (g_currentloc, tokLength));
884 static int returnInt (ctype ct, long i)
888 if (ctype_equal (ct, ctype_int))
892 c = context_typeofZero ();
896 c = context_typeofOne ();
900 yylval.expr = exprNode_numLiteral (c, cstring_fromChars (yytext),
901 fileloc_decColumn (g_currentloc, tokLength), i);
906 static int returnChar (char c)
908 yylval.expr = exprNode_charLiteral (c, cstring_fromChars (yytext),
909 fileloc_decColumn (g_currentloc, tokLength));
918 if (c != EOF && ((char)c == '\n'))
920 context_incLineno ();
926 static char macro_nextChar ()
928 static bool in_quote = FALSE, in_escape = FALSE, in_char = FALSE;
933 c = char_fromInt (ic);
935 if (!in_quote && !in_char && (c == '\\' || c == BEFORE_COMMENT_MARKER[0]))
939 while ((c = char_fromInt (lminput ())) != '\0' && c != '\n')
941 ; /* skip to newline */
944 context_incLineno ();
948 return macro_nextChar ();
955 else /* if (c == '@') */
957 llassert (FALSE); /*@i23@*/
958 if (handleLlSpecial () != BADTOK)
960 llerrorlit (FLG_SYNTAX, "Macro cannot use special syntax");
963 return macro_nextChar ();
966 else if (!in_escape && c == '\"')
968 in_quote = !in_quote;
970 else if (!in_escape && c == '\'')
974 else if ((in_quote || in_char) && c == '\\')
976 in_escape = !in_escape;
978 else if ((in_quote || in_char) && in_escape)
982 else if (!in_quote && c == '/')
986 if ((c2 = char_fromInt (lminput ())) == '*')
990 while ((c2 = char_fromInt (lminput ())) != '\0'
991 && c2 != '\n' && c2 != '*')
998 while ((c2 = char_fromInt (lminput ())) != '\0'
1011 llfatalerror (cstring_makeLiteral ("Macro: bad comment!"));
1015 return macro_nextChar ();
1019 /*** putchar does not work! why? puts to stdio...??! ***/
1027 ** keeps semantic comments
1030 static char macro_nextCharC ()
1032 static bool in_quote = FALSE, in_escape = FALSE, in_char = FALSE;
1035 c = char_fromInt (lminput ());
1037 if (!in_quote && !in_char && c == '\\')
1039 while ((c = char_fromInt (lminput ())) != '\0' && c != '\n')
1041 ; /* skip to newline */
1044 context_incLineno ();
1048 return macro_nextCharC ();
1055 else if (!in_escape && c == '\"')
1057 in_quote = !in_quote;
1059 else if (!in_escape && c == '\'')
1063 else if ((in_quote || in_char) && c == '\\')
1065 in_escape = !in_escape;
1067 else if ((in_quote || in_char) && in_escape)
1071 else if (!in_quote && c == '/')
1075 if ((c2 = char_fromInt (lminput ())) == '*')
1079 while ((c2 = char_fromInt (lminput ())) != '\0'
1080 && c2 != '\n' && c2 != '*')
1087 while ((c2 = char_fromInt (lminput ())) != '\0'
1100 llfatalerror (cstring_makeLiteral ("Macro: bad comment!"));
1104 return macro_nextCharC ();
1115 ** skips whitespace (handles line continuations)
1116 ** returns first non-whitespace character
1119 static char skip_whitespace ()
1123 while ((c = macro_nextChar ()) == ' ' || c == '\t')
1131 static void handleMacro ()
1133 cstring mac = cstring_undefined;
1137 while (currentColumn () > 2)
1139 mac = cstring_appendChar (mac, ' ');
1143 c = macro_nextCharC ();
1145 if (c >= '0' && c <= '9')
1149 for (i = 0; i < ((c - '0') + 1); i++)
1151 mac = cstring_appendChar (mac, ' ');
1159 while (((c = macro_nextCharC ()) != '\0') && (c != '\n'))
1161 mac = cstring_appendChar (mac, c);
1165 macrocode = tokenMacroCode (mac);
1167 if (macrocode == BADTOK && !isArtificial (mac))
1169 context_addMacroCache (mac);
1178 context_incLineno ();
1182 static bool processMacro (void)
1187 cstring fname = cstring_undefined;
1189 bool isspecfcn = FALSE;
1190 bool isiter = FALSE;
1191 bool skipparam = FALSE;
1192 bool isenditer = FALSE;
1193 bool unknownm = FALSE;
1194 bool hasParams = FALSE;
1195 bool emptyMacro = FALSE;
1196 char c = skip_whitespace ();
1197 fileloc loc = fileloc_noColumn (g_currentloc);
1199 /* are both of these necessary? what do they mean? */
1200 uentryList specparams = uentryList_undefined;
1201 uentryList pn = uentryList_undefined;
1203 context_resetMacroMissingParams ();
1205 if (c == '\0' || c == '\n')
1207 llcontbug (cstring_makeLiteral ("Bad macro"));
1212 fname = cstring_appendChar (fname, c);
1214 while ((c = macro_nextChar ()) != '(' && c != '\0'
1215 && c != ' ' && c != '\t' && c != '\n')
1217 fname = cstring_appendChar (fname, c);
1220 if (c == ' ' || c == '\t' || c == '\n')
1226 while (c == ' ' || c == '\t')
1228 c = macro_nextChar ();
1242 hasParams = (c == '(');
1244 if (usymtab_exists (fname))
1246 e2 = usymtab_lookupExpose (fname);
1247 ct = uentry_getType (e2);
1249 if (uentry_isCodeDefined (e2)
1250 && fileloc_isUser (uentry_whereDefined (e2)))
1254 message ("Macro %s already defined", fname),
1257 uentry_showWhereDefined (e2);
1258 uentry_clearDefined (e2);
1261 if (uentry_isFunction (e2))
1263 uentry_setType (e2, ctype_unknown);
1266 context_enterUnknownMacro (e2);
1270 context_enterConstantMacro (e2);
1275 if (uentry_isForward (e2) && uentry_isFunction (e2))
1282 ("Parameterized macro has no prototype or specification: %s ",
1287 uentry_setType (e2, ctype_unknown);
1288 uentry_setFunctionDefined (e2, loc);
1289 uentry_setUsed (e2, fileloc_undefined);
1290 context_enterUnknownMacro (e2);
1294 if (uentry_isIter (e2))
1297 specparams = uentry_getParams (e2);
1298 noparams = uentryList_size (specparams);
1299 uentry_setDefined (e2, loc);
1300 context_enterIterDef (e2);
1302 else if (uentry_isEndIter (e2))
1305 uentry_setDefined (e2, loc);
1306 context_enterIterEnd (e2); /* don't care about it now */
1307 /* but should parse like an iter! */
1309 else if (uentry_isConstant (e2))
1315 message ("Constant %s implemented as parameterized macro",
1319 uentry_showWhereSpecified (e2);
1320 uentry_setType (e2, ctype_unknown);
1321 uentry_makeConstantFunction (e2);
1322 uentry_setDefined (e2, g_currentloc);
1323 uentry_setFunctionDefined (e2, g_currentloc);
1324 context_enterUnknownMacro (e2);
1328 if (!uentry_isSpecified (e2))
1330 fileloc oloc = uentry_whereDeclared (e2);
1332 if (fileloc_isLib (oloc))
1336 else if (fileloc_isUndefined (oloc)
1337 || fileloc_isPreproc (oloc))
1342 (FLG_MACROCONSTDECL,
1344 ("Macro constant %q not declared",
1345 uentry_getName (e2)),
1349 else if (!fileloc_withinLines (oloc, loc, 2))
1350 { /* bogus! will give errors if there is too much whitespace */
1354 ("Macro constant name %s does not match name in "
1355 "previous constant declaration. This constant "
1356 "is declared at %q", fname,
1357 fileloc_unparse (oloc)),
1362 context_enterConstantMacro (e2);
1363 cstring_free (fname);
1369 else if (ctype_isFunction (ct))
1372 specparams = ctype_argsFunction (ct);
1373 noparams = uentryList_size (specparams);
1375 uentry_setFunctionDefined (e2, loc);
1376 context_enterMacro (e2);
1378 else if (uentry_isVar (e2))
1384 message ("Variable %s implemented as parameterized macro",
1388 uentry_showWhereSpecified (e2);
1389 uentry_setType (e2, ctype_unknown);
1390 uentry_makeVarFunction (e2);
1391 uentry_setDefined (e2, g_currentloc);
1392 uentry_setFunctionDefined (e2, g_currentloc);
1393 context_enterUnknownMacro (e2);
1397 uentry ucons = uentry_makeConstant (fname,
1400 if (uentry_isExpandedMacro (e2))
1408 message ("Variable %s implemented by a macro",
1412 uentry_showWhereSpecified (e2);
1416 uentry_setDefined (e2, loc);
1417 uentry_setUsed (ucons, loc);
1419 context_enterConstantMacro (ucons);
1420 uentry_markOwned (ucons);
1421 cstring_free (fname);
1427 if (uentry_isDatatype (e2))
1431 message ("Type implemented as macro: %x",
1432 uentry_getName (e2)),
1433 message ("A type is implemented using a macro definition. A "
1434 "typedef should be used instead."),
1438 /* Must exit scope (not sure why a new scope was entered?) */
1439 usymtab_quietExitScope (g_currentloc);
1440 uentry_setDefined (e2, g_currentloc);
1446 (message ("Unexpanded macro not function or constant: %q",
1447 uentry_unparse (e2)));
1448 uentry_setType (e2, ctype_unknown);
1452 uentry_makeVarFunction (e2);
1453 uentry_setDefined (e2, g_currentloc);
1454 uentry_setFunctionDefined (e2, g_currentloc);
1455 context_enterUnknownMacro (e2);
1466 /* evans 2001-09-09 - if it has params, assume a function */
1470 (FLG_MACROMATCHNAME,
1471 message ("Unexpanded macro %s does not match name of a declared "
1472 "function. The name used in the control "
1473 "comment on the previous line should match.",
1477 ce = uentry_makeFunction (fname, ctype_unknown,
1481 warnClause_undefined,
1483 uentry_setUsed (ce, loc); /* perhaps bogus? */
1484 e2 = usymtab_supEntryReturn (ce);
1485 context_enterUnknownMacro (e2);
1490 (FLG_MACROMATCHNAME,
1491 message ("Unexpanded macro %s does not match name of a constant "
1492 "or iter declaration. The name used in the control "
1493 "comment on the previous line should match. "
1494 "(Assuming macro defines a constant.)",
1498 ce = uentry_makeConstant (fname, ctype_unknown, fileloc_undefined);
1499 uentry_setUsed (ce, loc); /* perhaps bogus? */
1500 e2 = usymtab_supEntryReturn (ce);
1502 context_enterConstantMacro (e2);
1503 cstring_free (fname);
1509 /* in macros, ( must follow immediatetly after name */
1515 c = skip_whitespace ();
1517 while (c != ')' && c != '\0')
1520 bool suppress = context_inSuppressRegion ();
1521 cstring paramname = cstring_undefined;
1524 ** save the parameter location
1528 context_saveLocation ();
1531 while (c != ' ' && c != '\t' && c != ',' && c != '\0' && c != ')')
1533 paramname = cstring_appendChar (paramname, c);
1534 c = macro_nextChar ();
1537 if (c == ' ' || c == '\t') c = skip_whitespace ();
1541 c = macro_nextChar ();
1542 if (c == ' ' || c == '\t') c = skip_whitespace ();
1547 llfatalerror (cstring_makeLiteral
1548 ("Bad macro syntax: uentryList"));
1551 if ((isspecfcn || isiter) && (paramno < noparams)
1552 && !uentry_isElipsisMarker (uentryList_getN
1553 (specparams, paramno)))
1555 fileloc sloc = context_getSaveLocation ();
1556 uentry decl = uentryList_getN (specparams, paramno);
1559 param = uentry_nameCopy (paramname, decl);
1561 uentry_setParam (param);
1562 sr = sRef_makeParam (paramno, uentry_getType (param), stateInfo_makeLoc (sloc));
1564 if (sRef_getNullState (sr) == NS_ABSNULL)
1566 ctype pt = ctype_realType (uentry_getType (param));
1568 if (ctype_isUser (pt))
1570 uentry te = usymtab_getTypeEntrySafe (ctype_typeId (pt));
1572 if (uentry_isValid (te))
1574 sRef_setStateFromUentry (sr, te);
1579 sRef_setNullState (sr, NS_UNKNOWN, sloc);
1583 uentry_setSref (param, sr);
1584 uentry_setDeclaredForceOnly (param, sloc);
1586 skipparam = isiter && uentry_isOut (uentryList_getN (specparams, paramno));
1590 fileloc sloc = context_getSaveLocation ();
1592 param = uentry_makeVariableSrefParam
1593 (paramname, ctype_unknown, fileloc_copy (sloc),
1594 sRef_makeParam (paramno, ctype_unknown, stateInfo_makeLoc (sloc)));
1595 DPRINTF (("Unknown param: %s", uentry_unparseFull (param)));
1596 cstring_free (paramname);
1598 sRef_setPosNull (uentry_getSref (param), sloc);
1599 uentry_setDeclaredForce (param, sloc);
1602 fileloc_free (sloc);
1607 llassert (!uentry_isElipsisMarker (param));
1611 sRef_makeUnsafe (uentry_getSref (param));
1614 pn = uentryList_add (pn, uentry_copy (param));
1615 usymtab_supEntry (param);
1619 /* don't add param */
1620 uentry_free (param);
1625 (void) macro_nextChar ();
1626 c = skip_whitespace ();
1634 if (isspecfcn || isiter)
1636 if (paramno != noparams && noparams >= 0)
1642 message ("Macro %s specified with %d args, defined with %d",
1643 fname, noparams, paramno),
1646 uentry_showWhereSpecified (e2);
1647 uentry_resetParams (e2, pn);
1652 uentry_resetParams (e2, pn);
1659 ** the form should be:
1661 ** # define newname oldname
1662 ** where oldname refers to a function matching the specification
1668 sRef_setGlobalScope ();
1669 usymtab_supGlobalEntry (uentry_makeVariableLoc (fname, ctype_unknown));
1670 sRef_clearGlobalScope ();
1674 context_setMacroMissingParams ();
1679 /* context_setuentryList (pn); */
1680 usymtab_enterScope ();
1683 cstring_free (fname);
1688 static bool handleSpecial (char *yyt)
1690 char *l = mstring_create (MAX_NAME_LENGTH);
1691 static bool reportcpp = FALSE;
1697 strcpy (l, yyt + 1);
1699 /* Need to safe original l for deallocating. */
1702 l += strlen (yyt) - 1;
1704 while ((c = char_fromInt (lminput ())) != '\n' && c != '\0')
1710 olc = cstring_fromChars (ol);
1712 if (cstring_equalPrefixLit (olc, "pragma"))
1714 char *pname = mstring_create (longUnsigned_fromInt (MAX_PRAGMA_LEN));
1715 char *opname = pname;
1716 char *ptr = ol + 6; /* pragma is six characters, plus space */
1720 /* skip whitespace */
1721 while (((c = *ptr) != '\0') && isspace (c))
1727 while (((c = *ptr) != '\0') && !isspace (c))
1731 if (len > MAX_PRAGMA_LEN)
1742 if (len == PRAGMA_LEN_EXPAND
1743 && mstring_equal (opname, PRAGMA_EXPAND))
1745 cstring exname = cstring_undefined;
1749 while (((c = *ptr) != '\0') && !isspace (c))
1751 exname = cstring_appendChar (exname, c);
1756 ue = usymtab_lookupExposeGlob (exname);
1758 if (uentry_isExpandedMacro (ue))
1760 if (fileloc_isPreproc (uentry_whereDefined (ue)))
1762 fileloc_setColumn (g_currentloc, 1);
1763 uentry_setDefined (ue, g_currentloc);
1767 cstring_free (exname);
1770 else if (cstring_equalPrefixLit (olc, "ident"))
1772 /* Some pre-processors will leave these in the code. Ignore rest of line */
1775 ** Yuk...Win32 filenames can have spaces in them...we need to read
1776 ** to the matching end quote.
1778 else if ((sscanf (ol, "line %d \"", &lineno) == 1)
1779 || (sscanf (ol, " %d \"", &lineno) == 1))
1785 /*@access cstring@*/
1786 while (*tmp != '\"' && *tmp != '\0')
1791 llassert (*tmp == '\"');
1797 while (*tmp != '\"' && *tmp != '\0')
1802 llassert (*tmp == '\"');
1806 # if defined(OS2) || defined(MSDOS) || defined(WIN32)
1809 ** DOS-like path delimiters get delivered in pairs, something like
1810 ** \"..\\\\file.h\", so we have to make it normal again. We do NOT
1811 ** remove the pre dirs yet as we usually specify tmp paths relative
1812 ** to the current directory, so tmp files would not get found in
1813 ** the hash table. If this method fails we try it again later.
1820 ** Skip past the drive marker.
1823 if (strchr (stmp, ':') != NULL)
1825 stmp = strchr (stmp, ':') + 1;
1828 while ((stmp = strchr (stmp, CONNECTCHAR)) != NULL )
1830 if (*(stmp+1) == CONNECTCHAR)
1832 memmove (stmp, stmp+1, strlen (stmp));
1838 fid = fileTable_lookupBase (context_fileTable (), fname);
1839 if (!(fileId_isValid (fid)))
1841 fname = removePreDirs (fname);
1842 fid = fileTable_lookupBase (context_fileTable (), fname);
1845 # else /* !defined(OS2) && !defined(MSDOS) */
1846 fname = removePreDirs (fname);
1847 fid = fileTable_lookupBase (context_fileTable (), fname);
1848 # endif /* !defined(OS2) && !defined(MSDOS) */
1850 if (!(fileId_isValid (fid)))
1852 if (context_inXHFile ())
1854 fid = fileTable_addXHFile (context_fileTable (), fname);
1856 else if (isHeaderFile (fname))
1858 fid = fileTable_addHeaderFile (context_fileTable (), fname);
1862 fid = fileTable_addFile (context_fileTable (), fname);
1866 setFileLine (fid, lineno);
1867 /*@noaccess cstring@*/
1869 else if ((sscanf (ol, "line %d", &lineno) == 1)
1870 || (sscanf (ol, " %d", &lineno) == 1))
1872 setLine (lineno); /* next line is <cr> */
1876 if (mstring_equal (ol, "")) {
1877 DPRINTF (("Empty pp command!"));
1879 ** evs 2000-05-16: This is a horrible kludge, to get around a bug (well, difficulty) in the pre-processor.
1880 ** We handle a plain # in the input file, by echoing it, and ignoring it in the post-pp-file.
1889 llbug (message ("File contains preprocessor command: #%s",
1890 cstring_fromChars (ol)));
1903 static int handleLlSpecial ()
1908 char *s = mstring_createEmpty ();
1914 loc = fileloc_copy (g_currentloc);
1915 DPRINTF (("Handle special: %s", fileloc_unparse (loc)));
1917 while (((ic = ninput ()) != 0) && isalpha (ic))
1920 s = mstring_append (s, c);
1924 DPRINTF (("Read: %s / %s", s, fileloc_unparse (g_currentloc)));
1927 if (charsread == 0 && ic == (int) AFTER_COMMENT_MARKER[0])
1931 llassert (ic == AFTER_COMMENT_MARKER[1]);
1937 return QNOMODS; /* special token no modifications token */
1941 DPRINTF (("Coment marker: %s", os));
1942 tok = commentMarkerToken (cstring_fromChars (os));
1946 tokLength = charsread;
1953 DPRINTF (("Not a comment marker..."));
1954 /* Add rest of the comment */
1956 if (ic != 0 && ic != EOF)
1960 s = mstring_append (s, c);
1963 while (((ic = ninput ()) != 0) && (ic != EOF)
1964 && (ic != AFTER_COMMENT_MARKER[0]))
1968 /* evans 2001-09-01 added to prevent assertion failures for uncloses syntactic comments */
1971 hasnl = TRUE; /* This prevents tokLength from being set later. */
1976 message ("Likely parse error: syntactic comment token spans multiple lines: %s",
1977 cstring_fromChars (s)),
1981 s = mstring_append (s, c);
1986 DPRINTF (("Read: %s / %s", s, fileloc_unparse (g_currentloc)));
1988 if (ic == AFTER_COMMENT_MARKER[0])
1991 llassert ((char) nc == AFTER_COMMENT_MARKER[1]);
1997 while (*s == ' ' || *s == '\t' || *s == '\n')
2002 if (*s == '-' || *s == '+' || *s == '=') /* setting flags */
2006 while (c == '-' || c == '+' || c == '=')
2008 ynm set = ynm_fromCodeChar (c);
2013 thisflag = cstring_fromChars (s);
2015 while ((c = *s) != '\0' && (c != '-') && (c != '=')
2016 && (c != '+') && (c != ' ') && (c != '\t') && (c != '\n'))
2023 if (!context_getFlag (FLG_NOCOMMENTS))
2025 cstring flagname = thisflag;
2026 flagcode fflag = identifyFlag (flagname);
2028 if (flagcode_isSkip (fflag))
2032 else if (flagcode_isInvalid (fflag))
2034 if (isMode (flagname))
2036 if (ynm_isMaybe (set))
2041 ("Semantic comment attempts to restore flag %s. "
2042 "A mode flag cannot be restored.",
2047 context_setMode (flagname);
2053 (FLG_UNRECOGFLAGCOMMENTS,
2054 message ("Unrecognized option in semantic comment: %s",
2059 else if (flagcode_isGlobalFlag (fflag))
2064 ("Semantic comment attempts to set global flag %s. "
2065 "A global flag cannot be set locally.",
2071 context_fileSetFlag (fflag, set);
2073 if (flagcode_hasArgument (fflag))
2075 if (ynm_isMaybe (set))
2080 ("Semantic comment attempts to restore flag %s. "
2081 "A flag for setting a value cannot be restored.",
2086 { /* cut-and-pastied from llmain...blecch */
2087 cstring extra = cstring_undefined;
2093 rest = mstring_copy (s);
2097 while ((rchar = *rest) != '\0'
2098 && (isspace (rchar)))
2104 while ((rchar = *rest) != '\0'
2105 && !isspace (rchar))
2107 extra = cstring_appendChar (extra, rchar);
2114 if (cstring_isUndefined (extra))
2119 ("Flag %s (in semantic comment) must be followed by an argument",
2120 flagcode_unparse (fflag)));
2126 if (flagcode_hasValue (fflag))
2128 setValueFlag (fflag, extra);
2130 else if (flagcode_hasString (fflag))
2132 setStringFlag (fflag, extra);
2149 while ((c == ' ') || (c == '\t') || (c == '\n'))
2155 if (context_inHeader () && !isArtificial (cstring_fromChars (os)))
2157 DPRINTF (("Here adding comment: %s", os));
2158 context_addComment (cstring_fromCharsNew (os));
2170 annotationInfo ainfo;
2172 while (*s != '\0' && *s != ' ' && *s != '\t' && *s != '\n')
2184 t = cstring_toCharsSafe (cstring_downcase (cstring_fromChars (t)));
2185 macrocode = tokenMacroCode (cstring_fromChars (t));
2187 if (macrocode != BADTOK)
2189 tokLength = hasnl ? 0 : mstring_length (t);
2195 if (macrocode == SKIPTOK)
2203 ainfo = context_lookupAnnotation (cstring_fromChars (os));
2205 if (annotationInfo_isDefined (ainfo)) {
2206 DPRINTF (("Found annotation: %s", annotationInfo_unparse (ainfo)));
2207 /*@i324@*/ yylval.annotation = ainfo;
2215 if (context_inHeader ())
2222 if ((context_inMacro () || context_inGlobalContext ())
2223 && macrocode != SKIPTOK
2224 && !isArtificial (cstring_fromChars (os)))
2226 DPRINTF (("Add comment: %s", os));
2227 context_addComment (cstring_fromCharsNew (os));
2240 if (mstring_equal (t, "ignore"))
2242 if (!context_getFlag (FLG_NOCOMMENTS))
2244 context_enterSuppressRegion ();
2247 else if ((*t == 'i' || *t == 't')
2248 && (*(t + 1) == '\0'))
2250 if (!context_getFlag (FLG_NOCOMMENTS)
2251 && (*t == 'i' || context_getFlag (FLG_TMPCOMMENTS)))
2253 context_enterSuppressLine (-1); /* infinite suppression */
2256 else if (((*t == 'i') || (*t == 't'))
2257 && ((*(t + 1) >= '0' && *(t + 1) <= '9')))
2259 bool tmpcomment = (*t == 't');
2261 char *tt = t; /* don't mangle t, since it is free'd */
2264 if (lc >= '0' && lc <= '9')
2266 val = (int)(lc - '0');
2269 while (lc >= '0' && lc <= '9')
2278 if (!context_getFlag (FLG_NOCOMMENTS)
2279 && (!tmpcomment || context_getFlag (FLG_TMPCOMMENTS)))
2281 context_enterSuppressLine (val);
2284 else if (mstring_equal (t, "end"))
2286 if (!context_getFlag (FLG_NOCOMMENTS))
2288 context_exitSuppressRegion ();
2291 else if (mstring_equal (t, "notfunction"))
2293 ; /* handled by pcpp */
2295 else if (mstring_equal (t, "access"))
2301 while ((c = *s) && (c == ' ' || c == '\t' || c == '\n'))
2311 tname = cstring_fromChars (s);
2313 while ((c = *s) != '\0' && c != ' '
2314 && c != '\t' && c != '\n' && c != ',')
2321 DPRINTF (("Access %s", tname));
2323 if (!context_getFlag (FLG_NOCOMMENTS)
2324 && !context_getFlag (FLG_NOACCESS))
2326 if (usymtab_existsType (tname))
2328 typeId uid = usymtab_getTypeId (tname);
2329 uentry ue = usymtab_getTypeEntry (uid);
2331 if (uentry_isAbstractDatatype (ue))
2333 context_addFileAccessType (uid);
2334 DPRINTF (("Adding access to: %s / %d", tname, uid));
2341 ("Non-abstract type %s used in access comment",
2348 if (!(context_inSuppressRegion ()
2349 || context_inSuppressZone (g_currentloc)))
2354 ("Unrecognized type %s used in access comment",
2366 if (c != ',' && c != ' ')
2372 else if (mstring_equal (t, "noaccess"))
2379 while ((lc = *s) && (lc == ' ' || lc == '\t' || lc == '\n'))
2389 tname = cstring_fromChars (s);
2391 while ((lc = *s) != '\0' && lc != ' ' && lc != '\t'
2392 && lc != '\n' && lc != ',')
2399 if (!context_getFlag (FLG_NOCOMMENTS)
2400 && !context_getFlag (FLG_NOACCESS))
2402 if (usymtab_existsType (tname))
2404 typeId tuid = usymtab_getTypeId (tname);
2406 if (context_couldHaveAccess (tuid))
2408 DPRINTF (("Removing access: %s", tname));
2409 context_removeFileAccessType (tuid);
2413 if (!(context_inSuppressRegion ()
2414 || context_inSuppressZone (g_currentloc)))
2416 uentry ue = usymtab_getTypeEntry (tuid);
2418 if (uentry_isAbstractDatatype (ue))
2423 ("Non-accessible abstract type %s used in noaccess comment",
2432 ("Non-abstract type %s used in noaccess comment",
2441 if (!(context_inSuppressRegion ()
2442 || context_inSuppressZone (g_currentloc)))
2447 ("Unrecognized type %s used in noaccess comment",
2459 if (lc != ',' && lc != ' ')
2467 voptgenerror (FLG_UNRECOGCOMMENTS,
2468 message ("Semantic comment unrecognized: %s",
2469 cstring_fromChars (os)), loc);
2480 static /*@only@*/ cstring makeIdentifier (char *s)
2482 char *c = mstring_create (size_toInt (strlen (s)) + 1);
2483 cstring id = cstring_fromChars (c);
2485 while (isalnum (*s) || (*s == '_') || (*s == '$'))
2494 /*@observer@*/ /*@dependent@*/ uentry coerceId (cstring cn)
2496 if (!(usymtab_exists (cn)))
2498 fileloc loc = fileloc_createExternal ();
2501 ** We need to put this in a global scope, otherwise the sRef will be deallocated.
2504 uentry ce = uentry_makeUnrecognized (cn, loc);
2506 if (!context_inIterEnd ())
2510 message ("Unrecognized (possibly system) identifier: %q",
2511 uentry_getName (ce)),
2518 return (usymtab_lookup (cn));
2522 ** like, coerceId, but doesn't supercede for iters
2525 /*@observer@*/ uentry coerceIterId (cstring cn)
2527 if (!(usymtab_exists (cn)))
2529 return uentry_undefined;
2532 return (usymtab_lookup (cn));
2535 /*@observer@*/ cstring LastIdentifier ()
2537 return (lastidprocessed);
2540 static int processIdentifier (cstring id)
2544 if (context_getFlag (FLG_GRAMMAR))
2546 lldiagmsg (message ("Process identifier: %s", id));
2549 context_clearJustPopped ();
2550 lastidprocessed = id;
2552 if (context_inFunctionHeader ())
2554 int tok = commentMarkerToken (id);
2555 DPRINTF (("in function decl..."));
2563 tok = tokenMacroCode (id);
2571 annotationInfo ainfo;
2573 if (expectingMetaStateName)
2575 metaStateInfo msinfo = context_lookupMetaStateInfo (id);
2577 if (metaStateInfo_isDefined (msinfo))
2579 yylval.msinfo = msinfo;
2580 return METASTATE_NAME;
2584 DPRINTF (("Not meta state name: %s", cstring_toCharsSafe (id)));
2588 ainfo = context_lookupAnnotation (id);
2590 if (annotationInfo_isDefined (ainfo))
2592 DPRINTF (("Found annotation: %s", annotationInfo_unparse (ainfo)));
2593 /*@i324@*/ yylval.annotation = ainfo;
2598 DPRINTF (("Not annotation: %s", id));
2604 /* Consider handling: Defined by C99 as static const char __func__[] */
2606 if (context_getFlag (FLG_GNUEXTENSIONS))
2610 if (cstring_equalLit (id, "__stdcall")
2611 || cstring_equalLit (id, "__cdecl")
2612 || cstring_equalLit (id, "__extension__"))
2616 else if (cstring_equalLit (id, "__volatile__"))
2620 else if (cstring_equalLit (id, "__signed"))
2624 else if (cstring_equalLit (id, "__unsigned"))
2628 else if (cstring_equalLit (id, "__const__"))
2632 else if (cstring_equalLit (id, "__alignof__"))
2634 tok = CALIGNOF; /* alignof is parsed like sizeof */
2636 else if (cstring_equalLit (id, "__FUNCTION__")
2637 || cstring_equalLit (id, "__PRETTY_FUNCTION__"))
2639 /* These tokens hold the name of the current function as strings */
2640 yylval.expr = exprNode_stringLiteral (id, fileloc_copy (g_currentloc));
2642 lastWasString = TRUE;
2646 else if (cstring_equalLit (id, "__attribute__")
2647 || cstring_equalLit (id, "__asm__")
2648 || cstring_equalLit (id, "_asm")
2649 || cstring_equalLit (id, "__asm")
2650 || cstring_equalLit (id, "__declspec"))
2653 bool useparens = FALSE;
2654 bool usebraces = FALSE;
2655 bool inquote = FALSE;
2656 bool inescape = FALSE;
2659 while ((ic = input ()) != EOF)
2666 else if (ic == '\\')
2670 else if (ic == '\"')
2706 else if (ic == ')' && useparens)
2709 if (depth == 0) break;
2711 else if (ic == '}' && usebraces)
2714 if (depth == 0) break;
2717 && !usebraces && !useparens
2718 && cstring_equalLit (id, "__asm"))
2721 ** We need this because some MS VC++ include files
2722 ** have __asm mov ... }
2723 ** Its a kludge, but otherwise would need to parse
2732 context_incLineno ();
2734 if (cstring_equalLit (id, "__asm")
2735 && !useparens && !usebraces)
2742 llassert ((useparens && ic == ')')
2743 || (usebraces && ic == '}')
2744 || (!useparens && !usebraces));
2748 else if (cstring_equalLit (id, "inline")
2749 || cstring_equalLit (id, "__inline")
2750 || cstring_equalLit (id, "_inline")
2751 || cstring_equalLit (id, "__inline__"))
2762 le = usymtab_lookupSafe (id);
2764 /*@-dependenttrans@*/
2766 if (uentry_isIter (le))
2768 /*@i32@*/ yylval.entry = le;
2771 else if (uentry_isEndIter (le))
2773 /*@i32@*/ yylval.entry = le;
2774 return (ITER_ENDNAME);
2776 else if (uentry_isUndefined (le))
2780 /* avoid parse errors for certain system built ins */
2782 if (g_expectingTypeName && (cstring_firstChar (id) == '_')
2783 && (cstring_secondChar (id) == '_'))
2785 return (TYPE_NAME_OR_ID);
2788 return (NEW_IDENTIFIER);
2790 else if (!uentry_isDeclared (le) && !uentry_isCodeDefined (le))
2792 if (uentry_isDatatype (le))
2795 return (NEW_IDENTIFIER);
2799 /*@i32@*/ yylval.entry = le;
2800 return (IDENTIFIER);
2803 else if (uentry_isDatatype (le))
2805 if (!g_expectingTypeName)
2809 return (NEW_IDENTIFIER);
2813 yylval.ctyp = uentry_getAbstractType (le);
2815 uentry_setUsed (le, g_currentloc);
2821 /*@i32@*/ yylval.entry = le;
2822 return (IDENTIFIER);
2825 /*@=dependenttrans@*/
2828 static bool processHashIdentifier (/*@only@*/ cstring id)
2830 if (context_inMacro () || context_inIterDef () ||
2831 context_inIterEnd ())
2835 context_clearJustPopped ();
2837 lastidprocessed = id;
2838 le = usymtab_lookupSafe (id);
2840 if (uentry_isParam (le) || uentry_isRefParam (le))
2857 static /*@only@*/ exprNode processString ()
2861 char *nl = strchr (yytext, '\n');
2862 cstring ns = cstring_fromCharsNew (yytext);
2866 loc = fileloc_copy (g_currentloc);
2867 addColumn (cstring_length (ns));
2873 loc = fileloc_copy (g_currentloc);
2875 context_incLineno ();
2877 while ((nl = strchr ((nl + 1), '\n')) != NULL)
2879 context_incLineno ();
2885 res = exprNode_stringLiteral (ns, loc);
2895 llassert (*yytext != '\0');
2896 fchar = *(yytext + 1);
2897 if (fchar != '\\') return fchar;
2899 next = *(yytext + 2);
2903 case 'n': return '\n';
2904 case 't': return '\t';
2905 case '\"': return '\"';
2906 case '\'': return '\'';
2907 case '\\': return '\\';
2908 default: return '\0';
2913 double processFloat ()
2915 double ret = atof (yytext);
2926 llassert (yytext[0] == '0'
2927 && (yytext[1] == 'X' || yytext[1] == 'x'));
2929 while (yytext[index] != '\0') {
2931 char c = yytext[index];
2933 if (c >= '0' && c <= '9') {
2934 tval = (int) c - (int) '0';
2935 } else if (c >= 'A' && c <= 'F') {
2936 tval = (int) c - (int) 'A' + 10;
2937 } else if (c >= 'a' && c <= 'f') {
2938 tval = (int) c - (int) 'a' + 10;
2939 } else if (c == 'U' || c == 'L' || c == 'u' || c == 'l') {
2941 while (yytext[index] != '\0') {
2942 if (c == 'U' || c == 'L' || c == 'u' || c == 'l') {
2947 message ("Invalid character (%c) following specifier in hex constant: %s",
2948 c, cstring_fromChars (yytext)),
2958 message ("Invalid character (%c) in hex constant: %s",
2959 c, cstring_fromChars (yytext)),
2964 val = (val * 16) + tval;
2968 DPRINTF (("Hex constant: %s = %ld", yytext, val));
2973 long processOctal ()
2978 llassert (yytext[0] == '0' && yytext[1] != 'X' && yytext[1] != 'x');
2980 while (yytext[index] != '\0') {
2982 char c = yytext[index];
2984 if (c >= '0' && c <= '7') {
2985 tval = (int) c - (int) '0';
2989 message ("Invalid character (%c) in octal constant: %s",
2990 c, cstring_fromChars (yytext)),
2995 val = (val * 8) + tval;
2999 DPRINTF (("Octal constant: %s = %ld", yytext, val));
3006 return (atol (yytext));
3010 processSpec (int tok)
3012 size_t length = strlen (yytext);
3016 setTokLengthT (length);
3022 context_saveLocation ();
3023 setTokLengthT (length);
3024 return (processIdentifier (makeIdentifier (yytext)));
3028 void cscanner_expectingMetaStateName ()
3030 llassert (!expectingMetaStateName);
3031 llassert (context_inFunctionHeader ());
3032 expectingMetaStateName = TRUE;
3035 void cscanner_clearExpectingMetaStateName ()
3037 llassert (expectingMetaStateName);
3038 expectingMetaStateName = FALSE;