2 ** Copyright (c) Massachusetts Institute of Technology 1994-1998.
3 ** All Rights Reserved.
4 ** Unpublished rights reserved under the copyright laws of
7 ** THIS MATERIAL IS PROVIDED AS IS, WITH ABSOLUTELY NO WARRANTY EXPRESSED
8 ** OR IMPLIED. ANY USE IS AT YOUR OWN RISK.
10 ** This code is distributed freely and may be used freely under the
11 ** following conditions:
13 ** 1. This notice may not be removed or altered.
15 ** 2. Works derived from this code are not distributed for
16 ** commercial gain without explicit permission from MIT
17 ** (for permission contact lclint-request@sds.lcs.mit.edu).
20 * Modified by Herbert 08/19/97:
21 * - added #include for IBM's OS/2 compiler.
22 * - fixed weird bug with lookup of tmp files (OS/2 and MSDOS only).
26 * Modified by Mike Smith
27 * Corrected missing 'line' in scanf() calls in handleSpecial().
28 * Without this, I get an error when LCLint hits a '#line' directive
29 * in the pre-pre-processed source files. For safety, I have made these
30 * conditional on OS2 and MSDOS because I don't understand why noone else
31 * has seen this problem.
33 * Modified by Mike Smith, 4th June 1997
34 * Finally resolved the #line problem. The scanf() calls have been fixed to
35 * allow the following #line forms:-
37 * #line 123 "filename"
42 * The last two are generated by the GNU pre-processor, apparently
53 ULSuffix ({U}{L}|{L}{U})
57 ** based on original C lexer by Nate Osgood
58 ** from hacrat@catfish.lcs.mit.edu Mon Jun 14 13:06:32 1993
62 # include "lclintMacros.nf"
65 # include "cgrammar.h"
66 # include "cgrammar_tokens.h"
68 # include "fileIdList.h"
71 # if defined(OS2) && defined(__IBMC__)
72 /* needed for isatty()... */
76 static bool lastWasString = FALSE;
77 static char savechar = '\0';
80 # define yyinput() (incColumn (), getc (yyin))
84 extern /*@external@*/ int read ();
88 static /*@owned@*/ cstring lastidprocessed = cstring_undefined;
90 static int lminput (void);
91 static int tokLength = 0;
92 static bool inSpecPart = FALSE;
93 static bool continueLine = FALSE;
95 static int ninput (void);
96 static char processChar (void);
97 static double processFloat (void);
98 static /*@only@*/ exprNode processString (void);
99 static long processDec (void);
100 static long processHex (void);
101 static long processOctal (void);
102 static int processIdentifier (/*@only@*/ cstring)
103 /*@globals undef lastidprocessed@*/ ;
104 static bool processHashIdentifier (/*@only@*/ cstring)
105 /*@globals undef lastidprocessed@*/ ;
107 static int processSpec (int);
108 static bool handleSpecial (char *);
109 static int handleLlSpecial (void);
110 static void handleMacro (void);
111 static bool processMacro (void);
112 static /*@only@*/ cstring makeIdentifier (char *);
114 /* yes, this is exported! */
115 bool g_expectingTypeName = TRUE; /* beginning of file can be type name! */
117 static int returnInt (ctype, long);
118 static int returnFloat (ctype, double);
119 static int returnChar (char);
120 static void setTokLength (int) /*@modifies g_currentloc@*/ ;
121 static void setTokLengthT (size_t) /*@modifies g_currentloc@*/ ;
123 static void advanceLine (void)
130 # define RETURN_INT(c,i) \
131 do { lastWasString = FALSE; \
132 return (returnInt (c, i)); } while (FALSE)
134 # define RETURN_FLOAT(c,f) \
135 do { lastWasString = FALSE; \
136 return (returnFloat (c, f)); \
139 # define RETURN_CHAR(c) \
140 do { lastWasString = FALSE; \
141 return (returnChar (c)); \
144 # define RETURN_TOK(t) \
145 do { yylval.tok = lltok_create (t, fileloc_decColumn (g_currentloc, tokLength)); \
147 lastWasString = FALSE; \
148 return (t); } while (FALSE)
150 # define RETURN_TYPE(t, ct) \
151 do { yylval.ctyp = ct; tokLength = 0; return (t); } while (FALSE)
153 /* don't fileloc_decColumn (g_currentloc, tokLength));
154 the string could have \n's in it!
157 # define RETURN_STRING(c) \
158 do { yylval.expr = exprNode_stringLiteral (c, fileloc_decColumn (g_currentloc, tokLength)); \
160 lastWasString = TRUE; \
161 return (CCONSTANT); } while (FALSE)
163 # define RETURN_EXPR(e) \
164 do { yylval.expr = e; \
166 lastWasString = TRUE; \
167 return (CCONSTANT); } while (FALSE)
171 static void setTokLength (int len)
177 static void setTokLengthT (size_t len)
179 setTokLength (size_toInt (len));
182 # include "flex.head"
187 "/*" { llfatalbug (cstring_makeLiteral ("Comment in pre-processor output")); }
189 "#"{Letter}({Letter}|{Digit})* {
190 context_saveLocation ();
191 setTokLength (longUnsigned_toInt (mstring_length (yytext)));
193 if (processHashIdentifier (makeIdentifier (yytext + 1)))
201 RETURN_STRING (cstring_makeLiteral ("\"\""));
206 if (handleSpecial (yytext))
213 "#" { if (handleSpecial (yytext))
215 setTokLength (1); RETURN_TOK (0);
218 "..." { setTokLength (3); RETURN_TOK (CTOK_ELIPSIS); }
219 "break" { setTokLength (5); RETURN_TOK (BREAK); }
220 "case" { setTokLength (4); RETURN_TOK (CASE); }
221 "continue" { setTokLength (8); RETURN_TOK (CONTINUE); }
222 "default" { setTokLength (7); RETURN_TOK (DEFAULT); }
223 "do" { setTokLength (2); RETURN_TOK (DO); }
224 "else" { setTokLength (4); RETURN_TOK (CELSE); }
225 "for" { setTokLength (3); RETURN_TOK (CFOR); }
226 "goto" { setTokLength (4); RETURN_TOK (GOTO); }
227 "if" { setTokLength (2); RETURN_TOK (CIF); }
228 "return" { setTokLength (6); RETURN_TOK (RETURN); }
229 "sizeof" { setTokLength (6); RETURN_TOK (CSIZEOF); }
230 "offsetof" { setTokLength (8); RETURN_TOK (COFFSETOF); }
231 "switch" { setTokLength (6); RETURN_TOK (SWITCH); }
232 "while" { setTokLength (5); RETURN_TOK (WHILE); }
233 "va_arg" { setTokLength (6); RETURN_TOK (VA_ARG); }
234 "va_dcl" { setTokLength (6); RETURN_TOK (VA_DCL); }
236 /* gcc extension...this might not be appropriate */
237 setTokLength (6); RETURN_TOK (QINLINE); }
239 "struct" { setTokLength (6); RETURN_TOK (CSTRUCT); }
240 "typedef" { setTokLength (7); RETURN_TOK (CTYPEDEF); }
242 "union" { setTokLength (5); RETURN_TOK (CUNION); }
243 "enum" { setTokLength (4); RETURN_TOK (CENUM); }
245 "void" { setTokLength (4); RETURN_TYPE (CVOID, ctype_void); }
246 "int" { setTokLength (3); RETURN_TYPE (CINT, ctype_int); }
247 "double" { setTokLength (6); RETURN_TYPE (CDOUBLE, ctype_double); }
248 "char" { setTokLength (4); RETURN_TYPE (CGCHAR, ctype_char); }
249 "float" { setTokLength (5); RETURN_TYPE (CGFLOAT, ctype_float); }
251 "long" { setTokLength (4); RETURN_TOK (QLONG); }
252 "short" { setTokLength (5); RETURN_TOK (QSHORT); }
253 "unsigned" { setTokLength (8); RETURN_TOK (QUNSIGNED); }
254 "signed" { setTokLength (6); RETURN_TOK (QSIGNED); }
256 "volatile" { setTokLength (8); RETURN_TOK (QVOLATILE); }
257 "const" { setTokLength (5); RETURN_TOK (QCONST); }
259 /* some systems expect this! [gack!] */
260 "__const" { setTokLength (7); RETURN_TOK (QCONST); }
262 "extern" { setTokLength (6); RETURN_TOK (QEXTERN); }
263 "auto" { setTokLength (4); RETURN_TOK (QAUTO); }
264 "register" { setTokLength (8); RETURN_TOK (QREGISTER); }
265 "static" { setTokLength (6); RETURN_TOK (QSTATIC); }
267 \"(\\.|[^\\"])*\"([ \t\n]*\"(\\.|[^\\"])*\")* { RETURN_EXPR (processString ()); }
268 "out" { return (processSpec (QOUT)); }
269 "in" { return (processSpec (QIN)); }
270 "partial" { return (processSpec (QPARTIAL)); }
271 "special" { return (processSpec (QSPECIAL)); }
272 "anytype" { return (processSpec (QANYTYPE)); }
273 "integraltype" { return (processSpec (QINTEGRALTYPE)); }
274 "unsignedintegraltype" { return (processSpec (QUNSIGNEDINTEGRALTYPE)); }
275 "signedintegraltype" { return (processSpec (QSIGNEDINTEGRALTYPE)); }
276 "keep" { return (processSpec (QKEEP)); }
277 "null" { return (processSpec (QNULL)); }
278 "notnull" { return (processSpec (QNOTNULL)); }
279 "isnull" { return (processSpec (QISNULL)); }
280 "truenull" { return (processSpec (QTRUENULL)); }
281 "falsenull" { return (processSpec (QFALSENULL)); }
282 "relnull" { return (processSpec (QRELNULL)); }
283 "reldef" { return (processSpec (QRELDEF)); }
284 "exposed" { return (processSpec (QEXPOSED)); }
285 "newref" { return (processSpec (QNEWREF)); }
286 "tempref" { return (processSpec (QTEMPREF)); }
287 "killref" { return (processSpec (QKILLREF)); }
288 "refcounted" { return (processSpec (QREFCOUNTED)); }
289 "checked" { return (processSpec (QCHECKED)); }
290 "checkmod" { return (processSpec (QCHECKMOD)); }
291 "checkedstrict" { return (processSpec (QCHECKEDSTRICT)); }
292 "unchecked" { return (processSpec (QUNCHECKED)); }
293 "only" { return (processSpec (QONLY)); }
294 "owned" { return (processSpec (QOWNED)); }
295 "observer" { return (processSpec (QOBSERVER)); }
296 "dependent" { return (processSpec (QDEPENDENT)); }
297 "unused" { return (processSpec (QUNUSED)); }
298 "external" { return (processSpec (QEXTERNAL)); }
299 "sef" { return (processSpec (QSEF)); }
300 "shared" { return (processSpec (QSHARED)); }
301 "yield" { return (processSpec (QYIELD)); }
302 "undef" { return (processSpec (QUNDEF)); }
303 "killed" { return (processSpec (QKILLED)); }
304 "nullterminated" { return (processSpec (QNULLTERMINATED));}
306 {Letter}({Letter}|{Digit})* { int tok;
307 context_saveLocation ();
308 setTokLength (longUnsigned_toInt (mstring_length (yytext)));
309 tok = processIdentifier (makeIdentifier (yytext));
315 0[xX]{H}+ { setTokLengthT (mstring_length (yytext));
316 RETURN_INT (ctype_int, processHex ()); /* evs 2000-05-17 was ctype_uint */
318 0[xX]{H}+{L} { setTokLengthT (mstring_length (yytext));
319 RETURN_INT (ctype_lint, processHex ()); }
320 0[xX]{H}+{L}{L} { setTokLengthT (mstring_length (yytext));
321 RETURN_INT (ctype_llint, processHex ()); }
322 0[xX]{H}+{U} { setTokLengthT (mstring_length (yytext));
323 RETURN_INT (ctype_uint, processHex ()); }
324 0[xX]{H}+{ULSuffix} { setTokLengthT (mstring_length (yytext));
325 RETURN_INT (ctype_ulint, processHex ()); }
326 0[xX]{H}+{U}{L}{L} { setTokLengthT (mstring_length (yytext));
327 RETURN_INT (ctype_ullint, processHex ()); }
328 0[xX]{H}+{L}{L}{U} { setTokLengthT (mstring_length (yytext));
329 RETURN_INT (ctype_ullint, processHex ()); }
330 0{Digit}+ { setTokLengthT (mstring_length (yytext));
331 RETURN_INT (ctype_int, processOctal ()); }
332 0{Digit}+{U} { setTokLengthT (mstring_length (yytext));
333 RETURN_INT (ctype_uint, processOctal ()); }
334 0{Digit}+{L} { setTokLengthT (mstring_length (yytext));
335 RETURN_INT (ctype_lint, processOctal ()); }
336 0{Digit}+{L}{L} { setTokLengthT (mstring_length (yytext));
337 RETURN_INT (ctype_llint, processOctal ()); }
338 0{Digit}+{ULSuffix} { setTokLengthT (mstring_length (yytext));
339 RETURN_INT (ctype_ulint, processOctal ()); }
340 0{Digit}+{U}{L}{L} { setTokLengthT (mstring_length (yytext));
341 RETURN_INT (ctype_ullint, processOctal ()); }
342 0{Digit}+{L}{L}{U} { setTokLengthT (mstring_length (yytext));
343 RETURN_INT (ctype_ullint, processOctal ()); }
344 {Digit}+ { setTokLengthT (mstring_length (yytext));
345 RETURN_INT (ctype_int, processDec ()); }
346 {Digit}+{U} { setTokLengthT (mstring_length (yytext));
347 RETURN_INT (ctype_uint, processDec ()); }
348 {Digit}+{L} { setTokLengthT (mstring_length (yytext));
349 RETURN_INT (ctype_lint, processDec ()); }
350 {Digit}+{L}{L} { setTokLengthT (mstring_length (yytext));
351 RETURN_INT (ctype_llint, processDec ()); }
352 {Digit}+{ULSuffix} { setTokLengthT (mstring_length (yytext));
353 RETURN_INT (ctype_ulint, processDec ()); }
354 {Digit}+{U}{L}{L} { setTokLengthT (mstring_length (yytext));
355 RETURN_INT (ctype_ullint, processDec ()); }
356 {Digit}+{L}{L}{U} { setTokLengthT (mstring_length (yytext));
357 RETURN_INT (ctype_ullint, processDec ()); }
358 '(\\.|[^\\'])+' { setTokLengthT (mstring_length (yytext));
359 RETURN_CHAR (processChar ()); }
360 {Digit}+{E}[fF] { setTokLengthT (mstring_length (yytext));
361 RETURN_FLOAT (ctype_float, processFloat ()); }
362 {Digit}+{E}[lL] { setTokLengthT (mstring_length (yytext));
363 RETURN_FLOAT (ctype_ldouble, processFloat ()); }
364 {Digit}+{E} { setTokLengthT (mstring_length (yytext));
365 RETURN_FLOAT (ctype_double, processFloat ()); }
367 {Digit}*"."{Digit}+({E})?[fF] { setTokLengthT (mstring_length (yytext));
368 RETURN_FLOAT (ctype_float, processFloat ()); }
369 {Digit}*"."{Digit}+({E})?[lL] { setTokLengthT (mstring_length (yytext));
370 RETURN_FLOAT (ctype_ldouble, processFloat ()); }
371 {Digit}*"."{Digit}+({E})? { setTokLengthT (mstring_length (yytext));
372 RETURN_FLOAT (ctype_double, processFloat ()); }
374 {Digit}+"."{Digit}*({E})?[fF] { setTokLengthT (mstring_length (yytext));
375 RETURN_FLOAT (ctype_float, processFloat ()); }
376 {Digit}+"."{Digit}*({E})?[lL] { setTokLengthT (mstring_length (yytext));
377 RETURN_FLOAT (ctype_ldouble, processFloat ()); }
378 {Digit}+"."{Digit}*({E})? { setTokLengthT (mstring_length (yytext));
379 RETURN_FLOAT (ctype_double, processFloat ()); }
381 ">>=" { setTokLength (3); RETURN_TOK (RIGHT_ASSIGN); }
382 "<<=" { setTokLength (3); RETURN_TOK (LEFT_ASSIGN); }
383 "+=" { setTokLength (2); RETURN_TOK (ADD_ASSIGN); }
384 "-=" { setTokLength (2); RETURN_TOK (SUB_ASSIGN); }
385 "*=" { setTokLength (2); RETURN_TOK (MUL_ASSIGN); }
386 "/=" { setTokLength (2); RETURN_TOK (DIV_ASSIGN); }
387 "%=" { setTokLength (2); RETURN_TOK (MOD_ASSIGN); }
388 "&=" { setTokLength (2); RETURN_TOK (AND_ASSIGN); }
389 "^=" { setTokLength (2); RETURN_TOK (XOR_ASSIGN); }
390 "|=" { setTokLength (2); RETURN_TOK (OR_ASSIGN); }
391 ">>" { setTokLength (2); RETURN_TOK (RIGHT_OP); }
392 "<<" { setTokLength (2); RETURN_TOK (LEFT_OP); }
393 "++" { setTokLength (2); RETURN_TOK (INC_OP); }
394 "--" { setTokLength (2); RETURN_TOK (DEC_OP); }
395 "->" { setTokLength (2); RETURN_TOK (ARROW_OP); }
396 "&&" { setTokLength (2); RETURN_TOK (AND_OP); }
397 "||" { setTokLength (2); RETURN_TOK (OR_OP); }
398 "<=" { setTokLength (2); RETURN_TOK (LE_OP); }
399 ">=" { setTokLength (2); RETURN_TOK (GE_OP); }
400 "==" { setTokLength (2); RETURN_TOK (EQ_OP); }
401 "!=" { setTokLength (2); RETURN_TOK (NE_OP); }
402 ";" { setTokLength (1); RETURN_TOK (TSEMI); }
403 "{" { setTokLength (1); RETURN_TOK (TLBRACE); }
404 "}" { setTokLength (1); RETURN_TOK (TRBRACE); }
405 "," { setTokLength (1); RETURN_TOK (TCOMMA); }
406 ":" { setTokLength (1); RETURN_TOK (TCOLON); }
407 "=" { setTokLength (1); RETURN_TOK (TASSIGN); }
408 "(" { setTokLength (1); RETURN_TOK (TLPAREN); }
409 ")" { setTokLength (1); RETURN_TOK (TRPAREN); }
410 "[" { setTokLength (1); RETURN_TOK (TLSQBR); }
411 "]" { setTokLength (1); RETURN_TOK (TRSQBR); }
412 "." { setTokLength (1); RETURN_TOK (TDOT); }
413 "&" { setTokLength (1); RETURN_TOK (TAMPERSAND); }
414 "!" { setTokLength (1); RETURN_TOK (TEXCL); }
417 "~" { setTokLength (1); RETURN_TOK (TTILDE); }
418 "-" { setTokLength (1); RETURN_TOK (TMINUS); }
419 "+" { setTokLength (1); RETURN_TOK (TPLUS); }
420 "*" { setTokLength (1); RETURN_TOK (TMULT); }
421 "/" { setTokLength (1); RETURN_TOK (TDIV); }
422 "%" { setTokLength (1); RETURN_TOK (TPERCENT); }
423 "<" { setTokLength (1); RETURN_TOK (TLT); }
424 ">" { setTokLength (1); RETURN_TOK (TGT); }
425 "^" { setTokLength (1); RETURN_TOK (TCIRC); }
426 "|" { setTokLength (1); RETURN_TOK (TBAR); }
427 "?" { setTokLength (1); RETURN_TOK (TQUEST); }
429 [ \t\v\f] { incColumn (); }
430 \n { context_incLineno ();
433 continueLine = FALSE;
437 if (context_inMacro ())
439 /* Don't use RETURN_TOK */
440 yylval.tok = lltok_create (TENDMACRO, g_currentloc);
441 lastWasString = FALSE;
446 "@@MR@@" { setTokLength (6);
448 if (processMacro ()) {
449 if (context_inIterDef ())
451 RETURN_TOK (LLMACROITER);
453 if (context_inIterEnd ())
455 RETURN_TOK (LLMACROEND);
457 if (context_inMacro ())
459 RETURN_TOK (LLMACRO);
463 "@QLMR" { if (context_inHeader () || context_inFunction ())
469 int nspchar = ninput ();
473 ** This is a hack to get the column number correct.
476 llassert (nspchar >= '0' && nspchar <= '9');
478 nspaces = nspchar - '0';
480 setTokLength (5 + nspaces);
484 if (context_inIterDef ())
486 RETURN_TOK (LLMACROITER);
488 if (context_inIterEnd ())
490 RETURN_TOK (LLMACROEND);
492 if (context_inMacro ())
494 RETURN_TOK (LLMACRO);
499 "@.CT" { setTokLength (4); lldiagmsg (ctype_unparseTable ()); }
500 "@.F" { setTokLength (3);
501 lldiagmsg (message ("%q: *** marker ***", fileloc_unparse (g_currentloc)));
503 "@.L" { setTokLength (3); usymtab_printLocal (); }
504 "@.A" { setTokLength (3); lldiagmsg (usymtab_unparseAliases ()); }
505 "@.C" { setTokLength (3); lldiagmsg (context_unparse ()); }
506 "@.W" { setTokLength (3); lldiagmsg (context_unparseClauses ()); }
507 "@.G" { setTokLength (3); usymtab_printGuards (); }
508 "@.S" { setTokLength (3); usymtab_printOut (); }
509 "@.X" { setTokLength (3); usymtab_printAll (); }
510 "@.Z" { setTokLength (3); usymtab_printComplete (); }
511 "@.T" { setTokLength (3); usymtab_printTypes (); }
512 "@.K" { setTokLength (3); lldiagmsg (usymtab_unparseStack ()); }
513 "@.M" { setTokLength (3);
514 lldiagmsg (message ("Can modify: %q",
515 sRefSet_unparse (context_modList ())));
517 "%{" { /* BEFORE_COMMENT_MARKER */
519 incColumn (); incColumn ();
520 tok = handleLlSpecial ();
526 "%}" { /* AFTER_COMMENT_MARKER */
529 RETURN_TOK (QENDMACRO); }
530 "\\" { incColumn (); continueLine = TRUE; }
534 message ("Invalid character (ascii: %d), skipping character",
542 /*@null@*/ /*@observer@*/ char *name;
547 ** These tokens are followed by syntax that is parsed by the
551 struct skeyword s_parsetable[] = {
552 { "modifies", QMODIFIES } ,
553 { "globals", QGLOBALS } ,
555 { "constant", QCONSTANT } ,
556 { "function", QFUNCTION } ,
558 { "defines", QDEFINES } ,
560 { "allocates", QALLOCATES } ,
562 { "releases", QRELEASES } ,
563 { "pre", QPRECLAUSE } ,
564 { "post", QPOSTCLAUSE } ,
565 {"setBufferSize", QSETBUFFERSIZE},
566 {"setStringLength", QSETSTRINGLENGTH},
567 {"testinRange", QTESTINRANGE},
572 ** These tokens are either stand-alone tokens, or followed by
573 ** token-specific text.
576 struct skeyword s_keytable[] = {
577 { "anytype", QANYTYPE } ,
578 { "integraltype", QINTEGRALTYPE } ,
579 { "unsignedintegraltype", QUNSIGNEDINTEGRALTYPE } ,
580 { "signedintegraltype", QSIGNEDINTEGRALTYPE } ,
584 { "owned", QOWNED } ,
585 { "dependent", QDEPENDENT } ,
586 { "partial", QPARTIAL } ,
587 { "special", QSPECIAL } ,
588 { "truenull", QTRUENULL } ,
589 { "falsenull", QFALSENULL } ,
592 { "notnull", QNOTNULL } ,
593 { "abstract", QABSTRACT } ,
594 { "concrete", QCONCRETE } ,
595 { "mutable", QMUTABLE } ,
596 { "immutable", QIMMUTABLE } ,
597 { "unused", QUNUSED } ,
598 { "external", QEXTERNAL } ,
600 { "unique", QUNIQUE } ,
601 { "returned", QRETURNED } ,
602 { "exposed", QEXPOSED } ,
603 { "refcounted", QREFCOUNTED } ,
605 { "newref", QNEWREF } ,
606 { "tempref", QTEMPREF } ,
607 { "killref", QKILLREF } ,
609 { "relnull", QRELNULL } ,
610 { "nullterminated", QNULLTERMINATED },
611 { "setBufferSize", QSETBUFFERSIZE },
612 { "testInRange", QTESTINRANGE},
613 { "reldef", QRELDEF } ,
614 { "observer", QOBSERVER } ,
615 { "exits", QEXITS } ,
616 { "mayexit", QMAYEXIT } ,
617 { "trueexit", QTRUEEXIT } ,
618 { "falseexit", QFALSEEXIT } ,
619 { "neverexit", QNEVEREXIT } ,
621 { "shared", QSHARED } ,
623 { "unchecked", QUNCHECKED } ,
624 { "checked", QCHECKED } ,
625 { "checkmod", QCHECKMOD } ,
626 { "checkedstrict", QCHECKEDSTRICT } ,
627 { "innercontinue", QINNERCONTINUE } ,
628 { "innerbreak", QINNERBREAK } ,
629 { "loopbreak", QLOOPBREAK } ,
630 { "switchbreak", QSWITCHBREAK } ,
631 { "safebreak", QSAFEBREAK } ,
632 { "fallthrough", QFALLTHROUGH } ,
633 { "l_fallthrou", QLINTFALLTHROUGH } ,
634 { "l_fallth", QLINTFALLTHRU } ,
635 { "notreached", QNOTREACHED } ,
636 { "l_notreach", QLINTNOTREACHED } ,
637 { "printflike", QPRINTFLIKE } ,
638 { "l_printfli", QLINTPRINTFLIKE } ,
639 { "scanflike", QSCANFLIKE } ,
640 { "messagelike", QMESSAGELIKE } ,
641 { "l_argsus", QARGSUSED } ,
646 ** would be better if these weren't hard coded...
649 static bool isArtificial (cstring s)
651 return (cstring_equalLit (s, "modifies")
652 || cstring_equalLit (s, "globals")
653 || cstring_equalLit (s, "alt"));
656 void swallowMacro (void)
659 bool skipnext = FALSE;
661 while ((i = lminput ()) != EOF)
678 checkUngetc (i, yyin);
686 checkUngetc (i, yyin);
690 static int commentMarkerToken (cstring s)
694 while (s_parsetable[i].name != NULL)
696 if (cstring_equalLit (s, s_parsetable[i].name))
698 return s_parsetable[i].token;
707 static int tokenMacroCode (cstring s)
711 while (s_keytable[i].name != NULL)
713 if (cstring_equalLit (s, s_keytable[i].name))
715 if (s_keytable[i].token == QLINTFALLTHROUGH)
718 (FLG_WARNLINTCOMMENTS,
720 ("Traditional lint comment /*FALLTHROUGH*/ used. "
721 "This is interpreted by "
722 "LCLint in the same way as most Unix lints, but it is "
723 "preferable to replace it with the /*@fallthrough@*/ "
728 else if (s_keytable[i].token == QLINTFALLTHRU)
731 (FLG_WARNLINTCOMMENTS,
733 ("Traditional lint comment /*FALLTHRU*/ used. "
734 "This is interpreted by "
735 "LCLint in the same way as most Unix lints, but it is "
736 "preferable to replace it with the /*@fallthrough@*/ "
741 else if (s_keytable[i].token == QLINTNOTREACHED)
744 (FLG_WARNLINTCOMMENTS,
746 ("Traditional lint comment /*NOTREACHED*/ used. "
747 "This is interpreted by "
748 "LCLint in the same way as most Unix lints, but it is "
749 "preferable to replace it with the /*@notreached@*/ "
750 "stylized comment."),
755 else if (s_keytable[i].token == QPRINTFLIKE)
757 setSpecialFunction (QU_PRINTFLIKE);
760 else if (s_keytable[i].token == QLINTPRINTFLIKE)
763 (FLG_WARNLINTCOMMENTS,
765 ("Traditional lint comment /*PRINTFLIKE*/ used. "
766 "This is interpreted by "
767 "LCLint in the same way as most Unix lints, but it is "
768 "preferable to replace it with either /*@printflike@*/, "
769 "/*@scanflike@*/ or /*@messagelike@*/."),
772 setSpecialFunction (QU_PRINTFLIKE);
775 else if (s_keytable[i].token == QSCANFLIKE)
777 setSpecialFunction (QU_SCANFLIKE);
780 else if (s_keytable[i].token == QMESSAGELIKE)
782 setSpecialFunction (QU_MESSAGELIKE);
785 else if (s_keytable[i].token == QARGSUSED)
788 (FLG_WARNLINTCOMMENTS,
790 ("Traditional lint comment /*ARGSUSED*/ used. "
791 "This is interpreted by "
792 "LCLint in the same way as most Unix lints, but it is "
793 "preferable to use /*@unused@*/ annotations on "
794 "the unused parameters."),
801 return s_keytable[i].token;
810 static int lminput ()
812 if (savechar == '\0')
819 int save = (int) savechar;
825 static void lmsavechar (char c)
827 if (savechar == '\0') savechar = c;
830 llbuglit ("lmsavechar: override");
834 static int returnFloat (ctype ct, double f)
836 yylval.expr = exprNode_floatLiteral (f, ct, cstring_fromChars (yytext),
837 fileloc_decColumn (g_currentloc, tokLength));
842 static int returnInt (ctype ct, long i)
846 if (ctype_equal (ct, ctype_int))
850 c = context_typeofZero ();
854 c = context_typeofOne ();
858 yylval.expr = exprNode_numLiteral (c, cstring_fromChars (yytext),
859 fileloc_decColumn (g_currentloc, tokLength), i);
864 static int returnChar (char c)
866 yylval.expr = exprNode_charLiteral (c, cstring_fromChars (yytext),
867 fileloc_decColumn (g_currentloc, tokLength));
876 if (c != EOF && ((char)c == '\n'))
878 context_incLineno ();
884 static char macro_nextChar ()
886 static bool in_quote = FALSE, in_escape = FALSE, in_char = FALSE;
891 c = char_fromInt (ic);
893 if (!in_quote && !in_char && (c == '\\' || c == BEFORE_COMMENT_MARKER[0]))
897 while ((c = char_fromInt (lminput ())) != '\0' && c != '\n')
899 ; /* skip to newline */
902 context_incLineno ();
906 return macro_nextChar ();
913 else /* if (c == '@') */
915 if (handleLlSpecial () != BADTOK)
917 llerrorlit (FLG_SYNTAX, "Macro cannot use special syntax");
920 return macro_nextChar ();
923 else if (!in_escape && c == '\"')
925 in_quote = !in_quote;
927 else if (!in_escape && c == '\'')
931 else if ((in_quote || in_char) && c == '\\')
933 in_escape = !in_escape;
935 else if ((in_quote || in_char) && in_escape)
939 else if (!in_quote && c == '/')
943 if ((c2 = char_fromInt (lminput ())) == '*')
947 while ((c2 = char_fromInt (lminput ())) != '\0'
948 && c2 != '\n' && c2 != '*')
955 while ((c2 = char_fromInt (lminput ())) != '\0'
968 llfatalerror (cstring_makeLiteral ("Macro: bad comment!"));
972 return macro_nextChar ();
976 /*** putchar does not work! why? puts to stdio...??! ***/
984 ** keeps stylized comments
987 static char macro_nextCharC ()
989 static bool in_quote = FALSE, in_escape = FALSE, in_char = FALSE;
992 c = char_fromInt (lminput ());
994 if (!in_quote && !in_char && c == '\\')
996 while ((c = char_fromInt (lminput ())) != '\0' && c != '\n')
998 ; /* skip to newline */
1001 context_incLineno ();
1005 return macro_nextCharC ();
1012 else if (!in_escape && c == '\"')
1014 in_quote = !in_quote;
1016 else if (!in_escape && c == '\'')
1020 else if ((in_quote || in_char) && c == '\\')
1022 in_escape = !in_escape;
1024 else if ((in_quote || in_char) && in_escape)
1028 else if (!in_quote && c == '/')
1032 if ((c2 = char_fromInt (lminput ())) == '*')
1036 while ((c2 = char_fromInt (lminput ())) != '\0'
1037 && c2 != '\n' && c2 != '*')
1044 while ((c2 = char_fromInt (lminput ())) != '\0'
1057 llfatalerror (cstring_makeLiteral ("Macro: bad comment!"));
1061 return macro_nextCharC ();
1072 ** skips whitespace (handles line continuations)
1073 ** returns first non-whitespace character
1076 static char skip_whitespace ()
1080 while ((c = macro_nextChar ()) == ' ' || c == '\t')
1088 static void handleMacro ()
1090 cstring mac = cstring_undefined;
1094 while (currentColumn () > 2)
1096 mac = cstring_appendChar (mac, ' ');
1100 c = macro_nextCharC ();
1102 if (c >= '0' && c <= '9')
1106 for (i = 0; i < ((c - '0') + 1); i++)
1108 mac = cstring_appendChar (mac, ' ');
1116 while (((c = macro_nextCharC ()) != '\0') && (c != '\n'))
1118 mac = cstring_appendChar (mac, c);
1122 macrocode = tokenMacroCode (mac);
1124 if (macrocode == BADTOK && !isArtificial (mac))
1126 DPRINTF (("Add macro: %s", mac));
1127 context_addMacroCache (mac);
1136 context_incLineno ();
1140 static bool processMacro (void)
1145 cstring fname = cstring_undefined;
1147 bool isspecfcn = FALSE;
1148 bool isiter = FALSE;
1149 bool skipparam = FALSE;
1150 bool isenditer = FALSE;
1151 bool unknownm = FALSE;
1152 bool hasParams = FALSE;
1153 bool emptyMacro = FALSE;
1154 char c = skip_whitespace ();
1155 fileloc loc = fileloc_noColumn (g_currentloc);
1157 /* are both of these necessary? what do they mean? */
1158 uentryList specparams = uentryList_undefined;
1159 uentryList pn = uentryList_undefined;
1161 context_resetMacroMissingParams ();
1163 if (c == '\0' || c == '\n')
1165 llcontbug (cstring_makeLiteral ("Bad macro"));
1170 fname = cstring_appendChar (fname, c);
1172 while ((c = macro_nextChar ()) != '(' && c != '\0'
1173 && c != ' ' && c != '\t' && c != '\n')
1175 fname = cstring_appendChar (fname, c);
1178 if (c == ' ' || c == '\t' || c == '\n')
1184 while (c == ' ' || c == '\t')
1186 c = macro_nextChar ();
1200 hasParams = (c == '(');
1203 if (usymtab_exists (fname))
1205 e2 = usymtab_lookupExpose (fname);
1206 ct = uentry_getType (e2);
1209 if (uentry_isCodeDefined (e2)
1210 && fileloc_isUser (uentry_whereDefined (e2)))
1214 message ("Macro %s already defined", fname),
1217 uentry_showWhereDefined (e2);
1218 uentry_clearDefined (e2);
1221 if (uentry_isFunction (e2))
1223 uentry_setType (e2, ctype_unknown);
1226 context_enterUnknownMacro (e2);
1230 context_enterConstantMacro (e2);
1235 if (uentry_isForward (e2) && uentry_isFunction (e2))
1242 ("Parameterized macro has no prototype or specification: %s ",
1247 uentry_setType (e2, ctype_unknown);
1248 uentry_setFunctionDefined (e2, loc);
1249 uentry_setUsed (e2, fileloc_undefined);
1250 context_enterUnknownMacro (e2);
1254 if (uentry_isIter (e2))
1257 specparams = uentry_getParams (e2);
1258 noparams = uentryList_size (specparams);
1259 uentry_setDefined (e2, loc);
1260 context_enterIterDef (e2);
1262 else if (uentry_isEndIter (e2))
1265 uentry_setDefined (e2, loc);
1266 context_enterIterEnd (e2); /* don't care about it now */
1267 /* but should parse like an iter! */
1269 else if (uentry_isConstant (e2))
1275 message ("Constant %s implemented as parameterized macro",
1279 uentry_showWhereSpecified (e2);
1280 uentry_setType (e2, ctype_unknown);
1281 uentry_makeVarFunction (e2);
1282 uentry_setDefined (e2, g_currentloc);
1283 uentry_setFunctionDefined (e2, g_currentloc);
1284 context_enterUnknownMacro (e2);
1288 if (!uentry_isSpecified (e2))
1290 fileloc oloc = uentry_whereDeclared (e2);
1292 if (fileloc_isLib (oloc))
1296 else if (fileloc_isUndefined (oloc)
1297 || fileloc_isPreproc (oloc))
1302 (FLG_MACROCONSTDECL,
1304 ("Macro constant %q not declared",
1305 uentry_getName (e2)),
1309 else if (!fileloc_withinLines (oloc, loc, 2))
1310 { /* bogus! will give errors if there is too much whitespace */
1314 ("Macro constant name %s does not match name in "
1315 "previous constant declaration. This constant "
1316 "is declared at %q", fname,
1317 fileloc_unparse (oloc)),
1322 context_enterConstantMacro (e2);
1323 cstring_free (fname);
1329 else if (ctype_isFunction (ct))
1332 specparams = ctype_argsFunction (ct);
1333 noparams = uentryList_size (specparams);
1335 uentry_setFunctionDefined (e2, loc);
1336 context_enterMacro (e2);
1338 else if (uentry_isVar (e2))
1344 message ("Variable %s implemented as parameterized macro",
1348 uentry_showWhereSpecified (e2);
1349 uentry_setType (e2, ctype_unknown);
1350 uentry_makeVarFunction (e2);
1351 uentry_setDefined (e2, g_currentloc);
1352 uentry_setFunctionDefined (e2, g_currentloc);
1353 context_enterUnknownMacro (e2);
1357 uentry ucons = uentry_makeConstant (fname,
1360 if (uentry_isExpandedMacro (e2))
1368 message ("Variable %s implemented by a macro",
1372 uentry_showWhereSpecified (e2);
1376 uentry_setDefined (e2, loc);
1377 uentry_setUsed (ucons, loc);
1379 context_enterConstantMacro (ucons);
1380 uentry_markOwned (ucons);
1381 cstring_free (fname);
1387 if (uentry_isDatatype (e2))
1391 message ("Type implemented as macro: %x",
1392 uentry_getName (e2)),
1393 message ("A type is implemented using a macro definition. A "
1394 "typedef should be used instead."),
1398 /* Must exit scope (not sure why a new scope was entered?) */
1399 usymtab_quietExitScope (g_currentloc);
1400 uentry_setDefined (e2, g_currentloc);
1406 (message ("Unexpanded macro not function or constant: %q",
1407 uentry_unparse (e2)));
1408 uentry_setType (e2, ctype_unknown);
1412 uentry_makeVarFunction (e2);
1413 uentry_setDefined (e2, g_currentloc);
1414 uentry_setFunctionDefined (e2, g_currentloc);
1415 context_enterUnknownMacro (e2);
1427 (FLG_MACROMATCHNAME,
1428 message ("Unexpanded macro %s does not match name of a constant "
1429 "or iter declaration. The name used in the control "
1430 "comment on the previous line should match. "
1431 "(Assuming macro defines a constant.)",
1436 ce = uentry_makeConstant (fname, ctype_unknown, fileloc_undefined);
1437 uentry_setUsed (ce, loc); /* perhaps bogus? */
1438 e2 = usymtab_supEntryReturn (ce);
1440 context_enterConstantMacro (e2);
1441 cstring_free (fname);
1446 /* in macros, ( must follow immediatetly after name */
1452 c = skip_whitespace ();
1454 while (c != ')' && c != '\0')
1457 bool suppress = context_inSuppressRegion ();
1458 cstring paramname = cstring_undefined;
1461 ** save the parameter location
1465 context_saveLocation ();
1468 while (c != ' ' && c != '\t' && c != ',' && c != '\0' && c != ')')
1470 paramname = cstring_appendChar (paramname, c);
1471 c = macro_nextChar ();
1474 if (c == ' ' || c == '\t') c = skip_whitespace ();
1478 c = macro_nextChar ();
1479 if (c == ' ' || c == '\t') c = skip_whitespace ();
1484 llfatalerror (cstring_makeLiteral
1485 ("Bad macro syntax: uentryList"));
1488 if ((isspecfcn || isiter) && (paramno < noparams)
1489 && !uentry_isElipsisMarker (uentryList_getN
1490 (specparams, paramno)))
1492 uentry decl = uentryList_getN (specparams, paramno);
1495 param = uentry_nameCopy (paramname, decl);
1498 uentry_setParam (param);
1499 sr = sRef_makeParam (paramno, uentry_getType (param));
1501 if (sRef_getNullState (sr) == NS_ABSNULL)
1503 ctype pt = ctype_realType (uentry_getType (param));
1505 if (ctype_isUser (pt))
1507 uentry te = usymtab_getTypeEntrySafe (ctype_typeId (pt));
1509 if (uentry_isValid (te))
1511 sRef_setStateFromUentry (sr, te);
1516 sRef_setNullState (sr, NS_UNKNOWN, g_currentloc);
1520 uentry_setSref (param, sr);
1521 uentry_setDeclaredForceOnly (param, context_getSaveLocation ());
1523 skipparam = isiter && uentry_isOut (uentryList_getN (specparams, paramno));
1527 fileloc sloc = context_getSaveLocation ();
1529 param = uentry_makeVariableSrefParam
1530 (paramname, ctype_unknown, sRef_makeParam (paramno, ctype_unknown));
1531 cstring_free (paramname);
1533 sRef_setPosNull (uentry_getSref (param), sloc);
1535 uentry_setDeclaredForce (param, sloc);
1538 fileloc_free (sloc);
1543 llassert (!uentry_isElipsisMarker (param));
1547 sRef_makeUnsafe (uentry_getSref (param));
1550 pn = uentryList_add (pn, uentry_copy (param));
1551 usymtab_supEntry (param);
1555 /* don't add param */
1556 uentry_free (param);
1561 (void) macro_nextChar ();
1562 c = skip_whitespace ();
1570 if (isspecfcn || isiter)
1572 if (paramno != noparams && noparams >= 0)
1578 message ("Macro %s specified with %d args, defined with %d",
1579 fname, noparams, paramno),
1582 uentry_showWhereSpecified (e2);
1583 uentry_resetParams (e2, pn);
1588 uentry_resetParams (e2, pn);
1595 ** the form should be:
1597 ** # define newname oldname
1598 ** where oldname refers to a function matching the specification
1604 sRef_setGlobalScope ();
1605 usymtab_supGlobalEntry (uentry_makeVariableLoc (fname, ctype_unknown));
1606 sRef_clearGlobalScope ();
1610 context_setMacroMissingParams ();
1615 /* context_setuentryList (pn); */
1616 usymtab_enterScope ();
1619 cstring_free (fname);
1624 static bool handleSpecial (char *yyt)
1626 char *l = mstring_create (MAX_NAME_LENGTH);
1627 static bool reportcpp = FALSE;
1633 strcpy (l, yyt + 1);
1635 /* Need to safe original l for deallocating. */
1638 l += strlen (yyt) - 1;
1640 while ((c = char_fromInt (lminput ())) != '\n' && c != '\0')
1646 olc = cstring_fromChars (ol);
1648 if (cstring_equalPrefix (olc, "pragma"))
1650 char *pname = mstring_create (longUnsigned_fromInt (MAX_PRAGMA_LEN));
1651 char *opname = pname;
1652 char *ptr = ol + 6; /* pragma is six characters, plus space */
1656 /* skip whitespace */
1657 while (((c = *ptr) != '\0') && isspace (c))
1663 while (((c = *ptr) != '\0') && !isspace (c))
1667 if (len > MAX_PRAGMA_LEN)
1678 if (len == PRAGMA_LEN_EXPAND
1679 && mstring_equal (opname, PRAGMA_EXPAND))
1681 cstring exname = cstring_undefined;
1685 while (((c = *ptr) != '\0') && !isspace (c))
1687 exname = cstring_appendChar (exname, c);
1692 ue = usymtab_lookupExposeGlob (exname);
1694 if (uentry_isExpandedMacro (ue))
1696 if (fileloc_isPreproc (uentry_whereDefined (ue)))
1698 fileloc_setColumn (g_currentloc, 1);
1699 uentry_setDefined (ue, g_currentloc);
1703 cstring_free (exname);
1706 else if (cstring_equalPrefix (olc, "ident"))
1708 /* Some pre-processors will leave these in the code. Ignore rest of line */
1711 ** Yuk...Win32 filenames can have spaces in them...we need to read
1712 ** to the matching end quote.
1714 else if ((sscanf (ol, "line %d \"", &lineno) == 1)
1715 || (sscanf (ol, " %d \"", &lineno) == 1))
1721 while (*tmp != '\"' && *tmp != '\0')
1726 llassert (*tmp == '\"');
1731 while (*tmp != '\"' && *tmp != '\0')
1736 llassert (*tmp == '\"');
1740 DPRINTF (("fname: %s", fname));
1742 # if defined(OS2) || defined(MSDOS) || defined(WIN32)
1745 ** DOS-like path delimiters get delivered in pairs, something like
1746 ** \"..\\\\file.h\", so we have to make it normal again. We do NOT
1747 ** remove the pre dirs yet as we usually specify tmp paths relative
1748 ** to the current directory, so tmp files would not get found in
1749 ** the hash table. If this method fails we try it again later.
1756 ** Skip past the drive marker.
1759 DPRINTF (("stmp: %s / %s", stmp, fname));
1761 if (strchr (stmp, ':') != NULL)
1763 stmp = strchr (stmp, ':') + 1;
1766 DPRINTF (("stmp: %s / %s", stmp, fname));
1768 while ((stmp = strchr (stmp, CONNECTCHAR)) != NULL )
1770 if (*(stmp+1) == CONNECTCHAR)
1772 memmove (stmp, stmp+1, strlen (stmp));
1776 DPRINTF (("stmp: %s / %s", stmp, fname));
1779 DPRINTF (("Now: base = %s", fname));
1781 fid = fileTable_lookupBase (context_fileTable (),
1782 cstring_fromChars (fname));
1783 if (!(fileId_isValid (fid)))
1785 fname = removePreDirs (fname);
1786 fid = fileTable_lookupBase (context_fileTable (),
1787 cstring_fromChars (fname));
1790 # else /* !defined(OS2) && !defined(MSDOS) */
1791 fname = removePreDirs (fname);
1792 fid = fileTable_lookupBase (context_fileTable (),
1793 cstring_fromChars (fname));
1794 # endif /* !defined(OS2) && !defined(MSDOS) */
1796 if (!(fileId_isValid (fid)))
1798 if (isHeaderFile (cstring_fromChars (fname)))
1800 fid = fileTable_addHeaderFile (context_fileTable (),
1801 cstring_fromChars (fname));
1805 fid = fileTable_addFile (context_fileTable (),
1806 cstring_fromChars (fname));
1810 setFileLine (fid, lineno);
1812 else if ((sscanf (ol, "line %d", &lineno) == 1)
1813 || (sscanf (ol, " %d", &lineno) == 1))
1815 setLine (lineno); /* next line is <cr> */
1819 if (mstring_equal (ol, "")) {
1820 DPRINTF (("Empty pp command!"));
1822 ** evs 2000-05-16: This is a horrible kludge, to get around a bug (well, difficulty) in the pre-processor.
1823 ** We handle a plain # in the input file, by echoing it, and ignoring it in the post-pp-file.
1832 llbug (message ("File contains preprocessor command: #%s",
1833 cstring_fromChars (ol)));
1846 static int handleLlSpecial ()
1850 char *s = mstring_createEmpty ();
1855 while (((ic = ninput ()) != 0) && isalpha (ic))
1858 s = mstring_append (s, c);
1864 if (charsread == 0 && ic == (int) AFTER_COMMENT_MARKER[0])
1868 llassert (ic == AFTER_COMMENT_MARKER[1]);
1871 if (isProcessingGlobMods () && (*s == '\0'))
1874 return QNOMODS; /* special token no modifications token */
1882 tok = commentMarkerToken (cstring_fromChars (os));
1886 tokLength = charsread;
1892 /* Add rest of the comment */
1894 if (ic != 0 && ic != EOF)
1899 s = mstring_append (s, c);
1902 while (((ic = ninput ()) != 0) && (ic != EOF)
1903 && (ic != AFTER_COMMENT_MARKER[0]))
1906 s = mstring_append (s, c);
1911 if (ic == AFTER_COMMENT_MARKER[0])
1914 llassert ((char) nc == AFTER_COMMENT_MARKER[1]);
1921 while (*s == ' ' || *s == '\t' || *s == '\n')
1926 if (*s == '-' || *s == '+' || *s == '=') /* setting flags */
1930 while (c == '-' || c == '+' || c == '=')
1932 ynm set = ynm_fromCodeChar (c);
1937 thisflag = cstring_fromChars (s);
1939 while ((c = *s) != '\0' && (c != '-') && (c != '=')
1940 && (c != '+') && (c != ' ') && (c != '\t') && (c != '\n'))
1947 if (!context_getFlag (FLG_NOCOMMENTS))
1949 cstring flagname = thisflag;
1950 flagcode fflag = identifyFlag (flagname);
1952 if (flagcode_isSkip (fflag))
1956 else if (flagcode_isInvalid (fflag))
1958 if (isMode (flagname))
1960 if (ynm_isMaybe (set))
1965 ("Stylized comment attempts to restore flag %s. "
1966 "A mode flag cannot be restored.",
1971 context_setMode (flagname);
1978 message ("Unrecognized option in stylized comment: %s",
1982 else if (flagcode_isGlobalFlag (fflag))
1987 ("Stylized comment attempts to set global flag %s. "
1988 "A global flag cannot be set locally.",
1993 context_fileSetFlag (fflag, set);
1995 if (flagcode_hasArgument (fflag))
1997 if (ynm_isMaybe (set))
2002 ("Stylized comment attempts to restore flag %s. "
2003 "A flag for setting a value cannot be restored.",
2007 { /* cut-and-pastied from llmain...blecch */
2008 cstring extra = cstring_undefined;
2014 rest = mstring_copy (s);
2018 while ((rchar = *rest) != '\0'
2019 && (isspace (rchar)))
2025 while ((rchar = *rest) != '\0'
2026 && !isspace (rchar))
2028 extra = cstring_appendChar (extra, rchar);
2035 if (cstring_isUndefined (extra))
2040 ("Flag %s (in stylized comment) must be followed by an argument",
2041 flagcode_unparse (fflag)));
2047 if (flagcode_hasValue (fflag))
2049 setValueFlag (fflag, extra);
2051 else if (flagcode_hasString (fflag))
2053 setStringFlag (fflag, extra);
2070 while ((c == ' ') || (c == '\t') || (c == '\n'))
2076 if (context_inHeader () && !isArtificial (cstring_fromChars (os)))
2078 context_addComment (cstring_fromCharsNew (os));
2091 while (*s != '\0' && *s != ' ' && *s != '\t' && *s != '\n')
2103 t = cstring_toCharsSafe (cstring_downcase (cstring_fromChars (t)));
2104 macrocode = tokenMacroCode (cstring_fromChars (t));
2106 if (macrocode != BADTOK)
2108 tokLength = mstring_length (t);
2114 if (macrocode == SKIPTOK)
2122 if (context_inHeader ())
2129 if ((context_inMacro () || context_inGlobalContext ())
2130 && macrocode != SKIPTOK
2131 && !isArtificial (cstring_fromChars (os)))
2133 context_addComment (cstring_fromCharsNew (os));
2146 if (mstring_equal (t, "ignore"))
2148 if (!context_getFlag (FLG_NOCOMMENTS))
2150 context_enterSuppressRegion ();
2153 else if ((*t == 'i' || *t == 't')
2154 && (*(t + 1) == '\0'))
2156 if (!context_getFlag (FLG_NOCOMMENTS)
2157 && (*t == 'i' || context_getFlag (FLG_TMPCOMMENTS)))
2159 context_enterSuppressLine (-1); /* infinite suppression */
2162 else if (((*t == 'i') || (*t == 't'))
2163 && ((*(t + 1) >= '0' && *(t + 1) <= '9')))
2165 bool tmpcomment = (*t == 't');
2167 char *tt = t; /* don't mangle t, since it is free'd */
2170 if (lc >= '0' && lc <= '9')
2172 val = (int)(lc - '0');
2175 while (lc >= '0' && lc <= '9')
2184 if (!context_getFlag (FLG_NOCOMMENTS)
2185 && (!tmpcomment || context_getFlag (FLG_TMPCOMMENTS)))
2187 context_enterSuppressLine (val);
2190 else if (mstring_equal (t, "end"))
2192 if (!context_getFlag (FLG_NOCOMMENTS))
2194 context_exitSuppressRegion ();
2197 else if (mstring_equal (t, "notfunction"))
2199 ; /* handled by pcpp */
2201 else if (mstring_equal (t, "access"))
2207 while ((c = *s) && (c == ' ' || c == '\t' || c == '\n'))
2217 tname = cstring_fromChars (s);
2219 while ((c = *s) != '\0' && c != ' '
2220 && c != '\t' && c != '\n' && c != ',')
2228 if (!context_getFlag (FLG_NOCOMMENTS)
2229 && !context_getFlag (FLG_NOACCESS))
2231 if (usymtab_existsType (tname))
2233 usymId uid = usymtab_getTypeId (tname);
2234 context_addFileAccessType (uid);
2238 if (!(context_inSuppressRegion ()
2239 || context_inSuppressZone (g_currentloc)))
2243 ("%q: Unrecognized type %s used in access comment",
2244 fileloc_unparse (g_currentloc), tname));
2254 if (c != ',' && c != ' ')
2260 else if (mstring_equal (t, "noaccess"))
2267 while ((lc = *s) && (lc == ' ' || lc == '\t' || lc == '\n'))
2277 tname = cstring_fromChars (s);
2279 while ((lc = *s) != '\0' && lc != ' ' && lc != '\t'
2280 && lc != '\n' && lc != ',')
2287 if (!context_getFlag (FLG_NOCOMMENTS)
2288 && !context_getFlag (FLG_NOACCESS))
2290 if (usymtab_existsType (tname))
2292 typeId tuid = usymtab_getTypeId (tname);
2294 if (context_couldHaveAccess (tuid))
2296 context_removeFileAccessType (tuid);
2300 if (!(context_inSuppressRegion ()
2301 || context_inSuppressZone (g_currentloc)))
2303 uentry ue = usymtab_getTypeEntry (tuid);
2305 if (uentry_isAbstractDatatype (ue))
2309 ("%q: Non-accessible abstract type %s used in noaccess comment",
2310 fileloc_unparse (g_currentloc), tname));
2316 ("%q: Non-abstract type %s used in noaccess comment",
2317 fileloc_unparse (g_currentloc), tname));
2324 if (!(context_inSuppressRegion ()
2325 || context_inSuppressZone (g_currentloc)))
2329 ("%q: Unrecognized type %s used in noaccess comment",
2330 fileloc_unparse (g_currentloc), tname));
2340 if (lc != ',' && lc != ' ')
2348 setTokLength (- (2 + charsread));
2350 voptgenerror (FLG_UNRECOGCOMMENTS,
2351 message ("Stylized comment unrecognized: %s",
2352 cstring_fromChars (os)),
2363 static /*@only@*/ cstring makeIdentifier (char *s)
2365 char *c = mstring_create (size_toInt (strlen (s)) + 1);
2366 cstring id = cstring_fromChars (c);
2368 while (isalnum (*s) || (*s == '_') || (*s == '$'))
2377 /*@observer@*/ /*@dependent@*/ uentry coerceId (cstring cn)
2379 if (!(usymtab_exists (cn)))
2381 fileloc loc = fileloc_createExternal ();
2384 ** We need to put this in a global scope, otherwise the sRef will be deallocated.
2387 uentry ce = uentry_makeUnrecognized (cn, loc);
2389 if (!context_inIterEnd ())
2393 message ("Unrecognized (possibly system) identifier: %q",
2394 uentry_getName (ce)),
2401 return (usymtab_lookup (cn));
2405 ** like, coerceId, but doesn't supercede for iters
2408 /*@observer@*/ uentry coerceIterId (cstring cn)
2410 if (!(usymtab_exists (cn)))
2412 return uentry_undefined;
2415 return (usymtab_lookup (cn));
2418 /*@observer@*/ cstring LastIdentifier ()
2420 return (lastidprocessed);
2423 static int processIdentifier (cstring id)
2427 DPRINTF (("Process identifier: %s", id));
2429 context_clearJustPopped ();
2430 lastidprocessed = id;
2432 if (context_inFunctionDecl ())
2434 int tok = commentMarkerToken (id);
2442 tok = tokenMacroCode (id);
2451 /* Consider handling: Defined by C99 as static const char __func__[] */
2453 if (context_getFlag (FLG_GNUEXTENSIONS))
2457 if (cstring_equalLit (id, "__stdcall")
2458 || cstring_equalLit (id, "__cdecl")
2459 || cstring_equalLit (id, "__extension__"))
2463 else if (cstring_equalLit (id, "__volatile__"))
2467 else if (cstring_equalLit (id, "__signed"))
2471 else if (cstring_equalLit (id, "__unsigned"))
2475 else if (cstring_equalLit (id, "__const__"))
2479 else if (cstring_equalLit (id, "__alignof__"))
2481 tok = CALIGNOF; /* alignof is parsed like sizeof */
2483 else if (cstring_equalLit (id, "__FUNCTION__")
2484 || cstring_equalLit (id, "__PRETTY_FUNCTION__"))
2486 /* These tokens hold the name of the current function as strings */
2487 yylval.expr = exprNode_stringLiteral (id, fileloc_copy (g_currentloc));
2489 lastWasString = TRUE;
2493 else if (cstring_equalLit (id, "__attribute__")
2494 || cstring_equalLit (id, "__asm__")
2495 || cstring_equalLit (id, "_asm")
2496 || cstring_equalLit (id, "__asm")
2497 || cstring_equalLit (id, "__declspec"))
2500 bool useparens = FALSE;
2501 bool usebraces = FALSE;
2502 bool inquote = FALSE;
2503 bool inescape = FALSE;
2506 while ((ic = input ()) != EOF)
2513 else if (ic == '\\')
2517 else if (ic == '\"')
2553 else if (ic == ')' && useparens)
2556 if (depth == 0) break;
2558 else if (ic == '}' && usebraces)
2561 if (depth == 0) break;
2564 && !usebraces && !useparens
2565 && cstring_equalLit (id, "__asm"))
2568 ** We need this because some MS VC++ include files
2569 ** have __asm mov ... }
2570 ** Its a kludge, but otherwise would need to parse
2579 context_incLineno ();
2581 if (cstring_equalLit (id, "__asm")
2582 && !useparens && !usebraces)
2589 llassert ((useparens && ic == ')')
2590 || (usebraces && ic == '}')
2591 || (!useparens && !usebraces));
2595 else if (cstring_equalLit (id, "inline")
2596 || cstring_equalLit (id, "__inline")
2597 || cstring_equalLit (id, "_inline")
2598 || cstring_equalLit (id, "__inline__"))
2609 le = usymtab_lookupSafe (id);
2611 /*@-dependenttrans@*/
2613 if (uentry_isIter (le))
2618 else if (uentry_isEndIter (le))
2621 return (ITER_ENDNAME);
2623 else if (uentry_isUndefined (le))
2627 /* avoid parse errors for certain system built ins */
2629 if (g_expectingTypeName && (cstring_firstChar (id) == '_')
2630 && (cstring_secondChar (id) == '_'))
2632 return (TYPE_NAME_OR_ID);
2635 return (NEW_IDENTIFIER);
2637 else if (!uentry_isDeclared (le) && !uentry_isCodeDefined (le))
2639 if (uentry_isDatatype (le))
2642 return (NEW_IDENTIFIER);
2647 return (IDENTIFIER);
2650 else if (uentry_isDatatype (le))
2652 if (!g_expectingTypeName)
2656 return (NEW_IDENTIFIER);
2660 yylval.ctyp = uentry_getAbstractType (le);
2662 uentry_setUsed (le, g_currentloc);
2669 return (IDENTIFIER);
2672 /*@=dependenttrans@*/
2675 static bool processHashIdentifier (/*@only@*/ cstring id)
2677 if (context_inMacro () || context_inIterDef () ||
2678 context_inIterEnd ())
2682 context_clearJustPopped ();
2684 lastidprocessed = id;
2685 le = usymtab_lookupSafe (id);
2687 if (uentry_isParam (le) || uentry_isRefParam (le))
2704 static /*@only@*/ exprNode processString ()
2708 char *nl = strchr (yytext, '\n');
2709 cstring ns = cstring_fromCharsNew (yytext);
2713 loc = fileloc_copy (g_currentloc);
2714 addColumn (cstring_length (ns));
2720 loc = fileloc_copy (g_currentloc);
2722 context_incLineno ();
2724 while ((nl = strchr ((nl + 1), '\n')) != NULL)
2726 context_incLineno ();
2732 res = exprNode_stringLiteral (ns, loc);
2742 llassert (*yytext != '\0');
2743 fchar = *(yytext + 1);
2744 if (fchar != '\\') return fchar;
2746 next = *(yytext + 2);
2750 case 'n': return '\n';
2751 case 't': return '\t';
2752 case '\"': return '\"';
2753 case '\'': return '\'';
2754 case '\\': return '\\';
2755 default: return '\0';
2760 double processFloat ()
2762 double ret = atof (yytext);
2773 llassert (yytext[0] == '0'
2774 && (yytext[1] == 'X' || yytext[1] == 'x'));
2776 while (yytext[index] != '\0') {
2778 char c = yytext[index];
2780 if (c >= '0' && c <= '9') {
2781 tval = (int) c - (int) '0';
2782 } else if (c >= 'A' && c <= 'F') {
2783 tval = (int) c - (int) 'A' + 10;
2784 } else if (c >= 'a' && c <= 'f') {
2785 tval = (int) c - (int) 'a' + 10;
2786 } else if (c == 'U' || c == 'L' || c == 'u' || c == 'l') {
2788 while (yytext[index] != '\0') {
2789 if (c == 'U' || c == 'L' || c == 'u' || c == 'l') {
2794 message ("Invalid character (%c) following specifier in hex constant: %s",
2795 c, cstring_fromChars (yytext)),
2805 message ("Invalid character (%c) in hex constant: %s",
2806 c, cstring_fromChars (yytext)),
2811 val = (val * 16) + tval;
2815 DPRINTF (("Hex constant: %s = %ld", yytext, val));
2820 long processOctal ()
2825 llassert (yytext[0] == '0' && yytext[1] != 'X' && yytext[1] != 'x');
2827 while (yytext[index] != '\0') {
2829 char c = yytext[index];
2831 if (c >= '0' && c <= '7') {
2832 tval = (int) c - (int) '0';
2836 message ("Invalid character (%c) in octal constant: %s",
2837 c, cstring_fromChars (yytext)),
2842 val = (val * 8) + tval;
2846 DPRINTF (("Octal constant: %s = %ld", yytext, val));
2853 return (atol (yytext));
2857 processSpec (int tok)
2859 size_t length = strlen (yytext);
2864 setTokLengthT (length);
2870 context_saveLocation ();
2871 setTokLengthT (length);
2872 return (processIdentifier (makeIdentifier (yytext)));