cmMakefile: Use enumeration for policy scope

Create `cmPolicyScope` to describe whether `ReadDependentFile` creates a
policy scope, rather than a bare `bool`. Replace a local enumeration in
`cmFindPackageCommand` serving the same purpose with this new, global
one.
This commit is contained in:
Matthew Woehlke
2026-03-13 16:44:09 -04:00
parent 66a5deb5cf
commit d80a38e524
5 changed files with 33 additions and 25 deletions

View File

@@ -1581,7 +1581,7 @@ bool cmFindPackageCommand::FindModule(bool& found)
found = true;
std::string const var = cmStrCat(this->Name, "_FIND_MODULE");
this->Makefile->AddDefinition(var, "1");
bool result = this->ReadListFile(mfile, DoPolicyScope);
bool result = this->ReadListFile(mfile, cm::PolicyScope::Local);
this->Makefile->RemoveDefinition(var);
std::string const foundVar = cmStrCat(this->Name, "_FOUND");
@@ -1705,7 +1705,7 @@ bool cmFindPackageCommand::HandlePackageMode(
// The package has been found.
found = true;
result = this->ReadPackage();
} else if (this->ReadListFile(this->FileFound, DoPolicyScope)) {
} else if (this->ReadListFile(this->FileFound, cm::PolicyScope::Local)) {
// The package has been found.
found = true;
@@ -2071,9 +2071,11 @@ cmFindPackageCommand::AppendixMap cmFindPackageCommand::FindAppendices(
}
bool cmFindPackageCommand::ReadListFile(std::string const& f,
PolicyScopeRule const psr)
cm::PolicyScope ps)
{
bool const noPolicyScope = !this->PolicyScope || psr == NoPolicyScope;
if (!this->PolicyScope) {
ps = cm::PolicyScope::None;
}
using ITScope = cmMakefile::ImportedTargetScope;
ITScope scope = this->GlobalScope ? ITScope::Global : ITScope::Local;
@@ -2084,7 +2086,7 @@ bool cmFindPackageCommand::ReadListFile(std::string const& f,
// This allows child snapshots to inherit the CAN_UNWIND state from us, we'll
// reset it immediately after the dependent file is done
this->Makefile->GetStateSnapshot().SetUnwindType(cmStateEnums::CAN_UNWIND);
bool result = this->Makefile->ReadDependentFile(f, noPolicyScope);
bool result = this->Makefile->ReadDependentFile(f, ps);
this->Makefile->GetStateSnapshot().SetUnwindType(oldUnwind);
this->Makefile->GetStateSnapshot().SetUnwindState(
@@ -3205,7 +3207,7 @@ bool cmFindPackageCommand::CheckVersionFile(std::string const& version_file,
// Load the version check file.
// Pass NoPolicyScope because we do our own policy push/pop.
bool suitable = false;
if (this->ReadListFile(version_file, NoPolicyScope)) {
if (this->ReadListFile(version_file, cm::PolicyScope::None)) {
// Check the output variables.
bool okay = this->Makefile->IsOn("PACKAGE_VERSION_EXACT");
bool const unsuitable = this->Makefile->IsOn("PACKAGE_VERSION_UNSUITABLE");

View File

@@ -32,6 +32,10 @@ namespace std {
/* clang-format on */
#endif
namespace cm {
enum class PolicyScope : bool;
}
class cmExecutionStatus;
class cmPackageState;
class cmSearchPath;
@@ -139,12 +143,7 @@ private:
bool FindFrameworkConfig();
bool FindAppBundleConfig();
bool FindEnvironmentConfig();
enum PolicyScopeRule
{
NoPolicyScope,
DoPolicyScope
};
bool ReadListFile(std::string const& f, PolicyScopeRule psr);
bool ReadListFile(std::string const& f, cm::PolicyScope ps);
bool ReadPackage();
struct Appendix

View File

@@ -48,7 +48,7 @@ bool cmIncludeCommand(std::vector<std::string> const& args,
}
bool optional = false;
bool noPolicyScope = false;
cm::PolicyScope policyScope = cm::PolicyScope::Local;
std::string fname = args[0];
std::string resultVarName;
@@ -72,7 +72,7 @@ bool cmIncludeCommand(std::vector<std::string> const& args,
return false;
}
} else if (args[i] == "NO_POLICY_SCOPE") {
noPolicyScope = true;
policyScope = cm::PolicyScope::None;
} else if (i > 1) // compat.: in previous cmake versions the second
// parameter was ignored if it wasn't "OPTIONAL"
{
@@ -161,8 +161,7 @@ bool cmIncludeCommand(std::vector<std::string> const& args,
}
}
bool readit =
status.GetMakefile().ReadDependentFile(listFile, noPolicyScope);
bool readit = status.GetMakefile().ReadDependentFile(listFile, policyScope);
// add the location of the included file if a result variable was given
if (!resultVarName.empty()) {

View File

@@ -620,7 +620,7 @@ class cmMakefile::IncludeScope : public FileScopeBase
{
public:
IncludeScope(cmMakefile* mf, std::string const& filenametoread,
bool noPolicyScope);
cm::PolicyScope policyScope);
~IncludeScope();
void Quiet() { this->ReportError = false; }
@@ -628,15 +628,15 @@ public:
IncludeScope& operator=(IncludeScope const&) = delete;
private:
bool NoPolicyScope;
cm::PolicyScope PolicyScope;
bool ReportError = true;
};
cmMakefile::IncludeScope::IncludeScope(cmMakefile* mf,
std::string const& filenametoread,
bool noPolicyScope)
cm::PolicyScope policyScope)
: FileScopeBase(mf)
, NoPolicyScope(noPolicyScope)
, PolicyScope(policyScope)
{
this->Makefile->Backtrace = this->Makefile->Backtrace.Push(
cmListFileContext::FromListFilePath(filenametoread));
@@ -646,7 +646,7 @@ cmMakefile::IncludeScope::IncludeScope(cmMakefile* mf,
this->Makefile->StateSnapshot =
this->Makefile->GetState()->CreateIncludeFileSnapshot(
this->Makefile->StateSnapshot, filenametoread);
if (!this->NoPolicyScope) {
if (this->PolicyScope == cm::PolicyScope::Local) {
this->Makefile->PushPolicy();
}
this->PushListFileVars(filenametoread);
@@ -655,7 +655,7 @@ cmMakefile::IncludeScope::IncludeScope(cmMakefile* mf,
cmMakefile::IncludeScope::~IncludeScope()
{
this->PopListFileVars();
if (!this->NoPolicyScope) {
if (this->PolicyScope == cm::PolicyScope::Local) {
// Pop the scope we pushed for the script.
this->Makefile->PopPolicy();
}
@@ -667,12 +667,12 @@ cmMakefile::IncludeScope::~IncludeScope()
}
bool cmMakefile::ReadDependentFile(std::string const& filename,
bool noPolicyScope)
cm::PolicyScope policyScope)
{
std::string filenametoread = cmSystemTools::CollapseFullPath(
filename, this->GetCurrentSourceDirectory());
IncludeScope incScope(this, filenametoread, noPolicyScope);
IncludeScope incScope(this, filenametoread, policyScope);
#ifdef CMake_ENABLE_DEBUGGER
if (this->GetCMakeInstance()->GetDebugAdapter()) {

View File

@@ -64,6 +64,14 @@ class cmTestGenerator;
class cmVariableWatch;
class cmake;
namespace cm {
enum class PolicyScope : bool
{
None,
Local,
};
}
/** A type-safe wrapper for a string representing a directory id. */
class cmDirectoryId
{
@@ -109,7 +117,7 @@ public:
std::string const& virtualFileName);
bool ReadDependentFile(std::string const& filename,
bool noPolicyScope = true);
cm::PolicyScope policyScope = cm::PolicyScope::None);
/**
* Add a function blocker to this makefile