[go: nahoru, domu]

Fix more MSVC warnings, courgette/ edition.

This is mostly about changing types and inserting casts so as to avoid implicit
value truncations.

BUG=81439
TEST=none

Review URL: https://codereview.chromium.org/613893002

Cr-Commit-Position: refs/heads/master@{#298069}
diff --git a/courgette/adjustment_method_2.cc b/courgette/adjustment_method_2.cc
index 791870a5..1119ccd 100644
--- a/courgette/adjustment_method_2.cc
+++ b/courgette/adjustment_method_2.cc
@@ -364,7 +364,7 @@
 // position of one of the occurrences in the Trace.
 class Shingle {
  public:
-  static const size_t kWidth = 5;
+  static const uint8 kWidth = 5;
 
   struct InterningLess {
     bool operator()(const Shingle& a, const Shingle& b) const;
@@ -430,7 +430,7 @@
 std::string ToString(const Shingle* instance) {
   std::string s;
   const char* sep = "<";
-  for (size_t i = 0; i < Shingle::kWidth; ++i) {
+  for (uint8 i = 0; i < Shingle::kWidth; ++i) {
     // base::StringAppendF(&s, "%s%x ", sep, instance.at(i)->label_->rva_);
     s += sep;
     s += ToString(instance->at(i));
@@ -446,7 +446,7 @@
 bool Shingle::InterningLess::operator()(
     const Shingle& a,
     const Shingle& b) const {
-  for (size_t i = 0;  i < kWidth;  ++i) {
+  for (uint8 i = 0; i < kWidth; ++i) {
     LabelInfo* info_a = a.at(i);
     LabelInfo* info_b = b.at(i);
     if (info_a->label_->rva_ < info_b->label_->rva_)
@@ -526,7 +526,7 @@
   } else {
     base::StringAppendF(&s, "<%d: ", index->variables_);
     const char* sep = "";
-    for (size_t i = 0;  i < Shingle::kWidth;  ++i) {
+    for (uint8 i = 0; i < Shingle::kWidth; ++i) {
       s += sep;
       sep = ", ";
       uint32 kind = index->kinds_[i];
@@ -622,7 +622,7 @@
     if (a.hash_ < b.hash_) return true;
     if (a.hash_ > b.hash_) return false;
 
-    for (size_t i = 0;  i < Shingle::kWidth;  ++i) {
+    for (uint8 i = 0; i < Shingle::kWidth; ++i) {
       if (a.kinds_[i] < b.kinds_[i]) return true;
       if (a.kinds_[i] > b.kinds_[i]) return false;
       if ((a.kinds_[i] & ShinglePattern::kVariable) == 0) {
@@ -647,11 +647,11 @@
   unique_variables_ = 0;
   first_variable_index_ = 255;
 
-  for (uint32 i = 0; i < Shingle::kWidth; ++i) {
+  for (uint8 i = 0; i < Shingle::kWidth; ++i) {
     LabelInfo* info = instance->at(i);
-    uint32 kind = 0;
+    uint8 kind = 0;
     int code = -1;
-    size_t j = 0;
+    uint8 j = 0;
     for ( ; j < i; ++j) {
       if (info == instance->at(j)) {  // Duplicate LabelInfo
         kind = kinds_[j];
@@ -889,7 +889,7 @@
 
 
   void AddShingles(size_t begin, size_t end) {
-    for (size_t i = begin;  i + Shingle::kWidth - 1 < end;  ++i) {
+    for (size_t i = begin; i + Shingle::kWidth - 1 < end; ++i) {
       instances_[i] = Shingle::Find(trace_, i, &shingle_instances_);
     }
   }
@@ -942,7 +942,7 @@
 
   // For the positions in |info|, find the shingles that overlap that position.
   void AddAffectedPositions(LabelInfo* info, ShingleSet* affected_shingles) {
-    const size_t kWidth = Shingle::kWidth;
+    const uint8 kWidth = Shingle::kWidth;
     for (size_t i = 0;  i < info->positions_.size();  ++i) {
       size_t position = info->positions_[i];
       // Find bounds to the subrange of |trace_| we are in.
@@ -950,9 +950,8 @@
       size_t end = position < model_end_ ? model_end_ : trace_.size();
 
       // Clip [position-kWidth+1, position+1)
-      size_t low = position > start + kWidth - 1
-          ? position - kWidth + 1
-          : start;
+      size_t low =
+          position > start + kWidth - 1 ? position - kWidth + 1 : start;
       size_t high = position + kWidth < end ? position + 1 : end - kWidth + 1;
 
       for (size_t shingle_position = low;
@@ -1060,7 +1059,7 @@
         // int score = p1;  // ? weigh all equally??
         int score = std::min(p1, m1);
 
-        for (size_t i = 0;  i < Shingle::kWidth;  ++i) {
+        for (uint8 i = 0; i < Shingle::kWidth; ++i) {
           LabelInfo* program_info = program_instance->at(i);
           LabelInfo* model_info = model_instance->at(i);
           if ((model_info->assignment_ == NULL) !=
diff --git a/courgette/assembly_program.cc b/courgette/assembly_program.cc
index bfdf6ef..4718cbf 100644
--- a/courgette/assembly_program.cc
+++ b/courgette/assembly_program.cc
@@ -90,16 +90,16 @@
 // Emits a single byte.
 class BytesInstruction : public Instruction {
  public:
-  BytesInstruction(const uint8* values, uint32 len)
+  BytesInstruction(const uint8* values, size_t len)
       : Instruction(DEFBYTES, 0),
         values_(values),
         len_(len) {}
   const uint8* byte_values() const { return values_; }
-  uint32 len() const { return len_; }
+  size_t len() const { return len_; }
 
  private:
   const uint8* values_;
-  uint32 len_;
+  size_t len_;
 };
 
 // A ABS32 to REL32 instruction emits a reference to a label's address.
@@ -179,7 +179,7 @@
 }
 
 CheckBool AssemblyProgram::EmitBytesInstruction(const uint8* values,
-                                                uint32 len) {
+                                                size_t len) {
   return Emit(new(std::nothrow) BytesInstruction(values, len));
 }
 
@@ -421,7 +421,7 @@
       case DEFBYTES: {
         const uint8* byte_values =
           static_cast<BytesInstruction*>(instruction)->byte_values();
-        uint32 len = static_cast<BytesInstruction*>(instruction)->len();
+        size_t len = static_cast<BytesInstruction*>(instruction)->len();
 
         if (!encoded->AddCopy(len, byte_values))
           return NULL;
@@ -547,19 +547,6 @@
   return true;
 }
 
-void AssemblyProgram::PrintLabelCounts(RVAToLabel* labels) {
-  for (RVAToLabel::const_iterator p = labels->begin(); p != labels->end();
-       ++p) {
-    Label* current = p->second;
-    if (current->index_ != Label::kNoIndex)
-      printf("%d\n", current->count_);
-  }
-}
-
-void AssemblyProgram::CountRel32ARM() {
-  PrintLabelCounts(&rel32_labels_);
-}
-
 ////////////////////////////////////////////////////////////////////////////////
 
 Status TrimLabels(AssemblyProgram* program) {
diff --git a/courgette/assembly_program.h b/courgette/assembly_program.h
index 701b476..4c1cba2 100644
--- a/courgette/assembly_program.h
+++ b/courgette/assembly_program.h
@@ -87,7 +87,7 @@
   CheckBool EmitByteInstruction(uint8 byte) WARN_UNUSED_RESULT;
 
   // Generates multiple bytes of data or machine instructions.
-  CheckBool EmitBytesInstruction(const uint8* value, uint32 len)
+  CheckBool EmitBytesInstruction(const uint8* value, size_t len)
       WARN_UNUSED_RESULT;
 
   // Generates 4-byte relative reference to address of 'label'.
@@ -129,9 +129,6 @@
   // Trim underused labels
   CheckBool TrimLabels();
 
-  void PrintLabelCounts(RVAToLabel* labels);
-  void CountRel32ARM();
-
  private:
   ExecutableType kind_;
 
diff --git a/courgette/disassembler_elf_32.cc b/courgette/disassembler_elf_32.cc
index bfd1ef4..ff2b9e03 100644
--- a/courgette/disassembler_elf_32.cc
+++ b/courgette/disassembler_elf_32.cc
@@ -405,16 +405,12 @@
     size_t start_file_offset,
     size_t end_file_offset,
     AssemblyProgram* program) {
-
-  const uint8* start = OffsetToPointer(start_file_offset);
-  const uint8* end = OffsetToPointer(end_file_offset);
-
   // Callers don't guarantee start < end
-  if (start >= end) return true;
+  if (start_file_offset >= end_file_offset) return true;
 
-  const ptrdiff_t len = end - start;  // Works because vars are byte pointers
+  const size_t len = end_file_offset - start_file_offset;
 
-  if (!program->EmitBytesInstruction(start, len))
+  if (!program->EmitBytesInstruction(OffsetToPointer(start_file_offset), len))
     return false;
 
   return true;
diff --git a/courgette/disassembler_elf_32_arm.cc b/courgette/disassembler_elf_32_arm.cc
index f0f94b9..800a64c 100644
--- a/courgette/disassembler_elf_32_arm.cc
+++ b/courgette/disassembler_elf_32_arm.cc
@@ -38,7 +38,7 @@
       fflush(stdout);
 
       (*addr) = temp;
-      (*c_op) = (arm_op >> 8) | 0x1000;
+      (*c_op) = static_cast<uint16>(arm_op >> 8) | 0x1000;
       break;
     }
     case ARM_OFF11: {
@@ -50,7 +50,7 @@
       temp += 4;  // Offset from _next_ PC.
 
       (*addr) = temp;
-      (*c_op) = (arm_op >> 11) | 0x2000;
+      (*c_op) = static_cast<uint16>(arm_op >> 11) | 0x2000;
       break;
     }
     case ARM_OFF24: {
@@ -101,7 +101,7 @@
       temp2 |= (arm_op & (1 << 15)) >> 13;
       temp2 |= (arm_op & 0xF8000000) >> 24;
       temp2 |= (prefetch & 0x0000000F) << 8;
-      (*c_op) = temp2;
+      (*c_op) = static_cast<uint16>(temp2);
       break;
     }
     case ARM_OFF21: {
@@ -122,7 +122,7 @@
 
       uint32 temp2 = 0x5000;
       temp2 |= (arm_op & 0x03C00000) >> 22;  // just save the cond
-      (*c_op) = temp2;
+      (*c_op) = static_cast<uint16>(temp2);
       break;
     }
     default:
diff --git a/courgette/disassembler_win32_x64.cc b/courgette/disassembler_win32_x64.cc
index 1da5951..04356d7f 100644
--- a/courgette/disassembler_win32_x64.cc
+++ b/courgette/disassembler_win32_x64.cc
@@ -573,7 +573,9 @@
 
     if (abs32_pos != abs32_locations_.end() && *abs32_pos == current_rva) {
       uint32 target_address = Read32LittleEndian(p);
-      RVA target_rva = target_address - image_base();
+      // TODO(wfh): image_base() can be larger than 32 bits, so this math can
+      // underflow.  Figure out the right solution here.
+      RVA target_rva = target_address - static_cast<uint32>(image_base());
       // TODO(sra): target could be Label+offset.  It is not clear how to guess
       // which it might be.  We assume offset==0.
       if (!program->EmitAbs32(program->FindOrMakeAbs32Label(target_rva)))
diff --git a/courgette/encoded_program.cc b/courgette/encoded_program.cc
index b78e7fe..b120246 100644
--- a/courgette/encoded_program.cc
+++ b/courgette/encoded_program.cc
@@ -46,8 +46,6 @@
   size_t count = items.size();
   bool ok = buffer->WriteSizeVarint32(count);
   for (size_t i = 0; ok && i < count;  ++i) {
-    COMPILE_ASSERT(sizeof(items[0]) <= sizeof(uint32),  // NOLINT
-                   T_must_fit_in_uint32);
     ok = buffer->WriteSizeVarint32(items[i]);
   }
   return ok;
@@ -196,7 +194,7 @@
   return ops_.push_back(ORIGIN) && origins_.push_back(origin);
 }
 
-CheckBool EncodedProgram::AddCopy(uint32 count, const void* bytes) {
+CheckBool EncodedProgram::AddCopy(size_t count, const void* bytes) {
   const uint8* source = static_cast<const uint8*>(bytes);
 
   bool ok = true;
@@ -214,7 +212,7 @@
     }
     if (ok && ops_.back() == COPY) {
       copy_counts_.back() += count;
-      for (uint32 i = 0; ok && i < count; ++i) {
+      for (size_t i = 0; ok && i < count; ++i) {
         ok = copy_bytes_.push_back(source[i]);
       }
       return ok;
@@ -226,7 +224,7 @@
       ok = ops_.push_back(COPY1) && copy_bytes_.push_back(source[0]);
     } else {
       ok = ops_.push_back(COPY) && copy_counts_.push_back(count);
-      for (uint32 i = 0; ok && i < count; ++i) {
+      for (size_t i = 0; ok && i < count; ++i) {
         ok = copy_bytes_.push_back(source[i]);
       }
     }
@@ -427,7 +425,7 @@
                                             &decompressed_op)) {
         return false;
       }
-      uint16 op16 = decompressed_op;
+      uint16 op16 = static_cast<uint16>(decompressed_op);
       if (!output->Write(&op16, 2))
         return false;
       current_rva += 2;
@@ -447,7 +445,7 @@
                                             &decompressed_op)) {
         return false;
       }
-      uint16 op16 = decompressed_op;
+      uint16 op16 = static_cast<uint16>(decompressed_op);
       if (!output->Write(&op16, 2))
         return false;
       current_rva += 2;
@@ -556,11 +554,11 @@
       }
 
       case COPY: {
-        uint32 count;
+        size_t count;
         if (!VectorAt(copy_counts_, ix_copy_counts, &count))
           return false;
         ++ix_copy_counts;
-        for (uint32 i = 0;  i < count;  ++i) {
+        for (size_t i = 0;  i < count;  ++i) {
           uint8 b;
           if (!VectorAt(copy_bytes_, ix_copy_bytes, &b))
             return false;
@@ -568,7 +566,7 @@
           if (!output->Write(&b, 1))
             return false;
         }
-        current_rva += count;
+        current_rva += static_cast<RVA>(count);
         break;
       }
 
diff --git a/courgette/encoded_program.h b/courgette/encoded_program.h
index a370e3a..a9208adef 100644
--- a/courgette/encoded_program.h
+++ b/courgette/encoded_program.h
@@ -41,7 +41,7 @@
   // NOTE: If any of these methods ever fail, the EncodedProgram instance
   // has failed and should be discarded.
   CheckBool AddOrigin(RVA rva) WARN_UNUSED_RESULT;
-  CheckBool AddCopy(uint32 count, const void* bytes) WARN_UNUSED_RESULT;
+  CheckBool AddCopy(size_t count, const void* bytes) WARN_UNUSED_RESULT;
   CheckBool AddRel32(int label_index) WARN_UNUSED_RESULT;
   CheckBool AddRel32ARM(uint16 op, int label_index) WARN_UNUSED_RESULT;
   CheckBool AddAbs32(int label_index) WARN_UNUSED_RESULT;
@@ -87,6 +87,7 @@
   };
 
   typedef NoThrowBuffer<RVA> RvaVector;
+  typedef NoThrowBuffer<size_t> SizeTVector;
   typedef NoThrowBuffer<uint32> UInt32Vector;
   typedef NoThrowBuffer<uint8> UInt8Vector;
   typedef NoThrowBuffer<OP> OPVector;
@@ -109,7 +110,7 @@
   RvaVector abs32_rva_;
   OPVector ops_;
   RvaVector origins_;
-  UInt32Vector copy_counts_;
+  SizeTVector copy_counts_;
   UInt8Vector copy_bytes_;
   UInt32Vector rel32_ix_;
   UInt32Vector abs32_ix_;
diff --git a/courgette/streams.cc b/courgette/streams.cc
index df769b1..fcac593 100644
--- a/courgette/streams.cc
+++ b/courgette/streams.cc
@@ -115,10 +115,10 @@
 // have the high bit set to indicate more digits.
 inline uint8* Varint::Encode32(uint8* destination, uint32 value) {
   while (value >= 128) {
-    *(destination++) = value | 128;
+    *(destination++) = static_cast<uint8>(value) | 128;
     value = value >> 7;
   }
-  *(destination++) = value;
+  *(destination++) = static_cast<uint8>(value);
   return destination;
 }