Skip to content

Commit

Permalink
Placate some of TIOBE's MISRA warnings (#3487)
Browse files Browse the repository at this point in the history
  • Loading branch information
RAOF authored Aug 5, 2024
2 parents 89c43df + 9f914be commit 70cb097
Show file tree
Hide file tree
Showing 11 changed files with 122 additions and 102 deletions.
1 change: 1 addition & 0 deletions include/common/mir/events/event_type_to_string.h
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
namespace mir
{
std::string event_type_to_string(MirEventType t);
char const* event_type_to_c_str(MirEventType t);
}

#endif // MIR_EVENT_TYPE_TO_STRING_H_
2 changes: 2 additions & 0 deletions include/common/mir_toolkit/events/event.h
Original file line number Diff line number Diff line change
Expand Up @@ -173,13 +173,15 @@ MirWindowPlacementEvent const* mir_event_get_window_placement_event(MirEvent con
* \param[in] event The event to reference
* \return The event pointer to now use
*/
[[deprecated("Not meaningful: legacy of mirclient API")]]
MirEvent const* mir_event_ref(MirEvent const* event) __attribute__((warn_unused_result));

/**
* Release a reference to a MirEvent.
*
* \param[in] event The event to un-reference
*/
[[deprecated("Not meaningful: legacy of mirclient API")]]
void mir_event_unref(MirEvent const* event);

#ifdef __cplusplus
Expand Down
47 changes: 34 additions & 13 deletions src/common/event.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@
#include "handle_event_exception.h"

#include "mir/events/event_type_to_string.h"
#include "mir/log.h"
#include <mir/fatal.h>

#include "mir_toolkit/events/event.h"
#include "mir/events/event_private.h"
Expand All @@ -30,7 +30,6 @@
#include "mir_toolkit/events/orientation_event.h"
#include "mir_toolkit/events/input_device_state_event.h"

#include <cstdlib>
#include <cstring>

namespace ml = mir::logging;
Expand All @@ -43,23 +42,28 @@ void expect_event_type(EventType const* ev, MirEventType t) MIR_HANDLE_EVENT_EXC
auto type = ev->type();
if (type != t)
{
mir::log_critical("Expected " + mir::event_type_to_string(t) + " but event is of type " +
mir::event_type_to_string(type));
abort();
mir::fatal_error("Expected %s but event is of type %s",
mir::event_type_to_c_str(t), mir::event_type_to_c_str(type));
}
})

void expect_index_in_range(size_t size, size_t index)
{
if (index >= size)
{
mir::log_critical("Index out of range in event data access");
abort();
mir::fatal_error("Index out of range in event data access");
}
}
}

std::string mir::event_type_to_string(MirEventType t)
// GCC and Clang both ensure the switch is exhaustive.
// GCC, however, gets a "control reaches end of non-void function" warning without this
#ifndef __clang__
#pragma GCC diagnostic push
#pragma GCC diagnostic ignored "-Wreturn-type"
#endif

char const* mir::event_type_to_c_str(MirEventType t)
{
switch (t)
{
Expand All @@ -79,11 +83,30 @@ std::string mir::event_type_to_string(MirEventType t)
return "mir_event_type_input_device_state";
case mir_event_type_window_output:
return "mir_event_type_window_output";
default:
abort();
case mir_event_type_window_placement:
return "mir_event_type_window_placement";

#pragma GCC diagnostic push
#pragma GCC diagnostic ignored "-Wdeprecated-declarations"
case mir_event_type_key:
return "mir_event_type_key";
case mir_event_type_motion:
return "mir_event_type_motion";
case mir_event_type_input_configuration:
return "mir_event_type_input_configuration";
#pragma GCC diagnostic pop
}
}

#ifndef __clang__
#pragma GCC diagnostic pop
#endif

std::string mir::event_type_to_string(MirEventType t)
{
return event_type_to_c_str(t);
}


MirEventType mir_event_get_type(MirEvent const* ev) MIR_HANDLE_EVENT_EXCEPTION(
{
Expand All @@ -95,9 +118,7 @@ MirInputEvent const* mir_event_get_input_event(MirEvent const* ev) MIR_HANDLE_EV
auto const type = ev->type();
if (type != mir_event_type_input)
{
mir::log_critical("Expected input event but event is of type " +
mir::event_type_to_string(type));
abort();
mir::fatal_error("Expected input event but event is of type %s", mir::event_type_to_c_str(type));
}

return ev->to_input();
Expand Down
4 changes: 3 additions & 1 deletion src/common/events/window_placement_event.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,9 @@

#include "mir/events/window_placement_event.h"

MirWindowPlacementEvent::MirWindowPlacementEvent() : MirEvent(mir_event_type_window_placement)
MirWindowPlacementEvent::MirWindowPlacementEvent() :
MirEvent(mir_event_type_window_placement),
placement_{0, 0, 0U, 0U}
{
}

Expand Down
78 changes: 41 additions & 37 deletions src/common/input/input_event.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -19,12 +19,13 @@

#include "mir/events/event_type_to_string.h"
#include "mir/events/event_private.h"
#include "mir/fatal.h"
#include "mir/log.h"
#include <mir_toolkit/events/input/pointer_event.h>

#include "../handle_event_exception.h"

#include <string.h>
#include <mir_toolkit/events/input/pointer_event.h>


namespace ml = mir::logging;
Expand All @@ -33,31 +34,43 @@ namespace geom = mir::geometry;

namespace
{
std::string input_event_type_to_string(MirInputEventType input_event_type)
// GCC and Clang both ensure the switch is exhaustive.
// GCC, however, gets a "control reaches end of non-void function" warning without this
#ifndef __clang__
#pragma GCC diagnostic push
#pragma GCC diagnostic ignored "-Wreturn-type"
#endif

char const* input_event_type_to_c_str(MirInputEventType input_event_type)
{
switch (input_event_type)
{
case mir_input_event_type_key:
return "mir_input_event_type_key";
case mir_input_event_type_touch:
return "mir_input_event_type_touch";
case mir_input_event_type_pointer:
return "mir_input_event_type_pointer";
default:
abort();
case mir_input_event_type_key:
return "mir_input_event_type_key";
case mir_input_event_type_touch:
return "mir_input_event_type_touch";
case mir_input_event_type_pointer:
return "mir_input_event_type_pointer";
case mir_input_event_type_keyboard_resync:
return "mir_input_event_type_keyboard_resync";
case mir_input_event_types:
mir::fatal_error_abort("Sentinel value 'mir_input_event_types' not supported");
}
}

#ifndef __clang__
#pragma GCC diagnostic pop
#endif

template <typename EventType>
void expect_event_type(EventType const* ev, MirEventType t) MIR_HANDLE_EVENT_EXCEPTION(
void expect_event_type(EventType const* ev, MirEventType expect)
{
if (ev->type() != t)
if (auto const actual = ev->type(); actual != expect)
{
mir::log_critical("Expected " + mir::event_type_to_string(t) + " but event is of type " +
mir::event_type_to_string(ev->type()));
abort();
mir::fatal_error_abort("Expected %s but event is of type %s",
mir::event_type_to_c_str(expect), mir::event_type_to_c_str(actual));
}
})
}
}

MirInputEventType mir_input_event_get_type(MirInputEvent const* ev) MIR_HANDLE_EVENT_EXCEPTION(
Expand Down Expand Up @@ -111,9 +124,8 @@ MirKeyboardEvent const* mir_input_event_get_keyboard_event(MirInputEvent const*
{
if (ev->input_type() != mir_input_event_type_key)
{
mir::log_critical("expected key input event but event was of type " +
input_event_type_to_string(ev->input_type()));
abort();
mir::fatal_error_abort("expected key input event but event was of type %s",
input_event_type_to_c_str(ev->input_type()));
}

return reinterpret_cast<MirKeyboardEvent const*>(ev);
Expand Down Expand Up @@ -155,9 +167,8 @@ MirTouchEvent const* mir_input_event_get_touch_event(MirInputEvent const* ev) MI
{
if(ev->input_type() != mir_input_event_type_touch)
{
mir::log_critical("expected touch input event but event was of type " +
input_event_type_to_string(ev->input_type()));
abort();
mir::fatal_error_abort("expected touch input event but event was of type %s",
input_event_type_to_c_str(ev->input_type()));
}

return reinterpret_cast<MirTouchEvent const*>(ev);
Expand All @@ -172,8 +183,7 @@ MirTouchId mir_touch_event_id(MirTouchEvent const* event, size_t touch_index) MI
{
if (touch_index >= event->pointer_count())
{
mir::log_critical("touch index is greater than pointer count");
abort();
mir::fatal_error_abort("touch index is greater than pointer count");
}
return event->id(touch_index);

Expand All @@ -183,8 +193,7 @@ MirTouchAction mir_touch_event_action(MirTouchEvent const* event, size_t touch_i
{
if(touch_index > event->pointer_count())
{
mir::log_critical("touch index is greater than pointer count");
abort();
mir::fatal_error_abort("touch index is greater than pointer count");
}

return static_cast<MirTouchAction>(event->action(touch_index));
Expand All @@ -195,8 +204,7 @@ MirTouchTooltype mir_touch_event_tooltype(MirTouchEvent const* event,
{
if(touch_index > event->pointer_count())
{
mir::log_critical("touch index is greater than pointer count");
abort();
mir::fatal_error_abort("touch index is greater than pointer count");
}

return event->tool_type(touch_index);
Expand All @@ -207,8 +215,7 @@ float mir_touch_event_axis_value(MirTouchEvent const* event,
{
if(touch_index > event->pointer_count())
{
mir::log_critical("touch index is greater than pointer count");
abort();
mir::fatal_error_abort("touch index is greater than pointer count");
}

switch (axis)
Expand Down Expand Up @@ -238,9 +245,8 @@ MirPointerEvent const* mir_input_event_get_pointer_event(MirInputEvent const* ev
{
if(ev->input_type() != mir_input_event_type_pointer)
{
mir::log_critical("expected pointer input event but event was of type " +
input_event_type_to_string(ev->input_type()));
abort();
mir::fatal_error_abort("expected pointer input event but event was of type ",
input_event_type_to_c_str(ev->input_type()));
}

return reinterpret_cast<MirPointerEvent const*>(ev);
Expand Down Expand Up @@ -303,8 +309,7 @@ float mir_pointer_event_axis_value(MirPointerEvent const* pev, MirPointerAxis ax
case mir_pointer_axis_hscroll_value120:
return pev->h_scroll().value120.as_value();
default:
mir::log_critical("Invalid axis enumeration " + std::to_string(axis));
abort();
mir::fatal_error_abort("Invalid axis enumeration %d", axis);
}
})

Expand All @@ -326,7 +331,6 @@ bool mir_pointer_event_axis_stop(MirPointerEvent const* pev, MirPointerAxis axis
case mir_pointer_axis_hscroll_value120:
return false;
default:
mir::log_critical("Invalid axis enumeration " + std::to_string(axis));
abort();
mir::fatal_error_abort("Invalid axis enumeration %d", axis);
}
})
1 change: 1 addition & 0 deletions src/common/symbols.map
Original file line number Diff line number Diff line change
Expand Up @@ -693,5 +693,6 @@ global:
MIR_COMMON_2.18 {
global: extern "C++" {
MirTouchpadConfig::disable_with_external_mouse*;
mir::event_type_to_c_str*;
};
} MIR_COMMON_2.17;
27 changes: 15 additions & 12 deletions src/core/depth_layer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -16,24 +16,27 @@

#include "mir/depth_layer.h"

// GCC and Clang both ensure the switch is exhaustive.
// GCC, however, gets a "control reaches end of non-void function" warning without this
#ifndef __clang__
#include "mir/fatal.h"
#pragma GCC diagnostic push
#pragma GCC diagnostic ignored "-Wreturn-type"
#endif

auto mir::mir_depth_layer_get_index(MirDepthLayer depth_layer) -> unsigned int
auto mir::mir_depth_layer_get_index(MirDepthLayer const depth_layer) -> unsigned int
{
switch (depth_layer)
{
case mir_depth_layer_background: return 0;
case mir_depth_layer_below: return 1;
case mir_depth_layer_application: return 2;
case mir_depth_layer_always_on_top: return 3;
case mir_depth_layer_above: return 4;
case mir_depth_layer_overlay: return 5;
case mir_depth_layer_background:
case mir_depth_layer_below:
case mir_depth_layer_application:
case mir_depth_layer_always_on_top:
case mir_depth_layer_above:
case mir_depth_layer_overlay:
return depth_layer;
}
// GCC and Clang both ensure the switch is exhaustive.
// GCC, however, gets a "control reaches end of non-void function" warning without this
}

#ifndef __clang__
fatal_error_abort("Invalid MirDepthLayer in mir::mir_depth_layer_get_index()");
#pragma GCC diagnostic pop
#endif
}
1 change: 1 addition & 0 deletions src/core/geometry/rectangles.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
#include "mir/geometry/rectangles.h"
#include "mir/geometry/displacement.h"
#include <algorithm>
#include <compare>
#include <limits>
#include <ostream>

Expand Down
10 changes: 2 additions & 8 deletions src/miral/window_specification_internal.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -18,12 +18,6 @@

namespace
{
template<typename Dest, typename Source>
void copy_if_set(Dest& dest, mir::optional_value<Source> const& source)
{
if (source.is_set()) dest = source.value();
}

template<typename Dest, typename Source>
void copy_if_set(Dest& dest, std::weak_ptr<Source> const& source)
{
Expand Down Expand Up @@ -68,9 +62,9 @@ void copy_if_set(

}

auto miral::make_surface_spec(WindowSpecification const& window_spec) -> mir::shell::SurfaceSpecification
auto miral::make_surface_spec(WindowSpecification const& miral_spec) -> mir::shell::SurfaceSpecification
{
auto& spec = *window_spec.self;
auto& spec = *miral_spec.self;
mir::shell::SurfaceSpecification result;
copy_if_set(result.top_left, spec.top_left);
copy_if_set(result.pixel_format, spec.pixel_format);
Expand Down
Loading

0 comments on commit 70cb097

Please sign in to comment.