cmListFileCache.h 7.3 KB

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