cmDefinitions.cxx 3.0 KB

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