Coverage Report

Created: 2026-04-29 07:01

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/CMake/Source/cmCMakeDiagnosticCommand.cxx
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
#include "cmCMakeDiagnosticCommand.h"
4
5
#include <cm/optional>
6
#include <cmext/string_view>
7
8
#include "cmDiagnostics.h"
9
#include "cmExecutionStatus.h"
10
#include "cmMakefile.h"
11
#include "cmStringAlgorithms.h"
12
13
namespace {
14
using AlterFunction = bool (cmMakefile::*)(cmDiagnosticCategory,
15
                                           cmDiagnosticAction, bool);
16
17
bool HandleAlterMode(std::vector<std::string> const& args,
18
                     cmExecutionStatus& status, AlterFunction function,
19
                     bool recursive)
20
0
{
21
0
  if (args.size() < 3 || args.size() > 4) {
22
0
    status.SetError(cmStrCat(
23
0
      args[0], " must be given exactly 2 or 3 additional arguments."_s));
24
0
    return false;
25
0
  }
26
27
0
  if (args.size() == 4) {
28
0
    if (args[3] == "RECURSE") {
29
0
      recursive = true;
30
0
    } else if (args[3] == "NO_RECURSE") {
31
0
      recursive = false;
32
0
    } else {
33
0
      status.SetError(
34
0
        cmStrCat(args[0], " given unknown option \""_s, args[3], "\"."_s));
35
0
      return false;
36
0
    }
37
0
  }
38
39
0
  cm::optional<cmDiagnosticAction> const action =
40
0
    cmDiagnostics::GetDiagnosticAction(args[2]);
41
0
  if (!action) {
42
0
    status.SetError(cmStrCat(
43
0
      args[0], " given unrecognized diagnostic action \"", args[2], "\"."_s));
44
0
    return false;
45
0
  }
46
47
0
  cm::optional<cmDiagnosticCategory> const category =
48
0
    cmDiagnostics::GetDiagnosticCategory(args[1]);
49
0
  if (!category) {
50
0
    status.SetError(cmStrCat(args[0],
51
0
                             " given unrecognized diagnostic category \"",
52
0
                             args[1], "\"."_s));
53
0
    return false;
54
0
  }
55
56
0
  if (!(status.GetMakefile().*function)(*category, *action, recursive)) {
57
0
    status.SetError(cmStrCat(args[0], " failed to set diagnostic action."_s));
58
0
    return false;
59
0
  }
60
0
  return true;
61
0
}
62
63
bool HandleGetMode(std::vector<std::string> const& args,
64
                   cmExecutionStatus& status)
65
0
{
66
0
  if (args.size() != 3) {
67
0
    status.SetError("GET must be given exactly 2 additional arguments.");
68
0
    return false;
69
0
  }
70
71
0
  cm::optional<cmDiagnosticCategory> const category =
72
0
    cmDiagnostics::GetDiagnosticCategory(args[1]);
73
0
  if (!category) {
74
0
    status.SetError(cmStrCat(args[0],
75
0
                             " given unrecognized diagnostic category \"",
76
0
                             args[1], "\"."_s));
77
0
    return false;
78
0
  }
79
80
0
  cmDiagnosticAction const action =
81
0
    status.GetMakefile().GetDiagnosticAction(*category);
82
83
0
  status.GetMakefile().AddDefinition(args[2],
84
0
                                     cmDiagnostics::GetActionString(action));
85
0
  return true;
86
0
}
87
}
88
89
// cmCMakeDiagnosticCommand
90
bool cmCMakeDiagnosticCommand(std::vector<std::string> const& args,
91
                              cmExecutionStatus& status)
92
0
{
93
0
  if (args.empty()) {
94
0
    status.SetError("requires at least one argument.");
95
0
    return false;
96
0
  }
97
98
0
  if (args[0] == "SET") {
99
0
    return HandleAlterMode(args, status, &cmMakefile::SetDiagnostic, false);
100
0
  }
101
0
  if (args[0] == "PROMOTE") {
102
0
    return HandleAlterMode(args, status, &cmMakefile::PromoteDiagnostic, true);
103
0
  }
104
0
  if (args[0] == "DEMOTE") {
105
0
    return HandleAlterMode(args, status, &cmMakefile::DemoteDiagnostic, true);
106
0
  }
107
0
  if (args[0] == "GET") {
108
0
    return HandleGetMode(args, status);
109
0
  }
110
0
  if (args[0] == "PUSH") {
111
0
    if (args.size() > 1) {
112
0
      status.SetError("PUSH may not be given additional arguments.");
113
0
      return false;
114
0
    }
115
0
    status.GetMakefile().PushDiagnostic();
116
0
    return true;
117
0
  }
118
0
  if (args[0] == "POP") {
119
0
    if (args.size() > 1) {
120
0
      status.SetError("POP may not be given additional arguments.");
121
0
      return false;
122
0
    }
123
0
    status.GetMakefile().PopDiagnostic();
124
0
    return true;
125
0
  }
126
127
0
  status.SetError(cmStrCat("given unknown first argument \"", args[0], '"'));
128
0
  return false;
129
0
}