Cleanup str(n)cmp with std::string_view #4567#4742
Cleanup str(n)cmp with std::string_view #4567#4742sivansh11 wants to merge 4 commits intocanonical:mainfrom
Conversation
|
@tarek-y-ismail, I am not able to test the changes, since ctest is getting stuck on the 6th test (named wlcs) |
There was a problem hiding this comment.
Pull request overview
This PR modernizes C-string comparisons across Mir by replacing strcmp/strncmp usages with clearer std::string_view equality and starts_with() checks, aligning with #4567’s guidance.
Changes:
- Replaced
strcmpequality/inequality checks withstd::string_viewcomparisons (often using"..."svliterals). - Replaced common
strncmp(prefix, ..., N)patterns withstd::string_view::starts_with(). - Added
<string_view>includes (andstd::string_view_literalswhere needed) in many touched files.
Reviewed changes
Copilot reviewed 28 out of 28 changed files in this pull request and generated 2 comments.
Show a summary per file
| File | Description |
|---|---|
| tests/unit-tests/input/evdev/test_evdev_input_platform.cpp | Switches devnode matching from strcmp to std::string_view compare (currently missing <string_view> include). |
| tests/unit-tests/console/test_minimal_console_services.cpp | Replaces /dev/ + /sys comparisons with starts_with() / std::string_view equality; adds <string_view>. |
| tests/unit-tests/console/test_linux_virtual_terminal.cpp | Replaces /dev/ prefix check with starts_with(); adds <string_view>. |
| tests/miral/wayland_extensions.cpp | Replaces Wayland interface name strcmp checks with std::string_view comparisons; adds <string_view>. |
| tests/miral/external_client.cpp | Replaces getenv/strcmp check with safe pointer check + string_view literal compare; adds <cstdlib>, <string_view>, and literals. |
| tests/mir_test_framework/test_wlcs_display_server.cpp | Replaces Wayland resource class strcmp checks with string_view/literal comparisons; adds <string_view> and literals. |
| tests/mir_test_doubles/mock_drm.cpp | Replaces /dev/dri/ prefix strncmp with starts_with(); adds <string_view>. |
| src/server/scene/basic_surface.cpp | Replaces strcmp on pthread name with string_view literal compare; adds <string_view> and literals. |
| src/server/graphics/platform_probe.cpp | Replaces strcmp ordering / module name checks with std::string_view comparisons; adds <string_view> and literals. |
| src/server/console/minimal_console_services.cpp | Replaces DEVNAME= strncmp parsing with starts_with(); adds <string_view>. |
| src/server/console/logind_console_services.cpp | Replaces several strcmp/strncmp checks with string_view equality/starts_with(); adds <string_view> and literals. |
| src/server/console/linux_virtual_terminal.cpp | Replaces DEVNAME= strncmp parsing with starts_with(); adds <string_view>. |
| src/platforms/wayland/displayclient.cpp | Replaces Wayland global interface strcmp checks with string_view/literal comparisons; adds <string_view> and literals. |
| src/platforms/gbm-kms/server/kms/real_kms_output.cpp | Replaces DRM property/mode strcmp checks with string_view literal comparisons; adds <string_view> and literals. |
| src/platforms/gbm-kms/server/kms/platform_symbols.cpp | Replaces driver string comparisons and prefix checks with string_view compares/starts_with(); adds <string_view> and literals. |
| src/platforms/gbm-kms/server/kms/display.cpp | Replaces devnode strcmp checks with std::string_view equality; adds <string_view>. |
| src/platforms/evdev/platform.cpp | Replaces strncmp/strcmp checks with starts_with() / std::string_view equality; adds <string_view>. |
| src/platforms/eglstream-kms/server/egl_output.cpp | Replaces DRM property strcmp check with string_view literal compare; adds <string_view> and literals. |
| src/platforms/atomic-kms/server/kms/display.cpp | Replaces devnode strcmp checks with std::string_view equality; adds <string_view>. |
| src/platforms/atomic-kms/server/kms/atomic_kms_output.cpp | Replaces mode strcmp check with string_view literal compare; adds <string_view> and literals. |
| src/platform/udev/udev_wrapper.cpp | Replaces devpath equality and action string comparisons with std::string_view compares/literals; adds <string_view> and literals. |
| src/platform/options/default_configuration.cpp | Replaces library name strcmp ordering check with std::string_view ordering; adds <string_view> and literals. |
| src/miral/wayland_app.cpp | Replaces Wayland registry interface strcmp checks with string_view literal comparisons; adds <string_view> and literals. |
| src/miral/launch_app.cpp | Replaces env-var prefix strncmp checks with starts_with(); adds <string_view>. |
| include/test/mir/test/event_matchers.h | Replaces matcher strcmp with std::string_view inequality; adds <string_view>. |
| examples/miral-shell/shell_main.cpp | Replaces strcmp against env-var values with string_view literal comparisons; includes <string_view> and literals. |
| examples/example-server-lib/wayland_app.cpp | Replaces Wayland registry interface strcmp checks with string_view literal comparisons; includes <string_view> and literals. |
| examples/client/mir_shell_demo.cpp | Replaces registry strcmp checks with "..."sv and std::string_view compares (currently missing <string_view> + literals import). |
| if (interface == "wl_compositor"sv) | ||
| { | ||
| globals::compositor = static_cast<wl_compositor*>(wl_registry_bind( | ||
| registry, id, &wl_compositor_interface, std::min(version, 3u))); | ||
| } | ||
| else if (strcmp(interface, "wl_shm") == 0) | ||
| else if (interface == "wl_shm"sv) | ||
| { |
There was a problem hiding this comment.
This block uses the "..."sv string_view literal and std::string_view{...} but the file doesn't include <string_view> nor import std::string_view_literals, so it will not compile. Add #include <string_view> and using namespace std::string_view_literals; (or avoid sv by comparing via std::string_view{interface} == "wl_compositor").
There was a problem hiding this comment.
This block uses the
"..."svstring_view literal andstd::string_view{...}but the file doesn't include<string_view>nor importstd::string_view_literals, so it will not compile. Add#include <string_view>andusing namespace std::string_view_literals;(or avoidsvby comparing viastd::string_view{interface} == "wl_compositor").
This only compiles because <string> transitively includes <string_view> in the current libstcc++ implementation. (Which then means using namespace std::literals pulls in string_view_literals.)
There's no guarantee this will work with other library implementations (nor future versions of this one).
| { | ||
| // Starting an X server on LP builder, or Fedora CI, doesn't work | ||
| auto const lp_fake_runtime_dir = strcmp(getenv("XDG_RUNTIME_DIR"), "/tmp") == 0; | ||
| auto const xdg_dir = getenv("XDG_RUNTIME_DIR"); |
There was a problem hiding this comment.
There is a subtle change here: the original code has undefined behaviour (probably a SIGSEGV) if getenv("XDG_RUNTIME_DIR") returns nullptr. The new code sets lp_fake_runtime_dir to false and carries on.
It would probably be better to abort() if there is no XDG_RUNTIME_DIR in the environment.
However, there are checks elsewhere that ensure XDG_RUNTIME_DIR is set, so this is likely moot.
There was a problem hiding this comment.
@AlanGriffiths should I add the abort check here even tho it is rechecked later on ? Or maybe just assert ?
There was a problem hiding this comment.
@sivansh11 in general, it is better to fail noisily (i.e. abort()) when things are wrong than to carry on (set lp_fake_runtime_dir to false, assert() or "undefined behaviour").
| if (interface == "wl_compositor"sv) | ||
| { | ||
| globals::compositor = static_cast<wl_compositor*>(wl_registry_bind( | ||
| registry, id, &wl_compositor_interface, std::min(version, 3u))); | ||
| } | ||
| else if (strcmp(interface, "wl_shm") == 0) | ||
| else if (interface == "wl_shm"sv) | ||
| { |
| if (!xdg_dir) | ||
| std::runtime_error("XDG_RUNTIME_DIR not found in environment"); |
There was a problem hiding this comment.
What is the intention here? This constructs an unnamed runtime_error instance (and discards it): that doesn't achieve anything.
There was a problem hiding this comment.
I forgot to throw, force pushed a fix to this
if user does not have XDG_RUNTIME_DIR, error out
| #include <string_view> | ||
|
|
||
| namespace mo = mir::options; | ||
| using namespace std::string_view_literals; |
| auto const rhs_desc = rhs->load_function<mir::graphics::DescribeModule>("describe_graphics_module", MIR_SERVER_GRAPHICS_PLATFORM_VERSION); | ||
|
|
||
| return strcmp(rhs_desc()->name, lhs_desc()->name) > 0; | ||
| return std::string_view{rhs_desc()->name} > std::string_view{lhs_desc()->name}; |
There was a problem hiding this comment.
Where are you using a literal?
| if (!xdg_dir) | ||
| throw std::runtime_error("XDG_RUNTIME_DIR not found in environment"); |
There was a problem hiding this comment.
Throwing an exception likely results in an unhelpful message from the test framework:
| if (!xdg_dir) | |
| throw std::runtime_error("XDG_RUNTIME_DIR not found in environment"); | |
| if (!xdg_dir) | |
| { | |
| puts("This test expects XDG_RUNTIME_DIR to be set"); | |
| abort(); | |
| } |
| if (interface == "wl_compositor"sv) | ||
| { | ||
| globals::compositor = static_cast<wl_compositor*>(wl_registry_bind( | ||
| registry, id, &wl_compositor_interface, std::min(version, 3u))); | ||
| } | ||
| else if (strcmp(interface, "wl_shm") == 0) | ||
| else if (interface == "wl_shm"sv) | ||
| { |
There was a problem hiding this comment.
This block uses the
"..."svstring_view literal andstd::string_view{...}but the file doesn't include<string_view>nor importstd::string_view_literals, so it will not compile. Add#include <string_view>andusing namespace std::string_view_literals;(or avoidsvby comparing viastd::string_view{interface} == "wl_compositor").
This only compiles because <string> transitively includes <string_view> in the current libstcc++ implementation. (Which then means using namespace std::literals pulls in string_view_literals.)
There's no guarantee this will work with other library implementations (nor future versions of this one).
|
@sivansh11 have you abandoned this? You've not responded to review comments for a couple of weeks:
Closing for now: feel free to reopen if you can address the above. |
Closes #4567
replaces error prone str(n)cmp with more expressive std::string_view