Coverage Report

Created: 2026-10-28 06:38

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/CMake/Source/cmDefinitions.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 "cmDefinitions.h"
4
5
#include <cassert>
6
#include <unordered_set>
7
#include <utility>
8
9
#include <cm/string_view>
10
11
cmDefinitions::Def cmDefinitions::NoDef;
12
13
cmDefinitions::Def const& cmDefinitions::GetInternal(std::string const& key,
14
                                                     StackIter begin,
15
                                                     StackIter end, bool raise)
16
6.45M
{
17
6.45M
  assert(begin != end);
18
6.45M
  {
19
6.45M
    auto it = begin->Map.find(cm::String::borrow(key));
20
6.45M
    if (it != begin->Map.end()) {
21
512k
      return it->second;
22
512k
    }
23
6.45M
  }
24
5.94M
  StackIter it = begin;
25
5.94M
  ++it;
26
5.94M
  if (it == end) {
27
5.94M
    return cmDefinitions::NoDef;
28
5.94M
  }
29
0
  Def const& def = cmDefinitions::GetInternal(key, it, end, raise);
30
0
  if (!raise) {
31
0
    return def;
32
0
  }
33
0
  return begin->Map.emplace(key, def).first->second;
34
0
}
35
36
cmValue cmDefinitions::Get(std::string const& key, StackIter begin,
37
                           StackIter end)
38
6.45M
{
39
6.45M
  Def const& def = cmDefinitions::GetInternal(key, begin, end, false);
40
6.45M
  return def.Value ? cmValue(def.Value.str_if_stable()) : nullptr;
41
6.45M
}
42
43
void cmDefinitions::Raise(std::string const& key, StackIter begin,
44
                          StackIter end)
45
0
{
46
0
  cmDefinitions::GetInternal(key, begin, end, true);
47
0
}
48
49
bool cmDefinitions::HasKey(std::string const& key, StackIter begin,
50
                           StackIter end)
51
0
{
52
0
  for (StackIter it = begin; it != end; ++it) {
53
0
    if (it->Map.find(cm::String::borrow(key)) != it->Map.end()) {
54
0
      return true;
55
0
    }
56
0
  }
57
0
  return false;
58
0
}
59
60
cmDefinitions cmDefinitions::MakeClosure(StackIter begin, StackIter end)
61
0
{
62
0
  cmDefinitions closure;
63
0
  std::unordered_set<cm::string_view> undefined;
64
0
  for (StackIter it = begin; it != end; ++it) {
65
    // Consider local definitions.
66
0
    for (auto const& mi : it->Map) {
67
      // Use this key if it is not already set or unset.
68
0
      if (closure.Map.find(mi.first) == closure.Map.end() &&
69
0
          undefined.find(mi.first.view()) == undefined.end()) {
70
0
        if (mi.second.Value) {
71
0
          closure.Map.insert(mi);
72
0
        } else {
73
0
          undefined.emplace(mi.first.view());
74
0
        }
75
0
      }
76
0
    }
77
0
  }
78
0
  return closure;
79
0
}
80
81
std::vector<std::string> cmDefinitions::ClosureKeys(StackIter begin,
82
                                                    StackIter end,
83
                                                    UnsetKeys unsetKeys)
84
0
{
85
0
  std::vector<std::string> defined;
86
0
  std::unordered_set<cm::string_view> bound;
87
88
0
  for (StackIter it = begin; it != end; ++it) {
89
0
    defined.reserve(defined.size() + it->Map.size());
90
0
    for (auto const& mi : it->Map) {
91
      // Use this key if it is not already set or unset.
92
0
      if (bound.emplace(mi.first.view()).second &&
93
0
          (unsetKeys == UnsetKeys::Include || mi.second.Value)) {
94
0
        defined.push_back(*mi.first.str_if_stable());
95
0
      }
96
0
    }
97
0
  }
98
99
0
  return defined;
100
0
}
101
102
void cmDefinitions::Set(std::string const& key, cm::string_view value)
103
447k
{
104
447k
  this->Map[key] = Def(value);
105
447k
}
106
107
void cmDefinitions::Unset(std::string const& key)
108
17.7k
{
109
17.7k
  this->Map[key] = Def();
110
17.7k
}