file_test_base.cpp 39 KB

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