enable msvc bool conversion check.

we now enable warning 4800. I ran though the msvc compile with this
warning and i found some (in overlay.hpp, unit.cpp, whiteboard/move.cpp)
unintended implicit bool b = cfg["attributename"] casts that use
the implicit attribute_value to int cast and then a int to bool cast.
So for attributename=true b would be false. So i believe this
is a useful warning.

There are also a lot of implicit int to bool cast from the SDL and LUA
C-libraries which use int instead of bool because C doesn't have bool.
In this case this warning is less useful, but i still think it's worth
it since a "!= 0" or "== 1" isn't cost.

Plus if someone really wants to disable this warning he can still
disable it in the msvc project settings, while before is was not
possible for someone to enable this warning the the settings.
This commit is contained in:
gfgtdf 2014-04-24 21:12:00 +02:00
parent 03721d9fe6
commit 02a97838b8
16 changed files with 69 additions and 53 deletions

View file

@ -176,7 +176,7 @@ static int ai_move(lua_State *L, bool exec, bool remove_movement)
if (!to_map_location(L, index, to)) goto error_call_destructors;
bool unreach_is_ok = false;
if (lua_isboolean(L, index)) {
unreach_is_ok = lua_toboolean(L, index);
unreach_is_ok = luaW_toboolean(L, index);
}
ai::move_result_ptr move_result = ai::actions::execute_move_action(side,exec,from,to,remove_movement, unreach_is_ok);
return transform_ai_action(L,move_result);

View file

@ -93,16 +93,7 @@ inline boost::shared_ptr<std::string> lua_object<std::string>::to_type(lua_State
template <>
inline boost::shared_ptr<bool> lua_object<bool>::to_type(lua_State *L, int n)
{
#ifdef _MSC_VER
#pragma warning (push)
#pragma warning (disable : 4800)
#endif
return boost::shared_ptr<bool>(new bool(lua_toboolean(L, n)));
#ifdef _MSC_VER
#pragma warning (pop)
#endif
return boost::shared_ptr<bool>(new bool(luaW_toboolean(L, n)));
}
template <>

View file

@ -344,7 +344,7 @@ t_string config::attribute_value::t_str() const
*/
bool config::attribute_value::blank() const
{
return boost::get<const boost::blank>(&value_);
return boost::get<const boost::blank>(&value_) != NULL;
}
/**

View file

@ -541,7 +541,7 @@ void discard_input()
bool is_input(const SDL_Event& event)
{
return SDL_EVENTMASK(event.type) & INPUT_MASK;
return (SDL_EVENTMASK(event.type) & INPUT_MASK) != 0;
}
static void discard(Uint32 event_mask)

View file

@ -31,7 +31,6 @@
#pragma warning(disable: 4345)
#pragma warning(disable: 4250)
#pragma warning(disable: 4355)
#pragma warning(disable: 4800)
#pragma warning(disable: 4351)
#endif //_MSC_VER

View file

@ -642,9 +642,9 @@ void tdistributor::initialize_state()
{
const Uint8 button_state = SDL_GetMouseState(NULL, NULL);
tmouse_button_left::initialize_state(button_state & SDL_BUTTON(1));
tmouse_button_middle::initialize_state(button_state & SDL_BUTTON(2));
tmouse_button_right::initialize_state(button_state & SDL_BUTTON(3));
tmouse_button_left::initialize_state((button_state & SDL_BUTTON(1)) != 0);
tmouse_button_middle::initialize_state((button_state & SDL_BUTTON(2)) != 0);
tmouse_button_right::initialize_state((button_state & SDL_BUTTON(3)) != 0);
init_mouse_location();
}

View file

@ -55,7 +55,7 @@ static const char* name(
static bool attached(const std::vector<SDL_Joystick*>&, const size_t index)
{
return SDL_JoystickOpened(index);
return SDL_JoystickOpened(index) == 1;
}
static const char* name(const std::vector<SDL_Joystick*>&, const size_t index)

View file

@ -27,7 +27,7 @@ struct overlay
overlay(const config& cfg) :
image(cfg["image"]), halo(cfg["halo"]), team_name(cfg["team_name"]),
name(cfg["name"].t_str()), id(cfg["id"]),
halo_handle(-1), visible_in_fog(cfg["visible_in_fog"].to_int())
halo_handle(-1), visible_in_fog(cfg["visible_in_fog"].to_bool())
{
}

View file

@ -422,7 +422,7 @@ static int impl_vconfig_collect(lua_State *L)
#define modify_bool_attrib(name, accessor) \
if (strcmp(m, name) == 0) { \
bool value = lua_toboolean(L, 3); \
bool value = luaW_toboolean(L, 3); \
accessor; \
return 0; \
}
@ -673,7 +673,7 @@ static int impl_unit_status_set(lua_State *L)
unit *u = luaW_tounit(L, -1);
if (!u) return luaL_argerror(L, 1, "unknown unit");
char const *m = luaL_checkstring(L, 2);
u->set_state(m, lua_toboolean(L, 3));
u->set_state(m, luaW_toboolean(L, 3));
return 0;
}
@ -720,7 +720,7 @@ static int impl_unit_variables_set(lua_State *L)
u->variables().remove_attribute(m);
break;
case LUA_TBOOLEAN:
v = bool(lua_toboolean(L, 3));
v = bool(luaW_toboolean(L, 3));
break;
case LUA_TNUMBER:
v = lua_tonumber(L, 3);
@ -980,7 +980,7 @@ static int intf_set_variable(lua_State *L)
variable_info v(m);
switch (lua_type(L, 2)) {
case LUA_TBOOLEAN:
v.as_scalar() = bool(lua_toboolean(L, 2));
v.as_scalar() = luaW_toboolean(L, 2);
break;
case LUA_TNUMBER:
v.as_scalar() = lua_tonumber(L, 2);
@ -1143,7 +1143,7 @@ static int intf_view_locked(lua_State *L)
*/
static int intf_lock_view(lua_State *L)
{
bool lock = lua_toboolean(L, 1);
bool lock = luaW_toboolean(L, 1);
resources::screen->set_view_locked(lock);
return 0;
}
@ -1279,7 +1279,7 @@ static int intf_set_terrain(lua_State *L)
}
if(!lua_isnoneornil(L, 5)) {
replace_if_failed = lua_toboolean(L, 5);
replace_if_failed = luaW_toboolean(L, 5);
}
}
@ -1352,7 +1352,7 @@ static int intf_get_time_of_day(lua_State *L)
lua_pop(L, 2);
lua_rawgeti(L, arg, 3);
consider_illuminates = lua_toboolean(L, -1);
consider_illuminates = luaW_toboolean(L, -1);
lua_pop(L, 1);
}
@ -1426,7 +1426,7 @@ static int intf_set_village_owner(lua_State *L)
return 0;
if (old_side) teams[old_side - 1].lose_village(loc);
if (new_side) teams[new_side - 1].get_village(loc, old_side, lua_toboolean(L, 4));
if (new_side) teams[new_side - 1].get_village(loc, old_side, luaW_toboolean(L, 4));
return 0;
}
@ -1762,12 +1762,12 @@ static int intf_find_path(lua_State *L)
{
lua_pushstring(L, "ignore_units");
lua_rawget(L, arg);
ignore_units = lua_toboolean(L, -1);
ignore_units = luaW_toboolean(L, -1);
lua_pop(L, 1);
lua_pushstring(L, "ignore_teleport");
lua_rawget(L, arg);
ignore_teleport = lua_toboolean(L, -1);
ignore_teleport = luaW_toboolean(L, -1);
lua_pop(L, 1);
lua_pushstring(L, "max_cost");
@ -1862,12 +1862,12 @@ static int intf_find_reach(lua_State *L)
{
lua_pushstring(L, "ignore_units");
lua_rawget(L, arg);
ignore_units = lua_toboolean(L, -1);
ignore_units = luaW_toboolean(L, -1);
lua_pop(L, 1);
lua_pushstring(L, "ignore_teleport");
lua_rawget(L, arg);
ignore_teleport = lua_toboolean(L, -1);
ignore_teleport = luaW_toboolean(L, -1);
lua_pop(L, 1);
lua_pushstring(L, "additional_turns");
@ -2004,7 +2004,7 @@ static int intf_find_cost_map(lua_State *L)
lua_rawget(L, arg);
if (!lua_isnil(L, -1))
{
ignore_units = lua_toboolean(L, -1);
ignore_units = luaW_toboolean(L, -1);
}
lua_pop(L, 1);
@ -2012,7 +2012,7 @@ static int intf_find_cost_map(lua_State *L)
lua_rawget(L, arg);
if (!lua_isnil(L, -1))
{
ignore_teleport = lua_toboolean(L, -1);
ignore_teleport = luaW_toboolean(L, -1);
}
lua_pop(L, 1);
@ -2032,7 +2032,7 @@ static int intf_find_cost_map(lua_State *L)
lua_rawget(L, arg);
if (!lua_isnil(L, -1))
{
debug = lua_toboolean(L, -1);
debug = luaW_toboolean(L, -1);
}
lua_pop(L, 1);
@ -2040,7 +2040,7 @@ static int intf_find_cost_map(lua_State *L)
lua_rawget(L, arg);
if (!lua_isnil(L, -1))
{
use_max_moves = lua_toboolean(L, -1);
use_max_moves = luaW_toboolean(L, -1);
}
lua_pop(L, 1);
++arg;
@ -2360,7 +2360,7 @@ static int intf_unit_resistance(lua_State *L)
{
unit const *u = luaW_checkunit(L, 1);
char const *m = luaL_checkstring(L, 2);
bool a = lua_toboolean(L, 3);
bool a = luaW_toboolean(L, 3);
map_location loc = u->get_location();
if (!lua_isnoneornil(L, 4)) {
@ -2598,8 +2598,8 @@ static int intf_scroll_to_tile(lua_State *L)
{
int x = luaL_checkinteger(L, 1) - 1;
int y = luaL_checkinteger(L, 2) - 1;
bool check_fogged = lua_toboolean(L, 3);
bool immediate = lua_toboolean(L, 4);
bool check_fogged = luaW_toboolean(L, 3);
bool immediate = luaW_toboolean(L, 4);
resources::screen->scroll_to_tile(map_location(x, y),
immediate ? game_display::WARP : game_display::SCROLL, check_fogged);
return 0;
@ -2640,8 +2640,8 @@ static int intf_select_hex(lua_State *L)
if(!resources::game_map->on_board(loc)) return luaL_argerror(L, 1, "not on board");
bool highlight = true;
if(!lua_isnoneornil(L, 3))
highlight = lua_toboolean(L, 3);
const bool fire_event = lua_toboolean(L, 4);
highlight = luaW_toboolean(L, 3);
const bool fire_event = luaW_toboolean(L, 4);
resources::controller->get_mouse_handler_base().select_hex(
loc, false, highlight, fire_event);
if(highlight)
@ -2893,7 +2893,7 @@ static int intf_set_dialog_value(lua_State *L)
}
else if (gui2::tselectable_ *s = dynamic_cast<gui2::tselectable_ *>(w))
{
s->set_value(lua_toboolean(L, 1));
s->set_value(luaW_toboolean(L, 1));
}
else if (gui2::ttext_box *t = dynamic_cast<gui2::ttext_box *>(w))
{
@ -3060,7 +3060,7 @@ static int intf_set_dialog_callback(lua_State *L)
*/
static int intf_set_dialog_markup(lua_State *L)
{
bool b = lua_toboolean(L, 1);
bool b = luaW_toboolean(L, 1);
gui2::twidget *w = find_widget(L, 2, true);
gui2::tcontrol *c = dynamic_cast<gui2::tcontrol *>(w);
if (!c) return luaL_argerror(L, lua_gettop(L), "unsupported widget");
@ -3098,7 +3098,7 @@ static int intf_set_dialog_canvas(lua_State *L)
*/
static int intf_set_dialog_active(lua_State *L)
{
const bool b = lua_toboolean(L, 1);
const bool b = luaW_toboolean(L, 1);
gui2::twidget *w = find_widget(L, 2, true);
gui2::tcontrol *c = dynamic_cast<gui2::tcontrol *>(w);
if (!c) return luaL_argerror(L, lua_gettop(L), "unsupported widget");
@ -3398,7 +3398,7 @@ static int intf_modify_ai(lua_State *L)
static int cfun_exec_candidate_action(lua_State *L)
{
bool exec = lua_toboolean(L, -1);
bool exec = luaW_toboolean(L, -1);
lua_pop(L, 1);
lua_getfield(L, -1, "ca_ptr");
@ -4195,7 +4195,7 @@ bool LuaKernel::run_filter(char const *name, unit const &u)
if (!luaW_pcall(L, 1, 1)) return false;
bool b = lua_toboolean(L, -1);
bool b = luaW_toboolean(L, -1);
lua_pop(L, 1);
return b;
}

View file

@ -98,7 +98,7 @@ bool luaW_hasmetatable(lua_State *L
return false;
lua_pushlightuserdata(L, key);
lua_rawget(L, LUA_REGISTRYINDEX);
bool ok = lua_rawequal(L, -1, -2);
bool ok = lua_rawequal(L, -1, -2) == 1;
lua_pop(L, 2);
return ok;
}
@ -227,7 +227,7 @@ bool luaW_toconfig(lua_State *L, int index, config &cfg, int tstring_meta)
config::attribute_value &v = cfg[lua_tostring(L, -2)];
switch (lua_type(L, -1)) {
case LUA_TBOOLEAN:
v = bool(lua_toboolean(L, -1));
v = luaW_toboolean(L, -1);
break;
case LUA_TNUMBER:
v = lua_tonumber(L, -1);
@ -417,3 +417,8 @@ unit *luaW_checkunit(lua_State *L, int index, bool only_on_map)
if (!u) luaL_typerror(L, index, "unit");
return u;
}
bool luaW_toboolean(lua_State *L, int n)
{
return lua_toboolean(L,n) != 0;
}

View file

@ -125,6 +125,7 @@ bool luaW_getglobal(lua_State *L, ...);
*/
unit *luaW_checkunit(lua_State *L, int index, bool only_on_map = false);
bool luaW_toboolean(lua_State *L, int n);
/**
* Storage for a unit, either owned by the Lua code (#ptr != 0), on a
* recall list (#side != 0), or on the map. Shared units are represented

View file

@ -132,6 +132,7 @@ private:
enum
{
TOK_NONE = 0,
TOK_SPACE = 1,
TOK_NUMERIC = 2,
TOK_ALPHA = 4
@ -144,17 +145,17 @@ private:
bool is_space(int c) const
{
return char_type(c) & TOK_SPACE;
return (char_type(c) & TOK_SPACE) == TOK_SPACE;
}
bool is_num(int c) const
{
return char_type(c) & TOK_NUMERIC;
return (char_type(c) & TOK_NUMERIC) == TOK_NUMERIC;
}
bool is_alnum(int c) const
{
return char_type(c) & (TOK_ALPHA | TOK_NUMERIC);
return (char_type(c) & (TOK_ALPHA | TOK_NUMERIC)) != TOK_NONE;
}
void skip_comment();

View file

@ -2396,7 +2396,7 @@ void unit::add_modification(const std::string& mod_type, const config& mod, bool
is_fearless_ = is_fearless_ || id == "fearless";
is_healthy_ = is_healthy_ || id == "healthy";
if (!mod["generate_description"].empty()) {
generate_description = mod["generate_description"];
generate_description = mod["generate_description"].to_bool();
}
}

View file

@ -204,6 +204,21 @@ return data_.empty() ||
template class progressive_<int>;
template class progressive_<double>;
bool tristate_to_bool(tristate tri, bool def)
{
switch(tri)
{
case(t_false):
return false;
case(t_true):
return true;
case(t_unset):
return def;
default:
throw "found unexpected tristate";
}
}
frame_parameters::frame_parameters() :
duration(0),
image(),
@ -860,7 +875,7 @@ const frame_parameters unit_frame::merge_parameters(int current_time,const frame
result.primary_frame = engine_val.primary_frame;
if(animation_val.primary_frame != t_unset) result.primary_frame = animation_val.primary_frame;
if(current_val.primary_frame != t_unset) result.primary_frame = current_val.primary_frame;
const bool primary = result.primary_frame;
const bool primary = tristate_to_bool(result.primary_frame, true);
/** engine provides a default image to use for the unit when none is available */
result.image = current_val.image.is_void() || current_val.image.get_filename() == ""?animation_val.image:current_val.image;

View file

@ -65,6 +65,7 @@ typedef progressive_<int> progressive_int;
typedef progressive_<double> progressive_double;
typedef enum tristate {t_false,t_true,t_unset} tristate;
bool tristate_to_bool(tristate tri, bool def);
/** All parameters from a frame at a given instant */
class frame_parameters{
public:

View file

@ -111,7 +111,10 @@ move::move(config const& cfg, bool hidden)
}
BOOST_FOREACH(config const& mark_cfg, route_cfg.child_range("mark")) {
route_->marks[map_location(mark_cfg["x"],mark_cfg["y"])]
= pathfind::marked_route::mark(mark_cfg["turns"],mark_cfg["zoc"],mark_cfg["capture"],mark_cfg["invisible"]);
= pathfind::marked_route::mark(mark_cfg["turns"],
mark_cfg["zoc"].to_bool(),
mark_cfg["capture"].to_bool(),
mark_cfg["invisible"].to_bool());
}
// Validate route_ some more