From 85e0e02f6f02ca2ccfe4941270201066451eb2d5 Mon Sep 17 00:00:00 2001 From: John Kessenich Date: Wed, 6 Feb 2013 02:23:47 +0000 Subject: [PATCH] Fix a preprocessor defect, where nested dead #if-#endif had nesting depth off by one level, turning what should be dead code into live code. git-svn-id: https://cvs.khronos.org/svn/repos/ogl/trunk/ecosystem/public/sdk/tools/glslang@20461 e7fa87d3-cd2b-0410-9028-fcbf551c1848 --- Test/cppNest.vert | 10 +++ glslang/MachineIndependent/preprocessor/cpp.c | 68 ++++++++++--------- 2 files changed, 47 insertions(+), 31 deletions(-) diff --git a/Test/cppNest.vert b/Test/cppNest.vert index 1613db22..0bc97dde 100644 --- a/Test/cppNest.vert +++ b/Test/cppNest.vert @@ -38,6 +38,16 @@ sum += 50000.0; sum += 0.2; #endif + //no + sum += 0.01; + #ifdef ON + //no + sum += 0.02; + #else + //no + sum += 0.03; + #endif + //no sum + 0.3; diff --git a/glslang/MachineIndependent/preprocessor/cpp.c b/glslang/MachineIndependent/preprocessor/cpp.c index 83bb4b3a..87687647 100644 --- a/glslang/MachineIndependent/preprocessor/cpp.c +++ b/glslang/MachineIndependent/preprocessor/cpp.c @@ -295,65 +295,71 @@ static int CPPundef(yystypepp * yylvalpp) } // CPPundef /* CPPelse -- skip forward to appropriate spot. This is actually used -** to skip to and #endif after seeing an #else, AND to skip to a #else, +** to skip to a #endif after seeing an #else, AND to skip to a #else, ** #elif, or #endif after a #if/#ifdef/#ifndef/#elif test was false */ static int CPPelse(int matchelse, yystypepp * yylvalpp) { - int atom,depth=0; + int atom; + int depth = 0; int token = cpp->currentInput->scan(cpp->currentInput, yylvalpp); - - while (token > 0) { + + while (token > 0) { if (token != '#') { - while (token != '\n') + 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; + + 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++; - } - else if (atom == endifAtom) { - if(--depth<=0){ - cpp->elsedepth[cpp->elsetracker]=0; - --cpp->elsetracker; + if (atom == ifAtom || atom == ifdefAtom || atom == ifndefAtom) { + depth++; + cpp->ifdepth++; + cpp->elsetracker++; + } else if (atom == endifAtom) { + if (depth == 0) { + // found the #endif we are looking for + cpp->elsedepth[cpp->elsetracker] = 0; + --cpp->elsetracker; if (cpp->ifdepth) --cpp->ifdepth; break; - } - --cpp->elsetracker; - --cpp->ifdepth; } - else if (((int)(matchelse) != 0)&& depth==0) { - if (atom == elseAtom ) { + --depth; + --cpp->elsetracker; + --cpp->ifdepth; + } else if (matchelse && depth == 0) { + if (atom == elseAtom ) { + // found the #else we are looking for 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) { + 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; - } + * it and we really want to leave it alone */ + if (cpp->ifdepth) { + --cpp->ifdepth; + --cpp->elsetracker; + } + return CPPif(yylvalpp); } - } - else if((atom==elseAtom) && (!ChkCorrectElseNesting())){ + } else if((atom == elseAtom) && (!ChkCorrectElseNesting())) { CPPErrorToInfoLog("#else after a #else"); cpp->CompileError=1; } - }; + }; // end while + return token; }