cmListFileCache.h 7.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250
  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 <iosfwd>
  6. #include <memory>
  7. #include <string>
  8. #include <utility>
  9. #include <vector>
  10. #include <cm/optional>
  11. #include "cmSystemTools.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 cmListFileArgument
  20. {
  21. enum Delimiter
  22. {
  23. Unquoted,
  24. Quoted,
  25. Bracket
  26. };
  27. cmListFileArgument() = default;
  28. cmListFileArgument(std::string v, Delimiter d, long line)
  29. : Value(std::move(v))
  30. , Delim(d)
  31. , Line(line)
  32. {
  33. }
  34. bool operator==(const cmListFileArgument& r) const
  35. {
  36. return (this->Value == r.Value) && (this->Delim == r.Delim);
  37. }
  38. bool operator!=(const cmListFileArgument& r) const { return !(*this == r); }
  39. std::string Value;
  40. Delimiter Delim = Unquoted;
  41. long Line = 0;
  42. };
  43. class cmListFileFunction
  44. {
  45. public:
  46. cmListFileFunction(std::string name, long line, long lineEnd,
  47. std::vector<cmListFileArgument> args)
  48. : Impl{ std::make_shared<Implementation>(std::move(name), line, lineEnd,
  49. std::move(args)) }
  50. {
  51. }
  52. std::string const& OriginalName() const noexcept
  53. {
  54. return this->Impl->OriginalName;
  55. }
  56. std::string const& LowerCaseName() const noexcept
  57. {
  58. return this->Impl->LowerCaseName;
  59. }
  60. long Line() const noexcept { return this->Impl->Line; }
  61. long LineEnd() const noexcept { return this->Impl->LineEnd; }
  62. std::vector<cmListFileArgument> const& Arguments() const noexcept
  63. {
  64. return this->Impl->Arguments;
  65. }
  66. private:
  67. struct Implementation
  68. {
  69. Implementation(std::string name, long line, long lineEnd,
  70. std::vector<cmListFileArgument> args)
  71. : OriginalName{ std::move(name) }
  72. , LowerCaseName{ cmSystemTools::LowerCase(this->OriginalName) }
  73. , Line{ line }
  74. , LineEnd{ lineEnd }
  75. , Arguments{ std::move(args) }
  76. {
  77. }
  78. std::string OriginalName;
  79. std::string LowerCaseName;
  80. long Line = 0;
  81. long LineEnd = 0;
  82. std::vector<cmListFileArgument> Arguments;
  83. };
  84. std::shared_ptr<Implementation const> Impl;
  85. };
  86. class cmListFileContext
  87. {
  88. public:
  89. std::string Name;
  90. std::string FilePath;
  91. long Line = 0;
  92. static long const DeferPlaceholderLine = -1;
  93. cm::optional<std::string> DeferId;
  94. cmListFileContext() = default;
  95. cmListFileContext(cmListFileContext&& /*other*/) = default;
  96. cmListFileContext(const cmListFileContext& /*other*/) = default;
  97. cmListFileContext& operator=(const cmListFileContext& /*other*/) = default;
  98. #if __cplusplus >= 201703L || (defined(_MSVC_LANG) && _MSVC_LANG >= 201703L)
  99. cmListFileContext& operator=(cmListFileContext&& /*other*/) = default;
  100. #else
  101. // The move assignment operators for several STL classes did not become
  102. // noexcept until C++17, which causes some tools to warn about this move
  103. // assignment operator throwing an exception when it shouldn't.
  104. cmListFileContext& operator=(cmListFileContext&& /*other*/) = delete;
  105. #endif
  106. cmListFileContext(std::string name, std::string filePath, long line)
  107. : Name(std::move(name))
  108. , FilePath(std::move(filePath))
  109. , Line(line)
  110. {
  111. }
  112. static cmListFileContext FromListFileFunction(
  113. cmListFileFunction const& lff, std::string const& fileName,
  114. cm::optional<std::string> deferId = {})
  115. {
  116. cmListFileContext lfc;
  117. lfc.FilePath = fileName;
  118. lfc.Line = lff.Line();
  119. lfc.Name = lff.OriginalName();
  120. lfc.DeferId = std::move(deferId);
  121. return lfc;
  122. }
  123. };
  124. std::ostream& operator<<(std::ostream&, cmListFileContext const&);
  125. bool operator<(const cmListFileContext& lhs, const cmListFileContext& rhs);
  126. bool operator==(cmListFileContext const& lhs, cmListFileContext const& rhs);
  127. bool operator!=(cmListFileContext const& lhs, cmListFileContext const& rhs);
  128. // Represent a backtrace (call stack). Provide value semantics
  129. // but use efficient reference-counting underneath to avoid copies.
  130. class cmListFileBacktrace
  131. {
  132. public:
  133. // Default-constructed backtrace is empty.
  134. cmListFileBacktrace() = default;
  135. // Get a backtrace with the given file scope added to the top.
  136. cmListFileBacktrace Push(std::string const& file) const;
  137. // Get a backtrace with the given call context added to the top.
  138. cmListFileBacktrace Push(cmListFileContext const& lfc) const;
  139. // Get a backtrace with the top level removed.
  140. // May not be called until after a matching Push.
  141. cmListFileBacktrace Pop() const;
  142. // Get the context at the top of the backtrace.
  143. // This may be called only if Empty() would return false.
  144. cmListFileContext const& Top() const;
  145. // Return true if this backtrace is empty.
  146. bool Empty() const;
  147. private:
  148. struct Entry;
  149. std::shared_ptr<Entry const> TopEntry;
  150. cmListFileBacktrace(std::shared_ptr<Entry const> parent,
  151. cmListFileContext const& lfc);
  152. cmListFileBacktrace(std::shared_ptr<Entry const> top);
  153. };
  154. // Wrap type T as a value with a backtrace. For purposes of
  155. // ordering and equality comparison, only the original value is
  156. // used. The backtrace is considered incidental.
  157. template <typename T>
  158. class BT
  159. {
  160. public:
  161. BT(T v = T(), cmListFileBacktrace bt = cmListFileBacktrace())
  162. : Value(std::move(v))
  163. , Backtrace(std::move(bt))
  164. {
  165. }
  166. T Value;
  167. cmListFileBacktrace Backtrace;
  168. friend bool operator==(BT<T> const& l, BT<T> const& r)
  169. {
  170. return l.Value == r.Value;
  171. }
  172. friend bool operator<(BT<T> const& l, BT<T> const& r)
  173. {
  174. return l.Value < r.Value;
  175. }
  176. friend bool operator==(BT<T> const& l, T const& r) { return l.Value == r; }
  177. friend bool operator==(T const& l, BT<T> const& r) { return l == r.Value; }
  178. };
  179. std::ostream& operator<<(std::ostream& os, BT<std::string> const& s);
  180. // Wrap type T as a value with potentially multiple backtraces. For purposes
  181. // of ordering and equality comparison, only the original value is used. The
  182. // backtrace is considered incidental.
  183. template <typename T>
  184. class BTs
  185. {
  186. public:
  187. BTs(T v = T(), cmListFileBacktrace bt = cmListFileBacktrace())
  188. : Value(std::move(v))
  189. {
  190. this->Backtraces.emplace_back(std::move(bt));
  191. }
  192. T Value;
  193. std::vector<cmListFileBacktrace> Backtraces;
  194. friend bool operator==(BTs<T> const& l, BTs<T> const& r)
  195. {
  196. return l.Value == r.Value;
  197. }
  198. friend bool operator<(BTs<T> const& l, BTs<T> const& r)
  199. {
  200. return l.Value < r.Value;
  201. }
  202. friend bool operator==(BTs<T> const& l, T const& r) { return l.Value == r; }
  203. friend bool operator==(T const& l, BTs<T> const& r) { return l == r.Value; }
  204. };
  205. std::vector<BT<std::string>> cmExpandListWithBacktrace(
  206. std::string const& list,
  207. cmListFileBacktrace const& bt = cmListFileBacktrace(),
  208. bool emptyArgs = false);
  209. struct cmListFile
  210. {
  211. bool ParseFile(const char* path, cmMessenger* messenger,
  212. cmListFileBacktrace const& lfbt);
  213. bool ParseString(const char* str, const char* virtual_filename,
  214. cmMessenger* messenger, cmListFileBacktrace const& lfbt);
  215. std::vector<cmListFileFunction> Functions;
  216. };