Fix more disabled MSVC warnings, base/ edition.
Mostly this fixes cases of "possible value truncation", usually by inserting
explicit typecasts.
BUG=81439
TEST=none
Review URL: https://codereview.chromium.org/596103002
Cr-Commit-Position: refs/heads/master@{#297723}
CrOS-Libchrome-Original-Commit: 9cf9b94a63dec7653bb48bac948deb4008b532c8
diff --git a/base/debug/crash_logging.cc b/base/debug/crash_logging.cc
index caf10b4..f9b4449 100644
--- a/base/debug/crash_logging.cc
+++ b/base/debug/crash_logging.cc
@@ -36,7 +36,8 @@
// For a given |length|, computes the number of chunks a value of that size
// will occupy.
size_t NumChunksForLength(size_t length) {
- return std::ceil(length / static_cast<float>(g_chunk_max_length_));
+ // Compute (length / g_chunk_max_length_), rounded up.
+ return (length + g_chunk_max_length_ - 1) / g_chunk_max_length_;
}
// The longest max_length allowed by the system.
diff --git a/base/debug/trace_event_impl.cc b/base/debug/trace_event_impl.cc
index db321e2..9110bf2 100644
--- a/base/debug/trace_event_impl.cc
+++ b/base/debug/trace_event_impl.cc
@@ -1319,8 +1319,8 @@
char* duration_end;
double target_duration = strtod(token.c_str(), &duration_end);
if (duration_end != token.c_str()) {
- delay->SetTargetDuration(
- TimeDelta::FromMicroseconds(target_duration * 1e6));
+ delay->SetTargetDuration(TimeDelta::FromMicroseconds(
+ static_cast<int64>(target_duration * 1e6)));
} else if (token == "static") {
delay->SetMode(TraceEventSyntheticDelay::STATIC);
} else if (token == "oneshot") {
diff --git a/base/debug/trace_event_synthetic_delay_unittest.cc b/base/debug/trace_event_synthetic_delay_unittest.cc
index 7833e7b..a418eed 100644
--- a/base/debug/trace_event_synthetic_delay_unittest.cc
+++ b/base/debug/trace_event_synthetic_delay_unittest.cc
@@ -41,19 +41,19 @@
void AdvanceTime(base::TimeDelta delta) { now_ += delta; }
- int TestFunction() {
+ int64 TestFunction() {
base::TimeTicks start = Now();
{ TRACE_EVENT_SYNTHETIC_DELAY("test.Delay"); }
return (Now() - start).InMilliseconds();
}
- int AsyncTestFunctionBegin() {
+ int64 AsyncTestFunctionBegin() {
base::TimeTicks start = Now();
{ TRACE_EVENT_SYNTHETIC_DELAY_BEGIN("test.AsyncDelay"); }
return (Now() - start).InMilliseconds();
}
- int AsyncTestFunctionEnd() {
+ int64 AsyncTestFunctionEnd() {
base::TimeTicks start = Now();
{ TRACE_EVENT_SYNTHETIC_DELAY_END("test.AsyncDelay"); }
return (Now() - start).InMilliseconds();
diff --git a/base/files/file_path_unittest.cc b/base/files/file_path_unittest.cc
index fa7627c..8b6f3a8 100644
--- a/base/files/file_path_unittest.cc
+++ b/base/files/file_path_unittest.cc
@@ -770,16 +770,16 @@
FilePath path(cases[i].input);
FilePath::StringType extension = path.Extension();
FilePath::StringType final_extension = path.FinalExtension();
- EXPECT_STREQ(cases[i].expected, extension.c_str()) << "i: " << i <<
- ", path: " << path.value();
- EXPECT_STREQ(cases[i].expected, final_extension.c_str()) << "i: " << i <<
- ", path: " << path.value();
+ EXPECT_STREQ(cases[i].expected, extension.c_str())
+ << "i: " << i << ", path: " << path.value();
+ EXPECT_STREQ(cases[i].expected, final_extension.c_str())
+ << "i: " << i << ", path: " << path.value();
}
for (unsigned int i = 0; i < arraysize(double_extension_cases); ++i) {
- FilePath path(cases[i].input);
+ FilePath path(double_extension_cases[i].input);
FilePath::StringType extension = path.Extension();
- EXPECT_STREQ(cases[i].expected, extension.c_str()) << "i: " << i <<
- ", path: " << path.value();
+ EXPECT_STREQ(double_extension_cases[i].expected, extension.c_str())
+ << "i: " << i << ", path: " << path.value();
}
}
diff --git a/base/files/file_util.cc b/base/files/file_util.cc
index 96a7164..17b5969 100644
--- a/base/files/file_util.cc
+++ b/base/files/file_util.cc
@@ -75,7 +75,7 @@
if ((file1.eof() != file2.eof()) ||
(file1.gcount() != file2.gcount()) ||
- (memcmp(buffer1, buffer2, file1.gcount()))) {
+ (memcmp(buffer1, buffer2, static_cast<size_t>(file1.gcount())))) {
file1.close();
file2.close();
return false;
diff --git a/base/json/json_parser.cc b/base/json/json_parser.cc
index f1f4333..32e55e8 100644
--- a/base/json/json_parser.cc
+++ b/base/json/json_parser.cc
@@ -652,7 +652,7 @@
NextChar();
if (hex_digit < kExtendedASCIIStart)
- string.Append(hex_digit);
+ string.Append(static_cast<char>(hex_digit));
else
DecodeUTF8(hex_digit, &string);
break;
@@ -714,7 +714,7 @@
return true;
} else {
if (next_char < kExtendedASCIIStart)
- string.Append(next_char);
+ string.Append(static_cast<char>(next_char));
else
DecodeUTF8(next_char, &string);
}
@@ -791,7 +791,7 @@
// Anything outside of the basic ASCII plane will need to be decoded from
// int32 to a multi-byte sequence.
if (point < kExtendedASCIIStart) {
- dest->Append(point);
+ dest->Append(static_cast<char>(point));
} else {
char utf8_units[4] = { 0 };
int offset = 0;
diff --git a/base/logging.cc b/base/logging.cc
index 8f75aa0..6593d2c 100644
--- a/base/logging.cc
+++ b/base/logging.cc
@@ -687,7 +687,7 @@
stream_ << ":" << filename << "(" << line << ")] ";
- message_start_ = stream_.tellp();
+ message_start_ = stream_.str().length();
}
#if defined(OS_WIN)
diff --git a/base/logging.h b/base/logging.h
index 7435864..4661f0f 100644
--- a/base/logging.h
+++ b/base/logging.h
@@ -618,12 +618,12 @@
// variable warnings if the only use of a variable is in a DCHECK.
// This behavior is different from DLOG_IF et al.
-#define DCHECK(condition) \
- LAZY_STREAM(LOG_STREAM(DCHECK), DCHECK_IS_ON && !(condition)) \
+#define DCHECK(condition) \
+ LAZY_STREAM(LOG_STREAM(DCHECK), DCHECK_IS_ON ? !(condition) : false) \
<< "Check failed: " #condition ". "
-#define DPCHECK(condition) \
- LAZY_STREAM(PLOG_STREAM(DCHECK), DCHECK_IS_ON && !(condition)) \
+#define DPCHECK(condition) \
+ LAZY_STREAM(PLOG_STREAM(DCHECK), DCHECK_IS_ON ? !(condition) : false) \
<< "Check failed: " #condition ". "
// Helper macro for binary operators.
diff --git a/base/metrics/field_trial_unittest.cc b/base/metrics/field_trial_unittest.cc
index a6dee11..52ce855 100644
--- a/base/metrics/field_trial_unittest.cc
+++ b/base/metrics/field_trial_unittest.cc
@@ -114,10 +114,11 @@
char default_always_false[] = " default always false";
for (int i = 1; i < 250; ++i) {
// Try lots of names, by changing the first character of the name.
- always_true[0] = i;
- default_always_true[0] = i;
- always_false[0] = i;
- default_always_false[0] = i;
+ char c = static_cast<char>(i);
+ always_true[0] = c;
+ default_always_true[0] = c;
+ always_false[0] = c;
+ default_always_false[0] = c;
scoped_refptr<FieldTrial> trial_true =
CreateFieldTrial(always_true, 10, default_always_true, NULL);
@@ -190,8 +191,9 @@
bool false_event_seen = false;
bool true_event_seen = false;
for (int i = 1; i < 250; ++i) {
- name[0] = i;
- default_group_name[0] = i;
+ char c = static_cast<char>(i);
+ name[0] = c;
+ default_group_name[0] = c;
scoped_refptr<FieldTrial> trial =
CreateFieldTrial(name, 10, default_group_name, NULL);
int might_win = trial->AppendGroup("MightWin", 5);
diff --git a/base/metrics/histogram.cc b/base/metrics/histogram.cc
index 6e30892..6bab0eb 100644
--- a/base/metrics/histogram.cc
+++ b/base/metrics/histogram.cc
@@ -128,8 +128,9 @@
TimeDelta maximum,
size_t bucket_count,
int32 flags) {
- return FactoryGet(name, minimum.InMilliseconds(), maximum.InMilliseconds(),
- bucket_count, flags);
+ return FactoryGet(name, static_cast<Sample>(minimum.InMilliseconds()),
+ static_cast<Sample>(maximum.InMilliseconds()), bucket_count,
+ flags);
}
// Calculate what range of values are held in each bucket.
@@ -528,8 +529,9 @@
TimeDelta maximum,
size_t bucket_count,
int32 flags) {
- return FactoryGet(name, minimum.InMilliseconds(), maximum.InMilliseconds(),
- bucket_count, flags);
+ return FactoryGet(name, static_cast<Sample>(minimum.InMilliseconds()),
+ static_cast<Sample>(maximum.InMilliseconds()), bucket_count,
+ flags);
}
HistogramBase* LinearHistogram::FactoryGetWithRangeDescription(
diff --git a/base/metrics/histogram_base.cc b/base/metrics/histogram_base.cc
index 6e7e69e..f09c84e 100644
--- a/base/metrics/histogram_base.cc
+++ b/base/metrics/histogram_base.cc
@@ -109,7 +109,7 @@
DictionaryValue root;
root.SetString("name", histogram_name());
root.SetInteger("count", count);
- root.SetDouble("sum", sum);
+ root.SetDouble("sum", static_cast<double>(sum));
root.SetInteger("flags", flags());
root.Set("params", parameters.release());
root.Set("buckets", buckets.release());
diff --git a/base/numerics/safe_math.h b/base/numerics/safe_math.h
index 51a534f..b3694fe 100644
--- a/base/numerics/safe_math.h
+++ b/base/numerics/safe_math.h
@@ -205,9 +205,10 @@
lhs.ValueUnsafe() OP rhs.ValueUnsafe(), \
GetRangeConstraint(rhs.validity() | lhs.validity())); \
RangeConstraint validity = RANGE_VALID; \
- T result = Checked##NAME(static_cast<Promotion>(lhs.ValueUnsafe()), \
- static_cast<Promotion>(rhs.ValueUnsafe()), \
- &validity); \
+ T result = static_cast<T>(Checked##NAME( \
+ static_cast<Promotion>(lhs.ValueUnsafe()), \
+ static_cast<Promotion>(rhs.ValueUnsafe()), \
+ &validity)); \
return CheckedNumeric<Promotion>( \
result, \
GetRangeConstraint(validity | lhs.validity() | rhs.validity())); \
diff --git a/base/numerics/safe_math_impl.h b/base/numerics/safe_math_impl.h
index 3b5e64d..34e2bf5 100644
--- a/base/numerics/safe_math_impl.h
+++ b/base/numerics/safe_math_impl.h
@@ -277,7 +277,7 @@
CheckedAbs(T value, RangeConstraint* validity) {
*validity =
value != std::numeric_limits<T>::min() ? RANGE_VALID : RANGE_OVERFLOW;
- return std::abs(value);
+ return static_cast<T>(std::abs(value));
}
template <typename T>
@@ -359,7 +359,7 @@
template <typename Src>
CheckedNumericState(Src value, RangeConstraint validity)
- : value_(value),
+ : value_(static_cast<T>(value)),
validity_(GetRangeConstraint(validity |
DstRangeRelationToSrcRange<T>(value))) {
COMPILE_ASSERT(std::numeric_limits<Src>::is_specialized,
diff --git a/base/strings/string_number_conversions.cc b/base/strings/string_number_conversions.cc
index d6bd5c4..642d24e 100644
--- a/base/strings/string_number_conversions.cc
+++ b/base/strings/string_number_conversions.cc
@@ -103,7 +103,7 @@
public:
static bool Convert(CHAR c, uint8* digit) {
if (c >= '0' && c < '0' + BASE) {
- *digit = c - '0';
+ *digit = static_cast<uint8>(c - '0');
return true;
}
return false;
diff --git a/base/strings/string_util.h b/base/strings/string_util.h
index 0ec94d3..c3464e6 100644
--- a/base/strings/string_util.h
+++ b/base/strings/string_util.h
@@ -358,14 +358,14 @@
}
template <typename Char>
-inline Char HexDigitToInt(Char c) {
+inline char HexDigitToInt(Char c) {
DCHECK(IsHexDigit(c));
if (c >= '0' && c <= '9')
- return c - '0';
+ return static_cast<char>(c - '0');
if (c >= 'A' && c <= 'F')
- return c - 'A' + 10;
+ return static_cast<char>(c - 'A' + 10);
if (c >= 'a' && c <= 'f')
- return c - 'a' + 10;
+ return static_cast<char>(c - 'a' + 10);
return 0;
}
diff --git a/base/strings/utf_string_conversion_utils.cc b/base/strings/utf_string_conversion_utils.cc
index 09a003d..022c0df 100644
--- a/base/strings/utf_string_conversion_utils.cc
+++ b/base/strings/utf_string_conversion_utils.cc
@@ -71,7 +71,7 @@
size_t WriteUnicodeCharacter(uint32 code_point, std::string* output) {
if (code_point <= 0x7f) {
// Fast path the common case of one byte.
- output->push_back(code_point);
+ output->push_back(static_cast<char>(code_point));
return 1;
}
diff --git a/base/test/gtest_xml_util.cc b/base/test/gtest_xml_util.cc
index 8a153dc..7a5ba8a 100644
--- a/base/test/gtest_xml_util.cc
+++ b/base/test/gtest_xml_util.cc
@@ -178,9 +178,9 @@
std::string test_time_str;
if (!xml_reader.NodeAttribute("time", &test_time_str))
return false;
- result.elapsed_time =
- TimeDelta::FromMicroseconds(strtod(test_time_str.c_str(), NULL) *
- Time::kMicrosecondsPerSecond);
+ result.elapsed_time = TimeDelta::FromMicroseconds(
+ static_cast<int64>(strtod(test_time_str.c_str(), NULL) *
+ Time::kMicrosecondsPerSecond));
result.status = TestResult::TEST_SUCCESS;
diff --git a/base/test/launcher/test_results_tracker.cc b/base/test/launcher/test_results_tracker.cc
index 1e3b2b0..b553fd6 100644
--- a/base/test/launcher/test_results_tracker.cc
+++ b/base/test/launcher/test_results_tracker.cc
@@ -288,7 +288,8 @@
test_result_value->SetString("status", test_result.StatusAsString());
test_result_value->SetInteger(
- "elapsed_time_ms", test_result.elapsed_time.InMilliseconds());
+ "elapsed_time_ms",
+ static_cast<int>(test_result.elapsed_time.InMilliseconds()));
// There are no guarantees about character encoding of the output
// snippet. Escape it and record whether it was losless.
diff --git a/base/test/trace_event_analyzer_unittest.cc b/base/test/trace_event_analyzer_unittest.cc
index a17c7e5..5604508 100644
--- a/base/test/trace_event_analyzer_unittest.cc
+++ b/base/test/trace_event_analyzer_unittest.cc
@@ -399,7 +399,7 @@
const base::TimeDelta kSleepTime = base::TimeDelta::FromMilliseconds(200);
// We will search for events that have a duration of greater than 90% of the
// sleep time, so that there is no flakiness.
- int duration_cutoff_us = (kSleepTime.InMicroseconds() * 9) / 10;
+ int64 duration_cutoff_us = (kSleepTime.InMicroseconds() * 9) / 10;
BeginTracing();
{
@@ -427,7 +427,8 @@
TraceEventVector found;
analyzer->FindEvents(
Query::MatchBeginWithEnd() &&
- Query::EventDuration() > Query::Int(duration_cutoff_us) &&
+ Query::EventDuration() >
+ Query::Int(static_cast<int>(duration_cutoff_us)) &&
(Query::EventCategory() == Query::String("cat1") ||
Query::EventCategory() == Query::String("cat2") ||
Query::EventCategory() == Query::String("cat3")),
@@ -444,7 +445,7 @@
const base::TimeDelta kSleepTime = base::TimeDelta::FromMilliseconds(200);
// We will search for events that have a duration of greater than 90% of the
// sleep time, so that there is no flakiness.
- int duration_cutoff_us = (kSleepTime.InMicroseconds() * 9) / 10;
+ int64 duration_cutoff_us = (kSleepTime.InMicroseconds() * 9) / 10;
BeginTracing();
{
@@ -467,7 +468,8 @@
TraceEventVector found;
analyzer->FindEvents(
- Query::EventCompleteDuration() > Query::Int(duration_cutoff_us) &&
+ Query::EventCompleteDuration() >
+ Query::Int(static_cast<int>(duration_cutoff_us)) &&
(Query::EventCategory() == Query::String("cat1") ||
Query::EventCategory() == Query::String("cat2") ||
Query::EventCategory() == Query::String("cat3")),
diff --git a/base/time/time.h b/base/time/time.h
index d6f6f52..3cf3746 100644
--- a/base/time/time.h
+++ b/base/time/time.h
@@ -538,7 +538,7 @@
// Preserve max to prevent overflow.
if (secs == std::numeric_limits<double>::infinity())
return Max();
- return TimeDelta(secs * Time::kMicrosecondsPerSecond);
+ return TimeDelta(static_cast<int64>(secs * Time::kMicrosecondsPerSecond));
}
// static
@@ -546,7 +546,7 @@
// Preserve max to prevent overflow.
if (ms == std::numeric_limits<double>::infinity())
return Max();
- return TimeDelta(ms * Time::kMicrosecondsPerMillisecond);
+ return TimeDelta(static_cast<int64>(ms * Time::kMicrosecondsPerMillisecond));
}
// static