cmListFileCache.h 5.6 KB

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