cmDefinitions.cxx 3.7 KB

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