Coverage Report

Created: 2026-07-30 06:52

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
36
  SELECT(ACTION, Warn, CMD_NONE, CMD_AUTHOR, 12)                              \
30
36
  SELECT(ACTION, Warn, CMD_AUTHOR, CMD_DEPRECATED, 1)                         \
31
36
  SELECT(ACTION, Warn, CMD_AUTHOR, CMD_EXPERIMENTAL, 12)                      \
32
36
  SELECT(ACTION, Ignore, CMD_AUTHOR, CMD_STRICT, 13)                          \
33
36
  SELECT(ACTION, Ignore, CMD_STRICT, CMD_INSTALL_ABSOLUTE_DESTINATION, 12)    \
34
36
  SELECT(ACTION, Ignore, CMD_STRICT, CMD_NON_TARGET_DIRECTIVE, 13)            \
35
36
  SELECT(ACTION, Warn, CMD_AUTHOR, CMD_POLICY, 12)                            \
36
36
  SELECT(ACTION, Ignore, CMD_NONE, CMD_UNINITIALIZED, 1)                      \
37
36
  SELECT(ACTION, Warn, CMD_NONE, CMD_UNUSED_CLI, 1)
38
39
324
#define CM_SELECT_CATEGORY(F, D, P, C, V) F(C)
40
#define CM_FOR_EACH_DIAGNOSTIC_CATEGORY(ACTION)                               \
41
324
  CM_FOR_EACH_DIAGNOSTIC_TABLE(ACTION, CM_SELECT_CATEGORY)
42
43
/** \class cmDiagnostics
44
 * \brief Handles CMake diagnostic (warning) behavior
45
 *
46
 * See the cmake-diagnostics(7) manual for an overview of this class's purpose.
47
 */
48
class cmDiagnostics
49
{
50
public:
51
  /// Action to take when a diagnostic is triggered
52
  enum DiagnosticAction : std::uint8_t
53
  {
54
    Undefined = 0,
55
    Ignore,
56
    Warn,
57
    SendError,
58
    FatalError,
59
  };
60
61
  /// Diagnostic category identifiers
62
  enum DiagnosticCategory : unsigned
63
  {
64
    CMD_NONE,
65
#define DIAGNOSTIC_ENUM(CATEGORY) CATEGORY,
66
    CM_FOR_EACH_DIAGNOSTIC_CATEGORY(DIAGNOSTIC_ENUM)
67
#undef DIAGNOSTIC_ENUM
68
69
    /** \brief Always the last entry.
70
     *
71
     * Used to determine the number of diagnostic categories.  Also useful to
72
     * avoid adding a comma the last diagnostic category when adding a new one.
73
     */
74
    CMD_COUNT
75
  };
76
  constexpr static size_t CategoryCount = static_cast<size_t>(CMD_COUNT);
77
78
  struct DiagnosticCategoryInformation
79
  {
80
    DiagnosticCategory Parent;
81
    DiagnosticAction DefaultAction;
82
    int PresetVersion;
83
  };
84
85
  constexpr static DiagnosticCategoryInformation
86
    CategoryInfo[CategoryCount] = {
87
      { CMD_NONE, Undefined, 0 }, // CMD_NONE
88
#define DIAGNOSTIC_CATEGORY_INFO(F, D, P, C, V) { P, D, V },
89
      CM_FOR_EACH_DIAGNOSTIC_TABLE(UNUSED, DIAGNOSTIC_CATEGORY_INFO)
90
#undef DIAGNOSTIC_CATEGORY_INFO
91
    };
92
93
  //! convert an action identifier into a string
94
  static cm::string_view GetActionString(DiagnosticAction);
95
96
  //! convert a category identifier into a string
97
  static cm::string_view GetCategoryString(DiagnosticCategory);
98
99
  //! Convert a string action into an identifier
100
  static cm::optional<DiagnosticAction> GetDiagnosticAction(
101
    cm::string_view name);
102
103
  //! Convert a string category into an identifier
104
  static cm::optional<DiagnosticCategory> GetDiagnosticCategory(
105
    cm::string_view name);
106
107
  /** Represent a set of diagnostic category actions.  */
108
  struct DiagnosticMap : public std::array<DiagnosticAction, CategoryCount>
109
  {
110
    DiagnosticMap()
111
36
      : array{} {};
112
  };
113
};
114
115
using cmDiagnosticCategory = cmDiagnostics::DiagnosticCategory;
116
using cmDiagnosticAction = cmDiagnostics::DiagnosticAction;