file_test_base.cpp 38 KB

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