Fix issues reported by cppcheck.
authorLei Zhang <antiagainst@google.com>
Thu, 25 Feb 2016 21:11:16 +0000 (16:11 -0500)
committerLei Zhang <antiagainst@gmail.com>
Thu, 25 Feb 2016 21:16:28 +0000 (16:16 -0500)
Remove code not being used, add explicit to constructors, and
add missing fields in constructors.

source/assembly_grammar.h
source/binary.cpp
source/opcode.cpp
source/print.h
source/text_handler.h
source/util/hex_float.h
source/validate.h
test/TextToBinary.cpp
test/UnitSPIRV.h
test/Validate.Layout.cpp

index fab31b8..3364f71 100644 (file)
@@ -37,7 +37,7 @@ namespace libspirv {
 // Contains methods to query for valid instructions and operands.
 class AssemblyGrammar {
  public:
-  AssemblyGrammar(const spv_const_context context)
+  explicit AssemblyGrammar(const spv_const_context context)
       : operandTable_(context->operand_table),
         opcodeTable_(context->opcode_table),
         extInstTable_(context->ext_inst_table) {}
index 2c42c92..5c6aa8f 100644 (file)
@@ -62,47 +62,6 @@ spv_result_t spvBinaryHeaderGet(const spv_const_binary binary,
   return SPV_SUCCESS;
 }
 
-// TODO(dneto): This API is not powerful enough in the case that the
-// number and type of operands are not known until partway through parsing
-// the operation.  This happens when enum operands might have different number
-// of operands, or with extended instructions.
-spv_operand_type_t spvBinaryOperandInfo(const uint32_t word,
-                                        const uint16_t operandIndex,
-                                        const spv_opcode_desc opcodeEntry,
-                                        const spv_operand_table operandTable,
-                                        spv_operand_desc* pOperandEntry) {
-  spv_operand_type_t type;
-  if (operandIndex < opcodeEntry->numTypes) {
-    // NOTE: Do operand table lookup to set operandEntry if successful
-    const int index = operandIndex - 1;
-    type = opcodeEntry->operandTypes[index];
-    spv_operand_desc entry = nullptr;
-    if (!spvOperandTableValueLookup(operandTable, type, word, &entry)) {
-      if (SPV_OPERAND_TYPE_NONE != entry->operandTypes[0]) {
-        *pOperandEntry = entry;
-      }
-    }
-  } else if (*pOperandEntry) {
-    // NOTE: Use specified operand entry operand type for this word
-    const int index = operandIndex - opcodeEntry->numTypes;
-    type = (*pOperandEntry)->operandTypes[index];
-  } else if (SpvOpSwitch == opcodeEntry->opcode) {
-    // NOTE: OpSwitch is a special case which expects a list of paired extra
-    // operands
-    assert(0 &&
-           "This case is previously untested, remove this assert and ensure it "
-           "is behaving correctly!");
-    const int lastIndex = opcodeEntry->numTypes - 1;
-    const int index = lastIndex + ((operandIndex - lastIndex) % 2);
-    type = opcodeEntry->operandTypes[index];
-  } else {
-    // NOTE: Default to last operand type in opcode entry
-    const int index = opcodeEntry->numTypes - 1;
-    type = opcodeEntry->operandTypes[index];
-  }
-  return type;
-}
-
 namespace {
 
 // A SPIR-V binary parser.  A parser instance communicates detailed parse
@@ -228,7 +187,8 @@ class Parser {
           num_words(num_words_arg),
           diagnostic(diagnostic_arg),
           word_index(0),
-          endian() {}
+          endian(),
+          requires_endian_conversion(false) {}
     State() : State(0, 0, nullptr) {}
     const uint32_t* words;       // Words in the binary SPIR-V module.
     size_t num_words;            // Number of words in the module.
index 63ba9a9..6e9d240 100644 (file)
@@ -466,13 +466,6 @@ int32_t spvOpcodeIsComposite(const SpvOp opcode) {
   }
 }
 
-int32_t spvOpcodeAreTypesEqual(const spv_instruction_t* pTypeInst0,
-                               const spv_instruction_t* pTypeInst1) {
-  if (pTypeInst0->opcode != pTypeInst1->opcode) return false;
-  if (pTypeInst0->words[1] != pTypeInst1->words[1]) return false;
-  return true;
-}
-
 int32_t spvOpcodeIsPointer(const SpvOp opcode) {
   switch (opcode) {
     case SpvOpVariable:
@@ -485,16 +478,6 @@ int32_t spvOpcodeIsPointer(const SpvOp opcode) {
   }
 }
 
-int32_t spvInstructionIsInBasicBlock(const spv_instruction_t* pFirstInst,
-                                     const spv_instruction_t* pInst) {
-  while (pFirstInst != pInst) {
-    if (SpvOpFunction == pInst->opcode) break;
-    pInst--;
-  }
-  if (SpvOpFunction != pInst->opcode) return false;
-  return true;
-}
-
 int32_t spvOpcodeGeneratesType(SpvOp op) {
   switch (op) {
     case SpvOpTypeVoid:
index 7ae3644..fe19969 100644 (file)
@@ -36,7 +36,7 @@ namespace libspirv {
 class out_stream {
  public:
   out_stream() : pStream(nullptr) {}
-  out_stream(std::stringstream& stream) : pStream(&stream) {}
+  explicit out_stream(std::stringstream& stream) : pStream(&stream) {}
 
   std::ostream& get() {
     if (pStream) {
index 6f003db..386a4dc 100644 (file)
@@ -263,10 +263,9 @@ class AssemblyContext {
     // It also allows octal input, but we don't care about that case.
     text_stream >> std::setbase(0);
     text_stream >> *value_pointer;
-    bool ok = true;
 
     // We should have read something.
-    ok = (text[0] != 0) && !text_stream.bad();
+    bool ok = (text[0] != 0) && !text_stream.bad();
     // It should have been all the text.
     ok = ok && text_stream.eof();
     // It should have been in range.
index 879d0ce..25db7f6 100644 (file)
@@ -42,11 +42,11 @@ class Float16 {
  public:
   Float16(uint16_t v) : val(v) {}
   Float16() = default;
-  static bool isNan(const Float16 val) {
+  static bool isNan(const Float16& val) {
     return ((val.val & 0x7C00) == 0x7C00) && ((val.val & 0x3FF) != 0);
   }
   // Returns true if the given value is any kind of infinity.
-  static bool isInfinity(const Float16 val) {
+  static bool isInfinity(const Float16& val) {
     return ((val.val & 0x7C00) == 0x7C00) && ((val.val & 0x3FF) == 0);
   }
   Float16(const Float16& other) { val = other.val; }
index 7833e5a..5438924 100644 (file)
@@ -95,7 +95,7 @@ class ValidationState_t;
 // types are used to manage the state while parsing the function.
 class Functions {
  public:
-  Functions(ValidationState_t& module);
+  explicit Functions(ValidationState_t& module);
 
   // Registers the function in the module. Subsequent instructions will be
   // called against this function
index e30e332..6dc8902 100644 (file)
@@ -134,12 +134,6 @@ INSTANTIATE_TEST_CASE_P(ParseMask, BadFPFastMathMaskParseTest,
                             "Unroll"  // A good word, but for the wrong enum
                         }));
 
-// TODO(dneto): Aliasing like this relies on undefined behaviour. Fix this.
-union char_word_t {
-  char cs[4];
-  uint32_t u;
-};
-
 TEST_F(TextToBinaryTest, InvalidText) {
   ASSERT_EQ(SPV_ERROR_INVALID_TEXT,
             spvTextToBinary(context, nullptr, 0, &binary, &diagnostic));
index 558b790..2e902b0 100644 (file)
@@ -169,7 +169,8 @@ inline std::vector<uint32_t> MakeVector(std::string input) {
 // A type for easily creating spv_text_t values, with an implicit conversion to
 // spv_text.
 struct AutoText {
-  AutoText(std::string value) : str(value), text({str.data(), str.size()}) {}
+  explicit AutoText(std::string value)
+      : str(value), text({str.data(), str.size()}) {}
   operator spv_text() { return &text; }
   std::string str;
   spv_text_t text;
index 308103b..8c69f21 100644 (file)
@@ -66,7 +66,7 @@ spv_result_t Equals(int order) {
 // returns true if order is between MIN and MAX(inclusive)
 template <int MIN, int MAX, spv_result_t RET = SPV_ERROR_INVALID_LAYOUT>
 struct Range {
-  Range(bool inverse = false) : inverse_(inverse) {}
+  explicit Range(bool inverse = false) : inverse_(inverse) {}
   spv_result_t operator()(int order) {
     return (inverse_ ^ (order >= MIN && order <= MAX)) ? SPV_SUCCESS : RET;
   }