cmListFileCache.h 7.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246
  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. // This move constructor is marked `noexcept` yet `clang-tidy` 14 reports it
  97. // as being able to throw an exception. Suppress the warning as there doesn't
  98. // seem to be any way for this to happen given the member types.
  99. // NOLINTNEXTLINE(bugprone-exception-escape)
  100. cmListFileContext(cmListFileContext&& /*other*/) noexcept = default;
  101. cmListFileContext(const cmListFileContext& /*other*/) = default;
  102. cmListFileContext& operator=(const cmListFileContext& /*other*/) = default;
  103. #if __cplusplus >= 201703L || (defined(_MSVC_LANG) && _MSVC_LANG >= 201703L)
  104. cmListFileContext& operator=(cmListFileContext&& /*other*/) noexcept =
  105. default;
  106. #else
  107. // The move assignment operators for several STL classes did not become
  108. // noexcept until C++17, which causes some tools to warn about this move
  109. // assignment operator throwing an exception when it shouldn't.
  110. cmListFileContext& operator=(cmListFileContext&& /*other*/) noexcept =
  111. delete;
  112. #endif
  113. cmListFileContext(std::string name, std::string filePath, long line)
  114. : Name(std::move(name))
  115. , FilePath(std::move(filePath))
  116. , Line(line)
  117. {
  118. }
  119. static cmListFileContext FromListFilePath(std::string const& filePath)
  120. {
  121. // We are entering a file-level scope but have not yet reached
  122. // any specific line or command invocation within it. This context
  123. // is useful to print when it is at the top but otherwise can be
  124. // skipped during call stack printing.
  125. cmListFileContext lfc;
  126. lfc.FilePath = filePath;
  127. return lfc;
  128. }
  129. static cmListFileContext FromListFileFunction(
  130. cmListFileFunction const& lff, std::string const& fileName,
  131. cm::optional<std::string> deferId = {})
  132. {
  133. cmListFileContext lfc;
  134. lfc.FilePath = fileName;
  135. lfc.Line = lff.Line();
  136. lfc.Name = lff.OriginalName();
  137. lfc.DeferId = std::move(deferId);
  138. return lfc;
  139. }
  140. };
  141. std::ostream& operator<<(std::ostream&, cmListFileContext const&);
  142. bool operator<(const cmListFileContext& lhs, const cmListFileContext& rhs);
  143. bool operator==(cmListFileContext const& lhs, cmListFileContext const& rhs);
  144. bool operator!=(cmListFileContext const& lhs, cmListFileContext const& rhs);
  145. // Represent a backtrace (call stack) with efficient value semantics.
  146. class cmListFileBacktrace
  147. : public cmConstStack<cmListFileContext, cmListFileBacktrace>
  148. {
  149. using cmConstStack::cmConstStack;
  150. friend class cmConstStack<cmListFileContext, cmListFileBacktrace>;
  151. };
  152. #ifndef cmListFileCache_cxx
  153. extern template class cmConstStack<cmListFileContext, cmListFileBacktrace>;
  154. #endif
  155. // Wrap type T as a value with a backtrace. For purposes of
  156. // ordering and equality comparison, only the original value is
  157. // used. The backtrace is considered incidental.
  158. template <typename T>
  159. class BT
  160. {
  161. public:
  162. BT(T v = T(), cmListFileBacktrace bt = cmListFileBacktrace())
  163. : Value(std::move(v))
  164. , Backtrace(std::move(bt))
  165. {
  166. }
  167. T Value;
  168. cmListFileBacktrace Backtrace;
  169. friend bool operator==(BT<T> const& l, BT<T> const& r)
  170. {
  171. return l.Value == r.Value;
  172. }
  173. friend bool operator<(BT<T> const& l, BT<T> const& r)
  174. {
  175. return l.Value < r.Value;
  176. }
  177. friend bool operator==(BT<T> const& l, T const& r) { return l.Value == r; }
  178. friend bool operator==(T const& l, BT<T> const& r) { return l == r.Value; }
  179. };
  180. std::ostream& operator<<(std::ostream& os, BT<std::string> const& s);
  181. // Wrap type T as a value with potentially multiple backtraces. For purposes
  182. // of ordering and equality comparison, only the original value is used. The
  183. // backtrace is considered incidental.
  184. template <typename T>
  185. class BTs
  186. {
  187. public:
  188. BTs(T v = T(), cmListFileBacktrace bt = cmListFileBacktrace())
  189. : Value(std::move(v))
  190. {
  191. this->Backtraces.emplace_back(std::move(bt));
  192. }
  193. T Value;
  194. std::vector<cmListFileBacktrace> Backtraces;
  195. friend bool operator==(BTs<T> const& l, BTs<T> const& r)
  196. {
  197. return l.Value == r.Value;
  198. }
  199. friend bool operator<(BTs<T> const& l, BTs<T> const& r)
  200. {
  201. return l.Value < r.Value;
  202. }
  203. friend bool operator==(BTs<T> const& l, T const& r) { return l.Value == r; }
  204. friend bool operator==(T const& l, BTs<T> const& r) { return l == r.Value; }
  205. };
  206. std::vector<BT<std::string>> cmExpandListWithBacktrace(
  207. std::string const& list,
  208. cmListFileBacktrace const& bt = cmListFileBacktrace(),
  209. bool emptyArgs = false);
  210. struct cmListFile
  211. {
  212. bool ParseFile(const char* path, cmMessenger* messenger,
  213. cmListFileBacktrace const& lfbt);
  214. bool ParseString(const char* str, const char* virtual_filename,
  215. cmMessenger* messenger, cmListFileBacktrace const& lfbt);
  216. std::vector<cmListFileFunction> Functions;
  217. };