From: Joel Rosdahl Date: Sun, 4 Jul 2021 04:59:07 +0000 (+0200) Subject: Move is_absolute_path to util X-Git-Tag: v4.4~162 X-Git-Url: http://git.ipfire.org/?a=commitdiff_plain;h=c3559353525c1ed5b368a49220ed10a731b7eeba;p=thirdparty%2Fccache.git Move is_absolute_path to util --- diff --git a/src/Config.cpp b/src/Config.cpp index 2163c68b3..7eed171f1 100644 --- a/src/Config.cpp +++ b/src/Config.cpp @@ -27,6 +27,7 @@ #include "exceptions.hpp" #include "fmtmacros.hpp" +#include #include #include "third_party/fmt/core.h" @@ -349,7 +350,7 @@ format_umask(nonstd::optional umask) void verify_absolute_path(const std::string& value) { - if (!Util::is_absolute_path(value)) { + if (!util::is_absolute_path(value)) { throw Error("not an absolute path: \"{}\"", value); } } diff --git a/src/Depfile.cpp b/src/Depfile.cpp index 13106ce7f..ec49f4418 100644 --- a/src/Depfile.cpp +++ b/src/Depfile.cpp @@ -23,6 +23,8 @@ #include "Logging.hpp" #include "assertions.hpp" +#include + static inline bool is_blank(const std::string& s) { @@ -79,7 +81,7 @@ rewrite_paths(const Context& ctx, const std::string& file_content) const auto& token = tokens[i]; bool token_rewritten = false; - if (Util::is_absolute_path(token)) { + if (util::is_absolute_path(token)) { const auto new_path = Util::make_relative_path(ctx, token); if (new_path != token) { adjusted_file_content.append(new_path); diff --git a/src/Result.cpp b/src/Result.cpp index 86ba5a24a..3633c2cba 100644 --- a/src/Result.cpp +++ b/src/Result.cpp @@ -32,6 +32,8 @@ #include "exceptions.hpp" #include "fmtmacros.hpp" +#include + #include // Result data format @@ -182,7 +184,7 @@ gcno_file_in_mangled_form(const Context& ctx) { const auto& output_obj = ctx.args_info.output_obj; const std::string abs_output_obj = - Util::is_absolute_path(output_obj) + util::is_absolute_path(output_obj) ? output_obj : FMT("{}/{}", ctx.apparent_cwd, output_obj); std::string hashified_obj = abs_output_obj; diff --git a/src/Util.cpp b/src/Util.cpp index 92bf7892a..e4ad44673 100644 --- a/src/Util.cpp +++ b/src/Util.cpp @@ -27,6 +27,7 @@ #include "fmtmacros.hpp" #include +#include extern "C" { #include "third_party/base32hex.h" @@ -605,7 +606,7 @@ get_apparent_cwd(const std::string& actual_cwd) return actual_cwd; #else auto pwd = getenv("PWD"); - if (!pwd || !Util::is_absolute_path(pwd)) { + if (!pwd || !util::is_absolute_path(pwd)) { return actual_cwd; } @@ -716,8 +717,8 @@ get_hostname() std::string get_relative_path(string_view dir, string_view path) { - ASSERT(Util::is_absolute_path(dir)); - ASSERT(Util::is_absolute_path(path)); + ASSERT(util::is_absolute_path(dir)); + ASSERT(util::is_absolute_path(path)); #ifdef _WIN32 // Paths can be escaped by a slash for use with e.g. -isystem. @@ -802,18 +803,6 @@ hard_link(const std::string& oldpath, const std::string& newpath) #endif } -bool -is_absolute_path(string_view path) -{ -#ifdef _WIN32 - if (path.length() >= 2 && path[1] == ':' - && (path[2] == '/' || path[2] == '\\')) { - return true; - } -#endif - return !path.empty() && path[0] == '/'; -} - #if defined(HAVE_LINUX_FS_H) || defined(HAVE_STRUCT_STATFS_F_FSTYPENAME) int is_nfs_fd(int fd, bool* is_nfs) @@ -946,7 +935,7 @@ matches_dir_prefix_or_file(string_view dir_prefix_or_file, string_view path) std::string normalize_absolute_path(string_view path) { - if (!is_absolute_path(path)) { + if (!util::is_absolute_path(path)) { return std::string(path); } diff --git a/src/Util.hpp b/src/Util.hpp index e7ca46009..6e3ed4a46 100644 --- a/src/Util.hpp +++ b/src/Util.hpp @@ -265,9 +265,6 @@ int_to_big_endian(int8_t value, uint8_t* buffer) buffer[0] = value; } -// Return whether `path` is absolute. -bool is_absolute_path(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/ccache.cpp b/src/ccache.cpp index f41a323fc..a7cc3a313 100644 --- a/src/ccache.cpp +++ b/src/ccache.cpp @@ -55,6 +55,7 @@ #include "language.hpp" #include +#include #include "third_party/fmt/core.h" #include "third_party/nonstd/optional.hpp" @@ -277,7 +278,7 @@ guess_compiler(string_view path) if (symlink_value.empty()) { break; } - if (Util::is_absolute_path(symlink_value)) { + if (util::is_absolute_path(symlink_value)) { compiler_path = symlink_value; } else { compiler_path = @@ -608,7 +609,7 @@ process_preprocessed_file(Context& ctx, // p and q span the include file path. std::string inc_path(p, q - p); if (!ctx.has_absolute_include_headers) { - ctx.has_absolute_include_headers = Util::is_absolute_path(inc_path); + ctx.has_absolute_include_headers = util::is_absolute_path(inc_path); } inc_path = Util::make_relative_path(ctx, inc_path); @@ -700,7 +701,7 @@ result_key_from_depfile(Context& ctx, Hash& hash) continue; } if (!ctx.has_absolute_include_headers) { - ctx.has_absolute_include_headers = Util::is_absolute_path(token); + ctx.has_absolute_include_headers = util::is_absolute_path(token); } std::string path = Util::make_relative_path(ctx, token); remember_include_file(ctx, path, hash, false, &hash); @@ -1651,7 +1652,7 @@ calculate_result_and_manifest_key(Context& ctx, // the profile filename so we need to include the same information in the // hash. const std::string profile_path = - Util::is_absolute_path(ctx.args_info.profile_path) + util::is_absolute_path(ctx.args_info.profile_path) ? ctx.args_info.profile_path : FMT("{}/{}", ctx.apparent_cwd, ctx.args_info.profile_path); LOG("Adding profile directory {} to our hash", profile_path); diff --git a/src/execute.cpp b/src/execute.cpp index ebcf2becb..86d0a8084 100644 --- a/src/execute.cpp +++ b/src/execute.cpp @@ -29,6 +29,8 @@ #include "Util.hpp" #include "fmtmacros.hpp" +#include + #ifdef _WIN32 # include "Finalizer.hpp" # include "Win32Util.hpp" @@ -241,7 +243,7 @@ find_executable(const Context& ctx, const std::string& name, const std::string& exclude_name) { - if (Util::is_absolute_path(name)) { + if (util::is_absolute_path(name)) { return name; } diff --git a/src/util/CMakeLists.txt b/src/util/CMakeLists.txt index a9e1f0438..669f92b07 100644 --- a/src/util/CMakeLists.txt +++ b/src/util/CMakeLists.txt @@ -1,7 +1,8 @@ set( sources - ${CMAKE_CURRENT_SOURCE_DIR}/file_utils.cpp ${CMAKE_CURRENT_SOURCE_DIR}/Tokenizer.cpp + ${CMAKE_CURRENT_SOURCE_DIR}/file_utils.cpp + ${CMAKE_CURRENT_SOURCE_DIR}/path_utils.cpp ${CMAKE_CURRENT_SOURCE_DIR}/string_utils.cpp ) diff --git a/src/util/path_utils.cpp b/src/util/path_utils.cpp new file mode 100644 index 000000000..ef0ffa388 --- /dev/null +++ b/src/util/path_utils.cpp @@ -0,0 +1,39 @@ +// Copyright (C) 2021 Joel Rosdahl and other contributors +// +// See doc/AUTHORS.adoc for a complete list of contributors. +// +// This program is free software; you can redistribute it and/or modify it +// under the terms of the GNU General Public License as published by the Free +// Software Foundation; either version 3 of the License, or (at your option) +// any later version. +// +// This program is distributed in the hope that it will be useful, but WITHOUT +// ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or +// FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for +// more details. +// +// You should have received a copy of the GNU General Public License along with +// this program; if not, write to the Free Software Foundation, Inc., 51 +// Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA + +#include "path_utils.hpp" + +#include +#include +#include + +namespace util { + +bool +is_absolute_path(nonstd::string_view path) +{ +#ifdef _WIN32 + if (path.length() >= 2 && path[1] == ':' + && (path[2] == '/' || path[2] == '\\')) { + return true; + } +#endif + return !path.empty() && path[0] == '/'; +} + +} // namespace util diff --git a/src/util/path_utils.hpp b/src/util/path_utils.hpp new file mode 100644 index 000000000..9423e57ba --- /dev/null +++ b/src/util/path_utils.hpp @@ -0,0 +1,30 @@ +// Copyright (C) 2021 Joel Rosdahl and other contributors +// +// See doc/AUTHORS.adoc for a complete list of contributors. +// +// This program is free software; you can redistribute it and/or modify it +// under the terms of the GNU General Public License as published by the Free +// Software Foundation; either version 3 of the License, or (at your option) +// any later version. +// +// This program is distributed in the hope that it will be useful, but WITHOUT +// ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or +// FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for +// more details. +// +// You should have received a copy of the GNU General Public License along with +// this program; if not, write to the Free Software Foundation, Inc., 51 +// Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA + +#pragma once + +#include + +#include + +namespace util { + +// Return whether `path` is absolute. +bool is_absolute_path(nonstd::string_view path); + +} // namespace util diff --git a/unittest/CMakeLists.txt b/unittest/CMakeLists.txt index 68e462986..2d5b2d870 100644 --- a/unittest/CMakeLists.txt +++ b/unittest/CMakeLists.txt @@ -23,6 +23,7 @@ set( test_hashutil.cpp test_util_Tokenizer.cpp test_util_string_utils.cpp + test_util_path_utils.cpp ) if(INODE_CACHE_SUPPORTED) diff --git a/unittest/test_Util.cpp b/unittest/test_Util.cpp index 92343b966..07cdc2183 100644 --- a/unittest/test_Util.cpp +++ b/unittest/test_Util.cpp @@ -531,22 +531,6 @@ TEST_CASE("Util::int_to_big_endian") CHECK(bytes[7] == 0xca); } -TEST_CASE("Util::is_absolute_path") -{ -#ifdef _WIN32 - CHECK(Util::is_absolute_path("C:/")); - CHECK(Util::is_absolute_path("C:\\foo/fie")); - CHECK(Util::is_absolute_path("/C:\\foo/fie")); // MSYS/Cygwin path - CHECK(!Util::is_absolute_path("")); - CHECK(!Util::is_absolute_path("foo\\fie/fum")); - CHECK(!Util::is_absolute_path("C:foo/fie")); -#endif - CHECK(Util::is_absolute_path("/")); - CHECK(Util::is_absolute_path("/foo/fie")); - CHECK(!Util::is_absolute_path("")); - CHECK(!Util::is_absolute_path("foo/fie")); -} - TEST_CASE("Util::is_dir_separator") { CHECK(!Util::is_dir_separator('x')); diff --git a/unittest/test_util_path_utils.cpp b/unittest/test_util_path_utils.cpp new file mode 100644 index 000000000..8f9e3f0ad --- /dev/null +++ b/unittest/test_util_path_utils.cpp @@ -0,0 +1,37 @@ +// Copyright (C) 2021 Joel Rosdahl and other contributors +// +// See doc/AUTHORS.adoc for a complete list of contributors. +// +// This program is free software; you can redistribute it and/or modify it +// under the terms of the GNU General Public License as published by the Free +// Software Foundation; either version 3 of the License, or (at your option) +// any later version. +// +// This program is distributed in the hope that it will be useful, but WITHOUT +// ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or +// FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for +// more details. +// +// You should have received a copy of the GNU General Public License along with +// this program; if not, write to the Free Software Foundation, Inc., 51 +// Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA + +#include + +#include + +TEST_CASE("util::is_absolute_path") +{ +#ifdef _WIN32 + CHECK(util::is_absolute_path("C:/")); + CHECK(util::is_absolute_path("C:\\foo/fie")); + CHECK(util::is_absolute_path("/C:\\foo/fie")); // MSYS/Cygwin path + CHECK(!util::is_absolute_path("")); + CHECK(!util::is_absolute_path("foo\\fie/fum")); + CHECK(!util::is_absolute_path("C:foo/fie")); +#endif + CHECK(util::is_absolute_path("/")); + CHECK(util::is_absolute_path("/foo/fie")); + CHECK(!util::is_absolute_path("")); + CHECK(!util::is_absolute_path("foo/fie")); +}