Commit 461e3af8 by Jamie Madill Committed by Commit Bot

preprocessor: Fix negative shift with bad ids.

Fix this by producing an error on undefined or negative shifts. BUG=629518 Change-Id: Idfca5ed3fc8e557f6178408f3426a5ef2ce7cf14 Reviewed-on: https://chromium-review.googlesource.com/362020Reviewed-by: 's avatarAntoine Labour <piman@chromium.org> Reviewed-by: 's avatarCorentin Wallez <cwallez@chromium.org> Commit-Queue: Jamie Madill <jmadill@chromium.org>
parent 27776e33
......@@ -115,6 +115,8 @@ std::string Diagnostics::message(ID id)
return "invalid line directive";
case PP_NON_PP_TOKEN_BEFORE_EXTENSION_ESSL3:
return "extension directive must occur before any non-preprocessor tokens in ESSL3";
case PP_UNDEFINED_SHIFT:
return "shift exponent is negative or undefined";
// Errors end.
// Warnings begin.
case PP_EOF_IN_DIRECTIVE:
......
......@@ -65,6 +65,7 @@ class Diagnostics
PP_INVALID_FILE_NUMBER,
PP_INVALID_LINE_DIRECTIVE,
PP_NON_PP_TOKEN_BEFORE_EXTENSION_ESSL3,
PP_UNDEFINED_SHIFT,
PP_ERROR_END,
PP_WARNING_BEGIN,
......
......@@ -498,12 +498,9 @@ static const yytype_uint8 yytranslate[] =
#if YYDEBUG
/* YYRLINE[YYN] -- Source line where rule number YYN was defined. */
static const yytype_uint16 yyrline[] =
{
0, 110, 110, 117, 118, 129, 129, 150, 150, 171,
174, 177, 180, 183, 186, 189, 192, 195, 198, 201,
204, 207, 210, 230, 250, 253, 256, 259, 262, 265
};
static const yytype_uint16 yyrline[] = {0, 110, 110, 117, 118, 129, 129, 150, 150, 171,
174, 177, 180, 183, 186, 189, 192, 195, 198, 218,
238, 241, 244, 264, 284, 287, 290, 293, 296, 299};
#endif
#if YYDEBUG || YYERROR_VERBOSE || 0
......@@ -1495,7 +1492,23 @@ yyreduce:
case 18:
{
(yyval) = (yyvsp[-2]) >> (yyvsp[0]);
if ((yyvsp[0]) < 0)
{
if (!context->isIgnoringErrors())
{
std::ostringstream stream;
stream << (yyvsp[-2]) << " >> " << (yyvsp[0]);
std::string text = stream.str();
context->diagnostics->report(pp::Diagnostics::PP_UNDEFINED_SHIFT,
context->token->location, text.c_str());
*(context->valid) = false;
}
(yyval) = static_cast<YYSTYPE>(0);
}
else
{
(yyval) = (yyvsp[-2]) >> (yyvsp[0]);
}
}
break;
......@@ -1503,7 +1516,23 @@ yyreduce:
case 19:
{
(yyval) = (yyvsp[-2]) << (yyvsp[0]);
if ((yyvsp[0]) < 0)
{
if (!context->isIgnoringErrors())
{
std::ostringstream stream;
stream << (yyvsp[-2]) << " << " << (yyvsp[0]);
std::string text = stream.str();
context->diagnostics->report(pp::Diagnostics::PP_UNDEFINED_SHIFT,
context->token->location, text.c_str());
*(context->valid) = false;
}
(yyval) = static_cast<YYSTYPE>(0);
}
else
{
(yyval) = (yyvsp[-2]) << (yyvsp[0]);
}
}
break;
......
......@@ -196,10 +196,44 @@ expression
$$ = $1 < $3;
}
| expression TOK_OP_RIGHT expression {
$$ = $1 >> $3;
if ($3 < 0)
{
if (!context->isIgnoringErrors())
{
std::ostringstream stream;
stream << $1 << " >> " << $3;
std::string text = stream.str();
context->diagnostics->report(pp::Diagnostics::PP_UNDEFINED_SHIFT,
context->token->location,
text.c_str());
*(context->valid) = false;
}
$$ = static_cast<YYSTYPE>(0);
}
else
{
$$ = $1 >> $3;
}
}
| expression TOK_OP_LEFT expression {
$$ = $1 << $3;
if ($3 < 0)
{
if (!context->isIgnoringErrors())
{
std::ostringstream stream;
stream << $1 << " << " << $3;
std::string text = stream.str();
context->diagnostics->report(pp::Diagnostics::PP_UNDEFINED_SHIFT,
context->token->location,
text.c_str());
*(context->valid) = false;
}
$$ = static_cast<YYSTYPE>(0);
}
else
{
$$ = $1 << $3;
}
}
| expression '-' expression {
$$ = $1 - $3;
......
......@@ -1635,3 +1635,23 @@ TEST_F(MalformedShaderTest, DefineWithSemicolon)
FAIL() << "Shader compilation succeeded, expecting failure " << mInfoLog;
}
}
// Covers a bug in our parsing of malformed shift preprocessor expressions.
TEST_F(MalformedShaderTest, LineDirectiveUndefinedShift)
{
const std::string &shaderString = "#line x << y";
if (compile(shaderString))
{
FAIL() << "Shader compilation succeeded, expecting failure " << mInfoLog;
}
}
// Covers a bug in our parsing of malformed shift preprocessor expressions.
TEST_F(MalformedShaderTest, LineDirectiveNegativeShift)
{
const std::string &shaderString = "#line x << -1";
if (compile(shaderString))
{
FAIL() << "Shader compilation succeeded, expecting failure " << mInfoLog;
}
}
......@@ -7,6 +7,8 @@
#include "PreprocessorTest.h"
#include "compiler/preprocessor/Token.h"
using testing::_;
class DefineTest : public PreprocessorTest
{
};
......@@ -918,3 +920,22 @@ TEST_F(DefineTest, ExpandedDefinedNotParsedOutsideIf)
"defined(bar)\n";
preprocess(input, expected);
}
// Test that line directive expressions give errors on negative or undefined shifts.
TEST_F(DefineTest, NegativeShiftInLineDirective)
{
const char *input =
"#line 1 << -1\n"
"#line 1 >> -1\n"
"#line 1 << x\n"
"#line 1 >> x\n";
const char *expected =
"\n"
"\n"
"\n"
"\n";
EXPECT_CALL(mDiagnostics, print(pp::Diagnostics::PP_UNDEFINED_SHIFT, _, _)).Times(4);
EXPECT_CALL(mDiagnostics, print(pp::Diagnostics::PP_INVALID_LINE_NUMBER, _, _)).Times(2);
preprocess(input, expected);
}
\ No newline at end of file
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