autoupdate.h 8.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237
  1. // Part of the Carbon Language project, under the Apache License v2.0 with LLVM
  2. // Exceptions. See /LICENSE for license information.
  3. // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
  4. #ifndef CARBON_TESTING_FILE_TEST_AUTOUPDATE_H_
  5. #define CARBON_TESTING_FILE_TEST_AUTOUPDATE_H_
  6. #include <filesystem>
  7. #include <utility>
  8. #include "common/check.h"
  9. #include "llvm/ADT/DenseMap.h"
  10. #include "llvm/ADT/SmallVector.h"
  11. #include "llvm/ADT/StringRef.h"
  12. #include "re2/re2.h"
  13. #include "testing/file_test/line.h"
  14. namespace Carbon::Testing {
  15. class FileTestAutoupdater {
  16. public:
  17. struct LineNumberReplacement {
  18. bool has_file;
  19. // The line replacement. The pattern should match lines. If has_file,
  20. // pattern should have a file and line group; otherwise, only a line group,
  21. // but default_file_re should be provided.
  22. //
  23. // Uses shared_ptr for storage in SmallVector.
  24. std::shared_ptr<RE2> re;
  25. // line_formatv should provide {0} to substitute with [[@LINE...]] deltas.
  26. std::string line_formatv;
  27. };
  28. explicit FileTestAutoupdater(
  29. const std::filesystem::path& file_test_path, std::string test_command,
  30. std::string dump_command, llvm::StringRef input_content,
  31. const llvm::SmallVector<llvm::StringRef>& filenames,
  32. int autoupdate_line_number,
  33. const llvm::SmallVector<FileTestLine>& non_check_lines,
  34. llvm::StringRef stdout, llvm::StringRef stderr,
  35. const std::optional<RE2>& default_file_re,
  36. const llvm::SmallVector<LineNumberReplacement>& line_number_replacements,
  37. std::function<void(std::string&)> do_extra_check_replacements)
  38. : file_test_path_(file_test_path),
  39. test_command_(std::move(test_command)),
  40. dump_command_(std::move(dump_command)),
  41. input_content_(input_content),
  42. filenames_(filenames),
  43. autoupdate_line_number_(autoupdate_line_number),
  44. non_check_lines_(non_check_lines),
  45. default_file_re_(default_file_re),
  46. line_number_replacements_(line_number_replacements),
  47. do_extra_check_replacements_(std::move(do_extra_check_replacements)),
  48. // BuildCheckLines should only be called after other member
  49. // initialization.
  50. stdout_(BuildCheckLines(stdout, "STDOUT")),
  51. stderr_(BuildCheckLines(stderr, "STDERR")),
  52. any_attached_stdout_lines_(std::any_of(
  53. stdout_.lines.begin(), stdout_.lines.end(),
  54. [&](const CheckLine& line) { return line.line_number() != -1; })),
  55. non_check_line_(non_check_lines_.begin()) {
  56. for (const auto& replacement : line_number_replacements_) {
  57. CARBON_CHECK(replacement.has_file || default_file_re_,
  58. "For replacement with pattern `{0}` to have has_file=false, "
  59. "override GetDefaultFileRE.",
  60. replacement.re->pattern());
  61. CARBON_CHECK(replacement.re->ok(), "Invalid line replacement RE2: {0}",
  62. replacement.re->error());
  63. }
  64. }
  65. // Automatically updates CHECKs in the provided file when dry_run=false.
  66. // Returns true if generated file content differs from actual file content.
  67. auto Run(bool dry_run) -> bool;
  68. private:
  69. // The file and line number that a CHECK line refers to, and the
  70. // replacement from which they were determined, if any.
  71. struct FileAndLineNumber {
  72. explicit FileAndLineNumber(int file_number) : file_number(file_number) {}
  73. explicit FileAndLineNumber(const LineNumberReplacement* replacement,
  74. int file_number, absl::string_view line_number);
  75. const LineNumberReplacement* replacement = nullptr;
  76. int file_number;
  77. int line_number = -1;
  78. };
  79. // A TIP line added by autoupdate. Not associated with any line in output.
  80. class TipLine : public FileTestLineBase {
  81. public:
  82. explicit TipLine(std::string line)
  83. : FileTestLineBase(-1, -1), line_(std::move(line)) {}
  84. auto Print(llvm::raw_ostream& out) const -> void override { out << line_; }
  85. auto is_blank() const -> bool override { return line_.empty(); }
  86. private:
  87. std::string line_;
  88. };
  89. // A CHECK line which is integrated into autoupdate output.
  90. class CheckLine : public FileTestLineBase {
  91. public:
  92. // RE2 is passed by a pointer because it doesn't support std::optional.
  93. explicit CheckLine(FileAndLineNumber file_and_line_number, std::string line)
  94. : FileTestLineBase(file_and_line_number.file_number,
  95. file_and_line_number.line_number),
  96. replacement_(file_and_line_number.replacement),
  97. line_(std::move(line)) {}
  98. auto Print(llvm::raw_ostream& out) const -> void override {
  99. out << indent_ << line_;
  100. }
  101. // When the location of the CHECK in output is known, we can set the indent
  102. // and its line.
  103. auto SetOutputLine(llvm::StringRef indent, int output_file_number,
  104. int output_line_number) -> void {
  105. indent_ = indent;
  106. output_file_number_ = output_file_number;
  107. output_line_number_ = output_line_number;
  108. }
  109. // When the location of all lines in a file are known, we can set the line
  110. // offset based on the target line.
  111. auto RemapLineNumbers(
  112. const llvm::DenseMap<std::pair<int, int>, int>& output_line_remap,
  113. const llvm::SmallVector<int>& new_last_line_numbers) -> void;
  114. auto is_blank() const -> bool override { return false; }
  115. private:
  116. const LineNumberReplacement* replacement_;
  117. std::string line_;
  118. llvm::StringRef indent_;
  119. int output_file_number_ = -1;
  120. int output_line_number_ = -1;
  121. };
  122. // Clusters information for stdout and stderr.
  123. struct CheckLines {
  124. explicit CheckLines(llvm::SmallVector<CheckLine> lines)
  125. : lines(std::move(lines)), cursor(this->lines.begin()) {}
  126. // The full list of check lines.
  127. llvm::SmallVector<CheckLine> lines;
  128. // An iterator into check_lines.
  129. CheckLine* cursor;
  130. };
  131. // Looks for the patterns in the line. Returns the first match, or defaulted
  132. // information if not found.
  133. auto GetFileAndLineNumber(
  134. const llvm::DenseMap<llvm::StringRef, int>& file_to_number_map,
  135. int default_file_number, const std::string& check_line)
  136. -> FileAndLineNumber;
  137. // Builds CheckLine lists for autoupdate.
  138. auto BuildCheckLines(llvm::StringRef output, const char* label) -> CheckLines;
  139. // Adds a non-check line to the new_lines and output_line_remap. The caller
  140. // still needs to advance the cursor when ready.
  141. auto AddRemappedNonCheckLine() -> void;
  142. // Adds TIP lines for file_test usage.
  143. auto AddTips() -> void;
  144. // Returns true if there's a CheckLine that should be added at
  145. // `to_line_number`.
  146. auto ShouldAddCheckLine(const CheckLines& check_lines, bool to_file_end) const
  147. -> bool;
  148. // Adds check_lines until output reaches:
  149. // - If not to_file_end, non_check_line.
  150. // - If to_file_end, the end of the file.
  151. auto AddCheckLines(CheckLines& check_lines, bool to_file_end) -> void;
  152. // Adds remaining check lines for the current file. stderr is always included,
  153. // but stdout is only included when either any_attached_stdout_lines_ or
  154. // is_last_file is true.
  155. auto FinishFile(bool is_last_file) -> void;
  156. // Starts a new split file, updating file and line numbers. Advances past the
  157. // split line.
  158. auto StartSplitFile() -> void;
  159. // Passed-in state.
  160. const std::filesystem::path& file_test_path_;
  161. std::string test_command_;
  162. std::string dump_command_;
  163. llvm::StringRef input_content_;
  164. const llvm::SmallVector<llvm::StringRef>& filenames_;
  165. int autoupdate_line_number_;
  166. const llvm::SmallVector<FileTestLine>& non_check_lines_;
  167. const std::optional<RE2>& default_file_re_;
  168. const llvm::SmallVector<LineNumberReplacement>& line_number_replacements_;
  169. std::function<void(std::string&)> do_extra_check_replacements_;
  170. // Generated TIP lines, from AddTips.
  171. llvm::SmallVector<TipLine> tips_;
  172. // The constructed CheckLine list and cursor.
  173. CheckLines stdout_;
  174. CheckLines stderr_;
  175. // Whether any stdout lines have an associated line number.
  176. bool any_attached_stdout_lines_;
  177. // Iterators for the main Run loop.
  178. const FileTestLine* non_check_line_;
  179. // Tracks the new last line numbers for each file.
  180. llvm::SmallVector<int> new_last_line_numbers_;
  181. // A reusable blank line. new_lines_ can contain a reference back to it.
  182. const FileTestLine blank_line_ = FileTestLine(-1, -1, "");
  183. // Stitched-together content.
  184. llvm::SmallVector<const FileTestLineBase*> new_lines_;
  185. // Maps {file_number, original line number} to a new line number.
  186. llvm::DenseMap<std::pair<int, int>, int> output_line_remap_;
  187. // The current output file number; mainly used for tracking progression.
  188. int output_file_number_ = 0;
  189. // The current output line number in stitched content.
  190. int output_line_number_ = 0;
  191. };
  192. } // namespace Carbon::Testing
  193. #endif // CARBON_TESTING_FILE_TEST_AUTOUPDATE_H_