test_file.cpp 32 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914
  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 <optional>
  7. #include <string>
  8. #include <utility>
  9. #include "common/check.h"
  10. #include "common/error.h"
  11. #include "common/find.h"
  12. #include "common/raw_string_ostream.h"
  13. #include "common/set.h"
  14. #include "llvm/ADT/StringExtras.h"
  15. #include "llvm/Support/JSON.h"
  16. #include "testing/base/file_helpers.h"
  17. #include "testing/file_test/line.h"
  18. namespace Carbon::Testing {
  19. using ::testing::Matcher;
  20. using ::testing::MatchesRegex;
  21. using ::testing::StrEq;
  22. // Processes conflict markers, including tracking of whether code is within a
  23. // conflict marker. Returns true if the line is consumed.
  24. static auto TryConsumeConflictMarker(bool running_autoupdate,
  25. llvm::StringRef line,
  26. llvm::StringRef line_trimmed,
  27. bool& inside_conflict_marker)
  28. -> ErrorOr<bool> {
  29. bool is_start = line.starts_with("<<<<<<<");
  30. bool is_end = line.starts_with(">>>>>>>");
  31. bool is_middle =
  32. // git internal conflict markers ("merge" and "diff3" style).
  33. line.starts_with("=======") ||
  34. line.starts_with("|||||||")
  35. // jj internal conflict markers ("snapshot" style).
  36. || line.starts_with("+++++++") || line.starts_with("-------");
  37. // jj internal conflict marker ("diff" style)
  38. bool is_jj_diff = line.starts_with("%%%%%%%");
  39. // When running the test, any conflict marker is an error.
  40. if (!running_autoupdate && (is_start || is_middle || is_end || is_jj_diff)) {
  41. return ErrorBuilder() << "Conflict marker found:\n" << line;
  42. }
  43. if (is_jj_diff && running_autoupdate) {
  44. // TODO: Add support for JJ's diff-style conflict markers.
  45. return ErrorBuilder()
  46. << "Found jj \"diff\" style conflict marker."
  47. " Autoupdate only supports \"snapshot\" style conflict markers."
  48. " To switch, use `jj config set --repo ui.conflict-marker-style"
  49. " \"snapshot\"`, and then run `jj new` (or `jj edit`) again to "
  50. " materialize the new style. For more details, see: "
  51. "https://docs.jj-vcs.dev/latest/conflicts/";
  52. }
  53. // Autoupdate tracks conflict markers for context, and will discard
  54. // conflicting lines when it can autoupdate them.
  55. if (inside_conflict_marker) {
  56. if (is_start) {
  57. return ErrorBuilder() << "Unexpected conflict marker inside conflict:\n"
  58. << line;
  59. }
  60. if (is_middle) {
  61. return true;
  62. }
  63. if (is_end) {
  64. inside_conflict_marker = false;
  65. return true;
  66. }
  67. // Look for CHECK and TIP lines, which can be discarded.
  68. if (line_trimmed.starts_with("// CHECK:STDOUT:") ||
  69. line_trimmed.starts_with("// CHECK:STDERR:") ||
  70. line_trimmed.starts_with("// TIP:")) {
  71. return true;
  72. }
  73. return ErrorBuilder()
  74. << "Autoupdate can't discard non-CHECK lines inside conflicts:\n"
  75. << line;
  76. } else {
  77. if (is_start) {
  78. inside_conflict_marker = true;
  79. return true;
  80. }
  81. if (is_middle || is_end) {
  82. return ErrorBuilder() << "Unexpected conflict marker outside conflict:\n"
  83. << line;
  84. }
  85. return false;
  86. }
  87. }
  88. // State for file splitting logic: TryConsumeSplit and FinishSplit.
  89. struct SplitState {
  90. auto has_splits() const -> bool { return file_index > 0; }
  91. auto add_content(llvm::StringRef line) -> void {
  92. content.append(line.str());
  93. content.append("\n");
  94. }
  95. // Whether content has been found. Only updated before a file split is found
  96. // (which may be never).
  97. bool found_code_pre_split = false;
  98. // The current file name, considering splits. Empty for the default file.
  99. llvm::StringRef filename = "";
  100. // The accumulated content for the file being built. This may elide some of
  101. // the original content, such as conflict markers.
  102. std::string content;
  103. // The current file index.
  104. int file_index = 0;
  105. };
  106. // Given a `file:/<filename>` URI, returns the filename.
  107. static auto ExtractFilePathFromUri(llvm::StringRef uri)
  108. -> ErrorOr<llvm::StringRef> {
  109. static constexpr llvm::StringRef FilePrefix = "file:/";
  110. if (!uri.starts_with(FilePrefix)) {
  111. return ErrorBuilder() << "uri `" << uri << "` is not a file uri";
  112. }
  113. return uri.drop_front(FilePrefix.size());
  114. }
  115. // When `FROM_FILE_SPLIT` is used in path `textDocument.text`, populate the
  116. // value from the split matching the `uri`. Only used for
  117. // `textDocument/didOpen`.
  118. static auto AutoFillDidOpenParams(llvm::json::Object& params,
  119. llvm::ArrayRef<TestFile::Split> splits)
  120. -> ErrorOr<Success> {
  121. auto* text_document = params.getObject("textDocument");
  122. if (text_document == nullptr) {
  123. return Success();
  124. }
  125. auto attr_it = text_document->find("text");
  126. if (attr_it == text_document->end() || attr_it->second != "FROM_FILE_SPLIT") {
  127. return Success();
  128. }
  129. auto uri = text_document->getString("uri");
  130. if (!uri) {
  131. return Error("missing uri in params.textDocument");
  132. }
  133. CARBON_ASSIGN_OR_RETURN(auto file_path, ExtractFilePathFromUri(*uri));
  134. const auto* split = FindIfOrNull(splits, [&](const TestFile::Split& split) {
  135. return split.filename == file_path;
  136. });
  137. if (!split) {
  138. return ErrorBuilder() << "No split found for uri: " << *uri;
  139. }
  140. attr_it->second = split->content;
  141. return Success();
  142. }
  143. // Reformats `[[@LSP:` and similar keyword as an LSP call with headers. Returns
  144. // the position to start a find for the next keyword.
  145. static auto ReplaceLspKeywordAt(std::string& content, size_t keyword_pos,
  146. int& lsp_call_id,
  147. llvm::ArrayRef<TestFile::Split> splits)
  148. -> ErrorOr<size_t> {
  149. llvm::StringRef content_at_keyword =
  150. llvm::StringRef(content).substr(keyword_pos);
  151. auto [keyword, body_start] = content_at_keyword.split(":");
  152. if (keyword.size() == content_at_keyword.size()) {
  153. return ErrorBuilder() << "Missing `:` for `"
  154. << content_at_keyword.take_front(10) << "`";
  155. }
  156. // Whether the first param is a method or id.
  157. llvm::StringRef method_or_id_label = "method";
  158. // Whether to attach the `lsp_call_id`.
  159. bool use_call_id = false;
  160. // The JSON label for extra content.
  161. llvm::StringRef extra_content_label;
  162. if (keyword == "[[@LSP-CALL") {
  163. use_call_id = true;
  164. extra_content_label = "params";
  165. } else if (keyword == "[[@LSP-NOTIFY") {
  166. extra_content_label = "params";
  167. } else if (keyword == "[[@LSP-REPLY") {
  168. method_or_id_label = "id";
  169. extra_content_label = "result";
  170. } else if (keyword != "[[@LSP") {
  171. return ErrorBuilder() << "Unrecognized @LSP keyword at `"
  172. << keyword.take_front(10) << "`";
  173. }
  174. static constexpr llvm::StringLiteral LspEnd = "]]";
  175. auto [body, rest] = body_start.split("]]");
  176. if (body.size() == body_start.size()) {
  177. return ErrorBuilder() << "Missing `" << LspEnd << "` after `" << keyword
  178. << "`";
  179. }
  180. auto [method_or_id, extra_content] = body.split(":");
  181. llvm::json::Value parsed_extra_content = nullptr;
  182. if (!extra_content.empty()) {
  183. std::string extra_content_as_object =
  184. llvm::formatv("{{{0}}", extra_content);
  185. auto parse_result = llvm::json::parse(extra_content_as_object);
  186. if (auto err = parse_result.takeError()) {
  187. return ErrorBuilder() << "Error parsing extra content: " << err;
  188. }
  189. parsed_extra_content = std::move(*parse_result);
  190. CARBON_CHECK(parsed_extra_content.kind() == llvm::json::Value::Object);
  191. if (extra_content_label == "params" &&
  192. method_or_id == "textDocument/didOpen") {
  193. CARBON_RETURN_IF_ERROR(
  194. AutoFillDidOpenParams(*parsed_extra_content.getAsObject(), splits));
  195. }
  196. }
  197. // Form the JSON.
  198. RawStringOstream buffer;
  199. llvm::json::OStream json(buffer);
  200. json.object([&] {
  201. json.attribute("jsonrpc", "2.0");
  202. json.attribute(method_or_id_label, method_or_id);
  203. if (use_call_id) {
  204. json.attribute("id", ++lsp_call_id);
  205. }
  206. if (parsed_extra_content != nullptr) {
  207. if (!extra_content_label.empty()) {
  208. json.attribute(extra_content_label, parsed_extra_content);
  209. } else {
  210. for (const auto& [key, value] : *parsed_extra_content.getAsObject()) {
  211. json.attribute(key, value);
  212. }
  213. }
  214. }
  215. });
  216. // Add the Content-Length header. The `2` accounts for extra newlines.
  217. int content_length = buffer.size() + 2;
  218. auto json_with_header = llvm::formatv("Content-Length: {0}\n\n{1}\n",
  219. content_length, buffer.TakeStr())
  220. .str();
  221. size_t keyword_len = rest.data() - keyword.data();
  222. content.replace(keyword_pos, keyword_len, json_with_header);
  223. return keyword_pos + json_with_header.size();
  224. }
  225. // Replaces `[[@0xAB]]` with the raw byte with value 0xAB. Returns the position
  226. // to start a find for the next keyword.
  227. static auto ReplaceRawByteKeywordAt(std::string& content, size_t keyword_pos)
  228. -> ErrorOr<size_t> {
  229. llvm::StringRef content_at_keyword =
  230. llvm::StringRef(content).substr(keyword_pos);
  231. auto [keyword, rest] = content_at_keyword.split("]]");
  232. if (keyword.size() == content_at_keyword.size()) {
  233. return ErrorBuilder() << "Missing `]]` after " << keyword.take_front(10)
  234. << "`";
  235. }
  236. unsigned char byte_value;
  237. if (keyword.substr(std::size("[[@0x") - 1).getAsInteger(16, byte_value)) {
  238. return ErrorBuilder() << "Invalid raw byte specifier `"
  239. << keyword.take_front(10) << "`";
  240. }
  241. content.replace(keyword_pos, keyword.size() + 2, 1, byte_value);
  242. return keyword_pos + 1;
  243. }
  244. // Replaces the keyword at the given position. Returns the position to start a
  245. // find for the next keyword.
  246. static auto ReplaceContentKeywordAt(std::string& content, size_t keyword_pos,
  247. llvm::StringRef test_name, int& lsp_call_id,
  248. llvm::ArrayRef<TestFile::Split> splits)
  249. -> ErrorOr<size_t> {
  250. auto keyword = llvm::StringRef(content).substr(keyword_pos);
  251. // Line replacements aren't handled here.
  252. static constexpr llvm::StringLiteral Line = "[[@LINE";
  253. if (keyword.starts_with(Line)) {
  254. // Just move past the prefix to find the next one.
  255. return keyword_pos + Line.size();
  256. }
  257. // Replaced with the actual test name.
  258. static constexpr llvm::StringLiteral TestName = "[[@TEST_NAME]]";
  259. if (keyword.starts_with(TestName)) {
  260. content.replace(keyword_pos, TestName.size(), test_name);
  261. return keyword_pos + test_name.size();
  262. }
  263. if (keyword.starts_with("[[@LSP")) {
  264. return ReplaceLspKeywordAt(content, keyword_pos, lsp_call_id, splits);
  265. }
  266. if (keyword.starts_with("[[@0x")) {
  267. return ReplaceRawByteKeywordAt(content, keyword_pos);
  268. }
  269. return ErrorBuilder() << "Unexpected use of `[[@` at `"
  270. << keyword.substr(0, 5) << "`";
  271. }
  272. // Replaces the content keywords.
  273. //
  274. // This handles content keywords such as [[@TEST_NAME]] and [[@LSP*]]. Unknown
  275. // content keywords are diagnosed.
  276. static auto ReplaceContentKeywords(llvm::StringRef filename,
  277. std::string& content,
  278. llvm::ArrayRef<TestFile::Split> splits)
  279. -> ErrorOr<Success> {
  280. static constexpr llvm::StringLiteral Prefix = "[[@";
  281. auto keyword_pos = content.find(Prefix);
  282. // Return early if not finding anything.
  283. if (keyword_pos == std::string::npos) {
  284. return Success();
  285. }
  286. // Construct the test name by getting the base name without the extension,
  287. // then removing any "fail_" or "todo_" prefixes.
  288. llvm::StringRef test_name = filename;
  289. if (auto last_slash = test_name.rfind("/");
  290. last_slash != llvm::StringRef::npos) {
  291. test_name = test_name.substr(last_slash + 1);
  292. }
  293. if (auto ext_dot = test_name.find("."); ext_dot != llvm::StringRef::npos) {
  294. test_name = test_name.substr(0, ext_dot);
  295. }
  296. // Note this also handles `fail_todo_` and `todo_fail_`.
  297. test_name.consume_front("todo_");
  298. test_name.consume_front("fail_");
  299. test_name.consume_front("todo_");
  300. // A counter for LSP calls.
  301. int lsp_call_id = 0;
  302. while (keyword_pos != std::string::npos) {
  303. CARBON_ASSIGN_OR_RETURN(
  304. auto keyword_end,
  305. ReplaceContentKeywordAt(content, keyword_pos, test_name, lsp_call_id,
  306. splits));
  307. keyword_pos = content.find(Prefix, keyword_end);
  308. }
  309. return Success();
  310. }
  311. // Adds a file. Used for both split and unsplit test files.
  312. static auto AddSplit(llvm::StringRef filename, std::string& content,
  313. llvm::SmallVector<TestFile::Split>& file_splits)
  314. -> ErrorOr<Success> {
  315. CARBON_RETURN_IF_ERROR(
  316. ReplaceContentKeywords(filename, content, file_splits));
  317. file_splits.push_back(
  318. {.filename = filename.str(), .content = std::move(content)});
  319. content.clear();
  320. return Success();
  321. }
  322. // Process file split ("---") lines when found. Returns true if the line is
  323. // consumed. `non_check_lines` is only provided for the main file, and will be
  324. // null for includes.
  325. static auto TryConsumeSplit(llvm::StringRef line, llvm::StringRef line_trimmed,
  326. bool missing_autoupdate, int& line_index,
  327. SplitState& split,
  328. llvm::SmallVector<TestFile::Split>& file_splits,
  329. llvm::SmallVector<FileTestLine>* non_check_lines)
  330. -> ErrorOr<bool> {
  331. if (!line_trimmed.consume_front("// ---")) {
  332. if (!split.has_splits() && !line_trimmed.starts_with("//") &&
  333. !line_trimmed.empty()) {
  334. split.found_code_pre_split = true;
  335. }
  336. // Add the line to the current file's content (which may not be a split
  337. // file).
  338. split.add_content(line);
  339. return false;
  340. }
  341. if (missing_autoupdate) {
  342. // If there's a split, all output is appended at the end of each file
  343. // before AUTOUPDATE. We may want to change that, but it's not
  344. // necessary to handle right now.
  345. return Error(
  346. "AUTOUPDATE/NOAUTOUPDATE setting must be in "
  347. "the first file.");
  348. }
  349. // On a file split, add the previous file, then start a new one.
  350. if (split.has_splits()) {
  351. CARBON_RETURN_IF_ERROR(
  352. AddSplit(split.filename, split.content, file_splits));
  353. } else {
  354. split.content.clear();
  355. if (split.found_code_pre_split) {
  356. // For the first split, we make sure there was no content prior.
  357. return Error(
  358. "When using split files, there must be no content before the first "
  359. "split file.");
  360. }
  361. }
  362. ++split.file_index;
  363. split.filename = line_trimmed.trim();
  364. if (split.filename.empty()) {
  365. return Error("Missing filename for split.");
  366. }
  367. // The split line is added to non_check_lines for retention in autoupdate, but
  368. // is not added to the test file content.
  369. line_index = 0;
  370. if (non_check_lines) {
  371. non_check_lines->push_back(
  372. FileTestLine(split.file_index, line_index, line));
  373. }
  374. return true;
  375. }
  376. // Converts a `FileCheck`-style expectation string into a single complete regex
  377. // string by escaping all regex characters outside of the designated `{{...}}`
  378. // regex sequences, and switching those to a normal regex sub-pattern syntax.
  379. static auto ConvertExpectationStringToRegex(std::string& str) -> void {
  380. for (int pos = 0; pos < static_cast<int>(str.size());) {
  381. switch (str[pos]) {
  382. case '(':
  383. case ')':
  384. case '[':
  385. case ']':
  386. case '}':
  387. case '.':
  388. case '^':
  389. case '$':
  390. case '*':
  391. case '+':
  392. case '?':
  393. case '|':
  394. case '\\': {
  395. // Escape regex characters.
  396. str.insert(pos, "\\");
  397. pos += 2;
  398. break;
  399. }
  400. case '{': {
  401. if (pos + 1 == static_cast<int>(str.size()) || str[pos + 1] != '{') {
  402. // Single `{`, escape it.
  403. str.insert(pos, "\\");
  404. pos += 2;
  405. break;
  406. }
  407. // Replace the `{{...}}` regex syntax with standard `(...)` syntax.
  408. str.replace(pos, 2, "(");
  409. for (++pos; pos < static_cast<int>(str.size() - 1); ++pos) {
  410. if (str[pos] == '}' && str[pos + 1] == '}') {
  411. str.replace(pos, 2, ")");
  412. ++pos;
  413. break;
  414. }
  415. }
  416. break;
  417. }
  418. default: {
  419. ++pos;
  420. }
  421. }
  422. }
  423. }
  424. // Transforms an expectation on a given line from `FileCheck` syntax into a
  425. // standard regex matcher.
  426. static auto TransformExpectation(int line_index, llvm::StringRef in)
  427. -> ErrorOr<Matcher<std::string>> {
  428. if (in.empty()) {
  429. return Matcher<std::string>{StrEq("")};
  430. }
  431. if (!in.consume_front(" ")) {
  432. return ErrorBuilder() << "Malformated CHECK line: " << in;
  433. }
  434. // Check early if we have a regex component as we can avoid building an
  435. // expensive matcher when not using those.
  436. bool has_regex = in.find("{{") != llvm::StringRef::npos;
  437. // Now scan the string and expand any keywords. Note that this needs to be
  438. // `size_t` to correctly store `npos`.
  439. size_t keyword_pos = in.find("[[");
  440. // If there are neither keywords nor regex sequences, we can match the
  441. // incoming string directly.
  442. if (!has_regex && keyword_pos == llvm::StringRef::npos) {
  443. return Matcher<std::string>{StrEq(in)};
  444. }
  445. std::string str = in.str();
  446. // First expand the keywords.
  447. while (keyword_pos != std::string::npos) {
  448. llvm::StringRef line_keyword_cursor =
  449. llvm::StringRef(str).substr(keyword_pos);
  450. CARBON_CHECK(line_keyword_cursor.consume_front("[["));
  451. static constexpr llvm::StringLiteral LineKeyword = "@LINE";
  452. if (!line_keyword_cursor.consume_front(LineKeyword)) {
  453. return ErrorBuilder()
  454. << "Unexpected [[, should be {{\\[\\[}} at `"
  455. << line_keyword_cursor.substr(0, 5) << "` in: " << in;
  456. }
  457. // Allow + or - here; consumeInteger handles -.
  458. line_keyword_cursor.consume_front("+");
  459. int offset;
  460. // consumeInteger returns true for errors, not false.
  461. if (line_keyword_cursor.consumeInteger(10, offset) ||
  462. !line_keyword_cursor.consume_front("]]")) {
  463. return ErrorBuilder()
  464. << "Unexpected @LINE offset at `"
  465. << line_keyword_cursor.substr(0, 5) << "` in: " << in;
  466. }
  467. std::string int_str = llvm::Twine(line_index + offset).str();
  468. int remove_len = (line_keyword_cursor.data() - str.data()) - keyword_pos;
  469. str.replace(keyword_pos, remove_len, int_str);
  470. keyword_pos += int_str.size();
  471. // Find the next keyword start or the end of the string.
  472. keyword_pos = str.find("[[", keyword_pos);
  473. }
  474. // If there was no regex, we can directly match the adjusted string.
  475. if (!has_regex) {
  476. return Matcher<std::string>{StrEq(str)};
  477. }
  478. // Otherwise, we need to turn the entire string into a regex by escaping
  479. // things outside the regex region and transforming the regex region into a
  480. // normal syntax.
  481. ConvertExpectationStringToRegex(str);
  482. return Matcher<std::string>{MatchesRegex(str)};
  483. }
  484. // Once all content is processed, do any remaining split processing.
  485. static auto FinishSplit(llvm::StringRef filename, bool is_include_file,
  486. SplitState& split,
  487. llvm::SmallVector<TestFile::Split>& file_splits)
  488. -> ErrorOr<Success> {
  489. if (split.has_splits()) {
  490. return AddSplit(split.filename, split.content, file_splits);
  491. } else {
  492. // If no file splitting happened, use the main file as the test file.
  493. // There will always be a `/` unless tests are in the repo root.
  494. std::string split_name = std::filesystem::path(filename.str()).filename();
  495. if (is_include_file) {
  496. split_name.insert(0, "include_files/");
  497. }
  498. return AddSplit(split_name, split.content, file_splits);
  499. }
  500. }
  501. // Process CHECK lines when found. Returns true if the line is consumed.
  502. // `expected_stdout` and `expected_stderr` are null in included files, where
  503. // it's an error to use `CHECK`.
  504. static auto TryConsumeCheck(
  505. bool running_autoupdate, int line_index, llvm::StringRef line,
  506. llvm::StringRef line_trimmed,
  507. llvm::SmallVector<testing::Matcher<std::string>>* expected_stdout,
  508. llvm::SmallVector<testing::Matcher<std::string>>* expected_stderr)
  509. -> ErrorOr<bool> {
  510. if (!line_trimmed.consume_front("// CHECK")) {
  511. return false;
  512. }
  513. if (!expected_stdout) {
  514. return ErrorBuilder() << "Included files can't add CHECKs: "
  515. << line_trimmed;
  516. }
  517. // Don't build expectations when doing an autoupdate. We don't want to
  518. // break the autoupdate on an invalid CHECK line.
  519. if (!running_autoupdate) {
  520. llvm::SmallVector<Matcher<std::string>>* expected;
  521. if (line_trimmed.consume_front(":STDOUT:")) {
  522. expected = expected_stdout;
  523. } else if (line_trimmed.consume_front(":STDERR:")) {
  524. expected = expected_stderr;
  525. } else {
  526. return ErrorBuilder() << "Unexpected CHECK in input: " << line.str();
  527. }
  528. CARBON_ASSIGN_OR_RETURN(Matcher<std::string> check_matcher,
  529. TransformExpectation(line_index, line_trimmed));
  530. expected->push_back(check_matcher);
  531. }
  532. return true;
  533. }
  534. // Processes ARGS and EXTRA-ARGS lines when found. Returns true if the line is
  535. // consumed.
  536. static auto TryConsumeArgs(llvm::StringRef line, llvm::StringRef line_trimmed,
  537. llvm::SmallVector<std::string>& args)
  538. -> ErrorOr<bool> {
  539. if (!line_trimmed.consume_front("// ARGS: ")) {
  540. return false;
  541. }
  542. if (!args.empty()) {
  543. return ErrorBuilder() << "ARGS specified multiple times: " << line.str();
  544. }
  545. // Split the line into arguments.
  546. std::pair<llvm::StringRef, llvm::StringRef> cursor =
  547. llvm::getToken(line_trimmed);
  548. while (!cursor.first.empty()) {
  549. args.push_back(std::string(cursor.first));
  550. cursor = llvm::getToken(cursor.second);
  551. }
  552. return true;
  553. }
  554. static auto TryConsumeExtraArgs(llvm::StringRef line_trimmed,
  555. llvm::SmallVector<std::string>& extra_args)
  556. -> ErrorOr<bool> {
  557. if (!line_trimmed.consume_front("// EXTRA-ARGS: ")) {
  558. return false;
  559. }
  560. // Split the line into arguments.
  561. std::pair<llvm::StringRef, llvm::StringRef> cursor =
  562. llvm::getToken(line_trimmed);
  563. while (!cursor.first.empty()) {
  564. extra_args.push_back(std::string(cursor.first));
  565. cursor = llvm::getToken(cursor.second);
  566. }
  567. return true;
  568. }
  569. static auto TryConsumeIncludeFile(llvm::StringRef line_trimmed,
  570. llvm::SmallVector<std::string>& include_files)
  571. -> ErrorOr<bool> {
  572. if (!line_trimmed.consume_front("// INCLUDE-FILE: ")) {
  573. return false;
  574. }
  575. include_files.push_back(line_trimmed.str());
  576. return true;
  577. }
  578. // Processes AUTOUPDATE lines when found. Returns true if the line is consumed.
  579. // `found_autoupdate` and `autoupdate_line_number` are only provided for the
  580. // main file; it's an error to have autoupdate in included files.
  581. static auto TryConsumeAutoupdate(int line_index, llvm::StringRef line_trimmed,
  582. bool* found_autoupdate,
  583. std::optional<int>* autoupdate_line_number)
  584. -> ErrorOr<bool> {
  585. static constexpr llvm::StringLiteral Autoupdate = "// AUTOUPDATE";
  586. static constexpr llvm::StringLiteral NoAutoupdate = "// NOAUTOUPDATE";
  587. if (line_trimmed != Autoupdate && line_trimmed != NoAutoupdate) {
  588. return false;
  589. }
  590. if (!found_autoupdate) {
  591. return ErrorBuilder() << "Included files can't control autoupdate: "
  592. << line_trimmed;
  593. }
  594. if (*found_autoupdate) {
  595. return Error("Multiple AUTOUPDATE/NOAUTOUPDATE settings found");
  596. }
  597. *found_autoupdate = true;
  598. if (line_trimmed == Autoupdate) {
  599. *autoupdate_line_number = line_index;
  600. }
  601. return true;
  602. }
  603. // Processes SET-* lines when found. Returns true if the line is consumed.
  604. // If `flag` is null, we're in an included file where the flag can't be set.
  605. static auto TryConsumeSetFlag(llvm::StringRef line_trimmed,
  606. llvm::StringLiteral flag_name, bool* flag)
  607. -> ErrorOr<bool> {
  608. if (!line_trimmed.consume_front("// ") || line_trimmed != flag_name) {
  609. return false;
  610. }
  611. if (!flag) {
  612. return ErrorBuilder() << "Included files can't set flag: " << line_trimmed;
  613. }
  614. if (*flag) {
  615. return ErrorBuilder() << flag_name << " was specified multiple times";
  616. }
  617. *flag = true;
  618. return true;
  619. }
  620. // Process content for either the main file (with `test_file` and
  621. // `found_autoupdate` provided) or an included file (with those arguments null).
  622. //
  623. // - `found_autoupdate` is set to true when either `AUTOUPDATE` or
  624. // `NOAUTOUPDATE` are found.
  625. // - `args` is set from `ARGS`.
  626. // - `extra_args` accumulates `EXTRA-ARGS`.
  627. // - `splits` accumulates split form for the test (`// --- <filename>`, or the
  628. // full file named as `filename` when there are no splits in the file).
  629. // - `include_files` accumulates `INCLUDE-FILE`.
  630. static auto ProcessFileContent(llvm::StringRef filename,
  631. llvm::StringRef content_cursor,
  632. bool running_autoupdate, TestFile* test_file,
  633. bool* found_autoupdate,
  634. llvm::SmallVector<std::string>& args,
  635. llvm::SmallVector<std::string>& extra_args,
  636. llvm::SmallVector<TestFile::Split>& splits,
  637. llvm::SmallVector<std::string>& include_files)
  638. -> ErrorOr<Success> {
  639. // The index in the current test file. Will be reset on splits.
  640. int line_index = 0;
  641. // When autoupdating, we track whether we're inside conflict markers.
  642. // Otherwise conflict markers are errors.
  643. bool inside_conflict_marker = false;
  644. SplitState split_state;
  645. while (!content_cursor.empty()) {
  646. auto [line, next_cursor] = content_cursor.split("\n");
  647. content_cursor = next_cursor;
  648. auto line_trimmed = line.ltrim();
  649. bool is_consumed = false;
  650. CARBON_ASSIGN_OR_RETURN(
  651. is_consumed,
  652. TryConsumeConflictMarker(running_autoupdate, line, line_trimmed,
  653. inside_conflict_marker));
  654. if (is_consumed) {
  655. continue;
  656. }
  657. // At this point, remaining lines are part of the test input.
  658. // We need to consume a split, but the main file has a little more handling.
  659. bool missing_autoupdate = false;
  660. llvm::SmallVector<FileTestLine>* non_check_lines = nullptr;
  661. if (test_file) {
  662. missing_autoupdate = !*found_autoupdate;
  663. non_check_lines = &test_file->non_check_lines;
  664. }
  665. CARBON_ASSIGN_OR_RETURN(
  666. is_consumed,
  667. TryConsumeSplit(line, line_trimmed, missing_autoupdate, line_index,
  668. split_state, splits, non_check_lines));
  669. if (is_consumed) {
  670. continue;
  671. }
  672. ++line_index;
  673. // TIP lines have no impact on validation.
  674. if (line_trimmed.starts_with("// TIP:")) {
  675. continue;
  676. }
  677. CARBON_ASSIGN_OR_RETURN(
  678. is_consumed,
  679. TryConsumeCheck(running_autoupdate, line_index, line, line_trimmed,
  680. test_file ? &test_file->expected_stdout : nullptr,
  681. test_file ? &test_file->expected_stderr : nullptr));
  682. if (is_consumed) {
  683. continue;
  684. }
  685. if (test_file) {
  686. // At this point, lines are retained as non-CHECK lines.
  687. test_file->non_check_lines.push_back(
  688. FileTestLine(split_state.file_index, line_index, line));
  689. }
  690. CARBON_ASSIGN_OR_RETURN(is_consumed,
  691. TryConsumeArgs(line, line_trimmed, args));
  692. if (is_consumed) {
  693. continue;
  694. }
  695. CARBON_ASSIGN_OR_RETURN(is_consumed,
  696. TryConsumeExtraArgs(line_trimmed, extra_args));
  697. if (is_consumed) {
  698. continue;
  699. }
  700. CARBON_ASSIGN_OR_RETURN(is_consumed,
  701. TryConsumeIncludeFile(line_trimmed, include_files));
  702. if (is_consumed) {
  703. continue;
  704. }
  705. CARBON_ASSIGN_OR_RETURN(
  706. is_consumed,
  707. TryConsumeAutoupdate(
  708. line_index, line_trimmed, found_autoupdate,
  709. test_file ? &test_file->autoupdate_line_number : nullptr));
  710. if (is_consumed) {
  711. continue;
  712. }
  713. CARBON_ASSIGN_OR_RETURN(
  714. is_consumed,
  715. TryConsumeSetFlag(
  716. line_trimmed, "SET-CAPTURE-CONSOLE-OUTPUT",
  717. test_file ? &test_file->capture_console_output : nullptr));
  718. if (is_consumed) {
  719. continue;
  720. }
  721. CARBON_ASSIGN_OR_RETURN(
  722. is_consumed,
  723. TryConsumeSetFlag(line_trimmed, "SET-CHECK-SUBSET",
  724. test_file ? &test_file->check_subset : nullptr));
  725. if (is_consumed) {
  726. continue;
  727. }
  728. }
  729. CARBON_RETURN_IF_ERROR(FinishSplit(filename, /*is_include_file=*/!test_file,
  730. split_state, splits));
  731. if (test_file) {
  732. test_file->has_splits = split_state.has_splits();
  733. }
  734. return Success();
  735. }
  736. auto ProcessTestFile(llvm::StringRef test_name, bool running_autoupdate)
  737. -> ErrorOr<TestFile> {
  738. TestFile test_file;
  739. // Store the original content, to avoid a read when autoupdating.
  740. CARBON_ASSIGN_OR_RETURN(test_file.input_content, ReadFile(test_name.str()));
  741. // Whether either AUTOUDPATE or NOAUTOUPDATE was found.
  742. bool found_autoupdate = false;
  743. // INCLUDE-FILE uses, accumulated across both the main file and any includes
  744. // (recursively).
  745. llvm::SmallVector<std::string> include_files;
  746. // Store the main file's `EXTRA-ARGS` so that they can be put after any that
  747. // come from `INCLUDE-FILE`.
  748. llvm::SmallVector<std::string> main_extra_args;
  749. // Process the main file.
  750. CARBON_RETURN_IF_ERROR(ProcessFileContent(
  751. test_name, test_file.input_content, running_autoupdate, &test_file,
  752. &found_autoupdate, test_file.test_args, main_extra_args,
  753. test_file.file_splits, include_files));
  754. if (!found_autoupdate) {
  755. return ErrorBuilder() << "Missing AUTOUPDATE/NOAUTOUPDATE setting";
  756. }
  757. constexpr llvm::StringLiteral AutoupdateSplit = "AUTOUPDATE-SPLIT";
  758. // Validate AUTOUPDATE-SPLIT use, and remove it from test files if present.
  759. if (test_file.has_splits) {
  760. for (const auto& test_file :
  761. llvm::ArrayRef(test_file.file_splits).drop_back()) {
  762. if (test_file.filename == AutoupdateSplit) {
  763. return Error("AUTOUPDATE-SPLIT must be the last split");
  764. }
  765. }
  766. if (test_file.file_splits.back().filename == AutoupdateSplit) {
  767. if (!test_file.autoupdate_line_number) {
  768. return Error("AUTOUPDATE-SPLIT requires AUTOUPDATE");
  769. }
  770. test_file.autoupdate_split = true;
  771. test_file.file_splits.pop_back();
  772. }
  773. }
  774. // Assume there is always a suffix `\n` in output.
  775. if (!test_file.expected_stdout.empty()) {
  776. test_file.expected_stdout.push_back(StrEq(""));
  777. }
  778. if (!test_file.expected_stderr.empty()) {
  779. test_file.expected_stderr.push_back(StrEq(""));
  780. }
  781. // Process includes. This can add entries to `include_files`.
  782. Set<std::string> processed_includes;
  783. for (size_t i = 0; i < include_files.size(); ++i) {
  784. const auto& filename = include_files[i];
  785. if (!processed_includes.Insert(filename).is_inserted()) {
  786. // Ignore repeated includes, mainly so that included files can include the
  787. // same file (i.e., repeated indirectly).
  788. continue;
  789. }
  790. CARBON_ASSIGN_OR_RETURN(std::string content, ReadFile(filename));
  791. // Note autoupdate never touches included files.
  792. CARBON_RETURN_IF_ERROR(ProcessFileContent(
  793. filename, content, /*running_autoupdate=*/false,
  794. /*test_file=*/nullptr,
  795. /*found_autoupdate=*/nullptr, test_file.test_args, test_file.extra_args,
  796. test_file.include_file_splits, include_files));
  797. }
  798. for (const auto& split : test_file.include_file_splits) {
  799. if (split.filename == AutoupdateSplit) {
  800. return Error("AUTOUPDATE-SPLIT is disallowed in included files");
  801. }
  802. }
  803. // Copy over `EXTRA-ARGS` from the main file (after includes).
  804. test_file.extra_args.append(main_extra_args);
  805. return std::move(test_file);
  806. }
  807. } // namespace Carbon::Testing