cmListFileCache.h 4.1 KB

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