Coverage Report

Created: 2026-06-15 07:03

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/CMake/Source/cmDiagnostics.h
Line
Count
Source
1
/* Distributed under the OSI-approved BSD 3-Clause License.  See accompanying
2
   file LICENSE.rst or https://cmake.org/licensing for details.  */
3
#pragma once
4
5
#include "cmConfigure.h" // IWYU pragma: keep
6
7
#include <array>
8
#include <cstddef>
9
#include <cstdint>
10
11
#include <cm/optional>
12
#include <cm/string_view>
13
14
// https://github.com/include-what-you-use/include-what-you-use/issues/1934
15
// IWYU pragma: no_forward_declare cmDiagnostics::DiagnosticAction
16
// IWYU pragma: no_forward_declare cmDiagnostics::DiagnosticCategory
17
18
// The list of diagnostic categories along with their associated data.
19
// Each entry is of the form:
20
//    `SELECT(ACTION, <default>, <parent>, <name>, <preset-version>)`
21
// Entries MUST appear in the order that a depth-first enumeration would
22
// produce.
23
//
24
// When changing this table, Utilities/Scripts/regenerate-presets.py must be
25
// run to update the JSON schema for CMake presets. If the _TABLE macro name
26
// changes, said script will also need to be updated with the new name.
27
28
#define CM_FOR_EACH_DIAGNOSTIC_TABLE(ACTION, SELECT)                          \
29
28
  SELECT(ACTION, Warn, CMD_NONE, CMD_AUTHOR, 12)                              \
30
28
  SELECT(ACTION, Warn, CMD_AUTHOR, CMD_DEPRECATED, 1)                         \
31
28
  SELECT(ACTION, Warn, CMD_AUTHOR, CMD_EXPERIMENTAL, 12)                      \
32
28
  SELECT(ACTION, Ignore, CMD_AUTHOR, CMD_INSTALL_ABSOLUTE_DESTINATION, 12)    \
33
28
  SELECT(ACTION, Warn, CMD_AUTHOR, CMD_POLICY, 12)                            \
34
28
  SELECT(ACTION, Ignore, CMD_NONE, CMD_UNINITIALIZED, 1)                      \
35
28
  SELECT(ACTION, Warn, CMD_NONE, CMD_UNUSED_CLI, 1)
36
37
196
#define CM_SELECT_CATEGORY(F, D, P, C, V) F(C)
38
#define CM_FOR_EACH_DIAGNOSTIC_CATEGORY(ACTION)                               \
39
196
  CM_FOR_EACH_DIAGNOSTIC_TABLE(ACTION, CM_SELECT_CATEGORY)
40
41
/** \class cmDiagnostics
42
 * \brief Handles CMake diagnostic (warning) behavior
43
 *
44
 * See the cmake-diagnostics(7) manual for an overview of this class's purpose.
45
 */
46
class cmDiagnostics
47
{
48
public:
49
  /// Action to take when a diagnostic is triggered
50
  enum DiagnosticAction : std::uint8_t
51
  {
52
    Undefined = 0,
53
    Ignore,
54
    Warn,
55
    SendError,
56
    FatalError,
57
  };
58
59
  /// Diagnostic category identifiers
60
  enum DiagnosticCategory : unsigned
61
  {
62
    CMD_NONE,
63
#define DIAGNOSTIC_ENUM(CATEGORY) CATEGORY,
64
    CM_FOR_EACH_DIAGNOSTIC_CATEGORY(DIAGNOSTIC_ENUM)
65
#undef DIAGNOSTIC_ENUM
66
67
    /** \brief Always the last entry.
68
     *
69
     * Used to determine the number of diagnostic categories.  Also useful to
70
     * avoid adding a comma the last diagnostic category when adding a new one.
71
     */
72
    CMD_COUNT
73
  };
74
  constexpr static size_t CategoryCount = static_cast<size_t>(CMD_COUNT);
75
76
  struct DiagnosticCategoryInformation
77
  {
78
    DiagnosticCategory Parent;
79
    DiagnosticAction DefaultAction;
80
    int PresetVersion;
81
  };
82
83
  constexpr static DiagnosticCategoryInformation
84
    CategoryInfo[CategoryCount] = {
85
      { CMD_NONE, Undefined, 0 }, // CMD_NONE
86
#define DIAGNOSTIC_CATEGORY_INFO(F, D, P, C, V) { P, D, V },
87
      CM_FOR_EACH_DIAGNOSTIC_TABLE(UNUSED, DIAGNOSTIC_CATEGORY_INFO)
88
#undef DIAGNOSTIC_CATEGORY_INFO
89
    };
90
91
  //! convert an action identifier into a string
92
  static cm::string_view GetActionString(DiagnosticAction);
93
94
  //! convert a category identifier into a string
95
  static cm::string_view GetCategoryString(DiagnosticCategory);
96
97
  //! Convert a string action into an identifier
98
  static cm::optional<DiagnosticAction> GetDiagnosticAction(
99
    cm::string_view name);
100
101
  //! Convert a string category into an identifier
102
  static cm::optional<DiagnosticCategory> GetDiagnosticCategory(
103
    cm::string_view name);
104
105
  /** Represent a set of diagnostic category actions.  */
106
  struct DiagnosticMap : public std::array<DiagnosticAction, CategoryCount>
107
  {
108
    DiagnosticMap()
109
36
      : array{} {};
110
  };
111
};
112
113
using cmDiagnosticCategory = cmDiagnostics::DiagnosticCategory;
114
using cmDiagnosticAction = cmDiagnostics::DiagnosticAction;