cmListFileCache.h 7.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275
  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 <cstddef>
  6. #include <iosfwd>
  7. #include <memory>
  8. #include <string>
  9. #include <utility>
  10. #include <vector>
  11. #include <cm/optional>
  12. #include "cmStateSnapshot.h"
  13. #include "cmSystemTools.h"
  14. /** \class cmListFileCache
  15. * \brief A class to cache list file contents.
  16. *
  17. * cmListFileCache is a class used to cache the contents of parsed
  18. * cmake list files.
  19. */
  20. class cmMessenger;
  21. struct cmCommandContext
  22. {
  23. struct cmCommandName
  24. {
  25. std::string Original;
  26. std::string Lower;
  27. cmCommandName() = default;
  28. cmCommandName(std::string name)
  29. : Original(std::move(name))
  30. , Lower(cmSystemTools::LowerCase(this->Original))
  31. {
  32. }
  33. } Name;
  34. long Line = 0;
  35. cmCommandContext() = default;
  36. cmCommandContext(std::string name, long line)
  37. : Name(std::move(name))
  38. , Line(line)
  39. {
  40. }
  41. };
  42. struct cmListFileArgument
  43. {
  44. enum Delimiter
  45. {
  46. Unquoted,
  47. Quoted,
  48. Bracket
  49. };
  50. cmListFileArgument() = default;
  51. cmListFileArgument(std::string v, Delimiter d, long line)
  52. : Value(std::move(v))
  53. , Delim(d)
  54. , Line(line)
  55. {
  56. }
  57. bool operator==(const cmListFileArgument& r) const
  58. {
  59. return (this->Value == r.Value) && (this->Delim == r.Delim);
  60. }
  61. bool operator!=(const cmListFileArgument& r) const { return !(*this == r); }
  62. std::string Value;
  63. Delimiter Delim = Unquoted;
  64. long Line = 0;
  65. };
  66. class cmListFileContext
  67. {
  68. public:
  69. std::string Name;
  70. std::string FilePath;
  71. long Line = 0;
  72. static long const DeferPlaceholderLine = -1;
  73. cm::optional<std::string> DeferId;
  74. cmListFileContext() = default;
  75. cmListFileContext(std::string name, std::string filePath, long line)
  76. : Name(std::move(name))
  77. , FilePath(std::move(filePath))
  78. , Line(line)
  79. {
  80. }
  81. #if __cplusplus < 201703L && (!defined(_MSVC_LANG) || _MSVC_LANG < 201703L)
  82. cmListFileContext(const cmListFileContext& /*other*/) = default;
  83. cmListFileContext(cmListFileContext&& /*other*/) = default;
  84. cmListFileContext& operator=(const cmListFileContext& /*other*/) = default;
  85. cmListFileContext& operator=(cmListFileContext&& /*other*/) = delete;
  86. #endif
  87. static cmListFileContext FromCommandContext(
  88. cmCommandContext const& lfcc, std::string const& fileName,
  89. cm::optional<std::string> deferId = {})
  90. {
  91. cmListFileContext lfc;
  92. lfc.FilePath = fileName;
  93. lfc.Line = lfcc.Line;
  94. lfc.Name = lfcc.Name.Original;
  95. lfc.DeferId = std::move(deferId);
  96. return lfc;
  97. }
  98. };
  99. std::ostream& operator<<(std::ostream&, cmListFileContext const&);
  100. bool operator<(const cmListFileContext& lhs, const cmListFileContext& rhs);
  101. bool operator==(cmListFileContext const& lhs, cmListFileContext const& rhs);
  102. bool operator!=(cmListFileContext const& lhs, cmListFileContext const& rhs);
  103. class cmListFileFunction
  104. {
  105. public:
  106. cmListFileFunction(std::string name, long line,
  107. std::vector<cmListFileArgument> args)
  108. : Impl{ std::make_shared<Implementation>(std::move(name), line,
  109. std::move(args)) }
  110. {
  111. }
  112. std::string const& OriginalName() const noexcept
  113. {
  114. return this->Impl->Name.Original;
  115. }
  116. std::string const& LowerCaseName() const noexcept
  117. {
  118. return this->Impl->Name.Lower;
  119. }
  120. long Line() const noexcept { return this->Impl->Line; }
  121. std::vector<cmListFileArgument> const& Arguments() const noexcept
  122. {
  123. return this->Impl->Arguments;
  124. }
  125. operator cmCommandContext const&() const noexcept { return *this->Impl; }
  126. private:
  127. struct Implementation : public cmCommandContext
  128. {
  129. Implementation(std::string name, long line,
  130. std::vector<cmListFileArgument> args)
  131. : cmCommandContext{ std::move(name), line }
  132. , Arguments{ std::move(args) }
  133. {
  134. }
  135. std::vector<cmListFileArgument> Arguments;
  136. };
  137. std::shared_ptr<Implementation const> Impl;
  138. };
  139. // Represent a backtrace (call stack). Provide value semantics
  140. // but use efficient reference-counting underneath to avoid copies.
  141. class cmListFileBacktrace
  142. {
  143. public:
  144. // Default-constructed backtrace may not be used until after
  145. // set via assignment from a backtrace constructed with a
  146. // valid snapshot.
  147. cmListFileBacktrace() = default;
  148. // Construct an empty backtrace whose bottom sits in the directory
  149. // indicated by the given valid snapshot.
  150. cmListFileBacktrace(cmStateSnapshot const& snapshot);
  151. cmStateSnapshot GetBottom() const;
  152. // Get a backtrace with the given file scope added to the top.
  153. // May not be called until after construction with a valid snapshot.
  154. cmListFileBacktrace Push(std::string const& file) const;
  155. // Get a backtrace with the given call context added to the top.
  156. // May not be called until after construction with a valid snapshot.
  157. cmListFileBacktrace Push(cmListFileContext const& lfc) const;
  158. // Get a backtrace with the top level removed.
  159. // May not be called until after a matching Push.
  160. cmListFileBacktrace Pop() const;
  161. // Get the context at the top of the backtrace.
  162. // This may be called only if Empty() would return false.
  163. cmListFileContext const& Top() const;
  164. // Get the number of 'frames' in this backtrace
  165. size_t Depth() const;
  166. // Return true if this backtrace is empty.
  167. bool Empty() const;
  168. private:
  169. struct Entry;
  170. std::shared_ptr<Entry const> TopEntry;
  171. cmListFileBacktrace(std::shared_ptr<Entry const> parent,
  172. cmListFileContext const& lfc);
  173. cmListFileBacktrace(std::shared_ptr<Entry const> top);
  174. };
  175. // Wrap type T as a value with a backtrace. For purposes of
  176. // ordering and equality comparison, only the original value is
  177. // used. The backtrace is considered incidental.
  178. template <typename T>
  179. class BT
  180. {
  181. public:
  182. BT(T v = T(), cmListFileBacktrace bt = cmListFileBacktrace())
  183. : Value(std::move(v))
  184. , Backtrace(std::move(bt))
  185. {
  186. }
  187. T Value;
  188. cmListFileBacktrace Backtrace;
  189. friend bool operator==(BT<T> const& l, BT<T> const& r)
  190. {
  191. return l.Value == r.Value;
  192. }
  193. friend bool operator<(BT<T> const& l, BT<T> const& r)
  194. {
  195. return l.Value < r.Value;
  196. }
  197. friend bool operator==(BT<T> const& l, T const& r) { return l.Value == r; }
  198. friend bool operator==(T const& l, BT<T> const& r) { return l == r.Value; }
  199. };
  200. std::ostream& operator<<(std::ostream& os, BT<std::string> const& s);
  201. // Wrap type T as a value with potentially multiple backtraces. For purposes
  202. // of ordering and equality comparison, only the original value is used. The
  203. // backtrace is considered incidental.
  204. template <typename T>
  205. class BTs
  206. {
  207. public:
  208. BTs(T v = T(), cmListFileBacktrace bt = cmListFileBacktrace())
  209. : Value(std::move(v))
  210. {
  211. this->Backtraces.emplace_back(std::move(bt));
  212. }
  213. T Value;
  214. std::vector<cmListFileBacktrace> Backtraces;
  215. friend bool operator==(BTs<T> const& l, BTs<T> const& r)
  216. {
  217. return l.Value == r.Value;
  218. }
  219. friend bool operator<(BTs<T> const& l, BTs<T> const& r)
  220. {
  221. return l.Value < r.Value;
  222. }
  223. friend bool operator==(BTs<T> const& l, T const& r) { return l.Value == r; }
  224. friend bool operator==(T const& l, BTs<T> const& r) { return l == r.Value; }
  225. };
  226. std::vector<BT<std::string>> ExpandListWithBacktrace(
  227. std::string const& list,
  228. cmListFileBacktrace const& bt = cmListFileBacktrace());
  229. struct cmListFile
  230. {
  231. bool ParseFile(const char* path, cmMessenger* messenger,
  232. cmListFileBacktrace const& lfbt);
  233. bool ParseString(const char* str, const char* virtual_filename,
  234. cmMessenger* messenger, cmListFileBacktrace const& lfbt);
  235. std::vector<cmListFileFunction> Functions;
  236. };