CMP0037: Restore diagnostic message for invalid ALIAS target names

Refactoring in commit 7a4c02cb38 (cmGlobalGenerator: factor out
messaging for CMP0037, 2023-09-24, v3.28.0-rc1~39^2~7) incorrectly
switched to reporting the aliased target name instead of the invalid
name of the alias itself.

Fixes: #25979
diff --git a/Source/cmGlobalGenerator.cxx b/Source/cmGlobalGenerator.cxx
index ba1938f..759a7d6 100644
--- a/Source/cmGlobalGenerator.cxx
+++ b/Source/cmGlobalGenerator.cxx
@@ -2707,6 +2707,7 @@
 }
 
 static bool RaiseCMP0037Message(cmake* cm, cmTarget* tgt,
+                                std::string const& targetNameAsWritten,
                                 std::string const& reason)
 {
   MessageType messageType = MessageType::AUTHOR_WARNING;
@@ -2727,8 +2728,8 @@
       break;
   }
   if (issueMessage) {
-    e << "The target name \"" << tgt->GetName() << "\" is reserved " << reason
-      << ".";
+    e << "The target name \"" << targetNameAsWritten << "\" is reserved "
+      << reason << ".";
     if (messageType == MessageType::AUTHOR_WARNING) {
       e << "  It may result in undefined behavior.";
     }
@@ -2747,7 +2748,8 @@
   if (!tgt) {
     return true;
   }
-  return RaiseCMP0037Message(this->GetCMakeInstance(), tgt, reason);
+  return RaiseCMP0037Message(this->GetCMakeInstance(), tgt, targetName,
+                             reason);
 }
 
 void cmGlobalGenerator::CreateDefaultGlobalTargets(
diff --git a/Tests/RunCMake/CMP0037/RunCMakeTest.cmake b/Tests/RunCMake/CMP0037/RunCMakeTest.cmake
index 558fba3..89beb59 100644
--- a/Tests/RunCMake/CMP0037/RunCMakeTest.cmake
+++ b/Tests/RunCMake/CMP0037/RunCMakeTest.cmake
@@ -45,6 +45,10 @@
 run_cmake(WARN-cond-test)
 run_cmake(WARN-cond-package)
 
+run_cmake(alias-test-NEW)
+run_cmake(alias-test-OLD)
+run_cmake(alias-test-WARN)
+
 if(RunCMake_GENERATOR MATCHES "Make|Ninja")
   run_cmake(NEW-cond-package_source)
   run_cmake(OLD-cond-package_source)
diff --git a/Tests/RunCMake/CMP0037/alias-test-NEW-result.txt b/Tests/RunCMake/CMP0037/alias-test-NEW-result.txt
new file mode 100644
index 0000000..d00491f
--- /dev/null
+++ b/Tests/RunCMake/CMP0037/alias-test-NEW-result.txt
@@ -0,0 +1 @@
+1
diff --git a/Tests/RunCMake/CMP0037/alias-test-NEW-stderr.txt b/Tests/RunCMake/CMP0037/alias-test-NEW-stderr.txt
new file mode 100644
index 0000000..68a36cc
--- /dev/null
+++ b/Tests/RunCMake/CMP0037/alias-test-NEW-stderr.txt
@@ -0,0 +1,5 @@
+^CMake Error at alias-test-common\.cmake:[0-9]+ \(add_library\):
+  The target name "test" is reserved when CTest testing is enabled\.
+Call Stack \(most recent call first\):
+  alias-test-NEW\.cmake:[0-9]+ \(include\)
+  CMakeLists\.txt:[0-9]+ \(include\)$
diff --git a/Tests/RunCMake/CMP0037/alias-test-NEW.cmake b/Tests/RunCMake/CMP0037/alias-test-NEW.cmake
new file mode 100644
index 0000000..32569b5
--- /dev/null
+++ b/Tests/RunCMake/CMP0037/alias-test-NEW.cmake
@@ -0,0 +1,2 @@
+cmake_policy(SET CMP0037 NEW)
+include(alias-test-common.cmake)
diff --git a/Tests/RunCMake/CMP0037/alias-test-OLD-stderr.txt b/Tests/RunCMake/CMP0037/alias-test-OLD-stderr.txt
new file mode 100644
index 0000000..bf177e2
--- /dev/null
+++ b/Tests/RunCMake/CMP0037/alias-test-OLD-stderr.txt
@@ -0,0 +1,10 @@
+^CMake Deprecation Warning at alias-test-OLD\.cmake:[0-9]+ \(cmake_policy\):
+  The OLD behavior for policy CMP0037 will be removed from a future version
+  of CMake\.
+
+  The cmake-policies\(7\) manual explains that the OLD behaviors of all
+  policies are deprecated and that a policy should be set to OLD only under
+  specific short-term circumstances\.  Projects should be ported to the NEW
+  behavior and not rely on setting a policy to OLD\.
+Call Stack \(most recent call first\):
+  CMakeLists\.txt:[0-9]+ \(include\)$
diff --git a/Tests/RunCMake/CMP0037/alias-test-OLD.cmake b/Tests/RunCMake/CMP0037/alias-test-OLD.cmake
new file mode 100644
index 0000000..1f3e770
--- /dev/null
+++ b/Tests/RunCMake/CMP0037/alias-test-OLD.cmake
@@ -0,0 +1,2 @@
+cmake_policy(SET CMP0037 OLD)
+include(alias-test-common.cmake)
diff --git a/Tests/RunCMake/CMP0037/alias-test-WARN-stderr.txt b/Tests/RunCMake/CMP0037/alias-test-WARN-stderr.txt
new file mode 100644
index 0000000..43bf98b
--- /dev/null
+++ b/Tests/RunCMake/CMP0037/alias-test-WARN-stderr.txt
@@ -0,0 +1,11 @@
+^CMake Warning \(dev\) at alias-test-common\.cmake:[0-9]+ \(add_library\):
+  Policy CMP0037 is not set: Target names should not be reserved and should
+  match a validity pattern\.  Run "cmake --help-policy CMP0037" for policy
+  details\.  Use the cmake_policy command to set the policy and suppress this
+  warning\.
+
+  The target name "test" is reserved when CTest testing is enabled\.  It may
+  result in undefined behavior\.
+Call Stack \(most recent call first\):
+  alias-test-WARN\.cmake:[0-9]+ \(include\)
+  CMakeLists\.txt:[0-9]+ \(include\)
diff --git a/Tests/RunCMake/CMP0037/alias-test-WARN.cmake b/Tests/RunCMake/CMP0037/alias-test-WARN.cmake
new file mode 100644
index 0000000..688cb95
--- /dev/null
+++ b/Tests/RunCMake/CMP0037/alias-test-WARN.cmake
@@ -0,0 +1,2 @@
+# leave CMP0037 unset
+include(alias-test-common.cmake)
diff --git a/Tests/RunCMake/CMP0037/alias-test-common.cmake b/Tests/RunCMake/CMP0037/alias-test-common.cmake
new file mode 100644
index 0000000..f6e5000
--- /dev/null
+++ b/Tests/RunCMake/CMP0037/alias-test-common.cmake
@@ -0,0 +1,3 @@
+enable_testing()
+add_library(iface INTERFACE)
+add_library(test ALIAS iface)