cmake/Source/cmDefinitions.cxx

119 lines
3.3 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"
2015-08-17 11:37:30 +02:00
#include <assert.h>
2016-10-30 18:24:19 +01:00
#include <set>
#include <utility>
2009-10-04 10:30:41 +03:00
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);
MapType::iterator i = begin->Map.find(key);
2016-07-09 11:21:54 +02:00
if (i != begin->Map.end()) {
2015-08-17 11:37:30 +02:00
i->second.Used = true;
2009-10-04 10:30:41 +03:00
return i->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
}
2015-08-17 11:37:30 +02:00
return begin->Map.insert(MapType::value_type(key, def)).first->second;
2009-10-04 10:30:41 +03:00
}
2016-07-09 11:21:54 +02:00
const char* 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);
2016-10-30 18:24:19 +01:00
return def.Exists ? def.c_str() : CM_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) {
2015-08-17 11:37:30 +02:00
MapType::const_iterator i = it->Map.find(key);
2016-07-09 11:21:54 +02:00
if (i != 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
}
2015-08-17 11:37:30 +02:00
void cmDefinitions::Set(const std::string& key, const char* value)
2009-10-04 10:30:41 +03:00
{
2015-08-17 11:37:30 +02:00
Def def(value);
this->Map[key] = def;
2009-10-04 10:30:41 +03:00
}
2015-08-17 11:37:30 +02:00
std::vector<std::string> cmDefinitions::UnusedKeys() const
2009-10-04 10:30:41 +03:00
{
2015-08-17 11:37:30 +02:00
std::vector<std::string> keys;
keys.reserve(this->Map.size());
// Consider local definitions.
2016-07-09 11:21:54 +02:00
for (MapType::const_iterator mi = this->Map.begin(); mi != this->Map.end();
++mi) {
if (!mi->second.Used) {
2015-08-17 11:37:30 +02:00
keys.push_back(mi->first);
}
2016-07-09 11:21:54 +02:00
}
2015-08-17 11:37:30 +02:00
return keys;
2009-10-04 10:30:41 +03: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;
std::set<std::string> 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.
2016-07-09 11:21:54 +02:00
for (MapType::const_iterator mi = it->Map.begin(); mi != it->Map.end();
++mi) {
2015-08-17 11:37:30 +02:00
// Use this key if it is not already set or unset.
2016-07-09 11:21:54 +02:00
if (closure.Map.find(mi->first) == closure.Map.end() &&
undefined.find(mi->first) == undefined.end()) {
if (mi->second.Exists) {
2015-08-17 11:37:30 +02:00
closure.Map.insert(*mi);
2016-07-09 11:21:54 +02:00
} else {
2015-08-17 11:37:30 +02:00
undefined.insert(mi->first);
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::set<std::string> bound;
std::vector<std::string> defined;
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());
2016-07-09 11:21:54 +02:00
for (MapType::const_iterator mi = it->Map.begin(); mi != it->Map.end();
++mi) {
2015-08-17 11:37:30 +02:00
// Use this key if it is not already set or unset.
2016-07-09 11:21:54 +02:00
if (bound.insert(mi->first).second && mi->second.Exists) {
2015-08-17 11:37:30 +02:00
defined.push_back(mi->first);
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
}