Browse Source

cmListCommand: make list(ACTION not_a_list) succeed when idempotent

The operations changed here all are no-ops on empty lists anyways, so
just have them succeed when given non-extant lists.

  - `list(REMOVE_ITEM)`
  - `list(REMOVE_DUPLICATES)`
  - `list(SORT)`
  - `list(FILTER)`
  - `list(REVERSE)`
Ben Boeckel 7 years ago
parent
commit
acfe53c588

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

@@ -0,0 +1,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.

+ 5 - 11
Source/cmListCommand.cxx

@@ -346,8 +346,7 @@ bool cmListCommand::HandleRemoveItemCommand(
   // expand the variable
   std::vector<std::string> varArgsExpanded;
   if (!this->GetList(varArgsExpanded, listName)) {
-    this->SetError("sub-command REMOVE_ITEM requires list to be present.");
-    return false;
+    return true;
   }
 
   std::vector<std::string> remove(args.begin() + 2, args.end());
@@ -376,8 +375,7 @@ bool cmListCommand::HandleReverseCommand(std::vector<std::string> const& args)
   // expand the variable
   std::vector<std::string> varArgsExpanded;
   if (!this->GetList(varArgsExpanded, listName)) {
-    this->SetError("sub-command REVERSE requires list to be present.");
-    return false;
+    return true;
   }
 
   std::string value = cmJoin(cmReverseRange(varArgsExpanded), ";");
@@ -399,9 +397,7 @@ bool cmListCommand::HandleRemoveDuplicatesCommand(
   // expand the variable
   std::vector<std::string> varArgsExpanded;
   if (!this->GetList(varArgsExpanded, listName)) {
-    this->SetError(
-      "sub-command REMOVE_DUPLICATES requires list to be present.");
-    return false;
+    return true;
   }
 
   std::vector<std::string>::const_iterator argsEnd =
@@ -1152,8 +1148,7 @@ bool cmListCommand::HandleSortCommand(std::vector<std::string> const& args)
   // expand the variable
   std::vector<std::string> varArgsExpanded;
   if (!this->GetList(varArgsExpanded, listName)) {
-    this->SetError("sub-command SORT requires list to be present.");
-    return false;
+    return true;
   }
 
   if ((sortCompare == cmStringSorter::Compare::STRING) &&
@@ -1304,8 +1299,7 @@ bool cmListCommand::HandleFilterCommand(std::vector<std::string> const& args)
   // expand the variable
   std::vector<std::string> varArgsExpanded;
   if (!this->GetList(varArgsExpanded, listName)) {
-    this->SetError("sub-command FILTER requires list to be present.");
-    return false;
+    return true;
   }
 
   const std::string& mode = args[3];

+ 0 - 1
Tests/RunCMake/list/FILTER-NotList-result.txt

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

+ 0 - 4
Tests/RunCMake/list/FILTER-NotList-stderr.txt

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

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

@@ -1,2 +1,6 @@
 unset(nosuchlist)
 list(FILTER nosuchlist EXCLUDE REGEX "^FILTER_THIS_.+")
+if (DEFINED nosuchlist)
+  message(FATAL_ERROR
+    "list(FILTER) created our list")
+endif ()

+ 0 - 1
Tests/RunCMake/list/REMOVE_DUPLICATES-NotList-result.txt

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

+ 0 - 4
Tests/RunCMake/list/REMOVE_DUPLICATES-NotList-stderr.txt

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

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

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

+ 0 - 1
Tests/RunCMake/list/REMOVE_ITEM-NotList-result.txt

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

+ 0 - 4
Tests/RunCMake/list/REMOVE_ITEM-NotList-stderr.txt

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

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

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

+ 0 - 1
Tests/RunCMake/list/REVERSE-NotList-result.txt

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

+ 0 - 4
Tests/RunCMake/list/REVERSE-NotList-stderr.txt

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

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

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

+ 0 - 1
Tests/RunCMake/list/SORT-NotList-result.txt

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

+ 0 - 4
Tests/RunCMake/list/SORT-NotList-stderr.txt

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

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

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