cmListFileCache.h 7.1 KB

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