From e5b92a39116791cfd4e1f4931221c9f69125bae3 Mon Sep 17 00:00:00 2001 From: Robert Schumacher Date: Wed, 19 Jun 2019 11:49:57 -0700 Subject: [vcpkg] Improve vcpkg::Files::Filesystem error handling (#6919) * [vcpkg] Modify Filesystem::remove and Filesystem::rename to not throw. * [.gitignore] Ignore new VS2019 CMake integration default location * [.gitignore] Ignore CMakeSettings.json in toolsrc * [vcpkg] Time external processes called with System::cmd_execute * [vcpkg] Work around VS2019 CMake bug * [vcpkg] Fix several unused variable warnings. * [vcpkg] Improve error handling in vcpkg::Files::Filesystem Always require either std::error_code or LineInfo to print better errors. * [vcpkg] Fixup missing return value. Drive by fix: silence warnings in tests. * [vcpkg] Fix exiting in error_code overload Drive by fixes for /analyze with VS2019 --- toolsrc/include/tests.utils.h | 8 ++++---- toolsrc/include/vcpkg/base/files.h | 14 ++++++++------ toolsrc/include/vcpkg/base/graphs.h | 4 ++-- toolsrc/include/vcpkg/base/optional.h | 23 +++++++++++++++++++---- toolsrc/include/vcpkg/base/strings.h | 2 +- 5 files changed, 34 insertions(+), 17 deletions(-) (limited to 'toolsrc/include') diff --git a/toolsrc/include/tests.utils.h b/toolsrc/include/tests.utils.h index 7f7ec9e88..3e8514e67 100644 --- a/toolsrc/include/tests.utils.h +++ b/toolsrc/include/tests.utils.h @@ -13,7 +13,7 @@ namespace Microsoft::VisualStudio::CppUnitTestFramework { template<> - std::wstring ToString(const vcpkg::Dependencies::InstallPlanType& t) + inline std::wstring ToString(const vcpkg::Dependencies::InstallPlanType& t) { switch (t) { @@ -26,7 +26,7 @@ namespace Microsoft::VisualStudio::CppUnitTestFramework } template<> - std::wstring ToString(const vcpkg::Dependencies::RequestType& t) + inline std::wstring ToString(const vcpkg::Dependencies::RequestType& t) { switch (t) { @@ -38,13 +38,13 @@ namespace Microsoft::VisualStudio::CppUnitTestFramework } template<> - std::wstring ToString(const vcpkg::PackageSpecParseResult& t) + inline std::wstring ToString(const vcpkg::PackageSpecParseResult& t) { return ToString(static_cast(t)); } template<> - std::wstring ToString(const vcpkg::PackageSpec& t) + inline std::wstring ToString(const vcpkg::PackageSpec& t) { return ToString(t.to_string()); } diff --git a/toolsrc/include/vcpkg/base/files.h b/toolsrc/include/vcpkg/base/files.h index b07ff25b3..02e2b8db8 100644 --- a/toolsrc/include/vcpkg/base/files.h +++ b/toolsrc/include/vcpkg/base/files.h @@ -27,21 +27,25 @@ namespace vcpkg::Files { struct Filesystem { + std::string read_contents(const fs::path& file_path, LineInfo linfo) const; virtual Expected read_contents(const fs::path& file_path) const = 0; virtual Expected> read_lines(const fs::path& file_path) const = 0; virtual fs::path find_file_recursively_up(const fs::path& starting_dir, const std::string& filename) const = 0; virtual std::vector get_files_recursive(const fs::path& dir) const = 0; virtual std::vector get_files_non_recursive(const fs::path& dir) const = 0; - - virtual void write_lines(const fs::path& file_path, const std::vector& lines) = 0; + void write_lines(const fs::path& file_path, const std::vector& lines, LineInfo linfo); + virtual void write_lines(const fs::path& file_path, + const std::vector& lines, + std::error_code& ec) = 0; + void write_contents(const fs::path& path, const std::string& data, LineInfo linfo); virtual void write_contents(const fs::path& file_path, const std::string& data, std::error_code& ec) = 0; - virtual void rename(const fs::path& oldpath, const fs::path& newpath) = 0; + void rename(const fs::path& oldpath, const fs::path& newpath, LineInfo linfo); virtual void rename(const fs::path& oldpath, const fs::path& newpath, std::error_code& ec) = 0; virtual void rename_or_copy(const fs::path& oldpath, const fs::path& newpath, StringLiteral temp_suffix, std::error_code& ec) = 0; - virtual bool remove(const fs::path& path) = 0; + bool remove(const fs::path& path, LineInfo linfo); virtual bool remove(const fs::path& path, std::error_code& ec) = 0; virtual std::uintmax_t remove_all(const fs::path& path, std::error_code& ec) = 0; virtual bool exists(const fs::path& path) const = 0; @@ -60,8 +64,6 @@ namespace vcpkg::Files virtual fs::file_status symlink_status(const fs::path& path, std::error_code& ec) const = 0; virtual std::vector find_from_PATH(const std::string& name) const = 0; - - void write_contents(const fs::path& file_path, const std::string& data); }; Filesystem& get_real_filesystem(); diff --git a/toolsrc/include/vcpkg/base/graphs.h b/toolsrc/include/vcpkg/base/graphs.h index 46831a911..9901a49a0 100644 --- a/toolsrc/include/vcpkg/base/graphs.h +++ b/toolsrc/include/vcpkg/base/graphs.h @@ -44,9 +44,9 @@ namespace vcpkg::Graphs void shuffle(Container& c, Randomizer* r) { if (!r) return; - for (int i = static_cast(c.size()); i > 1; --i) + for (auto i = c.size(); i > 1; --i) { - auto j = r->random(i); + auto j = r->random(static_cast(i)); if (j != i - 1) { std::swap(c[i - 1], c[j]); diff --git a/toolsrc/include/vcpkg/base/optional.h b/toolsrc/include/vcpkg/base/optional.h index 4d386a961..2d8c126c6 100644 --- a/toolsrc/include/vcpkg/base/optional.h +++ b/toolsrc/include/vcpkg/base/optional.h @@ -19,6 +19,9 @@ namespace vcpkg template::value> struct OptionalStorage { +#if defined(_WIN32) +#pragma warning(suppress : 26495) +#endif constexpr OptionalStorage() noexcept : m_is_present(false), m_inactive() {} constexpr OptionalStorage(const T& t) : m_is_present(true), m_t(t) {} constexpr OptionalStorage(T&& t) : m_is_present(true), m_t(std::move(t)) {} @@ -28,12 +31,18 @@ namespace vcpkg if (m_is_present) m_t.~T(); } +#if defined(_WIN32) +#pragma warning(suppress : 26495) +#endif OptionalStorage(const OptionalStorage& o) : m_is_present(o.m_is_present), m_inactive() { if (m_is_present) new (&m_t) T(o.m_t); } - OptionalStorage(OptionalStorage&& o) : m_is_present(o.m_is_present), m_inactive() +#if defined(_WIN32) +#pragma warning(suppress : 26495) +#endif + OptionalStorage(OptionalStorage&& o) noexcept : m_is_present(o.m_is_present), m_inactive() { if (m_is_present) { @@ -59,7 +68,7 @@ namespace vcpkg return *this; } - OptionalStorage& operator=(OptionalStorage&& o) + OptionalStorage& operator=(OptionalStorage&& o) noexcept { if (m_is_present && o.m_is_present) { @@ -100,6 +109,9 @@ namespace vcpkg template struct OptionalStorage { +#if defined(_WIN32) +#pragma warning(suppress : 26495) +#endif constexpr OptionalStorage() noexcept : m_is_present(false), m_inactive() {} constexpr OptionalStorage(T&& t) : m_is_present(true), m_t(std::move(t)) {} @@ -108,7 +120,10 @@ namespace vcpkg if (m_is_present) m_t.~T(); } - OptionalStorage(OptionalStorage&& o) : m_is_present(o.m_is_present), m_inactive() +#if defined(_WIN32) +#pragma warning(suppress : 26495) +#endif + OptionalStorage(OptionalStorage&& o) noexcept : m_is_present(o.m_is_present), m_inactive() { if (m_is_present) { @@ -116,7 +131,7 @@ namespace vcpkg } } - OptionalStorage& operator=(OptionalStorage&& o) + OptionalStorage& operator=(OptionalStorage&& o) noexcept { if (m_is_present && o.m_is_present) { diff --git a/toolsrc/include/vcpkg/base/strings.h b/toolsrc/include/vcpkg/base/strings.h index 3a9de97df..d553d1d41 100644 --- a/toolsrc/include/vcpkg/base/strings.h +++ b/toolsrc/include/vcpkg/base/strings.h @@ -163,7 +163,7 @@ namespace vcpkg::Strings std::vector split(const std::string& s, const std::string& delimiter); - std::vector split(const std::string& s, const std::string& delimiter, int max_count); + std::vector split(const std::string& s, const std::string& delimiter, size_t max_count); std::vector find_all_enclosed(StringView input, StringView left_delim, StringView right_delim); -- cgit v1.2.3