From e7e081bda9a4f4dfeb7fc96d9f222e58df209d63 Mon Sep 17 00:00:00 2001 From: John Kessenich Date: Mon, 19 Mar 2018 00:43:18 -0600 Subject: [PATCH] PP: don't give errors on some tokens under #if 0 (or similar). Fixes #1295. Tokens that are accepted by any version of HLSL or GLSL should be allowed when #ifdef'd off, such that errors are not reported. --- .../cppRelaxSkipTokensErrors.vert.out | 14 ++++ Test/cppBad.vert | 2 +- Test/cppRelaxSkipTokensErrors.vert | 14 ++++ .../preprocessor/PpScanner.cpp | 71 +++++++++++-------- gtests/AST.FromFile.cpp | 1 + 5 files changed, 71 insertions(+), 31 deletions(-) create mode 100755 Test/baseResults/cppRelaxSkipTokensErrors.vert.out create mode 100644 Test/cppRelaxSkipTokensErrors.vert diff --git a/Test/baseResults/cppRelaxSkipTokensErrors.vert.out b/Test/baseResults/cppRelaxSkipTokensErrors.vert.out new file mode 100755 index 000000000..e9b4b1b7a --- /dev/null +++ b/Test/baseResults/cppRelaxSkipTokensErrors.vert.out @@ -0,0 +1,14 @@ +cppRelaxSkipTokensErrors.vert +Shader version: 110 +0:? Sequence +0:? Linker Objects + + +Linked vertex stage: + +ERROR: Linking vertex stage: Missing entry point: Each stage requires one entry point + +Shader version: 110 +0:? Sequence +0:? Linker Objects + diff --git a/Test/cppBad.vert b/Test/cppBad.vert index 49600f916..0044c44fc 100644 --- a/Test/cppBad.vert +++ b/Test/cppBad.vert @@ -1,5 +1,5 @@ #define m#0# #if m - +#endif #define n() int n" \ No newline at end of file diff --git a/Test/cppRelaxSkipTokensErrors.vert b/Test/cppRelaxSkipTokensErrors.vert new file mode 100644 index 000000000..b30af0c9f --- /dev/null +++ b/Test/cppRelaxSkipTokensErrors.vert @@ -0,0 +1,14 @@ +#version 110 + +#if 0 +3.5L +3.5h +2034h +1.#INF +0x1234567812345L +12323394203923879234L +0123s; +123s; +0123456712345671234L +"string" +#endif diff --git a/glslang/MachineIndependent/preprocessor/PpScanner.cpp b/glslang/MachineIndependent/preprocessor/PpScanner.cpp index 61bf027fa..f4eaf57d3 100644 --- a/glslang/MachineIndependent/preprocessor/PpScanner.cpp +++ b/glslang/MachineIndependent/preprocessor/PpScanner.cpp @@ -118,7 +118,7 @@ int TPpContext::lFloatConst(int len, int ch, TPpToken* ppToken) ch = getChar(); // 1.#INF or -1.#INF - if (parseContext.intermediate.getSource() == EShSourceHlsl && ch == '#') { + if (ch == '#' && (ifdepth > 0 || parseContext.intermediate.getSource() == EShSourceHlsl)) { if ((len < 2) || (len == 2 && ppToken->name[0] != '1') || (len == 3 && ppToken->name[1] != '1' && !(ppToken->name[0] == '-' || ppToken->name[0] == '+')) || @@ -174,9 +174,9 @@ int TPpContext::lFloatConst(int len, int ch, TPpToken* ppToken) // Suffix: bool isFloat16 = false; if (ch == 'l' || ch == 'L') { - if (parseContext.intermediate.getSource() == EShSourceGlsl) + if (ifdepth == 0 && parseContext.intermediate.getSource() == EShSourceGlsl) parseContext.doubleCheck(ppToken->loc, "double floating-point suffix"); - if (! HasDecimalOrExponent) + if (ifdepth == 0 && !HasDecimalOrExponent) parseContext.ppError(ppToken->loc, "float literal needs a decimal point or exponent", "", ""); if (parseContext.intermediate.getSource() == EShSourceGlsl) { int ch2 = getChar(); @@ -193,9 +193,9 @@ int TPpContext::lFloatConst(int len, int ch, TPpToken* ppToken) isDouble = 1; } } else if (ch == 'h' || ch == 'H') { - if (parseContext.intermediate.getSource() == EShSourceGlsl) + if (ifdepth == 0 && parseContext.intermediate.getSource() == EShSourceGlsl) parseContext.float16Check(ppToken->loc, "half floating-point suffix"); - if (!HasDecimalOrExponent) + if (ifdepth == 0 && !HasDecimalOrExponent) parseContext.ppError(ppToken->loc, "float literal needs a decimal point or exponent", "", ""); if (parseContext.intermediate.getSource() == EShSourceGlsl) { int ch2 = getChar(); @@ -212,10 +212,11 @@ int TPpContext::lFloatConst(int len, int ch, TPpToken* ppToken) isFloat16 = true; } } else if (ch == 'f' || ch == 'F') { - parseContext.profileRequires(ppToken->loc, EEsProfile, 300, nullptr, "floating-point suffix"); - if (! parseContext.relaxedErrors()) + if (ifdepth == 0) + parseContext.profileRequires(ppToken->loc, EEsProfile, 300, nullptr, "floating-point suffix"); + if (ifdepth == 0 && !parseContext.relaxedErrors()) parseContext.profileRequires(ppToken->loc, ~EEsProfile, 120, nullptr, "floating-point suffix"); - if (! HasDecimalOrExponent) + if (ifdepth == 0 && !HasDecimalOrExponent) parseContext.ppError(ppToken->loc, "float literal needs a decimal point or exponent", "", ""); saveName(ch); } else @@ -483,18 +484,22 @@ int TPpContext::tStringInput::scan(TPpToken* ppToken) ppToken->name[len] = '\0'; if (isInt64 && pp->parseContext.intermediate.getSource() == EShSourceGlsl) { - pp->parseContext.requireProfile(ppToken->loc, ~EEsProfile, + if (pp->ifdepth == 0) { + pp->parseContext.requireProfile(ppToken->loc, ~EEsProfile, "64-bit hexadecimal literal"); - pp->parseContext.profileRequires(ppToken->loc, ~EEsProfile, 0, - Num_Int64_Extensions, Int64_Extensions, "64-bit hexadecimal literal"); + pp->parseContext.profileRequires(ppToken->loc, ~EEsProfile, 0, + Num_Int64_Extensions, Int64_Extensions, "64-bit hexadecimal literal"); + } ppToken->i64val = ival; return isUnsigned ? PpAtomConstUint64 : PpAtomConstInt64; } else if (isInt16) { - if (pp->parseContext.intermediate.getSource() == EShSourceGlsl) { - pp->parseContext.requireProfile(ppToken->loc, ~EEsProfile, - "16-bit hexadecimal literal"); - pp->parseContext.profileRequires(ppToken->loc, ~EEsProfile, 0, - Num_Int16_Extensions, Int16_Extensions, "16-bit hexadecimal literal"); + if (pp->ifdepth == 0) { + if (pp->parseContext.intermediate.getSource() == EShSourceGlsl) { + pp->parseContext.requireProfile(ppToken->loc, ~EEsProfile, + "16-bit hexadecimal literal"); + pp->parseContext.profileRequires(ppToken->loc, ~EEsProfile, 0, + Num_Int16_Extensions, Int16_Extensions, "16-bit hexadecimal literal"); + } } ppToken->ival = (int)ival; return isUnsigned ? PpAtomConstUint16 : PpAtomConstInt16; @@ -595,18 +600,22 @@ int TPpContext::tStringInput::scan(TPpToken* ppToken) pp->parseContext.ppError(ppToken->loc, "octal literal too big", "", ""); if (isInt64 && pp->parseContext.intermediate.getSource() == EShSourceGlsl) { - pp->parseContext.requireProfile(ppToken->loc, ~EEsProfile, + if (pp->ifdepth == 0) { + pp->parseContext.requireProfile(ppToken->loc, ~EEsProfile, "64-bit octal literal"); - pp->parseContext.profileRequires(ppToken->loc, ~EEsProfile, 0, - Num_Int64_Extensions, Int64_Extensions, "64-bit octal literal"); + pp->parseContext.profileRequires(ppToken->loc, ~EEsProfile, 0, + Num_Int64_Extensions, Int64_Extensions, "64-bit octal literal"); + } ppToken->i64val = ival; return isUnsigned ? PpAtomConstUint64 : PpAtomConstInt64; } else if (isInt16) { - if (pp->parseContext.intermediate.getSource() == EShSourceGlsl) { - pp->parseContext.requireProfile(ppToken->loc, ~EEsProfile, - "16-bit octal literal"); - pp->parseContext.profileRequires(ppToken->loc, ~EEsProfile, 0, - Num_Int16_Extensions, Int16_Extensions, "16-bit octal literal"); + if (pp->ifdepth == 0) { + if (pp->parseContext.intermediate.getSource() == EShSourceGlsl) { + pp->parseContext.requireProfile(ppToken->loc, ~EEsProfile, + "16-bit octal literal"); + pp->parseContext.profileRequires(ppToken->loc, ~EEsProfile, 0, + Num_Int16_Extensions, Int16_Extensions, "16-bit octal literal"); + } } ppToken->ival = (int)ival; return isUnsigned ? PpAtomConstUint16 : PpAtomConstInt16; @@ -700,16 +709,18 @@ int TPpContext::tStringInput::scan(TPpToken* ppToken) } if (isInt64 && pp->parseContext.intermediate.getSource() == EShSourceGlsl) { - pp->parseContext.requireProfile(ppToken->loc, ~EEsProfile, + if (pp->ifdepth == 0) { + pp->parseContext.requireProfile(ppToken->loc, ~EEsProfile, "64-bit literal"); - pp->parseContext.profileRequires(ppToken->loc, ~EEsProfile, 0, - Num_Int64_Extensions, Int64_Extensions, "64-bit literal"); + pp->parseContext.profileRequires(ppToken->loc, ~EEsProfile, 0, + Num_Int64_Extensions, Int64_Extensions, "64-bit literal"); + } ppToken->i64val = ival; return isUnsigned ? PpAtomConstUint64 : PpAtomConstInt64; } else if (isInt16) { - if (pp->parseContext.intermediate.getSource() == EShSourceGlsl) { + if (pp->ifdepth == 0 && pp->parseContext.intermediate.getSource() == EShSourceGlsl) { pp->parseContext.requireProfile(ppToken->loc, ~EEsProfile, - "16-bit literal"); + "16-bit literal"); pp->parseContext.profileRequires(ppToken->loc, ~EEsProfile, 0, Num_Int16_Extensions, Int16_Extensions, "16-bit literal"); } @@ -972,7 +983,7 @@ int TPpContext::tokenize(TPpToken& ppToken) continue; break; case PpAtomConstString: - if (parseContext.intermediate.getSource() != EShSourceHlsl) { + if (ifdepth == 0 && parseContext.intermediate.getSource() != EShSourceHlsl) { // HLSL allows string literals. parseContext.ppError(ppToken.loc, "string literals not supported", "\"\"", ""); continue; diff --git a/gtests/AST.FromFile.cpp b/gtests/AST.FromFile.cpp index fea758633..061c86c30 100644 --- a/gtests/AST.FromFile.cpp +++ b/gtests/AST.FromFile.cpp @@ -99,6 +99,7 @@ INSTANTIATE_TEST_CASE_P( "cppComplexExpr.vert", "cppDeepNest.frag", "cppPassMacroName.frag", + "cppRelaxSkipTokensErrors.vert", "badChars.frag", "pointCoord.frag", "array.frag",