cmDefinitions.cxx 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119
  1. /* Distributed under the OSI-approved BSD 3-Clause License. See accompanying
  2. file Copyright.txt or https://cmake.org/licensing for details. */
  3. #include "cmDefinitions.h"
  4. #include <assert.h>
  5. #include <cmConfigure.h>
  6. #include <set>
  7. #include <utility>
  8. cmDefinitions::Def cmDefinitions::NoDef;
  9. cmDefinitions::Def const& cmDefinitions::GetInternal(const std::string& key,
  10. StackIter begin,
  11. StackIter end, bool raise)
  12. {
  13. assert(begin != end);
  14. MapType::iterator i = begin->Map.find(key);
  15. if (i != begin->Map.end()) {
  16. i->second.Used = true;
  17. return i->second;
  18. }
  19. StackIter it = begin;
  20. ++it;
  21. if (it == end) {
  22. return cmDefinitions::NoDef;
  23. }
  24. Def const& def = cmDefinitions::GetInternal(key, it, end, raise);
  25. if (!raise) {
  26. return def;
  27. }
  28. return begin->Map.insert(MapType::value_type(key, def)).first->second;
  29. }
  30. const char* cmDefinitions::Get(const std::string& key, StackIter begin,
  31. StackIter end)
  32. {
  33. Def const& def = cmDefinitions::GetInternal(key, begin, end, false);
  34. return def.Exists ? def.c_str() : CM_NULLPTR;
  35. }
  36. void cmDefinitions::Raise(const std::string& key, StackIter begin,
  37. StackIter end)
  38. {
  39. cmDefinitions::GetInternal(key, begin, end, true);
  40. }
  41. bool cmDefinitions::HasKey(const std::string& key, StackIter begin,
  42. StackIter end)
  43. {
  44. for (StackIter it = begin; it != end; ++it) {
  45. MapType::const_iterator i = it->Map.find(key);
  46. if (i != it->Map.end()) {
  47. return true;
  48. }
  49. }
  50. return false;
  51. }
  52. void cmDefinitions::Set(const std::string& key, const char* value)
  53. {
  54. Def def(value);
  55. this->Map[key] = def;
  56. }
  57. std::vector<std::string> cmDefinitions::UnusedKeys() const
  58. {
  59. std::vector<std::string> keys;
  60. keys.reserve(this->Map.size());
  61. // Consider local definitions.
  62. for (MapType::const_iterator mi = this->Map.begin(); mi != this->Map.end();
  63. ++mi) {
  64. if (!mi->second.Used) {
  65. keys.push_back(mi->first);
  66. }
  67. }
  68. return keys;
  69. }
  70. cmDefinitions cmDefinitions::MakeClosure(StackIter begin, StackIter end)
  71. {
  72. cmDefinitions closure;
  73. std::set<std::string> undefined;
  74. for (StackIter it = begin; it != end; ++it) {
  75. // Consider local definitions.
  76. for (MapType::const_iterator mi = it->Map.begin(); mi != it->Map.end();
  77. ++mi) {
  78. // Use this key if it is not already set or unset.
  79. if (closure.Map.find(mi->first) == closure.Map.end() &&
  80. undefined.find(mi->first) == undefined.end()) {
  81. if (mi->second.Exists) {
  82. closure.Map.insert(*mi);
  83. } else {
  84. undefined.insert(mi->first);
  85. }
  86. }
  87. }
  88. }
  89. return closure;
  90. }
  91. std::vector<std::string> cmDefinitions::ClosureKeys(StackIter begin,
  92. StackIter end)
  93. {
  94. std::set<std::string> bound;
  95. std::vector<std::string> defined;
  96. for (StackIter it = begin; it != end; ++it) {
  97. defined.reserve(defined.size() + it->Map.size());
  98. for (MapType::const_iterator mi = it->Map.begin(); mi != it->Map.end();
  99. ++mi) {
  100. // Use this key if it is not already set or unset.
  101. if (bound.insert(mi->first).second && mi->second.Exists) {
  102. defined.push_back(mi->first);
  103. }
  104. }
  105. }
  106. return defined;
  107. }