From 11437165d603ae6ab28b4a02e349748632785eea Mon Sep 17 00:00:00 2001 From: Umar Arshad Date: Thu, 9 Jun 2016 15:51:39 -0400 Subject: [PATCH] Fix diagnostic message for layout errors --- source/val/ValidationState.cpp | 3 ++- source/validate_layout.cpp | 3 ++- test/Validate.Layout.cpp | 21 ++++++++++++++++++++- 3 files changed, 24 insertions(+), 3 deletions(-) diff --git a/source/val/ValidationState.cpp b/source/val/ValidationState.cpp index f937e2f..cf7193c 100644 --- a/source/val/ValidationState.cpp +++ b/source/val/ValidationState.cpp @@ -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) { diff --git a/source/validate_layout.cpp b/source/validate_layout.cpp index 943fb59..3c3cb66 100644 --- a/source/validate_layout.cpp +++ b/source/validate_layout.cpp @@ -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 { diff --git a/test/Validate.Layout.cpp b/test/Validate.Layout.cpp index c680be1..f647490 100644 --- a/test/Validate.Layout.cpp +++ b/test/Validate.Layout.cpp @@ -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; TEST_F(ValidateOpFunctionParameter, OpLineBetweenParameters) { -- 2.7.4