cmListCommand: handle empty lists for list(REMOVE_AT)

Treat an empty list as a list with no valid bounds and return an error
message indicating that any given indices are out-of-bounds.
This commit is contained in:
Ben Boeckel 2018-10-11 17:26:44 -04:00
parent acfe53c588
commit 121a036f73
9 changed files with 33 additions and 9 deletions

View File

@ -4,3 +4,6 @@ better-empty-list-behavior
* The :command:`list` operations ``REMOVE_ITEM``, ``REMOVE_DUPLICATES``,
``SORT``, ``REVERSE``, and ``FILTER`` all now accept a non-existent variable
as the list since these operations on empty lists is also the empty list.
* The :command:`list` operation ``REMOVE_AT`` now indicates that the given
indices are invalid for a non-existent variable or empty list.

View File

@ -1225,13 +1225,17 @@ bool cmListCommand::HandleRemoveAtCommand(std::vector<std::string> const& args)
const std::string& listName = args[1];
// expand the variable
std::vector<std::string> varArgsExpanded;
if (!this->GetList(varArgsExpanded, listName)) {
this->SetError("sub-command REMOVE_AT requires list to be present.");
return false;
}
// FIXME: Add policy to make non-existing lists an error like empty lists.
if (varArgsExpanded.empty()) {
this->SetError("REMOVE_AT given empty list");
if (!this->GetList(varArgsExpanded, listName) || varArgsExpanded.empty()) {
std::ostringstream str;
str << "index: ";
for (size_t i = 1; i < args.size(); ++i) {
str << args[i];
if (i != args.size() - 1) {
str << ", ";
}
}
str << " out of range (0, 0)";
this->SetError(str.str());
return false;
}

View File

@ -1,4 +1,4 @@
CMake Error at EmptyRemoveAt0.cmake:2 \(list\):
list REMOVE_AT given empty list
list index: mylist, 0 out of range \(0, 0\)
Call Stack \(most recent call first\):
CMakeLists.txt:3 \(include\)$

View File

@ -0,0 +1 @@
1

View File

@ -0,0 +1,4 @@
^CMake Error at REMOVE_AT-EmptyList.cmake:2 \(list\):
list index: nosuchlist, 0 out of range \(0, 0\)
Call Stack \(most recent call first\):
CMakeLists.txt:3 \(include\)$

View File

@ -0,0 +1,6 @@
set(nosuchlist "")
list(REMOVE_AT nosuchlist 0)
if (NOT DEFINED nosuchlist OR NOT nosuchlist STREQUAL "")
message(FATAL_ERROR
"list(REMOVE_AT) modified our list")
endif ()

View File

@ -1,4 +1,4 @@
^CMake Error at REMOVE_AT-NotList.cmake:2 \(list\):
list sub-command REMOVE_AT requires list to be present.
list index: nosuchlist, 0 out of range \(0, 0\)
Call Stack \(most recent call first\):
CMakeLists.txt:3 \(include\)$

View File

@ -1,2 +1,6 @@
unset(nosuchlist)
list(REMOVE_AT nosuchlist 0)
if (DEFINED nosuchlist)
message(FATAL_ERROR
"list(REMOVE_AT) created our list")
endif ()

View File

@ -22,6 +22,8 @@ run_cmake(REMOVE_DUPLICATES-TooManyArguments)
run_cmake(REVERSE-TooManyArguments)
run_cmake(SUBLIST-TooManyArguments)
run_cmake(REMOVE_AT-EmptyList)
run_cmake(FILTER-NotList)
run_cmake(REMOVE_AT-NotList)
run_cmake(REMOVE_DUPLICATES-NotList)