From 5c2e16b692ddd4feeda27f2662f6f04fd606d775 Mon Sep 17 00:00:00 2001 From: Richard Smith Date: Mon, 11 Sep 2023 13:52:58 -0700 Subject: [PATCH] Fix oscillating autoupdate output for empty source files. (#3209) toolchain/driver/testdata/fail_missing_file.carbon has no content after its AUTOUPDATE line. This caused it to oscillate between three states: 1) If there were no content lines after AUTOUPDATE, autoupdate would add a blank line and then its STDERR CHECK line. 2) If there was a (blank) content line after AUTOUPDATE, autoupdate would move the STDERR CHECK line to immediately after AUTOUPDATE, leaving a trailing blank line. 3) pre-commit would remove the trailing blank line, leaving no content lines after AUTOUPDATE. Handle the special case of no content lines after AUTOUPDATE by producing "early" STDERR check lines immediately after the AUTOUPDATE line, rather than before the next line, which might not exist. We already did this in the case where there were early STDOUT check lines. --- testing/file_test/autoupdate.cpp | 17 +++++++++-------- .../driver/testdata/fail_missing_file.carbon | 1 - 2 files changed, 9 insertions(+), 9 deletions(-) diff --git a/testing/file_test/autoupdate.cpp b/testing/file_test/autoupdate.cpp index 718d7e60f8d3..b945e42feea6 100644 --- a/testing/file_test/autoupdate.cpp +++ b/testing/file_test/autoupdate.cpp @@ -286,17 +286,18 @@ auto AutoupdateFileTest( .insert({non_check_line.line_number(), ++output_line_number}) .second); + // If we just added the AUTOUPDATE line, include any early STDERR lines + // now, so that the initial batch of CHECK lines have STDERR before + // STDOUT. This also ensures we don't insert a blank line before the + // STDERR checks if there are no more lines after AUTOUPDATE. + if (autoupdate_line_number == non_check_line.line_number()) { + add_check_lines(stderr_check_lines, stderr_check_line, + non_check_line.line_number(), non_check_line.indent()); + } + // STDOUT check lines are placed after the line they refer to, or at the // end of the file if none of them refers to a line. if (reached_autoupdate && any_attached_stdout_lines) { - // Include any early STDERR lines now, so that the initial batch of - // CHECK lines have STDERR before STDOUT. - if (autoupdate_line_number == non_check_line.line_number()) { - add_check_lines(stderr_check_lines, stderr_check_line, - non_check_line.line_number(), - non_check_line.indent()); - } - add_check_lines(stdout_check_lines, stdout_check_line, non_check_line.line_number(), non_check_line.indent()); } diff --git a/toolchain/driver/testdata/fail_missing_file.carbon b/toolchain/driver/testdata/fail_missing_file.carbon index 45528c34d07c..3d87be4cd51d 100644 --- a/toolchain/driver/testdata/fail_missing_file.carbon +++ b/toolchain/driver/testdata/fail_missing_file.carbon @@ -5,5 +5,4 @@ // ARGS: compile --phase=lex nonexistent.carbon // // AUTOUPDATE - // CHECK:STDERR: nonexistent.carbon: Error opening file for read: No such file or directory