Skip to content

Commit 86f73d8

Browse files
committed
Explicit casts to avoid conversion warnings
Visual Studio warns about signed/unsigned conversion and limited range of types.
1 parent 5db89ef commit 86f73d8

22 files changed

+46
-34
lines changed

src/ansi-c/expr2c.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2233,7 +2233,7 @@ std::string expr2ct::convert_array(const exprt &src)
22332233
if(last_was_hex)
22342234
{
22352235
// we use "string splicing" to avoid ambiguity
2236-
if(isxdigit(ch))
2236+
if(isxdigit(static_cast<int>(ch)))
22372237
dest+="\" \"";
22382238

22392239
last_was_hex=false;

src/ansi-c/literals/convert_string_literal.cpp

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@ std::basic_string<char32_t> convert_one_string_literal(const std::string &src)
3636
// pad into wide string
3737
value.resize(utf8_value.size());
3838
for(std::size_t i=0; i<utf8_value.size(); i++)
39-
value[i]=utf8_value[i];
39+
value[i] = static_cast<unsigned int>(utf8_value[i]);
4040

4141
return value;
4242
}
@@ -59,7 +59,7 @@ std::basic_string<char32_t> convert_one_string_literal(const std::string &src)
5959
std::basic_string<char32_t> value;
6060
value.resize(char_value.size());
6161
for(std::size_t i=0; i<char_value.size(); i++)
62-
value[i]=char_value[i];
62+
value[i] = static_cast<unsigned int>(char_value[i]);
6363

6464
return value;
6565
}
@@ -143,7 +143,7 @@ exprt convert_string_literal(const std::string &src)
143143
{
144144
// Loss of data here if value[i]>255.
145145
// gcc issues a warning in this case.
146-
char_value[i]=value[i];
146+
char_value[i] = static_cast<char>(value[i]);
147147
}
148148

149149
return string_constantt(char_value);

src/ansi-c/literals/unescape_string.cpp

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -43,15 +43,15 @@ std::basic_string<T> unescape_string_templ(const std::string &src)
4343

4444
for(unsigned i=0; i<src.size(); i++)
4545
{
46-
T ch=(unsigned char)src[i];
46+
T ch = static_cast<T>(static_cast<unsigned char>(src[i]));
4747

4848
if(ch=='\\') // escape?
4949
{
5050
// go to next character
5151
i++;
5252
INVARIANT(i < src.size(), "backslash can't be last character");
5353

54-
ch=(unsigned char)src[i];
54+
ch = static_cast<T>(static_cast<unsigned char>(src[i]));
5555
switch(ch)
5656
{
5757
case '\\': dest.push_back(ch); break;
@@ -105,7 +105,7 @@ std::basic_string<T> unescape_string_templ(const std::string &src)
105105
// go back
106106
i--;
107107

108-
ch=hex_to_unsigned(hex.c_str(), hex.size());
108+
ch = static_cast<T>(hex_to_unsigned(hex.c_str(), hex.size()));
109109
}
110110

111111
// if T isn't sufficiently wide to hold unsigned values
@@ -116,7 +116,7 @@ std::basic_string<T> unescape_string_templ(const std::string &src)
116116
break;
117117

118118
default:
119-
if(isdigit(ch)) // octal
119+
if(isdigit(static_cast<int>(ch))) // octal
120120
{
121121
std::string octal;
122122

@@ -129,7 +129,7 @@ std::basic_string<T> unescape_string_templ(const std::string &src)
129129
// go back
130130
i--;
131131

132-
ch=octal_to_unsigned(octal.c_str(), octal.size());
132+
ch = static_cast<T>(octal_to_unsigned(octal.c_str(), octal.size()));
133133
dest.push_back(ch);
134134
}
135135
else

src/goto-checker/symex_coverage.cpp

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -194,7 +194,8 @@ goto_program_coverage_recordt::goto_program_coverage_recordt(
194194
xmlt &condition = conditions.new_element("condition");
195195
condition.set_attribute("number", std::to_string(number++));
196196
condition.set_attribute("type", "jump");
197-
unsigned taken = c.second.false_taken + c.second.true_taken;
197+
unsigned taken =
198+
static_cast<unsigned>(c.second.false_taken + c.second.true_taken);
198199
total_taken += taken;
199200
condition.set_attribute("coverage", rate(taken, 2, true));
200201
}

src/goto-programs/elf_reader.cpp

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -107,7 +107,8 @@ elf_readert::elf_readert(std::istream &_in):in(_in)
107107
for(std::size_t i=0; i<elf32_section_header_table.size(); i++)
108108
{
109109
// go to right place
110-
in.seekg(elf32_header.e_shoff+i*elf32_header.e_shentsize);
110+
in.seekg(static_cast<std::streamoff>(
111+
elf32_header.e_shoff + i * elf32_header.e_shentsize));
111112

112113
// read section header
113114
in.read(
@@ -187,7 +188,8 @@ elf_readert::elf_readert(std::istream &_in):in(_in)
187188
for(std::size_t i=0; i<elf64_section_header_table.size(); i++)
188189
{
189190
// go to right place
190-
in.seekg(elf64_header.e_shoff+i*elf64_header.e_shentsize);
191+
in.seekg(static_cast<std::streamoff>(
192+
elf64_header.e_shoff + i * elf64_header.e_shentsize));
191193

192194
// read section header
193195
in.read(

src/goto-programs/elf_reader.h

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -143,9 +143,14 @@ class elf_readert
143143

144144
std::streampos section_offset(std::size_t index) const
145145
{
146-
return
147-
elf_class==ELF32?elf32_section_header_table[index].sh_offset:
148-
elf64_section_header_table[index].sh_offset;
146+
// Visual Studio insists on a type cast to std::streamoff, for an unknown
147+
// reason
148+
if(elf_class == ELF32)
149+
return static_cast<std::streamoff>(
150+
elf32_section_header_table[index].sh_offset);
151+
else
152+
return static_cast<std::streamoff>(
153+
elf64_section_header_table[index].sh_offset);
149154
}
150155

151156
bool has_section(const std::string &name) const;

src/goto-programs/read_bin_goto_object.cpp

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -146,7 +146,8 @@ static void read_bin_functions_object(
146146
std::size_t t_count = irepconverter.read_gb_word(in); // # of targets
147147
for(std::size_t i=0; i<t_count; i++)
148148
// just save the target numbers
149-
target_map[itarget].push_back(irepconverter.read_gb_word(in));
149+
target_map[itarget].push_back(
150+
static_cast<unsigned>(irepconverter.read_gb_word(in)));
150151

151152
std::size_t l_count = irepconverter.read_gb_word(in); // # of labels
152153

src/goto-programs/read_goto_binary.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -98,7 +98,7 @@ static bool read_goto_binary(
9898
for(unsigned i=0; i<elf_reader.number_of_sections; i++)
9999
if(elf_reader.section_name(i)=="goto-cc")
100100
{
101-
in.seekg(elf_reader.section_offset(i));
101+
in.seekg(static_cast<std::streamoff>(elf_reader.section_offset(i)));
102102
return read_bin_goto_object(
103103
in, filename, symbol_table, goto_functions, message_handler);
104104
}

src/solvers/floatbv/float_bv.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -426,7 +426,7 @@ exprt float_bvt::conversion(
426426

427427
int sourceSmallestNormalExponent = -((1 << (src_spec.e - 1)) - 1);
428428
int sourceSmallestDenormalExponent =
429-
sourceSmallestNormalExponent - src_spec.f;
429+
sourceSmallestNormalExponent - static_cast<int>(src_spec.f);
430430

431431
// Using the fact that f doesn't include the hidden bit
432432

src/solvers/floatbv/float_utils.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -168,7 +168,7 @@ bvt float_utilst::conversion(
168168

169169
int sourceSmallestNormalExponent=-((1 << (spec.e - 1)) - 1);
170170
int sourceSmallestDenormalExponent =
171-
sourceSmallestNormalExponent - spec.f;
171+
sourceSmallestNormalExponent - static_cast<int>(spec.f);
172172

173173
// Using the fact that f doesn't include the hidden bit
174174

0 commit comments

Comments
 (0)