From 04870ac77e568cbd8609974a9914e0f6330cb008 Mon Sep 17 00:00:00 2001 From: Joel Rosdahl Date: Sun, 27 Feb 2022 20:11:47 +0100 Subject: [PATCH] refactor: Make Util::is_absolute_path_with_prefix return optional --- src/Util.cpp | 50 +++++++++++++++++++++--------------------- src/Util.hpp | 8 +++---- src/argprocessing.cpp | 10 ++++----- unittest/test_Util.cpp | 37 +++++++++++++------------------ 4 files changed, 49 insertions(+), 56 deletions(-) diff --git a/src/Util.cpp b/src/Util.cpp index 007006cb2..146ae7a9a 100644 --- a/src/Util.cpp +++ b/src/Util.cpp @@ -233,31 +233,6 @@ base_name(string_view path) return n == std::string::npos ? path : path.substr(n + 1); } -bool -is_absolute_path_with_prefix(nonstd::string_view path, size_t& split_pos) -{ -#ifdef _WIN32 - const char delim[] = "/\\"; -#else - const char delim[] = "/"; -#endif - split_pos = path.find_first_of(delim); - if (split_pos != std::string::npos) { -#ifdef _WIN32 - // -I/C:/foo and -I/c/foo will already be handled by delim_pos - // correctly resulting in -I and /C:/foo or /c/foo respectively. - // -IC:/foo will not as we would get -IC: and /foo - if (split_pos > 0 && path[split_pos - 1] == ':') { - split_pos = split_pos - 2; - } -#endif - // this is not redundant on some platforms so nothing to simplify - // NOLINTNEXTLINE(readability-simplify-boolean-expr) - return true; - } - return false; -} - std::string change_extension(string_view path, string_view new_ext) { @@ -808,6 +783,31 @@ hard_link(const std::string& oldpath, const std::string& newpath) #endif } +nonstd::optional +is_absolute_path_with_prefix(nonstd::string_view path) +{ +#ifdef _WIN32 + const char delim[] = "/\\"; +#else + const char delim[] = "/"; +#endif + auto split_pos = path.find_first_of(delim); + if (split_pos != std::string::npos) { +#ifdef _WIN32 + // -I/C:/foo and -I/c/foo will already be handled by delim_pos correctly + // resulting in -I and /C:/foo or /c/foo respectively. -IC:/foo will not as + // we would get -IC: and /foo. + if (split_pos > 0 && path[split_pos - 1] == ':') { + split_pos = split_pos - 2; + } +#endif + // This is not redundant on some platforms, so nothing to simplify. + // NOLINTNEXTLINE(readability-simplify-boolean-expr) + return split_pos; + } + return nonstd::nullopt; +} + #if defined(HAVE_LINUX_FS_H) || defined(HAVE_STRUCT_STATFS_F_FSTYPENAME) int is_nfs_fd(int fd, bool* is_nfs) diff --git a/src/Util.hpp b/src/Util.hpp index b92a3d5ca..9ad678c7b 100644 --- a/src/Util.hpp +++ b/src/Util.hpp @@ -46,10 +46,6 @@ enum class UnlinkLog { log_failure, ignore_failure }; // Get base name of path. nonstd::string_view base_name(nonstd::string_view path); -// Determine if `path` is an absolute path with prefix, returning the split -// point -bool is_absolute_path_with_prefix(nonstd::string_view path, size_t& split_pos); - // Get an integer value from bytes in big endian order. // // Parameters: @@ -229,6 +225,10 @@ int_to_big_endian(int8_t value, uint8_t* buffer) buffer[0] = value; } +// Determine if `path` is an absolute path with prefix, returning the split +// point. +nonstd::optional is_absolute_path_with_prefix(nonstd::string_view path); + // Test if a file is on nfs. // // Sets is_nfs to the result if fstatfs is available and no error occurred. diff --git a/src/argprocessing.cpp b/src/argprocessing.cpp index 345cb9fa5..f8d90198b 100644 --- a/src/argprocessing.cpp +++ b/src/argprocessing.cpp @@ -919,12 +919,12 @@ process_arg(const Context& ctx, // Potentially rewrite concatenated absolute path argument to relative. if (args[i][0] == '-') { - size_t slash_pos = 0; - if (Util::is_absolute_path_with_prefix(args[i], slash_pos)) { - std::string option = args[i].substr(0, slash_pos); + const auto slash_pos = Util::is_absolute_path_with_prefix(args[i]); + if (slash_pos) { + std::string option = args[i].substr(0, *slash_pos); if (compopt_takes_concat_arg(option) && compopt_takes_path(option)) { - auto relpath = - Util::make_relative_path(ctx, string_view(args[i]).substr(slash_pos)); + auto relpath = Util::make_relative_path( + ctx, string_view(args[i]).substr(*slash_pos)); std::string new_option = option + relpath; if (compopt_affects_cpp_output(option)) { state.cpp_args.push_back(new_option); diff --git a/unittest/test_Util.cpp b/unittest/test_Util.cpp index f1945d63a..913b73624 100644 --- a/unittest/test_Util.cpp +++ b/unittest/test_Util.cpp @@ -1,4 +1,4 @@ -// Copyright (C) 2019-2021 Joel Rosdahl and other contributors +// Copyright (C) 2019-2022 Joel Rosdahl and other contributors // // See doc/AUTHORS.adoc for a complete list of contributors. // @@ -52,27 +52,6 @@ TEST_CASE("Util::base_name") CHECK(Util::base_name("/foo/bar/f.txt") == "f.txt"); } -TEST_CASE("Util::is_absolute_path_with_prefix") -{ - size_t delim_pos = 0; - CHECK(Util::is_absolute_path_with_prefix("-I/c/foo", delim_pos)); - CHECK(delim_pos == 2); - CHECK(Util::is_absolute_path_with_prefix("-W,path/c/foo", delim_pos)); - CHECK(delim_pos == 7); - CHECK(!Util::is_absolute_path_with_prefix("-DMACRO", delim_pos)); -#ifdef _WIN32 - CHECK(Util::is_absolute_path_with_prefix("-I/C:/foo", delim_pos)); - CHECK(delim_pos == 2); - CHECK(Util::is_absolute_path_with_prefix("-IC:/foo", delim_pos)); - CHECK(delim_pos == 2); - CHECK(Util::is_absolute_path_with_prefix("-W,path/c:/foo", delim_pos)); - CHECK(delim_pos == 7); - CHECK(Util::is_absolute_path_with_prefix("-W,pathc:/foo", delim_pos)); - CHECK(delim_pos == 7); - CHECK(!Util::is_absolute_path_with_prefix("-opt:value", delim_pos)); -#endif -} - TEST_CASE("Util::big_endian_to_int") { uint8_t bytes[8] = {0x70, 0x9e, 0x9a, 0xbc, 0xd6, 0x54, 0x4b, 0xca}; @@ -457,6 +436,20 @@ TEST_CASE("Util::int_to_big_endian") CHECK(bytes[7] == 0xca); } +TEST_CASE("Util::is_absolute_path_with_prefix") +{ + CHECK(*Util::is_absolute_path_with_prefix("-I/c/foo") == 2); + CHECK(*Util::is_absolute_path_with_prefix("-W,path/c/foo") == 7); + CHECK(!Util::is_absolute_path_with_prefix("-DMACRO")); +#ifdef _WIN32 + CHECK(*Util::is_absolute_path_with_prefix("-I/C:/foo") == 2); + CHECK(*Util::is_absolute_path_with_prefix("-IC:/foo") == 2); + CHECK(*Util::is_absolute_path_with_prefix("-W,path/c:/foo") == 7); + CHECK(*Util::is_absolute_path_with_prefix("-W,pathc:/foo") == 7); + CHECK(!Util::is_absolute_path_with_prefix("-opt:value")); +#endif +} + TEST_CASE("Util::is_dir_separator") { CHECK(!Util::is_dir_separator('x')); -- 2.47.2