cmDefinitions.cxx 3.1 KB

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