static int CPPifdef(int defined, yystypepp * yylvalpp) { int token = cpp->currentInput->scan(cpp->currentInput, yylvalpp); int name = yylvalpp->sc_ident; if (++cpp->ifdepth > MAX_IF_NESTING) { CPPErrorToInfoLog("max #if nesting depth exceeded"); return 0; } cpp->elsetracker++; cpp->elsedepth[cpp->elsetracker] = 0; if (token != CPP_IDENTIFIER) { defined ? CPPErrorToInfoLog("ifdef") : CPPErrorToInfoLog("ifndef"); } else { Symbol *s = LookUpSymbol(macros, name); token = cpp->currentInput->scan(cpp->currentInput, yylvalpp); if (token != '\n') { CPPWarningToInfoLog( "unexpected tokens following #ifdef preprocessor directive - expected a newline"); while (token != '\n') token = cpp->currentInput->scan(cpp->currentInput, yylvalpp); } if (((s && !s->details.mac.undef) ? 1 : 0) != defined) { token = CPPelse(1, yylvalpp); } } return token; } /* CPPifdef */
static int CPPif(yystypepp * yylvalpp) { int token = cpp->currentInput->scan(cpp->currentInput, yylvalpp); int res = 0, err = 0; cpp->elsetracker++; cpp->elsedepth[cpp->elsetracker] = 0; if (!cpp->ifdepth++) ifloc = *cpp->tokenLoc; if (cpp->ifdepth > MAX_IF_NESTING) { CPPErrorToInfoLog("max #if nesting depth exceeded"); return 0; } token = eval(token, MIN_PREC, &res, &err, yylvalpp); if (token != '\n') { CPPWarningToInfoLog( "unexpected tokens following the preprocessor directive - expected a newline"); while (token != '\n') token = cpp->currentInput->scan(cpp->currentInput, yylvalpp); } if (!res && !err) { token = CPPelse(1, yylvalpp); } return token; } /* CPPif */
static int CPPelse(int matchelse, yystypepp * yylvalpp) { int atom,depth=0; int token = cpp->currentInput->scan(cpp->currentInput, yylvalpp); while (token > 0) { if (token != '#') { while (token != '\n') token = cpp->currentInput->scan(cpp->currentInput, yylvalpp); token = cpp->currentInput->scan(cpp->currentInput, yylvalpp); continue; } if ((token = cpp->currentInput->scan(cpp->currentInput, yylvalpp)) != CPP_IDENTIFIER) continue; atom = yylvalpp->sc_ident; if (atom == ifAtom || atom == ifdefAtom || atom == ifndefAtom){ depth++; cpp->ifdepth++; cpp->elsetracker++; cpp->elsedepth[cpp->elsetracker] = 0; } else if (atom == endifAtom) { if(--depth<0){ --cpp->elsetracker; if (cpp->ifdepth) --cpp->ifdepth; break; } --cpp->elsetracker; --cpp->ifdepth; } else if (((int)(matchelse) != 0)&& depth==0) { if (atom == elseAtom ) { token = cpp->currentInput->scan(cpp->currentInput, yylvalpp); if (token != '\n') { CPPWarningToInfoLog("unexpected tokens following #else preprocessor directive - expected a newline"); while (token != '\n') token = cpp->currentInput->scan(cpp->currentInput, yylvalpp); } break; } else if (atom == elifAtom) { /* we decrement cpp->ifdepth here, because CPPif will increment * it and we really want to leave it alone */ if (cpp->ifdepth){ --cpp->ifdepth; --cpp->elsetracker; } return CPPif(yylvalpp); } } else if((atom==elseAtom) && (!ChkCorrectElseNesting())){ CPPErrorToInfoLog("#else after a #else"); cpp->CompileError=1; } }; return token; }
static int CPPifdef(int defined, yystypepp * yylvalpp) { int token = cpp->currentInput->scan(cpp->currentInput, yylvalpp); int name = yylvalpp->sc_ident; if(++cpp->ifdepth > MAX_IF_NESTING){ CPPErrorToInfoLog("max #if nesting depth exceeded"); cpp->CompileError = 1; return 0; } cpp->elsetracker++; // sanity check elsetracker if (cpp->elsetracker < 0 || cpp->elsetracker >= MAX_IF_NESTING) { CPPErrorToInfoLog("mismatched #if/#endif statements"); cpp->CompileError = 1; return 0; } cpp->elsedepth[cpp->elsetracker] = 0; if (token != CPP_IDENTIFIER) { defined ? CPPErrorToInfoLog("ifdef"):CPPErrorToInfoLog("ifndef"); } else { Symbol *s = LookUpSymbol(macros, name); token = cpp->currentInput->scan(cpp->currentInput, yylvalpp); if (token != '\n') { CPPWarningToInfoLog("unexpected tokens following #ifdef preprocessor directive - expected a newline"); while (token != '\n') { token = cpp->currentInput->scan(cpp->currentInput, yylvalpp); if (token <= 0) { // EOF or error CPPErrorToInfoLog("unexpected end of input in #ifdef preprocessor directive - expected a newline"); return 0; } } } if (((s && !s->details.mac.undef) ? 1 : 0) != defined) token = CPPelse(1, yylvalpp); } return token; } // CPPifdef
static int CPPif(yystypepp * yylvalpp) { int token = cpp->currentInput->scan(cpp->currentInput, yylvalpp); int res = 0, err = 0; if (!cpp->ifdepth++) ifloc = *cpp->tokenLoc; if(cpp->ifdepth > MAX_IF_NESTING){ CPPErrorToInfoLog("max #if nesting depth exceeded"); cpp->CompileError = 1; return 0; } cpp->elsetracker++; // sanity check elsetracker if (cpp->elsetracker < 0 || cpp->elsetracker >= MAX_IF_NESTING) { CPPErrorToInfoLog("mismatched #if/#endif statements"); cpp->CompileError = 1; return 0; } cpp->elsedepth[cpp->elsetracker] = 0; token = eval(token, MIN_PREC, &res, &err, yylvalpp); if (token != '\n') { CPPWarningToInfoLog("unexpected tokens following #if preprocessor directive - expected a newline"); while (token != '\n') { token = cpp->currentInput->scan(cpp->currentInput, yylvalpp); if (token <= 0) { // EOF or error CPPErrorToInfoLog("unexpected end of input in #if preprocessor directive - expected a newline"); return 0; } } } if (!res && !err) { token = CPPelse(1, yylvalpp); } return token; } // CPPif
static int CPPgotoEndOfIfBlock(int matchelse, yystypepp * yylvalpp) { int atom; int token = cpp->currentInput->scan(cpp->currentInput, yylvalpp); while (token > 0) { // jump to next line beginning with a # if (token != '#') { while (token != '\n') token = cpp->currentInput->scan(cpp->currentInput, yylvalpp); token = cpp->currentInput->scan(cpp->currentInput, yylvalpp); continue; } if ((token = cpp->currentInput->scan(cpp->currentInput, yylvalpp)) != CPP_IDENTIFIER) continue; atom = yylvalpp->sc_ident; if (atom == ifAtom || atom == ifdefAtom || atom == ifndefAtom){ // recursivelly ignore this imbricated if(def) block CPPgotoEndOfIfBlock(0, yylvalpp); assert(yylvalpp->sc_ident == endifAtom); token = cpp->currentInput->scan(cpp->currentInput, yylvalpp); if (token != '\n') { CPPWarningToInfoLog("unexpected tokens following #else preprocessor directive - expected a newline"); while (token != '\n') token = cpp->currentInput->scan(cpp->currentInput, yylvalpp); } continue; } else if (atom == endifAtom) break; else if (matchelse && (atom == elseAtom || atom == elifAtom)) break; } return token; }
int readCPPline(yystypepp * yylvalpp) { int token = cpp->currentInput->scan(cpp->currentInput, yylvalpp); const char *message; if (token == CPP_IDENTIFIER) { if (yylvalpp->sc_ident == defineAtom) { token = CPPdefine(yylvalpp); } else if (yylvalpp->sc_ident == elseAtom) { if(ChkCorrectElseNesting()){ if (!cpp->ifdepth ){ CPPErrorToInfoLog("#else mismatch"); cpp->CompileError=1; return 0; } token = cpp->currentInput->scan(cpp->currentInput, yylvalpp); if (token != '\n') { CPPWarningToInfoLog("unexpected tokens following #else preprocessor directive - expected a newline"); while (token != '\n') { token = cpp->currentInput->scan(cpp->currentInput, yylvalpp); if (token <= 0) { // EOF or error CPPErrorToInfoLog("unexpected end of input in #ifdef preprocessor directive - expected a newline"); return 0; } } } token = CPPelse(0, yylvalpp); }else{ CPPErrorToInfoLog("#else after a #else"); cpp->ifdepth = 0; cpp->elsetracker = 0; cpp->pastFirstStatement = 1; cpp->CompileError = 1; return 0; } } else if (yylvalpp->sc_ident == elifAtom) { if (!cpp->ifdepth){ CPPErrorToInfoLog("#elif mismatch"); cpp->CompileError=1; return 0; } // this token is really a dont care, but we still need to eat the tokens token = cpp->currentInput->scan(cpp->currentInput, yylvalpp); while (token != '\n') { token = cpp->currentInput->scan(cpp->currentInput, yylvalpp); if (token <= 0) { // EOF or error CPPErrorToInfoLog("unexpect tokens following #elif preprocessor directive - expected a newline"); cpp->CompileError = 1; return 0; } } token = CPPelse(0, yylvalpp); } else if (yylvalpp->sc_ident == endifAtom) { if (!cpp->ifdepth){ CPPErrorToInfoLog("#endif mismatch"); cpp->CompileError=1; return 0; } else --cpp->ifdepth; if (cpp->elsetracker) --cpp->elsetracker; } else if (yylvalpp->sc_ident == ifAtom) { token = CPPif(yylvalpp); } else if (yylvalpp->sc_ident == ifdefAtom) { token = CPPifdef(1, yylvalpp); } else if (yylvalpp->sc_ident == ifndefAtom) { token = CPPifdef(0, yylvalpp); } else if (yylvalpp->sc_ident == lineAtom) { token = CPPline(yylvalpp); } else if (yylvalpp->sc_ident == pragmaAtom) { token = CPPpragma(yylvalpp); } else if (yylvalpp->sc_ident == undefAtom) { token = CPPundef(yylvalpp); } else if (yylvalpp->sc_ident == errorAtom) { token = CPPerror(yylvalpp); } else if (yylvalpp->sc_ident == versionAtom) { token = CPPversion(yylvalpp); } else if (yylvalpp->sc_ident == extensionAtom) { token = CPPextension(yylvalpp); } else { StoreStr("Invalid Directive"); StoreStr(GetStringOfAtom(atable,yylvalpp->sc_ident)); message=GetStrfromTStr(); CPPShInfoLogMsg(message); ResetTString(); } } while (token != '\n' && token != 0 && token != EOF) { token = cpp->currentInput->scan(cpp->currentInput, yylvalpp); } cpp->pastFirstStatement = 1; return token; } // readCPPline
int readCPPline(yystypepp * yylvalpp) { int token = cpp->currentInput->scan(cpp->currentInput, yylvalpp); const char *message; int isVersion = 0; if (token == CPP_IDENTIFIER) { if (yylvalpp->sc_ident == defineAtom) { token = CPPdefine(yylvalpp); } else if (yylvalpp->sc_ident == elseAtom) { token = CPPgotoEndOfIfBlock(1, yylvalpp); if (yylvalpp->sc_ident != endifAtom) { CPPErrorToInfoLog("#else or #elif mismatch"); cpp->CompileError=1; } token = cpp->currentInput->scan(cpp->currentInput, yylvalpp); if (token != '\n') CPPWarningToInfoLog("unexpected tokens following #else preprocessor directive - expected a newline"); if (!cpp->ifdepth){ CPPErrorToInfoLog("#else or #elif mismatch"); cpp->CompileError=1; } else --cpp->ifdepth; } else if (yylvalpp->sc_ident == elifAtom) { token = CPPgotoEndOfIfBlock(0, yylvalpp); assert(yylvalpp->sc_ident == endifAtom); token = cpp->currentInput->scan(cpp->currentInput, yylvalpp); if (!cpp->ifdepth){ CPPErrorToInfoLog("#else or #elif mismatch"); cpp->CompileError=1; } else --cpp->ifdepth; } else if (yylvalpp->sc_ident == endifAtom) { if (!cpp->ifdepth){ CPPErrorToInfoLog("#endif mismatch"); cpp->CompileError=1; } else --cpp->ifdepth; } else if (yylvalpp->sc_ident == ifAtom) { token = CPPif(yylvalpp); } else if (yylvalpp->sc_ident == ifdefAtom) { token = CPPifdef(1, yylvalpp); } else if (yylvalpp->sc_ident == ifndefAtom) { token = CPPifdef(0, yylvalpp); } else if (yylvalpp->sc_ident == lineAtom) { token = CPPline(yylvalpp); } else if (yylvalpp->sc_ident == pragmaAtom) { token = CPPpragma(yylvalpp); } else if (yylvalpp->sc_ident == undefAtom) { token = CPPundef(yylvalpp); } else if (yylvalpp->sc_ident == errorAtom) { token = CPPerror(yylvalpp); } else if (yylvalpp->sc_ident == versionAtom) { token = CPPversion(yylvalpp); isVersion = 1; } else { StoreStr("Invalid Directive"); StoreStr(GetStringOfAtom(atable,yylvalpp->sc_ident)); message=GetStrfromTStr(); CPPShInfoLogMsg(message); ResetTString(); } } while (token != '\n' && token != 0 && token != EOF) { token = cpp->currentInput->scan(cpp->currentInput, yylvalpp); } cpp->notAVersionToken = !isVersion; return token; } // readCPPline