Commit ab4505a2 by Nicolas Capens

Validate that all function-like-macro arguments are unique.

Change-Id: Idc8c2a241af91916857ba015b061ce655b33e866 Reviewed-on: https://swiftshader-review.googlesource.com/5179Reviewed-by: 's avatarNicolas Capens <capn@google.com> Tested-by: 's avatarNicolas Capens <capn@google.com>
parent d39bf998
// //
// Copyright (c) 2012 The ANGLE Project Authors. All rights reserved. // Copyright (c) 2012 The ANGLE Project Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be // Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file. // found in the LICENSE file.
// //
#include "Diagnostics.h" #include "Diagnostics.h"
#include <cassert> #include <cassert>
namespace pp namespace pp
{ {
Diagnostics::~Diagnostics() Diagnostics::~Diagnostics()
{ {
} }
void Diagnostics::report(ID id, void Diagnostics::report(ID id,
const SourceLocation& loc, const SourceLocation& loc,
const std::string& text) 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);
} }
Diagnostics::Severity Diagnostics::severity(ID id) Diagnostics::Severity Diagnostics::severity(ID id)
{ {
if ((id > ERROR_BEGIN) && (id < ERROR_END)) if ((id > ERROR_BEGIN) && (id < ERROR_END))
return PP_ERROR; return PP_ERROR;
if ((id > WARNING_BEGIN) && (id < WARNING_END)) if ((id > WARNING_BEGIN) && (id < WARNING_END))
return PP_WARNING; return PP_WARNING;
assert(false); assert(false);
return PP_ERROR; return PP_ERROR;
} }
std::string Diagnostics::message(ID id) std::string Diagnostics::message(ID id)
{ {
switch (id) switch (id)
{ {
// Errors begin. // Errors begin.
case INTERNAL_ERROR: case INTERNAL_ERROR:
return "internal error"; return "internal error";
case OUT_OF_MEMORY: case OUT_OF_MEMORY:
return "out of memory"; return "out of memory";
case INVALID_CHARACTER: case INVALID_CHARACTER:
return "invalid character"; return "invalid character";
case INVALID_NUMBER: case INVALID_NUMBER:
return "invalid number"; return "invalid number";
case INTEGER_OVERFLOW: case INTEGER_OVERFLOW:
return "integer overflow"; return "integer overflow";
case FLOAT_OVERFLOW: case FLOAT_OVERFLOW:
return "float overflow"; return "float overflow";
case TOKEN_TOO_LONG: case TOKEN_TOO_LONG:
return "token too long"; return "token too long";
case INVALID_EXPRESSION: case INVALID_EXPRESSION:
return "invalid expression"; return "invalid expression";
case DIVISION_BY_ZERO: case DIVISION_BY_ZERO:
return "division by zero"; return "division by zero";
case EOF_IN_COMMENT: case EOF_IN_COMMENT:
return "unexpected end of file found in comment"; return "unexpected end of file found in comment";
case UNEXPECTED_TOKEN: case UNEXPECTED_TOKEN:
return "unexpected token"; return "unexpected token";
case DIRECTIVE_INVALID_NAME: case DIRECTIVE_INVALID_NAME:
return "invalid directive name"; return "invalid directive name";
case MACRO_NAME_RESERVED: case MACRO_NAME_RESERVED:
return "macro name is reserved"; return "macro name is reserved";
case MACRO_REDEFINED: case MACRO_REDEFINED:
return "macro redefined"; return "macro redefined";
case MACRO_PREDEFINED_REDEFINED: case MACRO_PREDEFINED_REDEFINED:
return "predefined macro redefined"; return "predefined macro redefined";
case MACRO_PREDEFINED_UNDEFINED: case MACRO_PREDEFINED_UNDEFINED:
return "predefined macro undefined"; return "predefined macro undefined";
case MACRO_UNTERMINATED_INVOCATION: case MACRO_UNTERMINATED_INVOCATION:
return "unterminated macro invocation"; return "unterminated macro invocation";
case MACRO_TOO_FEW_ARGS: case MACRO_TOO_FEW_ARGS:
return "Not enough arguments for macro"; return "Not enough arguments for macro";
case MACRO_TOO_MANY_ARGS: case MACRO_TOO_MANY_ARGS:
return "Too many arguments for macro"; return "Too many arguments for macro";
case CONDITIONAL_ENDIF_WITHOUT_IF: case MACRO_DUPLICATE_PARAMETER_NAMES:
return "unexpected #endif found without a matching #if"; return "duplicate macro parameter name";
case CONDITIONAL_ELSE_WITHOUT_IF: case CONDITIONAL_ENDIF_WITHOUT_IF:
return "unexpected #else found without a matching #if"; return "unexpected #endif found without a matching #if";
case CONDITIONAL_ELSE_AFTER_ELSE: case CONDITIONAL_ELSE_WITHOUT_IF:
return "unexpected #else found after another #else"; return "unexpected #else found without a matching #if";
case CONDITIONAL_ELIF_WITHOUT_IF: case CONDITIONAL_ELSE_AFTER_ELSE:
return "unexpected #elif found without a matching #if"; return "unexpected #else found after another #else";
case CONDITIONAL_ELIF_AFTER_ELSE: case CONDITIONAL_ELIF_WITHOUT_IF:
return "unexpected #elif found after #else"; return "unexpected #elif found without a matching #if";
case CONDITIONAL_UNTERMINATED: case CONDITIONAL_ELIF_AFTER_ELSE:
return "unexpected end of file found in conditional block"; return "unexpected #elif found after #else";
case INVALID_EXTENSION_NAME: case CONDITIONAL_UNTERMINATED:
return "invalid extension name"; return "unexpected end of file found in conditional block";
case INVALID_EXTENSION_BEHAVIOR: case INVALID_EXTENSION_NAME:
return "invalid extension behavior"; return "invalid extension name";
case INVALID_EXTENSION_DIRECTIVE: case INVALID_EXTENSION_BEHAVIOR:
return "invalid extension directive"; return "invalid extension behavior";
case INVALID_VERSION_NUMBER: case INVALID_EXTENSION_DIRECTIVE:
return "invalid version number"; return "invalid extension directive";
case INVALID_VERSION_DIRECTIVE: case INVALID_VERSION_NUMBER:
return "invalid version directive"; return "invalid version number";
case VERSION_NOT_FIRST_STATEMENT: case INVALID_VERSION_DIRECTIVE:
return "#version directive must occur before anything else, " return "invalid version directive";
"except for comments and white space"; case VERSION_NOT_FIRST_STATEMENT:
case INVALID_LINE_NUMBER: return "#version directive must occur before anything else, "
return "invalid line number"; "except for comments and white space";
case INVALID_FILE_NUMBER: case INVALID_LINE_NUMBER:
return "invalid file number"; return "invalid line number";
case INVALID_LINE_DIRECTIVE: case INVALID_FILE_NUMBER:
return "invalid line directive"; return "invalid file number";
case UNDEFINED_IDENTIFIER: case INVALID_LINE_DIRECTIVE:
return "undefined identifier"; return "invalid line directive";
// Errors end. case UNDEFINED_IDENTIFIER:
// Warnings begin. return "undefined identifier";
case EOF_IN_DIRECTIVE: // Errors end.
return "unexpected end of file found in directive"; // Warnings begin.
case CONDITIONAL_UNEXPECTED_TOKEN: case EOF_IN_DIRECTIVE:
return "unexpected token after conditional expression"; return "unexpected end of file found in directive";
case UNRECOGNIZED_PRAGMA: case CONDITIONAL_UNEXPECTED_TOKEN:
return "unrecognized pragma"; return "unexpected token after conditional expression";
// Warnings end. case UNRECOGNIZED_PRAGMA:
default: return "unrecognized pragma";
assert(false); // Warnings end.
return ""; default:
} assert(false);
} return "";
}
} // namespace pp }
} // namespace pp
...@@ -46,6 +46,7 @@ class Diagnostics ...@@ -46,6 +46,7 @@ class Diagnostics
MACRO_UNTERMINATED_INVOCATION, MACRO_UNTERMINATED_INVOCATION,
MACRO_TOO_FEW_ARGS, MACRO_TOO_FEW_ARGS,
MACRO_TOO_MANY_ARGS, MACRO_TOO_MANY_ARGS,
MACRO_DUPLICATE_PARAMETER_NAMES,
CONDITIONAL_ENDIF_WITHOUT_IF, CONDITIONAL_ENDIF_WITHOUT_IF,
CONDITIONAL_ELSE_WITHOUT_IF, CONDITIONAL_ELSE_WITHOUT_IF,
CONDITIONAL_ELSE_AFTER_ELSE, CONDITIONAL_ELSE_AFTER_ELSE,
......
...@@ -361,6 +361,14 @@ void DirectiveParser::parseDefine(Token* token) ...@@ -361,6 +361,14 @@ void DirectiveParser::parseDefine(Token* token)
mTokenizer->lex(token); mTokenizer->lex(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())
{
mDiagnostics->report(Diagnostics::MACRO_DUPLICATE_PARAMETER_NAMES,
token->location, token->text);
return;
}
macro.parameters.push_back(token->text); macro.parameters.push_back(token->text);
mTokenizer->lex(token); // Get ','. mTokenizer->lex(token); // Get ','.
......
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