Просмотр исходного кода

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.
Ben Boeckel 7 лет назад
Родитель
Сommit
121a036f73

+ 3 - 0
Help/release/dev/better-empty-list-behavior.rst

@@ -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.

+ 11 - 7
Source/cmListCommand.cxx

@@ -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;
   }
 

+ 1 - 1
Tests/RunCMake/list/EmptyRemoveAt0-stderr.txt

@@ -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\)$

+ 1 - 0
Tests/RunCMake/list/REMOVE_AT-EmptyList-result.txt

@@ -0,0 +1 @@
+1

+ 4 - 0
Tests/RunCMake/list/REMOVE_AT-EmptyList-stderr.txt

@@ -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\)$

+ 6 - 0
Tests/RunCMake/list/REMOVE_AT-EmptyList.cmake

@@ -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 ()

+ 1 - 1
Tests/RunCMake/list/REMOVE_AT-NotList-stderr.txt

@@ -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\)$

+ 4 - 0
Tests/RunCMake/list/REMOVE_AT-NotList.cmake

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

+ 2 - 0
Tests/RunCMake/list/RunCMakeTest.cmake

@@ -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)