cmListFileCache.h 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178
  1. /* Distributed under the OSI-approved BSD 3-Clause License. See accompanying
  2. file Copyright.txt or https://cmake.org/licensing for details. */
  3. #ifndef cmListFileCache_h
  4. #define cmListFileCache_h
  5. #include "cmConfigure.h" // IWYU pragma: keep
  6. #include <iosfwd>
  7. #include <memory> // IWYU pragma: keep
  8. #include <stddef.h>
  9. #include <string>
  10. #include <vector>
  11. #include "cmStateSnapshot.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 Lower;
  24. std::string Original;
  25. cmCommandName() {}
  26. cmCommandName(std::string const& name) { *this = name; }
  27. cmCommandName& operator=(std::string const& name);
  28. } Name;
  29. long Line;
  30. cmCommandContext()
  31. : Line(0)
  32. {
  33. }
  34. cmCommandContext(const char* name, int line)
  35. : Name(name)
  36. , Line(line)
  37. {
  38. }
  39. };
  40. struct cmListFileArgument
  41. {
  42. enum Delimiter
  43. {
  44. Unquoted,
  45. Quoted,
  46. Bracket
  47. };
  48. cmListFileArgument()
  49. : Value()
  50. , Delim(Unquoted)
  51. , Line(0)
  52. {
  53. }
  54. cmListFileArgument(const std::string& v, Delimiter d, long line)
  55. : Value(v)
  56. , Delim(d)
  57. , Line(line)
  58. {
  59. }
  60. bool operator==(const cmListFileArgument& r) const
  61. {
  62. return (this->Value == r.Value) && (this->Delim == r.Delim);
  63. }
  64. bool operator!=(const cmListFileArgument& r) const { return !(*this == r); }
  65. std::string Value;
  66. Delimiter Delim;
  67. long Line;
  68. };
  69. class cmListFileContext
  70. {
  71. public:
  72. std::string Name;
  73. std::string FilePath;
  74. long Line;
  75. cmListFileContext()
  76. : Name()
  77. , FilePath()
  78. , Line(0)
  79. {
  80. }
  81. static cmListFileContext FromCommandContext(cmCommandContext const& lfcc,
  82. std::string const& fileName)
  83. {
  84. cmListFileContext lfc;
  85. lfc.FilePath = fileName;
  86. lfc.Line = lfcc.Line;
  87. lfc.Name = lfcc.Name.Original;
  88. return lfc;
  89. }
  90. };
  91. std::ostream& operator<<(std::ostream&, cmListFileContext const&);
  92. bool operator<(const cmListFileContext& lhs, const cmListFileContext& rhs);
  93. bool operator==(cmListFileContext const& lhs, cmListFileContext const& rhs);
  94. bool operator!=(cmListFileContext const& lhs, cmListFileContext const& rhs);
  95. struct cmListFileFunction : public cmCommandContext
  96. {
  97. std::vector<cmListFileArgument> Arguments;
  98. };
  99. // Represent a backtrace (call stack). Provide value semantics
  100. // but use efficient reference-counting underneath to avoid copies.
  101. class cmListFileBacktrace
  102. {
  103. public:
  104. // Default-constructed backtrace may not be used until after
  105. // set via assignment from a backtrace constructed with a
  106. // valid snapshot.
  107. cmListFileBacktrace() = default;
  108. // Construct an empty backtrace whose bottom sits in the directory
  109. // indicated by the given valid snapshot.
  110. cmListFileBacktrace(cmStateSnapshot const& snapshot);
  111. // Backtraces may be copied, moved, and assigned as values.
  112. cmListFileBacktrace(cmListFileBacktrace const&) = default;
  113. cmListFileBacktrace(cmListFileBacktrace&&) noexcept = default;
  114. cmListFileBacktrace& operator=(cmListFileBacktrace const&) = default;
  115. cmListFileBacktrace& operator=(cmListFileBacktrace&&) noexcept = default;
  116. ~cmListFileBacktrace() = default;
  117. cmStateSnapshot GetBottom() const;
  118. // Get a backtrace with the given file scope added to the top.
  119. // May not be called until after construction with a valid snapshot.
  120. cmListFileBacktrace Push(std::string const& file) const;
  121. // Get a backtrace with the given call context added to the top.
  122. // May not be called until after construction with a valid snapshot.
  123. cmListFileBacktrace Push(cmListFileContext const& lfc) const;
  124. // Get a backtrace with the top level removed.
  125. // May not be called until after a matching Push.
  126. cmListFileBacktrace Pop() const;
  127. // Get the context at the top of the backtrace.
  128. // Returns an empty context if the backtrace is empty.
  129. cmListFileContext const& Top() const;
  130. // Print the top of the backtrace.
  131. void PrintTitle(std::ostream& out) const;
  132. // Print the call stack below the top of the backtrace.
  133. void PrintCallStack(std::ostream& out) const;
  134. // Get the number of 'frames' in this backtrace
  135. size_t Depth() const;
  136. // Return true if this backtrace is empty.
  137. bool Empty() const;
  138. private:
  139. struct Entry;
  140. std::shared_ptr<Entry const> TopEntry;
  141. cmListFileBacktrace(std::shared_ptr<Entry const> parent,
  142. cmListFileContext const& lfc);
  143. cmListFileBacktrace(std::shared_ptr<Entry const> top);
  144. };
  145. struct cmListFile
  146. {
  147. bool ParseFile(const char* path, cmMessenger* messenger,
  148. cmListFileBacktrace const& lfbt);
  149. std::vector<cmListFileFunction> Functions;
  150. };
  151. #endif