Fixed warnings on windows and constness of spv_binary.
authorAndrew Woloszyn <awoloszyn@google.com>
Wed, 11 Nov 2015 16:05:07 +0000 (11:05 -0500)
committerDavid Neto <dneto@google.com>
Wed, 11 Nov 2015 17:12:13 +0000 (12:12 -0500)
Replaced uint64_t with size_t in the places that make sense and
added spv_const_binary{,_t} to allow the interface to accept non
modifiable spirv where appropriate.

13 files changed:
include/libspirv/libspirv.h [changed mode: 0644->0755]
source/binary.cpp [changed mode: 0644->0755]
source/binary.h [changed mode: 0644->0755]
source/endian.cpp [changed mode: 0644->0755]
source/endian.h [changed mode: 0644->0755]
source/text.cpp [changed mode: 0644->0755]
source/validate.cpp [changed mode: 0644->0755]
test/BinaryEndianness.cpp [changed mode: 0644->0755]
test/BinaryHeaderGet.cpp [changed mode: 0644->0755]
test/HexFloat.cpp [changed mode: 0644->0755]
test/Validate.cpp [changed mode: 0644->0755]
test/ValidateID.cpp [changed mode: 0644->0755]
tools/val/val.cpp [changed mode: 0644->0755]

old mode 100644 (file)
new mode 100755 (executable)
index b194160..e724ff1
@@ -380,19 +380,25 @@ typedef struct spv_ext_inst_table_t {
 } spv_ext_inst_table_t;
 
 typedef struct spv_binary_t {
-  const uint32_t* code;
-  uint64_t wordCount;
+  uint32_t* code;
+  size_t wordCount;
 } spv_binary_t;
 
+typedef struct spv_const_binary_t {
+  const uint32_t* code;
+  const size_t wordCount;
+} spv_const_binary_t;
+
+
 typedef struct spv_text_t {
   const char* str;
-  uint64_t length;
+  size_t length;
 } spv_text_t;
 
 typedef struct spv_position_t {
-  uint64_t line;
-  uint64_t column;
-  uint64_t index;
+  size_t line;
+  size_t column;
+  size_t index;
 } spv_position_t;
 
 typedef struct spv_diagnostic_t {
@@ -409,6 +415,7 @@ typedef const spv_operand_desc_t* spv_operand_desc;
 typedef const spv_operand_table_t* spv_operand_table;
 typedef const spv_ext_inst_desc_t* spv_ext_inst_desc;
 typedef const spv_ext_inst_table_t* spv_ext_inst_table;
+typedef spv_const_binary_t* spv_const_binary;
 typedef spv_binary_t* spv_binary;
 typedef spv_text_t* spv_text;
 typedef spv_position_t* spv_position;
@@ -452,7 +459,7 @@ spv_result_t spvExtInstTableGet(spv_ext_inst_table* pTable);
 /// @param[out] pDiagnostic contains diagnostic on failure
 ///
 /// @return result code
-spv_result_t spvTextToBinary(const char* text, const uint64_t length,
+spv_result_t spvTextToBinary(const char* text, const size_t length,
                              const spv_opcode_table opcodeTable,
                              const spv_operand_table operandTable,
                              const spv_ext_inst_table extInstTable,
@@ -505,7 +512,7 @@ void spvBinaryDestroy(spv_binary binary);
 /// @param[out] pDiagnostic contains diagnostic on failure
 ///
 /// @return result code
-spv_result_t spvValidate(const spv_binary binary,
+spv_result_t spvValidate(const spv_const_binary binary,
                          const spv_opcode_table opcodeTable,
                          const spv_operand_table operandTable,
                          const spv_ext_inst_table extInstTable,
old mode 100644 (file)
new mode 100755 (executable)
index 699f6b0..5e4c732
@@ -39,7 +39,7 @@
 #include "opcode.h"
 #include "operand.h"
 
-spv_result_t spvBinaryHeaderGet(const spv_binary binary,
+spv_result_t spvBinaryHeaderGet(const spv_const_binary binary,
                                 const spv_endianness_t endian,
                                 spv_header_t* pHeader) {
   if (!binary->code) return SPV_ERROR_INVALID_BINARY;
@@ -241,7 +241,7 @@ spv_result_t Parser::parseModule() {
                         << " words instead of " << SPV_INDEX_INSTRUCTION;
 
   // Check the magic number and detect the module's endianness.
-  spv_binary_t binary = {_.words, _.num_words};  // Can't make this const. :-(
+  spv_const_binary_t binary{_.words, _.num_words};
   if (spvBinaryEndianness(&binary, &_.endian)) {
     return diagnostic() << "Invalid SPIR-V magic number '" << std::hex
                         << _.words[0] << "'.";
@@ -671,7 +671,7 @@ void Parser::recordNumberType(const spv_parsed_instruction_t* inst) {
 
 }  // anonymous namespace
 
-spv_result_t spvBinaryParse(void* user_data, const uint32_t* const code,
+spv_result_t spvBinaryParse(void* user_data, const uint32_t* code,
                             const size_t num_words,
                             spv_parsed_header_fn_t parsed_header,
                             spv_parsed_instruction_fn_t parsed_instruction,
old mode 100644 (file)
new mode 100755 (executable)
index 08f0699..889716f
@@ -107,7 +107,7 @@ typedef spv_result_t (*spv_parsed_instruction_fn_t)(
 // returns SPV_ERROR_INVALID_BINARY and emits a diagnostic.  If a callback
 // returns anything other than SPV_SUCCESS, then that error code is returned
 // and parsing terminates early.
-spv_result_t spvBinaryParse(void* user_data, const uint32_t* const words,
+spv_result_t spvBinaryParse(void* user_data, const uint32_t* words,
                             const size_t num_words,
                             spv_parsed_header_fn_t parse_header,
                             spv_parsed_instruction_fn_t parse_instruction,
@@ -124,7 +124,7 @@ spv_result_t spvBinaryParse(void* user_data, const uint32_t* const words,
 /// @param[out] pHeader the returned header
 ///
 /// @return result code
-spv_result_t spvBinaryHeaderGet(const spv_binary binary,
+spv_result_t spvBinaryHeaderGet(const spv_const_binary binary,
                                 const spv_endianness_t endian,
                                 spv_header_t* pHeader);
 
old mode 100644 (file)
new mode 100755 (executable)
index 44d9ac6..e0b2b2b
@@ -56,7 +56,7 @@ uint64_t spvFixDoubleWord(const uint32_t low, const uint32_t high,
   return (uint64_t(spvFixWord(high, endian)) << 32) | spvFixWord(low, endian);
 }
 
-spv_result_t spvBinaryEndianness(const spv_binary binary,
+spv_result_t spvBinaryEndianness(spv_const_binary binary,
                                  spv_endianness_t* pEndian) {
   if (!binary->code || !binary->wordCount) return SPV_ERROR_INVALID_BINARY;
   if (!pEndian) return SPV_ERROR_INVALID_POINTER;
old mode 100644 (file)
new mode 100755 (executable)
index e03eeb2..c5c097a
@@ -57,7 +57,7 @@ uint64_t spvFixDoubleWord(const uint32_t low, const uint32_t high,
 /// @param[out] pEndian return the endianness of the SPV module
 ///
 /// @return result code
-spv_result_t spvBinaryEndianness(const spv_binary binary,
+spv_result_t spvBinaryEndianness(const spv_const_binary binary,
                                  spv_endianness_t* pEndian);
 
 #endif  // LIBSPIRV_ENDIAN_H_
old mode 100644 (file)
new mode 100755 (executable)
index 48222b7..77da016
@@ -737,7 +737,7 @@ spv_result_t spvTextToBinaryInternal(const libspirv::AssemblyGrammar& grammar,
 }  // anonymous namespace
 
 spv_result_t spvTextToBinary(const char* input_text,
-                             const uint64_t input_text_size,
+                             const size_t input_text_size,
                              const spv_opcode_table opcodeTable,
                              const spv_operand_table operandTable,
                              const spv_ext_inst_table extInstTable,
old mode 100644 (file)
new mode 100755 (executable)
index c8de619..3a142fa
@@ -259,7 +259,7 @@ spv_result_t spvValidateIDs(const spv_instruction_t* pInsts,
   return SPV_SUCCESS;
 }
 
-spv_result_t spvValidate(const spv_binary binary,
+spv_result_t spvValidate(const spv_const_binary binary,
                          const spv_opcode_table opcodeTable,
                          const spv_operand_table operandTable,
                          const spv_ext_inst_table extInstTable,
old mode 100644 (file)
new mode 100755 (executable)
index 484f248..480f652
@@ -30,7 +30,7 @@ namespace {
 
 TEST(BinaryEndianness, InvalidCode) {
   uint32_t invalidMagicNumber[] = {0};
-  spv_binary_t binary = {invalidMagicNumber, 1};
+  spv_const_binary_t binary = {invalidMagicNumber, 1};
   spv_endianness_t endian;
   ASSERT_EQ(SPV_ERROR_INVALID_BINARY, spvBinaryEndianness(&binary, &endian));
 }
@@ -42,7 +42,7 @@ TEST(BinaryEndianness, Little) {
   } else {
     magicNumber = 0x03022307;
   }
-  spv_binary_t binary = {&magicNumber, 1};
+  spv_const_binary_t binary = {&magicNumber, 1};
   spv_endianness_t endian;
   ASSERT_EQ(SPV_SUCCESS, spvBinaryEndianness(&binary, &endian));
   ASSERT_EQ(SPV_ENDIANNESS_LITTLE, endian);
@@ -55,7 +55,7 @@ TEST(BinaryEndianness, Big) {
   } else {
     magicNumber = 0x03022307;
   }
-  spv_binary_t binary = {&magicNumber, 1};
+  spv_const_binary_t binary = {&magicNumber, 1};
   spv_endianness_t endian;
   ASSERT_EQ(SPV_SUCCESS, spvBinaryEndianness(&binary, &endian));
   ASSERT_EQ(SPV_ENDIANNESS_BIG, endian);
old mode 100644 (file)
new mode 100755 (executable)
index 0d7bec0..853912b
@@ -43,7 +43,9 @@ class BinaryHeaderGet : public ::testing::Test {
     binary.code = code;
     binary.wordCount = 6;
   }
-
+  spv_const_binary_t get_const_binary() {
+    return spv_const_binary_t{binary.code, binary.wordCount};
+  }
   virtual void TearDown() {}
 
   uint32_t code[6];
@@ -52,10 +54,11 @@ class BinaryHeaderGet : public ::testing::Test {
 
 TEST_F(BinaryHeaderGet, Default) {
   spv_endianness_t endian;
-  ASSERT_EQ(SPV_SUCCESS, spvBinaryEndianness(&binary, &endian));
+  spv_const_binary_t const_bin = get_const_binary();
+  ASSERT_EQ(SPV_SUCCESS, spvBinaryEndianness(&const_bin, &endian));
 
   spv_header_t header;
-  ASSERT_EQ(SPV_SUCCESS, spvBinaryHeaderGet(&binary, endian, &header));
+  ASSERT_EQ(SPV_SUCCESS, spvBinaryHeaderGet(&const_bin, endian, &header));
 
   ASSERT_EQ(static_cast<uint32_t>(SpvMagicNumber), header.magic);
   ASSERT_EQ(99u, header.version);
@@ -66,22 +69,24 @@ TEST_F(BinaryHeaderGet, Default) {
 }
 
 TEST_F(BinaryHeaderGet, InvalidCode) {
-  spv_binary_t binary = {nullptr, 0};
+  spv_const_binary_t binary = {nullptr, 0};
   spv_header_t header;
   ASSERT_EQ(SPV_ERROR_INVALID_BINARY,
             spvBinaryHeaderGet(&binary, SPV_ENDIANNESS_LITTLE, &header));
 }
 
 TEST_F(BinaryHeaderGet, InvalidPointerHeader) {
+  spv_const_binary_t const_bin = get_const_binary();
   ASSERT_EQ(SPV_ERROR_INVALID_POINTER,
-            spvBinaryHeaderGet(&binary, SPV_ENDIANNESS_LITTLE, nullptr));
+            spvBinaryHeaderGet(&const_bin, SPV_ENDIANNESS_LITTLE, nullptr));
 }
 
 TEST_F(BinaryHeaderGet, TruncatedHeader) {
   for (int i = 1; i < SPV_INDEX_INSTRUCTION; i++) {
     binary.wordCount = i;
+    spv_const_binary_t const_bin = get_const_binary();
     ASSERT_EQ(SPV_ERROR_INVALID_BINARY,
-              spvBinaryHeaderGet(&binary, SPV_ENDIANNESS_LITTLE, nullptr));
+              spvBinaryHeaderGet(&const_bin, SPV_ENDIANNESS_LITTLE, nullptr));
   }
 }
 
old mode 100644 (file)
new mode 100755 (executable)
index ee3be89..b4e96d7
 // TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
 // MATERIALS OR THE USE OR OTHER DEALINGS IN THE MATERIALS.
 
+#ifdef _MSC_VER
+// We define this so that we can use sscanf in the tests without
+// MSVC warning us all the time.
+#define _CRT_SECURE_NO_WARNINGS
+#endif
+
 #include <cmath>
 #include <cstdio>
 #include <sstream>
old mode 100644 (file)
new mode 100755 (executable)
index 2072ffe..7d1a9ca
@@ -39,6 +39,9 @@ class Validate : public ::testing::Test {
   }
 
   virtual void TearDown() { spvBinaryDestroy(binary); }
+  spv_const_binary get_const_binary() {
+      return spv_const_binary(binary);
+  }
 
   spv_binary binary;
   spv_opcode_table opcodeTable;
@@ -63,7 +66,7 @@ OpFunctionEnd
             spvTextToBinary(str, strlen(str), opcodeTable, operandTable,
                             extInstTable, &binary, &diagnostic));
   ASSERT_EQ(SPV_SUCCESS,
-            spvValidate(binary, opcodeTable, operandTable, extInstTable,
+            spvValidate(get_const_binary(), opcodeTable, operandTable, extInstTable,
                         SPV_VALIDATE_ALL, &diagnostic));
   if (diagnostic) {
     spvDiagnosticPrint(diagnostic);
@@ -88,7 +91,7 @@ OpFunctionEnd
             spvTextToBinary(str, strlen(str), opcodeTable, operandTable,
                             extInstTable, &binary, &diagnostic));
   ASSERT_EQ(SPV_ERROR_INVALID_ID,
-            spvValidate(binary, opcodeTable, operandTable, extInstTable,
+            spvValidate(get_const_binary(), opcodeTable, operandTable, extInstTable,
                         SPV_VALIDATE_ALL, &diagnostic));
   ASSERT_NE(nullptr, diagnostic);
   spvDiagnosticPrint(diagnostic);
@@ -113,7 +116,7 @@ OpFunctionEnd
                             extInstTable, &binary, &diagnostic));
   // TODO: Fix setting of bound in spvTextTo, then remove this!
   ASSERT_EQ(SPV_ERROR_INVALID_ID,
-            spvValidate(binary, opcodeTable, operandTable, extInstTable,
+            spvValidate(get_const_binary(), opcodeTable, operandTable, extInstTable,
                         SPV_VALIDATE_ALL, &diagnostic));
   ASSERT_NE(nullptr, diagnostic);
   spvDiagnosticPrint(diagnostic);
old mode 100644 (file)
new mode 100755 (executable)
index 9abd49f..17a963d
@@ -45,7 +45,9 @@ class ValidateID : public ::testing::Test {
   }
 
   virtual void TearDown() { spvBinaryDestroy(binary); }
-
+  spv_const_binary get_const_binary() {
+      return spv_const_binary(binary);
+  }
   spv_opcode_table opcodeTable;
   spv_operand_table operandTable;
   spv_ext_inst_table extInstTable;
@@ -63,8 +65,8 @@ class ValidateID : public ::testing::Test {
     ASSERT_EQ(SPV_SUCCESS, error);                                 \
   }                                                                \
   spv_result_t result =                                            \
-      spvValidate(binary, opcodeTable, operandTable, extInstTable, \
-                  SPV_VALIDATE_ID_BIT, &diagnostic);               \
+      spvValidate(get_const_binary(), opcodeTable, operandTable,   \
+                  extInstTable, SPV_VALIDATE_ID_BIT, &diagnostic); \
   if (SPV_SUCCESS != result) {                                     \
     spvDiagnosticPrint(diagnostic);                                \
     spvDiagnosticDestroy(diagnostic);                              \
old mode 100644 (file)
new mode 100755 (executable)
index e369ceb..8edfa85
@@ -96,7 +96,7 @@ int main(int argc, char** argv) {
     return 1;
   }
 
-  spv_binary_t binary = {contents.data(), contents.size()};
+  spv_const_binary_t binary = {contents.data(), contents.size()};
 
   spv_opcode_table opcodeTable;
   spv_result_t error = spvOpcodeTableGet(&opcodeTable);