Commit d526f989 by Zhenyao Mo

Fix code style violation in compiler/preprocessor

BUG=angle:650 TEST=no behavior change Change-Id: Ib52f15f6471fc7897b66d11baee11216cf08158a Reviewed-on: https://chromium-review.googlesource.com/199591Reviewed-by: 's avatarJamie Madill <jmadill@chromium.org> Reviewed-by: 's avatarZhenyao Mo <zmo@chromium.org> Tested-by: 's avatarZhenyao Mo <zmo@chromium.org>
parent ae1990c8
......@@ -16,8 +16,8 @@ Diagnostics::~Diagnostics()
}
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.
print(id, loc, text);
......@@ -41,86 +41,86 @@ std::string Diagnostics::message(ID id)
{
// Errors begin.
case PP_INTERNAL_ERROR:
return "internal error";
return "internal error";
case PP_OUT_OF_MEMORY:
return "out of memory";
return "out of memory";
case PP_INVALID_CHARACTER:
return "invalid character";
return "invalid character";
case PP_INVALID_NUMBER:
return "invalid number";
return "invalid number";
case PP_INTEGER_OVERFLOW:
return "integer overflow";
return "integer overflow";
case PP_FLOAT_OVERFLOW:
return "float overflow";
return "float overflow";
case PP_TOKEN_TOO_LONG:
return "token too long";
return "token too long";
case PP_INVALID_EXPRESSION:
return "invalid expression";
return "invalid expression";
case PP_DIVISION_BY_ZERO:
return "division by zero";
return "division by zero";
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:
return "unexpected token";
return "unexpected token";
case PP_DIRECTIVE_INVALID_NAME:
return "invalid directive name";
return "invalid directive name";
case PP_MACRO_NAME_RESERVED:
return "macro name is reserved";
return "macro name is reserved";
case PP_MACRO_REDEFINED:
return "macro redefined";
return "macro redefined";
case PP_MACRO_PREDEFINED_REDEFINED:
return "predefined macro redefined";
return "predefined macro redefined";
case PP_MACRO_PREDEFINED_UNDEFINED:
return "predefined macro undefined";
return "predefined macro undefined";
case PP_MACRO_UNTERMINATED_INVOCATION:
return "unterminated macro invocation";
return "unterminated macro invocation";
case PP_MACRO_TOO_FEW_ARGS:
return "Not enough arguments for macro";
return "Not enough arguments for macro";
case PP_MACRO_TOO_MANY_ARGS:
return "Too many arguments for macro";
return "Too many arguments for macro";
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:
return "unexpected #else found without a matching #if";
return "unexpected #else found without a matching #if";
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:
return "unexpected #elif found without a matching #if";
return "unexpected #elif found without a matching #if";
case PP_CONDITIONAL_ELIF_AFTER_ELSE:
return "unexpected #elif found after #else";
return "unexpected #elif found after #else";
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:
return "invalid extension name";
return "invalid extension name";
case PP_INVALID_EXTENSION_BEHAVIOR:
return "invalid extension behavior";
return "invalid extension behavior";
case PP_INVALID_EXTENSION_DIRECTIVE:
return "invalid extension directive";
return "invalid extension directive";
case PP_INVALID_VERSION_NUMBER:
return "invalid version number";
return "invalid version number";
case PP_INVALID_VERSION_DIRECTIVE:
return "invalid version directive";
return "invalid version directive";
case PP_VERSION_NOT_FIRST_STATEMENT:
return "#version directive must occur before anything else, "
"except for comments and white space";
case PP_INVALID_LINE_NUMBER:
return "invalid line number";
return "invalid line number";
case PP_INVALID_FILE_NUMBER:
return "invalid file number";
return "invalid file number";
case PP_INVALID_LINE_DIRECTIVE:
return "invalid line directive";
return "invalid line directive";
// Errors end.
// Warnings begin.
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:
return "unexpected token after conditional expression";
return "unexpected token after conditional expression";
case PP_UNRECOGNIZED_PRAGMA:
return "unrecognized pragma";
return "unrecognized pragma";
// Warnings end.
default:
assert(false);
return "";
assert(false);
return "";
}
}
......
......@@ -72,15 +72,15 @@ class Diagnostics
virtual ~Diagnostics();
void report(ID id, const SourceLocation& loc, const std::string& text);
void report(ID id, const SourceLocation &loc, const std::string &text);
protected:
Severity severity(ID id);
std::string message(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
......
......@@ -23,19 +23,19 @@ class DirectiveHandler
public:
virtual ~DirectiveHandler();
virtual void handleError(const SourceLocation& loc,
const std::string& msg) = 0;
virtual void handleError(const SourceLocation &loc,
const std::string &msg) = 0;
// Handle pragma of form: #pragma name[(value)]
virtual void handlePragma(const SourceLocation& loc,
const std::string& name,
const std::string& value) = 0;
virtual void handlePragma(const SourceLocation &loc,
const std::string &name,
const std::string &value) = 0;
virtual void handleExtension(const SourceLocation& loc,
const std::string& name,
const std::string& behavior) = 0;
virtual void handleExtension(const SourceLocation &loc,
const std::string &name,
const std::string &behavior) = 0;
virtual void handleVersion(const SourceLocation& loc,
virtual void handleVersion(const SourceLocation &loc,
int version) = 0;
};
......
......@@ -22,35 +22,35 @@ class Tokenizer;
class DirectiveParser : public Lexer
{
public:
DirectiveParser(Tokenizer* tokenizer,
MacroSet* macroSet,
Diagnostics* diagnostics,
DirectiveHandler* directiveHandler);
DirectiveParser(Tokenizer *tokenizer,
MacroSet *macroSet,
Diagnostics *diagnostics,
DirectiveHandler *directiveHandler);
virtual void lex(Token* token);
virtual void lex(Token *token);
private:
PP_DISALLOW_COPY_AND_ASSIGN(DirectiveParser);
void parseDirective(Token* token);
void parseDefine(Token* token);
void parseUndef(Token* token);
void parseIf(Token* token);
void parseIfdef(Token* token);
void parseIfndef(Token* token);
void parseElse(Token* token);
void parseElif(Token* token);
void parseEndif(Token* token);
void parseError(Token* token);
void parsePragma(Token* token);
void parseExtension(Token* token);
void parseVersion(Token* token);
void parseLine(Token* token);
void parseDirective(Token *token);
void parseDefine(Token *token);
void parseUndef(Token *token);
void parseIf(Token *token);
void parseIfdef(Token *token);
void parseIfndef(Token *token);
void parseElse(Token *token);
void parseElif(Token *token);
void parseEndif(Token *token);
void parseError(Token *token);
void parsePragma(Token *token);
void parseExtension(Token *token);
void parseVersion(Token *token);
void parseLine(Token *token);
bool skipping() const;
void parseConditionalIf(Token* token);
int parseExpressionIf(Token* token);
int parseExpressionIfdef(Token* token);
void parseConditionalIf(Token *token);
int parseExpressionIf(Token *token);
int parseExpressionIfdef(Token *token);
struct ConditionalBlock
{
......@@ -61,20 +61,20 @@ class DirectiveParser : public Lexer
bool foundValidGroup;
bool foundElseGroup;
ConditionalBlock() :
skipBlock(false),
skipGroup(false),
foundValidGroup(false),
foundElseGroup(false)
ConditionalBlock()
: skipBlock(false),
skipGroup(false),
foundValidGroup(false),
foundElseGroup(false)
{
}
};
bool mPastFirstStatement;
std::vector<ConditionalBlock> mConditionalStack;
Tokenizer* mTokenizer;
MacroSet* mMacroSet;
Diagnostics* mDiagnostics;
DirectiveHandler* mDirectiveHandler;
Tokenizer *mTokenizer;
MacroSet *mMacroSet;
Diagnostics *mDiagnostics;
DirectiveHandler *mDirectiveHandler;
};
} // namespace pp
......
......@@ -1891,15 +1891,14 @@ yyreturn:
int yylex(YYSTYPE* lvalp, Context* context)
int yylex(YYSTYPE *lvalp, Context *context)
{
int type = 0;
pp::Token* token = context->token;
pp::Token *token = context->token;
switch (token->type)
{
case pp::Token::CONST_INT:
{
case pp::Token::CONST_INT: {
unsigned int val = 0;
if (!token->uValue(&val))
{
......@@ -1910,39 +1909,59 @@ int yylex(YYSTYPE* lvalp, Context* context)
type = TOK_CONST_INT;
break;
}
case pp::Token::OP_OR: type = TOK_OP_OR; break;
case pp::Token::OP_AND: type = TOK_OP_AND; break;
case pp::Token::OP_NE: type = TOK_OP_NE; break;
case pp::Token::OP_EQ: type = TOK_OP_EQ; break;
case pp::Token::OP_GE: type = TOK_OP_GE; break;
case pp::Token::OP_LE: type = TOK_OP_LE; break;
case pp::Token::OP_RIGHT: type = TOK_OP_RIGHT; break;
case pp::Token::OP_LEFT: type = TOK_OP_LEFT; break;
case '|': type = '|'; break;
case '^': type = '^'; break;
case '&': type = '&'; break;
case '>': type = '>'; break;
case '<': type = '<'; break;
case '-': type = '-'; break;
case '+': type = '+'; break;
case '%': type = '%'; break;
case '/': type = '/'; break;
case '*': type = '*'; break;
case '!': type = '!'; break;
case '~': type = '~'; break;
case '(': type = '('; break;
case ')': type = ')'; break;
case pp::Token::OP_OR:
type = TOK_OP_OR;
break;
case pp::Token::OP_AND:
type = TOK_OP_AND;
break;
case pp::Token::OP_NE:
type = TOK_OP_NE;
break;
case pp::Token::OP_EQ:
type = TOK_OP_EQ;
break;
case pp::Token::OP_GE:
type = TOK_OP_GE;
break;
case pp::Token::OP_LE:
type = TOK_OP_LE;
break;
case pp::Token::OP_RIGHT:
type = TOK_OP_RIGHT;
break;
case pp::Token::OP_LEFT:
type = TOK_OP_LEFT;
break;
case '|':
case '^':
case '&':
case '>':
case '<':
case '-':
case '+':
case '%':
case '/':
case '*':
case '!':
case '~':
case '(':
case ')':
type = token->type;
break;
default: break;
default:
break;
}
// Advance to the next token if the current one is valid.
if (type != 0) context->lexer->lex(token);
if (type != 0)
context->lexer->lex(token);
return type;
}
void yyerror(Context* context, const char* reason)
void yyerror(Context *context, const char *reason)
{
context->diagnostics->report(pp::Diagnostics::PP_INVALID_EXPRESSION,
context->token->location,
......@@ -1951,13 +1970,13 @@ void yyerror(Context* context, const char* reason)
namespace pp {
ExpressionParser::ExpressionParser(Lexer* lexer, Diagnostics* diagnostics) :
mLexer(lexer),
mDiagnostics(diagnostics)
ExpressionParser::ExpressionParser(Lexer *lexer, Diagnostics *diagnostics)
: mLexer(lexer),
mDiagnostics(diagnostics)
{
}
bool ExpressionParser::parse(Token* token, int* result)
bool ExpressionParser::parse(Token *token, int *result)
{
Context context;
context.diagnostics = mDiagnostics;
......
......@@ -19,15 +19,15 @@ struct Token;
class ExpressionParser
{
public:
ExpressionParser(Lexer* lexer, Diagnostics* diagnostics);
ExpressionParser(Lexer *lexer, Diagnostics *diagnostics);
bool parse(Token* token, int* result);
bool parse(Token *token, int *result);
private:
PP_DISALLOW_COPY_AND_ASSIGN(ExpressionParser);
Lexer* mLexer;
Diagnostics* mDiagnostics;
Lexer *mLexer;
Diagnostics *mDiagnostics;
};
} // namespace pp
......
......@@ -195,15 +195,14 @@ expression
%%
int yylex(YYSTYPE* lvalp, Context* context)
int yylex(YYSTYPE *lvalp, Context *context)
{
int type = 0;
pp::Token* token = context->token;
pp::Token *token = context->token;
switch (token->type)
{
case pp::Token::CONST_INT:
{
case pp::Token::CONST_INT: {
unsigned int val = 0;
if (!token->uValue(&val))
{
......@@ -214,39 +213,59 @@ int yylex(YYSTYPE* lvalp, Context* context)
type = TOK_CONST_INT;
break;
}
case pp::Token::OP_OR: type = TOK_OP_OR; break;
case pp::Token::OP_AND: type = TOK_OP_AND; break;
case pp::Token::OP_NE: type = TOK_OP_NE; break;
case pp::Token::OP_EQ: type = TOK_OP_EQ; break;
case pp::Token::OP_GE: type = TOK_OP_GE; break;
case pp::Token::OP_LE: type = TOK_OP_LE; break;
case pp::Token::OP_RIGHT: type = TOK_OP_RIGHT; break;
case pp::Token::OP_LEFT: type = TOK_OP_LEFT; break;
case '|': type = '|'; break;
case '^': type = '^'; break;
case '&': type = '&'; break;
case '>': type = '>'; break;
case '<': type = '<'; break;
case '-': type = '-'; break;
case '+': type = '+'; break;
case '%': type = '%'; break;
case '/': type = '/'; break;
case '*': type = '*'; break;
case '!': type = '!'; break;
case '~': type = '~'; break;
case '(': type = '('; break;
case ')': type = ')'; break;
case pp::Token::OP_OR:
type = TOK_OP_OR;
break;
case pp::Token::OP_AND:
type = TOK_OP_AND;
break;
case pp::Token::OP_NE:
type = TOK_OP_NE;
break;
case pp::Token::OP_EQ:
type = TOK_OP_EQ;
break;
case pp::Token::OP_GE:
type = TOK_OP_GE;
break;
case pp::Token::OP_LE:
type = TOK_OP_LE;
break;
case pp::Token::OP_RIGHT:
type = TOK_OP_RIGHT;
break;
case pp::Token::OP_LEFT:
type = TOK_OP_LEFT;
break;
case '|':
case '^':
case '&':
case '>':
case '<':
case '-':
case '+':
case '%':
case '/':
case '*':
case '!':
case '~':
case '(':
case ')':
type = token->type;
break;
default: break;
default:
break;
}
// Advance to the next token if the current one is valid.
if (type != 0) context->lexer->lex(token);
if (type != 0)
context->lexer->lex(token);
return type;
}
void yyerror(Context* context, const char* reason)
void yyerror(Context *context, const char *reason)
{
context->diagnostics->report(pp::Diagnostics::PP_INVALID_EXPRESSION,
context->token->location,
......@@ -255,13 +274,13 @@ void yyerror(Context* context, const char* reason)
namespace pp {
ExpressionParser::ExpressionParser(Lexer* lexer, Diagnostics* diagnostics) :
mLexer(lexer),
mDiagnostics(diagnostics)
ExpressionParser::ExpressionParser(Lexer *lexer, Diagnostics *diagnostics)
: mLexer(lexer),
mDiagnostics(diagnostics)
{
}
bool ExpressionParser::parse(Token* token, int* result)
bool ExpressionParser::parse(Token *token, int *result)
{
Context context;
context.diagnostics = mDiagnostics;
......
......@@ -17,7 +17,7 @@ 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),
mString(string)
{
......@@ -29,7 +29,7 @@ Input::Input(size_t count, const char* const string[], const int length[]) :
}
}
size_t Input::read(char* buf, size_t maxSize)
size_t Input::read(char *buf, size_t maxSize)
{
size_t nRead = 0;
while ((nRead < maxSize) && (mReadLoc.sIndex < mCount))
......
......@@ -18,27 +18,40 @@ class Input
{
public:
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 { return mCount; }
const char* string(size_t index) const { return mString[index]; }
size_t length(size_t index) const { return mLength[index]; }
size_t count() const
{
return mCount;
}
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);
size_t read(char *buf, size_t maxSize);
struct Location
{
size_t sIndex; // String index;
size_t cIndex; // Char index.
Location() : sIndex(0), cIndex(0) { }
Location()
: sIndex(0),
cIndex(0)
{
}
};
const Location& readLoc() const { return mReadLoc; }
const Location &readLoc() const { return mReadLoc; }
private:
// Input.
size_t mCount;
const char* const* mString;
const char * const *mString;
std::vector<size_t> mLength;
Location mReadLoc;
......
......@@ -17,7 +17,7 @@ class Lexer
public:
virtual ~Lexer();
virtual void lex(Token* token) = 0;
virtual void lex(Token *token) = 0;
};
} // namespace pp
......
......@@ -11,7 +11,7 @@
namespace pp
{
bool Macro::equals(const Macro& other) const
bool Macro::equals(const Macro &other) const
{
return (type == other.type) &&
(name == other.name) &&
......
......@@ -26,8 +26,13 @@ struct Macro
typedef std::vector<std::string> Parameters;
typedef std::vector<Token> Replacements;
Macro() : predefined(false), disabled(false), type(kTypeObj) { }
bool equals(const Macro& other) const;
Macro()
: predefined(false),
disabled(false),
type(kTypeObj)
{
}
bool equals(const Macro &other) const;
bool predefined;
mutable bool disabled;
......
......@@ -20,13 +20,13 @@ class TokenLexer : public Lexer
public:
typedef std::vector<Token> TokenVector;
TokenLexer(TokenVector* tokens)
TokenLexer(TokenVector *tokens)
{
tokens->swap(mTokens);
mIter = mTokens.begin();
}
virtual void lex(Token* token)
virtual void lex(Token *token)
{
if (mIter == mTokens.end())
{
......@@ -46,12 +46,12 @@ class TokenLexer : public Lexer
TokenVector::const_iterator mIter;
};
MacroExpander::MacroExpander(Lexer* lexer,
MacroSet* macroSet,
Diagnostics* diagnostics) :
mLexer(lexer),
mMacroSet(macroSet),
mDiagnostics(diagnostics)
MacroExpander::MacroExpander(Lexer *lexer,
MacroSet *macroSet,
Diagnostics *diagnostics)
: mLexer(lexer),
mMacroSet(macroSet),
mDiagnostics(diagnostics)
{
}
......@@ -63,7 +63,7 @@ MacroExpander::~MacroExpander()
}
}
void MacroExpander::lex(Token* token)
void MacroExpander::lex(Token *token)
{
while (true)
{
......@@ -97,7 +97,7 @@ void MacroExpander::lex(Token* token)
}
}
void MacroExpander::getToken(Token* token)
void MacroExpander::getToken(Token *token)
{
if (mReserveToken.get())
{
......@@ -122,11 +122,11 @@ void MacroExpander::getToken(Token* token)
}
}
void MacroExpander::ungetToken(const Token& token)
void MacroExpander::ungetToken(const Token &token)
{
if (!mContextStack.empty())
{
MacroContext* context = mContextStack.back();
MacroContext *context = mContextStack.back();
context->unget();
assert(context->replacements[context->index] == token);
}
......@@ -148,7 +148,7 @@ bool MacroExpander::isNextTokenLeftParen()
return lparen;
}
bool MacroExpander::pushMacro(const Macro& macro, const Token& identifier)
bool MacroExpander::pushMacro(const Macro &macro, const Token &identifier)
{
assert(!macro.disabled);
assert(!identifier.expansionDisabled());
......@@ -162,7 +162,7 @@ bool MacroExpander::pushMacro(const Macro& macro, const Token& identifier)
// Macro is disabled for expansion until it is popped off the stack.
macro.disabled = true;
MacroContext* context = new MacroContext;
MacroContext *context = new MacroContext;
context->macro = &macro;
context->replacements.swap(replacements);
mContextStack.push_back(context);
......@@ -173,7 +173,7 @@ void MacroExpander::popMacro()
{
assert(!mContextStack.empty());
MacroContext* context = mContextStack.back();
MacroContext *context = mContextStack.back();
mContextStack.pop_back();
assert(context->empty());
......@@ -182,9 +182,9 @@ void MacroExpander::popMacro()
delete context;
}
bool MacroExpander::expandMacro(const Macro& macro,
const Token& identifier,
std::vector<Token>* replacements)
bool MacroExpander::expandMacro(const Macro &macro,
const Token &identifier,
std::vector<Token> *replacements)
{
replacements->clear();
if (macro.type == Macro::kTypeObj)
......@@ -239,9 +239,9 @@ bool MacroExpander::expandMacro(const Macro& macro,
return true;
}
bool MacroExpander::collectMacroArgs(const Macro& macro,
const Token& identifier,
std::vector<MacroArg>* args)
bool MacroExpander::collectMacroArgs(const Macro &macro,
const Token &identifier,
std::vector<MacroArg> *args)
{
Token token;
getToken(&token);
......@@ -276,7 +276,8 @@ bool MacroExpander::collectMacroArgs(const Macro& macro,
// The individual arguments are separated by comma tokens, but
// the comma tokens between matching inner parentheses do not
// seperate arguments.
if (openParens == 1) args->push_back(MacroArg());
if (openParens == 1)
args->push_back(MacroArg());
isArg = openParens != 1;
break;
default:
......@@ -285,14 +286,15 @@ bool MacroExpander::collectMacroArgs(const Macro& macro,
}
if (isArg)
{
MacroArg& arg = args->back();
MacroArg &arg = args->back();
// Initial whitespace is not part of the argument.
if (arg.empty()) token.setHasLeadingSpace(false);
if (arg.empty())
token.setHasLeadingSpace(false);
arg.push_back(token);
}
}
const Macro::Parameters& params = macro.parameters;
const Macro::Parameters &params = macro.parameters;
// If there is only one empty argument, it is equivalent to no argument.
if (params.empty() && (args->size() == 1) && args->front().empty())
{
......@@ -313,7 +315,7 @@ bool MacroExpander::collectMacroArgs(const Macro& macro,
// inserted into the macro body.
for (std::size_t i = 0; i < args->size(); ++i)
{
MacroArg& arg = args->at(i);
MacroArg &arg = args->at(i);
TokenLexer lexer(&arg);
MacroExpander expander(&lexer, mMacroSet, mDiagnostics);
......@@ -328,13 +330,13 @@ bool MacroExpander::collectMacroArgs(const Macro& macro,
return true;
}
void MacroExpander::replaceMacroParams(const Macro& macro,
const std::vector<MacroArg>& args,
std::vector<Token>* replacements)
void MacroExpander::replaceMacroParams(const Macro &macro,
const std::vector<MacroArg> &args,
std::vector<Token> *replacements)
{
for (std::size_t i = 0; i < macro.replacements.size(); ++i)
{
const Token& repl = macro.replacements[i];
const Token &repl = macro.replacements[i];
if (repl.type != Token::IDENTIFIER)
{
replacements->push_back(repl);
......@@ -353,7 +355,7 @@ void MacroExpander::replaceMacroParams(const Macro& macro,
}
std::size_t iArg = std::distance(macro.parameters.begin(), iter);
const MacroArg& arg = args[iArg];
const MacroArg &arg = args[iArg];
if (arg.empty())
{
continue;
......
......@@ -23,51 +23,65 @@ class Diagnostics;
class MacroExpander : public Lexer
{
public:
MacroExpander(Lexer* lexer, MacroSet* macroSet, Diagnostics* diagnostics);
MacroExpander(Lexer *lexer, MacroSet *macroSet, Diagnostics *diagnostics);
virtual ~MacroExpander();
virtual void lex(Token* token);
virtual void lex(Token *token);
private:
PP_DISALLOW_COPY_AND_ASSIGN(MacroExpander);
void getToken(Token* token);
void ungetToken(const Token& token);
void getToken(Token *token);
void ungetToken(const Token &token);
bool isNextTokenLeftParen();
bool pushMacro(const Macro& macro, const Token& identifier);
bool pushMacro(const Macro &macro, const Token &identifier);
void popMacro();
bool expandMacro(const Macro& macro,
const Token& identifier,
std::vector<Token>* replacements);
bool expandMacro(const Macro &macro,
const Token &identifier,
std::vector<Token> *replacements);
typedef std::vector<Token> MacroArg;
bool collectMacroArgs(const Macro& macro,
const Token& identifier,
std::vector<MacroArg>* args);
void replaceMacroParams(const Macro& macro,
const std::vector<MacroArg>& args,
std::vector<Token>* replacements);
bool collectMacroArgs(const Macro &macro,
const Token &identifier,
std::vector<MacroArg> *args);
void replaceMacroParams(const Macro &macro,
const std::vector<MacroArg> &args,
std::vector<Token> *replacements);
struct MacroContext
{
const Macro* macro;
const Macro *macro;
std::size_t index;
std::vector<Token> replacements;
MacroContext() : macro(0), index(0) { }
bool empty() const { return index == replacements.size(); }
const Token& get() { return replacements[index++]; }
void unget() { assert(index > 0); --index; }
MacroContext()
: macro(0),
index(0)
{
}
bool empty() const
{
return index == replacements.size();
}
const Token &get()
{
return replacements[index++];
}
void unget()
{
assert(index > 0);
--index;
}
};
Lexer* mLexer;
MacroSet* mMacroSet;
Diagnostics* mDiagnostics;
Lexer *mLexer;
MacroSet *mMacroSet;
Diagnostics *mDiagnostics;
std::auto_ptr<Token> mReserveToken;
std::vector<MacroContext*> mContextStack;
std::vector<MacroContext *> mContextStack;
};
} // namespace pp
......
......@@ -21,24 +21,24 @@ namespace pp
struct PreprocessorImpl
{
Diagnostics* diagnostics;
Diagnostics *diagnostics;
MacroSet macroSet;
Tokenizer tokenizer;
DirectiveParser directiveParser;
MacroExpander macroExpander;
PreprocessorImpl(Diagnostics* diag,
DirectiveHandler* directiveHandler) :
diagnostics(diag),
tokenizer(diag),
directiveParser(&tokenizer, &macroSet, diag, directiveHandler),
macroExpander(&directiveParser, &macroSet, diag)
PreprocessorImpl(Diagnostics *diag,
DirectiveHandler *directiveHandler)
: diagnostics(diag),
tokenizer(diag),
directiveParser(&tokenizer, &macroSet, diag, directiveHandler),
macroExpander(&directiveParser, &macroSet, diag)
{
}
};
Preprocessor::Preprocessor(Diagnostics* diagnostics,
DirectiveHandler* directiveHandler)
Preprocessor::Preprocessor(Diagnostics *diagnostics,
DirectiveHandler *directiveHandler)
{
mImpl = new PreprocessorImpl(diagnostics, directiveHandler);
}
......@@ -49,7 +49,7 @@ Preprocessor::~Preprocessor()
}
bool Preprocessor::init(size_t count,
const char* const string[],
const char * const string[],
const int length[])
{
static const int kGLSLVersion = 100;
......@@ -63,7 +63,7 @@ bool Preprocessor::init(size_t count,
return mImpl->tokenizer.init(count, string, length);
}
void Preprocessor::predefineMacro(const char* name, int value)
void Preprocessor::predefineMacro(const char *name, int value)
{
std::ostringstream stream;
stream << value;
......@@ -81,7 +81,7 @@ void Preprocessor::predefineMacro(const char* name, int value)
mImpl->macroSet[name] = macro;
}
void Preprocessor::lex(Token* token)
void Preprocessor::lex(Token *token)
{
bool validToken = false;
while (!validToken)
......
......@@ -22,7 +22,7 @@ struct Token;
class Preprocessor
{
public:
Preprocessor(Diagnostics* diagnostics, DirectiveHandler* directiveHandler);
Preprocessor(Diagnostics *diagnostics, DirectiveHandler *directiveHandler);
~Preprocessor();
// count: specifies the number of elements in the string and length arrays.
......@@ -34,11 +34,11 @@ class Preprocessor
// 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
// 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.
void predefineMacro(const char* name, int value);
void predefineMacro(const char *name, int value);
void lex(Token* token);
void lex(Token *token);
// Set maximum preprocessor token size
void setMaxTokenSize(size_t maxTokenSize);
......@@ -46,7 +46,7 @@ class Preprocessor
private:
PP_DISALLOW_COPY_AND_ASSIGN(Preprocessor);
PreprocessorImpl* mImpl;
PreprocessorImpl *mImpl;
};
} // namespace pp
......
......@@ -12,10 +12,18 @@ namespace pp
struct SourceLocation
{
SourceLocation() : file(0), line(0) { }
SourceLocation(int f, int l) : file(f), line(l) { }
SourceLocation()
: file(0),
line(0)
{
}
SourceLocation(int f, int l)
: file(f),
line(l)
{
}
bool equals(const SourceLocation& other) const
bool equals(const SourceLocation &other) const
{
return (file == other.file) && (line == other.line);
}
......@@ -24,12 +32,12 @@ struct SourceLocation
int line;
};
inline bool operator==(const SourceLocation& lhs, const SourceLocation& rhs)
inline bool operator==(const SourceLocation &lhs, const SourceLocation &rhs)
{
return lhs.equals(rhs);
}
inline bool operator!=(const SourceLocation& lhs, const SourceLocation& rhs)
inline bool operator!=(const SourceLocation &lhs, const SourceLocation &rhs)
{
return !lhs.equals(rhs);
}
......
......@@ -21,7 +21,7 @@ void Token::reset()
text.clear();
}
bool Token::equals(const Token& other) const
bool Token::equals(const Token &other) const
{
return (type == other.type) &&
(flags == other.flags) &&
......@@ -53,25 +53,25 @@ void Token::setExpansionDisabled(bool disable)
flags &= ~EXPANSION_DISABLED;
}
bool Token::iValue(int* value) const
bool Token::iValue(int *value) const
{
assert(type == CONST_INT);
return numeric_lex_int(text, value);
}
bool Token::uValue(unsigned int* value) const
bool Token::uValue(unsigned int *value) const
{
assert(type == CONST_INT);
return numeric_lex_int(text, value);
}
bool Token::fValue(float* value) const
bool Token::fValue(float *value) const
{
assert(type == CONST_FLOAT);
return numeric_lex_float(text, value);
}
std::ostream& operator<<(std::ostream& out, const Token& token)
std::ostream &operator<<(std::ostream &out, const Token &token)
{
if (token.hasLeadingSpace())
out << " ";
......
......@@ -62,27 +62,40 @@ struct Token
EXPANSION_DISABLED = 1 << 2
};
Token() : type(0), flags(0) { }
Token()
: type(0),
flags(0)
{
}
void reset();
bool equals(const Token& other) const;
bool equals(const Token &other) const;
// Returns true if this is the first token on line.
// It disregards any leading whitespace.
bool atStartOfLine() const { return (flags & AT_START_OF_LINE) != 0; }
bool atStartOfLine() const
{
return (flags & AT_START_OF_LINE) != 0;
}
void setAtStartOfLine(bool start);
bool hasLeadingSpace() const { return (flags & HAS_LEADING_SPACE) != 0; }
bool hasLeadingSpace() const
{
return (flags & HAS_LEADING_SPACE) != 0;
}
void setHasLeadingSpace(bool space);
bool expansionDisabled() const { return (flags & EXPANSION_DISABLED) != 0; }
bool expansionDisabled() const
{
return (flags & EXPANSION_DISABLED) != 0;
}
void setExpansionDisabled(bool disable);
// Converts text into numeric value for CONST_INT and CONST_FLOAT token.
// Returns false if the parsed value cannot fit into an int or float.
bool iValue(int* value) const;
bool uValue(unsigned int* value) const;
bool fValue(float* value) const;
bool iValue(int *value) const;
bool uValue(unsigned int *value) const;
bool fValue(float *value) const;
int type;
unsigned int flags;
......@@ -90,17 +103,17 @@ struct Token
std::string text;
};
inline bool operator==(const Token& lhs, const Token& rhs)
inline bool operator==(const Token &lhs, const Token &rhs)
{
return lhs.equals(rhs);
}
inline bool operator!=(const Token& lhs, const Token& rhs)
inline bool operator!=(const Token &lhs, const Token &rhs)
{
return !lhs.equals(rhs);
}
extern std::ostream& operator<<(std::ostream& out, const Token& token);
extern std::ostream &operator<<(std::ostream &out, const Token &token);
} // namepsace pp
#endif // COMPILER_PREPROCESSOR_TOKEN_H_
......@@ -2316,7 +2316,7 @@ void ppfree (void * ptr , yyscan_t yyscanner)
namespace pp {
Tokenizer::Tokenizer(Diagnostics* diagnostics)
Tokenizer::Tokenizer(Diagnostics *diagnostics)
: mHandle(0),
mMaxTokenSize(256)
{
......@@ -2328,9 +2328,10 @@ Tokenizer::~Tokenizer()
destroyScanner();
}
bool Tokenizer::init(size_t count, const char* const string[], const int length[])
bool Tokenizer::init(size_t count, const char * const string[], const int length[])
{
if ((count > 0) && (string == 0)) return false;
if ((count > 0) && (string == 0))
return false;
mContext.input = Input(count, string, length);
return initScanner();
......@@ -2353,7 +2354,7 @@ void Tokenizer::setMaxTokenSize(size_t maxTokenSize)
mMaxTokenSize = maxTokenSize;
}
void Tokenizer::lex(Token* token)
void Tokenizer::lex(Token *token)
{
token->type = pplex(&token->text,&token->location,mHandle);
if (token->text.size() > mMaxTokenSize)
......
......@@ -21,7 +21,7 @@ class Tokenizer : public Lexer
public:
struct Context
{
Diagnostics* diagnostics;
Diagnostics *diagnostics;
Input input;
// The location where yytext points to. Token location should track
......@@ -33,23 +33,23 @@ class Tokenizer : public Lexer
bool lineStart;
};
Tokenizer(Diagnostics* diagnostics);
Tokenizer(Diagnostics *diagnostics);
~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 setLineNumber(int line);
void setMaxTokenSize(size_t maxTokenSize);
virtual void lex(Token* token);
virtual void lex(Token *token);
private:
PP_DISALLOW_COPY_AND_ASSIGN(Tokenizer);
bool initScanner();
void destroyScanner();
void* mHandle; // Scanner handle.
void *mHandle; // Scanner handle.
Context mContext; // Scanner extra.
size_t mMaxTokenSize; // Maximum token size
};
......
......@@ -267,7 +267,7 @@ FRACTIONAL_CONSTANT ({DIGIT}*"."{DIGIT}+)|({DIGIT}+".")
namespace pp {
Tokenizer::Tokenizer(Diagnostics* diagnostics) : mHandle(0)
Tokenizer::Tokenizer(Diagnostics *diagnostics) : mHandle(0)
{
mContext.diagnostics = diagnostics;
}
......@@ -277,9 +277,10 @@ Tokenizer::~Tokenizer()
destroyScanner();
}
bool Tokenizer::init(size_t count, const char* const string[], const int length[])
bool Tokenizer::init(size_t count, const char * const string[], const int length[])
{
if ((count > 0) && (string == 0)) return false;
if ((count > 0) && (string == 0))
return false;
mContext.input = Input(count, string, length);
return initScanner();
......@@ -302,7 +303,7 @@ void Tokenizer::setMaxTokenSize(size_t maxTokenSize)
mMaxTokenSize = maxTokenSize;
}
void Tokenizer::lex(Token* token)
void Tokenizer::lex(Token *token)
{
token->type = yylex(&token->text, &token->location, mHandle);
if (token->text.size() > mMaxTokenSize)
......
......@@ -13,7 +13,7 @@
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) &&
(str[0] == '0') &&
......@@ -21,7 +21,7 @@ inline std::ios::fmtflags numeric_base_int(const std::string& str)
{
return std::ios::hex;
}
else if ((str.size() >= 1) && (str[0] == '0'))
if ((str.size() >= 1) && (str[0] == '0'))
{
return std::ios::oct;
}
......@@ -34,7 +34,7 @@ inline std::ios::fmtflags numeric_base_int(const std::string& str)
// in which case false is returned.
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);
// This should not be necessary, but MSVS has a buggy implementation.
......@@ -46,7 +46,7 @@ bool numeric_lex_int(const std::string& str, IntType* value)
}
template<typename FloatType>
bool numeric_lex_float(const std::string& str, FloatType* value)
bool numeric_lex_float(const std::string &str, FloatType *value)
{
std::istringstream stream(str);
// Force "C" locale so that decimal character is always '.', and
......
......@@ -12,7 +12,7 @@
// A macro to disallow the copy constructor and operator= functions
// This must be used in the private: declarations for a class.
#define PP_DISALLOW_COPY_AND_ASSIGN(TypeName) \
TypeName(const TypeName&); \
void operator=(const TypeName&)
TypeName(const TypeName &); \
void operator=(const TypeName &)
#endif // COMPILER_PREPROCESSOR_PPUTILS_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