From e28beee891b7d249338b1cc421351693db95c06b Mon Sep 17 00:00:00 2001
From: John Kessenich <cepheus@frii.com>
Date: Fri, 6 Dec 2013 16:13:47 +0000
Subject: [PATCH] Preprocessor:  Fixed that some comments incorrectly
 substituted a new line instead of space.  Also generally cleaned up the
 space-related coding.

git-svn-id: https://cvs.khronos.org/svn/repos/ogl/trunk/ecosystem/public/sdk/tools/glslang@24387 e7fa87d3-cd2b-0410-9028-fcbf551c1848
---
 Test/baseResults/cppSimple.vert.out           | 16 ++++++++-
 Test/cppSimple.vert                           | 25 +++++++++++++
 glslang/Include/revision.h                    |  4 +--
 .../MachineIndependent/preprocessor/Pp.cpp    | 35 +++++++++++--------
 .../preprocessor/PpContext.h                  |  3 +-
 .../preprocessor/PpScanner.cpp                | 20 +++++------
 .../preprocessor/PpTokens.cpp                 |  6 ----
 7 files changed, 72 insertions(+), 37 deletions(-)

diff --git a/Test/baseResults/cppSimple.vert.out b/Test/baseResults/cppSimple.vert.out
index 139afdbd9..a19885cd5 100644
--- a/Test/baseResults/cppSimple.vert.out
+++ b/Test/baseResults/cppSimple.vert.out
@@ -70,8 +70,9 @@ ERROR: 12:1234: '#line' : unexpected tokens following directive
 ERROR: 12:20001: '#error' : line should be 20001  
 ERROR: 12:20011: '#error' : line should be 20011  
 ERROR: 12:20021: '#error' : line should be 20021  
+ERROR: 12:20046: '#define' : Macro redefined; different substitutions: SPACE_IN_MIDDLE
 ERROR: 12:10003: '' : missing #endif 
-ERROR: 71 compilation errors.  No code generated.
+ERROR: 72 compilation errors.  No code generated.
 
 
 ERROR: node is still EOpNull!
@@ -148,6 +149,19 @@ ERROR: node is still EOpNull!
 0:202      'f' (double)
 0:202      Constant:
 0:202        0.000800
+12:20032  Function Definition: foo234( (void)
+12:20032    Function Parameters: 
+12:20034    Sequence
+12:20034      move second child to first child (4-component vector of float)
+12:20034        gl_Position: direct index for structure (gl_Position 4-component vector of float)
+12:20034          '__anon__1' (out block{gl_Position,gl_PointSize,gl_ClipDistance,gl_ClipVertex,gl_FrontColor,gl_BackColor,gl_FrontSecondaryColor,gl_BackSecondaryColor,gl_TexCoord,gl_FogFragCoord})
+12:20034          Constant:
+12:20034            0 (const uint)
+12:20034        Constant:
+12:20034          6.000000
+12:20034          6.000000
+12:20034          6.000000
+12:20034          6.000000
 0:?   Linker Objects
 0:?     'sum' (float)
 0:?     'linenumber' (int)
diff --git a/Test/cppSimple.vert b/Test/cppSimple.vert
index 1344c8d6c..8f1f8cb6d 100644
--- a/Test/cppSimple.vert
+++ b/Test/cppSimple.vert
@@ -263,6 +263,31 @@ double f = f1;
 #line +20020
 #error line should be 20021
 
+#define VAL1 1.0
+#define VAL2 2.0
+
+#define RES2 /* test a multiline
+                comment in a macro definition */ (RES1 * VAL2)
+#define RES1    (VAL2 / VAL1) 
+#define RES2    /* comment */(RES1 * VAL2)
+#define /* */SUM_VALUES   (RES2 + RES1)
+
+void foo234()
+{
+    gl_Position = vec4(SUM_VALUES);
+}
+
+// more whitespace recording tests
+#define SPACE_AT_END(a,b) spaceAtEndIsOkay
+#define SPACE_AT_END(a,b) spaceAtEndIsOkay // space at end
+
+#define SPACE_AT_BEGIN(a,b)spaceAtBeginIsOkay
+#define SPACE_AT_BEGIN(a,b) spaceAtBeginIsOkay
+
+// space in middle is an error
+#define SPACE_IN_MIDDLE(a,b) space +in middle
+#define SPACE_IN_MIDDLE(a,b) space + in middle
+
 #line 10000
 #if 1
 #else
diff --git a/glslang/Include/revision.h b/glslang/Include/revision.h
index bd4220144..1e71420e3 100644
--- a/glslang/Include/revision.h
+++ b/glslang/Include/revision.h
@@ -9,5 +9,5 @@
 // source have to figure out how to create revision.h just to get a build
 // going.  However, if it is not updated, it can be a version behind.
 
-#define GLSLANG_REVISION "24377"
-#define GLSLANG_DATE     "2013/12/05 13:58:16"
+#define GLSLANG_REVISION "24378"
+#define GLSLANG_DATE     "2013/12/05 14:51:40"
diff --git a/glslang/MachineIndependent/preprocessor/Pp.cpp b/glslang/MachineIndependent/preprocessor/Pp.cpp
index eccf359d3..67fca136d 100644
--- a/glslang/MachineIndependent/preprocessor/Pp.cpp
+++ b/glslang/MachineIndependent/preprocessor/Pp.cpp
@@ -128,24 +128,27 @@ int TPpContext::InitCPP()
 // Handle #define
 int TPpContext::CPPdefine(TPpToken* ppToken)
 {
-    int token, atom, args[maxMacroArgs], argc;
     MacroSymbol mac;
     Symbol *symb;
-    token = currentInput->scan(this, currentInput, ppToken);
+
+    // get macro name
+    int token = currentInput->scan(this, currentInput, ppToken);
     if (token != CPP_IDENTIFIER) {
         parseContext.error(ppToken->loc, "must be followed by macro name", "#define", "");
         return token;
     }
-    atom = ppToken->atom;
+    int atom = ppToken->atom;
     const char* definedName = GetAtomString(atom);
     if (ppToken->loc.string >= 0) {
         // We are in user code; check for reserved name use:
         parseContext.reservedPpErrorCheck(ppToken->loc, definedName, "#define");
     }
+
+    // gather parameters to the macro, between (...)
     token = currentInput->scan(this, currentInput, ppToken);
-    if (token == '(' && !ppToken->ival) {
-        // gather arguments
-        argc = 0;
+    if (token == '(' && ! ppToken->space) {
+        int argc = 0;
+        int args[maxMacroArgs];
         do {
             token = currentInput->scan(this, currentInput, ppToken);
             if (argc == 0 && token == ')') 
@@ -155,7 +158,7 @@ int TPpContext::CPPdefine(TPpToken* ppToken)
 
                 return token;
             }
-            // check for duplication
+            // check for duplication of parameter name
             bool duplicate = false;
             for (int a = 0; a < argc; ++a) {
                 if (args[a] == ppToken->atom) {
@@ -182,6 +185,8 @@ int TPpContext::CPPdefine(TPpToken* ppToken)
         memcpy(mac.args, args, argc * sizeof(int));
         token = currentInput->scan(this, currentInput, ppToken);
     }
+
+    // record the definition of the macro
     TSourceLoc defineLoc = ppToken->loc; // because ppToken is going to go to the next line before we report errors
     mac.body = new TokenStream;
     while (token != '\n') {
@@ -192,14 +197,12 @@ int TPpContext::CPPdefine(TPpToken* ppToken)
                 token = currentInput->scan(this, currentInput, ppToken);
         }
         RecordToken(mac.body, token, ppToken);
-        int spaceCandidate = currentInput->getch(this, currentInput, ppToken);
-        if (spaceCandidate == ' ' || spaceCandidate == '\t')
-            RecordToken(mac.body, ' ', 0);
-        else
-            currentInput->ungetch(this, currentInput, spaceCandidate, ppToken);
         token = currentInput->scan(this, currentInput, ppToken);
+        if (token != '\n' && ppToken->space)
+            RecordToken(mac.body, ' ', ppToken);
     }
 
+    // check for duplicate definition
     symb = LookUpSymbol(atom);
     if (symb) {
         if (! symb->mac.undef) {
@@ -209,7 +212,7 @@ int TPpContext::CPPdefine(TPpToken* ppToken)
             if (symb->mac.argc != mac.argc)
                 parseContext.error(defineLoc, "Macro redefined; different number of arguments:", "#define", GetAtomString(atom));
             else {
-                for (argc=0; argc < mac.argc; argc++) {
+                for (int argc = 0; argc < mac.argc; argc++) {
                     if (symb->mac.args[argc] != mac.args[argc])
                         parseContext.error(defineLoc, "Macro redefined; different argument names:", "#define", GetAtomString(atom));
                 }
@@ -229,9 +232,9 @@ int TPpContext::CPPdefine(TPpToken* ppToken)
                 } while (newToken > 0);
             }
         }
-    } else {
+    } else
         symb = AddSymbol(atom);
-    }
+
     delete symb->mac.body;
     symb->mac = mac;
 
@@ -930,6 +933,7 @@ int TPpContext::zero_scan(TPpContext* pp, InputSrc *inInput, TPpToken* ppToken)
 
     strcpy(ppToken->name, "0");
     ppToken->ival = 0;
+    ppToken->space = false;
 
     // pop input
     pp->currentInput = in->prev;
@@ -953,6 +957,7 @@ int TPpContext::MacroExpand(int atom, TPpToken* ppToken, int expandUndef)
     int token;
     int depth = 0;
 
+    ppToken->space = false;
     if (atom == __LINE__Atom) {
         ppToken->ival = parseContext.getCurrentLoc().line;
         sprintf(ppToken->name, "%d", ppToken->ival);
diff --git a/glslang/MachineIndependent/preprocessor/PpContext.h b/glslang/MachineIndependent/preprocessor/PpContext.h
index 8ea44992c..32691780f 100644
--- a/glslang/MachineIndependent/preprocessor/PpContext.h
+++ b/glslang/MachineIndependent/preprocessor/PpContext.h
@@ -84,7 +84,7 @@ namespace glslang {
 
 class TPpToken {
 public:
-    TPpToken() : token(0), ival(0), dval(0.0), atom(0)
+    TPpToken() : token(0), ival(0), space(false), dval(0.0), atom(0)
     { 
         loc.line = 0; 
         loc.string = 0; 
@@ -103,6 +103,7 @@ public:
 
     TSourceLoc loc;
     int    token;
+    bool   space;  // true if a space (for white space or a removed comment) should also be recognized, in front of the token returned
     int    ival;
     double dval;
     int    atom;
diff --git a/glslang/MachineIndependent/preprocessor/PpScanner.cpp b/glslang/MachineIndependent/preprocessor/PpScanner.cpp
index 0efeb6479..c62fff0e6 100644
--- a/glslang/MachineIndependent/preprocessor/PpScanner.cpp
+++ b/glslang/MachineIndependent/preprocessor/PpScanner.cpp
@@ -257,10 +257,11 @@ int TPpContext::sourceScan(TPpContext* pp, InputSrc*, TPpToken* ppToken)
     unsigned ival = 0;
 
     ppToken->ival = 0;
+    ppToken->space = false;
     ch = pp->currentInput->getch(pp, pp->currentInput, ppToken);
     for (;;) {
         while (ch == ' ' || ch == '\t' || ch == '\r') {
-            ppToken->ival = 1;
+            ppToken->space = true;
             ch = pp->currentInput->getch(pp, pp->currentInput, ppToken);
         }
 
@@ -649,17 +650,13 @@ int TPpContext::sourceScan(TPpContext* pp, InputSrc*, TPpToken* ppToken)
                         }
                     }
                 } while (ch != '\n' && ch != EOF);
-                if (ch == EOF)
-                    return EOF;
-                return '\n';
+                ppToken->space = true;
+                return ch;
             } else if (ch == '*') {
-                int nlcount = 0;
                 ch = pp->currentInput->getch(pp, pp->currentInput, ppToken);
                 do {
                     while (ch != '*') {
-                        if (ch == '\n')
-                            nlcount++;
-                        else if (ch == EOF) {
+                        if (ch == EOF) {
                             pp->parseContext.error(ppToken->loc, "EOF in comment", "comment", "");
 
                             return EOF;
@@ -673,9 +670,9 @@ int TPpContext::sourceScan(TPpContext* pp, InputSrc*, TPpToken* ppToken)
                         return EOF;
                     }
                 } while (ch != '/');
-                if (nlcount)
-                    return '\n';
-                // Go try it again...
+                ppToken->space = true;
+                // loop again to get the next token...
+                break;
             } else if (ch == '=') {
                 return CPP_DIV_ASSIGN;
             } else {
@@ -709,7 +706,6 @@ int TPpContext::sourceScan(TPpContext* pp, InputSrc*, TPpToken* ppToken)
             }
         }
 
-        ppToken->ival = 0;
         ch = pp->currentInput->getch(pp, pp->currentInput, ppToken);
     }
 }
diff --git a/glslang/MachineIndependent/preprocessor/PpTokens.cpp b/glslang/MachineIndependent/preprocessor/PpTokens.cpp
index 61e43301c..ee27cbd2f 100644
--- a/glslang/MachineIndependent/preprocessor/PpTokens.cpp
+++ b/glslang/MachineIndependent/preprocessor/PpTokens.cpp
@@ -143,9 +143,6 @@ void TPpContext::RecordToken(TokenStream *pTok, int token, TPpToken* ppToken)
         }
         lAddByte(pTok, 0);
         break;
-    case '(':
-        lAddByte(pTok, (unsigned char)(ppToken->ival ? 1 : 0));
-        break;
     default:
         break;
     }
@@ -179,9 +176,6 @@ int TPpContext::ReadToken(TokenStream *pTok, TPpToken *ppToken)
         if (ltoken > 127)
             ltoken += 128;
         switch (ltoken) {
-        case '(':
-            ppToken->ival = lReadByte(pTok);
-            break;
         case CPP_STRCONSTANT:
         case CPP_IDENTIFIER:
         case CPP_FLOATCONSTANT:
-- 
GitLab