cmListFileCache.h 7.2 KB

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