Skip to content

Commit

Permalink
Enforce -Wold-style-cast (#31361)
Browse files Browse the repository at this point in the history
* Remove assorted old-style casts

A lot of these are in the Android code, which I can't even compile, so I
might have broken the build there.

* Fix casts in dbg macros

All these macros involved a cast.  The cast wasn't really necessary;
added an operator overload to remove the need for it, and then remove
all the casts.

* Add -Wold-style-cast

Also, in each warning option list, make the warnings one per line so
it's easier to see what changes in commit diffs.

* Fix old style casts in json_formatter

* Fix old-style casts in Windows code
  • Loading branch information
jbytheway authored and ZhilkinSerg committed Jun 12, 2019
1 parent 3a3f0d9 commit b2016d7
Show file tree
Hide file tree
Showing 27 changed files with 82 additions and 55 deletions.
6 changes: 5 additions & 1 deletion CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -213,7 +213,11 @@ IF(MSVC)
endif()
ELSE()
SET(CATA_WARNINGS
"-Werror -Wall -Wextra -Woverloaded-virtual -Wpedantic -Wmissing-declarations")
"-Werror -Wall -Wextra \
-Wmissing-declarations \
-Wold-style-cast \
-Woverloaded-virtual \
-Wpedantic")
SET(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} ${CATA_WARNINGS} -std=c++14")
SET(CMAKE_CXX_FLAGS_DEBUG "-Og -g")
ENDIF()
Expand Down
7 changes: 6 additions & 1 deletion Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -83,7 +83,12 @@
# PROFILE is for use with gprof or a similar program -- don't bother generally.
# RELEASE_FLAGS is flags for release builds.
RELEASE_FLAGS =
WARNINGS = -Werror -Wall -Wextra -Woverloaded-virtual -Wpedantic -Wmissing-declarations
WARNINGS = \
-Werror -Wall -Wextra \
-Wmissing-declarations \
-Wold-style-cast \
-Woverloaded-virtual \
-Wpedantic
# Uncomment below to disable warnings
#WARNINGS = -w
DEBUGSYMS = -g
Expand Down
2 changes: 1 addition & 1 deletion src/activity_handlers.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -87,7 +87,7 @@

class npc;

#define dbg(x) DebugLog((DebugLevel)(x),D_GAME) << __FILE__ << ":" << __LINE__ << ": "
#define dbg(x) DebugLog((x),D_GAME) << __FILE__ << ":" << __LINE__ << ": "

const skill_id skill_survival( "survival" );
const skill_id skill_firstaid( "firstaid" );
Expand Down
2 changes: 1 addition & 1 deletion src/avatar_action.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@
#include "vpart_position.h"
#include "vpart_reference.h"

#define dbg(x) DebugLog((DebugLevel)(x),D_SDL) << __FILE__ << ":" << __LINE__ << ": "
#define dbg(x) DebugLog((x),D_SDL) << __FILE__ << ":" << __LINE__ << ": "

static const trait_id trait_BURROW( "BURROW" );
static const trait_id trait_SHELL2( "SHELL2" );
Expand Down
2 changes: 1 addition & 1 deletion src/cata_tiles.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,7 @@
#include "translations.h"
#include "type_id.h"

#define dbg(x) DebugLog((DebugLevel)(x),D_SDL) << __FILE__ << ":" << __LINE__ << ": "
#define dbg(x) DebugLog((x),D_SDL) << __FILE__ << ":" << __LINE__ << ": "

static const std::string ITEM_HIGHLIGHT( "highlight_item" );

Expand Down
2 changes: 1 addition & 1 deletion src/creature_tracker.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
#include "string_formatter.h"
#include "type_id.h"

#define dbg(x) DebugLog((DebugLevel)(x),D_GAME) << __FILE__ << ":" << __LINE__ << ": "
#define dbg(x) DebugLog((x),D_GAME) << __FILE__ << ":" << __LINE__ << ": "

Creature_tracker::Creature_tracker() = default;

Expand Down
10 changes: 6 additions & 4 deletions src/debug.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -588,14 +588,15 @@ void debug_write_backtrace( std::ostream &out )
for( USHORT i = 0; i < num_bt; ++i ) {
DWORD64 off;
out << "\n\t(";
if( SymFromAddr( proc, ( DWORD64 ) bt[i], &off, &sym ) ) {
if( SymFromAddr( proc, reinterpret_cast<DWORD64>( bt[i] ), &off, &sym ) ) {
out << sym.Name << "+0x" << std::hex << off << std::dec;
}
out << "@" << bt[i];
DWORD64 mod_base = SymGetModuleBase64( proc, ( DWORD64 ) bt[i] );
DWORD64 mod_base = SymGetModuleBase64( proc, reinterpret_cast<DWORD64>( bt[i] ) );
if( mod_base ) {
out << "[";
DWORD mod_len = GetModuleFileName( ( HMODULE ) mod_base, mod_path, module_path_len );
DWORD mod_len = GetModuleFileName( reinterpret_cast<HMODULE>( mod_base ), mod_path,
module_path_len );
// mod_len == module_path_len means insufficient buffer
if( mod_len > 0 && mod_len < module_path_len ) {
const char *mod_name = mod_path + mod_len;
Expand All @@ -605,7 +606,8 @@ void debug_write_backtrace( std::ostream &out )
} else {
out << "0x" << std::hex << mod_base << std::dec;
}
out << "+0x" << std::hex << ( uintptr_t ) bt[i] - mod_base << std::dec << "]";
out << "+0x" << std::hex << reinterpret_cast<uintptr_t>( bt[i] ) - mod_base <<
std::dec << "]";
}
out << "), ";
}
Expand Down
8 changes: 7 additions & 1 deletion src/debug.h
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@
* (e.g. mapgen.cpp contains only messages for D_MAP_GEN, npcmove.cpp only D_NPC).
* Those files contain a macro at top:
@code
#define dbg(x) DebugLog((DebugLevel)(x), D_NPC) << __FILE__ << ":" << __LINE__ << ": "
#define dbg(x) DebugLog((x), D_NPC) << __FILE__ << ":" << __LINE__ << ": "
@endcode
* It allows to call the debug system and just supply the debug level, the debug
* class is automatically inserted as it is the same for the whole file. Also this
Expand Down Expand Up @@ -123,6 +123,12 @@ enum DebugLevel {
DL_ALL = ( 1 << 5 ) - 1
};

inline DebugLevel operator|( DebugLevel l, DebugLevel r )
{
return static_cast<DebugLevel>(
static_cast<std::underlying_type_t<DebugLevel>>( l ) | r );
}

/**
* Debugging areas can be enabled for each of those areas separately.
* If you add an entry, add an entry in that function:
Expand Down
2 changes: 1 addition & 1 deletion src/debug_menu.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,7 @@
#include "sdl_wrappers.h"
#endif

#define dbg(x) DebugLog((DebugLevel)(x),D_GAME) << __FILE__ << ":" << __LINE__ << ": "
#define dbg(x) DebugLog((x),D_GAME) << __FILE__ << ":" << __LINE__ << ": "
const efftype_id effect_riding( "riding" );
namespace debug_menu
{
Expand Down
2 changes: 1 addition & 1 deletion src/game.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -164,7 +164,7 @@
# include <tchar.h>
#endif

#define dbg(x) DebugLog((DebugLevel)(x),D_GAME) << __FILE__ << ":" << __LINE__ << ": "
#define dbg(x) DebugLog((x),D_GAME) << __FILE__ << ":" << __LINE__ << ": "

const int core_version = 6;
static constexpr int DANGEROUS_PROXIMITY = 5;
Expand Down
2 changes: 1 addition & 1 deletion src/handle_action.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,7 @@
#include "units.h"
#include "string_id.h"

#define dbg(x) DebugLog((DebugLevel)(x),D_GAME) << __FILE__ << ":" << __LINE__ << ": "
#define dbg(x) DebugLog((x),D_GAME) << __FILE__ << ":" << __LINE__ << ": "

const efftype_id effect_alarm_clock( "alarm_clock" );
const efftype_id effect_laserlocked( "laserlocked" );
Expand Down
2 changes: 1 addition & 1 deletion src/handle_liquid.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@
#include "vehicle.h"
#include "vpart_position.h"

#define dbg(x) DebugLog((DebugLevel)(x),D_GAME) << __FILE__ << ":" << __LINE__ << ": "
#define dbg(x) DebugLog((x),D_GAME) << __FILE__ << ":" << __LINE__ << ": "

// All serialize_liquid_source functions should add the same number of elements to the vectors of
// the activity. This makes it easier to distinguish the values of the source and the values of the target.
Expand Down
2 changes: 1 addition & 1 deletion src/lightmap.h
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@
#define LIGHT_TRANSPARENCY_OPEN_AIR 0.038376418216
#define LIGHT_TRANSPARENCY_CLEAR 1

#define LIGHT_RANGE(b) static_cast<int>( -log(LIGHT_AMBIENT_LOW / (float)b) * (1.0 / LIGHT_TRANSPARENCY_OPEN_AIR) )
#define LIGHT_RANGE(b) static_cast<int>( -log(LIGHT_AMBIENT_LOW / static_cast<float>(b)) * (1.0 / LIGHT_TRANSPARENCY_OPEN_AIR) )

enum lit_level {
LL_DARK = 0,
Expand Down
2 changes: 1 addition & 1 deletion src/map.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -95,7 +95,7 @@ const efftype_id effect_boomered( "boomered" );
const efftype_id effect_crushed( "crushed" );
const efftype_id effect_stunned( "stunned" );

#define dbg(x) DebugLog((DebugLevel)(x),D_MAP) << __FILE__ << ":" << __LINE__ << ": "
#define dbg(x) DebugLog((x),D_MAP) << __FILE__ << ":" << __LINE__ << ": "

static std::list<item> nulitems; // Returned when &i_at() is asked for an OOB value
static field nulfield; // Returned when &field_at() is asked for an OOB value
Expand Down
2 changes: 1 addition & 1 deletion src/mapbuffer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@
#include "visitable.h"
#include "type_id.h"

#define dbg(x) DebugLog((DebugLevel)(x),D_MAP) << __FILE__ << ":" << __LINE__ << ": "
#define dbg(x) DebugLog((x),D_MAP) << __FILE__ << ":" << __LINE__ << ": "

mapbuffer MAPBUFFER;

Expand Down
2 changes: 1 addition & 1 deletion src/mapgen.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,7 @@
#include "cata_utility.h"
#include "int_id.h"

#define dbg(x) DebugLog((DebugLevel)(x),D_MAP_GEN) << __FILE__ << ":" << __LINE__ << ": "
#define dbg(x) DebugLog((x),D_MAP_GEN) << __FILE__ << ":" << __LINE__ << ": "

#define MON_RADIUS 3

Expand Down
2 changes: 1 addition & 1 deletion src/mapgen_functions.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@

class npc_template;

#define dbg(x) DebugLog((DebugLevel)(x),D_MAP_GEN) << __FILE__ << ":" << __LINE__ << ": "
#define dbg(x) DebugLog((x),D_MAP_GEN) << __FILE__ << ":" << __LINE__ << ": "

const mtype_id mon_ant_larva( "mon_ant_larva" );
const mtype_id mon_ant_queen( "mon_ant_queen" );
Expand Down
2 changes: 1 addition & 1 deletion src/mission.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@
#include "monster.h"
#include "player.h"

#define dbg(x) DebugLog((DebugLevel)(x),D_GAME) << __FILE__ << ":" << __LINE__ << ": "
#define dbg(x) DebugLog((x),D_GAME) << __FILE__ << ":" << __LINE__ << ": "

mission mission_type::create( const int npc_id ) const
{
Expand Down
14 changes: 7 additions & 7 deletions src/ncurses_def.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -285,19 +285,19 @@ input_event input_manager::get_input_event()
return input_event( KEY_BACKSPACE, CATA_INPUT_KEYBOARD );
}
rval.type = CATA_INPUT_KEYBOARD;
rval.text.append( 1, ( char ) key );
rval.text.append( 1, static_cast<char>( key ) );
// Read the UTF-8 sequence (if any)
if( key < 127 ) {
// Single byte sequence
} else if( 194 <= key && key <= 223 ) {
rval.text.append( 1, ( char ) getch() );
rval.text.append( 1, static_cast<char>( getch() ) );
} else if( 224 <= key && key <= 239 ) {
rval.text.append( 1, ( char ) getch() );
rval.text.append( 1, ( char ) getch() );
rval.text.append( 1, static_cast<char>( getch() ) );
rval.text.append( 1, static_cast<char>( getch() ) );
} else if( 240 <= key && key <= 244 ) {
rval.text.append( 1, ( char ) getch() );
rval.text.append( 1, ( char ) getch() );
rval.text.append( 1, ( char ) getch() );
rval.text.append( 1, static_cast<char>( getch() ) );
rval.text.append( 1, static_cast<char>( getch() ) );
rval.text.append( 1, static_cast<char>( getch() ) );
} else {
// Other control character, etc. - no text at all, return an event
// without the text property
Expand Down
2 changes: 1 addition & 1 deletion src/npctalk.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -101,7 +101,7 @@ static std::map<std::string, json_talk_topic> json_talk_topics;
// Every OWED_VAL that the NPC owes you counts as +1 towards convincing
#define OWED_VAL 1000

#define dbg(x) DebugLog((DebugLevel)(x),D_GAME) << __FILE__ << ":" << __LINE__ << ": "
#define dbg(x) DebugLog((x),D_GAME) << __FILE__ << ":" << __LINE__ << ": "

int topic_category( const talk_topic &the_topic );

Expand Down
2 changes: 1 addition & 1 deletion src/overmap.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@
#include "monster.h"
#include "string_formatter.h"

#define dbg(x) DebugLog((DebugLevel)(x),D_MAP_GEN) << __FILE__ << ":" << __LINE__ << ": "
#define dbg(x) DebugLog((x),D_MAP_GEN) << __FILE__ << ":" << __LINE__ << ": "

#define BUILDINGCHANCE 4
#define MIN_ANT_SIZE 8
Expand Down
2 changes: 1 addition & 1 deletion src/sdl_wrappers.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@
# endif
#endif // TILES

#define dbg(x) DebugLog((DebugLevel)(x),D_SDL) << __FILE__ << ":" << __LINE__ << ": "
#define dbg(x) DebugLog((x),D_SDL) << __FILE__ << ":" << __LINE__ << ": "

bool printErrorIf( const bool condition, const char *const message )
{
Expand Down
2 changes: 1 addition & 1 deletion src/sdlsound.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@
#include "sdl_wrappers.h"
#include "sounds.h"

#define dbg(x) DebugLog((DebugLevel)(x),D_SDL) << __FILE__ << ":" << __LINE__ << ": "
#define dbg(x) DebugLog((x),D_SDL) << __FILE__ << ":" << __LINE__ << ": "

using id_and_variant = std::pair<std::string, std::string>;
struct sound_effect_resource {
Expand Down
Loading

0 comments on commit b2016d7

Please sign in to comment.