Fix diagnostic message for layout errors

This commit is contained in:
Umar Arshad 2016-06-09 15:51:39 -04:00
parent 90a4252aae
commit 11437165d6
3 changed files with 24 additions and 3 deletions

View File

@ -288,7 +288,8 @@ Function& ValidationState_t::get_current_function() {
bool ValidationState_t::in_function_body() const { return in_function_; }
bool ValidationState_t::in_block() const {
return module_functions_.back().get_current_block() != nullptr;
return module_functions_.empty() == false &&
module_functions_.back().get_current_block() != nullptr;
}
void ValidationState_t::RegisterCapability(SpvCapability cap) {

View File

@ -158,7 +158,8 @@ spv_result_t FunctionScopedInstructions(ValidationState_t& _,
break;
default:
if (_.getLayoutSection() == kLayoutFunctionDeclarations) {
if (_.getLayoutSection() == kLayoutFunctionDeclarations &&
_.in_function_body()) {
return _.diag(SPV_ERROR_INVALID_LAYOUT)
<< "A function must begin with a label";
} else {

View File

@ -47,7 +47,7 @@ using std::tie;
using std::tuple;
using std::vector;
using ::testing::HasSubstr;
using ::testing::StrEq;
using libspirv::spvResultToString;
@ -313,6 +313,25 @@ TEST_F(ValidateLayout, FuncParameterNotImmediatlyAfterFuncBad) {
ASSERT_EQ(SPV_ERROR_INVALID_LAYOUT, ValidateInstructions());
}
TEST_F(ValidateLayout, InstructionAppearBeforeFunctionDefinition) {
string str = R"(
OpCapability Kernel
OpMemoryModel Logical OpenCL
%voidt = OpTypeVoid
%uintt = OpTypeInt 32 0
%funct = OpTypeFunction %voidt
%udef = OpUndef %uintt
%func = OpFunction %voidt None %funct
%entry = OpLabel
OpReturn
OpFunctionEnd
)";
CompileSuccessfully(str);
ASSERT_EQ(SPV_ERROR_INVALID_LAYOUT, ValidateInstructions());
EXPECT_THAT(getDiagnosticString(), StrEq("Undef must appear in a block"));
}
using ValidateOpFunctionParameter = spvtest::ValidateBase<int>;
TEST_F(ValidateOpFunctionParameter, OpLineBetweenParameters) {