cmListFileCache.h 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163
  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 <stddef.h>
  8. #include <string>
  9. #include <vector>
  10. #include "cmStateSnapshot.h"
  11. /** \class cmListFileCache
  12. * \brief A class to cache list file contents.
  13. *
  14. * cmListFileCache is a class used to cache the contents of parsed
  15. * cmake list files.
  16. */
  17. class cmMessenger;
  18. struct cmCommandContext
  19. {
  20. std::string Name;
  21. long Line;
  22. cmCommandContext()
  23. : Name()
  24. , Line(0)
  25. {
  26. }
  27. };
  28. struct cmListFileArgument
  29. {
  30. enum Delimiter
  31. {
  32. Unquoted,
  33. Quoted,
  34. Bracket
  35. };
  36. cmListFileArgument()
  37. : Value()
  38. , Delim(Unquoted)
  39. , Line(0)
  40. {
  41. }
  42. cmListFileArgument(const std::string& v, Delimiter d, long line)
  43. : Value(v)
  44. , Delim(d)
  45. , Line(line)
  46. {
  47. }
  48. bool operator==(const cmListFileArgument& r) const
  49. {
  50. return (this->Value == r.Value) && (this->Delim == r.Delim);
  51. }
  52. bool operator!=(const cmListFileArgument& r) const { return !(*this == r); }
  53. std::string Value;
  54. Delimiter Delim;
  55. long Line;
  56. };
  57. class cmListFileContext
  58. {
  59. public:
  60. std::string Name;
  61. std::string FilePath;
  62. long Line;
  63. cmListFileContext()
  64. : Name()
  65. , FilePath()
  66. , Line(0)
  67. {
  68. }
  69. static cmListFileContext FromCommandContext(cmCommandContext const& lfcc,
  70. std::string const& fileName)
  71. {
  72. cmListFileContext lfc;
  73. lfc.FilePath = fileName;
  74. lfc.Line = lfcc.Line;
  75. lfc.Name = lfcc.Name;
  76. return lfc;
  77. }
  78. };
  79. std::ostream& operator<<(std::ostream&, cmListFileContext const&);
  80. bool operator<(const cmListFileContext& lhs, const cmListFileContext& rhs);
  81. bool operator==(cmListFileContext const& lhs, cmListFileContext const& rhs);
  82. bool operator!=(cmListFileContext const& lhs, cmListFileContext const& rhs);
  83. struct cmListFileFunction : public cmCommandContext
  84. {
  85. std::vector<cmListFileArgument> Arguments;
  86. };
  87. // Represent a backtrace (call stack). Provide value semantics
  88. // but use efficient reference-counting underneath to avoid copies.
  89. class cmListFileBacktrace
  90. {
  91. public:
  92. // Default-constructed backtrace may not be used until after
  93. // set via assignment from a backtrace constructed with a
  94. // valid snapshot.
  95. cmListFileBacktrace();
  96. // Construct an empty backtrace whose bottom sits in the directory
  97. // indicated by the given valid snapshot.
  98. cmListFileBacktrace(cmStateSnapshot const& snapshot);
  99. // Backtraces may be copied and assigned as values.
  100. cmListFileBacktrace(cmListFileBacktrace const& r);
  101. cmListFileBacktrace& operator=(cmListFileBacktrace const& r);
  102. ~cmListFileBacktrace();
  103. cmStateSnapshot GetBottom() const { return this->Bottom; }
  104. // Get a backtrace with the given file scope added to the top.
  105. // May not be called until after construction with a valid snapshot.
  106. cmListFileBacktrace Push(std::string const& file) const;
  107. // Get a backtrace with the given call context added to the top.
  108. // May not be called until after construction with a valid snapshot.
  109. cmListFileBacktrace Push(cmListFileContext const& lfc) const;
  110. // Get a backtrace with the top level removed.
  111. // May not be called until after a matching Push.
  112. cmListFileBacktrace Pop() const;
  113. // Get the context at the top of the backtrace.
  114. // Returns an empty context if the backtrace is empty.
  115. cmListFileContext const& Top() const;
  116. // Print the top of the backtrace.
  117. void PrintTitle(std::ostream& out) const;
  118. // Print the call stack below the top of the backtrace.
  119. void PrintCallStack(std::ostream& out) const;
  120. // Get the number of 'frames' in this backtrace
  121. size_t Depth() const;
  122. private:
  123. struct Entry;
  124. cmStateSnapshot Bottom;
  125. Entry* Cur;
  126. cmListFileBacktrace(cmStateSnapshot const& bottom, Entry* up,
  127. cmListFileContext const& lfc);
  128. cmListFileBacktrace(cmStateSnapshot const& bottom, Entry* cur);
  129. };
  130. struct cmListFile
  131. {
  132. bool ParseFile(const char* path, cmMessenger* messenger,
  133. cmListFileBacktrace const& lfbt);
  134. std::vector<cmListFileFunction> Functions;
  135. };
  136. #endif