file_test_base.cpp 33 KB

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