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 DPRINTF (("Here we are: %s", context_unparse ()));
509 if (context_inIterDef ())
511 RETURN_TOK (LLMACROITER);
513 if (context_inIterEnd ())
515 RETURN_TOK (LLMACROEND);
517 if (context_inMacro ())
519 RETURN_TOK (LLMACRO);
524 "@.CT" { setTokLength (4); lldiagmsg (ctype_unparseTable ()); }
525 "@.FA" { setTokLength (4); lldiagmsg (message ("Access types: %q", typeIdSet_unparse (context_fileAccessTypes ()))); }
526 "@.F" { setTokLength (3);
527 lldiagmsg (message ("%q: *** marker ***", fileloc_unparse (g_currentloc)));
529 "@.L" { setTokLength (3); usymtab_printLocal (); }
530 "@.A" { setTokLength (3); lldiagmsg (usymtab_unparseAliases ()); }
531 "@.C" { setTokLength (3); lldiagmsg (context_unparse ()); }
532 "@.W" { setTokLength (3); lldiagmsg (context_unparseClauses ()); }
533 "@.G" { setTokLength (3); usymtab_printGuards (); }
534 "@.S" { setTokLength (3); usymtab_printOut (); }
535 "@.X" { setTokLength (3); usymtab_printAll (); }
536 "@.Z" { setTokLength (3); usymtab_printComplete (); }
537 "@.T" { setTokLength (3); usymtab_printTypes (); }
538 "@.K" { setTokLength (3); lldiagmsg (usymtab_unparseStack ()); }
539 "@.M" { setTokLength (3);
540 lldiagmsg (message ("Can modify: %q",
541 sRefSet_unparse (context_modList ())));
543 "%{" { /* BEFORE_COMMENT_MARKER */
545 incColumn (); incColumn ();
546 tok = handleLlSpecial ();
550 if (tok == CANNOTATION) {
553 /* Beware - this bashes yylval! */
558 "%}" { /* AFTER_COMMENT_MARKER */
561 RETURN_TOK (QENDMACRO); }
562 "\\" { incColumn (); continueLine = TRUE; }
564 if ((int) *yytext == 13 ) {
569 message ("Invalid character (ascii: %d), skipping character",
578 /*@null@*/ /*@observer@*/ char *name;
583 ** These tokens are followed by syntax that is parsed by the
587 struct skeyword s_parsetable[] = {
588 { "modifies", QMODIFIES } ,
589 { "globals", QGLOBALS } ,
592 { "constant", QCONSTANT } ,
593 { "function", QFUNCTION } ,
595 { "defines", QDEFINES } ,
597 { "allocates", QALLOCATES } ,
599 { "releases", QRELEASES } ,
600 { "pre", QPRECLAUSE } ,
601 { "post", QPOSTCLAUSE } ,
602 { "setBufferSize", QSETBUFFERSIZE},
603 { "setStringLength", QSETSTRINGLENGTH},
604 { "testinRange", QTESTINRANGE},
605 { "requires", QPRECLAUSE } ,
606 { "ensures", QPOSTCLAUSE } ,
611 ** These tokens are either stand-alone tokens, or followed by
612 ** token-specific text.
615 struct skeyword s_keytable[] = {
616 { "anytype", QANYTYPE } ,
617 { "integraltype", QINTEGRALTYPE } ,
618 { "unsignedintegraltype", QUNSIGNEDINTEGRALTYPE } ,
619 { "signedintegraltype", QSIGNEDINTEGRALTYPE } ,
623 { "owned", QOWNED } ,
624 { "dependent", QDEPENDENT } ,
625 { "partial", QPARTIAL } ,
626 { "special", QSPECIAL } ,
627 { "truenull", QTRUENULL } ,
628 { "falsenull", QFALSENULL } ,
631 { "notnull", QNOTNULL } ,
632 { "abstract", QABSTRACT } ,
633 { "concrete", QCONCRETE } ,
634 { "mutable", QMUTABLE } ,
635 { "immutable", QIMMUTABLE } ,
636 { "unused", QUNUSED } ,
637 { "external", QEXTERNAL } ,
639 { "unique", QUNIQUE } ,
640 { "returned", QRETURNED } ,
641 { "exposed", QEXPOSED } ,
642 { "refcounted", QREFCOUNTED } ,
644 { "newref", QNEWREF } ,
645 { "tempref", QTEMPREF } ,
646 { "killref", QKILLREF } ,
648 { "relnull", QRELNULL } ,
649 { "nullterminated", QNULLTERMINATED },
650 { "setBufferSize", QSETBUFFERSIZE },
651 { "testInRange", QTESTINRANGE},
652 { "MaxSet", QMAXSET},
653 { "MaxRead", QMAXREAD},
654 { "reldef", QRELDEF } ,
655 { "observer", QOBSERVER } ,
656 { "exits", QEXITS } ,
657 { "mayexit", QMAYEXIT } ,
658 { "trueexit", QTRUEEXIT } ,
659 { "falseexit", QFALSEEXIT } ,
660 { "neverexit", QNEVEREXIT } ,
662 { "shared", QSHARED } ,
664 { "unchecked", QUNCHECKED } ,
665 { "checked", QCHECKED } ,
666 { "checkmod", QCHECKMOD } ,
667 { "checkedstrict", QCHECKEDSTRICT } ,
668 { "innercontinue", QINNERCONTINUE } ,
669 { "innerbreak", QINNERBREAK } ,
670 { "loopbreak", QLOOPBREAK } ,
671 { "switchbreak", QSWITCHBREAK } ,
672 { "safebreak", QSAFEBREAK } ,
673 { "fallthrough", QFALLTHROUGH } ,
674 { "l_fallthrou", QLINTFALLTHROUGH } ,
675 { "l_fallth", QLINTFALLTHRU } ,
676 { "notreached", QNOTREACHED } ,
677 { "l_notreach", QLINTNOTREACHED } ,
678 { "printflike", QPRINTFLIKE } ,
679 { "l_printfli", QLINTPRINTFLIKE } ,
680 { "scanflike", QSCANFLIKE } ,
681 { "messagelike", QMESSAGELIKE } ,
682 { "l_argsus", QARGSUSED } ,
687 ** would be better if these weren't hard coded...
690 static bool isArtificial (cstring s)
692 return (cstring_equalLit (s, "modifies")
693 || cstring_equalLit (s, "globals")
694 || cstring_equalLit (s, "warn")
695 || cstring_equalLit (s, "alt"));
698 void swallowMacro (void)
701 bool skipnext = FALSE;
703 while ((i = lminput ()) != EOF)
720 reader_checkUngetc (i, yyin);
728 reader_checkUngetc (i, yyin);
732 static int commentMarkerToken (cstring s)
736 while (s_parsetable[i].name != NULL)
738 DPRINTF (("Try :%s:%s:", s, s_parsetable[i].name));
740 if (cstring_equalLit (s, s_parsetable[i].name))
742 return s_parsetable[i].token;
751 static int tokenMacroCode (cstring s)
755 while (s_keytable[i].name != NULL)
757 if (cstring_equalLit (s, s_keytable[i].name))
759 if (s_keytable[i].token == QLINTFALLTHROUGH)
762 (FLG_WARNLINTCOMMENTS,
764 ("Traditional lint comment /*FALLTHROUGH*/ used. "
765 "This is interpreted by "
766 "LCLint in the same way as most Unix lints, but it is "
767 "preferable to replace it with the /*@fallthrough@*/ "
772 else if (s_keytable[i].token == QLINTFALLTHRU)
775 (FLG_WARNLINTCOMMENTS,
777 ("Traditional lint comment /*FALLTHRU*/ used. "
778 "This is interpreted by "
779 "LCLint in the same way as most Unix lints, but it is "
780 "preferable to replace it with the /*@fallthrough@*/ "
785 else if (s_keytable[i].token == QLINTNOTREACHED)
788 (FLG_WARNLINTCOMMENTS,
790 ("Traditional lint comment /*NOTREACHED*/ used. "
791 "This is interpreted by "
792 "LCLint in the same way as most Unix lints, but it is "
793 "preferable to replace it with the /*@notreached@*/ "
794 "semantic comment."),
799 else if (s_keytable[i].token == QPRINTFLIKE)
801 setSpecialFunction (qual_createPrintfLike ());
804 else if (s_keytable[i].token == QLINTPRINTFLIKE)
807 (FLG_WARNLINTCOMMENTS,
809 ("Traditional lint comment /*PRINTFLIKE*/ used. "
810 "This is interpreted by "
811 "LCLint in the same way as most Unix lints, but it is "
812 "preferable to replace it with either /*@printflike@*/, "
813 "/*@scanflike@*/ or /*@messagelike@*/."),
816 setSpecialFunction (qual_createPrintfLike ());
819 else if (s_keytable[i].token == QSCANFLIKE)
821 setSpecialFunction (qual_createScanfLike ());
824 else if (s_keytable[i].token == QMESSAGELIKE)
826 setSpecialFunction (qual_createMessageLike ());
829 else if (s_keytable[i].token == QARGSUSED)
832 (FLG_WARNLINTCOMMENTS,
834 ("Traditional lint comment /*ARGSUSED*/ used. "
835 "This is interpreted by "
836 "LCLint in the same way as most Unix lints, but it is "
837 "preferable to use /*@unused@*/ annotations on "
838 "the unused parameters."),
845 return s_keytable[i].token;
854 static int lminput ()
856 if (savechar == '\0')
863 int save = (int) savechar;
869 static void lmsavechar (char c)
871 if (savechar == '\0') savechar = c;
874 llbuglit ("lmsavechar: override");
878 static int returnFloat (ctype ct, double f)
880 yylval.expr = exprNode_floatLiteral (f, ct, cstring_fromChars (yytext),
881 fileloc_decColumn (g_currentloc, tokLength));
886 static int returnInt (ctype ct, long i)
890 if (ctype_equal (ct, ctype_int))
894 c = context_typeofZero ();
898 c = context_typeofOne ();
902 yylval.expr = exprNode_numLiteral (c, cstring_fromChars (yytext),
903 fileloc_decColumn (g_currentloc, tokLength), i);
908 static int returnChar (char c)
910 yylval.expr = exprNode_charLiteral (c, cstring_fromChars (yytext),
911 fileloc_decColumn (g_currentloc, tokLength));
920 if (c != EOF && ((char)c == '\n'))
922 context_incLineno ();
928 static char macro_nextChar ()
930 static bool in_quote = FALSE, in_escape = FALSE, in_char = FALSE;
935 c = char_fromInt (ic);
937 if (!in_quote && !in_char && (c == '\\' || c == BEFORE_COMMENT_MARKER[0]))
941 while ((c = char_fromInt (lminput ())) != '\0' && c != '\n')
943 ; /* skip to newline */
946 context_incLineno ();
950 return macro_nextChar ();
957 else /* if (c == '@') */
959 llassert (FALSE); /*@i23@*/
960 if (handleLlSpecial () != BADTOK)
962 llerrorlit (FLG_SYNTAX, "Macro cannot use special syntax");
965 return macro_nextChar ();
968 else if (!in_escape && c == '\"')
970 in_quote = !in_quote;
972 else if (!in_escape && c == '\'')
976 else if ((in_quote || in_char) && c == '\\')
978 in_escape = !in_escape;
980 else if ((in_quote || in_char) && in_escape)
984 else if (!in_quote && c == '/')
988 if ((c2 = char_fromInt (lminput ())) == '*')
992 while ((c2 = char_fromInt (lminput ())) != '\0'
993 && c2 != '\n' && c2 != '*')
1000 while ((c2 = char_fromInt (lminput ())) != '\0'
1013 llfatalerror (cstring_makeLiteral ("Macro: bad comment!"));
1017 return macro_nextChar ();
1021 /*** putchar does not work! why? puts to stdio...??! ***/
1029 ** keeps semantic comments
1032 static char macro_nextCharC ()
1034 static bool in_quote = FALSE, in_escape = FALSE, in_char = FALSE;
1037 c = char_fromInt (lminput ());
1039 if (!in_quote && !in_char && c == '\\')
1041 while ((c = char_fromInt (lminput ())) != '\0' && c != '\n')
1043 ; /* skip to newline */
1046 context_incLineno ();
1050 return macro_nextCharC ();
1057 else if (!in_escape && c == '\"')
1059 in_quote = !in_quote;
1061 else if (!in_escape && c == '\'')
1065 else if ((in_quote || in_char) && c == '\\')
1067 in_escape = !in_escape;
1069 else if ((in_quote || in_char) && in_escape)
1073 else if (!in_quote && c == '/')
1077 if ((c2 = char_fromInt (lminput ())) == '*')
1081 while ((c2 = char_fromInt (lminput ())) != '\0'
1082 && c2 != '\n' && c2 != '*')
1089 while ((c2 = char_fromInt (lminput ())) != '\0'
1102 llfatalerror (cstring_makeLiteral ("Macro: bad comment!"));
1106 return macro_nextCharC ();
1117 ** skips whitespace (handles line continuations)
1118 ** returns first non-whitespace character
1121 static char skip_whitespace ()
1125 while ((c = macro_nextChar ()) == ' ' || c == '\t')
1133 static void handleMacro ()
1135 cstring mac = cstring_undefined;
1139 while (currentColumn () > 2)
1141 mac = cstring_appendChar (mac, ' ');
1145 c = macro_nextCharC ();
1147 if (c >= '0' && c <= '9')
1151 for (i = 0; i < ((c - '0') + 1); i++)
1153 mac = cstring_appendChar (mac, ' ');
1161 while (((c = macro_nextCharC ()) != '\0') && (c != '\n'))
1163 mac = cstring_appendChar (mac, c);
1167 macrocode = tokenMacroCode (mac);
1169 if (macrocode == BADTOK && !isArtificial (mac))
1171 context_addMacroCache (mac);
1180 context_incLineno ();
1184 static bool processMacro (void)
1189 cstring fname = cstring_undefined;
1191 bool isspecfcn = FALSE;
1192 bool isiter = FALSE;
1193 bool skipparam = FALSE;
1194 bool isenditer = FALSE;
1195 bool unknownm = FALSE;
1196 bool hasParams = FALSE;
1197 bool emptyMacro = FALSE;
1198 char c = skip_whitespace ();
1199 fileloc loc = fileloc_noColumn (g_currentloc);
1201 /* are both of these necessary? what do they mean? */
1202 uentryList specparams = uentryList_undefined;
1203 uentryList pn = uentryList_undefined;
1205 context_resetMacroMissingParams ();
1207 if (c == '\0' || c == '\n')
1209 llcontbug (cstring_makeLiteral ("Bad macro"));
1214 fname = cstring_appendChar (fname, c);
1216 while ((c = macro_nextChar ()) != '(' && c != '\0'
1217 && c != ' ' && c != '\t' && c != '\n')
1219 fname = cstring_appendChar (fname, c);
1222 if (c == ' ' || c == '\t' || c == '\n')
1228 while (c == ' ' || c == '\t')
1230 c = macro_nextChar ();
1244 hasParams = (c == '(');
1246 if (usymtab_exists (fname))
1248 e2 = usymtab_lookupExpose (fname);
1249 ct = uentry_getType (e2);
1251 if (uentry_isCodeDefined (e2)
1252 && fileloc_isUser (uentry_whereDefined (e2)))
1256 message ("Macro %s already defined", fname),
1259 uentry_showWhereDefined (e2);
1260 uentry_clearDefined (e2);
1263 if (uentry_isFunction (e2))
1265 uentry_setType (e2, ctype_unknown);
1268 context_enterUnknownMacro (e2);
1272 context_enterConstantMacro (e2);
1277 if (uentry_isForward (e2) && uentry_isFunction (e2))
1284 ("Parameterized macro has no prototype or specification: %s ",
1289 uentry_setType (e2, ctype_unknown);
1290 uentry_setFunctionDefined (e2, loc);
1291 uentry_setUsed (e2, fileloc_undefined);
1292 context_enterUnknownMacro (e2);
1296 if (uentry_isIter (e2))
1299 specparams = uentry_getParams (e2);
1300 noparams = uentryList_size (specparams);
1301 uentry_setDefined (e2, loc);
1302 context_enterIterDef (e2);
1304 else if (uentry_isEndIter (e2))
1307 uentry_setDefined (e2, loc);
1308 context_enterIterEnd (e2); /* don't care about it now */
1309 /* but should parse like an iter! */
1311 else if (uentry_isConstant (e2))
1317 message ("Constant %s implemented as parameterized macro",
1321 uentry_showWhereSpecified (e2);
1322 uentry_setType (e2, ctype_unknown);
1323 uentry_makeConstantFunction (e2);
1324 uentry_setDefined (e2, g_currentloc);
1325 uentry_setFunctionDefined (e2, g_currentloc);
1326 context_enterUnknownMacro (e2);
1330 if (!uentry_isSpecified (e2))
1332 fileloc oloc = uentry_whereDeclared (e2);
1334 if (fileloc_isLib (oloc))
1338 else if (fileloc_isUndefined (oloc)
1339 || fileloc_isPreproc (oloc))
1344 (FLG_MACROCONSTDECL,
1346 ("Macro constant %q not declared",
1347 uentry_getName (e2)),
1351 else if (!fileloc_withinLines (oloc, loc, 2))
1352 { /* bogus! will give errors if there is too much whitespace */
1356 ("Macro constant name %s does not match name in "
1357 "previous constant declaration. This constant "
1358 "is declared at %q", fname,
1359 fileloc_unparse (oloc)),
1364 context_enterConstantMacro (e2);
1365 cstring_free (fname);
1371 else if (ctype_isFunction (ct))
1374 specparams = ctype_argsFunction (ct);
1375 noparams = uentryList_size (specparams);
1377 uentry_setFunctionDefined (e2, loc);
1378 context_enterMacro (e2);
1380 else if (uentry_isVar (e2))
1386 message ("Variable %s implemented as parameterized macro",
1390 uentry_showWhereSpecified (e2);
1391 uentry_setType (e2, ctype_unknown);
1392 uentry_makeVarFunction (e2);
1393 uentry_setDefined (e2, g_currentloc);
1394 uentry_setFunctionDefined (e2, g_currentloc);
1395 context_enterUnknownMacro (e2);
1399 uentry ucons = uentry_makeConstant (fname,
1402 if (uentry_isExpandedMacro (e2))
1410 message ("Variable %s implemented by a macro",
1414 uentry_showWhereSpecified (e2);
1418 uentry_setDefined (e2, loc);
1419 uentry_setUsed (ucons, loc);
1421 context_enterConstantMacro (ucons);
1422 uentry_markOwned (ucons);
1423 cstring_free (fname);
1429 if (uentry_isDatatype (e2))
1433 message ("Type implemented as macro: %x",
1434 uentry_getName (e2)),
1435 message ("A type is implemented using a macro definition. A "
1436 "typedef should be used instead."),
1440 /* Must exit scope (not sure why a new scope was entered?) */
1441 usymtab_quietExitScope (g_currentloc);
1442 uentry_setDefined (e2, g_currentloc);
1448 (message ("Unexpanded macro not function or constant: %q",
1449 uentry_unparse (e2)));
1450 uentry_setType (e2, ctype_unknown);
1454 uentry_makeVarFunction (e2);
1455 uentry_setDefined (e2, g_currentloc);
1456 uentry_setFunctionDefined (e2, g_currentloc);
1457 context_enterUnknownMacro (e2);
1468 /* evans 2001-09-09 - if it has params, assume a function */
1472 (FLG_MACROMATCHNAME,
1473 message ("Unexpanded macro %s does not match name of a declared "
1474 "function. The name used in the control "
1475 "comment on the previous line should match.",
1479 ce = uentry_makeFunction (fname, ctype_unknown,
1483 warnClause_undefined,
1485 uentry_setUsed (ce, loc); /* perhaps bogus? */
1486 e2 = usymtab_supEntryReturn (ce);
1487 context_enterUnknownMacro (e2);
1492 (FLG_MACROMATCHNAME,
1493 message ("Unexpanded macro %s does not match name of a constant "
1494 "or iter declaration. The name used in the control "
1495 "comment on the previous line should match. "
1496 "(Assuming macro defines a constant.)",
1500 ce = uentry_makeConstant (fname, ctype_unknown, fileloc_undefined);
1501 uentry_setUsed (ce, loc); /* perhaps bogus? */
1502 e2 = usymtab_supEntryReturn (ce);
1504 context_enterConstantMacro (e2);
1505 cstring_free (fname);
1511 /* in macros, ( must follow immediatetly after name */
1517 c = skip_whitespace ();
1519 while (c != ')' && c != '\0')
1522 bool suppress = context_inSuppressRegion ();
1523 cstring paramname = cstring_undefined;
1526 ** save the parameter location
1530 context_saveLocation ();
1533 while (c != ' ' && c != '\t' && c != ',' && c != '\0' && c != ')')
1535 paramname = cstring_appendChar (paramname, c);
1536 c = macro_nextChar ();
1539 if (c == ' ' || c == '\t') c = skip_whitespace ();
1543 c = macro_nextChar ();
1544 if (c == ' ' || c == '\t') c = skip_whitespace ();
1549 llfatalerror (cstring_makeLiteral
1550 ("Bad macro syntax: uentryList"));
1553 if ((isspecfcn || isiter) && (paramno < noparams)
1554 && !uentry_isElipsisMarker (uentryList_getN
1555 (specparams, paramno)))
1557 fileloc sloc = context_getSaveLocation ();
1558 uentry decl = uentryList_getN (specparams, paramno);
1561 param = uentry_nameCopy (paramname, decl);
1563 uentry_setParam (param);
1564 sr = sRef_makeParam (paramno, uentry_getType (param), stateInfo_makeLoc (sloc));
1566 if (sRef_getNullState (sr) == NS_ABSNULL)
1568 ctype pt = ctype_realType (uentry_getType (param));
1570 if (ctype_isUser (pt))
1572 uentry te = usymtab_getTypeEntrySafe (ctype_typeId (pt));
1574 if (uentry_isValid (te))
1576 sRef_setStateFromUentry (sr, te);
1581 sRef_setNullState (sr, NS_UNKNOWN, sloc);
1585 uentry_setSref (param, sr);
1586 uentry_setDeclaredForceOnly (param, sloc);
1588 skipparam = isiter && uentry_isOut (uentryList_getN (specparams, paramno));
1592 fileloc sloc = context_getSaveLocation ();
1594 param = uentry_makeVariableSrefParam
1595 (paramname, ctype_unknown, fileloc_copy (sloc),
1596 sRef_makeParam (paramno, ctype_unknown, stateInfo_makeLoc (sloc)));
1597 DPRINTF (("Unknown param: %s", uentry_unparseFull (param)));
1598 cstring_free (paramname);
1600 sRef_setPosNull (uentry_getSref (param), sloc);
1601 uentry_setDeclaredForce (param, sloc);
1604 fileloc_free (sloc);
1609 llassert (!uentry_isElipsisMarker (param));
1613 sRef_makeUnsafe (uentry_getSref (param));
1616 pn = uentryList_add (pn, uentry_copy (param));
1617 usymtab_supEntry (param);
1621 /* don't add param */
1622 uentry_free (param);
1627 (void) macro_nextChar ();
1628 c = skip_whitespace ();
1636 if (isspecfcn || isiter)
1638 if (paramno != noparams && noparams >= 0)
1644 message ("Macro %s specified with %d args, defined with %d",
1645 fname, noparams, paramno),
1648 uentry_showWhereSpecified (e2);
1649 uentry_resetParams (e2, pn);
1654 uentry_resetParams (e2, pn);
1661 ** the form should be:
1663 ** # define newname oldname
1664 ** where oldname refers to a function matching the specification
1670 sRef_setGlobalScope ();
1671 usymtab_supGlobalEntry (uentry_makeVariableLoc (fname, ctype_unknown));
1672 sRef_clearGlobalScope ();
1676 context_setMacroMissingParams ();
1681 /* context_setuentryList (pn); */
1682 usymtab_enterScope ();
1685 cstring_free (fname);
1690 static bool handleSpecial (char *yyt)
1692 char *l = mstring_create (MAX_NAME_LENGTH);
1693 static bool reportcpp = FALSE;
1699 strcpy (l, yyt + 1);
1701 /* Need to safe original l for deallocating. */
1704 l += strlen (yyt) - 1;
1706 while ((c = char_fromInt (lminput ())) != '\n' && c != '\0')
1712 olc = cstring_fromChars (ol);
1714 if (cstring_equalPrefixLit (olc, "pragma"))
1716 char *pname = mstring_create (longUnsigned_fromInt (MAX_PRAGMA_LEN));
1717 char *opname = pname;
1718 char *ptr = ol + 6; /* pragma is six characters, plus space */
1722 /* skip whitespace */
1723 while (((c = *ptr) != '\0') && isspace (c))
1729 while (((c = *ptr) != '\0') && !isspace (c))
1733 if (len > MAX_PRAGMA_LEN)
1744 if (len == PRAGMA_LEN_EXPAND
1745 && mstring_equal (opname, PRAGMA_EXPAND))
1747 cstring exname = cstring_undefined;
1751 while (((c = *ptr) != '\0') && !isspace (c))
1753 exname = cstring_appendChar (exname, c);
1758 ue = usymtab_lookupExposeGlob (exname);
1760 if (uentry_isExpandedMacro (ue))
1762 if (fileloc_isPreproc (uentry_whereDefined (ue)))
1764 fileloc_setColumn (g_currentloc, 1);
1765 uentry_setDefined (ue, g_currentloc);
1769 cstring_free (exname);
1772 else if (cstring_equalPrefixLit (olc, "ident"))
1774 /* Some pre-processors will leave these in the code. Ignore rest of line */
1777 ** Yuk...Win32 filenames can have spaces in them...we need to read
1778 ** to the matching end quote.
1780 else if ((sscanf (ol, "line %d \"", &lineno) == 1)
1781 || (sscanf (ol, " %d \"", &lineno) == 1))
1787 /*@access cstring@*/
1788 while (*tmp != '\"' && *tmp != '\0')
1793 llassert (*tmp == '\"');
1799 while (*tmp != '\"' && *tmp != '\0')
1804 llassert (*tmp == '\"');
1808 # if defined(OS2) || defined(MSDOS) || defined(WIN32)
1811 ** DOS-like path delimiters get delivered in pairs, something like
1812 ** \"..\\\\file.h\", so we have to make it normal again. We do NOT
1813 ** remove the pre dirs yet as we usually specify tmp paths relative
1814 ** to the current directory, so tmp files would not get found in
1815 ** the hash table. If this method fails we try it again later.
1822 ** Skip past the drive marker.
1825 if (strchr (stmp, ':') != NULL)
1827 stmp = strchr (stmp, ':') + 1;
1830 while ((stmp = strchr (stmp, CONNECTCHAR)) != NULL )
1832 if (*(stmp+1) == CONNECTCHAR)
1834 memmove (stmp, stmp+1, strlen (stmp));
1840 fid = fileTable_lookupBase (context_fileTable (), fname);
1841 if (!(fileId_isValid (fid)))
1843 fname = removePreDirs (fname);
1844 fid = fileTable_lookupBase (context_fileTable (), fname);
1847 # else /* !defined(OS2) && !defined(MSDOS) */
1848 fname = removePreDirs (fname);
1849 fid = fileTable_lookupBase (context_fileTable (), fname);
1850 # endif /* !defined(OS2) && !defined(MSDOS) */
1852 if (!(fileId_isValid (fid)))
1854 if (context_inXHFile ())
1856 fid = fileTable_addXHFile (context_fileTable (), fname);
1858 else if (isHeaderFile (fname))
1860 fid = fileTable_addHeaderFile (context_fileTable (), fname);
1864 fid = fileTable_addFile (context_fileTable (), fname);
1868 setFileLine (fid, lineno);
1869 /*@noaccess cstring@*/
1871 else if ((sscanf (ol, "line %d", &lineno) == 1)
1872 || (sscanf (ol, " %d", &lineno) == 1))
1874 setLine (lineno); /* next line is <cr> */
1878 if (mstring_equal (ol, "")) {
1879 DPRINTF (("Empty pp command!"));
1881 ** evs 2000-05-16: This is a horrible kludge, to get around a bug (well, difficulty) in the pre-processor.
1882 ** We handle a plain # in the input file, by echoing it, and ignoring it in the post-pp-file.
1891 llbug (message ("File contains preprocessor command: #%s",
1892 cstring_fromChars (ol)));
1905 static int handleLlSpecial ()
1910 char *s = mstring_createEmpty ();
1916 loc = fileloc_copy (g_currentloc);
1917 DPRINTF (("Handle special: %s", fileloc_unparse (loc)));
1919 while (((ic = ninput ()) != 0) && isalpha (ic))
1922 s = mstring_append (s, c);
1926 DPRINTF (("Read: %s / %s", s, fileloc_unparse (g_currentloc)));
1929 if (charsread == 0 && ic == (int) AFTER_COMMENT_MARKER[0])
1933 llassert (ic == AFTER_COMMENT_MARKER[1]);
1939 return QNOMODS; /* special token no modifications token */
1943 DPRINTF (("Coment marker: %s", os));
1944 tok = commentMarkerToken (cstring_fromChars (os));
1948 tokLength = charsread;
1955 DPRINTF (("Not a comment marker..."));
1956 /* Add rest of the comment */
1958 if (ic != 0 && ic != EOF)
1962 s = mstring_append (s, c);
1965 while (((ic = ninput ()) != 0) && (ic != EOF)
1966 && (ic != AFTER_COMMENT_MARKER[0]))
1970 /* evans 2001-09-01 added to prevent assertion failures for uncloses syntactic comments */
1973 hasnl = TRUE; /* This prevents tokLength from being set later. */
1978 message ("Likely parse error: syntactic comment token spans multiple lines: %s",
1979 cstring_fromChars (s)),
1983 s = mstring_append (s, c);
1988 DPRINTF (("Read: %s / %s", s, fileloc_unparse (g_currentloc)));
1990 if (ic == AFTER_COMMENT_MARKER[0])
1993 llassert ((char) nc == AFTER_COMMENT_MARKER[1]);
1999 while (*s == ' ' || *s == '\t' || *s == '\n')
2004 if (*s == '-' || *s == '+' || *s == '=') /* setting flags */
2008 while (c == '-' || c == '+' || c == '=')
2010 ynm set = ynm_fromCodeChar (c);
2015 thisflag = cstring_fromChars (s);
2017 while ((c = *s) != '\0' && (c != '-') && (c != '=')
2018 && (c != '+') && (c != ' ') && (c != '\t') && (c != '\n'))
2025 if (!context_getFlag (FLG_NOCOMMENTS))
2027 cstring flagname = thisflag;
2028 flagcode fflag = identifyFlag (flagname);
2030 if (flagcode_isSkip (fflag))
2034 else if (flagcode_isInvalid (fflag))
2036 if (isMode (flagname))
2038 if (ynm_isMaybe (set))
2043 ("Semantic comment attempts to restore flag %s. "
2044 "A mode flag cannot be restored.",
2049 context_setMode (flagname);
2055 (FLG_UNRECOGFLAGCOMMENTS,
2056 message ("Unrecognized option in semantic comment: %s",
2061 else if (flagcode_isGlobalFlag (fflag))
2066 ("Semantic comment attempts to set global flag %s. "
2067 "A global flag cannot be set locally.",
2073 context_fileSetFlag (fflag, set);
2075 if (flagcode_hasArgument (fflag))
2077 if (ynm_isMaybe (set))
2082 ("Semantic comment attempts to restore flag %s. "
2083 "A flag for setting a value cannot be restored.",
2088 { /* cut-and-pastied from llmain...blecch */
2089 cstring extra = cstring_undefined;
2095 rest = mstring_copy (s);
2099 while ((rchar = *rest) != '\0'
2100 && (isspace (rchar)))
2106 while ((rchar = *rest) != '\0'
2107 && !isspace (rchar))
2109 extra = cstring_appendChar (extra, rchar);
2116 if (cstring_isUndefined (extra))
2121 ("Flag %s (in semantic comment) must be followed by an argument",
2122 flagcode_unparse (fflag)));
2128 if (flagcode_hasValue (fflag))
2130 setValueFlag (fflag, extra);
2132 else if (flagcode_hasString (fflag))
2134 setStringFlag (fflag, extra);
2151 while ((c == ' ') || (c == '\t') || (c == '\n'))
2157 if (context_inHeader () && !isArtificial (cstring_fromChars (os)))
2159 DPRINTF (("Here adding comment: %s", os));
2160 context_addComment (cstring_fromCharsNew (os));
2172 annotationInfo ainfo;
2174 while (*s != '\0' && *s != ' ' && *s != '\t' && *s != '\n')
2186 t = cstring_toCharsSafe (cstring_downcase (cstring_fromChars (t)));
2187 macrocode = tokenMacroCode (cstring_fromChars (t));
2189 if (macrocode != BADTOK)
2191 tokLength = hasnl ? 0 : mstring_length (t);
2197 if (macrocode == SKIPTOK)
2205 ainfo = context_lookupAnnotation (cstring_fromChars (os));
2207 if (annotationInfo_isDefined (ainfo)) {
2208 DPRINTF (("Found annotation: %s", annotationInfo_unparse (ainfo)));
2209 /*@i324@*/ yylval.annotation = ainfo;
2217 if (context_inHeader ())
2224 if ((context_inMacro () || context_inGlobalContext ())
2225 && macrocode != SKIPTOK
2226 && !isArtificial (cstring_fromChars (os)))
2228 DPRINTF (("Add comment: %s", os));
2229 context_addComment (cstring_fromCharsNew (os));
2242 if (mstring_equal (t, "ignore"))
2244 if (!context_getFlag (FLG_NOCOMMENTS))
2246 context_enterSuppressRegion ();
2249 else if ((*t == 'i' || *t == 't')
2250 && (*(t + 1) == '\0'))
2252 if (!context_getFlag (FLG_NOCOMMENTS)
2253 && (*t == 'i' || context_getFlag (FLG_TMPCOMMENTS)))
2255 context_enterSuppressLine (-1); /* infinite suppression */
2258 else if (((*t == 'i') || (*t == 't'))
2259 && ((*(t + 1) >= '0' && *(t + 1) <= '9')))
2261 bool tmpcomment = (*t == 't');
2263 char *tt = t; /* don't mangle t, since it is free'd */
2266 if (lc >= '0' && lc <= '9')
2268 val = (int)(lc - '0');
2271 while (lc >= '0' && lc <= '9')
2280 if (!context_getFlag (FLG_NOCOMMENTS)
2281 && (!tmpcomment || context_getFlag (FLG_TMPCOMMENTS)))
2283 context_enterSuppressLine (val);
2286 else if (mstring_equal (t, "end"))
2288 if (!context_getFlag (FLG_NOCOMMENTS))
2290 context_exitSuppressRegion ();
2293 else if (mstring_equal (t, "notfunction"))
2295 ; /* handled by pcpp */
2297 else if (mstring_equal (t, "access"))
2303 while ((c = *s) && (c == ' ' || c == '\t' || c == '\n'))
2313 tname = cstring_fromChars (s);
2315 while ((c = *s) != '\0' && c != ' '
2316 && c != '\t' && c != '\n' && c != ',')
2323 DPRINTF (("Access %s", tname));
2325 if (!context_getFlag (FLG_NOCOMMENTS)
2326 && !context_getFlag (FLG_NOACCESS))
2328 if (usymtab_existsType (tname))
2330 typeId uid = usymtab_getTypeId (tname);
2331 uentry ue = usymtab_getTypeEntry (uid);
2333 if (uentry_isAbstractDatatype (ue))
2335 context_addFileAccessType (uid);
2336 DPRINTF (("Adding access to: %s / %d", tname, uid));
2343 ("Non-abstract type %s used in access comment",
2350 if (!(context_inSuppressRegion ()
2351 || context_inSuppressZone (g_currentloc)))
2356 ("Unrecognized type %s used in access comment",
2368 if (c != ',' && c != ' ')
2374 else if (mstring_equal (t, "noaccess"))
2381 while ((lc = *s) && (lc == ' ' || lc == '\t' || lc == '\n'))
2391 tname = cstring_fromChars (s);
2393 while ((lc = *s) != '\0' && lc != ' ' && lc != '\t'
2394 && lc != '\n' && lc != ',')
2401 if (!context_getFlag (FLG_NOCOMMENTS)
2402 && !context_getFlag (FLG_NOACCESS))
2404 if (usymtab_existsType (tname))
2406 typeId tuid = usymtab_getTypeId (tname);
2408 if (context_couldHaveAccess (tuid))
2410 DPRINTF (("Removing access: %s", tname));
2411 context_removeFileAccessType (tuid);
2415 if (!(context_inSuppressRegion ()
2416 || context_inSuppressZone (g_currentloc)))
2418 uentry ue = usymtab_getTypeEntry (tuid);
2420 if (uentry_isAbstractDatatype (ue))
2425 ("Non-accessible abstract type %s used in noaccess comment",
2434 ("Non-abstract type %s used in noaccess comment",
2443 if (!(context_inSuppressRegion ()
2444 || context_inSuppressZone (g_currentloc)))
2449 ("Unrecognized type %s used in noaccess comment",
2461 if (lc != ',' && lc != ' ')
2469 voptgenerror (FLG_UNRECOGCOMMENTS,
2470 message ("Semantic comment unrecognized: %s",
2471 cstring_fromChars (os)), loc);
2482 static /*@only@*/ cstring makeIdentifier (char *s)
2484 char *c = mstring_create (size_toInt (strlen (s)) + 1);
2485 cstring id = cstring_fromChars (c);
2487 while (isalnum (*s) || (*s == '_') || (*s == '$'))
2496 /*@observer@*/ /*@dependent@*/ uentry coerceId (cstring cn)
2498 if (!(usymtab_exists (cn)))
2500 fileloc loc = fileloc_createExternal ();
2503 ** We need to put this in a global scope, otherwise the sRef will be deallocated.
2506 uentry ce = uentry_makeUnrecognized (cn, loc);
2508 if (!context_inIterEnd ())
2512 message ("Unrecognized (possibly system) identifier: %q",
2513 uentry_getName (ce)),
2520 return (usymtab_lookup (cn));
2524 ** like, coerceId, but doesn't supercede for iters
2527 /*@observer@*/ uentry coerceIterId (cstring cn)
2529 if (!(usymtab_exists (cn)))
2531 return uentry_undefined;
2534 return (usymtab_lookup (cn));
2537 /*@observer@*/ cstring LastIdentifier ()
2539 return (lastidprocessed);
2542 static int processIdentifier (cstring id)
2546 if (context_getFlag (FLG_GRAMMAR))
2548 lldiagmsg (message ("Process identifier: %s", id));
2551 context_clearJustPopped ();
2552 lastidprocessed = id;
2554 if (context_inFunctionHeader ())
2556 int tok = commentMarkerToken (id);
2557 DPRINTF (("in function decl..."));
2565 tok = tokenMacroCode (id);
2573 annotationInfo ainfo;
2575 if (expectingMetaStateName)
2577 metaStateInfo msinfo = context_lookupMetaStateInfo (id);
2579 if (metaStateInfo_isDefined (msinfo))
2581 yylval.msinfo = msinfo;
2582 return METASTATE_NAME;
2586 DPRINTF (("Not meta state name: %s", cstring_toCharsSafe (id)));
2590 ainfo = context_lookupAnnotation (id);
2592 if (annotationInfo_isDefined (ainfo))
2594 DPRINTF (("Found annotation: %s", annotationInfo_unparse (ainfo)));
2595 /*@i324@*/ yylval.annotation = ainfo;
2600 DPRINTF (("Not annotation: %s", id));
2606 /* Consider handling: Defined by C99 as static const char __func__[] */
2608 if (context_getFlag (FLG_GNUEXTENSIONS))
2612 if (cstring_equalLit (id, "__stdcall")
2613 || cstring_equalLit (id, "__cdecl")
2614 || cstring_equalLit (id, "__extension__"))
2618 else if (cstring_equalLit (id, "__volatile__"))
2622 else if (cstring_equalLit (id, "__signed"))
2626 else if (cstring_equalLit (id, "__unsigned"))
2630 else if (cstring_equalLit (id, "__const__"))
2634 else if (cstring_equalLit (id, "__alignof__"))
2636 tok = CALIGNOF; /* alignof is parsed like sizeof */
2638 else if (cstring_equalLit (id, "__FUNCTION__")
2639 || cstring_equalLit (id, "__PRETTY_FUNCTION__"))
2641 /* These tokens hold the name of the current function as strings */
2642 yylval.expr = exprNode_stringLiteral (id, fileloc_copy (g_currentloc));
2644 lastWasString = TRUE;
2648 else if (cstring_equalLit (id, "__attribute__")
2649 || cstring_equalLit (id, "__asm__")
2650 || cstring_equalLit (id, "_asm")
2651 || cstring_equalLit (id, "__asm")
2652 || cstring_equalLit (id, "__declspec"))
2655 bool useparens = FALSE;
2656 bool usebraces = FALSE;
2657 bool inquote = FALSE;
2658 bool inescape = FALSE;
2661 while ((ic = input ()) != EOF)
2668 else if (ic == '\\')
2672 else if (ic == '\"')
2708 else if (ic == ')' && useparens)
2711 if (depth == 0) break;
2713 else if (ic == '}' && usebraces)
2716 if (depth == 0) break;
2719 && !usebraces && !useparens
2720 && cstring_equalLit (id, "__asm"))
2723 ** We need this because some MS VC++ include files
2724 ** have __asm mov ... }
2725 ** Its a kludge, but otherwise would need to parse
2734 context_incLineno ();
2736 if (cstring_equalLit (id, "__asm")
2737 && !useparens && !usebraces)
2744 llassert ((useparens && ic == ')')
2745 || (usebraces && ic == '}')
2746 || (!useparens && !usebraces));
2750 else if (cstring_equalLit (id, "inline")
2751 || cstring_equalLit (id, "__inline")
2752 || cstring_equalLit (id, "_inline")
2753 || cstring_equalLit (id, "__inline__"))
2764 le = usymtab_lookupSafe (id);
2766 /*@-dependenttrans@*/
2768 if (uentry_isIter (le))
2770 /*@i32@*/ yylval.entry = le;
2773 else if (uentry_isEndIter (le))
2775 /*@i32@*/ yylval.entry = le;
2776 return (ITER_ENDNAME);
2778 else if (uentry_isUndefined (le))
2782 /* avoid parse errors for certain system built ins */
2784 if (g_expectingTypeName && (cstring_firstChar (id) == '_')
2785 && (cstring_secondChar (id) == '_'))
2787 return (TYPE_NAME_OR_ID);
2790 return (NEW_IDENTIFIER);
2792 else if (!uentry_isDeclared (le) && !uentry_isCodeDefined (le))
2794 if (uentry_isDatatype (le))
2797 return (NEW_IDENTIFIER);
2801 /*@i32@*/ yylval.entry = le;
2802 return (IDENTIFIER);
2805 else if (uentry_isDatatype (le))
2807 if (!g_expectingTypeName)
2811 return (NEW_IDENTIFIER);
2815 yylval.ctyp = uentry_getAbstractType (le);
2817 uentry_setUsed (le, g_currentloc);
2823 /*@i32@*/ yylval.entry = le;
2824 return (IDENTIFIER);
2827 /*@=dependenttrans@*/
2830 static bool processHashIdentifier (/*@only@*/ cstring id)
2832 if (context_inMacro () || context_inIterDef () ||
2833 context_inIterEnd ())
2837 context_clearJustPopped ();
2839 lastidprocessed = id;
2840 le = usymtab_lookupSafe (id);
2842 if (uentry_isParam (le) || uentry_isRefParam (le))
2859 static /*@only@*/ exprNode processString ()
2863 char *nl = strchr (yytext, '\n');
2864 cstring ns = cstring_fromCharsNew (yytext);
2868 loc = fileloc_copy (g_currentloc);
2869 addColumn (cstring_length (ns));
2875 loc = fileloc_copy (g_currentloc);
2877 context_incLineno ();
2879 while ((nl = strchr ((nl + 1), '\n')) != NULL)
2881 context_incLineno ();
2887 res = exprNode_stringLiteral (ns, loc);
2897 llassert (*yytext != '\0');
2898 fchar = *(yytext + 1);
2899 if (fchar != '\\') return fchar;
2901 next = *(yytext + 2);
2905 case 'n': return '\n';
2906 case 't': return '\t';
2907 case '\"': return '\"';
2908 case '\'': return '\'';
2909 case '\\': return '\\';
2910 default: return '\0';
2915 double processFloat ()
2917 double ret = atof (yytext);
2928 llassert (yytext[0] == '0'
2929 && (yytext[1] == 'X' || yytext[1] == 'x'));
2931 while (yytext[index] != '\0') {
2933 char c = yytext[index];
2935 if (c >= '0' && c <= '9') {
2936 tval = (int) c - (int) '0';
2937 } else if (c >= 'A' && c <= 'F') {
2938 tval = (int) c - (int) 'A' + 10;
2939 } else if (c >= 'a' && c <= 'f') {
2940 tval = (int) c - (int) 'a' + 10;
2941 } else if (c == 'U' || c == 'L' || c == 'u' || c == 'l') {
2943 while (yytext[index] != '\0') {
2944 if (c == 'U' || c == 'L' || c == 'u' || c == 'l') {
2949 message ("Invalid character (%c) following specifier in hex constant: %s",
2950 c, cstring_fromChars (yytext)),
2960 message ("Invalid character (%c) in hex constant: %s",
2961 c, cstring_fromChars (yytext)),
2966 val = (val * 16) + tval;
2970 DPRINTF (("Hex constant: %s = %ld", yytext, val));
2975 long processOctal ()
2980 llassert (yytext[0] == '0' && yytext[1] != 'X' && yytext[1] != 'x');
2982 while (yytext[index] != '\0') {
2984 char c = yytext[index];
2986 if (c >= '0' && c <= '7') {
2987 tval = (int) c - (int) '0';
2988 } else if (c == 'U' || c == 'L' || c == 'u' || c == 'l') {
2990 while (yytext[index] != '\0') {
2991 if (c == 'U' || c == 'L' || c == 'u' || c == 'l') {
2996 message ("Invalid character (%c) following specifier in octal constant: %s",
2997 c, cstring_fromChars (yytext)),
3007 message ("Invalid character (%c) in octal constant: %s",
3008 c, cstring_fromChars (yytext)),
3013 val = (val * 8) + tval;
3017 DPRINTF (("Octal constant: %s = %ld", yytext, val));
3024 return (atol (yytext));
3028 processSpec (int tok)
3030 size_t length = strlen (yytext);
3034 setTokLengthT (length);
3040 context_saveLocation ();
3041 setTokLengthT (length);
3042 return (processIdentifier (makeIdentifier (yytext)));
3046 void cscanner_expectingMetaStateName ()
3048 llassert (!expectingMetaStateName);
3049 llassert (context_inFunctionHeader ());
3050 expectingMetaStateName = TRUE;
3053 void cscanner_clearExpectingMetaStateName ()
3055 llassert (expectingMetaStateName);
3056 expectingMetaStateName = FALSE;