autoupdate.cpp 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348
  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. #include "testing/file_test/autoupdate.h"
  5. #include <fstream>
  6. #include "absl/strings/string_view.h"
  7. #include "common/check.h"
  8. #include "common/ostream.h"
  9. #include "llvm/ADT/DenseMap.h"
  10. #include "llvm/ADT/STLFunctionalExtras.h"
  11. #include "llvm/ADT/StringExtras.h"
  12. #include "llvm/Support/FormatVariadic.h"
  13. #include "re2/re2.h"
  14. namespace Carbon::Testing {
  15. // Put helper classes in an anonymous namespace.
  16. namespace {
  17. // Converts a matched line number to an int, trimming whitespace.
  18. static auto ParseLineNumber(absl::string_view matched_line_number) -> int {
  19. llvm::StringRef trimmed = matched_line_number;
  20. trimmed = trimmed.trim();
  21. // NOLINTNEXTLINE(google-runtime-int): API requirement.
  22. long long val;
  23. CARBON_CHECK(!llvm::getAsSignedInteger(trimmed, 10, val));
  24. return val;
  25. }
  26. class CheckLine : public FileTestLineBase {
  27. public:
  28. // RE2 is passed by a pointer because it doesn't support std::optional.
  29. explicit CheckLine(int line_number, bool line_number_re_has_file,
  30. const RE2* line_number_re, std::string line)
  31. : FileTestLineBase(line_number),
  32. line_number_re_has_file_(line_number_re_has_file),
  33. line_number_re_(line_number_re),
  34. line_(std::move(line)) {}
  35. auto Print(llvm::raw_ostream& out) const -> void override {
  36. out << indent_ << line_;
  37. }
  38. // When the location of the CHECK in output is known, we can set the indent
  39. // and its line.
  40. auto SetOutputLine(llvm::StringRef indent, int output_line_number) -> void {
  41. indent_ = indent;
  42. output_line_number_ = output_line_number;
  43. }
  44. // When the location of all lines in a file are known, we can set the line
  45. // offset based on the target line.
  46. auto RemapLineNumbers(const std::string& line_formatv,
  47. llvm::function_ref<int(int)> line_remap) -> void {
  48. // Only need to do remappings when there's a regex.
  49. if (!line_number_re_) {
  50. return;
  51. }
  52. bool found_one = false;
  53. while (true) {
  54. // Look for a line number to replace. There may be multiple, so we
  55. // repeatedly check.
  56. absl::string_view matched_line_number;
  57. if (line_number_re_has_file_) {
  58. RE2::PartialMatch(line_, *line_number_re_, nullptr,
  59. &matched_line_number);
  60. } else {
  61. RE2::PartialMatch(line_, *line_number_re_, &matched_line_number);
  62. }
  63. if (matched_line_number.empty()) {
  64. CARBON_CHECK(found_one) << line_;
  65. return;
  66. }
  67. found_one = true;
  68. // Calculate the offset from the CHECK line to the new line number
  69. // (possibly with new CHECK lines added, or some removed).
  70. int new_line_number = line_remap(ParseLineNumber(matched_line_number));
  71. int offset = new_line_number - output_line_number_;
  72. // Update the line offset in the CHECK line.
  73. const char* offset_prefix = offset < 0 ? "" : "+";
  74. std::string replacement = llvm::formatv(
  75. line_formatv.c_str(),
  76. llvm::formatv("[[@LINE{0}{1}]]", offset_prefix, offset));
  77. line_.replace(matched_line_number.data() - line_.data(),
  78. matched_line_number.size(), replacement);
  79. }
  80. }
  81. auto is_blank() const -> bool override { return false; }
  82. private:
  83. bool line_number_re_has_file_;
  84. const RE2* line_number_re_;
  85. std::string line_;
  86. llvm::StringRef indent_;
  87. int output_line_number_ = -1;
  88. };
  89. } // namespace
  90. // Builds CheckLine lists for autoupdate.
  91. static auto BuildCheckLines(
  92. llvm::StringRef output, const char* label,
  93. const llvm::SmallVector<llvm::StringRef>& filenames,
  94. bool line_number_re_has_file, const RE2& line_number_re,
  95. std::function<void(std::string&)> do_extra_check_replacements)
  96. -> llvm::SmallVector<llvm::SmallVector<CheckLine>> {
  97. llvm::SmallVector<llvm::SmallVector<CheckLine>> check_lines;
  98. check_lines.resize(filenames.size());
  99. if (output.empty()) {
  100. return check_lines;
  101. }
  102. // Prepare to look for filenames in lines.
  103. llvm::StringRef current_filename = filenames[0];
  104. const auto* remaining_filenames = filenames.begin() + 1;
  105. // %t substitution means we may see TEST_TMPDIR in output.
  106. char* tmpdir_env = getenv("TEST_TMPDIR");
  107. CARBON_CHECK(tmpdir_env != nullptr);
  108. llvm::StringRef tmpdir = tmpdir_env;
  109. llvm::SmallVector<llvm::StringRef> lines(llvm::split(output, '\n'));
  110. // It's typical that output ends with a newline, but we don't want to add a
  111. // blank CHECK for it.
  112. if (lines.back().empty()) {
  113. lines.pop_back();
  114. }
  115. // `{{` and `[[` are escaped as a regex matcher.
  116. RE2 double_brace_re(R"(\{\{)");
  117. RE2 double_square_bracket_re(R"(\[\[)");
  118. // End-of-line whitespace is replaced with a regex matcher to make it visible.
  119. RE2 end_of_line_whitespace_re(R"((\s+)$)");
  120. int append_to = 0;
  121. for (const auto& line : lines) {
  122. std::string check_line = llvm::formatv("// CHECK:{0}:{1}{2}", label,
  123. line.empty() ? "" : " ", line);
  124. RE2::Replace(&check_line, double_brace_re, R"({{\\{\\{}})");
  125. RE2::Replace(&check_line, double_square_bracket_re, R"({{\\[\\[}})");
  126. RE2::Replace(&check_line, end_of_line_whitespace_re, R"({{\1}})");
  127. // Ignore TEST_TMPDIR in output.
  128. if (auto pos = check_line.find(tmpdir); pos != std::string::npos) {
  129. check_line.replace(pos, tmpdir.size(), "{{.+}}");
  130. }
  131. do_extra_check_replacements(check_line);
  132. // Look for line information in the output. use_line_number is only set if
  133. // the match is correct.
  134. std::optional<llvm::StringRef> use_line_number;
  135. absl::string_view match_line_number;
  136. if (line_number_re_has_file) {
  137. absl::string_view match_filename;
  138. if (RE2::PartialMatch(check_line, line_number_re, &match_filename,
  139. &match_line_number)) {
  140. llvm::StringRef match_filename_ref = match_filename;
  141. if (match_filename_ref != current_filename) {
  142. // If the filename doesn't match, it may be still usable if it refers
  143. // to a later file.
  144. const auto* pos = std::find(remaining_filenames, filenames.end(),
  145. match_filename_ref);
  146. if (pos != filenames.end()) {
  147. remaining_filenames = pos + 1;
  148. append_to = pos - filenames.begin();
  149. use_line_number = match_line_number;
  150. }
  151. } else {
  152. // The line applies to the current file.
  153. use_line_number = match_line_number;
  154. }
  155. }
  156. } else {
  157. // There's no file association, so we only look at the line.
  158. if (RE2::PartialMatch(check_line, line_number_re, &match_line_number)) {
  159. use_line_number = match_line_number;
  160. }
  161. }
  162. int line_number = use_line_number ? ParseLineNumber(*use_line_number) : -1;
  163. check_lines[append_to].push_back(
  164. CheckLine(line_number, line_number_re_has_file,
  165. use_line_number ? &line_number_re : nullptr, check_line));
  166. }
  167. return check_lines;
  168. }
  169. auto AutoupdateFileTest(
  170. const std::filesystem::path& file_test_path, llvm::StringRef input_content,
  171. const llvm::SmallVector<llvm::StringRef>& filenames,
  172. int autoupdate_line_number,
  173. llvm::SmallVector<llvm::SmallVector<FileTestLine>>& non_check_lines,
  174. llvm::StringRef stdout, llvm::StringRef stderr,
  175. FileTestLineNumberReplacement line_number_replacement,
  176. std::function<void(std::string&)> do_extra_check_replacements) -> bool {
  177. RE2 line_number_re(line_number_replacement.pattern);
  178. CARBON_CHECK(line_number_re.ok()) << "Invalid line replacement RE2: `"
  179. << line_number_replacement.pattern << "`";
  180. // Prepare CHECK lines.
  181. llvm::SmallVector<llvm::SmallVector<CheckLine>> stdout_check_lines =
  182. BuildCheckLines(stdout, "STDOUT", filenames,
  183. line_number_replacement.has_file, line_number_re,
  184. do_extra_check_replacements);
  185. llvm::SmallVector<llvm::SmallVector<CheckLine>> stderr_check_lines =
  186. BuildCheckLines(stderr, "STDERR", filenames,
  187. line_number_replacement.has_file, line_number_re,
  188. do_extra_check_replacements);
  189. // All CHECK lines are suppressed until we reach AUTOUPDATE.
  190. bool reached_autoupdate = false;
  191. const FileTestLine blank_line(-1, "");
  192. // Stitch together content.
  193. llvm::SmallVector<const FileTestLineBase*> new_lines;
  194. for (auto [filename, non_check_file, stdout_check_file, stderr_check_file] :
  195. llvm::zip(filenames, non_check_lines, stdout_check_lines,
  196. stderr_check_lines)) {
  197. llvm::DenseMap<int, int> output_line_remap;
  198. int output_line_number = 0;
  199. auto* stdout_check_line = stdout_check_file.begin();
  200. auto* stderr_check_line = stderr_check_file.begin();
  201. // Add all check lines from the given vector until we reach a check line
  202. // attached to a line later than `to_line_number`.
  203. auto add_check_lines = [&](const llvm::SmallVector<CheckLine>& lines,
  204. CheckLine*& line, int to_line_number,
  205. llvm::StringRef indent) {
  206. for (; line != lines.end() && line->line_number() <= to_line_number;
  207. ++line) {
  208. new_lines.push_back(line);
  209. line->SetOutputLine(indent, ++output_line_number);
  210. }
  211. };
  212. bool any_attached_stdout_lines = std::any_of(
  213. stdout_check_file.begin(), stdout_check_file.end(),
  214. [&](const CheckLine& line) { return line.line_number() != -1; });
  215. // Looping through the original file, print check lines preceding each
  216. // original line.
  217. for (const auto& non_check_line : non_check_file) {
  218. // If there are any non-check lines with an invalid line_number, it's
  219. // something like a split directive which shouldn't increment
  220. // output_line_number.
  221. if (non_check_line.line_number() < 1) {
  222. new_lines.push_back(&non_check_line);
  223. continue;
  224. }
  225. // STDERR check lines are placed before the line they refer to, or as
  226. // early as possible if they don't refer to a line. Include all STDERR
  227. // lines until we find one that wants to go later in the file.
  228. if (reached_autoupdate) {
  229. add_check_lines(stderr_check_file, stderr_check_line,
  230. non_check_line.line_number(), non_check_line.indent());
  231. } else if (autoupdate_line_number == non_check_line.line_number()) {
  232. // This is the AUTOUPDATE line, so we'll print it, then start printing
  233. // CHECK lines.
  234. reached_autoupdate = true;
  235. }
  236. new_lines.push_back(&non_check_line);
  237. CARBON_CHECK(
  238. output_line_remap
  239. .insert({non_check_line.line_number(), ++output_line_number})
  240. .second);
  241. // STDOUT check lines are placed after the line they refer to, or at the
  242. // end of the file if none of them refers to a line.
  243. if (reached_autoupdate && any_attached_stdout_lines) {
  244. add_check_lines(stdout_check_file, stdout_check_line,
  245. non_check_line.line_number(), non_check_line.indent());
  246. }
  247. }
  248. // This should always be true after the first file is processed.
  249. CARBON_CHECK(reached_autoupdate);
  250. // Print remaining check lines which -- for whatever reason -- come after
  251. // all original lines.
  252. if (stderr_check_line != stderr_check_file.end() ||
  253. stdout_check_line != stdout_check_file.end()) {
  254. // Ensure there's a blank line before any trailing CHECKs.
  255. if (!new_lines.empty() && !new_lines.back()->is_blank()) {
  256. new_lines.push_back(&blank_line);
  257. ++output_line_number;
  258. }
  259. add_check_lines(stderr_check_file, stderr_check_line, INT_MAX, "");
  260. add_check_lines(stdout_check_file, stdout_check_line, INT_MAX, "");
  261. }
  262. // Update all remapped lines in CHECK output.
  263. for (auto* check_file : {&stdout_check_file, &stderr_check_file}) {
  264. for (auto& offset_check_line : *check_file) {
  265. int last_non_check_line = non_check_file.back().line_number();
  266. offset_check_line.RemapLineNumbers(
  267. line_number_replacement.line_formatv, [&](int old_line_number) {
  268. // Map old non-check lines to their new line numbers.
  269. auto remapped = output_line_remap.find(old_line_number);
  270. if (remapped != output_line_remap.end()) {
  271. return remapped->second;
  272. }
  273. // Map any reference to a line past the final non-check line to
  274. // the new end-of-file. We assume that any such reference is
  275. // referring to the end of file, not to some specific CHECK
  276. // comment.
  277. if (old_line_number > last_non_check_line) {
  278. return output_line_number;
  279. }
  280. // Line didn't get remapped; maybe it refers to a CHECK line.
  281. // We can't express that as an offset, just leave it as-is.
  282. return old_line_number;
  283. });
  284. }
  285. }
  286. }
  287. // Generate the autoupdated file.
  288. std::string new_content;
  289. llvm::raw_string_ostream new_content_stream(new_content);
  290. for (const auto& line : new_lines) {
  291. line->Print(new_content_stream);
  292. new_content_stream << '\n';
  293. }
  294. // Update the file on disk if needed.
  295. if (new_content == input_content) {
  296. return false;
  297. }
  298. std::ofstream out(file_test_path);
  299. out << new_content;
  300. return true;
  301. }
  302. } // namespace Carbon::Testing