cmListFileCache.h 4.2 KB

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