Files
Language-Server/include/vls/semantic/Diagnostics.hpp
T
faumaray 946b1a005e Offer compiler quick fixes as code actions
Decode GCC SARIF fixes into bounded diagnostic edits through the same
physical-origin policy as locations; unprovable regions are dropped. A
bounded projection filters fixes to the requested range, deduplicates
and bounds them, and textDocument/codeAction serves them from retained
published diagnostics. Registered tests cover the SARIF binding and the
projection; a real stdio session returned the compiler's
Replace 'valu' with 'value' quickfix.
2026-12-05 15:03:21 +03:00

123 lines
3.6 KiB
C++

#pragma once
#include <Void/LanguageServer/Documents/DocumentSnapshot.hpp>
#include <compare>
#include <cstddef>
#include <cstdint>
#include <expected>
#include <functional>
#include <memory>
#include <optional>
#include <string>
#include <string_view>
#include <vector>
namespace vls::semantic {
namespace Documents = Void::LanguageServer::Documents;
using DiagnosticSnapshot = std::shared_ptr<const Documents::DocumentSnapshot>;
// GCC can emit literal filenames, including percent signs. These spellings are
// supplied by the launcher and are compared exactly, without URL decoding.
struct ArtifactName {
std::string uri, base_uri;
auto operator<=>(const ArtifactName &) const = default;
};
struct DiagnosticArtifact {
ArtifactName name;
DiagnosticSnapshot snapshot;
};
struct ReportedRegion {
std::uint32_t first_line, first_column, last_line, end_column;
};
enum class ColumnUnit { ascii_consensus, utf8_bytes, gcc_display };
enum class FirstLineBom { unknown, counted, skipped };
struct CoordinatePolicy {
ColumnUnit unit{ColumnUnit::ascii_consensus};
unsigned tabstop{8};
FirstLineBom bom{FirstLineBom::unknown};
};
struct LocationRequest {
const DiagnosticArtifact &artifact;
ReportedRegion region;
std::size_t result_index, location_index;
bool related;
};
struct DiagnosticJob {
std::string invocation_id, input_view_id;
DiagnosticSnapshot owner;
std::vector<DiagnosticArtifact> artifacts;
// The input owner accounts for unresolved includes and lookup dependencies.
bool inputs_complete{};
// Return a policy only for a proven physical location in the immutable
// inputs. A matching snippet cannot establish the origin of a #line remap.
std::function<std::optional<CoordinatePolicy>(const LocationRequest &)> physical_origin;
};
struct DiagnosticOutput {
std::string_view invocation_id, input_view_id;
std::optional<int> exit_code;
bool stream_complete{};
std::string_view sarif;
};
enum class DiagnosticError {
invalid_job,
wrong_capture,
incomplete_stream,
failed_process,
malformed_report,
unsupported_report,
wrong_owner,
tool_failure,
resource_limit
};
enum class LocationGap {
none,
no_location,
unknown_artifact,
artifact_mismatch,
incomplete_region,
unproven_origin,
ambiguous_column
};
struct DiagnosticLocation {
LocationGap gap{LocationGap::no_location};
DiagnosticSnapshot snapshot;
std::optional<Documents::Range> range;
};
struct RelatedLocation {
std::string message;
DiagnosticLocation location;
};
enum class DiagnosticSeverity { error, warning, information, hint };
// One replacement from a compiler-suggested fix, in the owner snapshot's
// negotiated coordinates.
struct DiagnosticEdit {
Documents::Range range;
std::string replacement;
};
struct DiagnosticFix {
std::string description;
std::vector<DiagnosticEdit> edits;
};
struct CompilerDiagnostic {
DiagnosticSeverity severity;
std::string message, rule;
DiagnosticLocation location;
std::vector<RelatedLocation> related;
// Bounded compiler-suggested fixes. An edit whose region cannot be proven
// in the owner snapshot is dropped, never guessed.
std::vector<DiagnosticFix> fixes;
};
struct DiagnosticBatch {
std::string invocation_id, input_view_id;
DiagnosticSnapshot owner;
std::vector<CompilerDiagnostic> diagnostics;
// The publisher must still validate that this invocation is current.
bool complete_for_owner{};
};
[[nodiscard]] std::expected<DiagnosticBatch, DiagnosticError>
decode_gcc_diagnostics(const DiagnosticJob &, const DiagnosticOutput &);
} // namespace vls::semantic