cmListFileCache.h 6.2 KB

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