cmDefinitions.cxx 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120
  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 <set>
  6. #include <utility>
  7. cmDefinitions::Def cmDefinitions::NoDef;
  8. cmDefinitions::Def const& cmDefinitions::GetInternal(const std::string& key,
  9. StackIter begin,
  10. StackIter end, bool raise)
  11. {
  12. assert(begin != end);
  13. {
  14. MapType::iterator it = begin->Map.find(key);
  15. if (it != begin->Map.end()) {
  16. it->second.Used = true;
  17. return it->second;
  18. }
  19. }
  20. StackIter it = begin;
  21. ++it;
  22. if (it == end) {
  23. return cmDefinitions::NoDef;
  24. }
  25. Def const& def = cmDefinitions::GetInternal(key, it, end, raise);
  26. if (!raise) {
  27. return def;
  28. }
  29. return begin->Map.emplace(key, def).first->second;
  30. }
  31. const std::string* cmDefinitions::Get(const std::string& key, StackIter begin,
  32. StackIter end)
  33. {
  34. Def const& def = cmDefinitions::GetInternal(key, begin, end, false);
  35. return def.Exists ? &def.Value : nullptr;
  36. }
  37. void cmDefinitions::Raise(const std::string& key, StackIter begin,
  38. StackIter end)
  39. {
  40. cmDefinitions::GetInternal(key, begin, end, true);
  41. }
  42. bool cmDefinitions::HasKey(const std::string& key, StackIter begin,
  43. StackIter end)
  44. {
  45. for (StackIter it = begin; it != end; ++it) {
  46. if (it->Map.find(key) != it->Map.end()) {
  47. return true;
  48. }
  49. }
  50. return false;
  51. }
  52. void cmDefinitions::Set(const std::string& key, cm::string_view value)
  53. {
  54. this->Map[key] = Def(value);
  55. }
  56. void cmDefinitions::Unset(const std::string& key)
  57. {
  58. this->Map[key] = Def();
  59. }
  60. std::vector<std::string> cmDefinitions::UnusedKeys() const
  61. {
  62. std::vector<std::string> keys;
  63. keys.reserve(this->Map.size());
  64. // Consider local definitions.
  65. for (auto const& mi : this->Map) {
  66. if (!mi.second.Used) {
  67. keys.push_back(mi.first);
  68. }
  69. }
  70. return keys;
  71. }
  72. cmDefinitions cmDefinitions::MakeClosure(StackIter begin, StackIter end)
  73. {
  74. cmDefinitions closure;
  75. std::set<std::string> undefined;
  76. for (StackIter it = begin; it != end; ++it) {
  77. // Consider local definitions.
  78. for (auto const& mi : it->Map) {
  79. // Use this key if it is not already set or unset.
  80. if (closure.Map.find(mi.first) == closure.Map.end() &&
  81. undefined.find(mi.first) == undefined.end()) {
  82. if (mi.second.Exists) {
  83. closure.Map.insert(mi);
  84. } else {
  85. undefined.insert(mi.first);
  86. }
  87. }
  88. }
  89. }
  90. return closure;
  91. }
  92. std::vector<std::string> cmDefinitions::ClosureKeys(StackIter begin,
  93. StackIter end)
  94. {
  95. std::vector<std::string> defined;
  96. std::set<std::string> bound;
  97. for (StackIter it = begin; it != end; ++it) {
  98. defined.reserve(defined.size() + it->Map.size());
  99. for (auto const& mi : it->Map) {
  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. }