cmListFileCache.h 4.0 KB

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