test_file.cpp 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660
  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/test_file.h"
  5. #include <fstream>
  6. #include "llvm/ADT/StringExtras.h"
  7. #include "testing/base/file_helpers.h"
  8. namespace Carbon::Testing {
  9. using ::testing::Matcher;
  10. using ::testing::MatchesRegex;
  11. using ::testing::StrEq;
  12. // Processes conflict markers, including tracking of whether code is within a
  13. // conflict marker. Returns true if the line is consumed.
  14. static auto TryConsumeConflictMarker(bool running_autoupdate,
  15. llvm::StringRef line,
  16. llvm::StringRef line_trimmed,
  17. bool* inside_conflict_marker)
  18. -> ErrorOr<bool> {
  19. bool is_start = line.starts_with("<<<<<<<");
  20. bool is_middle = line.starts_with("=======") || line.starts_with("|||||||");
  21. bool is_end = line.starts_with(">>>>>>>");
  22. // When running the test, any conflict marker is an error.
  23. if (!running_autoupdate && (is_start || is_middle || is_end)) {
  24. return ErrorBuilder() << "Conflict marker found:\n" << line;
  25. }
  26. // Autoupdate tracks conflict markers for context, and will discard
  27. // conflicting lines when it can autoupdate them.
  28. if (*inside_conflict_marker) {
  29. if (is_start) {
  30. return ErrorBuilder() << "Unexpected conflict marker inside conflict:\n"
  31. << line;
  32. }
  33. if (is_middle) {
  34. return true;
  35. }
  36. if (is_end) {
  37. *inside_conflict_marker = false;
  38. return true;
  39. }
  40. // Look for CHECK and TIP lines, which can be discarded.
  41. if (line_trimmed.starts_with("// CHECK:STDOUT:") ||
  42. line_trimmed.starts_with("// CHECK:STDERR:") ||
  43. line_trimmed.starts_with("// TIP:")) {
  44. return true;
  45. }
  46. return ErrorBuilder()
  47. << "Autoupdate can't discard non-CHECK lines inside conflicts:\n"
  48. << line;
  49. } else {
  50. if (is_start) {
  51. *inside_conflict_marker = true;
  52. return true;
  53. }
  54. if (is_middle || is_end) {
  55. return ErrorBuilder() << "Unexpected conflict marker outside conflict:\n"
  56. << line;
  57. }
  58. return false;
  59. }
  60. }
  61. // State for file splitting logic: TryConsumeSplit and FinishSplit.
  62. struct SplitState {
  63. auto has_splits() const -> bool { return file_index > 0; }
  64. auto add_content(llvm::StringRef line) -> void {
  65. content.append(line.str());
  66. content.append("\n");
  67. }
  68. // Whether content has been found. Only updated before a file split is found
  69. // (which may be never).
  70. bool found_code_pre_split = false;
  71. // The current file name, considering splits. Empty for the default file.
  72. llvm::StringRef filename = "";
  73. // The accumulated content for the file being built. This may elide some of
  74. // the original content, such as conflict markers.
  75. std::string content;
  76. // The current file index.
  77. int file_index = 0;
  78. };
  79. // Reformats `[[@LSP:` and similar keyword as an LSP call with headers.
  80. static auto ReplaceLspKeywordAt(std::string* content, size_t keyword_pos,
  81. int& lsp_call_id) -> ErrorOr<size_t> {
  82. llvm::StringRef content_at_keyword =
  83. llvm::StringRef(*content).substr(keyword_pos);
  84. auto [keyword, body_start] = content_at_keyword.split(":");
  85. if (body_start.empty()) {
  86. return ErrorBuilder() << "Missing `:` for `"
  87. << content_at_keyword.take_front(10) << "`";
  88. }
  89. // Whether the first param is a method or id.
  90. llvm::StringRef method_or_id_label = "method";
  91. // Whether to attach the `lsp_call_id`.
  92. bool use_call_id = false;
  93. // The JSON label for extra content.
  94. llvm::StringRef extra_content_label;
  95. if (keyword == "[[@LSP-CALL") {
  96. use_call_id = true;
  97. extra_content_label = "params";
  98. } else if (keyword == "[[@LSP-NOTIFY") {
  99. extra_content_label = "params";
  100. } else if (keyword == "[[@LSP-REPLY") {
  101. method_or_id_label = "id";
  102. extra_content_label = "result";
  103. } else if (keyword != "[[@LSP") {
  104. return ErrorBuilder() << "Unrecognized @LSP keyword at `"
  105. << keyword.take_front(10) << "`";
  106. }
  107. static constexpr llvm::StringLiteral LspEnd = "]]";
  108. auto body_end = body_start.find(LspEnd);
  109. if (body_end == std::string::npos) {
  110. return ErrorBuilder() << "Missing `" << LspEnd << "` after `" << keyword
  111. << "`";
  112. }
  113. llvm::StringRef body = body_start.take_front(body_end);
  114. auto [method_or_id, extra_content] = body.split(":");
  115. // Form the JSON.
  116. std::string json = llvm::formatv(R"({{"jsonrpc": "2.0", "{0}": "{1}")",
  117. method_or_id_label, method_or_id);
  118. if (use_call_id) {
  119. // Omit quotes on the ID because we know it's an integer.
  120. json += llvm::formatv(R"(, "id": {0})", ++lsp_call_id);
  121. }
  122. if (!extra_content.empty()) {
  123. json += ",";
  124. if (extra_content_label.empty()) {
  125. if (!extra_content.starts_with("\n")) {
  126. json += " ";
  127. }
  128. json += extra_content;
  129. } else {
  130. json += llvm::formatv(R"( "{0}": {{{1}})", extra_content_label,
  131. extra_content);
  132. }
  133. }
  134. json += "}";
  135. // Add the Content-Length header. The `2` accounts for extra newlines.
  136. auto json_with_header =
  137. llvm::formatv("Content-Length: {0}\n\n{1}\n", json.size() + 2, json)
  138. .str();
  139. int keyword_len =
  140. (body_start.data() + body_end + LspEnd.size()) - keyword.data();
  141. content->replace(keyword_pos, keyword_len, json_with_header);
  142. return keyword_pos + json_with_header.size();
  143. }
  144. // Replaces the keyword at the given position. Returns the position to start a
  145. // find for the next keyword.
  146. static auto ReplaceContentKeywordAt(std::string* content, size_t keyword_pos,
  147. llvm::StringRef test_name, int& lsp_call_id)
  148. -> ErrorOr<size_t> {
  149. auto keyword = llvm::StringRef(*content).substr(keyword_pos);
  150. // Line replacements aren't handled here.
  151. static constexpr llvm::StringLiteral Line = "[[@LINE";
  152. if (keyword.starts_with(Line)) {
  153. // Just move past the prefix to find the next one.
  154. return keyword_pos + Line.size();
  155. }
  156. // Replaced with the actual test name.
  157. static constexpr llvm::StringLiteral TestName = "[[@TEST_NAME]]";
  158. if (keyword.starts_with(TestName)) {
  159. content->replace(keyword_pos, TestName.size(), test_name);
  160. return keyword_pos + test_name.size();
  161. }
  162. if (keyword.starts_with("[[@LSP")) {
  163. return ReplaceLspKeywordAt(content, keyword_pos, lsp_call_id);
  164. }
  165. return ErrorBuilder() << "Unexpected use of `[[@` at `"
  166. << keyword.substr(0, 5) << "`";
  167. }
  168. // Replaces the content keywords.
  169. //
  170. // TEST_NAME is the only content keyword at present, but we do validate that
  171. // other names are reserved.
  172. static auto ReplaceContentKeywords(llvm::StringRef filename,
  173. std::string* content) -> ErrorOr<Success> {
  174. static constexpr llvm::StringLiteral Prefix = "[[@";
  175. auto keyword_pos = content->find(Prefix);
  176. // Return early if not finding anything.
  177. if (keyword_pos == std::string::npos) {
  178. return Success();
  179. }
  180. // Construct the test name by getting the base name without the extension,
  181. // then removing any "fail_" or "todo_" prefixes.
  182. llvm::StringRef test_name = filename;
  183. if (auto last_slash = test_name.rfind("/");
  184. last_slash != llvm::StringRef::npos) {
  185. test_name = test_name.substr(last_slash + 1);
  186. }
  187. if (auto ext_dot = test_name.find("."); ext_dot != llvm::StringRef::npos) {
  188. test_name = test_name.substr(0, ext_dot);
  189. }
  190. // Note this also handles `fail_todo_` and `todo_fail_`.
  191. test_name.consume_front("todo_");
  192. test_name.consume_front("fail_");
  193. test_name.consume_front("todo_");
  194. // A counter for LSP calls.
  195. int lsp_call_id = 0;
  196. while (keyword_pos != std::string::npos) {
  197. CARBON_ASSIGN_OR_RETURN(
  198. auto keyword_end,
  199. ReplaceContentKeywordAt(content, keyword_pos, test_name, lsp_call_id));
  200. keyword_pos = content->find(Prefix, keyword_end);
  201. }
  202. return Success();
  203. }
  204. // Adds a file. Used for both split and unsplit test files.
  205. static auto AddSplit(llvm::StringRef filename, std::string* content,
  206. llvm::SmallVector<TestFile::Split>* file_splits)
  207. -> ErrorOr<Success> {
  208. CARBON_RETURN_IF_ERROR(ReplaceContentKeywords(filename, content));
  209. file_splits->push_back(
  210. {.filename = filename.str(), .content = std::move(*content)});
  211. content->clear();
  212. return Success();
  213. }
  214. // Process file split ("---") lines when found. Returns true if the line is
  215. // consumed.
  216. static auto TryConsumeSplit(llvm::StringRef line, llvm::StringRef line_trimmed,
  217. bool found_autoupdate, int* line_index,
  218. SplitState* split,
  219. llvm::SmallVector<TestFile::Split>* file_splits,
  220. llvm::SmallVector<FileTestLine>* non_check_lines)
  221. -> ErrorOr<bool> {
  222. if (!line_trimmed.consume_front("// ---")) {
  223. if (!split->has_splits() && !line_trimmed.starts_with("//") &&
  224. !line_trimmed.empty()) {
  225. split->found_code_pre_split = true;
  226. }
  227. // Add the line to the current file's content (which may not be a split
  228. // file).
  229. split->add_content(line);
  230. return false;
  231. }
  232. if (!found_autoupdate) {
  233. // If there's a split, all output is appended at the end of each file
  234. // before AUTOUPDATE. We may want to change that, but it's not
  235. // necessary to handle right now.
  236. return ErrorBuilder() << "AUTOUPDATE/NOAUTOUPDATE setting must be in "
  237. "the first file.";
  238. }
  239. // On a file split, add the previous file, then start a new one.
  240. if (split->has_splits()) {
  241. CARBON_RETURN_IF_ERROR(
  242. AddSplit(split->filename, &split->content, file_splits));
  243. } else {
  244. split->content.clear();
  245. if (split->found_code_pre_split) {
  246. // For the first split, we make sure there was no content prior.
  247. return ErrorBuilder() << "When using split files, there must be no "
  248. "content before the first split file.";
  249. }
  250. }
  251. ++split->file_index;
  252. split->filename = line_trimmed.trim();
  253. if (split->filename.empty()) {
  254. return ErrorBuilder() << "Missing filename for split.";
  255. }
  256. // The split line is added to non_check_lines for retention in autoupdate, but
  257. // is not added to the test file content.
  258. *line_index = 0;
  259. non_check_lines->push_back(
  260. FileTestLine(split->file_index, *line_index, line));
  261. return true;
  262. }
  263. // Converts a `FileCheck`-style expectation string into a single complete regex
  264. // string by escaping all regex characters outside of the designated `{{...}}`
  265. // regex sequences, and switching those to a normal regex sub-pattern syntax.
  266. static void ConvertExpectationStringToRegex(std::string& str) {
  267. for (int pos = 0; pos < static_cast<int>(str.size());) {
  268. switch (str[pos]) {
  269. case '(':
  270. case ')':
  271. case '[':
  272. case ']':
  273. case '}':
  274. case '.':
  275. case '^':
  276. case '$':
  277. case '*':
  278. case '+':
  279. case '?':
  280. case '|':
  281. case '\\': {
  282. // Escape regex characters.
  283. str.insert(pos, "\\");
  284. pos += 2;
  285. break;
  286. }
  287. case '{': {
  288. if (pos + 1 == static_cast<int>(str.size()) || str[pos + 1] != '{') {
  289. // Single `{`, escape it.
  290. str.insert(pos, "\\");
  291. pos += 2;
  292. break;
  293. }
  294. // Replace the `{{...}}` regex syntax with standard `(...)` syntax.
  295. str.replace(pos, 2, "(");
  296. for (++pos; pos < static_cast<int>(str.size() - 1); ++pos) {
  297. if (str[pos] == '}' && str[pos + 1] == '}') {
  298. str.replace(pos, 2, ")");
  299. ++pos;
  300. break;
  301. }
  302. }
  303. break;
  304. }
  305. default: {
  306. ++pos;
  307. }
  308. }
  309. }
  310. }
  311. // Transforms an expectation on a given line from `FileCheck` syntax into a
  312. // standard regex matcher.
  313. static auto TransformExpectation(int line_index, llvm::StringRef in)
  314. -> ErrorOr<Matcher<std::string>> {
  315. if (in.empty()) {
  316. return Matcher<std::string>{StrEq("")};
  317. }
  318. if (!in.consume_front(" ")) {
  319. return ErrorBuilder() << "Malformated CHECK line: " << in;
  320. }
  321. // Check early if we have a regex component as we can avoid building an
  322. // expensive matcher when not using those.
  323. bool has_regex = in.find("{{") != llvm::StringRef::npos;
  324. // Now scan the string and expand any keywords. Note that this needs to be
  325. // `size_t` to correctly store `npos`.
  326. size_t keyword_pos = in.find("[[");
  327. // If there are neither keywords nor regex sequences, we can match the
  328. // incoming string directly.
  329. if (!has_regex && keyword_pos == llvm::StringRef::npos) {
  330. return Matcher<std::string>{StrEq(in)};
  331. }
  332. std::string str = in.str();
  333. // First expand the keywords.
  334. while (keyword_pos != std::string::npos) {
  335. llvm::StringRef line_keyword_cursor =
  336. llvm::StringRef(str).substr(keyword_pos);
  337. CARBON_CHECK(line_keyword_cursor.consume_front("[["));
  338. static constexpr llvm::StringLiteral LineKeyword = "@LINE";
  339. if (!line_keyword_cursor.consume_front(LineKeyword)) {
  340. return ErrorBuilder()
  341. << "Unexpected [[, should be {{\\[\\[}} at `"
  342. << line_keyword_cursor.substr(0, 5) << "` in: " << in;
  343. }
  344. // Allow + or - here; consumeInteger handles -.
  345. line_keyword_cursor.consume_front("+");
  346. int offset;
  347. // consumeInteger returns true for errors, not false.
  348. if (line_keyword_cursor.consumeInteger(10, offset) ||
  349. !line_keyword_cursor.consume_front("]]")) {
  350. return ErrorBuilder()
  351. << "Unexpected @LINE offset at `"
  352. << line_keyword_cursor.substr(0, 5) << "` in: " << in;
  353. }
  354. std::string int_str = llvm::Twine(line_index + offset).str();
  355. int remove_len = (line_keyword_cursor.data() - str.data()) - keyword_pos;
  356. str.replace(keyword_pos, remove_len, int_str);
  357. keyword_pos += int_str.size();
  358. // Find the next keyword start or the end of the string.
  359. keyword_pos = str.find("[[", keyword_pos);
  360. }
  361. // If there was no regex, we can directly match the adjusted string.
  362. if (!has_regex) {
  363. return Matcher<std::string>{StrEq(str)};
  364. }
  365. // Otherwise, we need to turn the entire string into a regex by escaping
  366. // things outside the regex region and transforming the regex region into a
  367. // normal syntax.
  368. ConvertExpectationStringToRegex(str);
  369. return Matcher<std::string>{MatchesRegex(str)};
  370. }
  371. // Once all content is processed, do any remaining split processing.
  372. static auto FinishSplit(llvm::StringRef test_name, SplitState* split,
  373. llvm::SmallVector<TestFile::Split>* file_splits)
  374. -> ErrorOr<Success> {
  375. if (split->has_splits()) {
  376. return AddSplit(split->filename, &split->content, file_splits);
  377. } else {
  378. // If no file splitting happened, use the main file as the test file.
  379. // There will always be a `/` unless tests are in the repo root.
  380. return AddSplit(test_name.drop_front(test_name.rfind("/") + 1),
  381. &split->content, file_splits);
  382. }
  383. }
  384. // Process CHECK lines when found. Returns true if the line is consumed.
  385. static auto TryConsumeCheck(
  386. bool running_autoupdate, int line_index, llvm::StringRef line,
  387. llvm::StringRef line_trimmed,
  388. llvm::SmallVector<testing::Matcher<std::string>>* expected_stdout,
  389. llvm::SmallVector<testing::Matcher<std::string>>* expected_stderr)
  390. -> ErrorOr<bool> {
  391. if (!line_trimmed.consume_front("// CHECK")) {
  392. return false;
  393. }
  394. // Don't build expectations when doing an autoupdate. We don't want to
  395. // break the autoupdate on an invalid CHECK line.
  396. if (!running_autoupdate) {
  397. llvm::SmallVector<Matcher<std::string>>* expected;
  398. if (line_trimmed.consume_front(":STDOUT:")) {
  399. expected = expected_stdout;
  400. } else if (line_trimmed.consume_front(":STDERR:")) {
  401. expected = expected_stderr;
  402. } else {
  403. return ErrorBuilder() << "Unexpected CHECK in input: " << line.str();
  404. }
  405. CARBON_ASSIGN_OR_RETURN(Matcher<std::string> check_matcher,
  406. TransformExpectation(line_index, line_trimmed));
  407. expected->push_back(check_matcher);
  408. }
  409. return true;
  410. }
  411. // Processes ARGS and EXTRA-ARGS lines when found. Returns true if the line is
  412. // consumed.
  413. static auto TryConsumeArgs(llvm::StringRef line, llvm::StringRef line_trimmed,
  414. llvm::SmallVector<std::string>* args,
  415. llvm::SmallVector<std::string>* extra_args)
  416. -> ErrorOr<bool> {
  417. llvm::SmallVector<std::string>* arg_list = nullptr;
  418. if (line_trimmed.consume_front("// ARGS: ")) {
  419. arg_list = args;
  420. } else if (line_trimmed.consume_front("// EXTRA-ARGS: ")) {
  421. arg_list = extra_args;
  422. } else {
  423. return false;
  424. }
  425. if (!args->empty() || !extra_args->empty()) {
  426. return ErrorBuilder() << "ARGS / EXTRA-ARGS specified multiple times: "
  427. << line.str();
  428. }
  429. // Split the line into arguments.
  430. std::pair<llvm::StringRef, llvm::StringRef> cursor =
  431. llvm::getToken(line_trimmed);
  432. while (!cursor.first.empty()) {
  433. arg_list->push_back(std::string(cursor.first));
  434. cursor = llvm::getToken(cursor.second);
  435. }
  436. return true;
  437. }
  438. // Processes AUTOUPDATE lines when found. Returns true if the line is consumed.
  439. static auto TryConsumeAutoupdate(int line_index, llvm::StringRef line_trimmed,
  440. bool* found_autoupdate,
  441. std::optional<int>* autoupdate_line_number)
  442. -> ErrorOr<bool> {
  443. static constexpr llvm::StringLiteral Autoupdate = "// AUTOUPDATE";
  444. static constexpr llvm::StringLiteral NoAutoupdate = "// NOAUTOUPDATE";
  445. if (line_trimmed != Autoupdate && line_trimmed != NoAutoupdate) {
  446. return false;
  447. }
  448. if (*found_autoupdate) {
  449. return ErrorBuilder() << "Multiple AUTOUPDATE/NOAUTOUPDATE settings found";
  450. }
  451. *found_autoupdate = true;
  452. if (line_trimmed == Autoupdate) {
  453. *autoupdate_line_number = line_index;
  454. }
  455. return true;
  456. }
  457. // Processes SET-* lines when found. Returns true if the line is consumed.
  458. static auto TryConsumeSetFlag(llvm::StringRef line_trimmed,
  459. llvm::StringLiteral flag_name, bool* flag)
  460. -> ErrorOr<bool> {
  461. if (!line_trimmed.consume_front("// ") || line_trimmed != flag_name) {
  462. return false;
  463. }
  464. if (*flag) {
  465. return ErrorBuilder() << flag_name << " was specified multiple times";
  466. }
  467. *flag = true;
  468. return true;
  469. }
  470. auto ProcessTestFile(llvm::StringRef test_name, bool running_autoupdate)
  471. -> ErrorOr<TestFile> {
  472. TestFile test_file;
  473. // Store the file so that file_splits can use references to content.
  474. CARBON_ASSIGN_OR_RETURN(test_file.input_content, ReadFile(test_name.str()));
  475. // Original file content, and a cursor for walking through it.
  476. llvm::StringRef file_content = test_file.input_content;
  477. llvm::StringRef cursor = file_content;
  478. // Whether either AUTOUDPATE or NOAUTOUPDATE was found.
  479. bool found_autoupdate = false;
  480. // The index in the current test file. Will be reset on splits.
  481. int line_index = 0;
  482. SplitState split;
  483. // When autoupdating, we track whether we're inside conflict markers.
  484. // Otherwise conflict markers are errors.
  485. bool inside_conflict_marker = false;
  486. while (!cursor.empty()) {
  487. auto [line, next_cursor] = cursor.split("\n");
  488. cursor = next_cursor;
  489. auto line_trimmed = line.ltrim();
  490. bool is_consumed = false;
  491. CARBON_ASSIGN_OR_RETURN(
  492. is_consumed,
  493. TryConsumeConflictMarker(running_autoupdate, line, line_trimmed,
  494. &inside_conflict_marker));
  495. if (is_consumed) {
  496. continue;
  497. }
  498. // At this point, remaining lines are part of the test input.
  499. CARBON_ASSIGN_OR_RETURN(
  500. is_consumed,
  501. TryConsumeSplit(line, line_trimmed, found_autoupdate, &line_index,
  502. &split, &test_file.file_splits,
  503. &test_file.non_check_lines));
  504. if (is_consumed) {
  505. continue;
  506. }
  507. ++line_index;
  508. // TIP lines have no impact on validation.
  509. if (line_trimmed.starts_with("// TIP:")) {
  510. continue;
  511. }
  512. CARBON_ASSIGN_OR_RETURN(
  513. is_consumed, TryConsumeCheck(running_autoupdate, line_index, line,
  514. line_trimmed, &test_file.expected_stdout,
  515. &test_file.expected_stderr));
  516. if (is_consumed) {
  517. continue;
  518. }
  519. // At this point, lines are retained as non-CHECK lines.
  520. test_file.non_check_lines.push_back(
  521. FileTestLine(split.file_index, line_index, line));
  522. CARBON_ASSIGN_OR_RETURN(
  523. is_consumed, TryConsumeArgs(line, line_trimmed, &test_file.test_args,
  524. &test_file.extra_args));
  525. if (is_consumed) {
  526. continue;
  527. }
  528. CARBON_ASSIGN_OR_RETURN(
  529. is_consumed,
  530. TryConsumeAutoupdate(line_index, line_trimmed, &found_autoupdate,
  531. &test_file.autoupdate_line_number));
  532. if (is_consumed) {
  533. continue;
  534. }
  535. CARBON_ASSIGN_OR_RETURN(
  536. is_consumed,
  537. TryConsumeSetFlag(line_trimmed, "SET-CAPTURE-CONSOLE-OUTPUT",
  538. &test_file.capture_console_output));
  539. if (is_consumed) {
  540. continue;
  541. }
  542. CARBON_ASSIGN_OR_RETURN(is_consumed,
  543. TryConsumeSetFlag(line_trimmed, "SET-CHECK-SUBSET",
  544. &test_file.check_subset));
  545. if (is_consumed) {
  546. continue;
  547. }
  548. }
  549. if (!found_autoupdate) {
  550. return Error("Missing AUTOUPDATE/NOAUTOUPDATE setting");
  551. }
  552. test_file.has_splits = split.has_splits();
  553. CARBON_RETURN_IF_ERROR(
  554. FinishSplit(test_name, &split, &test_file.file_splits));
  555. // Validate AUTOUPDATE-SPLIT use, and remove it from test files if present.
  556. if (test_file.has_splits) {
  557. constexpr llvm::StringLiteral AutoupdateSplit = "AUTOUPDATE-SPLIT";
  558. for (const auto& test_file :
  559. llvm::ArrayRef(test_file.file_splits).drop_back()) {
  560. if (test_file.filename == AutoupdateSplit) {
  561. return Error("AUTOUPDATE-SPLIT must be the last split");
  562. }
  563. }
  564. if (test_file.file_splits.back().filename == AutoupdateSplit) {
  565. if (!test_file.autoupdate_line_number) {
  566. return Error("AUTOUPDATE-SPLIT requires AUTOUPDATE");
  567. }
  568. test_file.autoupdate_split = true;
  569. test_file.file_splits.pop_back();
  570. }
  571. }
  572. // Assume there is always a suffix `\n` in output.
  573. if (!test_file.expected_stdout.empty()) {
  574. test_file.expected_stdout.push_back(StrEq(""));
  575. }
  576. if (!test_file.expected_stderr.empty()) {
  577. test_file.expected_stderr.push_back(StrEq(""));
  578. }
  579. return std::move(test_file);
  580. }
  581. } // namespace Carbon::Testing