Skip to content

Commit

Permalink
use nullptr for all null pointer constants
Browse files Browse the repository at this point in the history
Also add `-Wzero-as-null-pointer-constant` to cxx flags.
  • Loading branch information
myQwil committed Nov 9, 2023
1 parent 3efb7ad commit ae366bc
Show file tree
Hide file tree
Showing 46 changed files with 230 additions and 217 deletions.
13 changes: 12 additions & 1 deletion CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,18 @@ list(APPEND CMAKE_MODULE_PATH "${CMAKE_CURRENT_LIST_DIR}/cmake")

include(CMakePushCheckState)
include(CheckCXXSourceCompiles)
include(CheckCXXCompilerFlag)
include(TestBigEndian)
include(GNUInstallDirs)
include(CTest)

macro(gme_add_cxx_warning_flag WARNINGFLAG WARNING_VAR)
check_cxx_compiler_flag("-W${WARNINGFLAG}" HAVE_CXXW_${WARNING_VAR})
if(HAVE_CXXW_${WARNING_VAR})
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -W${WARNINGFLAG}")
endif()
endmacro()

file(READ "gme/gme.h" GME_GME_H)
string(REGEX MATCH "/\\* Game_Music_Emu ([0-9]+)\\.([0-9]+).([0-9]+) \\*/" RE_GME_VERSION "${GME_GME_H}")
if(NOT RE_GME_VERSION)
Expand Down Expand Up @@ -64,13 +72,16 @@ elseif(CMAKE_CXX_COMPILER_ID MATCHES "GNU|Clang")
endif()

# Check for GCC/Clang "visibility" support.
if(CMAKE_CXX_COMPILER_ID MATCHES "GNU|Clang" AND ENABLE_UBSAN)
if(CMAKE_CXX_COMPILER_ID MATCHES "GNU|Clang")
gme_add_cxx_warning_flag(zero-as-null-pointer-constant ZERO_AS_NULLPTR_CONSTANT)
if(ENABLE_UBSAN)
# GCC needs -static-libubsan
if(NOT BUILD_SHARED_LIBS AND CMAKE_CXX_COMPILER_ID STREQUAL "GNU")
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -fsanitize=undefined -static-libubsan")
else()
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -fsanitize=undefined")
endif()
endif()
endif()

# Shared library defined here
Expand Down
6 changes: 3 additions & 3 deletions demo/Wave_Writer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ Wave_Writer::Wave_Writer( long sample_rate, const char* filename )
if ( !file )
exit_with_error( "Couldn't open WAVE file for writing" );

setvbuf( file, 0, _IOFBF, 32 * 1024L );
setvbuf( file, nullptr, _IOFBF, 32 * 1024L );
}

void Wave_Writer::flush()
Expand Down Expand Up @@ -151,7 +151,7 @@ void Wave_Writer::close()
fwrite( header, sizeof header, 1, file );

fclose( file );
file = 0;
file = nullptr;
free( buf );
}
}
Expand Down Expand Up @@ -180,5 +180,5 @@ void wave_write( const short* buf, long count ) { ww->write( buf, count ); }
void wave_close()
{
delete ww;
ww = 0;
ww = nullptr;
}
2 changes: 1 addition & 1 deletion gme/Ay_Apu.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -89,7 +89,7 @@ Ay_Apu::Ay_Apu()
}
}

output( 0 );
output( nullptr );
volume( 1.0 );
reset();
}
Expand Down
16 changes: 8 additions & 8 deletions gme/Ay_Emu.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ using std::max;

Ay_Emu::Ay_Emu()
{
beeper_output = 0;
beeper_output = nullptr;
set_type( gme_ay_type );

static const char* const names [osc_count] = {
Expand All @@ -57,7 +57,7 @@ static byte const* get_data( Ay_Emu::file_t const& file, byte const* ptr, int mi
assert( (unsigned long) pos <= (unsigned long) file_size - 2 );
int offset = (int16_t) get_be16( ptr );
if ( !offset || blargg_ulong (pos + offset) > blargg_ulong (file_size - min_size) )
return 0;
return nullptr;
return ptr + offset;
}

Expand All @@ -78,7 +78,7 @@ static blargg_err_t parse_header( byte const* in, long size, Ay_Emu::file_t* out
if ( !out->tracks )
return "Missing track data";

return 0;
return nullptr;
}

static void copy_ay_fields( Ay_Emu::file_t const& file, track_info_t* out, int track )
Expand All @@ -95,7 +95,7 @@ static void copy_ay_fields( Ay_Emu::file_t const& file, track_info_t* out, int t
blargg_err_t Ay_Emu::track_info_( track_info_t* out, int track ) const
{
copy_ay_fields( file, out, track );
return 0;
return nullptr;
}

struct Ay_File : Gme_Info_
Expand All @@ -108,13 +108,13 @@ struct Ay_File : Gme_Info_
{
RETURN_ERR( parse_header( begin, size, &file ) );
set_track_count( file.header->max_track + 1 );
return 0;
return nullptr;
}

blargg_err_t track_info_( track_info_t* out, int track ) const
{
copy_ay_fields( file, out, track );
return 0;
return nullptr;
}
};

Expand Down Expand Up @@ -274,7 +274,7 @@ blargg_err_t Ay_Emu::start_track_( int track )
cpc_mode = false;
cpc_latch = 0;

return 0;
return nullptr;
}

// Emulation
Expand Down Expand Up @@ -406,5 +406,5 @@ blargg_err_t Ay_Emu::run_clocks( blip_time_t& duration, int )

apu.end_frame( duration );

return 0;
return nullptr;
}
8 changes: 4 additions & 4 deletions gme/Blip_Buffer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ Blip_Buffer::Blip_Buffer()
{
factor_ = (blip_ulong)-1 / 2;
offset_ = 0;
buffer_ = 0;
buffer_ = nullptr;
buffer_size_ = 0;
sample_rate_ = 0;
reader_accum_ = 0;
Expand Down Expand Up @@ -117,7 +117,7 @@ Blip_Buffer::blargg_err_t Blip_Buffer::set_sample_rate( long new_rate, int msec

clear();

return 0; // success
return nullptr; // success
}

blip_resampled_time_t Blip_Buffer::clock_rate_factor( long rate ) const
Expand Down Expand Up @@ -191,7 +191,7 @@ void Blip_Buffer::remove_samples( long count )

Blip_Synth_Fast_::Blip_Synth_Fast_()
{
buf = 0;
buf = nullptr;
last_amp = 0;
delta_factor = 0;
}
Expand All @@ -209,7 +209,7 @@ Blip_Synth_::Blip_Synth_( short* p, int w ) :
{
volume_unit_ = 0.0;
kernel_unit = 0;
buf = 0;
buf = nullptr;
last_amp = 0;
delta_factor = 0;
}
Expand Down
18 changes: 9 additions & 9 deletions gme/Classic_Emu.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -20,9 +20,9 @@ Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA */

Classic_Emu::Classic_Emu()
{
buf = 0;
stereo_buffer = 0;
voice_types = 0;
buf = nullptr;
stereo_buffer = nullptr;
voice_types = nullptr;

// avoid inconsistency in our duplicated constants
blaarg_static_assert( (int) wave_type == (int) Multi_Buffer::wave_type, "wave_type inconsistent across two classes using it" );
Expand Down Expand Up @@ -57,7 +57,7 @@ blargg_err_t Classic_Emu::set_sample_rate_( long rate )
blargg_err_t Classic_Emu::set_multi_channel ( bool is_enabled )
{
RETURN_ERR( Music_Emu::set_multi_channel_( is_enabled ) );
return 0;
return nullptr;
}

void Classic_Emu::mute_voices_( int mask )
Expand All @@ -67,7 +67,7 @@ void Classic_Emu::mute_voices_( int mask )
{
if ( mask & (1 << i) )
{
set_voice( i, 0, 0, 0 );
set_voice( i, nullptr, nullptr, nullptr );
}
else
{
Expand All @@ -91,14 +91,14 @@ blargg_err_t Classic_Emu::setup_buffer( long rate )
RETURN_ERR( buf->set_channel_count( voice_count() ) );
set_equalizer( equalizer() );
buf_changed_count = buf->channels_changed_count();
return 0;
return nullptr;
}

blargg_err_t Classic_Emu::start_track_( int track )
{
RETURN_ERR( Music_Emu::start_track_( track ) );
buf->clear();
return 0;
return nullptr;
}

blargg_err_t Classic_Emu::play_( long count, sample_t* out )
Expand All @@ -121,7 +121,7 @@ blargg_err_t Classic_Emu::play_( long count, sample_t* out )
buf->end_frame( clocks_emulated );
}
}
return 0;
return nullptr;
}

// Rom_Data
Expand Down Expand Up @@ -154,7 +154,7 @@ blargg_err_t Rom_Data_::load_rom_data_( Data_Reader& in,
memset( rom.begin() , fill, pad_size );
memset( rom.end() - pad_size, fill, pad_size );

return 0;
return nullptr;
}

void Rom_Data_::set_addr_( long addr, int unit )
Expand Down
16 changes: 8 additions & 8 deletions gme/Data_Reader.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ blargg_err_t Data_Reader::read( void* p, long s )
return "Read error";
}

return 0;
return nullptr;
}

blargg_err_t Data_Reader::skip( long count )
Expand All @@ -65,7 +65,7 @@ blargg_err_t Data_Reader::skip( long count )
count -= n;
RETURN_ERR( read( buf, n ) );
}
return 0;
return nullptr;
}

long File_Reader::remain() const { return size() - tell(); }
Expand All @@ -75,7 +75,7 @@ blargg_err_t File_Reader::skip( long n )
RETURN_VALIDITY_CHECK( n >= 0 );

if ( !n )
return 0;
return nullptr;
return seek( tell() + n );
}

Expand Down Expand Up @@ -146,7 +146,7 @@ blargg_err_t Remaining_Reader::read( void* out, long count )
long first = read_first( out, count );
long second = max( 0l, count - first );
if ( !second )
return 0;
return nullptr;
return in->read( (char*) out + first, second );
}

Expand Down Expand Up @@ -197,7 +197,7 @@ blargg_err_t Mem_File_Reader::seek( long n )
if ( n > m_size )
return eof_error;
m_pos = n;
return 0;
return nullptr;
}

#ifdef HAVE_ZLIB_H
Expand All @@ -224,8 +224,8 @@ bool Mem_File_Reader::gz_decompress()
strm.next_in = const_cast<Bytef *>( reinterpret_cast<const Bytef *>( m_begin ) );
strm.avail_in = static_cast<uInt>( m_size );
strm.total_out = 0;
strm.zalloc = Z_NULL;
strm.zfree = Z_NULL;
strm.zalloc = nullptr;
strm.zfree = nullptr;

bool done = false;

Expand Down Expand Up @@ -404,7 +404,7 @@ blargg_err_t Std_File_Reader::read( void* p, long s )
#endif
const auto &file = reinterpret_cast<FILE*>( file_ );
if ( s == static_cast<long>( fread( p, 1, static_cast<size_t>(s), file ) ) )
return 0;
return nullptr;
if ( feof( file ) )
return eof_error;
return "Couldn't read from file";
Expand Down
2 changes: 1 addition & 1 deletion gme/Data_Reader.h
Original file line number Diff line number Diff line change
Expand Up @@ -135,7 +135,7 @@ class Remaining_Reader : public Data_Reader {
class Callback_Reader : public Data_Reader {
public:
typedef const char* (*callback_t)( void* data, void* out, int count );
Callback_Reader( callback_t, long size, void* data = 0 );
Callback_Reader( callback_t, long size, void* data = nullptr );
public:
long read_avail( void*, long );
blargg_err_t read( void*, long );
Expand Down
4 changes: 2 additions & 2 deletions gme/Fir_Resampler.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ Fir_Resampler_::Fir_Resampler_( int width, sample_t* impulses_ ) :
write_offset( width * stereo - stereo ),
impulses( impulses_ )
{
write_pos = 0;
write_pos = nullptr;
res = 1;
imp_phase = 0;
skip_bits = 0;
Expand All @@ -81,7 +81,7 @@ blargg_err_t Fir_Resampler_::buffer_size( int new_size )
{
RETURN_ERR( buf.resize( new_size + write_offset ) );
clear();
return 0;
return nullptr;
}

double Fir_Resampler_::time_ratio( double new_factor, double rolloff, double gain )
Expand Down
10 changes: 5 additions & 5 deletions gme/Gb_Apu.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -42,11 +42,11 @@ Gb_Apu::Gb_Apu()
{
Gb_Osc& osc = *oscs [i];
osc.regs = &regs [i * 5];
osc.output = 0;
osc.outputs [0] = 0;
osc.outputs [1] = 0;
osc.outputs [2] = 0;
osc.outputs [3] = 0;
osc.output = nullptr;
osc.outputs [0] = nullptr;
osc.outputs [1] = nullptr;
osc.outputs [2] = nullptr;
osc.outputs [3] = nullptr;
}

set_tempo( 1.0 );
Expand Down
2 changes: 1 addition & 1 deletion gme/Gb_Cpu.h
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ class Gb_Cpu {
enum { clocks_per_instr = 4 };
public:
// Clear registers and map all pages to unmapped
void reset( void* unmapped = 0 );
void reset( void* unmapped = nullptr );

// Map code memory (memory accessed via the program counter). Start and size
// must be multiple of page_size.
Expand Down
Loading

0 comments on commit ae366bc

Please sign in to comment.