Skip to content

Commit

Permalink
Fixes (most) -Wall and -Wextra warnings
Browse files Browse the repository at this point in the history
Doesn't fix cmidi2.hpp since that's external.

Signed-off-by: Gabe Gonzalez II <lilggamegenius@gmail.com>
  • Loading branch information
lilggamegenius authored and jcelerier committed Jan 10, 2024
1 parent cc9062e commit 741a30f
Show file tree
Hide file tree
Showing 16 changed files with 156 additions and 135 deletions.
17 changes: 17 additions & 0 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -178,6 +178,23 @@ else()
set(_private PRIVATE)
endif()

if(MSVC)
set(WARNING_FLAGS
/W4
)
elseif(CMAKE_CXX_COMPILER_ID MATCHES "Clang|GNU")
set(WARNING_FLAGS
-Werror # Turns all warnings into errors
-Wall # Enable most warning messages.
-Wextra # Print extra (possibly unwanted) warnings.
#-Wpedantic # Issue warnings needed for strict compliance to the standard.
)
else()
message(WARNING "CMake flags for compiler aren't set for compiler ${CMAKE_CXX_COMPILER_ID}")
endif()

target_compile_options(libremidi PUBLIC ${WARNING_FLAGS})

add_library(libremidi::libremidi ALIAS libremidi)

if(LIBREMIDI_SLIM_MESSAGE GREATER 0)
Expand Down
6 changes: 3 additions & 3 deletions include/libremidi/backends/dummy.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ namespace libremidi
class observer_dummy : public observer_api
{
public:
explicit observer_dummy(const auto& configuration, dummy_configuration) { }
explicit observer_dummy(const auto& /*configuration*/, dummy_configuration) { }

~observer_dummy() { }
libremidi::API get_current_api() const noexcept override { return libremidi::API::DUMMY; }
Expand All @@ -29,7 +29,7 @@ class midi_in_dummy final
}

libremidi::API get_current_api() const noexcept override { return libremidi::API::DUMMY; }
bool open_port(const input_port& pt, std::string_view local_port_name) override { return true; }
bool open_port(const input_port& /*pt*/, std::string_view /*local_port_name*/) override { return true; }
bool open_virtual_port(std::string_view /*portName*/) override { return true; }
void close_port() override { }
void set_client_name(std::string_view /*clientName*/) override { }
Expand All @@ -47,7 +47,7 @@ class midi_out_dummy final
}

libremidi::API get_current_api() const noexcept override { return libremidi::API::DUMMY; }
bool open_port(const output_port& pt, std::string_view local_port_name) override { return true; }
bool open_port(const output_port& /*pt*/, std::string_view /*local_port_name*/) override { return true; }
bool open_virtual_port(std::string_view /*portName*/) override { return true; }
void close_port() override { }
void set_client_name(std::string_view /*clientName*/) override { }
Expand Down
4 changes: 2 additions & 2 deletions include/libremidi/backends/winmm/helpers.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -35,8 +35,8 @@ inline std::string ConvertToUTF8(const TCHAR* str)
int length = WideCharToMultiByte(CP_UTF8, 0, wstr, -1, nullptr, 0, nullptr, nullptr);
if (length)
{
u8str.assign(length - 1, 0);
length = WideCharToMultiByte(CP_UTF8, 0, wstr, -1, &u8str[0], length, nullptr, nullptr);
u8str.assign(static_cast<std::string::size_type>(length - 1), 0);
/*length =*/ WideCharToMultiByte(CP_UTF8, 0, wstr, -1, &u8str[0], length, nullptr, nullptr);
}
return u8str;
}
Expand Down
31 changes: 17 additions & 14 deletions include/libremidi/backends/winmm/midi_in.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,7 @@ class midi_in_winmm final
bool do_open(unsigned int portNumber)
{
MMRESULT result = midiInOpen(
&this->inHandle, portNumber, (DWORD_PTR)&midiInputCallback, (DWORD_PTR)this,
&this->inHandle, portNumber, reinterpret_cast<DWORD_PTR>(&midiInputCallback), reinterpret_cast<DWORD_PTR>(this),
CALLBACK_FUNCTION);
if (result != MMSYSERR_NOERROR)
{
Expand All @@ -74,12 +74,13 @@ class midi_in_winmm final
}

// Allocate and init the sysex buffers.
this->sysexBuffer.resize(configuration.sysex_buffer_count);
for (int i = 0; i < configuration.sysex_buffer_count; ++i)
const auto bufferCount = static_cast<std::size_t>(configuration.sysex_buffer_count);
this->sysexBuffer.resize(bufferCount);
for (std::size_t i = 0; i < bufferCount; ++i)
{
this->sysexBuffer[i] = (MIDIHDR*)new char[sizeof(MIDIHDR)];
this->sysexBuffer[i] = reinterpret_cast<MIDIHDR*>(new char[sizeof(MIDIHDR)]);
this->sysexBuffer[i]->lpData = new char[configuration.sysex_buffer_size];
this->sysexBuffer[i]->dwBufferLength = configuration.sysex_buffer_size;
this->sysexBuffer[i]->dwBufferLength = static_cast<DWORD>(configuration.sysex_buffer_size);
this->sysexBuffer[i]->dwUser = i; // We use the dwUser parameter as buffer indicator
this->sysexBuffer[i]->dwFlags = 0;

Expand Down Expand Up @@ -152,9 +153,9 @@ class midi_in_winmm final
midiInReset(this->inHandle);
midiInStop(this->inHandle);

for (int i = 0; i < configuration.sysex_buffer_count; ++i)
for (std::size_t i = 0; i < static_cast<std::size_t>(configuration.sysex_buffer_count); ++i)
{
int res{};
MMRESULT res{};

int wait_count = 5;
while (
Expand All @@ -181,7 +182,7 @@ class midi_in_winmm final
}

midiInClose(this->inHandle);
this->inHandle = 0;
this->inHandle = nullptr;
LeaveCriticalSection(&(this->_mutex));
}
}
Expand All @@ -206,12 +207,12 @@ class midi_in_winmm final
}
else
{
message.timestamp = time;
message.timestamp = static_cast<int64_t>(time);
}
return;
}
case timestamp_mode::Absolute: {
msg.timestamp = ts * 1'000'000;
msg.timestamp = static_cast<int64_t>(ts * 1'000'000);
break;
}
case timestamp_mode::SystemMonotonic: {
Expand All @@ -221,6 +222,8 @@ class midi_in_winmm final
.count();
break;
}
default:
break;
}
}
static void CALLBACK midiInputCallback(
Expand All @@ -230,7 +233,7 @@ class midi_in_winmm final
if (inputStatus != MIM_DATA && inputStatus != MIM_LONGDATA && inputStatus != MIM_LONGERROR)
return;

auto& self = *(midi_in_winmm*)instancePtr;
auto& self = *reinterpret_cast<midi_in_winmm*>(instancePtr);

auto& message = self.message;

Expand All @@ -240,7 +243,7 @@ class midi_in_winmm final
{ // Channel or system message

// Make sure the first byte is a status byte.
unsigned char status = (unsigned char)(midiMessage & 0x000000FF);
const auto status = static_cast<unsigned char>(midiMessage & 0x000000FF);
if (!(status & 0x80))
return;

Expand Down Expand Up @@ -275,12 +278,12 @@ class midi_in_winmm final
}

// Copy bytes to our MIDI message.
unsigned char* ptr = (unsigned char*)&midiMessage;
const auto* ptr = reinterpret_cast<unsigned char*>(&midiMessage);
message.bytes.assign(ptr, ptr + nBytes);
}
else
{ // Sysex message ( MIM_LONGDATA or MIM_LONGERROR )
MIDIHDR* sysex = (MIDIHDR*)midiMessage;
const auto* sysex = reinterpret_cast<MIDIHDR*>(midiMessage);
if (!self.configuration.ignore_sysex && inputStatus != MIM_LONGERROR)
{
// Sysex message and we're not ignoring it
Expand Down
4 changes: 2 additions & 2 deletions include/libremidi/backends/winmm/observer.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -88,7 +88,7 @@ class observer_winmm : public observer_api

if (portRemovedFunc)
{
for (const auto port : prevList)
for (const auto &port : prevList)
{
auto iter
= std::ranges::find(currList, port.display_name, &port_information::display_name);
Expand Down Expand Up @@ -231,7 +231,7 @@ class observer_threaded final : public observer_winmm

private:
std::thread thread;
std::atomic_flag stop_flag{ATOMIC_FLAG_INIT};
std::atomic_flag stop_flag = ATOMIC_FLAG_INIT;
std::binary_semaphore sema;
};
}
Expand Down
6 changes: 3 additions & 3 deletions include/libremidi/client.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -4,16 +4,16 @@
#include <libremidi/backends.hpp>
#include <libremidi/shared_context.hpp>

#if LIBREMIDI_ALSA
#ifdef LIBREMIDI_ALSA
#include <libremidi/backends/alsa_seq/shared_handler.hpp>
#endif
#if LIBREMIDI_JACK
#ifdef LIBREMIDI_JACK
#include <libremidi/backends/jack/shared_handler.hpp>
#endif
namespace libremidi
{
LIBREMIDI_INLINE
shared_configurations create_shared_context(libremidi::API api, std::string_view client_name)
shared_configurations create_shared_context(const libremidi::API api, std::string_view /*client_name*/)
{
switch (api)
{
Expand Down
2 changes: 1 addition & 1 deletion include/libremidi/client.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ struct client_configuration
//! Set a callback function to be invoked for incoming MIDI messages.
//! Mandatory!
std::function<void(const libremidi::input_port&, message&&)> on_message
= [](const libremidi::input_port& port, libremidi::message&&) {};
= []([[maybe_unused]] const libremidi::input_port& port, libremidi::message&&) {};

//! Observation callbacks for when ports are added or removed
input_port_callback input_added;
Expand Down
6 changes: 3 additions & 3 deletions include/libremidi/detail/midi_out.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -24,13 +24,13 @@ class midi_out_api : public midi_api
virtual int64_t current_time() const noexcept { return 0; }

virtual void send_message(const unsigned char* message, size_t size) = 0;
virtual void schedule_message(int64_t ts, const unsigned char* message, size_t size)
virtual void schedule_message([[maybe_unused]] int64_t ts, const unsigned char* message, size_t size)
{
return send_message(message, size);
}

virtual void send_ump(const uint32_t* message, size_t size) = 0;
virtual void schedule_ump(int64_t ts, const uint32_t* ump, size_t size)
virtual void schedule_ump([[maybe_unused]] int64_t ts, const uint32_t* ump, size_t size)
{
return send_ump(ump, size);
}
Expand All @@ -45,7 +45,7 @@ class out_api : public midi_out_api
public:
using midi_out_api::midi_out_api;

void send_ump(const uint32_t* message, size_t size)
void send_ump(const uint32_t* message, [[maybe_unused]] size_t size)
{
uint8_t midi[65536];
int n = cmidi2_convert_single_ump_to_midi1(midi, sizeof(midi), (uint32_t*)message);
Expand Down
10 changes: 5 additions & 5 deletions include/libremidi/libremidi.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,7 @@ class LIBREMIDI_EXPORT observer
//! * api_conf can be an instance of observer_configuration,
//! such as jack_observer_configuration, winmm_observer_configuration, etc...
//! * if no callbacks are passed, no secondary thread will be created unless absolutely necessary
explicit observer(observer_configuration conf = {}) noexcept;
explicit observer(const observer_configuration &conf = {}) noexcept;
explicit observer(observer_configuration conf, std::any api_conf);
observer(const observer&) = delete;
observer(observer&& other) noexcept;
Expand All @@ -91,8 +91,8 @@ class LIBREMIDI_EXPORT observer
[[nodiscard]] libremidi::API get_current_api() const noexcept;

//! Return identifiers for the available MIDI ports
std::vector<libremidi::input_port> get_input_ports() const noexcept;
std::vector<libremidi::output_port> get_output_ports() const noexcept;
[[nodiscard]] std::vector<libremidi::input_port> get_input_ports() const noexcept;
[[nodiscard]] std::vector<libremidi::output_port> get_output_ports() const noexcept;

private:
std::unique_ptr<class observer_api> impl_;
Expand All @@ -103,7 +103,7 @@ class LIBREMIDI_EXPORT midi_in
{
public:
//! Construct a midi_in object with the default MIDI 1 back-end for the platform
explicit midi_in(input_configuration conf) noexcept;
explicit midi_in(const input_configuration& conf) noexcept;

//! Construct a midi_in object with a configuration object for a specific MIDI 1 back-end
//! see configuration.hpp for the available configuration types.
Expand Down Expand Up @@ -158,7 +158,7 @@ class LIBREMIDI_EXPORT midi_out
{
public:
//! Construct a midi_out object with the default back-end for the platform
explicit midi_out(output_configuration conf = {}) noexcept;
explicit midi_out(const output_configuration& conf = {}) noexcept;

//! Construct a midi_out object with a configuration object for a specific back-end
//! see configuration.hpp for the available configuration types.
Expand Down
33 changes: 18 additions & 15 deletions include/libremidi/message.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -95,8 +95,11 @@ struct message

auto clear() noexcept { bytes.clear(); }

auto& operator[](int i) const noexcept { return bytes[i]; }
auto& operator[](int i) noexcept { return bytes[i]; }
auto& operator[](int i) const noexcept { return bytes[static_cast<unsigned int>(i)]; }
auto& operator[](int i) noexcept { return bytes[static_cast<unsigned int>(i)]; }

auto& operator[](std::size_t i) const noexcept { return bytes[i]; }
auto& operator[](std::size_t i) noexcept { return bytes[i]; }

auto& front() const { return bytes.front(); }
auto& back() const { return bytes.back(); }
Expand Down Expand Up @@ -138,18 +141,18 @@ struct message
{
if (!is_meta_event())
return meta_event_type::UNKNOWN;
return (meta_event_type)bytes[1];
return static_cast<meta_event_type>(bytes[1]);
}

message_type get_message_type() const noexcept
{
if (bytes[0] >= uint8_t(message_type::SYSTEM_EXCLUSIVE))
if (bytes[0] >= static_cast<uint8_t>(message_type::SYSTEM_EXCLUSIVE))
{
return (message_type)(bytes[0] & 0xFF);
return static_cast<message_type>(bytes[0] & 0xFF);
}
else
{
return (message_type)(bytes[0] & 0xF0);
return static_cast<message_type>(bytes[0] & 0xF0);
}
}

Expand All @@ -169,12 +172,12 @@ struct channel_events
channel = 0;
else if (channel > 15)
channel = 15;
return channel;
return static_cast<uint8_t>(channel);
}

static uint8_t make_command(const message_type type, const int channel) noexcept
{
return (uint8_t)((uint8_t)type | clamp_channel(channel));
return static_cast<uint8_t>(static_cast<uint8_t>(type) | clamp_channel(channel));
}

static message note_on(uint8_t channel, uint8_t note, uint8_t velocity) noexcept
Expand All @@ -200,8 +203,8 @@ struct channel_events
static message pitch_bend(uint8_t channel, int value) noexcept
{
return {
make_command(message_type::PITCH_BEND, channel), (unsigned char)(value & 0x7F),
(uint8_t)((value >> 7) & 0x7F)};
make_command(message_type::PITCH_BEND, channel), static_cast<unsigned char>(value & 0x7F),
static_cast<uint8_t>((value >> 7) & 0x7F)};
}

static message pitch_bend(uint8_t channel, uint8_t lsb, uint8_t msb) noexcept
Expand All @@ -226,12 +229,12 @@ struct meta_events

static message channel(int channel) noexcept
{
return {0xff, 0x20, 0x01, (uint8_t)std::clamp(0, 0xff, channel - 1)};
return {0xff, 0x20, 0x01, static_cast<uint8_t>(std::clamp(0, 0xff, channel - 1))};
}

static message tempo(int mpqn) noexcept
{
return {0xff, 81, 3, (uint8_t)(mpqn >> 16), (uint8_t)(mpqn >> 8), (uint8_t)mpqn};
return {0xff, 81, 3, static_cast<uint8_t>(mpqn >> 16), static_cast<uint8_t>(mpqn >> 8), static_cast<uint8_t>(mpqn)};
}

static message time_signature(int numerator, int denominator)
Expand All @@ -245,7 +248,7 @@ struct meta_events
++powTwo;
}

return {0xff, 0x58, 0x04, (uint8_t)numerator, (uint8_t)powTwo, 1, 96};
return {0xff, 0x58, 0x04, static_cast<uint8_t>(numerator), static_cast<uint8_t>(powTwo), 1, 96};
}

// Where key index goes from -7 (7 flats, C♭ Major) to +7 (7 sharps, C♯
Expand All @@ -256,12 +259,12 @@ struct meta_events
if (keyIndex < -7 || keyIndex > 7)
throw std::range_error("meta_events::key_signature: out of range");
#endif
return {0xff, 0x59, 0x02, (uint8_t)keyIndex, isMinor ? (uint8_t)1 : (uint8_t)0};
return {0xff, 0x59, 0x02, static_cast<uint8_t>(keyIndex), isMinor ? static_cast<uint8_t>(1) : static_cast<uint8_t>(0)};
}

static message song_position(int positionInBeats) noexcept
{
return {0xf2, (uint8_t)(positionInBeats & 127), (uint8_t)((positionInBeats >> 7) & 127)};
return {0xf2, static_cast<uint8_t>(positionInBeats & 127), static_cast<uint8_t>((positionInBeats >> 7) & 127)};
}
};

Expand Down
4 changes: 2 additions & 2 deletions include/libremidi/midi_in.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ LIBREMIDI_INLINE auto make_midi_in(auto base_conf, std::any api_conf, auto backe

assert(base_conf.on_message);

auto from_api = [&]<typename T>(T& backend) mutable {
auto from_api = [&]<typename T>(T& /*backend*/) mutable {
if (auto conf = std::any_cast<typename T::midi_in_configuration>(&api_conf))
{
ptr = libremidi::make<typename T::midi_in>(std::move(base_conf), std::move(*conf));
Expand All @@ -27,7 +27,7 @@ LIBREMIDI_INLINE auto make_midi_in(auto base_conf, std::any api_conf, auto backe
return ptr;
}

LIBREMIDI_INLINE midi_in::midi_in(input_configuration base_conf) noexcept
LIBREMIDI_INLINE midi_in::midi_in(const input_configuration& base_conf) noexcept
{
for (const auto& api : available_apis())
{
Expand Down
Loading

0 comments on commit 741a30f

Please sign in to comment.