Diagnostics: Ensure correct ordering

Create a constexpr function that recursively validates that [a portion
of] the diagnostics tree is in proper depth-first order. Use this to
ensure at compile time that the diagnostics are, in fact, in the correct
order. This will make it harder to accidentally introduce order errors,
since any C++17 build will throw a compile error if the list is not
correctly ordered.
This commit is contained in:
Matthew Woehlke
2026-05-01 14:49:11 -04:00
parent 700f64cfb9
commit 91e81bf262
+22
View File
@@ -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>;