From cfeac48a37744f161840833a5fcb221f589d2cd4 Mon Sep 17 00:00:00 2001 From: Andrew Woloszyn Date: Wed, 9 Sep 2015 13:04:32 -0400 Subject: [PATCH] Removed the ambiguity in the API for cleaning input vs output data. Previous the api used spv_text_t and spv_binary_t for both input and output, but depending on the usage, you either MUST call spvBinaryDestroy or you MUST NOT call spvBinaryDestroy on the pointer. --- include/libspirv/libspirv.h | 11 ++++-- readme.md | 4 +++ source/binary.cpp | 18 ++++++---- source/text.cpp | 7 ++-- test/BinaryDestroy.cpp | 4 +-- test/BinaryToText.cpp | 42 +++++++++++++---------- test/Comment.cpp | 5 +-- test/ExtInstGLSLstd450.cpp | 12 +++---- test/ImmediateInt.cpp | 10 +++--- test/NamedId.cpp | 8 +++-- test/TestFixture.h | 10 +++--- test/TextDestroy.cpp | 18 +++++----- test/TextToBinary.cpp | 67 +++++++++++++++++++------------------ test/Validate.cpp | 15 ++++----- test/ValidateID.cpp | 3 +- tools/as/as.cpp | 6 ++-- tools/dis/dis.cpp | 7 ++-- 17 files changed, 136 insertions(+), 111 deletions(-) diff --git a/include/libspirv/libspirv.h b/include/libspirv/libspirv.h index ceab8fea..025175c9 100644 --- a/include/libspirv/libspirv.h +++ b/include/libspirv/libspirv.h @@ -404,6 +404,7 @@ spv_result_t spvExtInstTableGet(spv_ext_inst_table *pTable); /// @brief Entry point to covert text form to binary form /// /// @param[in] text input text +/// @param[in] length of the input text /// @param[in] opcodeTable of specified Opcodes /// @param[in] operandTable of specified operands /// @param[in] extInstTable of specified extended instructions @@ -411,7 +412,8 @@ spv_result_t spvExtInstTableGet(spv_ext_inst_table *pTable); /// @param[out] pDiagnostic contains diagnostic on failure /// /// @return result code -spv_result_t spvTextToBinary(const spv_text text, +spv_result_t spvTextToBinary(const char* text, + const uint64_t length, const spv_opcode_table opcodeTable, const spv_operand_table operandTable, const spv_ext_inst_table extInstTable, @@ -428,7 +430,8 @@ void spvTextDestroy(spv_text text); /// @brief Entry point to convert binary to text form /// -/// @param[in] binary the input binary stream +/// @param[in] binary the input binary +/// @param[in] wordCount the number of input words /// @param[in] options bitfield of spv_binary_to_text_options_t values /// @param[in] opcodeTable table of specified Opcodes /// @param[in] operandTable table of specified operands @@ -437,7 +440,9 @@ void spvTextDestroy(spv_text text); /// @param[out] pDiagnostic contains diagnostic on failure /// /// @return result code -spv_result_t spvBinaryToText(const spv_binary binary, const uint32_t options, +spv_result_t spvBinaryToText(uint32_t* binary, + const uint64_t wordCount, + const uint32_t options, const spv_opcode_table opcodeTable, const spv_operand_table operandTable, const spv_ext_inst_table extInstTable, diff --git a/readme.md b/readme.md index 9e0682fb..cf65ffa0 100644 --- a/readme.md +++ b/readme.md @@ -42,6 +42,10 @@ The validator is incomplete. See the Future Work section for more information. conformance test suite. * New code tends to use Google C++ style, including formatting as generated by `clang-format --style=google`. +* The spvBinaryToText and spvTextToBinary interfaces have been updated to + remove a conceptual ambiguity that arises when cleaning up `spv_binary_t` + and `spv_text_t` objects. + ## Where is the code? diff --git a/source/binary.cpp b/source/binary.cpp index ce7db0b9..4ce371ca 100644 --- a/source/binary.cpp +++ b/source/binary.cpp @@ -427,12 +427,16 @@ spv_result_t spvBinaryDecodeOpcode( return SPV_SUCCESS; } -spv_result_t spvBinaryToText(const spv_binary binary, const uint32_t options, +spv_result_t spvBinaryToText(uint32_t* code, + const uint64_t wordCount, + const uint32_t options, const spv_opcode_table opcodeTable, const spv_operand_table operandTable, const spv_ext_inst_table extInstTable, spv_text *pText, spv_diagnostic *pDiagnostic) { - spvCheck(!binary->code || !binary->wordCount, + spv_binary_t binary = {code, wordCount}; + + spvCheck(!binary.code || !binary.wordCount, return SPV_ERROR_INVALID_BINARY); spvCheck(!opcodeTable || !operandTable || !extInstTable, return SPV_ERROR_INVALID_TABLE); @@ -444,13 +448,13 @@ spv_result_t spvBinaryToText(const spv_binary binary, const uint32_t options, spv_endianness_t endian; spv_position_t position = {}; - spvCheck(spvBinaryEndianness(binary, &endian), + spvCheck(spvBinaryEndianness(&binary, &endian), DIAGNOSTIC << "Invalid SPIR-V magic number '" << std::hex - << binary->code[0] << "'."; + << binary.code[0] << "'."; return SPV_ERROR_INVALID_BINARY); spv_header_t header; - spvCheck(spvBinaryHeaderGet(binary, endian, &header), + spvCheck(spvBinaryHeaderGet(&binary, endian, &header), DIAGNOSTIC << "Invalid SPIR-V header."; return SPV_ERROR_INVALID_BINARY); @@ -476,10 +480,10 @@ spv_result_t spvBinaryToText(const spv_binary binary, const uint32_t options, stream.get() << clr::reset(); } - const uint32_t *words = binary->code; + const uint32_t *words = binary.code; position.index = SPV_INDEX_INSTRUCTION; spv_ext_inst_type_t extInstType = SPV_EXT_INST_TYPE_NONE; - while (position.index < binary->wordCount) { + while (position.index < binary.wordCount) { uint64_t index = position.index; uint16_t wordCount; Op opcode; diff --git a/source/text.cpp b/source/text.cpp index d06c55c3..9e3a7aaa 100644 --- a/source/text.cpp +++ b/source/text.cpp @@ -754,13 +754,16 @@ spv_result_t spvTextToBinaryInternal(const spv_text text, } // anonymous namespace -spv_result_t spvTextToBinary(const spv_text text, +spv_result_t spvTextToBinary(const char* input_text, + const uint64_t input_text_size, const spv_opcode_table opcodeTable, const spv_operand_table operandTable, const spv_ext_inst_table extInstTable, spv_binary *pBinary, spv_diagnostic *pDiagnostic) { + spv_text_t text = {input_text, input_text_size}; + spv_result_t result = spvTextToBinaryInternal( - text, opcodeTable, operandTable, extInstTable, pBinary, pDiagnostic); + &text, opcodeTable, operandTable, extInstTable, pBinary, pDiagnostic); if (pDiagnostic && *pDiagnostic) (*pDiagnostic)->isTextSource = true; return result; diff --git a/test/BinaryDestroy.cpp b/test/BinaryDestroy.cpp index 3608a4a7..2dc7de51 100644 --- a/test/BinaryDestroy.cpp +++ b/test/BinaryDestroy.cpp @@ -44,8 +44,8 @@ TEST_F(BinaryDestroySomething, Default) { SetText("OpSource OpenCL 120"); spv_binary my_binary = nullptr; ASSERT_EQ(SPV_SUCCESS, - spvTextToBinary(&text, opcodeTable, operandTable, extInstTable, - &my_binary, &diagnostic)); + spvTextToBinary(text.str, text.length, opcodeTable, operandTable, + extInstTable, &my_binary, &diagnostic)); ASSERT_NE(nullptr, my_binary); spvBinaryDestroy(my_binary); } diff --git a/test/BinaryToText.cpp b/test/BinaryToText.cpp index 27384d5f..c9327adf 100644 --- a/test/BinaryToText.cpp +++ b/test/BinaryToText.cpp @@ -61,8 +61,9 @@ class BinaryToText : public ::testing::Test { )"; spv_text_t text = {textStr, strlen(textStr)}; spv_diagnostic diagnostic = nullptr; - spv_result_t error = spvTextToBinary(&text, opcodeTable, operandTable, - extInstTable, &binary, &diagnostic); + spv_result_t error = + spvTextToBinary(text.str, text.length, opcodeTable, operandTable, + extInstTable, &binary, &diagnostic); if (error) { spvDiagnosticPrint(diagnostic); spvDiagnosticDestroy(diagnostic); @@ -82,7 +83,8 @@ TEST_F(BinaryToText, Default) { spv_text text = nullptr; spv_diagnostic diagnostic = nullptr; ASSERT_EQ(SPV_SUCCESS, - spvBinaryToText(binary, SPV_BINARY_TO_TEXT_OPTION_NONE, opcodeTable, + spvBinaryToText(binary->code, binary->wordCount, + SPV_BINARY_TO_TEXT_OPTION_NONE, opcodeTable, operandTable, extInstTable, &text, &diagnostic)); printf("%s", text->str); spvTextDestroy(text); @@ -92,10 +94,10 @@ TEST_F(BinaryToText, InvalidCode) { spv_binary_t binary = {nullptr, 42}; spv_text text; spv_diagnostic diagnostic = nullptr; - ASSERT_EQ( - SPV_ERROR_INVALID_BINARY, - spvBinaryToText(&binary, SPV_BINARY_TO_TEXT_OPTION_NONE, opcodeTable, - operandTable, extInstTable, &text, &diagnostic)); + ASSERT_EQ(SPV_ERROR_INVALID_BINARY, + spvBinaryToText(nullptr, 42, + SPV_BINARY_TO_TEXT_OPTION_NONE, opcodeTable, + operandTable, extInstTable, &text, &diagnostic)); if (diagnostic) { spvDiagnosticPrint(diagnostic); spvDiagnosticDestroy(diagnostic); @@ -106,13 +108,15 @@ TEST_F(BinaryToText, InvalidTable) { spv_text text; spv_diagnostic diagnostic = nullptr; ASSERT_EQ(SPV_ERROR_INVALID_TABLE, - spvBinaryToText(binary, 0, nullptr, operandTable, extInstTable, - &text, &diagnostic)); + spvBinaryToText(binary->code, binary->wordCount, 0, nullptr, + operandTable, extInstTable, &text, &diagnostic)); ASSERT_EQ(SPV_ERROR_INVALID_TABLE, - spvBinaryToText(binary, SPV_BINARY_TO_TEXT_OPTION_NONE, opcodeTable, + spvBinaryToText(binary->code, binary->wordCount, + SPV_BINARY_TO_TEXT_OPTION_NONE, opcodeTable, nullptr, extInstTable, &text, &diagnostic)); ASSERT_EQ(SPV_ERROR_INVALID_TABLE, - spvBinaryToText(binary, SPV_BINARY_TO_TEXT_OPTION_NONE, opcodeTable, + spvBinaryToText(binary->code, binary->wordCount, + SPV_BINARY_TO_TEXT_OPTION_NONE, opcodeTable, operandTable, nullptr, &text, &diagnostic)); if (diagnostic) { spvDiagnosticPrint(diagnostic); @@ -123,7 +127,8 @@ TEST_F(BinaryToText, InvalidTable) { TEST_F(BinaryToText, InvalidDiagnostic) { spv_text text; ASSERT_EQ(SPV_ERROR_INVALID_DIAGNOSTIC, - spvBinaryToText(binary, SPV_BINARY_TO_TEXT_OPTION_NONE, opcodeTable, + spvBinaryToText(binary->code, binary->wordCount, + SPV_BINARY_TO_TEXT_OPTION_NONE, opcodeTable, operandTable, extInstTable, &text, nullptr)); } @@ -137,12 +142,14 @@ TEST(BinaryToTextSmall, OneInstruction) { ASSERT_EQ(SPV_SUCCESS, spvExtInstTableGet(&extInstTable)); spv_binary binary; spv_diagnostic diagnostic = nullptr; + const char* input = "OpSource OpenCL 12"; spv_result_t error = - spvTextToBinary(AutoText("OpSource OpenCL 12"), opcodeTable, operandTable, + spvTextToBinary(input, strlen(input), opcodeTable, operandTable, extInstTable, &binary, &diagnostic); ASSERT_EQ(SPV_SUCCESS, error); spv_text text; - error = spvBinaryToText(binary, SPV_BINARY_TO_TEXT_OPTION_NONE, opcodeTable, + error = spvBinaryToText(binary->code, binary->wordCount, + SPV_BINARY_TO_TEXT_OPTION_NONE, opcodeTable, operandTable, extInstTable, &text, &diagnostic); EXPECT_EQ(SPV_SUCCESS, error); if (error) { @@ -172,11 +179,12 @@ TEST(BinaryToTextSmall, OperandWithOperands) { %fn = OpFunction %void None %fnType )"); spv_result_t error = - spvTextToBinary(input, opcodeTable, operandTable, - extInstTable, &binary, &diagnostic); + spvTextToBinary(input.str.c_str(), input.str.length(), opcodeTable, + operandTable, extInstTable, &binary, &diagnostic); ASSERT_EQ(SPV_SUCCESS, error); spv_text text; - error = spvBinaryToText(binary, SPV_BINARY_TO_TEXT_OPTION_NONE, opcodeTable, + error = spvBinaryToText(binary->code, binary->wordCount, + SPV_BINARY_TO_TEXT_OPTION_NONE, opcodeTable, operandTable, extInstTable, &text, &diagnostic); EXPECT_EQ(SPV_SUCCESS, error); if (error) { diff --git a/test/Comment.cpp b/test/Comment.cpp index 4974fd4a..556fc4b6 100644 --- a/test/Comment.cpp +++ b/test/Comment.cpp @@ -40,8 +40,9 @@ TEST_F(TextToBinaryTest, Whitespace) { %glsl450 = OpExtInstImport "GLSL.std.450" ; comment indented )"); - EXPECT_EQ(SPV_SUCCESS, spvTextToBinary(&text, opcodeTable, operandTable, - extInstTable, &binary, &diagnostic)); + EXPECT_EQ(SPV_SUCCESS, + spvTextToBinary(text.str, text.length, opcodeTable, operandTable, + extInstTable, &binary, &diagnostic)); if (diagnostic) { spvDiagnosticPrint(diagnostic); } diff --git a/test/ExtInstGLSLstd450.cpp b/test/ExtInstGLSLstd450.cpp index 7ae39fab..d3237519 100644 --- a/test/ExtInstGLSLstd450.cpp +++ b/test/ExtInstGLSLstd450.cpp @@ -82,11 +82,11 @@ OpFunctionEnd ; Generator: Khronos ; Bound: 10 ; Schema: 0)"; - spv_text_t text = {spirv.c_str(), spirv.size()}; spv_binary binary; spv_diagnostic diagnostic; - spv_result_t error = spvTextToBinary(&text, opcodeTable, operandTable, - extInstTable, &binary, &diagnostic); + spv_result_t error = + spvTextToBinary(spirv.c_str(), spirv.size(), opcodeTable, operandTable, + extInstTable, &binary, &diagnostic); if (error) { spvDiagnosticPrint(diagnostic); spvDiagnosticDestroy(diagnostic); @@ -112,9 +112,9 @@ OpFunctionEnd // Check round trip gives the same text. spv_text output_text; - error = - spvBinaryToText(binary, SPV_BINARY_TO_TEXT_OPTION_NONE, opcodeTable, - operandTable, extInstTable, &output_text, &diagnostic); + error = spvBinaryToText( + binary->code, binary->wordCount, SPV_BINARY_TO_TEXT_OPTION_NONE, + opcodeTable, operandTable, extInstTable, &output_text, &diagnostic); if (error) { spvDiagnosticPrint(diagnostic); diff --git a/test/ImmediateInt.cpp b/test/ImmediateInt.cpp index 3ed39a25..eafb3e4c 100644 --- a/test/ImmediateInt.cpp +++ b/test/ImmediateInt.cpp @@ -39,8 +39,9 @@ using test_fixture::TextToBinaryTest; TEST_F(TextToBinaryTest, ImmediateIntOpCode) { SetText("!0x00FF00FF"); - ASSERT_EQ(SPV_SUCCESS, spvTextToBinary(&text, opcodeTable, operandTable, - extInstTable, &binary, &diagnostic)); + ASSERT_EQ(SPV_SUCCESS, + spvTextToBinary(text.str, text.length, opcodeTable, operandTable, + extInstTable, &binary, &diagnostic)); EXPECT_EQ(0x00FF00FF, binary->code[5]); if (diagnostic) { spvDiagnosticPrint(diagnostic); @@ -49,8 +50,9 @@ TEST_F(TextToBinaryTest, ImmediateIntOpCode) { TEST_F(TextToBinaryTest, ImmediateIntOperand) { SetText("OpCapability !0x00FF00FF"); - EXPECT_EQ(SPV_SUCCESS, spvTextToBinary(&text, opcodeTable, operandTable, - extInstTable, &binary, &diagnostic)); + EXPECT_EQ(SPV_SUCCESS, + spvTextToBinary(text.str, text.length, opcodeTable, operandTable, + extInstTable, &binary, &diagnostic)); EXPECT_EQ(0x00FF00FF, binary->code[6]); if (diagnostic) { spvDiagnosticPrint(diagnostic); diff --git a/test/NamedId.cpp b/test/NamedId.cpp index 8a452aed..cc88c720 100644 --- a/test/NamedId.cpp +++ b/test/NamedId.cpp @@ -50,8 +50,9 @@ TEST(NamedId, Default) { ASSERT_EQ(SPV_SUCCESS, spvExtInstTableGet(&extInstTable)); spv_binary binary = nullptr; spv_diagnostic diagnostic; - spv_result_t error = spvTextToBinary(&text, opcodeTable, operandTable, - extInstTable, &binary, &diagnostic); + spv_result_t error = + spvTextToBinary(text.str, text.length, opcodeTable, operandTable, + extInstTable, &binary, &diagnostic); if (error) { spvDiagnosticPrint(diagnostic); spvDiagnosticDestroy(diagnostic); @@ -59,7 +60,8 @@ TEST(NamedId, Default) { ASSERT_EQ(SPV_SUCCESS, error); } error = spvBinaryToText( - binary, SPV_BINARY_TO_TEXT_OPTION_PRINT | SPV_BINARY_TO_TEXT_OPTION_COLOR, + binary->code, binary->wordCount, + SPV_BINARY_TO_TEXT_OPTION_PRINT | SPV_BINARY_TO_TEXT_OPTION_COLOR, opcodeTable, operandTable, extInstTable, nullptr, &diagnostic); if (error) { spvDiagnosticPrint(diagnostic); diff --git a/test/TestFixture.h b/test/TestFixture.h index e7c4cb88..26fd56bc 100644 --- a/test/TestFixture.h +++ b/test/TestFixture.h @@ -68,10 +68,9 @@ class TextToBinaryTestBase : public T { // Compiles SPIR-V text, asserting compilation success. Returns the compiled // code. SpirvVector CompileSuccessfully(const std::string& text) { - SetText(text); spv_result_t status = - spvTextToBinary(&this->text, opcodeTable, operandTable, extInstTable, - &binary, &diagnostic); + spvTextToBinary(text.c_str(), text.size(), opcodeTable, + operandTable, extInstTable, &binary, &diagnostic); EXPECT_EQ(SPV_SUCCESS, status) << text; SpirvVector code_copy; if (status == SPV_SUCCESS) { @@ -86,10 +85,9 @@ class TextToBinaryTestBase : public T { // Compiles SPIR-V text, asserting compilation failure. Returns the error // message(s). std::string CompileFailure(const std::string& text) { - SetText(text); EXPECT_NE(SPV_SUCCESS, - spvTextToBinary(&this->text, opcodeTable, operandTable, - extInstTable, &binary, &diagnostic)) + spvTextToBinary(text.c_str(), text.size(), opcodeTable, + operandTable, extInstTable, &binary, &diagnostic)) << text; DestroyBinary(); return diagnostic->error; diff --git a/test/TextDestroy.cpp b/test/TextDestroy.cpp index a465b631..023d9328 100644 --- a/test/TextDestroy.cpp +++ b/test/TextDestroy.cpp @@ -63,11 +63,12 @@ TEST(TextDestroy, Default) { OpTypeFloat 13 64 OpTypeVector 14 3 2 )"; - spv_text_t text = {textStr, strlen(textStr)}; + spv_binary binary = nullptr; spv_diagnostic diagnostic = nullptr; - EXPECT_EQ(SPV_SUCCESS, spvTextToBinary(&text, opcodeTable, operandTable, - extInstTable, &binary, &diagnostic)); + EXPECT_EQ(SPV_SUCCESS, + spvTextToBinary(textStr, strlen(textStr), opcodeTable, operandTable, + extInstTable, &binary, &diagnostic)); EXPECT_NE(nullptr, binary); EXPECT_NE(nullptr, binary->code); EXPECT_NE(0, binary->wordCount); @@ -77,17 +78,18 @@ TEST(TextDestroy, Default) { } spv_text resultText = nullptr; - EXPECT_EQ(SPV_SUCCESS, - spvBinaryToText(binary, 0, opcodeTable, operandTable, extInstTable, - &resultText, &diagnostic)); + EXPECT_EQ( + SPV_SUCCESS, + spvBinaryToText(binary->code, binary->wordCount, 0, opcodeTable, + operandTable, extInstTable, &resultText, &diagnostic)); spvBinaryDestroy(binary); if (diagnostic) { spvDiagnosticPrint(diagnostic); spvDiagnosticDestroy(diagnostic); ASSERT_TRUE(false); } - EXPECT_NE(nullptr, text.str); - EXPECT_NE(0, text.length); + EXPECT_NE(nullptr, resultText->str); + EXPECT_NE(0, resultText->length); spvTextDestroy(resultText); } diff --git a/test/TextToBinary.cpp b/test/TextToBinary.cpp index 16e02c30..bc2c1fe4 100644 --- a/test/TextToBinary.cpp +++ b/test/TextToBinary.cpp @@ -67,7 +67,6 @@ TEST(TextToBinary, Default) { %14 = OpTypeFloat 64 %15 = OpTypeVector 4 2 )"; - spv_text_t text = {textStr, strlen(textStr)}; spv_opcode_table opcodeTable; ASSERT_EQ(SPV_SUCCESS, spvOpcodeTableGet(&opcodeTable)); @@ -80,7 +79,7 @@ TEST(TextToBinary, Default) { spv_binary binary; spv_diagnostic diagnostic = nullptr; - spv_result_t error = spvTextToBinary(&text, opcodeTable, operandTable, + spv_result_t error = spvTextToBinary(textStr, strlen(textStr), opcodeTable, operandTable, extInstTable, &binary, &diagnostic); if (error) { @@ -89,8 +88,8 @@ TEST(TextToBinary, Default) { ASSERT_EQ(SPV_SUCCESS, error); } - EXPECT_NE(nullptr, text.str); - EXPECT_NE(0, text.length); + EXPECT_NE(nullptr, binary->code); + EXPECT_NE(0, binary->wordCount); // TODO: Verify binary ASSERT_EQ(SPV_MAGIC_NUMBER, binary->code[SPV_INDEX_MAGIC_NUMBER]); @@ -206,46 +205,46 @@ TEST(TextToBinary, Default) { } TEST_F(TextToBinaryTest, InvalidText) { - spv_text_t text = {nullptr, 0}; spv_binary binary; ASSERT_EQ(SPV_ERROR_INVALID_TEXT, - spvTextToBinary(&text, opcodeTable, operandTable, extInstTable, + spvTextToBinary(nullptr, 0, opcodeTable, operandTable, extInstTable, &binary, &diagnostic)); } TEST_F(TextToBinaryTest, InvalidTable) { - SetText("OpEntryPoint Kernel 0 \"\"\nOpExecutionMode 0 LocalSizeHint 1 1 1\n"); + SetText( + "OpEntryPoint Kernel 0 \"\"\nOpExecutionMode 0 LocalSizeHint 1 1 1\n"); ASSERT_EQ(SPV_ERROR_INVALID_TABLE, - spvTextToBinary(&text, nullptr, operandTable, extInstTable, &binary, - &diagnostic)); + spvTextToBinary(text.str, text.length, nullptr, operandTable, + extInstTable, &binary, &diagnostic)); ASSERT_EQ(SPV_ERROR_INVALID_TABLE, - spvTextToBinary(&text, opcodeTable, nullptr, extInstTable, &binary, - &diagnostic)); + spvTextToBinary(text.str, text.length, opcodeTable, nullptr, + extInstTable, &binary, &diagnostic)); ASSERT_EQ(SPV_ERROR_INVALID_TABLE, - spvTextToBinary(&text, opcodeTable, operandTable, nullptr, &binary, - &diagnostic)); + spvTextToBinary(text.str, text.length, opcodeTable, operandTable, + nullptr, &binary, &diagnostic)); } TEST_F(TextToBinaryTest, InvalidPointer) { SetText("OpEntryPoint Kernel 0 \"\"\nOpExecutionMode 0 LocalSizeHint 1 1 1\n"); ASSERT_EQ(SPV_ERROR_INVALID_POINTER, - spvTextToBinary(&text, opcodeTable, operandTable, extInstTable, - nullptr, &diagnostic)); + spvTextToBinary(text.str, text.length, opcodeTable, operandTable, + extInstTable, nullptr, &diagnostic)); } TEST_F(TextToBinaryTest, InvalidDiagnostic) { SetText("OpEntryPoint Kernel 0 \"\"\nOpExecutionMode 0 LocalSizeHint 1 1 1\n"); spv_binary binary; ASSERT_EQ(SPV_ERROR_INVALID_DIAGNOSTIC, - spvTextToBinary(&text, opcodeTable, operandTable, extInstTable, - &binary, nullptr)); + spvTextToBinary(text.str, text.length, opcodeTable, operandTable, + extInstTable, &binary, nullptr)); } TEST_F(TextToBinaryTest, InvalidPrefix) { SetText("Invalid"); ASSERT_EQ(SPV_ERROR_INVALID_TEXT, - spvTextToBinary(&text, opcodeTable, operandTable, extInstTable, - &binary, &diagnostic)); + spvTextToBinary(text.str, text.length, opcodeTable, operandTable, + extInstTable, &binary, &diagnostic)); if (diagnostic) { spvDiagnosticPrint(diagnostic); } @@ -253,8 +252,9 @@ TEST_F(TextToBinaryTest, InvalidPrefix) { TEST_F(TextToBinaryTest, StringSpace) { SetText("OpSourceExtension \"string with spaces\""); - EXPECT_EQ(SPV_SUCCESS, spvTextToBinary(&text, opcodeTable, operandTable, - extInstTable, &binary, &diagnostic)); + EXPECT_EQ(SPV_SUCCESS, + spvTextToBinary(text.str, text.length, opcodeTable, operandTable, + extInstTable, &binary, &diagnostic)); if (diagnostic) { spvDiagnosticPrint(diagnostic); } @@ -283,8 +283,9 @@ TEST_F(TextToBinaryTest, InstructionTwoFormats) { OpFunctionEnd )"); - EXPECT_EQ(SPV_SUCCESS, spvTextToBinary(&text, opcodeTable, operandTable, - extInstTable, &binary, &diagnostic)); + EXPECT_EQ(SPV_SUCCESS, + spvTextToBinary(text.str, text.length, opcodeTable, operandTable, + extInstTable, &binary, &diagnostic)); if (diagnostic) { spvDiagnosticPrint(diagnostic); } @@ -298,8 +299,8 @@ Google )"); EXPECT_EQ(SPV_ERROR_INVALID_TEXT, - spvTextToBinary(&text, opcodeTable, operandTable, extInstTable, - &binary, &diagnostic)); + spvTextToBinary(text.str, text.length, opcodeTable, operandTable, + extInstTable, &binary, &diagnostic)); EXPECT_EQ(4, diagnostic->position.line + 1); EXPECT_EQ(1, diagnostic->position.column + 1); EXPECT_STREQ( @@ -316,8 +317,8 @@ TEST_F(TextToBinaryTest, NoEqualSign) { )"); EXPECT_EQ(SPV_ERROR_INVALID_TEXT, - spvTextToBinary(&text, opcodeTable, operandTable, extInstTable, - &binary, &diagnostic)); + spvTextToBinary(text.str, text.length, opcodeTable, operandTable, + extInstTable, &binary, &diagnostic)); EXPECT_EQ(5, diagnostic->position.line + 1); EXPECT_EQ(1, diagnostic->position.column + 1); EXPECT_STREQ("Expected '=', found end of stream.", diagnostic->error); @@ -331,8 +332,8 @@ TEST_F(TextToBinaryTest, NoOpCode) { )"); EXPECT_EQ(SPV_ERROR_INVALID_TEXT, - spvTextToBinary(&text, opcodeTable, operandTable, extInstTable, - &binary, &diagnostic)); + spvTextToBinary(text.str, text.length, opcodeTable, operandTable, + extInstTable, &binary, &diagnostic)); EXPECT_EQ(5, diagnostic->position.line + 1); EXPECT_EQ(1, diagnostic->position.column + 1); EXPECT_STREQ("Expected opcode, found end of stream.", diagnostic->error); @@ -346,8 +347,8 @@ TEST_F(TextToBinaryTest, WrongOpCode) { )"); EXPECT_EQ(SPV_ERROR_INVALID_TEXT, - spvTextToBinary(&text, opcodeTable, operandTable, extInstTable, - &binary, &diagnostic)); + spvTextToBinary(text.str, text.length, opcodeTable, operandTable, + extInstTable, &binary, &diagnostic)); EXPECT_EQ(4, diagnostic->position.line + 1); EXPECT_EQ(6, diagnostic->position.column + 1); EXPECT_STREQ("Invalid Opcode prefix 'Wahahaha'.", diagnostic->error); @@ -395,8 +396,8 @@ TEST_F(TextToBinaryTest, BadSwitchTruncatedCase) { )"); EXPECT_EQ(SPV_ERROR_INVALID_TEXT, - spvTextToBinary(&text, opcodeTable, operandTable, extInstTable, - &binary, &diagnostic)); + spvTextToBinary(text.str, text.length, opcodeTable, operandTable, + extInstTable, &binary, &diagnostic)); EXPECT_EQ(6, diagnostic->position.line + 1); EXPECT_EQ(1, diagnostic->position.column + 1); EXPECT_STREQ("Expected operand, found next instruction instead.", diagnostic->error); diff --git a/test/Validate.cpp b/test/Validate.cpp index e6fc4229..949f336c 100644 --- a/test/Validate.cpp +++ b/test/Validate.cpp @@ -58,10 +58,10 @@ OpLabel 4 OpReturn OpFunctionEnd )"; - spv_text_t text = {str, strlen(str)}; spv_diagnostic diagnostic = nullptr; - ASSERT_EQ(SPV_SUCCESS, spvTextToBinary(&text, opcodeTable, operandTable, - extInstTable, &binary, &diagnostic)); + ASSERT_EQ(SPV_SUCCESS, + spvTextToBinary(str, strlen(str), opcodeTable, operandTable, + extInstTable, &binary, &diagnostic)); ASSERT_EQ(SPV_SUCCESS, spvValidate(binary, opcodeTable, operandTable, extInstTable, SPV_VALIDATE_ALL, &diagnostic)); @@ -83,9 +83,8 @@ OpLabel 4 OpReturn OpFunctionEnd )"; - spv_text_t text = {str, strlen(str)}; spv_diagnostic diagnostic = nullptr; - ASSERT_EQ(SPV_SUCCESS, spvTextToBinary(&text, opcodeTable, operandTable, + ASSERT_EQ(SPV_SUCCESS, spvTextToBinary(str, strlen(str), opcodeTable, operandTable, extInstTable, &binary, &diagnostic)); ASSERT_EQ(SPV_ERROR_INVALID_ID, spvValidate(binary, opcodeTable, operandTable, extInstTable, @@ -107,10 +106,10 @@ OpLabel 4 OpReturn OpFunctionEnd )"; - spv_text_t text = {str, strlen(str)}; spv_diagnostic diagnostic = nullptr; - ASSERT_EQ(SPV_SUCCESS, spvTextToBinary(&text, opcodeTable, operandTable, - extInstTable, &binary, &diagnostic)); + ASSERT_EQ(SPV_SUCCESS, + spvTextToBinary(str, strlen(str), opcodeTable, operandTable, + extInstTable, &binary, &diagnostic)); // TODO: Fix setting of bound in spvTextTo, then remove this! ASSERT_EQ(SPV_ERROR_INVALID_ID, spvValidate(binary, opcodeTable, operandTable, extInstTable, diff --git a/test/ValidateID.cpp b/test/ValidateID.cpp index 982c30d7..8bcec998 100644 --- a/test/ValidateID.cpp +++ b/test/ValidateID.cpp @@ -53,9 +53,8 @@ class ValidateID : public ::testing::Test { }; #define CHECK(str, expected) \ - spv_text_t text = {str, strlen(str)}; \ spv_diagnostic diagnostic; \ - spv_result_t error = spvTextToBinary(&text, opcodeTable, operandTable, \ + spv_result_t error = spvTextToBinary(str, strlen(str), opcodeTable, operandTable, \ extInstTable, &binary, &diagnostic); \ if (error) { \ spvDiagnosticPrint(diagnostic); \ diff --git a/tools/as/as.cpp b/tools/as/as.cpp index b36a1d98..53136312 100644 --- a/tools/as/as.cpp +++ b/tools/as/as.cpp @@ -88,8 +88,6 @@ int main(int argc, char **argv) { return 1; } - spv_text_t text = {contents.data(), contents.size()}; - spv_opcode_table opcodeTable; spv_result_t error = spvOpcodeTableGet(&opcodeTable); spvCheck(error, fprintf(stderr, "error: internal malfunction\n"); @@ -106,8 +104,8 @@ int main(int argc, char **argv) { spv_binary binary; spv_diagnostic diagnostic = nullptr; - error = spvTextToBinary(&text, opcodeTable, operandTable, extInstTable, - &binary, &diagnostic); + error = spvTextToBinary(contents.data(), contents.size(), opcodeTable, + operandTable, extInstTable, &binary, &diagnostic); spvCheck(error, spvDiagnosticPrint(diagnostic); spvDiagnosticDestroy(diagnostic); return error); diff --git a/tools/dis/dis.cpp b/tools/dis/dis.cpp index 5361339e..486a79cf 100644 --- a/tools/dis/dis.cpp +++ b/tools/dis/dis.cpp @@ -98,8 +98,6 @@ int main(int argc, char **argv) { return 1; } - spv_binary_t binary = {contents.data(), contents.size()}; - spv_opcode_table opcodeTable; spv_result_t error = spvOpcodeTableGet(&opcodeTable); spvCheck(error, fprintf(stderr, "error: internal malfunction\n"); @@ -126,8 +124,9 @@ int main(int argc, char **argv) { spv_text text; spv_text *textOrNull = printOptionOn ? nullptr : &text; spv_diagnostic diagnostic = nullptr; - error = spvBinaryToText(&binary, options, opcodeTable, operandTable, - extInstTable, textOrNull, &diagnostic); + error = + spvBinaryToText(contents.data(), contents.size(), options, opcodeTable, + operandTable, extInstTable, textOrNull, &diagnostic); spvCheck(error, spvDiagnosticPrint(diagnostic); spvDiagnosticDestroy(diagnostic); return error);