Commit f832c9dd by Jamie Madill Committed by Commit Bot

Fix style in the preprocessor.

Again using git cl format. BUG=angleproject:650 Change-Id: I8898d00bfc6a50db50bffd2cc30c3eda7c08c6c2 Reviewed-on: https://chromium-review.googlesource.com/419097Reviewed-by: 's avatarJamie Madill <jmadill@chromium.org> Commit-Queue: Jamie Madill <jmadill@chromium.org>
parent d7b1ab58
...@@ -15,9 +15,7 @@ Diagnostics::~Diagnostics() ...@@ -15,9 +15,7 @@ Diagnostics::~Diagnostics()
{ {
} }
void Diagnostics::report(ID id, void Diagnostics::report(ID id, const SourceLocation &loc, const std::string &text)
const SourceLocation &loc,
const std::string &text)
{ {
// TODO(alokp): Keep a count of errors and warnings. // TODO(alokp): Keep a count of errors and warnings.
print(id, loc, text); print(id, loc, text);
...@@ -39,104 +37,105 @@ std::string Diagnostics::message(ID id) ...@@ -39,104 +37,105 @@ std::string Diagnostics::message(ID id)
{ {
switch (id) switch (id)
{ {
// Errors begin. // Errors begin.
case PP_INTERNAL_ERROR: case PP_INTERNAL_ERROR:
return "internal error"; return "internal error";
case PP_OUT_OF_MEMORY: case PP_OUT_OF_MEMORY:
return "out of memory"; return "out of memory";
case PP_INVALID_CHARACTER: case PP_INVALID_CHARACTER:
return "invalid character"; return "invalid character";
case PP_INVALID_NUMBER: case PP_INVALID_NUMBER:
return "invalid number"; return "invalid number";
case PP_INTEGER_OVERFLOW: case PP_INTEGER_OVERFLOW:
return "integer overflow"; return "integer overflow";
case PP_FLOAT_OVERFLOW: case PP_FLOAT_OVERFLOW:
return "float overflow"; return "float overflow";
case PP_TOKEN_TOO_LONG: case PP_TOKEN_TOO_LONG:
return "token too long"; return "token too long";
case PP_INVALID_EXPRESSION: case PP_INVALID_EXPRESSION:
return "invalid expression"; return "invalid expression";
case PP_DIVISION_BY_ZERO: case PP_DIVISION_BY_ZERO:
return "division by zero"; return "division by zero";
case PP_EOF_IN_COMMENT: case PP_EOF_IN_COMMENT:
return "unexpected end of file found in comment"; return "unexpected end of file found in comment";
case PP_UNEXPECTED_TOKEN: case PP_UNEXPECTED_TOKEN:
return "unexpected token"; return "unexpected token";
case PP_DIRECTIVE_INVALID_NAME: case PP_DIRECTIVE_INVALID_NAME:
return "invalid directive name"; return "invalid directive name";
case PP_MACRO_NAME_RESERVED: case PP_MACRO_NAME_RESERVED:
return "macro name is reserved"; return "macro name is reserved";
case PP_MACRO_REDEFINED: case PP_MACRO_REDEFINED:
return "macro redefined"; return "macro redefined";
case PP_MACRO_PREDEFINED_REDEFINED: case PP_MACRO_PREDEFINED_REDEFINED:
return "predefined macro redefined"; return "predefined macro redefined";
case PP_MACRO_PREDEFINED_UNDEFINED: case PP_MACRO_PREDEFINED_UNDEFINED:
return "predefined macro undefined"; return "predefined macro undefined";
case PP_MACRO_UNTERMINATED_INVOCATION: case PP_MACRO_UNTERMINATED_INVOCATION:
return "unterminated macro invocation"; return "unterminated macro invocation";
case PP_MACRO_UNDEFINED_WHILE_INVOKED: case PP_MACRO_UNDEFINED_WHILE_INVOKED:
return "macro undefined while being invoked"; return "macro undefined while being invoked";
case PP_MACRO_TOO_FEW_ARGS: case PP_MACRO_TOO_FEW_ARGS:
return "Not enough arguments for macro"; return "Not enough arguments for macro";
case PP_MACRO_TOO_MANY_ARGS: case PP_MACRO_TOO_MANY_ARGS:
return "Too many arguments for macro"; return "Too many arguments for macro";
case PP_MACRO_DUPLICATE_PARAMETER_NAMES: case PP_MACRO_DUPLICATE_PARAMETER_NAMES:
return "duplicate macro parameter name"; return "duplicate macro parameter name";
case PP_MACRO_INVOCATION_CHAIN_TOO_DEEP: case PP_MACRO_INVOCATION_CHAIN_TOO_DEEP:
return "macro invocation chain too deep"; return "macro invocation chain too deep";
case PP_CONDITIONAL_ENDIF_WITHOUT_IF: case PP_CONDITIONAL_ENDIF_WITHOUT_IF:
return "unexpected #endif found without a matching #if"; return "unexpected #endif found without a matching #if";
case PP_CONDITIONAL_ELSE_WITHOUT_IF: case PP_CONDITIONAL_ELSE_WITHOUT_IF:
return "unexpected #else found without a matching #if"; return "unexpected #else found without a matching #if";
case PP_CONDITIONAL_ELSE_AFTER_ELSE: case PP_CONDITIONAL_ELSE_AFTER_ELSE:
return "unexpected #else found after another #else"; return "unexpected #else found after another #else";
case PP_CONDITIONAL_ELIF_WITHOUT_IF: case PP_CONDITIONAL_ELIF_WITHOUT_IF:
return "unexpected #elif found without a matching #if"; return "unexpected #elif found without a matching #if";
case PP_CONDITIONAL_ELIF_AFTER_ELSE: case PP_CONDITIONAL_ELIF_AFTER_ELSE:
return "unexpected #elif found after #else"; return "unexpected #elif found after #else";
case PP_CONDITIONAL_UNTERMINATED: case PP_CONDITIONAL_UNTERMINATED:
return "unexpected end of file found in conditional block"; return "unexpected end of file found in conditional block";
case PP_INVALID_EXTENSION_NAME: case PP_INVALID_EXTENSION_NAME:
return "invalid extension name"; return "invalid extension name";
case PP_INVALID_EXTENSION_BEHAVIOR: case PP_INVALID_EXTENSION_BEHAVIOR:
return "invalid extension behavior"; return "invalid extension behavior";
case PP_INVALID_EXTENSION_DIRECTIVE: case PP_INVALID_EXTENSION_DIRECTIVE:
return "invalid extension directive"; return "invalid extension directive";
case PP_INVALID_VERSION_NUMBER: case PP_INVALID_VERSION_NUMBER:
return "invalid version number"; return "invalid version number";
case PP_INVALID_VERSION_DIRECTIVE: case PP_INVALID_VERSION_DIRECTIVE:
return "invalid version directive"; return "invalid version directive";
case PP_VERSION_NOT_FIRST_STATEMENT: case PP_VERSION_NOT_FIRST_STATEMENT:
return "#version directive must occur before anything else, " return "#version directive must occur before anything else, "
"except for comments and white space"; "except for comments and white space";
case PP_VERSION_NOT_FIRST_LINE_ESSL3: case PP_VERSION_NOT_FIRST_LINE_ESSL3:
return "#version directive must occur on the first line of the shader"; return "#version directive must occur on the first line of the shader";
case PP_INVALID_LINE_NUMBER: case PP_INVALID_LINE_NUMBER:
return "invalid line number"; return "invalid line number";
case PP_INVALID_FILE_NUMBER: case PP_INVALID_FILE_NUMBER:
return "invalid file number"; return "invalid file number";
case PP_INVALID_LINE_DIRECTIVE: case PP_INVALID_LINE_DIRECTIVE:
return "invalid line directive"; return "invalid line directive";
case PP_NON_PP_TOKEN_BEFORE_EXTENSION_ESSL3: case PP_NON_PP_TOKEN_BEFORE_EXTENSION_ESSL3:
return "extension directive must occur before any non-preprocessor tokens in ESSL3"; return "extension directive must occur before any non-preprocessor tokens in ESSL3";
case PP_UNDEFINED_SHIFT: case PP_UNDEFINED_SHIFT:
return "shift exponent is negative or undefined"; return "shift exponent is negative or undefined";
// Errors end. // Errors end.
// Warnings begin. // Warnings begin.
case PP_EOF_IN_DIRECTIVE: case PP_EOF_IN_DIRECTIVE:
return "unexpected end of file found in directive"; return "unexpected end of file found in directive";
case PP_CONDITIONAL_UNEXPECTED_TOKEN: case PP_CONDITIONAL_UNEXPECTED_TOKEN:
return "unexpected token after conditional expression"; return "unexpected token after conditional expression";
case PP_UNRECOGNIZED_PRAGMA: case PP_UNRECOGNIZED_PRAGMA:
return "unrecognized pragma"; return "unrecognized pragma";
case PP_NON_PP_TOKEN_BEFORE_EXTENSION_ESSL1: case PP_NON_PP_TOKEN_BEFORE_EXTENSION_ESSL1:
return "extension directive should occur before any non-preprocessor tokens"; return "extension directive should occur before any non-preprocessor tokens";
case PP_WARNING_MACRO_NAME_RESERVED: case PP_WARNING_MACRO_NAME_RESERVED:
return "macro name with a double underscore is reserved - unintented behavior is possible"; return "macro name with a double underscore is reserved - unintented behavior is "
// Warnings end. "possible";
default: // Warnings end.
UNREACHABLE(); default:
return ""; UNREACHABLE();
return "";
} }
} }
......
...@@ -86,9 +86,7 @@ class Diagnostics ...@@ -86,9 +86,7 @@ class Diagnostics
Severity severity(ID id); Severity severity(ID id);
std::string message(ID id); std::string message(ID id);
virtual void print(ID id, virtual void print(ID id, const SourceLocation &loc, const std::string &text) = 0;
const SourceLocation &loc,
const std::string &text) = 0;
}; };
} // namespace pp } // namespace pp
......
...@@ -23,8 +23,7 @@ class DirectiveHandler ...@@ -23,8 +23,7 @@ class DirectiveHandler
public: public:
virtual ~DirectiveHandler(); virtual ~DirectiveHandler();
virtual void handleError(const SourceLocation &loc, virtual void handleError(const SourceLocation &loc, const std::string &msg) = 0;
const std::string &msg) = 0;
// Handle pragma of form: #pragma name[(value)] // Handle pragma of form: #pragma name[(value)]
virtual void handlePragma(const SourceLocation &loc, virtual void handlePragma(const SourceLocation &loc,
...@@ -36,8 +35,7 @@ class DirectiveHandler ...@@ -36,8 +35,7 @@ class DirectiveHandler
const std::string &name, const std::string &name,
const std::string &behavior) = 0; const std::string &behavior) = 0;
virtual void handleVersion(const SourceLocation &loc, virtual void handleVersion(const SourceLocation &loc, int version) = 0;
int version) = 0;
}; };
} // namespace pp } // namespace pp
......
...@@ -18,7 +18,8 @@ ...@@ -18,7 +18,8 @@
#include "compiler/preprocessor/Token.h" #include "compiler/preprocessor/Token.h"
#include "compiler/preprocessor/Tokenizer.h" #include "compiler/preprocessor/Tokenizer.h"
namespace { namespace
{
enum DirectiveType enum DirectiveType
{ {
DIRECTIVE_NONE, DIRECTIVE_NONE,
...@@ -39,19 +40,19 @@ enum DirectiveType ...@@ -39,19 +40,19 @@ enum DirectiveType
DirectiveType getDirective(const pp::Token *token) DirectiveType getDirective(const pp::Token *token)
{ {
const char kDirectiveDefine[] = "define"; const char kDirectiveDefine[] = "define";
const char kDirectiveUndef[] = "undef"; const char kDirectiveUndef[] = "undef";
const char kDirectiveIf[] = "if"; const char kDirectiveIf[] = "if";
const char kDirectiveIfdef[] = "ifdef"; const char kDirectiveIfdef[] = "ifdef";
const char kDirectiveIfndef[] = "ifndef"; const char kDirectiveIfndef[] = "ifndef";
const char kDirectiveElse[] = "else"; const char kDirectiveElse[] = "else";
const char kDirectiveElif[] = "elif"; const char kDirectiveElif[] = "elif";
const char kDirectiveEndif[] = "endif"; const char kDirectiveEndif[] = "endif";
const char kDirectiveError[] = "error"; const char kDirectiveError[] = "error";
const char kDirectivePragma[] = "pragma"; const char kDirectivePragma[] = "pragma";
const char kDirectiveExtension[] = "extension"; const char kDirectiveExtension[] = "extension";
const char kDirectiveVersion[] = "version"; const char kDirectiveVersion[] = "version";
const char kDirectiveLine[] = "line"; const char kDirectiveLine[] = "line";
if (token->type != pp::Token::IDENTIFIER) if (token->type != pp::Token::IDENTIFIER)
return DIRECTIVE_NONE; return DIRECTIVE_NONE;
...@@ -90,15 +91,15 @@ bool isConditionalDirective(DirectiveType directive) ...@@ -90,15 +91,15 @@ bool isConditionalDirective(DirectiveType directive)
{ {
switch (directive) switch (directive)
{ {
case DIRECTIVE_IF: case DIRECTIVE_IF:
case DIRECTIVE_IFDEF: case DIRECTIVE_IFDEF:
case DIRECTIVE_IFNDEF: case DIRECTIVE_IFNDEF:
case DIRECTIVE_ELSE: case DIRECTIVE_ELSE:
case DIRECTIVE_ELIF: case DIRECTIVE_ELIF:
case DIRECTIVE_ENDIF: case DIRECTIVE_ENDIF:
return true; return true;
default: default:
return false; return false;
} }
} }
...@@ -110,7 +111,7 @@ bool isEOD(const pp::Token *token) ...@@ -110,7 +111,7 @@ bool isEOD(const pp::Token *token)
void skipUntilEOD(pp::Lexer *lexer, pp::Token *token) void skipUntilEOD(pp::Lexer *lexer, pp::Token *token)
{ {
while(!isEOD(token)) while (!isEOD(token))
{ {
lexer->lex(token); lexer->lex(token);
} }
...@@ -127,8 +128,7 @@ bool hasDoubleUnderscores(const std::string &name) ...@@ -127,8 +128,7 @@ bool hasDoubleUnderscores(const std::string &name)
return (name.find("__") != std::string::npos); return (name.find("__") != std::string::npos);
} }
bool isMacroPredefined(const std::string &name, bool isMacroPredefined(const std::string &name, const pp::MacroSet &macroSet)
const pp::MacroSet &macroSet)
{ {
pp::MacroSet::const_iterator iter = macroSet.find(name); pp::MacroSet::const_iterator iter = macroSet.find(name);
return iter != macroSet.end() ? iter->second.predefined : false; return iter != macroSet.end() ? iter->second.predefined : false;
...@@ -236,14 +236,13 @@ void DirectiveParser::lex(Token *token) ...@@ -236,14 +236,13 @@ void DirectiveParser::lex(Token *token)
if (!mConditionalStack.empty()) if (!mConditionalStack.empty())
{ {
const ConditionalBlock &block = mConditionalStack.back(); const ConditionalBlock &block = mConditionalStack.back();
mDiagnostics->report(Diagnostics::PP_CONDITIONAL_UNTERMINATED, mDiagnostics->report(Diagnostics::PP_CONDITIONAL_UNTERMINATED, block.location,
block.location, block.type); block.type);
} }
break; break;
} }
} } while (skipping() || (token->type == '\n'));
while (skipping() || (token->type == '\n'));
mPastFirstStatement = true; mPastFirstStatement = true;
} }
...@@ -269,62 +268,61 @@ void DirectiveParser::parseDirective(Token *token) ...@@ -269,62 +268,61 @@ void DirectiveParser::parseDirective(Token *token)
return; return;
} }
switch(directive) switch (directive)
{ {
case DIRECTIVE_NONE: case DIRECTIVE_NONE:
mDiagnostics->report(Diagnostics::PP_DIRECTIVE_INVALID_NAME, mDiagnostics->report(Diagnostics::PP_DIRECTIVE_INVALID_NAME, token->location,
token->location, token->text); token->text);
skipUntilEOD(mTokenizer, token); skipUntilEOD(mTokenizer, token);
break; break;
case DIRECTIVE_DEFINE: case DIRECTIVE_DEFINE:
parseDefine(token); parseDefine(token);
break; break;
case DIRECTIVE_UNDEF: case DIRECTIVE_UNDEF:
parseUndef(token); parseUndef(token);
break; break;
case DIRECTIVE_IF: case DIRECTIVE_IF:
parseIf(token); parseIf(token);
break; break;
case DIRECTIVE_IFDEF: case DIRECTIVE_IFDEF:
parseIfdef(token); parseIfdef(token);
break; break;
case DIRECTIVE_IFNDEF: case DIRECTIVE_IFNDEF:
parseIfndef(token); parseIfndef(token);
break; break;
case DIRECTIVE_ELSE: case DIRECTIVE_ELSE:
parseElse(token); parseElse(token);
break; break;
case DIRECTIVE_ELIF: case DIRECTIVE_ELIF:
parseElif(token); parseElif(token);
break; break;
case DIRECTIVE_ENDIF: case DIRECTIVE_ENDIF:
parseEndif(token); parseEndif(token);
break; break;
case DIRECTIVE_ERROR: case DIRECTIVE_ERROR:
parseError(token); parseError(token);
break; break;
case DIRECTIVE_PRAGMA: case DIRECTIVE_PRAGMA:
parsePragma(token); parsePragma(token);
break; break;
case DIRECTIVE_EXTENSION: case DIRECTIVE_EXTENSION:
parseExtension(token); parseExtension(token);
break; break;
case DIRECTIVE_VERSION: case DIRECTIVE_VERSION:
parseVersion(token); parseVersion(token);
break; break;
case DIRECTIVE_LINE: case DIRECTIVE_LINE:
parseLine(token); parseLine(token);
break; break;
default: default:
UNREACHABLE(); UNREACHABLE();
break; break;
} }
skipUntilEOD(mTokenizer, token); skipUntilEOD(mTokenizer, token);
if (token->type == Token::LAST) if (token->type == Token::LAST)
{ {
mDiagnostics->report(Diagnostics::PP_EOF_IN_DIRECTIVE, mDiagnostics->report(Diagnostics::PP_EOF_IN_DIRECTIVE, token->location, token->text);
token->location, token->text);
} }
} }
...@@ -335,20 +333,18 @@ void DirectiveParser::parseDefine(Token *token) ...@@ -335,20 +333,18 @@ void DirectiveParser::parseDefine(Token *token)
mTokenizer->lex(token); mTokenizer->lex(token);
if (token->type != Token::IDENTIFIER) if (token->type != Token::IDENTIFIER)
{ {
mDiagnostics->report(Diagnostics::PP_UNEXPECTED_TOKEN, mDiagnostics->report(Diagnostics::PP_UNEXPECTED_TOKEN, token->location, token->text);
token->location, token->text);
return; return;
} }
if (isMacroPredefined(token->text, *mMacroSet)) if (isMacroPredefined(token->text, *mMacroSet))
{ {
mDiagnostics->report(Diagnostics::PP_MACRO_PREDEFINED_REDEFINED, mDiagnostics->report(Diagnostics::PP_MACRO_PREDEFINED_REDEFINED, token->location,
token->location, token->text); token->text);
return; return;
} }
if (isMacroNameReserved(token->text)) if (isMacroNameReserved(token->text))
{ {
mDiagnostics->report(Diagnostics::PP_MACRO_NAME_RESERVED, mDiagnostics->report(Diagnostics::PP_MACRO_NAME_RESERVED, token->location, token->text);
token->location, token->text);
return; return;
} }
// Using double underscores is allowed, but may result in unintended // Using double underscores is allowed, but may result in unintended
...@@ -377,7 +373,8 @@ void DirectiveParser::parseDefine(Token *token) ...@@ -377,7 +373,8 @@ void DirectiveParser::parseDefine(Token *token)
if (token->type != Token::IDENTIFIER) if (token->type != Token::IDENTIFIER)
break; break;
if (std::find(macro.parameters.begin(), macro.parameters.end(), token->text) != macro.parameters.end()) if (std::find(macro.parameters.begin(), macro.parameters.end(), token->text) !=
macro.parameters.end())
{ {
mDiagnostics->report(Diagnostics::PP_MACRO_DUPLICATE_PARAMETER_NAMES, mDiagnostics->report(Diagnostics::PP_MACRO_DUPLICATE_PARAMETER_NAMES,
token->location, token->text); token->location, token->text);
...@@ -387,14 +384,11 @@ void DirectiveParser::parseDefine(Token *token) ...@@ -387,14 +384,11 @@ void DirectiveParser::parseDefine(Token *token)
macro.parameters.push_back(token->text); macro.parameters.push_back(token->text);
mTokenizer->lex(token); // Get ','. mTokenizer->lex(token); // Get ','.
} } while (token->type == ',');
while (token->type == ',');
if (token->type != ')') if (token->type != ')')
{ {
mDiagnostics->report(Diagnostics::PP_UNEXPECTED_TOKEN, mDiagnostics->report(Diagnostics::PP_UNEXPECTED_TOKEN, token->location, token->text);
token->location,
token->text);
return; return;
} }
mTokenizer->lex(token); // Get ')'. mTokenizer->lex(token); // Get ')'.
...@@ -420,9 +414,7 @@ void DirectiveParser::parseDefine(Token *token) ...@@ -420,9 +414,7 @@ void DirectiveParser::parseDefine(Token *token)
MacroSet::const_iterator iter = mMacroSet->find(macro.name); MacroSet::const_iterator iter = mMacroSet->find(macro.name);
if (iter != mMacroSet->end() && !macro.equals(iter->second)) if (iter != mMacroSet->end() && !macro.equals(iter->second))
{ {
mDiagnostics->report(Diagnostics::PP_MACRO_REDEFINED, mDiagnostics->report(Diagnostics::PP_MACRO_REDEFINED, token->location, macro.name);
token->location,
macro.name);
return; return;
} }
mMacroSet->insert(std::make_pair(macro.name, macro)); mMacroSet->insert(std::make_pair(macro.name, macro));
...@@ -435,8 +427,7 @@ void DirectiveParser::parseUndef(Token *token) ...@@ -435,8 +427,7 @@ void DirectiveParser::parseUndef(Token *token)
mTokenizer->lex(token); mTokenizer->lex(token);
if (token->type != Token::IDENTIFIER) if (token->type != Token::IDENTIFIER)
{ {
mDiagnostics->report(Diagnostics::PP_UNEXPECTED_TOKEN, mDiagnostics->report(Diagnostics::PP_UNEXPECTED_TOKEN, token->location, token->text);
token->location, token->text);
return; return;
} }
...@@ -445,8 +436,8 @@ void DirectiveParser::parseUndef(Token *token) ...@@ -445,8 +436,8 @@ void DirectiveParser::parseUndef(Token *token)
{ {
if (iter->second.predefined) if (iter->second.predefined)
{ {
mDiagnostics->report(Diagnostics::PP_MACRO_PREDEFINED_UNDEFINED, mDiagnostics->report(Diagnostics::PP_MACRO_PREDEFINED_UNDEFINED, token->location,
token->location, token->text); token->text);
return; return;
} }
else if (iter->second.expansionCount > 0) else if (iter->second.expansionCount > 0)
...@@ -464,8 +455,7 @@ void DirectiveParser::parseUndef(Token *token) ...@@ -464,8 +455,7 @@ void DirectiveParser::parseUndef(Token *token)
mTokenizer->lex(token); mTokenizer->lex(token);
if (!isEOD(token)) if (!isEOD(token))
{ {
mDiagnostics->report(Diagnostics::PP_UNEXPECTED_TOKEN, mDiagnostics->report(Diagnostics::PP_UNEXPECTED_TOKEN, token->location, token->text);
token->location, token->text);
skipUntilEOD(mTokenizer, token); skipUntilEOD(mTokenizer, token);
} }
} }
...@@ -494,8 +484,8 @@ void DirectiveParser::parseElse(Token *token) ...@@ -494,8 +484,8 @@ void DirectiveParser::parseElse(Token *token)
if (mConditionalStack.empty()) if (mConditionalStack.empty())
{ {
mDiagnostics->report(Diagnostics::PP_CONDITIONAL_ELSE_WITHOUT_IF, mDiagnostics->report(Diagnostics::PP_CONDITIONAL_ELSE_WITHOUT_IF, token->location,
token->location, token->text); token->text);
skipUntilEOD(mTokenizer, token); skipUntilEOD(mTokenizer, token);
return; return;
} }
...@@ -509,22 +499,22 @@ void DirectiveParser::parseElse(Token *token) ...@@ -509,22 +499,22 @@ void DirectiveParser::parseElse(Token *token)
} }
if (block.foundElseGroup) if (block.foundElseGroup)
{ {
mDiagnostics->report(Diagnostics::PP_CONDITIONAL_ELSE_AFTER_ELSE, mDiagnostics->report(Diagnostics::PP_CONDITIONAL_ELSE_AFTER_ELSE, token->location,
token->location, token->text); token->text);
skipUntilEOD(mTokenizer, token); skipUntilEOD(mTokenizer, token);
return; return;
} }
block.foundElseGroup = true; block.foundElseGroup = true;
block.skipGroup = block.foundValidGroup; block.skipGroup = block.foundValidGroup;
block.foundValidGroup = true; block.foundValidGroup = true;
// Check if there are extra tokens after #else. // Check if there are extra tokens after #else.
mTokenizer->lex(token); mTokenizer->lex(token);
if (!isEOD(token)) if (!isEOD(token))
{ {
mDiagnostics->report(Diagnostics::PP_CONDITIONAL_UNEXPECTED_TOKEN, mDiagnostics->report(Diagnostics::PP_CONDITIONAL_UNEXPECTED_TOKEN, token->location,
token->location, token->text); token->text);
skipUntilEOD(mTokenizer, token); skipUntilEOD(mTokenizer, token);
} }
} }
...@@ -535,8 +525,8 @@ void DirectiveParser::parseElif(Token *token) ...@@ -535,8 +525,8 @@ void DirectiveParser::parseElif(Token *token)
if (mConditionalStack.empty()) if (mConditionalStack.empty())
{ {
mDiagnostics->report(Diagnostics::PP_CONDITIONAL_ELIF_WITHOUT_IF, mDiagnostics->report(Diagnostics::PP_CONDITIONAL_ELIF_WITHOUT_IF, token->location,
token->location, token->text); token->text);
skipUntilEOD(mTokenizer, token); skipUntilEOD(mTokenizer, token);
return; return;
} }
...@@ -550,8 +540,8 @@ void DirectiveParser::parseElif(Token *token) ...@@ -550,8 +540,8 @@ void DirectiveParser::parseElif(Token *token)
} }
if (block.foundElseGroup) if (block.foundElseGroup)
{ {
mDiagnostics->report(Diagnostics::PP_CONDITIONAL_ELIF_AFTER_ELSE, mDiagnostics->report(Diagnostics::PP_CONDITIONAL_ELIF_AFTER_ELSE, token->location,
token->location, token->text); token->text);
skipUntilEOD(mTokenizer, token); skipUntilEOD(mTokenizer, token);
return; return;
} }
...@@ -564,8 +554,8 @@ void DirectiveParser::parseElif(Token *token) ...@@ -564,8 +554,8 @@ void DirectiveParser::parseElif(Token *token)
return; return;
} }
int expression = parseExpressionIf(token); int expression = parseExpressionIf(token);
block.skipGroup = expression == 0; block.skipGroup = expression == 0;
block.foundValidGroup = expression != 0; block.foundValidGroup = expression != 0;
} }
...@@ -575,8 +565,8 @@ void DirectiveParser::parseEndif(Token *token) ...@@ -575,8 +565,8 @@ void DirectiveParser::parseEndif(Token *token)
if (mConditionalStack.empty()) if (mConditionalStack.empty())
{ {
mDiagnostics->report(Diagnostics::PP_CONDITIONAL_ENDIF_WITHOUT_IF, mDiagnostics->report(Diagnostics::PP_CONDITIONAL_ENDIF_WITHOUT_IF, token->location,
token->location, token->text); token->text);
skipUntilEOD(mTokenizer, token); skipUntilEOD(mTokenizer, token);
return; return;
} }
...@@ -587,8 +577,8 @@ void DirectiveParser::parseEndif(Token *token) ...@@ -587,8 +577,8 @@ void DirectiveParser::parseEndif(Token *token)
mTokenizer->lex(token); mTokenizer->lex(token);
if (!isEOD(token)) if (!isEOD(token))
{ {
mDiagnostics->report(Diagnostics::PP_CONDITIONAL_UNEXPECTED_TOKEN, mDiagnostics->report(Diagnostics::PP_CONDITIONAL_UNEXPECTED_TOKEN, token->location,
token->location, token->text); token->text);
skipUntilEOD(mTokenizer, token); skipUntilEOD(mTokenizer, token);
} }
} }
...@@ -632,25 +622,25 @@ void DirectiveParser::parsePragma(Token *token) ...@@ -632,25 +622,25 @@ void DirectiveParser::parsePragma(Token *token)
} }
while ((token->type != '\n') && (token->type != Token::LAST)) while ((token->type != '\n') && (token->type != Token::LAST))
{ {
switch(state++) switch (state++)
{ {
case PRAGMA_NAME: case PRAGMA_NAME:
name = token->text; name = token->text;
valid = valid && (token->type == Token::IDENTIFIER); valid = valid && (token->type == Token::IDENTIFIER);
break; break;
case LEFT_PAREN: case LEFT_PAREN:
valid = valid && (token->type == '('); valid = valid && (token->type == '(');
break; break;
case PRAGMA_VALUE: case PRAGMA_VALUE:
value = token->text; value = token->text;
valid = valid && (token->type == Token::IDENTIFIER); valid = valid && (token->type == Token::IDENTIFIER);
break; break;
case RIGHT_PAREN: case RIGHT_PAREN:
valid = valid && (token->type == ')'); valid = valid && (token->type == ')');
break; break;
default: default:
valid = false; valid = false;
break; break;
} }
mTokenizer->lex(token); mTokenizer->lex(token);
} }
...@@ -660,8 +650,7 @@ void DirectiveParser::parsePragma(Token *token) ...@@ -660,8 +650,7 @@ void DirectiveParser::parsePragma(Token *token)
(state == RIGHT_PAREN + 1)); // With value. (state == RIGHT_PAREN + 1)); // With value.
if (!valid) if (!valid)
{ {
mDiagnostics->report(Diagnostics::PP_UNRECOGNIZED_PRAGMA, mDiagnostics->report(Diagnostics::PP_UNRECOGNIZED_PRAGMA, token->location, name);
token->location, name);
} }
else if (state > PRAGMA_NAME) // Do not notify for empty pragma. else if (state > PRAGMA_NAME) // Do not notify for empty pragma.
{ {
...@@ -689,47 +678,49 @@ void DirectiveParser::parseExtension(Token *token) ...@@ -689,47 +678,49 @@ void DirectiveParser::parseExtension(Token *token)
{ {
switch (state++) switch (state++)
{ {
case EXT_NAME: case EXT_NAME:
if (valid && (token->type != Token::IDENTIFIER)) if (valid && (token->type != Token::IDENTIFIER))
{ {
mDiagnostics->report(Diagnostics::PP_INVALID_EXTENSION_NAME, mDiagnostics->report(Diagnostics::PP_INVALID_EXTENSION_NAME, token->location,
token->location, token->text); token->text);
valid = false; valid = false;
} }
if (valid) name = token->text; if (valid)
break; name = token->text;
case COLON: break;
if (valid && (token->type != ':')) case COLON:
{ if (valid && (token->type != ':'))
mDiagnostics->report(Diagnostics::PP_UNEXPECTED_TOKEN, {
token->location, token->text); mDiagnostics->report(Diagnostics::PP_UNEXPECTED_TOKEN, token->location,
valid = false; token->text);
} valid = false;
break; }
case EXT_BEHAVIOR: break;
if (valid && (token->type != Token::IDENTIFIER)) case EXT_BEHAVIOR:
{ if (valid && (token->type != Token::IDENTIFIER))
mDiagnostics->report(Diagnostics::PP_INVALID_EXTENSION_BEHAVIOR, {
token->location, token->text); mDiagnostics->report(Diagnostics::PP_INVALID_EXTENSION_BEHAVIOR,
valid = false; token->location, token->text);
} valid = false;
if (valid) behavior = token->text; }
break; if (valid)
default: behavior = token->text;
if (valid) break;
{ default:
mDiagnostics->report(Diagnostics::PP_UNEXPECTED_TOKEN, if (valid)
token->location, token->text); {
valid = false; mDiagnostics->report(Diagnostics::PP_UNEXPECTED_TOKEN, token->location,
} token->text);
break; valid = false;
}
break;
} }
mTokenizer->lex(token); mTokenizer->lex(token);
} }
if (valid && (state != EXT_BEHAVIOR + 1)) if (valid && (state != EXT_BEHAVIOR + 1))
{ {
mDiagnostics->report(Diagnostics::PP_INVALID_EXTENSION_DIRECTIVE, mDiagnostics->report(Diagnostics::PP_INVALID_EXTENSION_DIRECTIVE, token->location,
token->location, token->text); token->text);
valid = false; valid = false;
} }
if (valid && mSeenNonPreprocessorToken) if (valid && mSeenNonPreprocessorToken)
...@@ -756,8 +747,8 @@ void DirectiveParser::parseVersion(Token *token) ...@@ -756,8 +747,8 @@ void DirectiveParser::parseVersion(Token *token)
if (mPastFirstStatement) if (mPastFirstStatement)
{ {
mDiagnostics->report(Diagnostics::PP_VERSION_NOT_FIRST_STATEMENT, mDiagnostics->report(Diagnostics::PP_VERSION_NOT_FIRST_STATEMENT, token->location,
token->location, token->text); token->text);
skipUntilEOD(mTokenizer, token); skipUntilEOD(mTokenizer, token);
return; return;
} }
...@@ -769,47 +760,47 @@ void DirectiveParser::parseVersion(Token *token) ...@@ -769,47 +760,47 @@ void DirectiveParser::parseVersion(Token *token)
VERSION_ENDLINE VERSION_ENDLINE
}; };
bool valid = true; bool valid = true;
int version = 0; int version = 0;
int state = VERSION_NUMBER; int state = VERSION_NUMBER;
mTokenizer->lex(token); mTokenizer->lex(token);
while (valid && (token->type != '\n') && (token->type != Token::LAST)) while (valid && (token->type != '\n') && (token->type != Token::LAST))
{ {
switch (state) switch (state)
{ {
case VERSION_NUMBER: case VERSION_NUMBER:
if (token->type != Token::CONST_INT) if (token->type != Token::CONST_INT)
{ {
mDiagnostics->report(Diagnostics::PP_INVALID_VERSION_NUMBER, mDiagnostics->report(Diagnostics::PP_INVALID_VERSION_NUMBER, token->location,
token->location, token->text); token->text);
valid = false; valid = false;
} }
if (valid && !token->iValue(&version)) if (valid && !token->iValue(&version))
{ {
mDiagnostics->report(Diagnostics::PP_INTEGER_OVERFLOW, mDiagnostics->report(Diagnostics::PP_INTEGER_OVERFLOW, token->location,
token->location, token->text); token->text);
valid = false; valid = false;
} }
if (valid) if (valid)
{ {
state = (version < 300) ? VERSION_ENDLINE : VERSION_PROFILE; state = (version < 300) ? VERSION_ENDLINE : VERSION_PROFILE;
} }
break; break;
case VERSION_PROFILE: case VERSION_PROFILE:
if (token->type != Token::IDENTIFIER || token->text != "es") if (token->type != Token::IDENTIFIER || token->text != "es")
{ {
mDiagnostics->report(Diagnostics::PP_INVALID_VERSION_DIRECTIVE, mDiagnostics->report(Diagnostics::PP_INVALID_VERSION_DIRECTIVE, token->location,
token->location, token->text); token->text);
valid = false;
}
state = VERSION_ENDLINE;
break;
default:
mDiagnostics->report(Diagnostics::PP_UNEXPECTED_TOKEN, token->location,
token->text);
valid = false; valid = false;
} break;
state = VERSION_ENDLINE;
break;
default:
mDiagnostics->report(Diagnostics::PP_UNEXPECTED_TOKEN,
token->location, token->text);
valid = false;
break;
} }
mTokenizer->lex(token); mTokenizer->lex(token);
...@@ -817,15 +808,15 @@ void DirectiveParser::parseVersion(Token *token) ...@@ -817,15 +808,15 @@ void DirectiveParser::parseVersion(Token *token)
if (valid && (state != VERSION_ENDLINE)) if (valid && (state != VERSION_ENDLINE))
{ {
mDiagnostics->report(Diagnostics::PP_INVALID_VERSION_DIRECTIVE, mDiagnostics->report(Diagnostics::PP_INVALID_VERSION_DIRECTIVE, token->location,
token->location, token->text); token->text);
valid = false; valid = false;
} }
if (valid && version >= 300 && token->location.line > 1) if (valid && version >= 300 && token->location.line > 1)
{ {
mDiagnostics->report(Diagnostics::PP_VERSION_NOT_FIRST_LINE_ESSL3, mDiagnostics->report(Diagnostics::PP_VERSION_NOT_FIRST_LINE_ESSL3, token->location,
token->location, token->text); token->text);
valid = false; valid = false;
} }
...@@ -841,7 +832,7 @@ void DirectiveParser::parseLine(Token *token) ...@@ -841,7 +832,7 @@ void DirectiveParser::parseLine(Token *token)
{ {
ASSERT(getDirective(token) == DIRECTIVE_LINE); ASSERT(getDirective(token) == DIRECTIVE_LINE);
bool valid = true; bool valid = true;
bool parsedFileNumber = false; bool parsedFileNumber = false;
int line = 0, file = 0; int line = 0, file = 0;
...@@ -883,8 +874,8 @@ void DirectiveParser::parseLine(Token *token) ...@@ -883,8 +874,8 @@ void DirectiveParser::parseLine(Token *token)
{ {
if (valid) if (valid)
{ {
mDiagnostics->report(Diagnostics::PP_UNEXPECTED_TOKEN, mDiagnostics->report(Diagnostics::PP_UNEXPECTED_TOKEN, token->location,
token->location, token->text); token->text);
valid = false; valid = false;
} }
skipUntilEOD(mTokenizer, token); skipUntilEOD(mTokenizer, token);
...@@ -904,14 +895,14 @@ bool DirectiveParser::skipping() const ...@@ -904,14 +895,14 @@ bool DirectiveParser::skipping() const
if (mConditionalStack.empty()) if (mConditionalStack.empty())
return false; return false;
const ConditionalBlock& block = mConditionalStack.back(); const ConditionalBlock &block = mConditionalStack.back();
return block.skipBlock || block.skipGroup; return block.skipBlock || block.skipGroup;
} }
void DirectiveParser::parseConditionalIf(Token *token) void DirectiveParser::parseConditionalIf(Token *token)
{ {
ConditionalBlock block; ConditionalBlock block;
block.type = token->text; block.type = token->text;
block.location = token->location; block.location = token->location;
if (skipping()) if (skipping())
...@@ -930,20 +921,20 @@ void DirectiveParser::parseConditionalIf(Token *token) ...@@ -930,20 +921,20 @@ void DirectiveParser::parseConditionalIf(Token *token)
int expression = 0; int expression = 0;
switch (directive) switch (directive)
{ {
case DIRECTIVE_IF: case DIRECTIVE_IF:
expression = parseExpressionIf(token); expression = parseExpressionIf(token);
break; break;
case DIRECTIVE_IFDEF: case DIRECTIVE_IFDEF:
expression = parseExpressionIfdef(token); expression = parseExpressionIfdef(token);
break; break;
case DIRECTIVE_IFNDEF: case DIRECTIVE_IFNDEF:
expression = parseExpressionIfdef(token) == 0 ? 1 : 0; expression = parseExpressionIfdef(token) == 0 ? 1 : 0;
break; break;
default: default:
UNREACHABLE(); UNREACHABLE();
break; break;
} }
block.skipGroup = expression == 0; block.skipGroup = expression == 0;
block.foundValidGroup = expression != 0; block.foundValidGroup = expression != 0;
} }
mConditionalStack.push_back(block); mConditionalStack.push_back(block);
...@@ -960,7 +951,7 @@ int DirectiveParser::parseExpressionIf(Token *token) ...@@ -960,7 +951,7 @@ int DirectiveParser::parseExpressionIf(Token *token)
int expression = 0; int expression = 0;
ExpressionParser::ErrorSettings errorSettings; ExpressionParser::ErrorSettings errorSettings;
errorSettings.integerLiteralsMustFit32BitSignedRange = false; errorSettings.integerLiteralsMustFit32BitSignedRange = false;
errorSettings.unexpectedIdentifier = Diagnostics::PP_CONDITIONAL_UNEXPECTED_TOKEN; errorSettings.unexpectedIdentifier = Diagnostics::PP_CONDITIONAL_UNEXPECTED_TOKEN;
bool valid = true; bool valid = true;
expressionParser.parse(token, &expression, false, errorSettings, &valid); expressionParser.parse(token, &expression, false, errorSettings, &valid);
...@@ -968,8 +959,8 @@ int DirectiveParser::parseExpressionIf(Token *token) ...@@ -968,8 +959,8 @@ int DirectiveParser::parseExpressionIf(Token *token)
// Check if there are tokens after #if expression. // Check if there are tokens after #if expression.
if (!isEOD(token)) if (!isEOD(token))
{ {
mDiagnostics->report(Diagnostics::PP_CONDITIONAL_UNEXPECTED_TOKEN, mDiagnostics->report(Diagnostics::PP_CONDITIONAL_UNEXPECTED_TOKEN, token->location,
token->location, token->text); token->text);
skipUntilEOD(mTokenizer, token); skipUntilEOD(mTokenizer, token);
} }
...@@ -983,21 +974,20 @@ int DirectiveParser::parseExpressionIfdef(Token *token) ...@@ -983,21 +974,20 @@ int DirectiveParser::parseExpressionIfdef(Token *token)
mTokenizer->lex(token); mTokenizer->lex(token);
if (token->type != Token::IDENTIFIER) if (token->type != Token::IDENTIFIER)
{ {
mDiagnostics->report(Diagnostics::PP_UNEXPECTED_TOKEN, mDiagnostics->report(Diagnostics::PP_UNEXPECTED_TOKEN, token->location, token->text);
token->location, token->text);
skipUntilEOD(mTokenizer, token); skipUntilEOD(mTokenizer, token);
return 0; return 0;
} }
MacroSet::const_iterator iter = mMacroSet->find(token->text); MacroSet::const_iterator iter = mMacroSet->find(token->text);
int expression = iter != mMacroSet->end() ? 1 : 0; int expression = iter != mMacroSet->end() ? 1 : 0;
// Check if there are tokens after #ifdef expression. // Check if there are tokens after #ifdef expression.
mTokenizer->lex(token); mTokenizer->lex(token);
if (!isEOD(token)) if (!isEOD(token))
{ {
mDiagnostics->report(Diagnostics::PP_CONDITIONAL_UNEXPECTED_TOKEN, mDiagnostics->report(Diagnostics::PP_CONDITIONAL_UNEXPECTED_TOKEN, token->location,
token->location, token->text); token->text);
skipUntilEOD(mTokenizer, token); skipUntilEOD(mTokenizer, token);
} }
return expression; return expression;
......
...@@ -30,7 +30,6 @@ class DirectiveParser : public Lexer ...@@ -30,7 +30,6 @@ class DirectiveParser : public Lexer
void lex(Token *token) override; void lex(Token *token) override;
private: private:
void parseDirective(Token *token); void parseDirective(Token *token);
void parseDefine(Token *token); void parseDefine(Token *token);
void parseUndef(Token *token); void parseUndef(Token *token);
...@@ -61,16 +60,14 @@ class DirectiveParser : public Lexer ...@@ -61,16 +60,14 @@ class DirectiveParser : public Lexer
bool foundElseGroup; bool foundElseGroup;
ConditionalBlock() ConditionalBlock()
: skipBlock(false), : skipBlock(false), skipGroup(false), foundValidGroup(false), foundElseGroup(false)
skipGroup(false),
foundValidGroup(false),
foundElseGroup(false)
{ {
} }
}; };
bool mPastFirstStatement; bool mPastFirstStatement;
bool mSeenNonPreprocessorToken; // Tracks if a non-preprocessor token has been seen yet. Some macros, such as bool mSeenNonPreprocessorToken; // Tracks if a non-preprocessor token has been seen yet. Some
// #extension must be declared before all shader code. // macros, such as
// #extension must be declared before all shader code.
std::vector<ConditionalBlock> mConditionalStack; std::vector<ConditionalBlock> mConditionalStack;
Tokenizer *mTokenizer; Tokenizer *mTokenizer;
MacroSet *mMacroSet; MacroSet *mMacroSet;
......
...@@ -18,9 +18,8 @@ Input::Input() : mCount(0), mString(0) ...@@ -18,9 +18,8 @@ Input::Input() : mCount(0), mString(0)
{ {
} }
Input::Input(size_t count, const char *const string[], const int length[]) : Input::Input(size_t count, const char *const string[], const int length[])
mCount(count), : mCount(count), mString(string)
mString(string)
{ {
mLength.reserve(mCount); mLength.reserve(mCount);
for (size_t i = 0; i < mCount; ++i) for (size_t i = 0; i < mCount; ++i)
...@@ -87,7 +86,7 @@ size_t Input::read(char *buf, size_t maxSize, int *lineNo) ...@@ -87,7 +86,7 @@ size_t Input::read(char *buf, size_t maxSize, int *lineNo)
while ((nRead < maxRead) && (mReadLoc.sIndex < mCount)) while ((nRead < maxRead) && (mReadLoc.sIndex < mCount))
{ {
size_t size = mLength[mReadLoc.sIndex] - mReadLoc.cIndex; size_t size = mLength[mReadLoc.sIndex] - mReadLoc.cIndex;
size = std::min(size, maxSize); size = std::min(size, maxSize);
for (size_t i = 0; i < size; ++i) for (size_t i = 0; i < size; ++i)
{ {
// Stop if a possible line continuation is encountered. // Stop if a possible line continuation is encountered.
...@@ -95,7 +94,7 @@ size_t Input::read(char *buf, size_t maxSize, int *lineNo) ...@@ -95,7 +94,7 @@ size_t Input::read(char *buf, size_t maxSize, int *lineNo)
// and increments line number if necessary. // and increments line number if necessary.
if (*(mString[mReadLoc.sIndex] + mReadLoc.cIndex + i) == '\\') if (*(mString[mReadLoc.sIndex] + mReadLoc.cIndex + i) == '\\')
{ {
size = i; size = i;
maxRead = nRead + size; // Stop reading right before the backslash. maxRead = nRead + size; // Stop reading right before the backslash.
} }
} }
...@@ -114,4 +113,3 @@ size_t Input::read(char *buf, size_t maxSize, int *lineNo) ...@@ -114,4 +113,3 @@ size_t Input::read(char *buf, size_t maxSize, int *lineNo)
} }
} // namespace pp } // namespace pp
...@@ -20,18 +20,9 @@ class Input ...@@ -20,18 +20,9 @@ class Input
Input(); Input();
Input(size_t count, const char *const string[], const int length[]); Input(size_t count, const char *const string[], const int length[]);
size_t count() const size_t count() const { return mCount; }
{ const char *string(size_t index) const { return mString[index]; }
return mCount; size_t length(size_t index) const { return mLength[index]; }
}
const char *string(size_t index) const
{
return mString[index];
}
size_t length(size_t index) const
{
return mLength[index];
}
size_t read(char *buf, size_t maxSize, int *lineNo); size_t read(char *buf, size_t maxSize, int *lineNo);
...@@ -40,11 +31,7 @@ class Input ...@@ -40,11 +31,7 @@ class Input
size_t sIndex; // String index; size_t sIndex; // String index;
size_t cIndex; // Char index. size_t cIndex; // Char index.
Location() Location() : sIndex(0), cIndex(0) {}
: sIndex(0),
cIndex(0)
{
}
}; };
const Location &readLoc() const { return mReadLoc; } const Location &readLoc() const { return mReadLoc; }
...@@ -55,7 +42,7 @@ class Input ...@@ -55,7 +42,7 @@ class Input
// Input. // Input.
size_t mCount; size_t mCount;
const char * const *mString; const char *const *mString;
std::vector<size_t> mLength; std::vector<size_t> mLength;
Location mReadLoc; Location mReadLoc;
......
...@@ -14,9 +14,7 @@ namespace pp ...@@ -14,9 +14,7 @@ namespace pp
bool Macro::equals(const Macro &other) const bool Macro::equals(const Macro &other) const
{ {
return (type == other.type) && return (type == other.type) && (name == other.name) && (parameters == other.parameters) &&
(name == other.name) &&
(parameters == other.parameters) &&
(replacements == other.replacements); (replacements == other.replacements);
} }
...@@ -29,11 +27,10 @@ void PredefineMacro(MacroSet *macroSet, const char *name, int value) ...@@ -29,11 +27,10 @@ void PredefineMacro(MacroSet *macroSet, const char *name, int value)
Macro macro; Macro macro;
macro.predefined = true; macro.predefined = true;
macro.type = Macro::kTypeObj; macro.type = Macro::kTypeObj;
macro.name = name; macro.name = name;
macro.replacements.push_back(token); macro.replacements.push_back(token);
(*macroSet)[name] = macro; (*macroSet)[name] = macro;
} }
} // namespace pp } // namespace pp
...@@ -22,7 +22,7 @@ const size_t kMaxContextTokens = 10000; ...@@ -22,7 +22,7 @@ const size_t kMaxContextTokens = 10000;
class TokenLexer : public Lexer class TokenLexer : public Lexer
{ {
public: public:
typedef std::vector<Token> TokenVector; typedef std::vector<Token> TokenVector;
TokenLexer(TokenVector *tokens) TokenLexer(TokenVector *tokens)
...@@ -44,7 +44,7 @@ class TokenLexer : public Lexer ...@@ -44,7 +44,7 @@ class TokenLexer : public Lexer
} }
} }
private: private:
TokenVector mTokens; TokenVector mTokens;
TokenVector::const_iterator mIter; TokenVector::const_iterator mIter;
}; };
...@@ -115,7 +115,7 @@ void MacroExpander::lex(Token *token) ...@@ -115,7 +115,7 @@ void MacroExpander::lex(Token *token)
if (iter == mMacroSet->end()) if (iter == mMacroSet->end())
break; break;
const Macro& macro = iter->second; const Macro &macro = iter->second;
if (macro.disabled) if (macro.disabled)
{ {
// If a particular token is not expanded, it is never expanded. // If a particular token is not expanded, it is never expanded.
...@@ -205,7 +205,7 @@ bool MacroExpander::pushMacro(const Macro &macro, const Token &identifier) ...@@ -205,7 +205,7 @@ bool MacroExpander::pushMacro(const Macro &macro, const Token &identifier)
macro.disabled = true; macro.disabled = true;
MacroContext *context = new MacroContext; MacroContext *context = new MacroContext;
context->macro = &macro; context->macro = &macro;
context->replacements.swap(replacements); context->replacements.swap(replacements);
mContextStack.push_back(context); mContextStack.push_back(context);
mTotalTokensInContexts += context->replacements.size(); mTotalTokensInContexts += context->replacements.size();
...@@ -248,8 +248,7 @@ bool MacroExpander::expandMacro(const Macro &macro, ...@@ -248,8 +248,7 @@ bool MacroExpander::expandMacro(const Macro &macro,
SourceLocation replacementLocation = identifier.location; SourceLocation replacementLocation = identifier.location;
if (macro.type == Macro::kTypeObj) if (macro.type == Macro::kTypeObj)
{ {
replacements->assign(macro.replacements.begin(), replacements->assign(macro.replacements.begin(), macro.replacements.end());
macro.replacements.end());
if (macro.predefined) if (macro.predefined)
{ {
...@@ -257,7 +256,7 @@ bool MacroExpander::expandMacro(const Macro &macro, ...@@ -257,7 +256,7 @@ bool MacroExpander::expandMacro(const Macro &macro,
const char kFile[] = "__FILE__"; const char kFile[] = "__FILE__";
ASSERT(replacements->size() == 1); ASSERT(replacements->size() == 1);
Token& repl = replacements->front(); Token &repl = replacements->front();
if (macro.name == kLine) if (macro.name == kLine)
{ {
repl.text = ToString(identifier.location.line); repl.text = ToString(identifier.location.line);
...@@ -281,7 +280,7 @@ bool MacroExpander::expandMacro(const Macro &macro, ...@@ -281,7 +280,7 @@ bool MacroExpander::expandMacro(const Macro &macro,
for (std::size_t i = 0; i < replacements->size(); ++i) for (std::size_t i = 0; i < replacements->size(); ++i)
{ {
Token& repl = replacements->at(i); Token &repl = replacements->at(i);
if (i == 0) if (i == 0)
{ {
// The first token in the replacement list inherits the padding // The first token in the replacement list inherits the padding
...@@ -317,36 +316,36 @@ bool MacroExpander::collectMacroArgs(const Macro &macro, ...@@ -317,36 +316,36 @@ bool MacroExpander::collectMacroArgs(const Macro &macro,
if (token.type == Token::LAST) if (token.type == Token::LAST)
{ {
mDiagnostics->report(Diagnostics::PP_MACRO_UNTERMINATED_INVOCATION, mDiagnostics->report(Diagnostics::PP_MACRO_UNTERMINATED_INVOCATION, identifier.location,
identifier.location, identifier.text); identifier.text);
// Do not lose EOF token. // Do not lose EOF token.
ungetToken(token); ungetToken(token);
return false; return false;
} }
bool isArg = false; // True if token is part of the current argument. bool isArg = false; // True if token is part of the current argument.
switch (token.type) switch (token.type)
{ {
case '(': case '(':
++openParens; ++openParens;
isArg = true; isArg = true;
break; break;
case ')': case ')':
--openParens; --openParens;
isArg = openParens != 0; isArg = openParens != 0;
*closingParenthesisLocation = token.location; *closingParenthesisLocation = token.location;
break; break;
case ',': case ',':
// The individual arguments are separated by comma tokens, but // The individual arguments are separated by comma tokens, but
// the comma tokens between matching inner parentheses do not // the comma tokens between matching inner parentheses do not
// seperate arguments. // seperate arguments.
if (openParens == 1) if (openParens == 1)
args->push_back(MacroArg()); args->push_back(MacroArg());
isArg = openParens != 1; isArg = openParens != 1;
break; break;
default: default:
isArg = true; isArg = true;
break; break;
} }
if (isArg) if (isArg)
{ {
...@@ -367,9 +366,9 @@ bool MacroExpander::collectMacroArgs(const Macro &macro, ...@@ -367,9 +366,9 @@ bool MacroExpander::collectMacroArgs(const Macro &macro,
// Validate the number of arguments. // Validate the number of arguments.
if (args->size() != params.size()) if (args->size() != params.size())
{ {
Diagnostics::ID id = args->size() < macro.parameters.size() ? Diagnostics::ID id = args->size() < macro.parameters.size()
Diagnostics::PP_MACRO_TOO_FEW_ARGS : ? Diagnostics::PP_MACRO_TOO_FEW_ARGS
Diagnostics::PP_MACRO_TOO_MANY_ARGS; : Diagnostics::PP_MACRO_TOO_MANY_ARGS;
mDiagnostics->report(id, identifier.location, identifier.text); mDiagnostics->report(id, identifier.location, identifier.text);
return false; return false;
} }
...@@ -430,15 +429,15 @@ void MacroExpander::replaceMacroParams(const Macro &macro, ...@@ -430,15 +429,15 @@ void MacroExpander::replaceMacroParams(const Macro &macro,
// TODO(alokp): Optimize this. // TODO(alokp): Optimize this.
// There is no need to search for macro params every time. // There is no need to search for macro params every time.
// The param index can be cached with the replacement token. // The param index can be cached with the replacement token.
Macro::Parameters::const_iterator iter = std::find( Macro::Parameters::const_iterator iter =
macro.parameters.begin(), macro.parameters.end(), repl.text); std::find(macro.parameters.begin(), macro.parameters.end(), repl.text);
if (iter == macro.parameters.end()) if (iter == macro.parameters.end())
{ {
replacements->push_back(repl); replacements->push_back(repl);
continue; continue;
} }
std::size_t iArg = std::distance(macro.parameters.begin(), iter); std::size_t iArg = std::distance(macro.parameters.begin(), iter);
const MacroArg &arg = args[iArg]; const MacroArg &arg = args[iArg];
if (arg.empty()) if (arg.empty())
{ {
...@@ -473,4 +472,3 @@ void MacroExpander::MacroContext::unget() ...@@ -473,4 +472,3 @@ void MacroExpander::MacroContext::unget()
} }
} // namespace pp } // namespace pp
...@@ -38,9 +38,7 @@ class MacroExpander : public Lexer ...@@ -38,9 +38,7 @@ class MacroExpander : public Lexer
bool pushMacro(const Macro &macro, const Token &identifier); bool pushMacro(const Macro &macro, const Token &identifier);
void popMacro(); void popMacro();
bool expandMacro(const Macro &macro, bool expandMacro(const Macro &macro, const Token &identifier, std::vector<Token> *replacements);
const Token &identifier,
std::vector<Token> *replacements);
typedef std::vector<Token> MacroArg; typedef std::vector<Token> MacroArg;
bool collectMacroArgs(const Macro &macro, bool collectMacroArgs(const Macro &macro,
......
...@@ -52,9 +52,7 @@ Preprocessor::~Preprocessor() ...@@ -52,9 +52,7 @@ Preprocessor::~Preprocessor()
delete mImpl; delete mImpl;
} }
bool Preprocessor::init(size_t count, bool Preprocessor::init(size_t count, const char *const string[], const int length[])
const char * const string[],
const int length[])
{ {
static const int kDefaultGLSLVersion = 100; static const int kDefaultGLSLVersion = 100;
...@@ -80,23 +78,23 @@ void Preprocessor::lex(Token *token) ...@@ -80,23 +78,23 @@ void Preprocessor::lex(Token *token)
mImpl->macroExpander.lex(token); mImpl->macroExpander.lex(token);
switch (token->type) switch (token->type)
{ {
// We should not be returning internal preprocessing tokens. // We should not be returning internal preprocessing tokens.
// Convert preprocessing tokens to compiler tokens or report // Convert preprocessing tokens to compiler tokens or report
// diagnostics. // diagnostics.
case Token::PP_HASH: case Token::PP_HASH:
UNREACHABLE(); UNREACHABLE();
break; break;
case Token::PP_NUMBER: case Token::PP_NUMBER:
mImpl->diagnostics->report(Diagnostics::PP_INVALID_NUMBER, mImpl->diagnostics->report(Diagnostics::PP_INVALID_NUMBER, token->location,
token->location, token->text); token->text);
break; break;
case Token::PP_OTHER: case Token::PP_OTHER:
mImpl->diagnostics->report(Diagnostics::PP_INVALID_CHARACTER, mImpl->diagnostics->report(Diagnostics::PP_INVALID_CHARACTER, token->location,
token->location, token->text); token->text);
break; break;
default: default:
validToken = true; validToken = true;
break; break;
} }
} }
} }
......
...@@ -42,7 +42,7 @@ class Preprocessor : angle::NonCopyable ...@@ -42,7 +42,7 @@ class Preprocessor : angle::NonCopyable
// Each element in the length array may contain the length of the // Each element in the length array may contain the length of the
// corresponding string or a value less than 0 to indicate that the string // corresponding string or a value less than 0 to indicate that the string
// is null terminated. // is null terminated.
bool init(size_t count, const char * const string[], const int length[]); bool init(size_t count, const char *const string[], const int length[]);
// Adds a pre-defined macro. // Adds a pre-defined macro.
void predefineMacro(const char *name, int value); void predefineMacro(const char *name, int value);
......
...@@ -12,16 +12,8 @@ namespace pp ...@@ -12,16 +12,8 @@ namespace pp
struct SourceLocation struct SourceLocation
{ {
SourceLocation() SourceLocation() : file(0), line(0) {}
: file(0), SourceLocation(int f, int l) : file(f), line(l) {}
line(0)
{
}
SourceLocation(int f, int l)
: file(f),
line(l)
{
}
bool equals(const SourceLocation &other) const bool equals(const SourceLocation &other) const
{ {
......
...@@ -14,17 +14,15 @@ namespace pp ...@@ -14,17 +14,15 @@ namespace pp
void Token::reset() void Token::reset()
{ {
type = 0; type = 0;
flags = 0; flags = 0;
location = SourceLocation(); location = SourceLocation();
text.clear(); text.clear();
} }
bool Token::equals(const Token &other) const bool Token::equals(const Token &other) const
{ {
return (type == other.type) && return (type == other.type) && (flags == other.flags) && (location == other.location) &&
(flags == other.flags) &&
(location == other.location) &&
(text == other.text); (text == other.text);
} }
......
...@@ -62,33 +62,20 @@ struct Token ...@@ -62,33 +62,20 @@ struct Token
EXPANSION_DISABLED = 1 << 2 EXPANSION_DISABLED = 1 << 2
}; };
Token() Token() : type(0), flags(0) {}
: type(0),
flags(0)
{
}
void reset(); void reset();
bool equals(const Token &other) const; bool equals(const Token &other) const;
// Returns true if this is the first token on line. // Returns true if this is the first token on line.
// It disregards any leading whitespace. // It disregards any leading whitespace.
bool atStartOfLine() const bool atStartOfLine() const { return (flags & AT_START_OF_LINE) != 0; }
{
return (flags & AT_START_OF_LINE) != 0;
}
void setAtStartOfLine(bool start); void setAtStartOfLine(bool start);
bool hasLeadingSpace() const bool hasLeadingSpace() const { return (flags & HAS_LEADING_SPACE) != 0; }
{
return (flags & HAS_LEADING_SPACE) != 0;
}
void setHasLeadingSpace(bool space); void setHasLeadingSpace(bool space);
bool expansionDisabled() const bool expansionDisabled() const { return (flags & EXPANSION_DISABLED) != 0; }
{
return (flags & EXPANSION_DISABLED) != 0;
}
void setExpansionDisabled(bool disable); void setExpansionDisabled(bool disable);
// Converts text into numeric value for CONST_INT and CONST_FLOAT token. // Converts text into numeric value for CONST_INT and CONST_FLOAT token.
......
...@@ -36,7 +36,7 @@ class Tokenizer : public Lexer ...@@ -36,7 +36,7 @@ class Tokenizer : public Lexer
Tokenizer(Diagnostics *diagnostics); Tokenizer(Diagnostics *diagnostics);
~Tokenizer(); ~Tokenizer();
bool init(size_t count, const char * const string[], const int length[]); bool init(size_t count, const char *const string[], const int length[]);
void setFileNumber(int file); void setFileNumber(int file);
void setLineNumber(int line); void setLineNumber(int line);
...@@ -48,9 +48,9 @@ class Tokenizer : public Lexer ...@@ -48,9 +48,9 @@ class Tokenizer : public Lexer
bool initScanner(); bool initScanner();
void destroyScanner(); void destroyScanner();
void *mHandle; // Scanner handle. void *mHandle; // Scanner handle.
Context mContext; // Scanner extra. Context mContext; // Scanner extra.
size_t mMaxTokenSize; // Maximum token size size_t mMaxTokenSize; // Maximum token size
}; };
} // namespace pp } // namespace pp
......
...@@ -12,13 +12,12 @@ ...@@ -12,13 +12,12 @@
#include <cmath> #include <cmath>
#include <sstream> #include <sstream>
namespace pp { namespace pp
{
inline std::ios::fmtflags numeric_base_int(const std::string &str) inline std::ios::fmtflags numeric_base_int(const std::string &str)
{ {
if ((str.size() >= 2) && if ((str.size() >= 2) && (str[0] == '0') && (str[1] == 'x' || str[1] == 'X'))
(str[0] == '0') &&
(str[1] == 'x' || str[1] == 'X'))
{ {
return std::ios::hex; return std::ios::hex;
} }
...@@ -34,7 +33,7 @@ inline std::ios::fmtflags numeric_base_int(const std::string &str) ...@@ -34,7 +33,7 @@ inline std::ios::fmtflags numeric_base_int(const std::string &str)
// of the correct form. They can only fail if the parsed value is too big, // of the correct form. They can only fail if the parsed value is too big,
// in which case false is returned. // in which case false is returned.
template<typename IntType> template <typename IntType>
bool numeric_lex_int(const std::string &str, IntType *value) bool numeric_lex_int(const std::string &str, IntType *value)
{ {
std::istringstream stream(str); std::istringstream stream(str);
...@@ -46,7 +45,7 @@ bool numeric_lex_int(const std::string &str, IntType *value) ...@@ -46,7 +45,7 @@ bool numeric_lex_int(const std::string &str, IntType *value)
return !stream.fail(); return !stream.fail();
} }
template<typename FloatType> template <typename FloatType>
bool numeric_lex_float(const std::string &str, FloatType *value) bool numeric_lex_float(const std::string &str, FloatType *value)
{ {
// On 64-bit Intel Android, istringstream is broken. Until this is fixed in // On 64-bit Intel Android, istringstream is broken. Until this is fixed in
...@@ -68,6 +67,6 @@ bool numeric_lex_float(const std::string &str, FloatType *value) ...@@ -68,6 +67,6 @@ bool numeric_lex_float(const std::string &str, FloatType *value)
#endif #endif
} }
} // namespace pp. } // namespace pp.
#endif // COMPILER_PREPROCESSOR_NUMERICLEX_H_ #endif // COMPILER_PREPROCESSOR_NUMERICLEX_H_
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment