cmListFileCache.h 6.8 KB

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