Fix diagnostic message for layout errors
authorUmar Arshad <umar@arrayfire.com>
Thu, 9 Jun 2016 19:51:39 +0000 (15:51 -0400)
committerUmar Arshad <umar@arrayfire.com>
Thu, 9 Jun 2016 19:51:39 +0000 (15:51 -0400)
source/val/ValidationState.cpp
source/validate_layout.cpp
test/Validate.Layout.cpp

index f937e2f..cf7193c 100644 (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) {
index 943fb59..3c3cb66 100644 (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 {
index c680be1..f647490 100644 (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) {