/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 | 8 | { |
17 | 8 | assert(begin != end); |
18 | 8 | { |
19 | 8 | auto it = begin->Map.find(cm::String::borrow(key)); |
20 | 8 | if (it != begin->Map.end()) { |
21 | 6 | return it->second; |
22 | 6 | } |
23 | 8 | } |
24 | 2 | StackIter it = begin; |
25 | 2 | ++it; |
26 | 2 | if (it == end) { |
27 | 2 | return cmDefinitions::NoDef; |
28 | 2 | } |
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 | 8 | { |
39 | 8 | Def const& def = cmDefinitions::GetInternal(key, begin, end, false); |
40 | 8 | return def.Value ? cmValue(def.Value.str_if_stable()) : nullptr; |
41 | 8 | } |
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 | 0 | { |
84 | 0 | std::vector<std::string> defined; |
85 | 0 | std::unordered_set<cm::string_view> bound; |
86 | |
|
87 | 0 | for (StackIter it = begin; it != end; ++it) { |
88 | 0 | defined.reserve(defined.size() + it->Map.size()); |
89 | 0 | for (auto const& mi : it->Map) { |
90 | | // Use this key if it is not already set or unset. |
91 | 0 | if (bound.emplace(mi.first.view()).second && mi.second.Value) { |
92 | 0 | defined.push_back(*mi.first.str_if_stable()); |
93 | 0 | } |
94 | 0 | } |
95 | 0 | } |
96 | |
|
97 | 0 | return defined; |
98 | 0 | } |
99 | | |
100 | | void cmDefinitions::Set(std::string const& key, cm::string_view value) |
101 | 97 | { |
102 | 97 | this->Map[key] = Def(value); |
103 | 97 | } |
104 | | |
105 | | void cmDefinitions::Unset(std::string const& key) |
106 | 1 | { |
107 | 1 | this->Map[key] = Def(); |
108 | 1 | } |