Skip to content

Translate Fix-Its from the C++ diagnostics into swift-syntax diagnostics #80376

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 3 commits into from
Mar 29, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions include/swift/AST/ASTBridging.h
Original file line number Diff line number Diff line change
Expand Up @@ -544,6 +544,12 @@ class BridgedDiagnosticArgument {
BridgedDiagnosticArgument(BridgedStringRef s);
};

class BridgedFixIt {
public:
BridgedCharSourceRange replacementRange;
BridgedStringRef replacementText;
};

class BridgedDiagnosticFixIt {
public:
int64_t storage[7];
Expand Down
3 changes: 2 additions & 1 deletion include/swift/Bridging/ASTGen.h
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,8 @@ void swift_ASTGen_addQueuedDiagnostic(
BridgedStringRef categoryName,
BridgedStringRef documentationPath,
const BridgedCharSourceRange *_Nullable highlightRanges,
ptrdiff_t numHighlightRanges);
ptrdiff_t numHighlightRanges,
BridgedArrayRef /*BridgedFixIt*/ fixIts);
void swift_ASTGen_renderQueuedDiagnostics(
void *_Nonnull queued, ssize_t contextSize, ssize_t colorize,
BridgedStringRef *_Nonnull renderedString);
Expand Down
22 changes: 14 additions & 8 deletions lib/AST/DiagnosticBridge.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
#include "swift/AST/DiagnosticEngine.h"
#include "swift/Basic/SourceManager.h"
#include "swift/Bridging/ASTGen.h"
#include "llvm/ADT/ArrayRef.h"
#include "llvm/Support/raw_ostream.h"

using namespace swift;
Expand Down Expand Up @@ -66,14 +67,19 @@ static void addQueueDiagnostic(void *queuedDiagnostics,
if (info.EducationalNotePaths.size() > 0)
documentationPath = info.EducationalNotePaths[0];

// FIXME: Translate Fix-Its.
swift_ASTGen_addQueuedDiagnostic(queuedDiagnostics, perFrontendState,
text.str(),
severity, info.Loc,
info.Category,
documentationPath,
highlightRanges.data(),
highlightRanges.size());
SmallVector<BridgedFixIt, 2> fixIts;
for (const auto &fixIt : info.FixIts) {
fixIts.push_back(BridgedFixIt{ fixIt.getRange(), fixIt.getText() });
}

swift_ASTGen_addQueuedDiagnostic(
queuedDiagnostics, perFrontendState,
text.str(),
severity, info.Loc,
info.Category,
documentationPath,
highlightRanges.data(), highlightRanges.size(),
llvm::ArrayRef<BridgedFixIt>(fixIts));

// TODO: A better way to do this would be to pass the notes as an
// argument to `swift_ASTGen_addQueuedDiagnostic` but that requires
Expand Down
41 changes: 39 additions & 2 deletions lib/ASTGen/Sources/ASTGen/DiagnosticsBridge.swift
Original file line number Diff line number Diff line change
Expand Up @@ -236,6 +236,14 @@ public func addQueuedSourceFile(
queuedDiagnostics.pointee.sourceFileIDs[bufferID] = allocatedSourceFileID
}

private struct BridgedFixItMessage: FixItMessage {
var message: String { "" }

var fixItID: MessageID {
.init(domain: "SwiftCompiler", id: "BridgedFixIt")
}
}

/// Add a new diagnostic to the queue.
@_cdecl("swift_ASTGen_addQueuedDiagnostic")
public func addQueuedDiagnostic(
Expand All @@ -247,7 +255,8 @@ public func addQueuedDiagnostic(
categoryName: BridgedStringRef,
documentationPath: BridgedStringRef,
highlightRangesPtr: UnsafePointer<BridgedCharSourceRange>?,
numHighlightRanges: Int
numHighlightRanges: Int,
fixItsUntyped: BridgedArrayRef
) {
let queuedDiagnostics = queuedDiagnosticsPtr.assumingMemoryBound(
to: QueuedDiagnostics.self
Expand Down Expand Up @@ -374,6 +383,33 @@ public func addQueuedDiagnostic(
diagnosticState.pointee.referencedCategories.insert(category)
}

// Map the Fix-Its
let fixItChanges: [FixIt.Change] = fixItsUntyped.withElements(ofType: BridgedFixIt.self) { fixIts in
fixIts.compactMap { fixIt in
guard let startPos = sourceFile.position(of: fixIt.replacementRange.start),
let endPos = sourceFile.position(
of: fixIt.replacementRange.start.advanced(
by: fixIt.replacementRange.byteLength)) else {
return nil
}

return FixIt.Change.replaceText(
range: startPos..<endPos,
with: String(bridged: fixIt.replacementText),
in: sourceFile.syntax
)
}
}

let fixIts: [FixIt] = fixItChanges.isEmpty
? []
: [
FixIt(
message: BridgedFixItMessage(),
changes: fixItChanges
)
]

let diagnostic = Diagnostic(
node: node,
position: position,
Expand All @@ -382,7 +418,8 @@ public func addQueuedDiagnostic(
severity: severity.asSeverity,
category: category
),
highlights: highlights
highlights: highlights,
fixIts: fixIts
)

queuedDiagnostics.pointee.grouped.addDiagnostic(diagnostic)
Expand Down
15 changes: 15 additions & 0 deletions lib/ASTGen/Sources/MacroEvaluation/SourceManager.swift
Original file line number Diff line number Diff line change
Expand Up @@ -216,6 +216,21 @@ extension SourceManager {
at: replacementRange.upperBound
)
newText = replacingChildData.newChild.description

case .replaceText(
range: let replacementRange,
with: let replacementText,
in: let syntax
):
replaceStartLoc = bridgedSourceLoc(
for: syntax,
at: replacementRange.lowerBound
)
replaceEndLoc = bridgedSourceLoc(
for: syntax,
at: replacementRange.upperBound
)
newText = replacementText
}

newText.withBridgedString { bridgedMessage in
Expand Down