PP: don't give errors on some tokens under #if 0 (or similar).
authorJohn Kessenich <cepheus@frii.com>
Mon, 19 Mar 2018 06:43:18 +0000 (00:43 -0600)
committerJohn Kessenich <cepheus@frii.com>
Mon, 19 Mar 2018 06:43:18 +0000 (00:43 -0600)
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.

Test/baseResults/cppRelaxSkipTokensErrors.vert.out [new file with mode: 0755]
Test/cppBad.vert
Test/cppRelaxSkipTokensErrors.vert [new file with mode: 0644]
glslang/MachineIndependent/preprocessor/PpScanner.cpp
gtests/AST.FromFile.cpp

diff --git a/Test/baseResults/cppRelaxSkipTokensErrors.vert.out b/Test/baseResults/cppRelaxSkipTokensErrors.vert.out
new file mode 100755 (executable)
index 0000000..e9b4b1b
--- /dev/null
@@ -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
+
index 49600f91670877c99df04423abd283b15aa10667..0044c44fce0149bb5a400157235be8a1e3e90153 100644 (file)
@@ -1,5 +1,5 @@
 #define m#0#\r
 #if m\r
-\r
+#endif\r
 #define n()\r
 int n"
\ No newline at end of file
diff --git a/Test/cppRelaxSkipTokensErrors.vert b/Test/cppRelaxSkipTokensErrors.vert
new file mode 100644 (file)
index 0000000..b30af0c
--- /dev/null
@@ -0,0 +1,14 @@
+#version 110\r
+\r
+#if 0\r
+3.5L\r
+3.5h\r
+2034h\r
+1.#INF\r
+0x1234567812345L\r
+12323394203923879234L\r
+0123s;\r
+123s;\r
+0123456712345671234L\r
+"string"\r
+#endif\r
index 61bf027fa3ca20aae3181258348bd6e693e39977..f4eaf57d3527043c5d2b871312c7cfa12aafdc90 100644 (file)
@@ -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;
index fea75863344906cb3d2eb19de23c019e4e7c3c93..061c86c305de8f0273f1cb8c9e16574e41ed8cb1 100644 (file)
@@ -99,6 +99,7 @@ INSTANTIATE_TEST_CASE_P(
         "cppComplexExpr.vert",
         "cppDeepNest.frag",
         "cppPassMacroName.frag",
+        "cppRelaxSkipTokensErrors.vert",
         "badChars.frag",
         "pointCoord.frag",
         "array.frag",