cmDefinitions.cxx 3.3 KB

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