file_test_base.cpp 35 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042
  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.str());
  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. // Replaces the content keywords.
  412. //
  413. // TEST_NAME is the only content keyword at present, but we do validate that
  414. // other names are reserved.
  415. static auto ReplaceContentKeywords(llvm::StringRef filename,
  416. std::string* content) -> ErrorOr<Success> {
  417. static constexpr llvm::StringLiteral Prefix = "[[@";
  418. auto keyword_pos = content->find(Prefix);
  419. // Return early if not finding anything.
  420. if (keyword_pos == std::string::npos) {
  421. return Success();
  422. }
  423. // Construct the test name by getting the base name without the extension,
  424. // then removing any "fail_" or "todo_" prefixes.
  425. llvm::StringRef test_name = filename;
  426. if (auto last_slash = test_name.rfind("/");
  427. last_slash != llvm::StringRef::npos) {
  428. test_name = test_name.substr(last_slash + 1);
  429. }
  430. if (auto ext_dot = test_name.find("."); ext_dot != llvm::StringRef::npos) {
  431. test_name = test_name.substr(0, ext_dot);
  432. }
  433. // Note this also handles `fail_todo_` and `todo_fail_`.
  434. test_name.consume_front("todo_");
  435. test_name.consume_front("fail_");
  436. test_name.consume_front("todo_");
  437. while (keyword_pos != std::string::npos) {
  438. static constexpr llvm::StringLiteral TestName = "[[@TEST_NAME]]";
  439. auto keyword = llvm::StringRef(*content).substr(keyword_pos);
  440. if (keyword.starts_with(TestName)) {
  441. content->replace(keyword_pos, TestName.size(), test_name);
  442. keyword_pos += test_name.size();
  443. } else if (keyword.starts_with("[[@LINE")) {
  444. // Just move past the prefix to find the next one.
  445. keyword_pos += Prefix.size();
  446. } else {
  447. return ErrorBuilder()
  448. << "Unexpected use of `[[@` at `" << keyword.substr(0, 5) << "`";
  449. }
  450. keyword_pos = content->find(Prefix, keyword_pos);
  451. }
  452. return Success();
  453. }
  454. // Adds a file. Used for both split and unsplit test files.
  455. static auto AddTestFile(llvm::StringRef filename, std::string* content,
  456. llvm::SmallVector<FileTestBase::TestFile>* test_files)
  457. -> ErrorOr<Success> {
  458. CARBON_RETURN_IF_ERROR(ReplaceContentKeywords(filename, content));
  459. test_files->push_back(
  460. {.filename = filename.str(), .content = std::move(*content)});
  461. content->clear();
  462. return Success();
  463. }
  464. // Process file split ("---") lines when found. Returns true if the line is
  465. // consumed.
  466. static auto TryConsumeSplit(
  467. llvm::StringRef line, llvm::StringRef line_trimmed, bool found_autoupdate,
  468. int* line_index, SplitState* split,
  469. llvm::SmallVector<FileTestBase::TestFile>* test_files,
  470. llvm::SmallVector<FileTestLine>* non_check_lines) -> ErrorOr<bool> {
  471. if (!line_trimmed.consume_front("// ---")) {
  472. if (!split->has_splits() && !line_trimmed.starts_with("//") &&
  473. !line_trimmed.empty()) {
  474. split->found_code_pre_split = true;
  475. }
  476. // Add the line to the current file's content (which may not be a split
  477. // file).
  478. split->add_content(line);
  479. return false;
  480. }
  481. if (!found_autoupdate) {
  482. // If there's a split, all output is appended at the end of each file
  483. // before AUTOUPDATE. We may want to change that, but it's not
  484. // necessary to handle right now.
  485. return ErrorBuilder() << "AUTOUPDATE/NOAUTOUPDATE setting must be in "
  486. "the first file.";
  487. }
  488. // On a file split, add the previous file, then start a new one.
  489. if (split->has_splits()) {
  490. CARBON_RETURN_IF_ERROR(
  491. AddTestFile(split->filename, &split->content, test_files));
  492. } else {
  493. split->content.clear();
  494. if (split->found_code_pre_split) {
  495. // For the first split, we make sure there was no content prior.
  496. return ErrorBuilder() << "When using split files, there must be no "
  497. "content before the first split file.";
  498. }
  499. }
  500. ++split->file_index;
  501. split->filename = line_trimmed.trim();
  502. if (split->filename.empty()) {
  503. return ErrorBuilder() << "Missing filename for split.";
  504. }
  505. // The split line is added to non_check_lines for retention in autoupdate, but
  506. // is not added to the test file content.
  507. *line_index = 0;
  508. non_check_lines->push_back(
  509. FileTestLine(split->file_index, *line_index, line));
  510. return true;
  511. }
  512. // Converts a `FileCheck`-style expectation string into a single complete regex
  513. // string by escaping all regex characters outside of the designated `{{...}}`
  514. // regex sequences, and switching those to a normal regex sub-pattern syntax.
  515. static void ConvertExpectationStringToRegex(std::string& str) {
  516. for (int pos = 0; pos < static_cast<int>(str.size());) {
  517. switch (str[pos]) {
  518. case '(':
  519. case ')':
  520. case '[':
  521. case ']':
  522. case '}':
  523. case '.':
  524. case '^':
  525. case '$':
  526. case '*':
  527. case '+':
  528. case '?':
  529. case '|':
  530. case '\\': {
  531. // Escape regex characters.
  532. str.insert(pos, "\\");
  533. pos += 2;
  534. break;
  535. }
  536. case '{': {
  537. if (pos + 1 == static_cast<int>(str.size()) || str[pos + 1] != '{') {
  538. // Single `{`, escape it.
  539. str.insert(pos, "\\");
  540. pos += 2;
  541. break;
  542. }
  543. // Replace the `{{...}}` regex syntax with standard `(...)` syntax.
  544. str.replace(pos, 2, "(");
  545. for (++pos; pos < static_cast<int>(str.size() - 1); ++pos) {
  546. if (str[pos] == '}' && str[pos + 1] == '}') {
  547. str.replace(pos, 2, ")");
  548. ++pos;
  549. break;
  550. }
  551. }
  552. break;
  553. }
  554. default: {
  555. ++pos;
  556. }
  557. }
  558. }
  559. }
  560. // Transforms an expectation on a given line from `FileCheck` syntax into a
  561. // standard regex matcher.
  562. static auto TransformExpectation(int line_index, llvm::StringRef in)
  563. -> ErrorOr<Matcher<std::string>> {
  564. if (in.empty()) {
  565. return Matcher<std::string>{StrEq("")};
  566. }
  567. if (!in.consume_front(" ")) {
  568. return ErrorBuilder() << "Malformated CHECK line: " << in;
  569. }
  570. // Check early if we have a regex component as we can avoid building an
  571. // expensive matcher when not using those.
  572. bool has_regex = in.find("{{") != llvm::StringRef::npos;
  573. // Now scan the string and expand any keywords. Note that this needs to be
  574. // `size_t` to correctly store `npos`.
  575. size_t keyword_pos = in.find("[[");
  576. // If there are neither keywords nor regex sequences, we can match the
  577. // incoming string directly.
  578. if (!has_regex && keyword_pos == llvm::StringRef::npos) {
  579. return Matcher<std::string>{StrEq(in)};
  580. }
  581. std::string str = in.str();
  582. // First expand the keywords.
  583. while (keyword_pos != std::string::npos) {
  584. llvm::StringRef line_keyword_cursor =
  585. llvm::StringRef(str).substr(keyword_pos);
  586. CARBON_CHECK(line_keyword_cursor.consume_front("[["));
  587. static constexpr llvm::StringLiteral LineKeyword = "@LINE";
  588. if (!line_keyword_cursor.consume_front(LineKeyword)) {
  589. return ErrorBuilder()
  590. << "Unexpected [[, should be {{\\[\\[}} at `"
  591. << line_keyword_cursor.substr(0, 5) << "` in: " << in;
  592. }
  593. // Allow + or - here; consumeInteger handles -.
  594. line_keyword_cursor.consume_front("+");
  595. int offset;
  596. // consumeInteger returns true for errors, not false.
  597. if (line_keyword_cursor.consumeInteger(10, offset) ||
  598. !line_keyword_cursor.consume_front("]]")) {
  599. return ErrorBuilder()
  600. << "Unexpected @LINE offset at `"
  601. << line_keyword_cursor.substr(0, 5) << "` in: " << in;
  602. }
  603. std::string int_str = llvm::Twine(line_index + offset).str();
  604. int remove_len = (line_keyword_cursor.data() - str.data()) - keyword_pos;
  605. str.replace(keyword_pos, remove_len, int_str);
  606. keyword_pos += int_str.size();
  607. // Find the next keyword start or the end of the string.
  608. keyword_pos = str.find("[[", keyword_pos);
  609. }
  610. // If there was no regex, we can directly match the adjusted string.
  611. if (!has_regex) {
  612. return Matcher<std::string>{StrEq(str)};
  613. }
  614. // Otherwise, we need to turn the entire string into a regex by escaping
  615. // things outside the regex region and transforming the regex region into a
  616. // normal syntax.
  617. ConvertExpectationStringToRegex(str);
  618. return Matcher<std::string>{MatchesRegex(str)};
  619. }
  620. // Once all content is processed, do any remaining split processing.
  621. static auto FinishSplit(llvm::StringRef test_name, SplitState* split,
  622. llvm::SmallVector<FileTestBase::TestFile>* test_files)
  623. -> ErrorOr<Success> {
  624. if (split->has_splits()) {
  625. return AddTestFile(split->filename, &split->content, test_files);
  626. } else {
  627. // If no file splitting happened, use the main file as the test file.
  628. // There will always be a `/` unless tests are in the repo root.
  629. return AddTestFile(test_name.drop_front(test_name.rfind("/") + 1),
  630. &split->content, test_files);
  631. }
  632. }
  633. // Process CHECK lines when found. Returns true if the line is consumed.
  634. static auto TryConsumeCheck(
  635. int line_index, llvm::StringRef line, llvm::StringRef line_trimmed,
  636. llvm::SmallVector<testing::Matcher<std::string>>* expected_stdout,
  637. llvm::SmallVector<testing::Matcher<std::string>>* expected_stderr)
  638. -> ErrorOr<bool> {
  639. if (!line_trimmed.consume_front("// CHECK")) {
  640. return false;
  641. }
  642. // Don't build expectations when doing an autoupdate. We don't want to
  643. // break the autoupdate on an invalid CHECK line.
  644. if (!absl::GetFlag(FLAGS_autoupdate)) {
  645. llvm::SmallVector<Matcher<std::string>>* expected;
  646. if (line_trimmed.consume_front(":STDOUT:")) {
  647. expected = expected_stdout;
  648. } else if (line_trimmed.consume_front(":STDERR:")) {
  649. expected = expected_stderr;
  650. } else {
  651. return ErrorBuilder() << "Unexpected CHECK in input: " << line.str();
  652. }
  653. CARBON_ASSIGN_OR_RETURN(Matcher<std::string> check_matcher,
  654. TransformExpectation(line_index, line_trimmed));
  655. expected->push_back(check_matcher);
  656. }
  657. return true;
  658. }
  659. // Processes ARGS lines when found. Returns true if the line is consumed.
  660. static auto TryConsumeArgs(llvm::StringRef line, llvm::StringRef line_trimmed,
  661. llvm::SmallVector<std::string>* args)
  662. -> ErrorOr<bool> {
  663. if (!line_trimmed.consume_front("// ARGS: ")) {
  664. return false;
  665. }
  666. if (!args->empty()) {
  667. return ErrorBuilder() << "ARGS was specified multiple times: "
  668. << line.str();
  669. }
  670. // Split the line into arguments.
  671. std::pair<llvm::StringRef, llvm::StringRef> cursor =
  672. llvm::getToken(line_trimmed);
  673. while (!cursor.first.empty()) {
  674. args->push_back(std::string(cursor.first));
  675. cursor = llvm::getToken(cursor.second);
  676. }
  677. return true;
  678. }
  679. // Processes AUTOUPDATE lines when found. Returns true if the line is consumed.
  680. static auto TryConsumeAutoupdate(int line_index, llvm::StringRef line_trimmed,
  681. bool* found_autoupdate,
  682. std::optional<int>* autoupdate_line_number)
  683. -> ErrorOr<bool> {
  684. static constexpr llvm::StringLiteral Autoupdate = "// AUTOUPDATE";
  685. static constexpr llvm::StringLiteral NoAutoupdate = "// NOAUTOUPDATE";
  686. if (line_trimmed != Autoupdate && line_trimmed != NoAutoupdate) {
  687. return false;
  688. }
  689. if (*found_autoupdate) {
  690. return ErrorBuilder() << "Multiple AUTOUPDATE/NOAUTOUPDATE settings found";
  691. }
  692. *found_autoupdate = true;
  693. if (line_trimmed == Autoupdate) {
  694. *autoupdate_line_number = line_index;
  695. }
  696. return true;
  697. }
  698. // Processes SET-CHECK-SUBSET lines when found. Returns true if the line is
  699. // consumed.
  700. static auto TryConsumeSetCheckSubset(llvm::StringRef line_trimmed,
  701. bool* check_subset) -> ErrorOr<bool> {
  702. if (line_trimmed != "// SET-CHECK-SUBSET") {
  703. return false;
  704. }
  705. if (*check_subset) {
  706. return ErrorBuilder() << "SET-CHECK-SUBSET was specified multiple times";
  707. }
  708. *check_subset = true;
  709. return true;
  710. }
  711. auto FileTestBase::ProcessTestFile(TestContext& context) -> ErrorOr<Success> {
  712. // Original file content, and a cursor for walking through it.
  713. llvm::StringRef file_content = context.input_content;
  714. llvm::StringRef cursor = file_content;
  715. // Whether either AUTOUDPATE or NOAUTOUPDATE was found.
  716. bool found_autoupdate = false;
  717. // The index in the current test file. Will be reset on splits.
  718. int line_index = 0;
  719. SplitState split;
  720. // When autoupdating, we track whether we're inside conflict markers.
  721. // Otherwise conflict markers are errors.
  722. bool inside_conflict_marker = false;
  723. while (!cursor.empty()) {
  724. auto [line, next_cursor] = cursor.split("\n");
  725. cursor = next_cursor;
  726. auto line_trimmed = line.ltrim();
  727. bool is_consumed = false;
  728. CARBON_ASSIGN_OR_RETURN(
  729. is_consumed,
  730. TryConsumeConflictMarker(line, line_trimmed, &inside_conflict_marker));
  731. if (is_consumed) {
  732. continue;
  733. }
  734. // At this point, remaining lines are part of the test input.
  735. CARBON_ASSIGN_OR_RETURN(
  736. is_consumed,
  737. TryConsumeSplit(line, line_trimmed, found_autoupdate, &line_index,
  738. &split, &context.test_files, &context.non_check_lines));
  739. if (is_consumed) {
  740. continue;
  741. }
  742. ++line_index;
  743. // TIP lines have no impact on validation.
  744. if (line_trimmed.starts_with("// TIP:")) {
  745. continue;
  746. }
  747. CARBON_ASSIGN_OR_RETURN(
  748. is_consumed,
  749. TryConsumeCheck(line_index, line, line_trimmed,
  750. &context.expected_stdout, &context.expected_stderr));
  751. if (is_consumed) {
  752. continue;
  753. }
  754. // At this point, lines are retained as non-CHECK lines.
  755. context.non_check_lines.push_back(
  756. FileTestLine(split.file_index, line_index, line));
  757. CARBON_ASSIGN_OR_RETURN(
  758. is_consumed, TryConsumeArgs(line, line_trimmed, &context.test_args));
  759. if (is_consumed) {
  760. continue;
  761. }
  762. CARBON_ASSIGN_OR_RETURN(
  763. is_consumed,
  764. TryConsumeAutoupdate(line_index, line_trimmed, &found_autoupdate,
  765. &context.autoupdate_line_number));
  766. if (is_consumed) {
  767. continue;
  768. }
  769. CARBON_ASSIGN_OR_RETURN(
  770. is_consumed,
  771. TryConsumeSetCheckSubset(line_trimmed, &context.check_subset));
  772. if (is_consumed) {
  773. continue;
  774. }
  775. }
  776. if (!found_autoupdate) {
  777. return ErrorBuilder() << "Missing AUTOUPDATE/NOAUTOUPDATE setting";
  778. }
  779. context.has_splits = split.has_splits();
  780. CARBON_RETURN_IF_ERROR(FinishSplit(test_name_, &split, &context.test_files));
  781. // Assume there is always a suffix `\n` in output.
  782. if (!context.expected_stdout.empty()) {
  783. context.expected_stdout.push_back(StrEq(""));
  784. }
  785. if (!context.expected_stderr.empty()) {
  786. context.expected_stderr.push_back(StrEq(""));
  787. }
  788. return Success();
  789. }
  790. // Returns the tests to run.
  791. static auto GetTests() -> llvm::SmallVector<std::string> {
  792. // Prefer a user-specified list if present.
  793. auto specific_tests = absl::GetFlag(FLAGS_file_tests);
  794. if (!specific_tests.empty()) {
  795. return llvm::SmallVector<std::string>(specific_tests.begin(),
  796. specific_tests.end());
  797. }
  798. // Extracts tests from the target file.
  799. CARBON_CHECK(!absl::GetFlag(FLAGS_test_targets_file).empty())
  800. << "Missing --test_targets_file.";
  801. auto content = ReadFile(absl::GetFlag(FLAGS_test_targets_file));
  802. CARBON_CHECK(content.ok()) << content.error();
  803. llvm::SmallVector<std::string> all_tests;
  804. for (llvm::StringRef file_ref : llvm::split(*content, "\n")) {
  805. if (file_ref.empty()) {
  806. continue;
  807. }
  808. all_tests.push_back(file_ref.str());
  809. }
  810. return all_tests;
  811. }
  812. // Runs autoupdate for the given tests. This is multi-threaded to try to get a
  813. // little extra speed.
  814. static auto RunAutoupdate(llvm::StringRef exe_path,
  815. llvm::ArrayRef<std::string> tests,
  816. FileTestFactory& test_factory) -> int {
  817. llvm::CrashRecoveryContext::Enable();
  818. llvm::DefaultThreadPool pool(
  819. {.ThreadsRequested = absl::GetFlag(FLAGS_threads)});
  820. // Guard access to both `llvm::errs` and `crashed`.
  821. std::mutex mutex;
  822. bool crashed = false;
  823. for (const auto& test_name : tests) {
  824. pool.async([&test_factory, &mutex, &exe_path, &crashed, test_name] {
  825. // If any thread crashed, don't try running more.
  826. {
  827. std::unique_lock<std::mutex> lock(mutex);
  828. if (crashed) {
  829. return;
  830. }
  831. }
  832. // Use a crash recovery context to try to get a stack trace when
  833. // multiple threads may crash in parallel, which otherwise leads to the
  834. // program aborting without printing a stack trace.
  835. llvm::CrashRecoveryContext crc;
  836. crc.DumpStackAndCleanupOnFailure = true;
  837. bool thread_crashed = !crc.RunSafely([&] {
  838. std::unique_ptr<FileTestBase> test(
  839. test_factory.factory_fn(exe_path, test_name));
  840. auto result = test->Autoupdate();
  841. std::unique_lock<std::mutex> lock(mutex);
  842. if (result.ok()) {
  843. llvm::errs() << (*result ? "!" : ".");
  844. } else {
  845. llvm::errs() << "\n" << result.error().message() << "\n";
  846. }
  847. });
  848. if (thread_crashed) {
  849. std::unique_lock<std::mutex> lock(mutex);
  850. crashed = true;
  851. }
  852. });
  853. }
  854. pool.wait();
  855. if (crashed) {
  856. return EXIT_FAILURE;
  857. }
  858. llvm::errs() << "\nDone!\n";
  859. return EXIT_SUCCESS;
  860. }
  861. // Implements main() within the Carbon::Testing namespace for convenience.
  862. static auto Main(int argc, char** argv) -> int {
  863. Carbon::InitLLVM init_llvm(argc, argv);
  864. testing::InitGoogleTest(&argc, argv);
  865. auto args = absl::ParseCommandLine(argc, argv);
  866. if (args.size() > 1) {
  867. llvm::errs() << "Unexpected arguments:";
  868. for (char* arg : llvm::ArrayRef(args).drop_front()) {
  869. llvm::errs() << " ";
  870. llvm::errs().write_escaped(arg);
  871. }
  872. llvm::errs() << "\n";
  873. return EXIT_FAILURE;
  874. }
  875. std::string exe_path = FindExecutablePath(argv[0]);
  876. // Tests might try to read from stdin. Ensure those reads fail by closing
  877. // stdin and reopening it as /dev/null. Note that STDIN_FILENO doesn't exist
  878. // on Windows, but POSIX requires it to be 0.
  879. if (std::error_code error =
  880. llvm::sys::Process::SafelyCloseFileDescriptor(0)) {
  881. llvm::errs() << "Unable to close standard input: " << error.message()
  882. << "\n";
  883. return EXIT_FAILURE;
  884. }
  885. if (std::error_code error =
  886. llvm::sys::Process::FixupStandardFileDescriptors()) {
  887. llvm::errs() << "Unable to correct standard file descriptors: "
  888. << error.message() << "\n";
  889. return EXIT_FAILURE;
  890. }
  891. if (absl::GetFlag(FLAGS_autoupdate) && absl::GetFlag(FLAGS_dump_output)) {
  892. llvm::errs() << "--autoupdate and --dump_output are mutually exclusive.\n";
  893. return EXIT_FAILURE;
  894. }
  895. llvm::SmallVector<std::string> tests = GetTests();
  896. auto test_factory = GetFileTestFactory();
  897. if (absl::GetFlag(FLAGS_autoupdate)) {
  898. return RunAutoupdate(exe_path, tests, test_factory);
  899. } else if (absl::GetFlag(FLAGS_dump_output)) {
  900. for (const auto& test_name : tests) {
  901. std::unique_ptr<FileTestBase> test(
  902. test_factory.factory_fn(exe_path, test_name));
  903. auto result = test->DumpOutput();
  904. if (!result.ok()) {
  905. llvm::errs() << "\n" << result.error().message() << "\n";
  906. }
  907. }
  908. llvm::errs() << "\nDone!\n";
  909. return EXIT_SUCCESS;
  910. } else {
  911. for (llvm::StringRef test_name : tests) {
  912. testing::RegisterTest(
  913. test_factory.name, test_name.data(), nullptr, test_name.data(),
  914. __FILE__, __LINE__,
  915. [&test_factory, &exe_path, test_name = test_name]() {
  916. return test_factory.factory_fn(exe_path, test_name);
  917. });
  918. }
  919. return RUN_ALL_TESTS();
  920. }
  921. }
  922. } // namespace Carbon::Testing
  923. auto main(int argc, char** argv) -> int {
  924. return Carbon::Testing::Main(argc, argv);
  925. }