#include #include #include #include #include #include #include #include #include #include #include namespace { using namespace Void::LanguageServer::Build; using namespace std::string_view_literals; namespace Workspace = Void::LanguageServer::Workspace; namespace Protocol = Void::LanguageServer::Protocol; namespace fs = std::filesystem; using Protocol::Json; using Strings = std::vector; using Void::Testing::expect; using Void::Testing::require; void write_file(const fs::path &path, std::string_view bytes) { std::ofstream output(path, std::ios::binary); output.write(bytes.data(), static_cast(bytes.size())); output.close(); require(output.good()); } std::string encode(const Json &value) { auto encoded = Protocol::serialize_json(value); require(encoded.has_value()); return *encoded; } std::string command_row(std::string command, std::string file = "unit.cpp") { return encode(Json{Json::Object{{"directory", Json{std::string{"."}}}, {"file", Json{std::move(file)}}, {"command", Json{std::move(command)}}}}); } std::string argv_row(const Strings &argv, std::string file = "unit.cpp", std::string cwd = ".", std::optional output = {}, std::optional command = {}) { Json::Array array; for (const auto &arg : argv) array.push_back(Json{arg}); Json::Object row{{"directory", Json{std::move(cwd)}}, {"file", Json{std::move(file)}}, {"arguments", Json{std::move(array)}}}; if (output) row["output"] = Json{*output}; if (command) row["command"] = Json{*command}; return encode(Json{std::move(row)}); } ProviderScope bind(const fs::path &root) { auto scope = bind_provider_scope({.workspace = {root, "rows-test"}}); require(scope.has_value()); return *scope; } struct Fixture { Void::FileSystem::TempDir temp{"vls_rows_"}; fs::path root = temp.path() / "work"; fs::path base = root / "metadata", database = base / "compile_commands.json"; fs::path gcc = temp.path() / "g++", clang = temp.path() / "clang++"; ProviderScope scope; Fixture() { fs::create_directories(base); scope = bind(root); for (const auto &tool : {gcc, clang}) { write_file(tool, "compiler identity fixture"); fs::permissions(tool, fs::perms::owner_read | fs::perms::owner_exec); } } // Acquire real filesystem identities through the launcher's settings and pinning API. DriverInput pin(Workspace::CompilerDriver dialect = Workspace::CompilerDriver::gcc, Workspace::AuthorityLevel purpose = Workspace::AuthorityLevel::compiler_use, const fs::path &workspace = {}) const { const auto &owner = workspace.empty() ? root : workspace; const auto &tool = dialect == Workspace::CompilerDriver::clang ? clang : gcc; Workspace::SettingsOverrides overrides; overrides.tool_approvals = std::vector{{owner, purpose, tool}}; auto flags = Workspace::settings_from_cli(std::move(overrides), owner); auto local = Workspace::parse_workspace_settings(Json{Json::Object{}}, owner); Workspace::TrustPolicy policy; auto generation = policy.replace_configuration(Workspace::merge_configuration(local, nullptr, &flags)); require(generation.has_value()); require(policy.pin_approved_tool(owner, purpose, tool).has_value()); auto checked = policy.check_tool(owner, purpose, tool, *generation); require(checked.has_value()); return {std::move(*checked), dialect, {"ccache"}}; } std::expected import(std::string_view bytes, const std::optional &driver = {}, const std::optional &profile = {}) const { write_file(database, bytes); auto acquired = read_provider_file(scope, database, FilePurpose::database, scope.limits.max_compdb_bytes); if (!acquired) return std::unexpected(acquired.error()); return import_commands({scope, *acquired, profile, driver}); } ImportedCommands rows(std::initializer_list records, const std::optional &driver = {}, const std::optional &profile = {}) const { std::string bytes = "[\n"; for (const auto &record : records) { if (bytes.size() > 2) bytes += ",\n"; bytes += record; } auto imported = import(bytes + "\n]", driver, profile); require(imported.has_value()); require(imported->contexts.size() == records.size()); return std::move(*imported); } }; bool limited(const BuildContext &context, std::string_view fragment = {}) { return !context.semantic_arguments && std::ranges::any_of(context.limitations, [&](const auto &reason) { return reason.contains(fragment); }); } const Strings &semantic(const BuildContext &context) { require(context.semantic_arguments.has_value()); expect(context.limitations.empty()); return *context.semantic_arguments; } } // namespace VOID_TEST(CommandImport, passive_rows_retain_exact_json_and_unknown_provenance) { Fixture f; const std::string raw = R"json({ "directory":"dir été", "file":"u\"{x}.cpp", "arguments":["g++","-c","u\"{x}.cpp"], "vendor":{"nested":[true,{"x":"\\\"}]"}],"n":2.5} })json"; const auto result = f.rows({raw}, {}, "debug"); const auto &row = result.contexts.front(); require(row.original && row.original->arguments); expect(row.original->original_record == raw && row.original->metadata_file == f.database); expect(!row.original->command && row.original->arguments->at(2) == "u\"{x}.cpp"); expect(row.key.profile == "debug" && !row.key.target && row.key.role == ContextRole::unknown); expect(row.key.owning_tu == f.base / "dir été" / "u\"{x}.cpp"); expect(limited(row, "checked compiler") && !row.source_language); require(row.sources.size() == 1); expect(row.sources.front().source_path == row.key.owning_tu && !row.sources.front().compiler_path); expect(row.sources.front().origin == SourceOrigin::unknown && row.sources.front().editability == Editability::unknown); expect(!row.toolchain && !row.standard_library && !row.environment && !row.prerequisites && !row.artifacts); expect(result.dependencies.empty() && !result.revision.empty()); expect(BuildGeneration::create({f.scope.workspace, "compdb", result.revision}, result.contexts).has_value()); } VOID_TEST(CommandImport, malformed_structure_rejects_the_entire_database) { Fixture f; const auto good = argv_row({"c++", "-c", "a.cpp"}, "a.cpp"); for (const auto &bad : {"null", "[]", "{}", R"({"directory":".","file":"b.cpp"})", R"({"directory":2,"file":"b.cpp","arguments":[]})", R"({"directory":".","file":"b.cpp","arguments":null,"command":"c++ -c b.cpp"})", R"({"directory":".","file":"b.cpp","arguments":["c++",1],"command":"c++ -c b.cpp"})", R"({"directory":".","file":"b.cpp","command":42,"arguments":["ok"]})", R"({"directory":".","file":"b.cpp","arguments":[],"command":"c++ -c b.cpp"})", R"({"directory":".","file":"b.cpp","output":false,"arguments":["cc"]})", R"({"directory":".","file":"b.cpp","file":"c.cpp","command":""})", R"({"directory":".","file":"b.cpp","arguments":["nul\u0000arg"]})", R"({"directory":".","file":"\ud800","arguments":["cc"]})", R"({"x":[})"}) { auto result = f.import("[" + good + "," + bad + "]"); expect(!result.has_value()); } for (const auto &bad : {"", "{}", "[,]", "[{} ,]", "[]x", "[[]", "[\v]", "[\xff]"}) expect(!f.import(bad)); auto empty = f.import(" [ ]\t\n"); require(empty.has_value()); expect(empty->contexts.empty()); } VOID_TEST(CommandImport, command_quoting_and_typed_arguments_precedence) { Fixture f; const auto checked = f.pin(); const auto command = f.gcc.native() + R"( -DNAME="has spaces and \"quotes\"" -I'include folder' -x c++ -c café\ source.cpp)"; auto command_only = f.rows({command_row(command, "café source.cpp")}, checked); const auto &c = command_only.contexts.front(); expect(semantic(c) == Strings{f.gcc.native(), "-DNAME=has spaces and \"quotes\"", "-I" + (f.base / "include folder").native(), "-x", "c++", "-c", (f.base / "café source.cpp").native()}); expect(c.source_language == "c++" && c.original->command == command && !c.original->arguments); const Strings typed{f.gcc.native(), "-DEXACT=$literal", "-c", "unit.cpp"}; const auto secondary = "ignored && touch " + (f.temp.path() / "marker").native(); auto both = f.rows({argv_row(typed, "unit.cpp", ".", {}, secondary)}, checked); const auto &row = both.contexts.front(); expect(semantic(row)[1] == typed[1] && row.original->arguments == typed && row.original->command == secondary); expect(!fs::exists(f.temp.path() / "marker")); } VOID_TEST(CommandImport, checked_driver_uses_the_language_of_the_actual_input_operand) { Fixture f; write_file(f.base / "generated.inc", "int value;\n"); fs::create_symlink("generated.inc", f.base / "alias.cpp"); for (const auto dialect : {Workspace::CompilerDriver::gcc, Workspace::CompilerDriver::clang}) { const auto driver = f.pin(dialect); const auto &tool = driver.tool.identity.selected_path.native(); auto imported = f.rows({argv_row({tool, "-c", "alias.cpp"}, "generated.inc"), argv_row({tool, "-xc", "-c", "unit.cpp"}), argv_row({tool, "-c", "unit.cpp", "-x", "c"}), argv_row({tool, "-c", "ordinary.h"}, "ordinary.h")}, driver); const auto &rows = imported.contexts; require(rows.size() == 4); expect(rows[0].key.owning_tu == f.base / "generated.inc" && rows[0].source_language == "c++"); expect(rows[1].source_language == "c"); expect(rows[2].source_language == "c++"); expect(rows[3].semantic_arguments.has_value() && !rows[3].source_language); } } VOID_TEST(CommandImport, shell_constructs_and_incomplete_quoting_remain_passive) { Fixture f; const auto pin = f.pin(); const auto marker = f.temp.path() / "marker"; for (const std::string tail : {" && touch " + marker.native(), " -DA=$(touch " + marker.native() + ")", std::string{" *.cpp"}, std::string{" > file"}, std::string{" -DNAME=\"$HOME\""}, std::string{" -DNAME='broken"}, std::string{" \\"}}) { const auto command = f.gcc.native() + " -c unit.cpp" + tail; const auto result = f.rows({command_row(command)}, pin); const auto &row = result.contexts.front(); expect(limited(row) && row.original->command == command); } expect(!fs::exists(marker)); } VOID_TEST(CommandImport, duplicate_and_conflicting_alternatives_survive_reordering) { Fixture f; const auto a = argv_row({"g++", "-c", "unit.cpp"}); const auto b = argv_row({"g++", "-DOTHER", "-c", "unit.cpp"}); const std::string extension = a.substr(0, a.size()-1) + ",\"mode\":\"vendor\"}"; const auto ordered = f.rows({a, b, a, extension}); const auto changed = f.rows({extension, a, a, b}); auto alternatives = [](const auto &result) { std::map values; for (const auto &row : result.contexts) expect(values.emplace(row.key.alternative, *row.original->original_record).second && row.key.alternative == row.original->record_id); return values; }; expect(alternatives(ordered) == alternatives(changed)); const auto gen = BuildGeneration::create({f.scope.workspace, "compdb", ordered.revision}, ordered.contexts); require(gen.has_value()); expect((*gen)->candidates(f.base / "unit.cpp").size() == 4); } VOID_TEST(CommandImport, relative_paths_use_logical_database_parent) { Fixture f; const auto actual = f.root / "actual"; fs::create_directory(actual); fs::create_symlink(actual / "commands.json", f.database); const auto checked = f.pin(); const auto row = argv_row({f.gcc.native(), "-c", "源.cpp", "-o", "obj/x.o"}, "源.cpp", "./build", (f.base / "build/obj/x.o").native()); const auto result = f.rows({row}, checked); const auto &context = result.contexts.front(); expect(context.working_directory == f.base / "build" && context.key.owning_tu == f.base / "build/源.cpp"); expect(semantic(context) == Strings{f.gcc.native(), "-c", (f.base / "build/源.cpp").native()}); expect(context.original->metadata_file == f.database); } VOID_TEST(CommandImport, repeated_native_flags_and_cmake_output_are_preserved) { Fixture f; const auto pin = f.pin(); const auto obj = (f.base / "obj/unit.cpp.o").native(); const Strings native{f.gcc.native(), "-std=c++26", "-I", f.root.native(), "-I", f.root.native(), "-O0", "-g", "-freflection", "-freflection", "-freflection", "-c", "unit.cpp", "-o", obj}; const auto cmake = f.gcc.native() + " -I" + f.root.native() + " -g -std=c++26 -Wall -Wextra -Wpedantic -freflection -o obj/unit.cpp.o -c unit.cpp"; const auto named = argv_row(native, "unit.cpp", ".", obj); auto cmake_row = command_row(cmake); cmake_row.insert(cmake_row.size()-1, ",\"output\":" + encode(Json{obj})); const auto result = f.rows({named, cmake_row}, pin); expect(result.contexts[0].original->arguments == native && result.contexts[1].original->command == cmake); expect(std::ranges::count(semantic(result.contexts[0]), "-freflection"sv) == 3); for (const auto &row : result.contexts) { expect(!std::ranges::contains(semantic(row), "-o"sv)); expect(semantic(row).back() == (f.base / "unit.cpp").native()); } const auto passive = f.rows({named, cmake_row}); expect(std::ranges::all_of(passive.contexts, [](const auto &row) { return limited(row); })); } VOID_TEST(CommandImport, output_pair_requires_the_correct_option_boundary) { Fixture f; const auto pin = f.pin(); const auto include = f.rows({argv_row({f.gcc.native(), "-I", "-o", "-c", "unit.cpp"})}, pin); expect(semantic(include.contexts.front()) == Strings{f.gcc.native(), "-I", (f.base / "-o").native(), "-c", (f.base / "unit.cpp").native()}); const auto rejected = f.rows({ argv_row({f.gcc.native(), "-D", "-o", "-c", "unit.cpp"}), argv_row({f.gcc.native(), "-o", "x.o", "-c", "unit.cpp"}, "unit.cpp", ".", "other.o"), argv_row({f.gcc.native(), "-o", "x.o", "-o", "y.o", "-c", "unit.cpp"}), argv_row({f.gcc.native(), "-fsyntax-only", "-o", "x.o", "unit.cpp"}), argv_row({f.gcc.native(), "-c", "unit.cpp", "-o"})}, pin); expect(std::ranges::all_of(rejected.contexts, [](const auto &row) { return limited(row); })); } VOID_TEST(CommandImport, unsupported_options_and_different_owning_tu_are_retained) { Fixture f; const auto pin = f.pin(); for (const Strings args : {Strings{f.gcc.native(), "-c", "other.cpp"}, Strings{f.gcc.native(), "-c", "other.cpp", "unit.cpp"}, Strings{f.gcc.native(), "-include", "forced.h", "-c", "unit.cpp"}, Strings{f.gcc.native(), "-include", "-o", "-c", "unit.cpp"}, Strings{f.gcc.native(), "-fplugin=marker", "-c", "unit.cpp"}, Strings{f.gcc.native(), "-Xclang", "-load", "-c", "unit.cpp"}, Strings{f.gcc.native(), "-I=target-root", "-c", "unit.cpp"}, Strings{f.gcc.native(), "-isystem", "$SYSROOT/inc", "-c", "unit.cpp"}, Strings{f.gcc.native(), "-I-", "-c", "unit.cpp"}}) { const auto result = f.rows({argv_row(args)}, pin); const auto &row = result.contexts.front(); expect(limited(row) && row.key.owning_tu == f.base / "unit.cpp"); expect(row.original->arguments == args && !row.sources.front().compiler_path); } } VOID_TEST(CommandImport, wrong_driver_root_purpose_or_wrapper_never_reads_responses) { Fixture f; write_file(f.base / "ok.rsp", "-DFROM_RESPONSE"); const int watcher = ::inotify_init1(IN_CLOEXEC | IN_NONBLOCK); require(watcher >= 0); require(::inotify_add_watch(watcher, (f.base / "ok.rsp").c_str(), IN_ACCESS) >= 0); pollfd events{watcher, POLLIN, 0}; const auto checked = f.pin(); const auto row = argv_row({f.gcc.native(), "@ok.rsp", "-c", "unit.cpp"}); fs::create_directory(f.temp.path() / "other"); const auto other = f.pin(Workspace::CompilerDriver::gcc, Workspace::AuthorityLevel::compiler_use, f.temp.path() / "other"); const auto purpose = f.pin(Workspace::CompilerDriver::gcc, Workspace::AuthorityLevel::trusted_metadata_refresh); auto unknown = checked; unknown.driver = Workspace::CompilerDriver::unknown; auto wrong_dialect = checked; wrong_dialect.driver = Workspace::CompilerDriver::clang; for (const auto &pin : {other, purpose, unknown, wrong_dialect}) { const auto result = f.rows({row}, pin); expect(limited(result.contexts.front()) && result.dependencies.empty()); } for (const auto &args : {Strings{"g++", "@ok.rsp", "-c", "unit.cpp"}, Strings{f.clang.native(), "@ok.rsp", "-c", "unit.cpp"}, Strings{"wrapper", f.gcc.native(), "@ok.rsp", "-c", "unit.cpp"}, Strings{"ccache", "unselected", "@ok.rsp", f.gcc.native(), "-c", "unit.cpp"}, Strings{f.gcc.native(), "--rsp-quoting=windows", "@ok.rsp", "-c", "unit.cpp"}}) { const auto result = f.rows({argv_row(args)}, checked); expect(limited(result.contexts.front()) && result.dependencies.empty()); } expect(f.rows({row}).dependencies.empty()); expect(::poll(&events, 1, 0) == 0); const auto ok = f.rows({argv_row({"ccache", f.gcc.native(), "@ok.rsp", "-c", "unit.cpp"})}, checked); expect(semantic(ok.contexts.front()).front() == f.gcc.native() && ok.dependencies.size() == 1); expect(::poll(&events, 1, 0) == 1); ::close(watcher); } VOID_TEST(CommandImport, ambiguous_parent_steps_and_external_routes_stay_passive) { Fixture f; fs::create_directory_symlink(f.temp.path(), f.base / "link"); write_file(f.base / "in.rsp", "-DLOCAL"); write_file(f.temp.path() / "out.rsp", "-DEXTERNAL"); const auto pin = f.pin(); auto result = f.rows({ argv_row({f.gcc.native(), "@in.rsp", "-c", "unit.cpp"}, "unit.cpp", "link/.."), argv_row({f.gcc.native(), "@in.rsp", "-c", "unit.cpp"}, "link/../unit.cpp"), argv_row({f.gcc.native(), "@in.rsp", "-c", "unit.cpp"}, (f.temp.path() / "unit.cpp").native()), argv_row({f.gcc.native(), "@link/../in.rsp", "-c", "unit.cpp"}), argv_row({f.gcc.native(), "@" + (f.temp.path() / "out.rsp").native(), "-c", "unit.cpp"}), argv_row({f.gcc.native(), "-Ilink/../headers", "-c", "unit.cpp"}), argv_row({f.gcc.native(), "-I", f.temp.path().native(), "-c", "unit.cpp"}), argv_row({f.gcc.native(), "-o", "link/../x.o", "-c", "unit.cpp"})}, pin); expect(result.dependencies.empty()); expect(std::ranges::all_of(result.contexts, [](const auto &row) { return limited(row); })); } VOID_TEST(CommandImport, gcc_and_clang_response_dialects_use_the_invocation_cwd) { Fixture f; fs::create_directory(f.base / "sub"); write_file(f.base / "sub" / "first.rsp", R"('-DSINGLE=a\b' "-DDOUBLE=c\q" @second.rsp)"); write_file(f.base / "sub" / "second.rsp", "-DWRONG"); write_file(f.base / "second.rsp", "-I\"include path\" -c 'unit café.cpp'"); for (auto dialect : {Workspace::CompilerDriver::gcc, Workspace::CompilerDriver::clang}) { const auto checked = f.pin(dialect); const auto exe = checked.tool.identity.selected_path.native(); const auto result = f.rows({argv_row({exe, "@sub/first.rsp"}, "unit café.cpp")}, checked); const bool gcc = dialect == Workspace::CompilerDriver::gcc; expect(semantic(result.contexts.front()) == Strings{exe, gcc ? "-DSINGLE=a\\b" : "-DSINGLE=ab", gcc ? "-DDOUBLE=c\\q" : "-DDOUBLE=cq", "-I" + (f.base / "include path").native(), "-c", (f.base / "unit café.cpp").native()}); require(result.dependencies.size() == 2); expect(result.dependencies[0].logical_path == f.base / "sub/first.rsp" && result.dependencies[1].logical_path == f.base / "second.rsp"); } } VOID_TEST(CommandImport, response_cycles_use_active_physical_objects) { Fixture f; const auto pin = f.pin(); const auto row = argv_row({f.gcc.native(), "@one", "-c", "unit.cpp"}); f.scope.limits.max_response_file_depth = 2; write_file(f.base / "one", "@one"); expect(limited(f.rows({row}, pin).contexts.front(), "recursive")); write_file(f.base / "one", "@two"); fs::create_hard_link(f.base / "one", f.base / "two"); const auto aliased = f.rows({row}, pin); expect(limited(aliased.contexts.front(), "recursive")); require(aliased.dependencies.size() == 2); expect(same_provider_object(aliased.dependencies[0], aliased.dependencies[1])); fs::remove(f.base / "two"); write_file(f.base / "two", "@one"); f.scope.limits.max_response_file_depth = 3; expect(limited(f.rows({row}, pin).contexts.front(), "recursive")); } VOID_TEST(CommandImport, repeated_response_expansions_retain_each_logical_name) { Fixture f; const auto checked = f.pin(); write_file(f.base / "flags", "-DFROM=1"); fs::create_hard_link(f.base / "flags", f.base / "alias"); const auto row = argv_row({f.gcc.native(), "@flags", "@alias", "@flags", "-c", "unit.cpp"}); const auto first = f.rows({row}, checked); expect(std::ranges::count(semantic(first.contexts.front()), "-DFROM=1"sv) == 3); require(first.dependencies.size() == 2); expect(first.dependencies[0].logical_path != first.dependencies[1].logical_path); write_file(f.base / "replacement", "-DFROM=2"); fs::rename(f.base / "replacement", f.base / "alias"); expect(!provider_file_current(f.scope, first.dependencies[1])); const auto second = f.rows({row}, checked); expect(first.revision != second.revision); expect(first.contexts.front().key == second.contexts.front().key); expect(*first.dependencies[1].bytes == "-DFROM=1"); expect(std::ranges::count(semantic(second.contexts.front()), "-DFROM=2"sv) == 1); } VOID_TEST(CommandImport, response_depth_bytes_token_and_encoding_limits) { Fixture f; const auto pin = f.pin(); const auto row = argv_row({f.gcc.native(), "@one", "-c", "unit.cpp"}); write_file(f.base / "one", "@two"); write_file(f.base / "two", "-DMODE"); f.scope.limits.max_response_file_depth = 1; expect(limited(f.rows({row}, pin).contexts.front(), "nesting")); f.scope.limits.max_response_file_depth = 2; f.scope.limits.max_response_file_bytes = 9; expect(limited(f.rows({row}, pin).contexts.front())); f.scope.limits.max_response_file_bytes = 10; expect(std::ranges::contains(semantic(f.rows({row}, pin).contexts.front()), "-DMODE"sv)); write_file(f.base / "one", "@two @two"); f.scope.limits.max_response_file_bytes = 16; expect(limited(f.rows({row}, pin).contexts.front(), "byte limit")); f.scope.limits.max_response_file_bytes = 1024 * 1024; std::string many; for (std::size_t i = 0; i < 4096; ++i) many += "-g "; write_file(f.base / "one", many); expect(limited(f.rows({row}, pin).contexts.front(), "argument limit")); for (const std::string invalid : {std::string{"-D\xff"}, std::string{"x\0y", 3}, std::string{"'bad"}, std::string{"\xef\xbb\xbf-DNAME"}}) { write_file(f.base / "one", invalid); expect(limited(f.rows({row}, pin).contexts.front())); } fs::remove(f.base / "one"); expect(limited(f.rows({row}, pin).contexts.front(), "response file")); } VOID_TEST(CommandImport, oversized_json_argv_and_database_are_rejected_atomically) { Fixture f; const auto valid = argv_row({"cc", "-c", "unit.cpp"}); std::string prefix = "[" + valid + "," + valid.substr(0, valid.size()-1) + ",\"extra\":"; expect(!f.import(prefix + std::string(32, '[') + "0" + std::string(32, ']') + "}]")); expect(!f.import(prefix + "\"" + std::string(2 * 1024 * 1024, 'x') + "\"}]")); std::string dense{"["}; for (int i = 0; i < 20000; ++i) dense += "0,"; expect(!f.import(prefix + dense + "0]}]")); expect(!f.import("[" + argv_row(Strings(4097, "x")) + "]")); expect(!f.import("[" + argv_row({std::string(64 * 1024 + 1, 'x')}) + "]")); write_file(f.database, "[" + valid + "]"); auto acquired = read_provider_file(f.scope, f.database, FilePurpose::database, f.scope.limits.max_compdb_bytes); require(acquired.has_value()); f.scope.limits.max_compdb_bytes = 1; expect(!import_commands({f.scope, *acquired, {}, {}})); } VOID_TEST(CommandImport, database_must_match_the_current_scope_binding) { Fixture f; write_file(f.database, "[]"); auto file = read_provider_file(f.scope, f.database, FilePurpose::database, 2); require(file.has_value()); auto rebound = bind(f.root); expect(!import_commands({rebound, *file, {}, {}})); write_file(f.base / "new", "[]"); fs::rename(f.base / "new", f.database); auto stale = import_commands({f.scope, *file, {}, {}}); expect(!stale && stale.error().code == "changed"); } VOID_TEST(CommandImport, physical_owners_follow_links_before_parent_components) { Fixture f; for (const auto *name : {"real", "other"}) { fs::create_directories(f.base / name / "sub"); write_file(f.base / name / "x.cpp", "int real;\n"); } write_file(f.base / "x.cpp", "int innocent;\n"); fs::create_directory_symlink("real/sub", f.base / "link"); fs::create_symlink("real/x.cpp", f.base / "alias.cpp"); const auto linked = argv_row({"c++", "-c", "link/../x.cpp"}, "link/../x.cpp"); const auto cwd = argv_row({"c++", "-c", "x.cpp"}, "x.cpp", "link/.."); const auto alias = argv_row({"c++", "-c", "alias.cpp"}, "alias.cpp"); write_file(f.database, "[" + linked + "," + cwd + "," + alias + "]"); const auto original = observe_compdb(f.scope, f.database); require(original.health == ProviderHealth::ready && original.generation); expect(original.generation->candidates(f.base / "x.cpp").empty()); expect(original.generation->candidates(f.base / "real/x.cpp").size() == 3); expect(!changed_provider_input(f.scope, original)); fs::remove(f.base / "link"); fs::create_directory_symlink("other/sub", f.base / "link"); expect(changed_provider_input(f.scope, original).has_value()); const auto latest = observe_compdb(f.scope, f.database); require(latest.health == ProviderHealth::ready && latest.generation); expect(latest.generation->candidates(f.base / "x.cpp").empty()); expect(latest.generation->candidates(f.base / "real/x.cpp").size() == 1); expect(latest.generation->candidates(f.base / "other/x.cpp").size() == 2); expect(!changed_provider_input(f.scope, latest)); } VOID_TEST(CommandImport, command_and_argv_preserve_a_quoted_literal_backslash) { Fixture f; const auto result = f.rows({command_row(f.gcc.native() + R"( -I"inc\q" -c unit.cpp)"), argv_row({f.gcc.native(), R"(-Iinc\q)", "-c", "unit.cpp"})}, f.pin()); const auto &words = semantic(result.contexts.front()); expect(words == semantic(result.contexts.back())); expect(std::ranges::contains(words, "-I" + (f.base / R"(inc\q)").native())); } VOID_TEST(CommandImport, creating_a_missing_response_invalidates_the_observation) { Fixture f; const auto driver = f.pin(); write_file(f.database, "[" + argv_row({f.gcc.native(), "@later.rsp", "-c", "unit.cpp"}) + "]"); const auto missing = observe_compdb(f.scope, f.database, driver); require(missing.health == ProviderHealth::ready && missing.generation); expect(limited(missing.generation->contexts().front(), "missing")); expect(!changed_provider_input(f.scope, missing)); const auto response = f.base / "later.rsp"; write_file(response, "-DFROM_LATER"); expect(changed_provider_input(f.scope, missing) == response); const auto present = observe_compdb(f.scope, f.database, driver); require(present.health == ProviderHealth::ready && present.generation); expect( std::ranges::contains(semantic(present.generation->contexts().front()), "-DFROM_LATER"sv)); expect(present.inputs.size() == 2 && !changed_provider_input(f.scope, present)); fs::remove(response); expect(changed_provider_input(f.scope, present) == response); } VOID_TEST(CommandImport, search_directory_authority_follows_the_bound_link_target) { Fixture f; const auto driver = f.pin(); const auto include = f.base / "include-alias"; fs::create_directories(f.temp.path() / "outside"); fs::create_directory_symlink(f.temp.path() / "outside", include); write_file(f.database, "[" + argv_row({f.gcc.native(), "-Iinclude-alias", "-c", "unit.cpp"}) + "]"); const auto external = observe_compdb(f.scope, f.database, driver); require(external.health == ProviderHealth::ready && external.generation); expect(limited(external.generation->contexts().front())); expect(!changed_provider_input(f.scope, external)); fs::create_directory(f.base / "inside"); fs::remove(include); fs::create_directory_symlink("inside", include); expect(changed_provider_input(f.scope, external) == include); const auto internal = observe_compdb(f.scope, f.database, driver); require(internal.health == ProviderHealth::ready && internal.generation); expect(std::ranges::contains(semantic(internal.generation->contexts().front()), "-I" + include.native())); expect(!changed_provider_input(f.scope, internal)); } VOID_TEST(CommandImport, bounded_response_names_apply_across_the_whole_generation) { Fixture f; const auto checked = f.pin(); std::string rows = "[" + argv_row({f.gcc.native(), "-c", "unit.cpp"}); for (unsigned i = 0; i < 256; ++i) { rows += "," + argv_row({f.gcc.native(), "@missing-" + std::to_string(i), "-c", "unit.cpp"}); } auto within = f.import(rows + "]", checked); require(within.has_value()); expect(within->contexts.size() == 257 && within->contexts.front().semantic_arguments && limited(within->contexts.back(), "missing")); const auto repeated = argv_row({f.gcc.native(), "@missing-0", "-c", "unit.cpp"}); auto reuse = f.import(rows + "," + repeated + "]", checked); expect(reuse && reuse->contexts.size() == 258); const auto extra = argv_row({f.gcc.native(), "@extra", "-c", "unit.cpp"}); expect(!f.import(rows + "," + extra + "]", checked)); }