list: Extract RequireFunction helper for command validation

This commit is contained in:
Mickaël Germain
2026-04-30 21:43:24 -07:00
parent 05c103d92a
commit 08288eb14a

View File

@@ -92,6 +92,16 @@ std::string OutputVarFor(cm::string_view prefix, cmMakefile& makefile)
return cmStrCat(prefix, hash, "_"); return cmStrCat(prefix, hash, "_");
} }
void RequireFunction(cmMakefile const& makefile,
std::string const& functionName,
std::string const& errorPrefix)
{
if (!makefile.GetState()->GetCommand(functionName)) {
throw cmList::transform_error(
cmStrCat(errorPrefix, ": unknown function \"", functionName, "\"."));
}
}
class PredicateEvaluator class PredicateEvaluator
{ {
public: public:
@@ -103,11 +113,7 @@ public:
, ErrorPrefix(std::move(errorPrefix)) , ErrorPrefix(std::move(errorPrefix))
, OutputVar(OutputVarFor("_cmake_predicate_out_", makefile)) , OutputVar(OutputVarFor("_cmake_predicate_out_", makefile))
{ {
if (!makefile.GetState()->GetCommand(this->FunctionName)) { RequireFunction(makefile, this->FunctionName, this->ErrorPrefix);
throw cmList::transform_error(cmStrCat(this->ErrorPrefix,
": unknown function \"",
this->FunctionName, "\"."));
}
} }
bool operator()(std::string const& value) bool operator()(std::string const& value)
@@ -176,11 +182,8 @@ public:
, Makefile(&makefile) , Makefile(&makefile)
, OutputVar(OutputVarFor("_cmake_comparator_out_", makefile)) , OutputVar(OutputVarFor("_cmake_comparator_out_", makefile))
{ {
if (!makefile.GetState()->GetCommand(this->FunctionName)) { RequireFunction(makefile, this->FunctionName,
throw cmList::transform_error( "sub-command SORT, COMPARATOR");
cmStrCat("sub-command SORT, COMPARATOR: unknown function \"",
this->FunctionName, "\"."));
}
} }
bool operator()(std::string const& a, std::string const& b) bool operator()(std::string const& a, std::string const& b)
@@ -798,12 +801,8 @@ public:
this->Makefile = &makefile; this->Makefile = &makefile;
this->OutputVar = OutputVarFor("_cmake_transform_apply_out_", makefile); this->OutputVar = OutputVarFor("_cmake_transform_apply_out_", makefile);
// Validate: command must exist RequireFunction(makefile, this->FunctionName,
if (!makefile.GetState()->GetCommand(this->FunctionName)) { "sub-command TRANSFORM, action APPLY");
throw transform_error(
cmStrCat("sub-command TRANSFORM, action APPLY: unknown function \"",
this->FunctionName, "\"."));
}
} }
void Initialize(TransformSelector* /*selector*/, void Initialize(TransformSelector* /*selector*/,