cmListFileCache.h 5.5 KB

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