cmListFileCache.h 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230
  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. /** \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 Lower;
  25. std::string Original;
  26. cmCommandName() = default;
  27. cmCommandName(std::string const& name) { *this = name; }
  28. cmCommandName& operator=(std::string const& name);
  29. } Name;
  30. long Line = 0;
  31. cmCommandContext() = default;
  32. cmCommandContext(const char* name, int line)
  33. : Name(name)
  34. , Line(line)
  35. {
  36. }
  37. };
  38. struct cmListFileArgument
  39. {
  40. enum Delimiter
  41. {
  42. Unquoted,
  43. Quoted,
  44. Bracket
  45. };
  46. cmListFileArgument() = default;
  47. cmListFileArgument(std::string v, Delimiter d, long line)
  48. : Value(std::move(v))
  49. , Delim(d)
  50. , Line(line)
  51. {
  52. }
  53. bool operator==(const cmListFileArgument& r) const
  54. {
  55. return (this->Value == r.Value) && (this->Delim == r.Delim);
  56. }
  57. bool operator!=(const cmListFileArgument& r) const { return !(*this == r); }
  58. std::string Value;
  59. Delimiter Delim = Unquoted;
  60. long Line = 0;
  61. };
  62. class cmListFileContext
  63. {
  64. public:
  65. std::string Name;
  66. std::string FilePath;
  67. long Line = 0;
  68. static long const DeferPlaceholderLine = -1;
  69. cm::optional<std::string> DeferId;
  70. cmListFileContext() = default;
  71. cmListFileContext(std::string name, std::string filePath, long line)
  72. : Name(std::move(name))
  73. , FilePath(std::move(filePath))
  74. , Line(line)
  75. {
  76. }
  77. static cmListFileContext FromCommandContext(
  78. cmCommandContext const& lfcc, std::string const& fileName,
  79. cm::optional<std::string> deferId = {})
  80. {
  81. cmListFileContext lfc;
  82. lfc.FilePath = fileName;
  83. lfc.Line = lfcc.Line;
  84. lfc.Name = lfcc.Name.Original;
  85. lfc.DeferId = std::move(deferId);
  86. return lfc;
  87. }
  88. };
  89. std::ostream& operator<<(std::ostream&, cmListFileContext const&);
  90. bool operator<(const cmListFileContext& lhs, const cmListFileContext& rhs);
  91. bool operator==(cmListFileContext const& lhs, cmListFileContext const& rhs);
  92. bool operator!=(cmListFileContext const& lhs, cmListFileContext const& rhs);
  93. struct cmListFileFunction : public cmCommandContext
  94. {
  95. std::vector<cmListFileArgument> Arguments;
  96. };
  97. // Represent a backtrace (call stack). Provide value semantics
  98. // but use efficient reference-counting underneath to avoid copies.
  99. class cmListFileBacktrace
  100. {
  101. public:
  102. // Default-constructed backtrace may not be used until after
  103. // set via assignment from a backtrace constructed with a
  104. // valid snapshot.
  105. cmListFileBacktrace() = default;
  106. // Construct an empty backtrace whose bottom sits in the directory
  107. // indicated by the given valid snapshot.
  108. cmListFileBacktrace(cmStateSnapshot const& snapshot);
  109. cmStateSnapshot GetBottom() const;
  110. // Get a backtrace with the given file scope added to the top.
  111. // May not be called until after construction with a valid snapshot.
  112. cmListFileBacktrace Push(std::string const& file) const;
  113. // Get a backtrace with the given call context added to the top.
  114. // May not be called until after construction with a valid snapshot.
  115. cmListFileBacktrace Push(cmListFileContext const& lfc) const;
  116. // Get a backtrace with the top level removed.
  117. // May not be called until after a matching Push.
  118. cmListFileBacktrace Pop() const;
  119. // Get the context at the top of the backtrace.
  120. // This may be called only if Empty() would return false.
  121. cmListFileContext const& Top() const;
  122. // Print the top of the backtrace.
  123. void PrintTitle(std::ostream& out) const;
  124. // Print the call stack below the top of the backtrace.
  125. void PrintCallStack(std::ostream& out) const;
  126. // Get the number of 'frames' in this backtrace
  127. size_t Depth() const;
  128. // Return true if this backtrace is empty.
  129. bool Empty() const;
  130. private:
  131. struct Entry;
  132. std::shared_ptr<Entry const> TopEntry;
  133. cmListFileBacktrace(std::shared_ptr<Entry const> parent,
  134. cmListFileContext const& lfc);
  135. cmListFileBacktrace(std::shared_ptr<Entry const> top);
  136. };
  137. // Wrap type T as a value with a backtrace. For purposes of
  138. // ordering and equality comparison, only the original value is
  139. // used. The backtrace is considered incidental.
  140. template <typename T>
  141. class BT
  142. {
  143. public:
  144. BT(T v = T(), cmListFileBacktrace bt = cmListFileBacktrace())
  145. : Value(std::move(v))
  146. , Backtrace(std::move(bt))
  147. {
  148. }
  149. T Value;
  150. cmListFileBacktrace Backtrace;
  151. friend bool operator==(BT<T> const& l, BT<T> const& r)
  152. {
  153. return l.Value == r.Value;
  154. }
  155. friend bool operator<(BT<T> const& l, BT<T> const& r)
  156. {
  157. return l.Value < r.Value;
  158. }
  159. friend bool operator==(BT<T> const& l, T const& r) { return l.Value == r; }
  160. friend bool operator==(T const& l, BT<T> const& r) { return l == r.Value; }
  161. };
  162. std::ostream& operator<<(std::ostream& os, BT<std::string> const& s);
  163. // Wrap type T as a value with potentially multiple backtraces. For purposes
  164. // of ordering and equality comparison, only the original value is used. The
  165. // backtrace is considered incidental.
  166. template <typename T>
  167. class BTs
  168. {
  169. public:
  170. BTs(T v = T(), cmListFileBacktrace bt = cmListFileBacktrace())
  171. : Value(std::move(v))
  172. {
  173. Backtraces.emplace_back(std::move(bt));
  174. }
  175. T Value;
  176. std::vector<cmListFileBacktrace> Backtraces;
  177. friend bool operator==(BTs<T> const& l, BTs<T> const& r)
  178. {
  179. return l.Value == r.Value;
  180. }
  181. friend bool operator<(BTs<T> const& l, BTs<T> const& r)
  182. {
  183. return l.Value < r.Value;
  184. }
  185. friend bool operator==(BTs<T> const& l, T const& r) { return l.Value == r; }
  186. friend bool operator==(T const& l, BTs<T> const& r) { return l == r.Value; }
  187. };
  188. std::vector<BT<std::string>> ExpandListWithBacktrace(
  189. std::string const& list,
  190. cmListFileBacktrace const& bt = cmListFileBacktrace());
  191. struct cmListFile
  192. {
  193. bool ParseFile(const char* path, cmMessenger* messenger,
  194. cmListFileBacktrace const& lfbt);
  195. bool ParseString(const char* str, const char* virtual_filename,
  196. cmMessenger* messenger, cmListFileBacktrace const& lfbt);
  197. std::vector<cmListFileFunction> Functions;
  198. };