cmListFileCache.h 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210
  1. /* Distributed under the OSI-approved BSD 3-Clause License. See accompanying
  2. file Copyright.txt or https://cmake.org/licensing for details. */
  3. #ifndef cmListFileCache_h
  4. #define cmListFileCache_h
  5. #include "cmConfigure.h" // IWYU pragma: keep
  6. #include <iosfwd>
  7. #include <memory> // IWYU pragma: keep
  8. #include <stddef.h>
  9. #include <string>
  10. #include <utility>
  11. #include <vector>
  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() {}
  27. cmCommandName(std::string const& name) { *this = name; }
  28. cmCommandName& operator=(std::string const& name);
  29. } Name;
  30. long Line;
  31. cmCommandContext()
  32. : Line(0)
  33. {
  34. }
  35. cmCommandContext(const char* name, int line)
  36. : Name(name)
  37. , Line(line)
  38. {
  39. }
  40. };
  41. struct cmListFileArgument
  42. {
  43. enum Delimiter
  44. {
  45. Unquoted,
  46. Quoted,
  47. Bracket
  48. };
  49. cmListFileArgument()
  50. : Delim(Unquoted)
  51. , Line(0)
  52. {
  53. }
  54. cmListFileArgument(const std::string& v, Delimiter d, long line)
  55. : Value(v)
  56. , Delim(d)
  57. , Line(line)
  58. {
  59. }
  60. bool operator==(const cmListFileArgument& r) const
  61. {
  62. return (this->Value == r.Value) && (this->Delim == r.Delim);
  63. }
  64. bool operator!=(const cmListFileArgument& r) const { return !(*this == r); }
  65. std::string Value;
  66. Delimiter Delim;
  67. long Line;
  68. };
  69. class cmListFileContext
  70. {
  71. public:
  72. std::string Name;
  73. std::string FilePath;
  74. long Line;
  75. cmListFileContext()
  76. : Line(0)
  77. {
  78. }
  79. static cmListFileContext FromCommandContext(cmCommandContext const& lfcc,
  80. std::string const& fileName)
  81. {
  82. cmListFileContext lfc;
  83. lfc.FilePath = fileName;
  84. lfc.Line = lfcc.Line;
  85. lfc.Name = lfcc.Name.Original;
  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. // Backtraces may be copied, moved, and assigned as values.
  110. cmListFileBacktrace(cmListFileBacktrace const&) = default;
  111. cmListFileBacktrace(cmListFileBacktrace&&) // NOLINT(clang-tidy)
  112. noexcept = default;
  113. cmListFileBacktrace& operator=(cmListFileBacktrace const&) = default;
  114. cmListFileBacktrace& operator=(cmListFileBacktrace&&) // NOLINT(clang-tidy)
  115. noexcept = default;
  116. ~cmListFileBacktrace() = default;
  117. cmStateSnapshot GetBottom() const;
  118. // Get a backtrace with the given file scope added to the top.
  119. // May not be called until after construction with a valid snapshot.
  120. cmListFileBacktrace Push(std::string const& file) const;
  121. // Get a backtrace with the given call context added to the top.
  122. // May not be called until after construction with a valid snapshot.
  123. cmListFileBacktrace Push(cmListFileContext const& lfc) const;
  124. // Get a backtrace with the top level removed.
  125. // May not be called until after a matching Push.
  126. cmListFileBacktrace Pop() const;
  127. // Get the context at the top of the backtrace.
  128. // This may be called only if Empty() would return false.
  129. cmListFileContext const& Top() const;
  130. // Print the top of the backtrace.
  131. void PrintTitle(std::ostream& out) const;
  132. // Print the call stack below the top of the backtrace.
  133. void PrintCallStack(std::ostream& out) const;
  134. // Get the number of 'frames' in this backtrace
  135. size_t Depth() const;
  136. // Return true if this backtrace is empty.
  137. bool Empty() const;
  138. private:
  139. struct Entry;
  140. std::shared_ptr<Entry const> TopEntry;
  141. cmListFileBacktrace(std::shared_ptr<Entry const> parent,
  142. cmListFileContext const& lfc);
  143. cmListFileBacktrace(std::shared_ptr<Entry const> top);
  144. };
  145. // Wrap type T as a value with a backtrace. For purposes of
  146. // ordering and equality comparison, only the original value is
  147. // used. The backtrace is considered incidental.
  148. template <typename T>
  149. class BT
  150. {
  151. public:
  152. BT(T v = T(), cmListFileBacktrace bt = cmListFileBacktrace())
  153. : Value(std::move(v))
  154. , Backtrace(std::move(bt))
  155. {
  156. }
  157. T Value;
  158. cmListFileBacktrace Backtrace;
  159. friend bool operator==(BT<T> const& l, BT<T> const& r)
  160. {
  161. return l.Value == r.Value;
  162. }
  163. friend bool operator<(BT<T> const& l, BT<T> const& r)
  164. {
  165. return l.Value < r.Value;
  166. }
  167. friend bool operator==(BT<T> const& l, T const& r) { return l.Value == r; }
  168. friend bool operator==(T const& l, BT<T> const& r) { return l == r.Value; }
  169. };
  170. std::ostream& operator<<(std::ostream& os, BT<std::string> const& s);
  171. std::vector<BT<std::string>> ExpandListWithBacktrace(
  172. std::string const& list,
  173. cmListFileBacktrace const& bt = cmListFileBacktrace());
  174. struct cmListFile
  175. {
  176. bool ParseFile(const char* path, cmMessenger* messenger,
  177. cmListFileBacktrace const& lfbt);
  178. std::vector<cmListFileFunction> Functions;
  179. };
  180. #endif