From ea28b30e04fc4f1a2e14f91152b8bba396ba962a Mon Sep 17 00:00:00 2001 From: Matthew Woehlke Date: Sat, 23 Nov 2024 16:18:24 -0500 Subject: [PATCH] find_package: Allow cmDirectoryListGenerator::Names to be nullptr Change cmDirectoryListGenerator::Names to be a pointer rather than a reference wrapper. This allows it to be a null pointer, which allows cmAnyDirectoryListGenerator to pass a nullptr rather than needing to construct and hold an empty list just to satisfy the reference being non-null. Importantly, it also make it more obvious that anyone constructing a cmDirectoryListGenerator or subcless thereof needs to pass something that isn't going to immediately go out of scope. --- Source/cmFindPackageCommand.cxx | 28 +++++++++++++--------------- 1 file changed, 13 insertions(+), 15 deletions(-) diff --git a/Source/cmFindPackageCommand.cxx b/Source/cmFindPackageCommand.cxx index ebec44d270..b204725535 100644 --- a/Source/cmFindPackageCommand.cxx +++ b/Source/cmFindPackageCommand.cxx @@ -206,12 +206,14 @@ private: class cmDirectoryListGenerator { public: - cmDirectoryListGenerator(std::vector const& names, + cmDirectoryListGenerator(std::vector const* names, bool exactMatch) : Names{ names } , ExactMatch{ exactMatch } , Current{ this->Matches.cbegin() } { + assert(names || !exactMatch); + assert(!names || !names->empty()); } virtual ~cmDirectoryListGenerator() = default; @@ -238,10 +240,10 @@ public: continue; } - if (!this->ExactMatch && this->Names.get().empty()) { + if (!this->Names) { this->Matches.emplace_back(fname); } else { - for (const auto& n : this->Names.get()) { + for (const auto& n : *this->Names) { // NOTE Customization point for // `cmMacProjectDirectoryListGenerator` const auto name = this->TransformNameBeforeCmp(n); @@ -282,7 +284,7 @@ protected: virtual void OnMatchesLoaded() {} virtual std::string TransformNameBeforeCmp(std::string same) { return same; } - std::reference_wrapper> Names; + std::vector const* Names; bool const ExactMatch; std::vector Matches; std::vector::const_iterator Current; @@ -291,7 +293,7 @@ protected: class cmProjectDirectoryListGenerator : public cmDirectoryListGenerator { public: - cmProjectDirectoryListGenerator(std::vector const& names, + cmProjectDirectoryListGenerator(std::vector const* names, cmFindPackageCommand::SortOrderType so, cmFindPackageCommand::SortDirectionType sd, bool exactMatch) @@ -320,7 +322,7 @@ private: class cmMacProjectDirectoryListGenerator : public cmDirectoryListGenerator { public: - cmMacProjectDirectoryListGenerator(const std::vector& names, + cmMacProjectDirectoryListGenerator(std::vector const* names, cm::string_view ext) : cmDirectoryListGenerator{ names, true } , Extension{ ext } @@ -342,13 +344,9 @@ class cmAnyDirectoryListGenerator : public cmProjectDirectoryListGenerator public: cmAnyDirectoryListGenerator(cmFindPackageCommand::SortOrderType so, cmFindPackageCommand::SortDirectionType sd) - : cmProjectDirectoryListGenerator(this->EmptyNamesList, so, sd, false) + : cmProjectDirectoryListGenerator(nullptr, so, sd, false) { } - -private: - // NOTE `cmDirectoryListGenerator` needs to hold a reference to this - std::vector EmptyNamesList; }; #if defined(__LCC__) @@ -2634,7 +2632,7 @@ bool cmFindPackageCommand::SearchPrefix(std::string const& prefix_in) auto iCMakeGen = cmCaseInsensitiveDirectoryListGenerator{ "cmake"_s }; auto firstPkgDirGen = - cmProjectDirectoryListGenerator{ this->Names, this->SortOrder, + cmProjectDirectoryListGenerator{ &this->Names, this->SortOrder, this->SortDirection, false }; // PREFIX/(cmake|CMake)/ (useful on windows or in build trees) @@ -2653,7 +2651,7 @@ bool cmFindPackageCommand::SearchPrefix(std::string const& prefix_in) } auto secondPkgDirGen = - cmProjectDirectoryListGenerator{ this->Names, this->SortOrder, + cmProjectDirectoryListGenerator{ &this->Names, this->SortOrder, this->SortDirection, false }; // PREFIX/(Foo|foo|FOO).*/(cmake|CMake)/(Foo|foo|FOO).*/ @@ -2730,7 +2728,7 @@ bool cmFindPackageCommand::SearchFrameworkPrefix(std::string const& prefix_in) auto iCMakeGen = cmCaseInsensitiveDirectoryListGenerator{ "cmake"_s }; auto fwGen = - cmMacProjectDirectoryListGenerator{ this->Names, ".framework"_s }; + cmMacProjectDirectoryListGenerator{ &this->Names, ".framework"_s }; auto rGen = cmAppendPathSegmentGenerator{ "Resources"_s }; auto vGen = cmAppendPathSegmentGenerator{ "Versions"_s }; auto anyGen = @@ -2768,7 +2766,7 @@ bool cmFindPackageCommand::SearchAppBundlePrefix(std::string const& prefix_in) return this->SearchDirectory(fullPath); }; - auto appGen = cmMacProjectDirectoryListGenerator{ this->Names, ".app"_s }; + auto appGen = cmMacProjectDirectoryListGenerator{ &this->Names, ".app"_s }; auto crGen = cmAppendPathSegmentGenerator{ "Contents/Resources"_s }; // /Foo.app/Contents/Resources