cmListFileCache.h 4.4 KB

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