cmListFileCache.h 7.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273
  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. static cmListFileContext FromCommandContext(
  82. cmCommandContext const& lfcc, std::string const& fileName,
  83. cm::optional<std::string> deferId = {})
  84. {
  85. cmListFileContext lfc;
  86. lfc.FilePath = fileName;
  87. lfc.Line = lfcc.Line;
  88. lfc.Name = lfcc.Name.Original;
  89. lfc.DeferId = std::move(deferId);
  90. return lfc;
  91. }
  92. };
  93. std::ostream& operator<<(std::ostream&, cmListFileContext const&);
  94. bool operator<(const cmListFileContext& lhs, const cmListFileContext& rhs);
  95. bool operator==(cmListFileContext const& lhs, cmListFileContext const& rhs);
  96. bool operator!=(cmListFileContext const& lhs, cmListFileContext const& rhs);
  97. class cmListFileFunction
  98. {
  99. public:
  100. cmListFileFunction(std::string name, long line,
  101. std::vector<cmListFileArgument> args)
  102. : Impl{ std::make_shared<Implementation>(std::move(name), line,
  103. std::move(args)) }
  104. {
  105. }
  106. std::string const& OriginalName() const noexcept
  107. {
  108. return this->Impl->Name.Original;
  109. }
  110. std::string const& LowerCaseName() const noexcept
  111. {
  112. return this->Impl->Name.Lower;
  113. }
  114. long Line() const noexcept { return this->Impl->Line; }
  115. std::vector<cmListFileArgument> const& Arguments() const noexcept
  116. {
  117. return this->Impl->Arguments;
  118. }
  119. operator cmCommandContext const&() const noexcept { return *this->Impl; }
  120. private:
  121. struct Implementation : public cmCommandContext
  122. {
  123. Implementation(std::string name, long line,
  124. std::vector<cmListFileArgument> args)
  125. : cmCommandContext{ std::move(name), line }
  126. , Arguments{ std::move(args) }
  127. {
  128. }
  129. std::vector<cmListFileArgument> Arguments;
  130. };
  131. std::shared_ptr<Implementation const> Impl;
  132. };
  133. // Represent a backtrace (call stack). Provide value semantics
  134. // but use efficient reference-counting underneath to avoid copies.
  135. class cmListFileBacktrace
  136. {
  137. public:
  138. // Default-constructed backtrace may not be used until after
  139. // set via assignment from a backtrace constructed with a
  140. // valid snapshot.
  141. cmListFileBacktrace() = default;
  142. // Construct an empty backtrace whose bottom sits in the directory
  143. // indicated by the given valid snapshot.
  144. cmListFileBacktrace(cmStateSnapshot const& snapshot);
  145. cmStateSnapshot GetBottom() const;
  146. // Get a backtrace with the given file scope added to the top.
  147. // May not be called until after construction with a valid snapshot.
  148. cmListFileBacktrace Push(std::string const& file) const;
  149. // Get a backtrace with the given call context added to the top.
  150. // May not be called until after construction with a valid snapshot.
  151. cmListFileBacktrace Push(cmListFileContext const& lfc) const;
  152. // Get a backtrace with the top level removed.
  153. // May not be called until after a matching Push.
  154. cmListFileBacktrace Pop() const;
  155. // Get the context at the top of the backtrace.
  156. // This may be called only if Empty() would return false.
  157. cmListFileContext const& Top() const;
  158. // Print the top of the backtrace.
  159. void PrintTitle(std::ostream& out) const;
  160. // Print the call stack below the top of the backtrace.
  161. void PrintCallStack(std::ostream& out) const;
  162. // Get the number of 'frames' in this backtrace
  163. size_t Depth() const;
  164. // Return true if this backtrace is empty.
  165. bool Empty() const;
  166. private:
  167. struct Entry;
  168. std::shared_ptr<Entry const> TopEntry;
  169. cmListFileBacktrace(std::shared_ptr<Entry const> parent,
  170. cmListFileContext const& lfc);
  171. cmListFileBacktrace(std::shared_ptr<Entry const> top);
  172. };
  173. // Wrap type T as a value with a backtrace. For purposes of
  174. // ordering and equality comparison, only the original value is
  175. // used. The backtrace is considered incidental.
  176. template <typename T>
  177. class BT
  178. {
  179. public:
  180. BT(T v = T(), cmListFileBacktrace bt = cmListFileBacktrace())
  181. : Value(std::move(v))
  182. , Backtrace(std::move(bt))
  183. {
  184. }
  185. T Value;
  186. cmListFileBacktrace Backtrace;
  187. friend bool operator==(BT<T> const& l, BT<T> const& r)
  188. {
  189. return l.Value == r.Value;
  190. }
  191. friend bool operator<(BT<T> const& l, BT<T> const& r)
  192. {
  193. return l.Value < r.Value;
  194. }
  195. friend bool operator==(BT<T> const& l, T const& r) { return l.Value == r; }
  196. friend bool operator==(T const& l, BT<T> const& r) { return l == r.Value; }
  197. };
  198. std::ostream& operator<<(std::ostream& os, BT<std::string> const& s);
  199. // Wrap type T as a value with potentially multiple backtraces. For purposes
  200. // of ordering and equality comparison, only the original value is used. The
  201. // backtrace is considered incidental.
  202. template <typename T>
  203. class BTs
  204. {
  205. public:
  206. BTs(T v = T(), cmListFileBacktrace bt = cmListFileBacktrace())
  207. : Value(std::move(v))
  208. {
  209. Backtraces.emplace_back(std::move(bt));
  210. }
  211. T Value;
  212. std::vector<cmListFileBacktrace> Backtraces;
  213. friend bool operator==(BTs<T> const& l, BTs<T> const& r)
  214. {
  215. return l.Value == r.Value;
  216. }
  217. friend bool operator<(BTs<T> const& l, BTs<T> const& r)
  218. {
  219. return l.Value < r.Value;
  220. }
  221. friend bool operator==(BTs<T> const& l, T const& r) { return l.Value == r; }
  222. friend bool operator==(T const& l, BTs<T> const& r) { return l == r.Value; }
  223. };
  224. std::vector<BT<std::string>> ExpandListWithBacktrace(
  225. std::string const& list,
  226. cmListFileBacktrace const& bt = cmListFileBacktrace());
  227. struct cmListFile
  228. {
  229. bool ParseFile(const char* path, cmMessenger* messenger,
  230. cmListFileBacktrace const& lfbt);
  231. bool ParseString(const char* str, const char* virtual_filename,
  232. cmMessenger* messenger, cmListFileBacktrace const& lfbt);
  233. std::vector<cmListFileFunction> Functions;
  234. };