Cleanup some error messages.

This CL makes it clearer where errors are coming from and fixes the
source information on a few type determiner error messages.

Change-Id: I356518ac3004effe005bb7dea147c7fe442ab1a8
Reviewed-on: https://dawn-review.googlesource.com/c/tint/+/20063
Reviewed-by: David Neto <dneto@google.com>
This commit is contained in:
dan sinclair 2020-04-21 13:05:42 +00:00 committed by dan sinclair
parent aac5865121
commit 7025918287
4 changed files with 74 additions and 39 deletions

View File

@ -288,7 +288,7 @@ int main(int argc, const char** argv) {
return 1;
}
if (!reader->Parse()) {
std::cerr << reader->error() << std::endl;
std::cerr << "Parse: " << reader->error() << std::endl;
return 1;
}
@ -307,13 +307,13 @@ int main(int argc, const char** argv) {
tint::TypeDeterminer td(&ctx, &mod);
if (!td.Determine()) {
std::cerr << td.error() << std::endl;
std::cerr << "Type Determination: " << td.error() << std::endl;
return 1;
}
tint::Validator v;
if (!v.Validate(mod)) {
std::cerr << v.error() << std::endl;
std::cerr << "Validation: " << v.error() << std::endl;
return 1;
}

View File

@ -126,7 +126,8 @@ bool TypeDeterminer::DetermineVariableStorageClass(ast::Statement* stmt) {
}
if (var->storage_class() != ast::StorageClass::kNone) {
error_ = "function variable has a non-function storage class";
set_error(stmt->source(),
"function variable has a non-function storage class");
return false;
}
@ -308,14 +309,16 @@ bool TypeDeterminer::DetermineCall(ast::CallExpression* expr) {
if (ident->has_path()) {
auto* imp = mod_->FindImportByName(ident->path());
if (imp == nullptr) {
error_ = "Unable to find import for " + ident->name();
set_error(expr->source(), "Unable to find import for " + ident->name());
return false;
}
uint32_t ext_id = 0;
auto* result_type =
GetImportData(imp->path(), ident->name(), expr->params(), &ext_id);
auto* result_type = GetImportData(expr->source(), imp->path(),
ident->name(), expr->params(), &ext_id);
if (result_type == nullptr) {
set_error(expr->source(),
"Unable to determine result type for GLSL expression");
return false;
}
@ -414,7 +417,8 @@ bool TypeDeterminer::DetermineMemberAccessor(
return true;
}
set_error(expr->source(), "invalid type in member accessor");
set_error(expr->source(),
"invalid type " + data_type->type_name() + " in member accessor");
return false;
}
@ -489,6 +493,7 @@ bool TypeDeterminer::DetermineBinary(ast::BinaryExpression* expr) {
return true;
}
set_error(expr->source(), "Unknown binary expression");
return false;
}
@ -573,6 +578,7 @@ bool TypeDeterminer::DetermineUnaryOp(ast::UnaryOpExpression* expr) {
}
ast::type::Type* TypeDeterminer::GetImportData(
const Source& source,
const std::string& path,
const std::string& name,
const ast::ExpressionList& params,
@ -603,13 +609,14 @@ ast::type::Type* TypeDeterminer::GetImportData(
name == "sqrt" || name == "inversesqrt" || name == "normalize" ||
name == "length") {
if (params.size() != 1) {
error_ = "incorrect number of parameters for " + name +
". Expected 1 got " + std::to_string(params.size());
set_error(source, "incorrect number of parameters for " + name +
". Expected 1 got " +
std::to_string(params.size()));
return nullptr;
}
if (!params[0]->result_type()->is_float_scalar_or_vector()) {
error_ = "incorrect type for " + name +
". Requires a float scalar or a float vector";
set_error(source, "incorrect type for " + name +
". Requires a float scalar or a float vector");
return nullptr;
}

View File

@ -92,12 +92,14 @@ class TypeDeterminer {
}
/// Retrieves information for the requested import.
/// @param src the source of the import
/// @param path the import path
/// @param name the method name to get information on
/// @param params the parameters to the method call
/// @param id out parameter for the external call ID. Must not be a nullptr.
/// @returns the return type of |name| in |path| or nullptr on error.
ast::type::Type* GetImportData(const std::string& path,
ast::type::Type* GetImportData(const Source& src,
const std::string& path,
const std::string& name,
const ast::ExpressionList& params,
uint32_t* id);

View File

@ -1521,7 +1521,8 @@ TEST_P(ImportData_SingleParamTest, Scalar) {
ASSERT_TRUE(td()->DetermineResultType(params)) << td()->error();
uint32_t id = 0;
auto* type = td()->GetImportData("GLSL.std.450", param.name, params, &id);
auto* type =
td()->GetImportData({0, 0}, "GLSL.std.450", param.name, params, &id);
ASSERT_NE(type, nullptr);
EXPECT_TRUE(type->is_float_scalar());
EXPECT_EQ(id, param.value);
@ -1548,7 +1549,8 @@ TEST_P(ImportData_SingleParamTest, Vector) {
ASSERT_TRUE(td()->DetermineResultType(params)) << td()->error();
uint32_t id = 0;
auto* type = td()->GetImportData("GLSL.std.450", param.name, params, &id);
auto* type =
td()->GetImportData({0, 0}, "GLSL.std.450", param.name, params, &id);
ASSERT_NE(type, nullptr);
EXPECT_TRUE(type->is_float_vector());
EXPECT_EQ(type->AsVector()->size(), 3);
@ -1567,7 +1569,8 @@ TEST_P(ImportData_SingleParamTest, Error_Integer) {
ASSERT_TRUE(td()->DetermineResultType(params)) << td()->error();
uint32_t id = 0;
auto* type = td()->GetImportData("GLSL.std.450", param.name, params, &id);
auto* type =
td()->GetImportData({0, 0}, "GLSL.std.450", param.name, params, &id);
ASSERT_EQ(type, nullptr);
EXPECT_EQ(td()->error(), std::string("incorrect type for ") + param.name +
". Requires a float scalar or a float vector");
@ -1578,7 +1581,8 @@ TEST_P(ImportData_SingleParamTest, Error_NoParams) {
ast::ExpressionList params;
uint32_t id = 0;
auto* type = td()->GetImportData("GLSL.std.450", param.name, params, &id);
auto* type =
td()->GetImportData({0, 0}, "GLSL.std.450", param.name, params, &id);
ASSERT_EQ(type, nullptr);
EXPECT_EQ(td()->error(), std::string("incorrect number of parameters for ") +
param.name + ". Expected 1 got 0");
@ -1599,7 +1603,8 @@ TEST_P(ImportData_SingleParamTest, Error_MultipleParams) {
ASSERT_TRUE(td()->DetermineResultType(params)) << td()->error();
uint32_t id = 0;
auto* type = td()->GetImportData("GLSL.std.450", param.name, params, &id);
auto* type =
td()->GetImportData({0, 0}, "GLSL.std.450", param.name, params, &id);
ASSERT_EQ(type, nullptr);
EXPECT_EQ(td()->error(), std::string("incorrect number of parameters for ") +
param.name + ". Expected 1 got 3");
@ -1648,7 +1653,8 @@ TEST_F(TypeDeterminerTest, ImportData_Length_Scalar) {
ASSERT_TRUE(td()->DetermineResultType(params)) << td()->error();
uint32_t id = 0;
auto* type = td()->GetImportData("GLSL.std.450", "length", params, &id);
auto* type =
td()->GetImportData({0, 0}, "GLSL.std.450", "length", params, &id);
ASSERT_NE(type, nullptr);
EXPECT_TRUE(type->is_float_scalar());
EXPECT_EQ(id, GLSLstd450Length);
@ -1673,7 +1679,8 @@ TEST_F(TypeDeterminerTest, ImportData_Length_FloatVector) {
ASSERT_TRUE(td()->DetermineResultType(params)) << td()->error();
uint32_t id = 0;
auto* type = td()->GetImportData("GLSL.std.450", "length", params, &id);
auto* type =
td()->GetImportData({0, 0}, "GLSL.std.450", "length", params, &id);
ASSERT_NE(type, nullptr);
EXPECT_TRUE(type->is_float_scalar());
EXPECT_EQ(id, GLSLstd450Length);
@ -1689,7 +1696,8 @@ TEST_F(TypeDeterminerTest, ImportData_Length_Error_Integer) {
ASSERT_TRUE(td()->DetermineResultType(params)) << td()->error();
uint32_t id = 0;
auto* type = td()->GetImportData("GLSL.std.450", "length", params, &id);
auto* type =
td()->GetImportData({0, 0}, "GLSL.std.450", "length", params, &id);
ASSERT_EQ(type, nullptr);
EXPECT_EQ(
td()->error(),
@ -1699,7 +1707,8 @@ TEST_F(TypeDeterminerTest, ImportData_Length_Error_Integer) {
TEST_F(TypeDeterminerTest, ImportData_Length_Error_NoParams) {
ast::ExpressionList params;
uint32_t id = 0;
auto* type = td()->GetImportData("GLSL.std.450", "length", params, &id);
auto* type =
td()->GetImportData({0, 0}, "GLSL.std.450", "length", params, &id);
ASSERT_EQ(type, nullptr);
EXPECT_EQ(td()->error(),
"incorrect number of parameters for length. Expected 1 got 0");
@ -1718,7 +1727,8 @@ TEST_F(TypeDeterminerTest, ImportData_Length_Error_MultipleParams) {
ASSERT_TRUE(td()->DetermineResultType(params)) << td()->error();
uint32_t id = 0;
auto* type = td()->GetImportData("GLSL.std.450", "length", params, &id);
auto* type =
td()->GetImportData({0, 0}, "GLSL.std.450", "length", params, &id);
ASSERT_EQ(type, nullptr);
EXPECT_EQ(td()->error(),
"incorrect number of parameters for length. Expected 1 got 3");
@ -1740,7 +1750,8 @@ TEST_P(ImportData_TwoParamTest, Scalar) {
ASSERT_TRUE(td()->DetermineResultType(params)) << td()->error();
uint32_t id = 0;
auto* type = td()->GetImportData("GLSL.std.450", param.name, params, &id);
auto* type =
td()->GetImportData({0, 0}, "GLSL.std.450", param.name, params, &id);
ASSERT_NE(type, nullptr);
EXPECT_TRUE(type->is_float_scalar());
EXPECT_EQ(id, param.value);
@ -1777,7 +1788,8 @@ TEST_P(ImportData_TwoParamTest, Vector) {
ASSERT_TRUE(td()->DetermineResultType(params)) << td()->error();
uint32_t id = 0;
auto* type = td()->GetImportData("GLSL.std.450", param.name, params, &id);
auto* type =
td()->GetImportData({0, 0}, "GLSL.std.450", param.name, params, &id);
ASSERT_NE(type, nullptr);
EXPECT_TRUE(type->is_float_vector());
EXPECT_EQ(type->AsVector()->size(), 3);
@ -1798,7 +1810,8 @@ TEST_P(ImportData_TwoParamTest, Error_Integer) {
ASSERT_TRUE(td()->DetermineResultType(params)) << td()->error();
uint32_t id = 0;
auto* type = td()->GetImportData("GLSL.std.450", param.name, params, &id);
auto* type =
td()->GetImportData({0, 0}, "GLSL.std.450", param.name, params, &id);
ASSERT_EQ(type, nullptr);
EXPECT_EQ(td()->error(),
std::string("incorrect type for ") + param.name +
@ -1810,7 +1823,8 @@ TEST_P(ImportData_TwoParamTest, Error_NoParams) {
ast::ExpressionList params;
uint32_t id = 0;
auto* type = td()->GetImportData("GLSL.std.450", param.name, params, &id);
auto* type =
td()->GetImportData({0, 0}, "GLSL.std.450", param.name, params, &id);
ASSERT_EQ(type, nullptr);
EXPECT_EQ(td()->error(), std::string("incorrect number of parameters for ") +
param.name + ". Expected 2 got 0");
@ -1827,7 +1841,8 @@ TEST_P(ImportData_TwoParamTest, Error_OneParam) {
ASSERT_TRUE(td()->DetermineResultType(params)) << td()->error();
uint32_t id = 0;
auto* type = td()->GetImportData("GLSL.std.450", param.name, params, &id);
auto* type =
td()->GetImportData({0, 0}, "GLSL.std.450", param.name, params, &id);
ASSERT_EQ(type, nullptr);
EXPECT_EQ(td()->error(), std::string("incorrect number of parameters for ") +
param.name + ". Expected 2 got 1");
@ -1863,7 +1878,8 @@ TEST_P(ImportData_TwoParamTest, Error_MismatchedParamCount) {
ASSERT_TRUE(td()->DetermineResultType(params)) << td()->error();
uint32_t id = 0;
auto* type = td()->GetImportData("GLSL.std.450", param.name, params, &id);
auto* type =
td()->GetImportData({0, 0}, "GLSL.std.450", param.name, params, &id);
ASSERT_EQ(type, nullptr);
EXPECT_EQ(td()->error(),
std::string("mismatched parameter types for ") + param.name);
@ -1892,7 +1908,8 @@ TEST_P(ImportData_TwoParamTest, Error_MismatchedParamType) {
ASSERT_TRUE(td()->DetermineResultType(params)) << td()->error();
uint32_t id = 0;
auto* type = td()->GetImportData("GLSL.std.450", param.name, params, &id);
auto* type =
td()->GetImportData({0, 0}, "GLSL.std.450", param.name, params, &id);
ASSERT_EQ(type, nullptr);
EXPECT_EQ(td()->error(),
std::string("mismatched parameter types for ") + param.name);
@ -1913,7 +1930,8 @@ TEST_P(ImportData_TwoParamTest, Error_TooManyParams) {
ASSERT_TRUE(td()->DetermineResultType(params)) << td()->error();
uint32_t id = 0;
auto* type = td()->GetImportData("GLSL.std.450", param.name, params, &id);
auto* type =
td()->GetImportData({0, 0}, "GLSL.std.450", param.name, params, &id);
ASSERT_EQ(type, nullptr);
EXPECT_EQ(td()->error(), std::string("incorrect number of parameters for ") +
param.name + ". Expected 2 got 3");
@ -1942,7 +1960,8 @@ TEST_F(TypeDeterminerTest, ImportData_Distance_Scalar) {
ASSERT_TRUE(td()->DetermineResultType(params)) << td()->error();
uint32_t id = 0;
auto* type = td()->GetImportData("GLSL.std.450", "distance", params, &id);
auto* type =
td()->GetImportData({0, 0}, "GLSL.std.450", "distance", params, &id);
ASSERT_NE(type, nullptr);
EXPECT_TRUE(type->is_float_scalar());
EXPECT_EQ(id, GLSLstd450Distance);
@ -1977,7 +1996,8 @@ TEST_F(TypeDeterminerTest, ImportData_Distance_Vector) {
ASSERT_TRUE(td()->DetermineResultType(params)) << td()->error();
uint32_t id = 0;
auto* type = td()->GetImportData("GLSL.std.450", "distance", params, &id);
auto* type =
td()->GetImportData({0, 0}, "GLSL.std.450", "distance", params, &id);
ASSERT_NE(type, nullptr);
EXPECT_TRUE(type->IsF32());
EXPECT_EQ(id, GLSLstd450Distance);
@ -1995,7 +2015,8 @@ TEST_F(TypeDeterminerTest, ImportData_Distance_Error_Integer) {
ASSERT_TRUE(td()->DetermineResultType(params)) << td()->error();
uint32_t id = 0;
auto* type = td()->GetImportData("GLSL.std.450", "distance", params, &id);
auto* type =
td()->GetImportData({0, 0}, "GLSL.std.450", "distance", params, &id);
ASSERT_EQ(type, nullptr);
EXPECT_EQ(td()->error(),
"incorrect type for distance. Requires float scalar or a float "
@ -2005,7 +2026,8 @@ TEST_F(TypeDeterminerTest, ImportData_Distance_Error_Integer) {
TEST_F(TypeDeterminerTest, ImportData_Distance_Error_NoParams) {
ast::ExpressionList params;
uint32_t id = 0;
auto* type = td()->GetImportData("GLSL.std.450", "distance", params, &id);
auto* type =
td()->GetImportData({0, 0}, "GLSL.std.450", "distance", params, &id);
ASSERT_EQ(type, nullptr);
EXPECT_EQ(td()->error(),
"incorrect number of parameters for distance. Expected 2 got 0");
@ -2020,7 +2042,8 @@ TEST_F(TypeDeterminerTest, ImportData_Distance_Error_OneParam) {
ASSERT_TRUE(td()->DetermineResultType(params)) << td()->error();
uint32_t id = 0;
auto* type = td()->GetImportData("GLSL.std.450", "distance", params, &id);
auto* type =
td()->GetImportData({0, 0}, "GLSL.std.450", "distance", params, &id);
ASSERT_EQ(type, nullptr);
EXPECT_EQ(td()->error(),
"incorrect number of parameters for distance. Expected 2 got 1");
@ -2054,7 +2077,8 @@ TEST_F(TypeDeterminerTest, ImportData_Distance_Error_MismatchedParamCount) {
ASSERT_TRUE(td()->DetermineResultType(params)) << td()->error();
uint32_t id = 0;
auto* type = td()->GetImportData("GLSL.std.450", "distance", params, &id);
auto* type =
td()->GetImportData({0, 0}, "GLSL.std.450", "distance", params, &id);
ASSERT_EQ(type, nullptr);
EXPECT_EQ(td()->error(), "mismatched parameter types for distance");
}
@ -2080,7 +2104,8 @@ TEST_F(TypeDeterminerTest, ImportData_Distance_Error_MismatchedParamType) {
ASSERT_TRUE(td()->DetermineResultType(params)) << td()->error();
uint32_t id = 0;
auto* type = td()->GetImportData("GLSL.std.450", "distance", params, &id);
auto* type =
td()->GetImportData({0, 0}, "GLSL.std.450", "distance", params, &id);
ASSERT_EQ(type, nullptr);
EXPECT_EQ(td()->error(), "mismatched parameter types for distance");
}
@ -2098,7 +2123,8 @@ TEST_F(TypeDeterminerTest, ImportData_Distance_Error_TooManyParams) {
ASSERT_TRUE(td()->DetermineResultType(params)) << td()->error();
uint32_t id = 0;
auto* type = td()->GetImportData("GLSL.std.450", "distance", params, &id);
auto* type =
td()->GetImportData({0, 0}, "GLSL.std.450", "distance", params, &id);
ASSERT_EQ(type, nullptr);
EXPECT_EQ(td()->error(),
"incorrect number of parameters for distance. Expected 2 got 3");