cmListFileCache.h 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246
  1. /* Distributed under the OSI-approved BSD 3-Clause License. See accompanying
  2. file Copyright.txt or https://cmake.org/licensing for details. */
  3. #pragma once
  4. #include "cmConfigure.h" // IWYU pragma: keep
  5. #include <iosfwd>
  6. #include <memory>
  7. #include <string>
  8. #include <utility>
  9. #include <vector>
  10. #include <cm/optional>
  11. #include "cmSystemTools.h"
  12. /** \class cmListFileCache
  13. * \brief A class to cache list file contents.
  14. *
  15. * cmListFileCache is a class used to cache the contents of parsed
  16. * cmake list files.
  17. */
  18. class cmMessenger;
  19. struct cmListFileArgument
  20. {
  21. enum Delimiter
  22. {
  23. Unquoted,
  24. Quoted,
  25. Bracket
  26. };
  27. cmListFileArgument() = default;
  28. cmListFileArgument(std::string v, Delimiter d, long line)
  29. : Value(std::move(v))
  30. , Delim(d)
  31. , Line(line)
  32. {
  33. }
  34. bool operator==(const cmListFileArgument& r) const
  35. {
  36. return (this->Value == r.Value) && (this->Delim == r.Delim);
  37. }
  38. bool operator!=(const cmListFileArgument& r) const { return !(*this == r); }
  39. std::string Value;
  40. Delimiter Delim = Unquoted;
  41. long Line = 0;
  42. };
  43. class cmListFileFunction
  44. {
  45. public:
  46. cmListFileFunction(std::string name, long line,
  47. std::vector<cmListFileArgument> args)
  48. : Impl{ std::make_shared<Implementation>(std::move(name), line,
  49. std::move(args)) }
  50. {
  51. }
  52. std::string const& OriginalName() const noexcept
  53. {
  54. return this->Impl->OriginalName;
  55. }
  56. std::string const& LowerCaseName() const noexcept
  57. {
  58. return this->Impl->LowerCaseName;
  59. }
  60. long Line() const noexcept { return this->Impl->Line; }
  61. std::vector<cmListFileArgument> const& Arguments() const noexcept
  62. {
  63. return this->Impl->Arguments;
  64. }
  65. private:
  66. struct Implementation
  67. {
  68. Implementation(std::string name, long line,
  69. std::vector<cmListFileArgument> args)
  70. : OriginalName{ std::move(name) }
  71. , LowerCaseName{ cmSystemTools::LowerCase(this->OriginalName) }
  72. , Line{ line }
  73. , Arguments{ std::move(args) }
  74. {
  75. }
  76. std::string OriginalName;
  77. std::string LowerCaseName;
  78. long Line = 0;
  79. std::vector<cmListFileArgument> Arguments;
  80. };
  81. std::shared_ptr<Implementation const> Impl;
  82. };
  83. class cmListFileContext
  84. {
  85. public:
  86. std::string Name;
  87. std::string FilePath;
  88. long Line = 0;
  89. static long const DeferPlaceholderLine = -1;
  90. cm::optional<std::string> DeferId;
  91. cmListFileContext() = default;
  92. cmListFileContext(cmListFileContext&& /*other*/) = default;
  93. cmListFileContext(const cmListFileContext& /*other*/) = default;
  94. cmListFileContext& operator=(const cmListFileContext& /*other*/) = default;
  95. #if __cplusplus >= 201703L || (defined(_MSVC_LANG) && _MSVC_LANG >= 201703L)
  96. cmListFileContext& operator=(cmListFileContext&& /*other*/) = default;
  97. #else
  98. // The move assignment operators for several STL classes did not become
  99. // noexcept until C++17, which causes some tools to warn about this move
  100. // assignment operator throwing an exception when it shouldn't.
  101. cmListFileContext& operator=(cmListFileContext&& /*other*/) = delete;
  102. #endif
  103. cmListFileContext(std::string name, std::string filePath, long line)
  104. : Name(std::move(name))
  105. , FilePath(std::move(filePath))
  106. , Line(line)
  107. {
  108. }
  109. static cmListFileContext FromListFileFunction(
  110. cmListFileFunction const& lff, std::string const& fileName,
  111. cm::optional<std::string> deferId = {})
  112. {
  113. cmListFileContext lfc;
  114. lfc.FilePath = fileName;
  115. lfc.Line = lff.Line();
  116. lfc.Name = lff.OriginalName();
  117. lfc.DeferId = std::move(deferId);
  118. return lfc;
  119. }
  120. };
  121. std::ostream& operator<<(std::ostream&, cmListFileContext const&);
  122. bool operator<(const cmListFileContext& lhs, const cmListFileContext& rhs);
  123. bool operator==(cmListFileContext const& lhs, cmListFileContext const& rhs);
  124. bool operator!=(cmListFileContext const& lhs, cmListFileContext const& rhs);
  125. // Represent a backtrace (call stack). Provide value semantics
  126. // but use efficient reference-counting underneath to avoid copies.
  127. class cmListFileBacktrace
  128. {
  129. public:
  130. // Default-constructed backtrace is empty.
  131. cmListFileBacktrace() = default;
  132. // Get a backtrace with the given file scope added to the top.
  133. cmListFileBacktrace Push(std::string const& file) const;
  134. // Get a backtrace with the given call context added to the top.
  135. cmListFileBacktrace Push(cmListFileContext const& lfc) const;
  136. // Get a backtrace with the top level removed.
  137. // May not be called until after a matching Push.
  138. cmListFileBacktrace Pop() const;
  139. // Get the context at the top of the backtrace.
  140. // This may be called only if Empty() would return false.
  141. cmListFileContext const& Top() const;
  142. // Return true if this backtrace is empty.
  143. bool Empty() const;
  144. private:
  145. struct Entry;
  146. std::shared_ptr<Entry const> TopEntry;
  147. cmListFileBacktrace(std::shared_ptr<Entry const> parent,
  148. cmListFileContext const& lfc);
  149. cmListFileBacktrace(std::shared_ptr<Entry const> top);
  150. };
  151. // Wrap type T as a value with a backtrace. For purposes of
  152. // ordering and equality comparison, only the original value is
  153. // used. The backtrace is considered incidental.
  154. template <typename T>
  155. class BT
  156. {
  157. public:
  158. BT(T v = T(), cmListFileBacktrace bt = cmListFileBacktrace())
  159. : Value(std::move(v))
  160. , Backtrace(std::move(bt))
  161. {
  162. }
  163. T Value;
  164. cmListFileBacktrace Backtrace;
  165. friend bool operator==(BT<T> const& l, BT<T> const& r)
  166. {
  167. return l.Value == r.Value;
  168. }
  169. friend bool operator<(BT<T> const& l, BT<T> const& r)
  170. {
  171. return l.Value < r.Value;
  172. }
  173. friend bool operator==(BT<T> const& l, T const& r) { return l.Value == r; }
  174. friend bool operator==(T const& l, BT<T> const& r) { return l == r.Value; }
  175. };
  176. std::ostream& operator<<(std::ostream& os, BT<std::string> const& s);
  177. // Wrap type T as a value with potentially multiple backtraces. For purposes
  178. // of ordering and equality comparison, only the original value is used. The
  179. // backtrace is considered incidental.
  180. template <typename T>
  181. class BTs
  182. {
  183. public:
  184. BTs(T v = T(), cmListFileBacktrace bt = cmListFileBacktrace())
  185. : Value(std::move(v))
  186. {
  187. this->Backtraces.emplace_back(std::move(bt));
  188. }
  189. T Value;
  190. std::vector<cmListFileBacktrace> Backtraces;
  191. friend bool operator==(BTs<T> const& l, BTs<T> const& r)
  192. {
  193. return l.Value == r.Value;
  194. }
  195. friend bool operator<(BTs<T> const& l, BTs<T> const& r)
  196. {
  197. return l.Value < r.Value;
  198. }
  199. friend bool operator==(BTs<T> const& l, T const& r) { return l.Value == r; }
  200. friend bool operator==(T const& l, BTs<T> const& r) { return l == r.Value; }
  201. };
  202. std::vector<BT<std::string>> ExpandListWithBacktrace(
  203. std::string const& list,
  204. cmListFileBacktrace const& bt = cmListFileBacktrace());
  205. struct cmListFile
  206. {
  207. bool ParseFile(const char* path, cmMessenger* messenger,
  208. cmListFileBacktrace const& lfbt);
  209. bool ParseString(const char* str, const char* virtual_filename,
  210. cmMessenger* messenger, cmListFileBacktrace const& lfbt);
  211. std::vector<cmListFileFunction> Functions;
  212. };