function: Set variable ARGNC

This commit is contained in:
Arha Gatram
2026-06-15 10:37:11 -07:00
parent d14d0b0b23
commit 82c9d33e96
6 changed files with 18 additions and 9 deletions

View File

@@ -78,6 +78,11 @@ variables are set in the scope of the function:
``ARGN``
The list of arguments past the last expected argument.
``ARGNC``
.. versionadded:: 4.5
The number of arguments past the last expected argument.
See Also
^^^^^^^^

View File

@@ -0,0 +1,5 @@
function-argnc
--------------
* The :command:`function` command now sets the ``ARGNC`` variable, which
holds the number of arguments past the last expected argument.

View File

@@ -25,7 +25,7 @@
namespace {
std::string const ARGC = "ARGC";
std::string const kFUNCTION_ARGNC = "_FUNCTION_ARGNC";
std::string const ARGNC = "ARGNC";
std::string const ARGN = "ARGN";
std::string const ARGV = "ARGV";
std::string const CMAKE_CURRENT_FUNCTION = "CMAKE_CURRENT_FUNCTION";
@@ -94,7 +94,7 @@ bool cmFunctionHelperCommand::operator()(
makefile.AddDefinition(this->Args[j], expandedArgs[j - 1]);
}
// define ARGV, ARGN, and _FUNCTION_ARGNC
// define ARGV, ARGN, and ARGNC
auto const argvDef = cmList::to_string(expandedArgs);
auto const expIt = expandedArgs.begin() + (this->Args.size() - 1);
auto const argnDef =
@@ -105,8 +105,8 @@ bool cmFunctionHelperCommand::operator()(
makefile.MarkVariableAsUsed(ARGV);
makefile.AddDefinition(ARGN, argnDef);
makefile.MarkVariableAsUsed(ARGN);
makefile.AddDefinition(kFUNCTION_ARGNC, functionArgncDef);
makefile.MarkVariableAsUsed(kFUNCTION_ARGNC);
makefile.AddDefinition(ARGNC, functionArgncDef);
makefile.MarkVariableAsUsed(ARGNC);
makefile.AddDefinition(CMAKE_CURRENT_FUNCTION, this->Args.front());
makefile.MarkVariableAsUsed(CMAKE_CURRENT_FUNCTION);

View File

@@ -182,12 +182,11 @@ bool cmParseArgumentsCommand(std::vector<std::string> const& args,
}
parseFromArg = true;
argIter++; // move past PARSE_ARGN
std::string argncStr =
status.GetMakefile().GetSafeDefinition("_FUNCTION_ARGNC");
std::string argncStr = status.GetMakefile().GetSafeDefinition("ARGNC");
if (!cmStrToULong(argncStr, &argnc)) {
status.GetMakefile().IssueMessage(
MessageType::FATAL_ERROR,
cmStrCat("PARSE_ARGN called with _FUNCTION_ARGNC='", argncStr,
cmStrCat("PARSE_ARGN called with ARGNC='", argncStr,
"' that is not an unsigned integer"));
cmSystemTools::SetFatalErrorOccurred();
return true;

View File

@@ -1,5 +1,5 @@
^CMake Error at BadArgN2\.cmake:[0-9]+ \(cmake_parse_arguments\):
PARSE_ARGN called with _FUNCTION_ARGNC='' that is not an unsigned integer
PARSE_ARGN called with ARGNC='' that is not an unsigned integer
Call Stack \(most recent call first\):
BadArgN2\.cmake:[0-9]+ \(test2\)
CMakeLists\.txt:[0-9]+ \(include\)$

View File

@@ -1,5 +1,5 @@
function(test2)
unset(_FUNCTION_ARGNC)
unset(ARGNC)
cmake_parse_arguments(PARSE_ARGN pref "" "" "")
endfunction()
test2()