Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix more Wconversion warnings #125

Merged
merged 2 commits into from
Oct 10, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 7 additions & 7 deletions src/groups/mwc/mwcst/mwcst_basictableinfoprovider.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -178,14 +178,14 @@ int BasicTableInfoProvider_ValueSizeVisitor::operator()(
if (value == bsl::numeric_limits<bsls::Types::Int64>::max() ||
value == bsl::numeric_limits<bsls::Types::Int64>::min()) {
if (!d_fmt_p->d_extremeString.isNull()) {
return d_fmt_p->d_extremeString.value().length();
return static_cast<int>(d_fmt_p->d_extremeString.value().length());
}
else {
return NA_STRING.length();
return static_cast<int>(NA_STRING.length());
}
}
else if (!d_fmt_p->d_zeroString.isNull() && value == 0) {
return d_fmt_p->d_zeroString.value().length();
return static_cast<int>(d_fmt_p->d_zeroString.value().length());
}
else if (d_fmt_p->d_type == ColumnFormat::DMCU_NS_TIME_INTERVAL) {
return mwcstu::PrintUtil::printedTimeIntervalNsLength(
Expand All @@ -212,13 +212,13 @@ int BasicTableInfoProvider_ValueSizeVisitor::operator()(int value) const
int BasicTableInfoProvider_ValueSizeVisitor::operator()(
const bslstl::StringRef& value) const
{
return value.length();
return static_cast<int>(value.length());
}

int BasicTableInfoProvider_ValueSizeVisitor::operator()(double value) const
{
if (!d_fmt_p->d_zeroString.isNull() && value == 0.0) {
return d_fmt_p->d_zeroString.value().length();
return static_cast<int>(d_fmt_p->d_zeroString.value().length());
}
else if (d_fmt_p->d_printSeparators) {
return mwcstu::PrintUtil::printedValueLengthWithSeparator(
Expand Down Expand Up @@ -485,10 +485,10 @@ bsl::ostream& BasicTableInfoProvider::printValue(bsl::ostream& stream,
int BasicTableInfoProvider::getHeaderSize(int level, int column) const
{
if (level == 0) {
return d_columns[column].d_printColumnName.length();
return static_cast<int>(d_columns[column].d_printColumnName.length());
}
else {
return d_columnGroups[column].length();
return static_cast<int>(d_columnGroups[column].length());
}
}

Expand Down
16 changes: 10 additions & 6 deletions src/groups/mwc/mwcst/mwcst_printutil.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -76,8 +76,9 @@ const char* timeIntervalNsHelper(bsls::Types::Int64* num,
}

*num = ns / div;
*remainder = (int)bsl::floor(((double)(ns - *num * div) / div) *
bsl::pow(10.0, precision));
*remainder = (int)bsl::floor(
((double)(ns - *num * div) / static_cast<double>(div)) *
bsl::pow(10.0, precision));
return TIME_INTERVAL_NS_UNITS[level];
}

Expand Down Expand Up @@ -116,8 +117,9 @@ const char* memoryHelper(bsls::Types::Int64* num,

bsls::Types::Int64 div = (((bsls::Types::Int64)1) << shift);
*num = bytes >> shift;
*remainder = (int)bsl::floor((((double)(bytes - (*num << shift)) / div) *
bsl::pow(10.0, precision)));
*remainder = (int)bsl::floor(
(((double)(bytes - (*num << shift)) / static_cast<double>(div)) *
bsl::pow(10.0, precision)));

if (negative) {
*num = -*num;
Expand Down Expand Up @@ -322,7 +324,8 @@ int PrintUtil::printedMemoryLength(bsls::Types::Int64 bytes, int precision)
int remainder = 0;
const char* unit = memoryHelper(&value, &remainder, bytes, precision);

int ret = printedValueLength(value) + bsl::strlen(unit) + 1;
int ret = printedValueLength(value) + static_cast<int>(bsl::strlen(unit)) +
1;
if (unit != MEMORY_UNITS[0] && precision > 0) {
ret += 1 + precision;
}
Expand Down Expand Up @@ -367,7 +370,8 @@ int PrintUtil::printedTimeIntervalNsLength(bsls::Types::Int64 timeIntervalNs,
const char* unit =
timeIntervalNsHelper(&value, &remainder, timeIntervalNs, precision);

int ret = printedValueLength(value) + bsl::strlen(unit) + 1;
int ret = printedValueLength(value) + static_cast<int>(bsl::strlen(unit)) +
1;
if (unit != TIME_INTERVAL_NS_UNITS[0]) {
ret += 1 + precision;
}
Expand Down
14 changes: 7 additions & 7 deletions src/groups/mwc/mwcst/mwcst_statcontexttableinfoprovider.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -74,20 +74,20 @@ class DefaultIdColumn : public StatContextTableInfoProviderCustomColumn {
int length = 0;
if (valueType == StatContext::DMCST_TOTAL_VALUE) {
if (context.hasName()) {
length = context.name().length();
length = static_cast<int>(context.name().length());
}
else {
length = mwcstu::PrintUtil::printedValueLength(context.id());
}
}
else if (valueType == StatContext::DMCST_DIRECT_VALUE) {
length = DIRECT_NAME.length();
length = static_cast<int>(DIRECT_NAME.length());
}
else if (valueType == StatContext::DMCST_EXPIRED_VALUE) {
length = EXPIRED_NAME.length();
length = static_cast<int>(EXPIRED_NAME.length());
}
else {
length = UNKNOWN_NAME.length();
length = static_cast<int>(UNKNOWN_NAME.length());
}

length += 2 * level;
Expand Down Expand Up @@ -472,7 +472,7 @@ int StatContextTableInfoProvider::getValueSize(int row, int column) const
if (colInfo.d_intFunc) {
bsls::Types::Int64 funcValue = colInfo.d_intFunc(value);
if (funcValue == MAX_INT || funcValue == MIN_INT) {
return NA_STRING.length();
return static_cast<int>(NA_STRING.length());
}
else if (colInfo.d_printType == DMCST_INT_VALUE) {
if (d_printSeparators) {
Expand Down Expand Up @@ -555,10 +555,10 @@ bsl::ostream& StatContextTableInfoProvider::printValue(bsl::ostream& stream,
int StatContextTableInfoProvider::getHeaderSize(int level, int column) const
{
if (level == 0) {
return d_columns[column].d_name.length();
return static_cast<int>(d_columns[column].d_name.length());
}
else {
return d_columnGroups[column].length();
return static_cast<int>(d_columnGroups[column].length());
}
}

Expand Down
32 changes: 18 additions & 14 deletions src/groups/mwc/mwcst/mwcst_statutil.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -70,8 +70,9 @@ StatUtil::incrementRate(const StatValue& value,
const StatValue::SnapshotLocation& firstSnapshot,
const StatValue::SnapshotLocation& secondSnapshot)
{
double diff = value.snapshot(secondSnapshot).increments() -
value.snapshot(firstSnapshot).increments();
double diff = static_cast<double>(
value.snapshot(secondSnapshot).increments() -
value.snapshot(firstSnapshot).increments());

double divisor = firstSnapshot.index() - secondSnapshot.index();
for (int i = 0; i < firstSnapshot.level(); ++i) {
Expand All @@ -86,8 +87,9 @@ double StatUtil::incrementsPerSecond(
const StatValue::SnapshotLocation& firstSnapshot,
const StatValue::SnapshotLocation& secondSnapshot)
{
double diff = value.snapshot(secondSnapshot).increments() -
value.snapshot(firstSnapshot).increments();
double diff = static_cast<double>(
value.snapshot(secondSnapshot).increments() -
value.snapshot(firstSnapshot).increments());

bsls::Types::Int64 duration =
value.snapshot(secondSnapshot).snapshotTime() -
Expand Down Expand Up @@ -118,8 +120,9 @@ StatUtil::decrementRate(const StatValue& value,
const StatValue::SnapshotLocation& firstSnapshot,
const StatValue::SnapshotLocation& secondSnapshot)
{
double diff = value.snapshot(secondSnapshot).decrements() -
value.snapshot(firstSnapshot).decrements();
double diff = static_cast<double>(
value.snapshot(secondSnapshot).decrements() -
value.snapshot(firstSnapshot).decrements());

double divisor = firstSnapshot.index() - secondSnapshot.index();
for (int i = 0; i < firstSnapshot.level(); ++i) {
Expand All @@ -134,8 +137,9 @@ double StatUtil::decrementsPerSecond(
const StatValue::SnapshotLocation& firstSnapshot,
const StatValue::SnapshotLocation& secondSnapshot)
{
double diff = value.snapshot(secondSnapshot).decrements() -
value.snapshot(firstSnapshot).decrements();
double diff = static_cast<double>(
value.snapshot(secondSnapshot).decrements() -
value.snapshot(firstSnapshot).decrements());

bsls::Types::Int64 duration =
value.snapshot(secondSnapshot).snapshotTime() -
Expand Down Expand Up @@ -164,15 +168,15 @@ double StatUtil::average(const StatValue& value,
divisor *= value.historySize(i);
}

return 1.0 * sum / divisor;
return static_cast<double>(sum) / divisor;
}

double StatUtil::rate(const StatValue& value,
const StatValue::SnapshotLocation& firstSnapshot,
const StatValue::SnapshotLocation& secondSnapshot)
{
double diff = value.snapshot(secondSnapshot).value() -
value.snapshot(firstSnapshot).value();
double diff = static_cast<double>(value.snapshot(secondSnapshot).value() -
value.snapshot(firstSnapshot).value());

int divisor = firstSnapshot.index() - secondSnapshot.index();
for (int i = 0; i < firstSnapshot.level(); ++i) {
Expand All @@ -187,8 +191,8 @@ StatUtil::ratePerSecond(const StatValue& value,
const StatValue::SnapshotLocation& firstSnapshot,
const StatValue::SnapshotLocation& secondSnapshot)
{
double diff = value.snapshot(secondSnapshot).value() -
value.snapshot(firstSnapshot).value();
double diff = static_cast<double>(value.snapshot(secondSnapshot).value() -
value.snapshot(firstSnapshot).value());

bsls::Types::Int64 duration =
value.snapshot(secondSnapshot).snapshotTime() -
Expand Down Expand Up @@ -414,7 +418,7 @@ double StatUtil::averagePerEventReal(
bsls::Types::Int64 sum = sumDifference(value,
firstSnapshot,
secondSnapshot);
return static_cast<double>(sum) / events;
return static_cast<double>(sum) / static_cast<double>(events);
}
}

Expand Down
5 changes: 4 additions & 1 deletion src/groups/mwc/mwcst/mwcst_tableutil.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -140,7 +140,10 @@ int TableUtil::printTable(bsl::ostream& stream, const TableInfoProvider& info)
}

stream << bsl::setw(columnWidths[level][i]);
info.printHeader(stream, level, i, columnWidths[level][i]);
info.printHeader(stream,
level,
static_cast<int>(i),
columnWidths[level][i]);
}
}

Expand Down
4 changes: 2 additions & 2 deletions src/groups/mwc/mwcst/mwcst_testtableinfoprovider.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,7 @@ int TestTableInfoProvider::numHeaderLevels() const

int TestTableInfoProvider::getValueSize(int row, int column) const
{
return d_table[row][column].length();
return static_cast<int>(d_table[row][column].length());
}

bsl::ostream& TestTableInfoProvider::printValue(bsl::ostream& stream,
Expand All @@ -97,7 +97,7 @@ int TestTableInfoProvider::getHeaderSize(int level, int column) const
}

if (column == 0) {
return row[i].length();
return static_cast<int>(row[i].length());
}
else {
--column;
Expand Down
3 changes: 2 additions & 1 deletion src/groups/mwc/mwcst/mwcst_testutil.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -114,7 +114,8 @@ void TestUtil::hexToString(bsl::string* output, const bslstl::StringRef& input)
const char* q = bsl::lower_bound(HEX_DIGITS, HEX_DIGITS + 16, b);
BSLS_ASSERT_OPT(*q == b && "Bad hex input");

output->push_back(((p - HEX_DIGITS) << 4) | (q - HEX_DIGITS));
output->push_back(static_cast<char>((p - HEX_DIGITS) << 4) |
static_cast<char>(q - HEX_DIGITS));
}
}

Expand Down
2 changes: 1 addition & 1 deletion src/groups/mwc/mwcst/mwcst_value.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -114,7 +114,7 @@ size_t Value::hash() const
case VALUE_STRINGTYPE: {
d_hash = bdlb::HashUtil::hash1(
d_value.the<bslstl::StringRef>().data(),
d_value.the<bslstl::StringRef>().length());
static_cast<int>(d_value.the<bslstl::StringRef>().length()));
} break;
default: {
d_hash = (size_t)-1;
Expand Down
2 changes: 1 addition & 1 deletion src/groups/mwc/mwctst/mwctst_testhelper.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ void TestHelper::printTestName(bslstl::StringRef value)
}

bsl::cout << "\n" << value << "\n";
int length = value.length();
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Currently long unsigned int is identical to size_t, but for the future it's more safe to use size_t here, as length() returns. The reason is that size_t has an explicit contract in its definition size_t can store the maximum size of a theoretically possible object of any type (including array).

https://en.cppreference.com/w/c/types/size_t

There are size_t used in almost all similar cases in the codebase.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Addressed, thanks!

size_t length = value.length();
while (length--) {
bsl::cout << "=";
}
Expand Down
4 changes: 2 additions & 2 deletions src/groups/mwc/mwcu/mwcu_alignedprinter.h
Original file line number Diff line number Diff line change
Expand Up @@ -123,9 +123,9 @@ inline AlignedPrinter::AlignedPrinter(bsl::ostream& stream,
BSLS_ASSERT_SAFE(0 <= d_indent);
BSLS_ASSERT_SAFE(0 < d_fields_p->size());

int maxLen = bsl::strlen((*d_fields_p)[0]);
int maxLen = static_cast<int>(bsl::strlen((*d_fields_p)[0]));
for (unsigned int i = 1; i < d_fields_p->size(); ++i) {
int len = bsl::strlen((*d_fields_p)[i]);
int len = static_cast<int>(bsl::strlen((*d_fields_p)[i]));
if (maxLen < len) {
maxLen = len;
}
Expand Down
2 changes: 1 addition & 1 deletion src/groups/mwc/mwcu/mwcu_blob.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -522,7 +522,7 @@ int BlobUtil::readUpToNBytes(char* buf,
cursor.setByte(0);
}

return (outPosition - buf);
return static_cast<int>(outPosition - buf);
}

int BlobUtil::readNBytes(char* buf,
Expand Down
8 changes: 4 additions & 4 deletions src/groups/mwc/mwcu/mwcu_stringutil.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -107,8 +107,8 @@ bool StringUtil::endsWith(const bslstl::StringRef& str,
return false; // RETURN
}

int i = str.length() - 1;
int j = suffix.length() - 1;
int i = static_cast<int>(str.length() - 1);
int j = static_cast<int>(suffix.length() - 1);
while ((i >= 0) && (j >= 0)) {
if (str[i--] != suffix[j--]) {
return false; // RETURN
Expand Down Expand Up @@ -184,8 +184,8 @@ bool StringUtil::match(const bslstl::StringRef& str,
// This implementation is taken almost exactly from the blog post:
// https://research.swtch.com/glob

const int strLength = str.size();
const int patternLength = pattern.size();
const int strLength = static_cast<int>(str.size());
const int patternLength = static_cast<int>(pattern.size());

int i = 0; // index into 'str'
int j = 0; // index into 'pattern'
Expand Down
2 changes: 1 addition & 1 deletion src/groups/mwc/mwcu/mwcu_tlsbool.h
Original file line number Diff line number Diff line change
Expand Up @@ -186,7 +186,7 @@ inline void TLSBool::setFlag(int index, bool value)
d_flags = d_flags | (1 << index);
}
else {
d_flags = d_flags & ~(1 << index);
d_flags = d_flags & static_cast<char>(~(1 << index));
}
}

Expand Down