#include #include #include #include namespace { using namespace vls::semantic; namespace D = Void::LanguageServer::Documents; namespace P = Void::LanguageServer::Protocol; using Void::Testing::expect; using Void::Testing::require; using namespace std::string_view_literals; DiagnosticSnapshot snapshot(std::string uri, std::string text, D::PositionEncoding encoding = D::PositionEncoding::utf16) { auto doc = std::make_shared(); doc->identity.canonical_uri = doc->identity.display_uri = std::move(uri); doc->text = std::move(text); doc->line_index = D::LineIndex(doc->text); doc->encoding = encoding; doc->version = 7; return doc; } std::string quote(std::string_view s) { auto out = P::serialize_json(P::Json{std::string{s}}); require(out.has_value()); return *out; } constexpr auto primary = R"({"uri":"main.cpp","uriBaseId":"PWD"})"sv; constexpr auto header = R"({"uri":"a.hpp","uriBaseId":"PWD"})"sv; constexpr auto first_error = R"({"startLine":1,"startColumn":5,"endColumn":12})"sv; std::string artifact(std::string_view name, bool owner, std::optional bytes = {}) { std::string record = R"({"location":)" + std::string{name}; if (owner) record += R"(,"roles":["analysisTarget"])"; if (bytes) record += R"(,"contents":{"text":)" + quote(*bytes) + "}"; return record + "}"; } std::string location(std::string_view name = primary, std::string_view region = first_error) { return R"({"physicalLocation":{"artifactLocation":)" + std::string{name} + R"(,"region":)" + std::string{region} + "}}"; } std::string issue(std::string where = location(), std::string_view level = "error", std::string_view rule = "error", std::string_view related = {}) { return R"({"level":)" + quote(level) + R"(,"ruleId":)" + quote(rule) + R"(,"message":{"text":"the compiler message"},"locations":[)" + where + R"(],"relatedLocations":[)" + std::string{related} + "]}"; } std::string report(bool success, std::string findings = {}, std::string files = artifact(primary, true), std::string_view notifications = {}) { return R"({"version":"2.1.0","runs":[{"tool":{"driver":{"name":"GNU C++26","version":"16.2.0"}},)" R"("originalUriBaseIds":{"PWD":{"uri":"file:///work/"}},"artifacts":[)" + files + R"(],"invocations":[{"executionSuccessful":)" + (success ? "true" : "false") + R"(,"toolExecutionNotifications":[)" + std::string{notifications} + R"(]}],"results":[)" + findings + "]}]}"; } struct Capture { DiagnosticSnapshot owner; DiagnosticJob job; explicit Capture(std::string bytes = "int missing;\n", D::PositionEncoding encoding = D::PositionEncoding::utf16) : owner(snapshot("file:///work/main.cpp", std::move(bytes), encoding)), job{"run-17", "view-3", owner, {{{"main.cpp", "file:///work/"}, owner}}, true, [](const LocationRequest &) { return CoordinatePolicy{}; }} {} auto decode(const std::string &bytes, int status = 1) const { return decode_gcc_diagnostics(job, {job.invocation_id, job.input_view_id, status, true, bytes}); } DiagnosticBatch batch(const std::string &bytes, int status = 1) const { auto result = decode(bytes, status); require(result.has_value()); return std::move(*result); } }; DiagnosticLocation primary_location(const Capture &capture, std::string_view region = first_error) { auto batch = capture.batch(report(false, issue(location(primary, region)))); require(batch.diagnostics.size() == 1); return batch.diagnostics[0].location; } } // namespace VOID_TEST(DiagnosticTests, only_the_matched_owner_and_capture_can_clear) { Capture f; const auto wire = report(true); const auto clean = f.batch(wire, 0); expect(clean.complete_for_owner && clean.diagnostics.empty() && clean.owner == f.owner); expect(clean.invocation_id == "run-17" && clean.input_view_id == "view-3"); expect(!decode_gcc_diagnostics(f.job, {"run-18", "view-3", 0, true, wire})); expect(!decode_gcc_diagnostics(f.job, {"run-17", "view-4", 0, true, wire})); const auto other = snapshot("file:///work/other.cpp", f.owner->text); f.job.artifacts.push_back({{"other.cpp", "file:///work/"}, other}); const auto incorrect = artifact(R"({"uri":"other.cpp","uriBaseId":"PWD"})", true); expect(!f.decode(report(true, {}, incorrect), 0)); expect(!f.decode(report(true, {}, artifact(primary, true) + "," + incorrect), 0)); expect(!f.decode(report(true, {}, artifact(primary, false)), 0)); expect(!f.decode(report(true, {}, artifact(primary, true, "other content")), 0)); f.job.inputs_complete = false; expect(!f.batch(wire, 0).complete_for_owner); } VOID_TEST(DiagnosticTests, artifact_self_indices_must_match_the_table) { Capture f; auto indexed = [](std::string_view name, std::string_view index, bool owner) { return artifact(R"({"uri":")" + std::string{name} + R"(","uriBaseId":"PWD","index":)" + std::string{index} + "}", owner); }; const auto clean = f.batch( report(true, {}, indexed("a.hpp", "0", false) + "," + indexed("main.cpp", "1", true)), 0); expect(clean.complete_for_owner && clean.diagnostics.empty()); const auto second = indexed("a.hpp", "1", false); for (std::string_view index : {"999", R"("0")", "1", "0.0", "-1", "null"}) { const auto rejected = f.decode(report(true, {}, indexed("main.cpp", index, true) + "," + second), 0); expect(!rejected && rejected.error() == DiagnosticError::malformed_report); } const auto rejected = f.decode( report(true, {}, indexed("main.cpp", "0", true) + "," + indexed("a.hpp", "0", false)), 0); expect(!rejected && rejected.error() == DiagnosticError::malformed_report); } VOID_TEST(DiagnosticTests, source_error_and_warning_keep_message_rule_and_related_evidence) { Capture f; const auto note = snapshot("file:///work/a.hpp", "int missing;\n"); f.job.artifacts.push_back({{"a.hpp", "file:///work/"}, note}); const auto files = artifact(primary, true, f.owner->text) + "," + artifact(header, false, note->text); auto related = location(R"({"index":1})") + "," + location(header, R"({"startLine":1})"); const auto batch = f.batch(report(false, issue(location(), "error", "error", related), files)); require(batch.diagnostics.size() == 1); const auto &d = batch.diagnostics.front(); expect(batch.complete_for_owner && d.severity == DiagnosticSeverity::error && d.rule == "error" && d.message == "the compiler message"); expect(d.location.snapshot == f.owner && d.location.range == D::Range{{0, 4}, {0, 11}}); require(d.related.size() == 2); expect(d.related[0].location.snapshot == note && d.related[0].location.range == d.location.range); expect(d.related[1].message.empty() && d.related[1].location.gap == LocationGap::incomplete_region); const auto warning = f.batch(report(true, issue(location(), "warning", "-Wdeprecated-declarations")), 0); expect(warning.complete_for_owner && warning.diagnostics[0].severity == DiagnosticSeverity::warning); } VOID_TEST(DiagnosticTests, source_text_alone_cannot_map_a_remapped_occurrence) { Capture f{"#if 0\nint missing;\n#endif\n#include \"alias.hpp\"\n"}; const auto alias = snapshot("file:///work/alias.hpp", "#line 2 \"main.cpp\"\nint missing;\n"); f.job.artifacts.push_back({{"alias.hpp", "file:///work/"}, alias}); f.job.physical_origin = {}; const auto d = f.batch( report(false, issue(location(primary, R"({"startLine":2,"startColumn":5,"endColumn":12})")), artifact(primary, true, f.owner->text))); expect(!d.complete_for_owner && d.diagnostics[0].message == "the compiler message" && d.diagnostics[0].location.gap == LocationGap::unproven_origin); } VOID_TEST(DiagnosticTests, literal_artifact_names_and_indices_bind_exactly) { Capture f; const auto space = snapshot("file:///work/a%20b.cpp", "int missing;\n"); const auto percent = snapshot("file:///work/a%2520b.cpp", space->text); f.job.artifacts.push_back({{"a b.cpp", ""}, space}); f.job.artifacts.push_back({{"a%20b.cpp", ""}, percent}); const auto files = artifact(primary, true) + "," + artifact(R"({"uri":"a b.cpp"})", false) + "," + artifact(R"({"uri":"a%20b.cpp"})", false); auto locate = [&](std::string_view name) { return f.batch(report(false, issue(location(name)), files)).diagnostics.front().location; }; expect(locate(R"({"uri":"a%20b.cpp","index":2})").snapshot == percent); expect(locate(R"({"index":1})").snapshot == space); expect(locate(R"({"uri":"a%20b.cpp","index":1})").gap == LocationGap::unknown_artifact); expect(locate(R"({"uri":"a b.cpp","uriBaseId":"wrong"})").gap == LocationGap::unknown_artifact); expect(locate(R"({"index":1.00000000000000001})").gap == LocationGap::unknown_artifact); const auto stale = artifact(primary, true) + "," + artifact(R"({"uri":"a b.cpp"})", false, "old"); expect(f.batch(report(false, issue(location(R"({"index":1})")), stale)) .diagnostics[0] .location.gap == LocationGap::artifact_mismatch); } VOID_TEST(DiagnosticTests, certified_byte_positions_use_the_negotiated_encoding) { for (auto encoding : {D::PositionEncoding::utf8, D::PositionEncoding::utf16}) { Capture f{"\t😀é missing;\r\n", encoding}; f.job.physical_origin = [](const LocationRequest &) { return CoordinatePolicy{ColumnUnit::utf8_bytes}; }; const auto loc = primary_location(f, R"({"startLine":1,"startColumn":10,"endColumn":17})"); const std::uint32_t start = encoding == D::PositionEncoding::utf8 ? 9 : 6; expect(loc.range == D::Range{{0, start}, {0, start + 7}}); expect(primary_location(f, R"({"startLine":1,"startColumn":3,"endColumn":17})").gap == LocationGap::ambiguous_column); f.job.physical_origin = [](const LocationRequest &) { return CoordinatePolicy{ColumnUnit::gcc_display}; }; expect(primary_location(f, R"({"startLine":1,"startColumn":16,"endColumn":23})").gap == LocationGap::ambiguous_column); } } VOID_TEST(DiagnosticTests, display_columns_withhold_ambiguous_zero_width_boundaries) { const auto wire = report( false, issue(location(primary, R"({"startLine":1,"startColumn":16,"endColumn":17})"))); for (const auto unit : {ColumnUnit::ascii_consensus, ColumnUnit::gcc_display}) { const auto policy = [unit](const LocationRequest &) { return CoordinatePolicy{unit}; }; for (const char *source : {"int f(){return e\u0301;}\n", "int f(){return y\u200d;}\n", "int f(){return e\u0301\u200d;}\n"}) { Capture f{source}; f.job.physical_origin = policy; const auto batch = f.batch(wire); require(batch.diagnostics.size() == 1); const auto &diagnostic = batch.diagnostics.front(); expect(!batch.complete_for_owner && diagnostic.message == "the compiler message"); expect(diagnostic.location.gap == LocationGap::ambiguous_column && !diagnostic.location.range); } Capture leading{"\u0301x\nz\n"}; leading.job.physical_origin = policy; expect(primary_location(leading, R"({"startLine":1,"startColumn":1,"endLine":2,"endColumn":2})") .gap == LocationGap::ambiguous_column); Capture ascii{"int f(){return yy;} // \u0301\n"}; ascii.job.physical_origin = policy; expect( primary_location(ascii, R"({"startLine":1,"startColumn":16,"endColumn":18})").range == D::Range{{0, 15}, {0, 17}}); } } VOID_TEST(DiagnosticTests, certified_byte_columns_distinguish_zero_width_boundaries) { for (const auto encoding : {D::PositionEncoding::utf8, D::PositionEncoding::utf16}) { Capture f{"e\u0301;\n", encoding}; f.job.physical_origin = [](const LocationRequest &) { return CoordinatePolicy{ColumnUnit::utf8_bytes}; }; expect(primary_location(f, R"({"startLine":1,"startColumn":1,"endColumn":2})").range == D::Range{{0, 0}, {0, 1}}); const std::uint32_t end = encoding == D::PositionEncoding::utf8 ? 3 : 2; expect(primary_location(f, R"({"startLine":1,"startColumn":1,"endColumn":4})").range == D::Range{{0, 0}, {0, end}}); } } VOID_TEST(DiagnosticTests, tabstop_and_first_line_bom_require_known_producer_units) { Capture tab{"\tmissing;\n"}; for (const unsigned width : {4u, 8u}) { tab.job.physical_origin = [width](const LocationRequest &) { return CoordinatePolicy{ColumnUnit::gcc_display, width}; }; const auto region = R"({"startLine":1,"startColumn":)" + std::to_string(width + 1) + R"(,"endColumn":)" + std::to_string(width + 8) + "}"; expect(primary_location(tab, region).range == D::Range{{0, 1}, {0, 8}}); expect(primary_location(tab, R"({"startLine":1,"startColumn":2,"endColumn":3})").gap == LocationGap::ambiguous_column); } Capture bom{"\xef\xbb\xbfmissing;\n"}; const auto region = R"({"startLine":1,"startColumn":1,"endColumn":8})"; expect(primary_location(bom, region).gap == LocationGap::ambiguous_column); bom.job.physical_origin = [](const LocationRequest &) { return CoordinatePolicy{ColumnUnit::utf8_bytes, 8, FirstLineBom::skipped}; }; expect(primary_location(bom, region).range == D::Range{{0, 1}, {0, 8}}); } VOID_TEST(DiagnosticTests, invalid_lines_offsets_and_numeric_variants_never_clamp) { Capture f; for (std::string_view r : {R"({"startLine":0,"startColumn":1,"endColumn":2})", R"({"startLine":1,"startColumn":1.00000000000000001,"endColumn":2})", R"({"startLine":1,"startColumn":3,"endColumn":2})", R"({"startLine":1,"startColumn":1,"endColumn":2147483648})"}) expect(primary_location(f, r).gap == LocationGap::incomplete_region); for (std::string_view r : {R"({"startLine":1,"startColumn":1,"endColumn":14})", R"({"startLine":3,"startColumn":1,"endColumn":2})"}) expect(primary_location(f, r).gap == LocationGap::ambiguous_column); } VOID_TEST(DiagnosticTests, early_failures_preserve_the_problem_without_clearing_diagnostics) { Capture f; auto fatal = f.batch(report(false, issue(location(), "error", "fatal error"))); expect(!fatal.complete_for_owner && fatal.diagnostics.front().location.range.has_value()); auto before_open = f.batch(report(false, issue("", "error", "fatal error"), {})); expect(!before_open.complete_for_owner && before_open.diagnostics.front().message == "the compiler message"); auto unknown = f.batch(report(false, issue(), artifact(primary, false))); expect(!unknown.complete_for_owner && unknown.diagnostics[0].location.gap == LocationGap::unknown_artifact); } VOID_TEST(DiagnosticTests, incomplete_process_and_malformed_reports_cannot_clear) { Capture f; const auto wire = report(true); for (const auto &out : {DiagnosticOutput{"run-17", "view-3", {}, true, wire}, DiagnosticOutput{"run-17", "view-3", 126, true, wire}, DiagnosticOutput{"run-17", "view-3", 0, false, wire}, DiagnosticOutput{"run-17", "view-3", 1, true, {}}}) expect(!decode_gcc_diagnostics(f.job, out)); expect(!f.decode(wire)); expect(!f.decode(report(false))); expect(!f.decode(report(true, issue()), 0)); expect(!f.decode(report(false, issue(location(), "warning")))); expect(!f.decode(report(true, {}, artifact(primary, true), "{}"), 0)); expect(!f.decode(wire.substr(0, wire.size() - 1), 0)); } VOID_TEST(DiagnosticTests, report_structure_and_output_sizes_are_bounded) { Capture f; expect(f.decode(std::string(4 * 1024 * 1024 + 1, ' '), 0).error() == DiagnosticError::resource_limit); std::string repeated = "[0"; for (unsigned i = 0; i < 150000; ++i) repeated += ",0"; expect(f.decode(repeated + "]").error() == DiagnosticError::resource_limit); auto findings = issue(); const auto pos = findings.find("the compiler message"); require(pos != findings.npos); findings.replace(pos, 20, 65537, 'x'); expect(f.decode(report(false, findings)).error() == DiagnosticError::resource_limit); const auto quoted_text = std::string(150000, ','); expect(f.decode(report(true, {}, artifact(primary, true) + "," + artifact(header, false, quoted_text)), 0) .has_value()); } VOID_TEST(DiagnosticTests, compiler_fixes_bind_to_the_owner_snapshot) { Capture f{"int value = 1;\nint main() { return valu; }\n"}; const auto region = R"({"startLine":2,"startColumn":21,"endColumn":25})"sv; const auto fix = R"("fixes":[{"description":{"text":"Replace 'valu' with 'value'"},"artifactChanges":[{"artifactLocation":)" + std::string{primary} + R"(,"replacements":[{"deletedRegion":{"startLine":2,"startColumn":21,"endColumn":25},"insertedContent":{"text":"value"}}]}]}])"; const auto findings = R"({"level":"error","ruleId":"error","message":{"text":"did you mean value?"},"locations":[)" + location(primary, region) + "]," + fix + "}"; const auto batch = f.batch(report(false, findings)); require(batch.diagnostics.size() == 1); const auto &diagnostic = batch.diagnostics.front(); require(diagnostic.fixes.size() == 1); expect(diagnostic.fixes[0].description == "Replace 'valu' with 'value'"); require(diagnostic.fixes[0].edits.size() == 1); expect(diagnostic.fixes[0].edits[0].range == D::Range{{1, 20}, {1, 24}}); expect(diagnostic.fixes[0].edits[0].replacement == "value"); // A fix for another artifact, or one whose region cannot be proven, is dropped. const auto foreign = R"("fixes":[{"description":{"text":"x"},"artifactChanges":[{"artifactLocation":{"uri":"a.hpp","uriBaseId":"PWD"},"replacements":[{"deletedRegion":{"startLine":2,"startColumn":21,"endColumn":25},"insertedContent":{"text":"value"}}]}]}])"; const auto dropped = f.batch( report(false, R"({"level":"error","ruleId":"error","message":{"text":"m"},"locations":[)" + location(primary, region) + "]," + foreign + "}")); expect(dropped.diagnostics.front().fixes.empty()); }