Merge topic 'cmake-diagnostics-check-order'

91e81bf262 Diagnostics: Ensure correct ordering
700f64cfb9 cmDiagnostics: Fix typo in source documentation

Acked-by: Kitware Robot <kwrobot@kitware.com>
Merge-request: !12002
diff --git a/Source/cmDiagnostics.cxx b/Source/cmDiagnostics.cxx
index 60d3ec4..50d3c02 100644
--- a/Source/cmDiagnostics.cxx
+++ b/Source/cmDiagnostics.cxx
@@ -12,6 +12,28 @@
 #include "cmStringAlgorithms.h"
 
 namespace {
+
+#if __cplusplus >= 201703L
+constexpr unsigned validateDiagnosticsSubtree(unsigned parent, unsigned index)
+{
+  // Ensure that all diagnostics, starting from the specified index, have the
+  // specified parent as an ancestor. Return the first index that violates
+  // this condition.
+  while (index < cmDiagnostics::CategoryCount &&
+         cmDiagnostics::CategoryInfo[index].Parent == parent) {
+    unsigned const child = index;
+    // For each diagnostic, 'consume' its children (if any).
+    index = validateDiagnosticsSubtree(child, ++index);
+  }
+  return index;
+}
+
+static_assert(validateDiagnosticsSubtree(cmDiagnostics::CMD_NONE, 1) ==
+                cmDiagnostics::CategoryCount,
+              "Diagnostics are not properly ordered"
+              " (hint: LHS is the index of the first misordered diagnostic)");
+#endif
+
 cm::optional<cmDiagnosticCategory> stringToCategory(cm::string_view input)
 {
   using Map = std::map<cm::string_view, cmDiagnosticCategory>;
diff --git a/Source/cmDiagnostics.h b/Source/cmDiagnostics.h
index bad732b..84fe098 100644
--- a/Source/cmDiagnostics.h
+++ b/Source/cmDiagnostics.h
@@ -36,7 +36,7 @@
 #define CM_FOR_EACH_DIAGNOSTIC_CATEGORY(ACTION)                               \
   CM_FOR_EACH_DIAGNOSTIC_TABLE(ACTION, CM_SELECT_CATEGORY)
 
-/** \class cmDiagnostic
+/** \class cmDiagnostics
  * \brief Handles CMake diagnostic (warning) behavior
  *
  * See the cmake-diagnostics(7) manual for an overview of this class's purpose.