cmake/Source/cmDefinitions.cxx

110 lines
3.0 KiB
C++
Raw Normal View History

2016-10-30 18:24:19 +01:00
/* Distributed under the OSI-approved BSD 3-Clause License. See accompanying
file Copyright.txt or https://cmake.org/licensing for details. */
2009-10-04 10:30:41 +03:00
#include "cmDefinitions.h"
2020-02-01 23:06:01 +01:00
#include <cassert>
#include <functional>
#include <unordered_set>
2016-10-30 18:24:19 +01:00
#include <utility>
2009-10-04 10:30:41 +03:00
2020-02-01 23:06:01 +01:00
#include <cm/string_view>
2015-08-17 11:37:30 +02:00
cmDefinitions::Def cmDefinitions::NoDef;
2009-10-04 10:30:41 +03:00
2016-07-09 11:21:54 +02:00
cmDefinitions::Def const& cmDefinitions::GetInternal(const std::string& key,
StackIter begin,
StackIter end, bool raise)
2009-10-04 10:30:41 +03:00
{
2015-08-17 11:37:30 +02:00
assert(begin != end);
2020-02-01 23:06:01 +01:00
{
auto it = begin->Map.find(cm::String::borrow(key));
if (it != begin->Map.end()) {
return it->second;
}
2016-07-09 11:21:54 +02:00
}
2015-08-17 11:37:30 +02:00
StackIter it = begin;
++it;
2016-07-09 11:21:54 +02:00
if (it == end) {
2015-08-17 11:37:30 +02:00
return cmDefinitions::NoDef;
2016-07-09 11:21:54 +02:00
}
2015-08-17 11:37:30 +02:00
Def const& def = cmDefinitions::GetInternal(key, it, end, raise);
2016-07-09 11:21:54 +02:00
if (!raise) {
2015-08-17 11:37:30 +02:00
return def;
2016-07-09 11:21:54 +02:00
}
2020-02-01 23:06:01 +01:00
return begin->Map.emplace(key, def).first->second;
2009-10-04 10:30:41 +03:00
}
2018-10-28 12:09:07 +01:00
const std::string* cmDefinitions::Get(const std::string& key, StackIter begin,
StackIter end)
2009-10-04 10:30:41 +03:00
{
2015-08-17 11:37:30 +02:00
Def const& def = cmDefinitions::GetInternal(key, begin, end, false);
2020-02-01 23:06:01 +01:00
return def.Value ? def.Value.str_if_stable() : nullptr;
2009-10-04 10:30:41 +03:00
}
2016-07-09 11:21:54 +02:00
void cmDefinitions::Raise(const std::string& key, StackIter begin,
StackIter end)
2009-10-04 10:30:41 +03:00
{
2015-08-17 11:37:30 +02:00
cmDefinitions::GetInternal(key, begin, end, true);
2009-10-04 10:30:41 +03:00
}
2016-07-09 11:21:54 +02:00
bool cmDefinitions::HasKey(const std::string& key, StackIter begin,
StackIter end)
2011-02-07 16:37:25 +01:00
{
2016-07-09 11:21:54 +02:00
for (StackIter it = begin; it != end; ++it) {
2020-02-01 23:06:01 +01:00
if (it->Map.find(cm::String::borrow(key)) != it->Map.end()) {
2015-08-17 11:37:30 +02:00
return true;
2011-02-07 16:37:25 +01:00
}
2016-07-09 11:21:54 +02:00
}
2015-08-17 11:37:30 +02:00
return false;
2011-02-07 16:37:25 +01:00
}
2016-07-09 11:21:54 +02:00
cmDefinitions cmDefinitions::MakeClosure(StackIter begin, StackIter end)
2009-10-04 10:30:41 +03:00
{
2015-08-17 11:37:30 +02:00
cmDefinitions closure;
2020-02-01 23:06:01 +01:00
std::unordered_set<cm::string_view> undefined;
2016-07-09 11:21:54 +02:00
for (StackIter it = begin; it != end; ++it) {
2015-08-17 11:37:30 +02:00
// Consider local definitions.
2018-01-26 17:06:56 +01:00
for (auto const& mi : it->Map) {
2015-08-17 11:37:30 +02:00
// Use this key if it is not already set or unset.
2018-01-26 17:06:56 +01:00
if (closure.Map.find(mi.first) == closure.Map.end() &&
2020-02-01 23:06:01 +01:00
undefined.find(mi.first.view()) == undefined.end()) {
if (mi.second.Value) {
2018-01-26 17:06:56 +01:00
closure.Map.insert(mi);
2016-07-09 11:21:54 +02:00
} else {
2020-02-01 23:06:01 +01:00
undefined.emplace(mi.first.view());
2009-10-04 10:30:41 +03:00
}
}
}
2016-07-09 11:21:54 +02:00
}
2015-08-17 11:37:30 +02:00
return closure;
2009-10-04 10:30:41 +03:00
}
2016-07-09 11:21:54 +02:00
std::vector<std::string> cmDefinitions::ClosureKeys(StackIter begin,
StackIter end)
2009-10-04 10:30:41 +03:00
{
2015-08-17 11:37:30 +02:00
std::vector<std::string> defined;
2020-02-01 23:06:01 +01:00
std::unordered_set<cm::string_view> bound;
2009-10-04 10:30:41 +03:00
2016-07-09 11:21:54 +02:00
for (StackIter it = begin; it != end; ++it) {
2015-08-17 11:37:30 +02:00
defined.reserve(defined.size() + it->Map.size());
2018-01-26 17:06:56 +01:00
for (auto const& mi : it->Map) {
2015-08-17 11:37:30 +02:00
// Use this key if it is not already set or unset.
2020-02-01 23:06:01 +01:00
if (bound.emplace(mi.first.view()).second && mi.second.Value) {
defined.push_back(*mi.first.str_if_stable());
2009-10-04 10:30:41 +03:00
}
}
2016-07-09 11:21:54 +02:00
}
2009-10-04 10:30:41 +03:00
2015-08-17 11:37:30 +02:00
return defined;
2009-10-04 10:30:41 +03:00
}
2020-02-01 23:06:01 +01:00
void cmDefinitions::Set(const std::string& key, cm::string_view value)
{
this->Map[key] = Def(value);
}
void cmDefinitions::Unset(const std::string& key)
{
this->Map[key] = Def();
}