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
This commit is contained in:
John Kessenich 2013-12-06 16:13:47 +00:00
parent 8e789e8d62
commit e28beee891
7 changed files with 72 additions and 37 deletions

View file

@ -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);

View file

@ -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;

View file

@ -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);
}
}

View file

@ -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: