file_test_base.cpp 31 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899
  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/file_test_base.h"
  5. #include <gmock/gmock.h>
  6. #include <filesystem>
  7. #include <fstream>
  8. #include <optional>
  9. #include <string>
  10. #include <utility>
  11. #include "absl/flags/flag.h"
  12. #include "absl/flags/parse.h"
  13. #include "common/check.h"
  14. #include "common/error.h"
  15. #include "common/init_llvm.h"
  16. #include "llvm/ADT/StringExtras.h"
  17. #include "llvm/ADT/Twine.h"
  18. #include "llvm/Support/FormatVariadic.h"
  19. #include "llvm/Support/MemoryBuffer.h"
  20. #include "llvm/Support/PrettyStackTrace.h"
  21. #include "llvm/Support/Process.h"
  22. #include "llvm/Support/ThreadPool.h"
  23. #include "testing/file_test/autoupdate.h"
  24. ABSL_FLAG(std::vector<std::string>, file_tests, {},
  25. "A comma-separated list of repo-relative names of test files. "
  26. "Overrides test_targets_file.");
  27. ABSL_FLAG(std::string, test_targets_file, "",
  28. "A path to a file containing repo-relative names of test files.");
  29. ABSL_FLAG(bool, autoupdate, false,
  30. "Instead of verifying files match test output, autoupdate files "
  31. "based on test output.");
  32. ABSL_FLAG(unsigned int, threads, 0,
  33. "Number of threads to use when autoupdating tests, or 0 to "
  34. "automatically determine a thread count.");
  35. ABSL_FLAG(bool, dump_output, false,
  36. "Instead of verifying files match test output, directly dump output "
  37. "to stderr.");
  38. namespace Carbon::Testing {
  39. using ::testing::Matcher;
  40. using ::testing::MatchesRegex;
  41. using ::testing::StrEq;
  42. // Reads a file to string.
  43. static auto ReadFile(std::string_view path) -> std::string {
  44. std::ifstream proto_file{std::string(path)};
  45. std::stringstream buffer;
  46. buffer << proto_file.rdbuf();
  47. proto_file.close();
  48. return buffer.str();
  49. }
  50. // Splits outputs to string_view because gtest handles string_view by default.
  51. static auto SplitOutput(llvm::StringRef output)
  52. -> llvm::SmallVector<std::string_view> {
  53. if (output.empty()) {
  54. return {};
  55. }
  56. llvm::SmallVector<llvm::StringRef> lines;
  57. llvm::StringRef(output).split(lines, "\n");
  58. return llvm::SmallVector<std::string_view>(lines.begin(), lines.end());
  59. }
  60. // Verify that the success and `fail_` prefix use correspond. Separately handle
  61. // both cases for clearer test failures.
  62. static auto CompareFailPrefix(llvm::StringRef filename, bool success) -> void {
  63. if (success) {
  64. EXPECT_FALSE(filename.starts_with("fail_"))
  65. << "`" << filename
  66. << "` succeeded; if success is expected, remove the `fail_` "
  67. "prefix.";
  68. } else {
  69. EXPECT_TRUE(filename.starts_with("fail_"))
  70. << "`" << filename
  71. << "` failed; if failure is expected, add the `fail_` prefix.";
  72. }
  73. }
  74. // Runs a test and compares output. This keeps output split by line so that
  75. // issues are a little easier to identify by the different line.
  76. auto FileTestBase::TestBody() -> void {
  77. std::string test_command;
  78. std::optional<llvm::PrettyStackTraceString> stack_trace_entry;
  79. // If we're being run from bazel, provide some assistance for understanding
  80. // and reproducing failures.
  81. const char* target = getenv("TEST_TARGET");
  82. if (target) {
  83. constexpr const char* CommandFormat =
  84. "bazel {0} {1} --test_arg=--file_tests={2}";
  85. test_command = llvm::formatv(CommandFormat, "test", target, test_name_);
  86. // Add a crash trace entry with the run command.
  87. stack_trace_entry.emplace(test_command.c_str());
  88. // This advice overrides the --file_tests flag provided by the file_test
  89. // rule.
  90. llvm::errs() << "\nTo test this file alone, run:\n " << test_command
  91. << "\n\nTo view output, run:\n "
  92. << llvm::formatv(CommandFormat, "run", target, test_name_)
  93. << " --test_arg=--dump_output\n\n";
  94. }
  95. TestContext context;
  96. auto run_result = ProcessTestFileAndRun(context);
  97. ASSERT_TRUE(run_result.ok()) << run_result.error();
  98. ValidateRun();
  99. auto test_filename = std::filesystem::path(test_name_.str()).filename();
  100. // Check success/failure against `fail_` prefixes.
  101. if (context.run_result.per_file_success.empty()) {
  102. CompareFailPrefix(test_filename.string(), context.run_result.success);
  103. } else {
  104. bool require_overall_failure = false;
  105. for (const auto& [filename, success] :
  106. context.run_result.per_file_success) {
  107. CompareFailPrefix(filename, success);
  108. if (!success) {
  109. require_overall_failure = true;
  110. }
  111. }
  112. if (require_overall_failure) {
  113. EXPECT_FALSE(context.run_result.success)
  114. << "There is a per-file failure expectation, so the overall result "
  115. "should have been a failure.";
  116. } else {
  117. // Individual files all succeeded, so the prefix is enforced on the main
  118. // test file.
  119. CompareFailPrefix(test_filename.string(), context.run_result.success);
  120. }
  121. }
  122. // Check results. Include a reminder of the autoupdate command for any
  123. // stdout/stderr differences.
  124. std::string update_message;
  125. if (target && context.autoupdate_line_number) {
  126. update_message = llvm::formatv(
  127. "If these differences are expected, try the autoupdater:\n"
  128. "\tbazel run {0} -- --autoupdate --file_tests={1}",
  129. target, test_name_);
  130. } else {
  131. update_message =
  132. "If these differences are expected, content must be updated manually.";
  133. }
  134. SCOPED_TRACE(update_message);
  135. if (context.check_subset) {
  136. EXPECT_THAT(SplitOutput(context.stdout),
  137. IsSupersetOf(context.expected_stdout));
  138. EXPECT_THAT(SplitOutput(context.stderr),
  139. IsSupersetOf(context.expected_stderr));
  140. } else {
  141. EXPECT_THAT(SplitOutput(context.stdout),
  142. ElementsAreArray(context.expected_stdout));
  143. EXPECT_THAT(SplitOutput(context.stderr),
  144. ElementsAreArray(context.expected_stderr));
  145. }
  146. // If there are no other test failures, check if autoupdate would make
  147. // changes. We don't do this when there _are_ failures because the
  148. // SCOPED_TRACE already contains the autoupdate reminder.
  149. if (!HasFailure() && RunAutoupdater(context, /*dry_run=*/true)) {
  150. ADD_FAILURE() << "Autoupdate would make changes to the file content.";
  151. }
  152. }
  153. auto FileTestBase::RunAutoupdater(const TestContext& context, bool dry_run)
  154. -> bool {
  155. if (!context.autoupdate_line_number) {
  156. return false;
  157. }
  158. llvm::SmallVector<llvm::StringRef> filenames;
  159. filenames.reserve(context.non_check_lines.size());
  160. if (context.has_splits) {
  161. // There are splits, so we provide an empty name for the first file.
  162. filenames.push_back({});
  163. }
  164. for (const auto& file : context.test_files) {
  165. filenames.push_back(file.filename);
  166. }
  167. llvm::ArrayRef expected_filenames = filenames;
  168. if (filenames.size() > 1) {
  169. expected_filenames = expected_filenames.drop_front();
  170. }
  171. return FileTestAutoupdater(
  172. std::filesystem::absolute(test_name_.str()), context.input_content,
  173. filenames, *context.autoupdate_line_number,
  174. context.non_check_lines, context.stdout, context.stderr,
  175. GetDefaultFileRE(expected_filenames),
  176. GetLineNumberReplacements(expected_filenames),
  177. [&](std::string& line) { DoExtraCheckReplacements(line); })
  178. .Run(dry_run);
  179. }
  180. auto FileTestBase::Autoupdate() -> ErrorOr<bool> {
  181. // Add a crash trace entry mentioning which file we're updating.
  182. std::string stack_trace_string =
  183. llvm::formatv("performing autoupdate for {0}", test_name_);
  184. llvm::PrettyStackTraceString stack_trace_entry(stack_trace_string.c_str());
  185. TestContext context;
  186. auto run_result = ProcessTestFileAndRun(context);
  187. if (!run_result.ok()) {
  188. return ErrorBuilder() << "Error updating " << test_name_ << ": "
  189. << run_result.error();
  190. }
  191. return RunAutoupdater(context, /*dry_run=*/false);
  192. }
  193. auto FileTestBase::DumpOutput() -> ErrorOr<Success> {
  194. TestContext context;
  195. context.capture_output = false;
  196. std::string banner(79, '=');
  197. banner.append("\n");
  198. llvm::errs() << banner << "= " << test_name_ << "\n";
  199. auto run_result = ProcessTestFileAndRun(context);
  200. if (!run_result.ok()) {
  201. return ErrorBuilder() << "Error updating " << test_name_ << ": "
  202. << run_result.error();
  203. }
  204. llvm::errs() << banner << context.stdout << banner << "= Exit with success: "
  205. << (context.run_result.success ? "true" : "false") << "\n"
  206. << banner;
  207. return Success();
  208. }
  209. auto FileTestBase::GetLineNumberReplacements(
  210. llvm::ArrayRef<llvm::StringRef> filenames)
  211. -> llvm::SmallVector<LineNumberReplacement> {
  212. return {{.has_file = true,
  213. .re = std::make_shared<RE2>(
  214. llvm::formatv(R"(({0}):(\d+))", llvm::join(filenames, "|"))),
  215. .line_formatv = R"({0})"}};
  216. }
  217. auto FileTestBase::ProcessTestFileAndRun(TestContext& context)
  218. -> ErrorOr<Success> {
  219. // Store the file so that test_files can use references to content.
  220. context.input_content = ReadFile(test_name_);
  221. // Load expected output.
  222. CARBON_RETURN_IF_ERROR(ProcessTestFile(context));
  223. // Process arguments.
  224. if (context.test_args.empty()) {
  225. context.test_args = GetDefaultArgs();
  226. }
  227. CARBON_RETURN_IF_ERROR(
  228. DoArgReplacements(context.test_args, context.test_files));
  229. // Create the files in-memory.
  230. llvm::vfs::InMemoryFileSystem fs;
  231. for (const auto& test_file : context.test_files) {
  232. if (!fs.addFile(test_file.filename, /*ModificationTime=*/0,
  233. llvm::MemoryBuffer::getMemBuffer(
  234. test_file.content, test_file.filename,
  235. /*RequiresNullTerminator=*/false))) {
  236. return ErrorBuilder() << "File is repeated: " << test_file.filename;
  237. }
  238. }
  239. // Convert the arguments to StringRef and const char* to match the
  240. // expectations of PrettyStackTraceProgram and Run.
  241. llvm::SmallVector<llvm::StringRef> test_args_ref;
  242. llvm::SmallVector<const char*> test_argv_for_stack_trace;
  243. test_args_ref.reserve(context.test_args.size());
  244. test_argv_for_stack_trace.reserve(context.test_args.size() + 1);
  245. for (const auto& arg : context.test_args) {
  246. test_args_ref.push_back(arg);
  247. test_argv_for_stack_trace.push_back(arg.c_str());
  248. }
  249. // Add a trailing null so that this is a proper argv.
  250. test_argv_for_stack_trace.push_back(nullptr);
  251. // Add a stack trace entry for the test invocation.
  252. llvm::PrettyStackTraceProgram stack_trace_entry(
  253. test_argv_for_stack_trace.size() - 1, test_argv_for_stack_trace.data());
  254. // Prepare string streams to capture output. In order to address casting
  255. // constraints, we split calls to Run as a ternary based on whether we want to
  256. // capture output.
  257. llvm::raw_svector_ostream stdout(context.stdout);
  258. llvm::raw_svector_ostream stderr(context.stderr);
  259. CARBON_ASSIGN_OR_RETURN(
  260. context.run_result,
  261. context.capture_output
  262. ? Run(test_args_ref, fs, stdout, stderr)
  263. : Run(test_args_ref, fs, llvm::outs(), llvm::errs()));
  264. return Success();
  265. }
  266. auto FileTestBase::DoArgReplacements(
  267. llvm::SmallVector<std::string>& test_args,
  268. const llvm::SmallVector<TestFile>& test_files) -> ErrorOr<Success> {
  269. for (auto* it = test_args.begin(); it != test_args.end(); ++it) {
  270. auto percent = it->find("%");
  271. if (percent == std::string::npos) {
  272. continue;
  273. }
  274. if (percent + 1 >= it->size()) {
  275. return ErrorBuilder() << "% is not allowed on its own: " << *it;
  276. }
  277. char c = (*it)[percent + 1];
  278. switch (c) {
  279. case 's': {
  280. if (*it != "%s") {
  281. return ErrorBuilder() << "%s must be the full argument: " << *it;
  282. }
  283. it = test_args.erase(it);
  284. for (const auto& file : test_files) {
  285. it = test_args.insert(it, file.filename);
  286. ++it;
  287. }
  288. // Back up once because the for loop will advance.
  289. --it;
  290. break;
  291. }
  292. case 't': {
  293. char* tmpdir = getenv("TEST_TMPDIR");
  294. CARBON_CHECK(tmpdir != nullptr);
  295. it->replace(percent, 2, llvm::formatv("{0}/temp_file", tmpdir));
  296. break;
  297. }
  298. default:
  299. return ErrorBuilder() << "%" << c << " is not supported: " << *it;
  300. }
  301. }
  302. return Success();
  303. }
  304. // Processes conflict markers, including tracking of whether code is within a
  305. // conflict marker. Returns true if the line is consumed.
  306. static auto TryConsumeConflictMarker(llvm::StringRef line,
  307. llvm::StringRef line_trimmed,
  308. bool* inside_conflict_marker)
  309. -> ErrorOr<bool> {
  310. bool is_start = line.starts_with("<<<<<<<");
  311. bool is_middle = line.starts_with("=======") || line.starts_with("|||||||");
  312. bool is_end = line.starts_with(">>>>>>>");
  313. // When running the test, any conflict marker is an error.
  314. if (!absl::GetFlag(FLAGS_autoupdate) && (is_start || is_middle || is_end)) {
  315. return ErrorBuilder() << "Conflict marker found:\n" << line;
  316. }
  317. // Autoupdate tracks conflict markers for context, and will discard
  318. // conflicting lines when it can autoupdate them.
  319. if (*inside_conflict_marker) {
  320. if (is_start) {
  321. return ErrorBuilder() << "Unexpected conflict marker inside conflict:\n"
  322. << line;
  323. }
  324. if (is_middle) {
  325. return true;
  326. }
  327. if (is_end) {
  328. *inside_conflict_marker = false;
  329. return true;
  330. }
  331. // Look for CHECK lines, which can be discarded.
  332. if (line_trimmed.starts_with("// CHECK:STDOUT:") ||
  333. line_trimmed.starts_with("// CHECK:STDERR:")) {
  334. return true;
  335. }
  336. return ErrorBuilder()
  337. << "Autoupdate can't discard non-CHECK lines inside conflicts:\n"
  338. << line;
  339. } else {
  340. if (is_start) {
  341. *inside_conflict_marker = true;
  342. return true;
  343. }
  344. if (is_middle || is_end) {
  345. return ErrorBuilder() << "Unexpected conflict marker outside conflict:\n"
  346. << line;
  347. }
  348. return false;
  349. }
  350. }
  351. // State for file splitting logic: TryConsumeSplit and FinishSplit.
  352. struct SplitState {
  353. auto has_splits() const -> bool { return file_index > 0; }
  354. auto add_content(llvm::StringRef line) -> void {
  355. content.append(line);
  356. content.append("\n");
  357. }
  358. // Whether content has been found. Only updated before a file split is found
  359. // (which may be never).
  360. bool found_code_pre_split = false;
  361. // The current file name, considering splits. Empty for the default file.
  362. llvm::StringRef filename = "";
  363. // The accumulated content for the file being built. This may elide some of
  364. // the original content, such as conflict markers.
  365. std::string content;
  366. // The current file index.
  367. int file_index = 0;
  368. };
  369. // Adds a file. Used for both split and unsplit test files.
  370. static auto AddTestFile(llvm::StringRef filename, std::string* content,
  371. llvm::SmallVector<FileTestBase::TestFile>* test_files)
  372. -> void {
  373. test_files->push_back(
  374. {.filename = filename.str(), .content = std::move(*content)});
  375. content->clear();
  376. }
  377. // Process file split ("---") lines when found. Returns true if the line is
  378. // consumed.
  379. static auto TryConsumeSplit(
  380. llvm::StringRef line, llvm::StringRef line_trimmed, bool found_autoupdate,
  381. int* line_index, SplitState* split,
  382. llvm::SmallVector<FileTestBase::TestFile>* test_files,
  383. llvm::SmallVector<FileTestLine>* non_check_lines) -> ErrorOr<bool> {
  384. if (!line_trimmed.consume_front("// ---")) {
  385. if (!split->has_splits() && !line_trimmed.starts_with("//") &&
  386. !line_trimmed.empty()) {
  387. split->found_code_pre_split = true;
  388. }
  389. // Add the line to the current file's content (which may not be a split
  390. // file).
  391. split->add_content(line);
  392. return false;
  393. }
  394. if (!found_autoupdate) {
  395. // If there's a split, all output is appended at the end of each file
  396. // before AUTOUPDATE. We may want to change that, but it's not
  397. // necessary to handle right now.
  398. return ErrorBuilder() << "AUTOUPDATE/NOAUTOUPDATE setting must be in "
  399. "the first file.";
  400. }
  401. // On a file split, add the previous file, then start a new one.
  402. if (split->has_splits()) {
  403. AddTestFile(split->filename, &split->content, test_files);
  404. } else {
  405. split->content.clear();
  406. if (split->found_code_pre_split) {
  407. // For the first split, we make sure there was no content prior.
  408. return ErrorBuilder() << "When using split files, there must be no "
  409. "content before the first split file.";
  410. }
  411. }
  412. ++split->file_index;
  413. split->filename = line_trimmed.trim();
  414. if (split->filename.empty()) {
  415. return ErrorBuilder() << "Missing filename for split.";
  416. }
  417. // The split line is added to non_check_lines for retention in autoupdate, but
  418. // is not added to the test file content.
  419. *line_index = 0;
  420. non_check_lines->push_back(
  421. FileTestLine(split->file_index, *line_index, line));
  422. return true;
  423. }
  424. // Converts a `FileCheck`-style expectation string into a single complete regex
  425. // string by escaping all regex characters outside of the designated `{{...}}`
  426. // regex sequences, and switching those to a normal regex sub-pattern syntax.
  427. static void ConvertExpectationStringToRegex(std::string& str) {
  428. for (int pos = 0; pos < static_cast<int>(str.size());) {
  429. switch (str[pos]) {
  430. case '(':
  431. case ')':
  432. case '[':
  433. case ']':
  434. case '}':
  435. case '.':
  436. case '^':
  437. case '$':
  438. case '*':
  439. case '+':
  440. case '?':
  441. case '|':
  442. case '\\': {
  443. // Escape regex characters.
  444. str.insert(pos, "\\");
  445. pos += 2;
  446. break;
  447. }
  448. case '{': {
  449. if (pos + 1 == static_cast<int>(str.size()) || str[pos + 1] != '{') {
  450. // Single `{`, escape it.
  451. str.insert(pos, "\\");
  452. pos += 2;
  453. break;
  454. }
  455. // Replace the `{{...}}` regex syntax with standard `(...)` syntax.
  456. str.replace(pos, 2, "(");
  457. for (++pos; pos < static_cast<int>(str.size() - 1); ++pos) {
  458. if (str[pos] == '}' && str[pos + 1] == '}') {
  459. str.replace(pos, 2, ")");
  460. ++pos;
  461. break;
  462. }
  463. }
  464. break;
  465. }
  466. default: {
  467. ++pos;
  468. }
  469. }
  470. }
  471. }
  472. // Transforms an expectation on a given line from `FileCheck` syntax into a
  473. // standard regex matcher.
  474. static auto TransformExpectation(int line_index, llvm::StringRef in)
  475. -> ErrorOr<Matcher<std::string>> {
  476. if (in.empty()) {
  477. return Matcher<std::string>{StrEq("")};
  478. }
  479. if (!in.consume_front(" ")) {
  480. return ErrorBuilder() << "Malformated CHECK line: " << in;
  481. }
  482. // Check early if we have a regex component as we can avoid building an
  483. // expensive matcher when not using those.
  484. bool has_regex = in.find("{{") != llvm::StringRef::npos;
  485. // Now scan the string and expand any keywords. Note that this needs to be
  486. // `size_t` to correctly store `npos`.
  487. size_t keyword_pos = in.find("[[");
  488. // If there are neither keywords nor regex sequences, we can match the
  489. // incoming string directly.
  490. if (!has_regex && keyword_pos == llvm::StringRef::npos) {
  491. return Matcher<std::string>{StrEq(in)};
  492. }
  493. std::string str = in.str();
  494. // First expand the keywords.
  495. while (keyword_pos != std::string::npos) {
  496. llvm::StringRef line_keyword_cursor =
  497. llvm::StringRef(str).substr(keyword_pos);
  498. CARBON_CHECK(line_keyword_cursor.consume_front("[["));
  499. static constexpr llvm::StringLiteral LineKeyword = "@LINE";
  500. if (!line_keyword_cursor.consume_front(LineKeyword)) {
  501. return ErrorBuilder()
  502. << "Unexpected [[, should be {{\\[\\[}} at `"
  503. << line_keyword_cursor.substr(0, 5) << "` in: " << in;
  504. }
  505. // Allow + or - here; consumeInteger handles -.
  506. line_keyword_cursor.consume_front("+");
  507. int offset;
  508. // consumeInteger returns true for errors, not false.
  509. if (line_keyword_cursor.consumeInteger(10, offset) ||
  510. !line_keyword_cursor.consume_front("]]")) {
  511. return ErrorBuilder()
  512. << "Unexpected @LINE offset at `"
  513. << line_keyword_cursor.substr(0, 5) << "` in: " << in;
  514. }
  515. std::string int_str = llvm::Twine(line_index + offset).str();
  516. int remove_len = (line_keyword_cursor.data() - str.data()) - keyword_pos;
  517. str.replace(keyword_pos, remove_len, int_str);
  518. keyword_pos += int_str.size();
  519. // Find the next keyword start or the end of the string.
  520. keyword_pos = str.find("[[", keyword_pos);
  521. }
  522. // If there was no regex, we can directly match the adjusted string.
  523. if (!has_regex) {
  524. return Matcher<std::string>{StrEq(str)};
  525. }
  526. // Otherwise, we need to turn the entire string into a regex by escaping
  527. // things outside the regex region and transforming the regex region into a
  528. // normal syntax.
  529. ConvertExpectationStringToRegex(str);
  530. return Matcher<std::string>{MatchesRegex(str)};
  531. }
  532. // Once all content is processed, do any remaining split processing.
  533. static auto FinishSplit(llvm::StringRef test_name, SplitState* split,
  534. llvm::SmallVector<FileTestBase::TestFile>* test_files)
  535. -> void {
  536. if (split->has_splits()) {
  537. AddTestFile(split->filename, &split->content, test_files);
  538. } else {
  539. // If no file splitting happened, use the main file as the test file.
  540. // There will always be a `/` unless tests are in the repo root.
  541. AddTestFile(test_name.drop_front(test_name.rfind("/") + 1), &split->content,
  542. test_files);
  543. }
  544. }
  545. // Process CHECK lines when found. Returns true if the line is consumed.
  546. static auto TryConsumeCheck(
  547. int line_index, llvm::StringRef line, llvm::StringRef line_trimmed,
  548. llvm::SmallVector<testing::Matcher<std::string>>* expected_stdout,
  549. llvm::SmallVector<testing::Matcher<std::string>>* expected_stderr)
  550. -> ErrorOr<bool> {
  551. if (!line_trimmed.consume_front("// CHECK")) {
  552. return false;
  553. }
  554. // Don't build expectations when doing an autoupdate. We don't want to
  555. // break the autoupdate on an invalid CHECK line.
  556. if (!absl::GetFlag(FLAGS_autoupdate)) {
  557. llvm::SmallVector<Matcher<std::string>>* expected;
  558. if (line_trimmed.consume_front(":STDOUT:")) {
  559. expected = expected_stdout;
  560. } else if (line_trimmed.consume_front(":STDERR:")) {
  561. expected = expected_stderr;
  562. } else {
  563. return ErrorBuilder() << "Unexpected CHECK in input: " << line.str();
  564. }
  565. CARBON_ASSIGN_OR_RETURN(Matcher<std::string> check_matcher,
  566. TransformExpectation(line_index, line_trimmed));
  567. expected->push_back(check_matcher);
  568. }
  569. return true;
  570. }
  571. // Processes ARGS lines when found. Returns true if the line is consumed.
  572. static auto TryConsumeArgs(llvm::StringRef line, llvm::StringRef line_trimmed,
  573. llvm::SmallVector<std::string>* args)
  574. -> ErrorOr<bool> {
  575. if (!line_trimmed.consume_front("// ARGS: ")) {
  576. return false;
  577. }
  578. if (!args->empty()) {
  579. return ErrorBuilder() << "ARGS was specified multiple times: "
  580. << line.str();
  581. }
  582. // Split the line into arguments.
  583. std::pair<llvm::StringRef, llvm::StringRef> cursor =
  584. llvm::getToken(line_trimmed);
  585. while (!cursor.first.empty()) {
  586. args->push_back(std::string(cursor.first));
  587. cursor = llvm::getToken(cursor.second);
  588. }
  589. return true;
  590. }
  591. // Processes AUTOUPDATE lines when found. Returns true if the line is consumed.
  592. static auto TryConsumeAutoupdate(int line_index, llvm::StringRef line_trimmed,
  593. bool* found_autoupdate,
  594. std::optional<int>* autoupdate_line_number)
  595. -> ErrorOr<bool> {
  596. static constexpr llvm::StringLiteral Autoupdate = "// AUTOUPDATE";
  597. static constexpr llvm::StringLiteral NoAutoupdate = "// NOAUTOUPDATE";
  598. if (line_trimmed != Autoupdate && line_trimmed != NoAutoupdate) {
  599. return false;
  600. }
  601. if (*found_autoupdate) {
  602. return ErrorBuilder() << "Multiple AUTOUPDATE/NOAUTOUPDATE settings found";
  603. }
  604. *found_autoupdate = true;
  605. if (line_trimmed == Autoupdate) {
  606. *autoupdate_line_number = line_index;
  607. }
  608. return true;
  609. }
  610. // Processes SET-CHECK-SUBSET lines when found. Returns true if the line is
  611. // consumed.
  612. static auto TryConsumeSetCheckSubset(llvm::StringRef line_trimmed,
  613. bool* check_subset) -> ErrorOr<bool> {
  614. if (line_trimmed != "// SET-CHECK-SUBSET") {
  615. return false;
  616. }
  617. if (*check_subset) {
  618. return ErrorBuilder() << "SET-CHECK-SUBSET was specified multiple times";
  619. }
  620. *check_subset = true;
  621. return true;
  622. }
  623. auto FileTestBase::ProcessTestFile(TestContext& context) -> ErrorOr<Success> {
  624. // Original file content, and a cursor for walking through it.
  625. llvm::StringRef file_content = context.input_content;
  626. llvm::StringRef cursor = file_content;
  627. // Whether either AUTOUDPATE or NOAUTOUPDATE was found.
  628. bool found_autoupdate = false;
  629. // The index in the current test file. Will be reset on splits.
  630. int line_index = 0;
  631. SplitState split;
  632. // When autoupdating, we track whether we're inside conflict markers.
  633. // Otherwise conflict markers are errors.
  634. bool inside_conflict_marker = false;
  635. while (!cursor.empty()) {
  636. auto [line, next_cursor] = cursor.split("\n");
  637. cursor = next_cursor;
  638. auto line_trimmed = line.ltrim();
  639. bool is_consumed = false;
  640. CARBON_ASSIGN_OR_RETURN(
  641. is_consumed,
  642. TryConsumeConflictMarker(line, line_trimmed, &inside_conflict_marker));
  643. if (is_consumed) {
  644. continue;
  645. }
  646. // At this point, remaining lines are part of the test input.
  647. CARBON_ASSIGN_OR_RETURN(
  648. is_consumed,
  649. TryConsumeSplit(line, line_trimmed, found_autoupdate, &line_index,
  650. &split, &context.test_files, &context.non_check_lines));
  651. if (is_consumed) {
  652. continue;
  653. }
  654. ++line_index;
  655. CARBON_ASSIGN_OR_RETURN(
  656. is_consumed,
  657. TryConsumeCheck(line_index, line, line_trimmed,
  658. &context.expected_stdout, &context.expected_stderr));
  659. if (is_consumed) {
  660. continue;
  661. }
  662. // At this point, lines are retained as non-CHECK lines.
  663. context.non_check_lines.push_back(
  664. FileTestLine(split.file_index, line_index, line));
  665. CARBON_ASSIGN_OR_RETURN(
  666. is_consumed, TryConsumeArgs(line, line_trimmed, &context.test_args));
  667. if (is_consumed) {
  668. continue;
  669. }
  670. CARBON_ASSIGN_OR_RETURN(
  671. is_consumed,
  672. TryConsumeAutoupdate(line_index, line_trimmed, &found_autoupdate,
  673. &context.autoupdate_line_number));
  674. if (is_consumed) {
  675. continue;
  676. }
  677. CARBON_ASSIGN_OR_RETURN(
  678. is_consumed,
  679. TryConsumeSetCheckSubset(line_trimmed, &context.check_subset));
  680. if (is_consumed) {
  681. continue;
  682. }
  683. }
  684. if (!found_autoupdate) {
  685. return ErrorBuilder() << "Missing AUTOUPDATE/NOAUTOUPDATE setting";
  686. }
  687. context.has_splits = split.has_splits();
  688. FinishSplit(test_name_, &split, &context.test_files);
  689. // Assume there is always a suffix `\n` in output.
  690. if (!context.expected_stdout.empty()) {
  691. context.expected_stdout.push_back(StrEq(""));
  692. }
  693. if (!context.expected_stderr.empty()) {
  694. context.expected_stderr.push_back(StrEq(""));
  695. }
  696. return Success();
  697. }
  698. // Returns the tests to run.
  699. static auto GetTests() -> llvm::SmallVector<std::string> {
  700. // Prefer a user-specified list if present.
  701. auto specific_tests = absl::GetFlag(FLAGS_file_tests);
  702. if (!specific_tests.empty()) {
  703. return llvm::SmallVector<std::string>(specific_tests.begin(),
  704. specific_tests.end());
  705. }
  706. // Extracts tests from the target file.
  707. CARBON_CHECK(!absl::GetFlag(FLAGS_test_targets_file).empty())
  708. << "Missing --test_targets_file.";
  709. auto content = ReadFile(absl::GetFlag(FLAGS_test_targets_file));
  710. llvm::SmallVector<std::string> all_tests;
  711. for (llvm::StringRef file_ref : llvm::split(content, "\n")) {
  712. if (file_ref.empty()) {
  713. continue;
  714. }
  715. all_tests.push_back(file_ref.str());
  716. }
  717. return all_tests;
  718. }
  719. // Implements main() within the Carbon::Testing namespace for convenience.
  720. static auto Main(int argc, char** argv) -> int {
  721. Carbon::InitLLVM init_llvm(argc, argv);
  722. testing::InitGoogleTest(&argc, argv);
  723. auto args = absl::ParseCommandLine(argc, argv);
  724. if (args.size() > 1) {
  725. llvm::errs() << "Unexpected arguments:";
  726. for (char* arg : llvm::ArrayRef(args).drop_front()) {
  727. llvm::errs() << " ";
  728. llvm::errs().write_escaped(arg);
  729. }
  730. llvm::errs() << "\n";
  731. return EXIT_FAILURE;
  732. }
  733. // Tests might try to read from stdin. Ensure those reads fail by closing
  734. // stdin and reopening it as /dev/null. Note that STDIN_FILENO doesn't exist
  735. // on Windows, but POSIX requires it to be 0.
  736. if (std::error_code error =
  737. llvm::sys::Process::SafelyCloseFileDescriptor(0)) {
  738. llvm::errs() << "Unable to close standard input: " << error.message()
  739. << "\n";
  740. return EXIT_FAILURE;
  741. }
  742. if (std::error_code error =
  743. llvm::sys::Process::FixupStandardFileDescriptors()) {
  744. llvm::errs() << "Unable to correct standard file descriptors: "
  745. << error.message() << "\n";
  746. return EXIT_FAILURE;
  747. }
  748. if (absl::GetFlag(FLAGS_autoupdate) && absl::GetFlag(FLAGS_dump_output)) {
  749. llvm::errs() << "--autoupdate and --dump_output are mutually exclusive.\n";
  750. return EXIT_FAILURE;
  751. }
  752. llvm::SmallVector<std::string> tests = GetTests();
  753. auto test_factory = GetFileTestFactory();
  754. if (absl::GetFlag(FLAGS_autoupdate)) {
  755. llvm::DefaultThreadPool pool(
  756. {.ThreadsRequested = absl::GetFlag(FLAGS_threads)});
  757. std::mutex errs_mutex;
  758. for (const auto& test_name : tests) {
  759. pool.async([&test_factory, &errs_mutex, test_name] {
  760. std::unique_ptr<FileTestBase> test(test_factory.factory_fn(test_name));
  761. auto result = test->Autoupdate();
  762. // Guard access to llvm::errs, which is not thread-safe.
  763. std::unique_lock<std::mutex> lock(errs_mutex);
  764. if (result.ok()) {
  765. llvm::errs() << (*result ? "!" : ".");
  766. } else {
  767. llvm::errs() << "\n" << result.error().message() << "\n";
  768. }
  769. });
  770. }
  771. pool.wait();
  772. llvm::errs() << "\nDone!\n";
  773. return EXIT_SUCCESS;
  774. } else if (absl::GetFlag(FLAGS_dump_output)) {
  775. for (const auto& test_name : tests) {
  776. std::unique_ptr<FileTestBase> test(test_factory.factory_fn(test_name));
  777. auto result = test->DumpOutput();
  778. if (!result.ok()) {
  779. llvm::errs() << "\n" << result.error().message() << "\n";
  780. }
  781. }
  782. llvm::errs() << "\nDone!\n";
  783. return EXIT_SUCCESS;
  784. } else {
  785. for (llvm::StringRef test_name : tests) {
  786. testing::RegisterTest(test_factory.name, test_name.data(), nullptr,
  787. test_name.data(), __FILE__, __LINE__,
  788. [&test_factory, test_name = test_name]() {
  789. return test_factory.factory_fn(test_name);
  790. });
  791. }
  792. return RUN_ALL_TESTS();
  793. }
  794. }
  795. } // namespace Carbon::Testing
  796. auto main(int argc, char** argv) -> int {
  797. return Carbon::Testing::Main(argc, argv);
  798. }