Coverage Report

Created: 2026-09-14 06:43

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/CMake/Source/cmJSONHelpers.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 "cmConfigure.h" // IWYU pragma: keep
4
5
#include "cmJSONHelpers.h"
6
7
#include <functional>
8
#include <string>
9
#include <vector>
10
11
#include <cm3p/json/value.h>
12
13
#include "cmJSONState.h"
14
15
namespace JsonErrors {
16
ErrorGenerator EXPECTED_TYPE(std::string const& type)
17
3.91k
{
18
3.91k
  return [type](Json::Value const* value, cmJSONState* state) -> void {
19
3.90k
    if (state->key().empty()) {
20
0
      state->AddErrorAtValue(cmStrCat("Expected ", type), value);
21
0
      return;
22
0
    }
23
3.90k
    std::string errMsg = cmStrCat('"', state->key(), "\" expected ", type);
24
3.90k
    if (value && value->isConvertibleTo(Json::ValueType::stringValue)) {
25
3.59k
      errMsg = cmStrCat(errMsg, ", got: ", value->asString());
26
3.59k
    }
27
3.90k
    state->AddErrorAtValue(errMsg, value);
28
3.90k
  };
29
3.91k
}
30
31
void INVALID_STRING(Json::Value const* value, cmJSONState* state)
32
1.30k
{
33
1.30k
  JsonErrors::EXPECTED_TYPE("a string")(value, state);
34
1.30k
}
35
36
void INVALID_BOOL(Json::Value const* value, cmJSONState* state)
37
183
{
38
183
  JsonErrors::EXPECTED_TYPE("a bool")(value, state);
39
183
}
40
41
void INVALID_INT(Json::Value const* value, cmJSONState* state)
42
0
{
43
0
  JsonErrors::EXPECTED_TYPE("an integer")(value, state);
44
0
}
45
46
void INVALID_UINT(Json::Value const* value, cmJSONState* state)
47
2.42k
{
48
2.42k
  JsonErrors::EXPECTED_TYPE("an unsigned integer")(value, state);
49
2.42k
}
50
51
ObjectErrorGenerator INVALID_NAMED_OBJECT(
52
  std::function<std::string(Json::Value const*, cmJSONState*)> const&
53
    nameGenerator)
54
264k
{
55
#if defined(__GNUC__) && __GNUC__ >= 15
56
#  define CM_GCC_diagnostic_push_Wmaybe_uninitialized
57
#  pragma GCC diagnostic push
58
#  pragma GCC diagnostic ignored "-Wmaybe-uninitialized"
59
#endif
60
264k
  return [nameGenerator](
61
264k
           ObjectError errorType,
62
264k
           Json::Value::Members const& extraFields) -> ErrorGenerator {
63
264k
    return [nameGenerator, errorType, extraFields](
64
264k
             Json::Value const* value, cmJSONState* state) -> void {
65
264k
      std::string name = nameGenerator(value, state);
66
264k
      switch (errorType) {
67
0
        case ObjectError::RequiredMissing:
68
0
          state->AddErrorAtValue(cmStrCat("Invalid Required ", name), value);
69
0
          break;
70
5.85k
        case ObjectError::InvalidObject:
71
5.85k
          state->AddErrorAtValue(cmStrCat("Invalid ", name), value);
72
5.85k
          break;
73
127k
        case ObjectError::ExtraField: {
74
221k
          for (auto const& member : extraFields) {
75
221k
            if (value) {
76
221k
              state->AddErrorAtValue(
77
221k
                cmStrCat("Invalid extra field \"", member, "\" in ", name),
78
221k
                &(*value)[member]);
79
221k
            } else {
80
0
              state->AddError(
81
0
                cmStrCat("Invalid extra field \"", member, "\" in ", name));
82
0
            }
83
221k
          }
84
127k
        } break;
85
130k
        case ObjectError::MissingRequired:
86
130k
          state->AddErrorAtValue(cmStrCat("Missing required field \"",
87
130k
                                          state->key(), "\" in ", name),
88
130k
                                 value);
89
130k
          break;
90
264k
      }
91
264k
    };
92
264k
  };
93
#ifdef CM_GCC_diagnostic_push_Wmaybe_uninitialized
94
#  pragma GCC diagnostic pop
95
#  undef CM_GCC_diagnostic_push_Wmaybe_uninitialized
96
#endif
97
264k
}
98
99
ErrorGenerator INVALID_OBJECT(ObjectError errorType,
100
                              Json::Value::Members const& extraFields)
101
64.0k
{
102
64.0k
  return INVALID_NAMED_OBJECT(
103
64.0k
    [](Json::Value const*, cmJSONState*) -> std::string { return "Object"; })(
104
64.0k
    errorType, extraFields);
105
64.0k
}
106
107
ErrorGenerator INVALID_NAMED_OBJECT_KEY(
108
  ObjectError errorType, Json::Value::Members const& extraFields)
109
2.18k
{
110
2.18k
  return INVALID_NAMED_OBJECT(
111
2.18k
    [](Json::Value const*, cmJSONState* state) -> std::string {
112
2.18k
      for (auto it = state->parseStack.rbegin();
113
2.18k
           it != state->parseStack.rend(); ++it) {
114
2.18k
        if (it->first.rfind("$vector_item_", 0) == 0) {
115
0
          continue;
116
0
        }
117
2.18k
        return cmStrCat('"', it->first, '"');
118
2.18k
      }
119
0
      return "root";
120
2.18k
    })(errorType, extraFields);
121
2.18k
}
122
}