Skip to content

Commit 561071b

Browse files
authored
Merge pull request #80126 from xedin/refactor-diagnostic-bridge-to-use-bridged-structs
[DiagnosticBridge] NFC: Refactor `addQueuedDiagnostic` to use Bridged* types instead of pointers
2 parents 29876fd + e456bba commit 561071b

File tree

3 files changed

+41
-63
lines changed

3 files changed

+41
-63
lines changed

include/swift/Bridging/ASTGen.h

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -27,12 +27,12 @@ void swift_ASTGen_addQueuedSourceFile(
2727
intptr_t displayNameLength, ssize_t parentID, ssize_t positionInParent);
2828
void swift_ASTGen_addQueuedDiagnostic(
2929
void *_Nonnull queued, void *_Nonnull state,
30-
const char *_Nonnull text, ptrdiff_t textLength,
31-
BridgedDiagnosticSeverity severity, const void *_Nullable sourceLoc,
32-
const char *_Nullable categoryName, ptrdiff_t categoryNameLength,
33-
const char *_Nullable documentationPath,
34-
ptrdiff_t documentationPathLength,
35-
const void *_Nullable *_Nullable highlightRanges,
30+
BridgedStringRef text,
31+
BridgedDiagnosticSeverity severity,
32+
BridgedSourceLoc sourceLoc,
33+
BridgedStringRef categoryName,
34+
BridgedStringRef documentationPath,
35+
const BridgedCharSourceRange *_Nullable highlightRanges,
3636
ptrdiff_t numHighlightRanges);
3737
void swift_ASTGen_renderQueuedDiagnostics(
3838
void *_Nonnull queued, ssize_t contextSize, ssize_t colorize,

lib/AST/DiagnosticBridge.cpp

Lines changed: 8 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -56,13 +56,10 @@ static void addQueueDiagnostic(void *queuedDiagnostics,
5656
}
5757

5858
// Map the highlight ranges.
59-
SmallVector<const void *, 2> highlightRanges;
59+
SmallVector<BridgedCharSourceRange, 2> highlightRanges;
6060
for (const auto &range : info.Ranges) {
61-
if (range.isInvalid())
62-
continue;
63-
64-
highlightRanges.push_back(range.getStart().getOpaquePointerValue());
65-
highlightRanges.push_back(range.getEnd().getOpaquePointerValue());
61+
if (range.isValid())
62+
highlightRanges.push_back(range);
6663
}
6764

6865
StringRef documentationPath;
@@ -71,14 +68,12 @@ static void addQueueDiagnostic(void *queuedDiagnostics,
7168

7269
// FIXME: Translate Fix-Its.
7370
swift_ASTGen_addQueuedDiagnostic(queuedDiagnostics, perFrontendState,
74-
text.data(), text.size(),
75-
severity, info.Loc.getOpaquePointerValue(),
76-
info.Category.data(),
77-
info.Category.size(),
78-
documentationPath.data(),
79-
documentationPath.size(),
71+
text.str(),
72+
severity, info.Loc,
73+
info.Category,
74+
documentationPath,
8075
highlightRanges.data(),
81-
highlightRanges.size() / 2);
76+
highlightRanges.size());
8277

8378
// TODO: A better way to do this would be to pass the notes as an
8479
// argument to `swift_ASTGen_addQueuedDiagnostic` but that requires

lib/ASTGen/Sources/ASTGen/DiagnosticsBridge.swift

Lines changed: 27 additions & 44 deletions
Original file line numberDiff line numberDiff line change
@@ -241,15 +241,12 @@ public func addQueuedSourceFile(
241241
public func addQueuedDiagnostic(
242242
queuedDiagnosticsPtr: UnsafeMutableRawPointer,
243243
perFrontendDiagnosticStatePtr: UnsafeMutableRawPointer,
244-
text: UnsafePointer<UInt8>,
245-
textLength: Int,
244+
text: BridgedStringRef,
246245
severity: BridgedDiagnosticSeverity,
247246
cLoc: BridgedSourceLoc,
248-
categoryName: UnsafePointer<UInt8>?,
249-
categoryLength: Int,
250-
documentationPath: UnsafePointer<UInt8>?,
251-
documentationPathLength: Int,
252-
highlightRangesPtr: UnsafePointer<BridgedSourceLoc>?,
247+
categoryName: BridgedStringRef,
248+
documentationPath: BridgedStringRef,
249+
highlightRangesPtr: UnsafePointer<BridgedCharSourceRange>?,
253250
numHighlightRanges: Int
254251
) {
255252
let queuedDiagnostics = queuedDiagnosticsPtr.assumingMemoryBound(
@@ -302,14 +299,16 @@ public func addQueuedDiagnostic(
302299

303300
// Map the highlights.
304301
var highlights: [Syntax] = []
305-
let highlightRanges = UnsafeBufferPointer<BridgedSourceLoc>(
302+
let highlightRanges = UnsafeBufferPointer<BridgedCharSourceRange>(
306303
start: highlightRangesPtr,
307-
count: numHighlightRanges * 2
304+
count: numHighlightRanges
308305
)
309306
for index in 0..<numHighlightRanges {
307+
let range = highlightRanges[index]
308+
310309
// Make sure both the start and the end land within this source file.
311-
guard let start = highlightRanges[index * 2].getOpaquePointerValue(),
312-
let end = highlightRanges[index * 2 + 1].getOpaquePointerValue()
310+
guard let start = range.start.getOpaquePointerValue(),
311+
let end = range.start.advanced(by: range.byteLength).getOpaquePointerValue()
313312
else {
314313
continue
315314
}
@@ -349,53 +348,37 @@ public func addQueuedDiagnostic(
349348
}
350349
}
351350

352-
let category: DiagnosticCategory? = categoryName.flatMap { categoryNamePtr in
353-
let categoryNameBuffer = UnsafeBufferPointer(
354-
start: categoryNamePtr,
355-
count: categoryLength
356-
)
357-
let categoryName = String(decoding: categoryNameBuffer, as: UTF8.self)
358-
359-
// If the data comes from serialized diagnostics, it's possible that
360-
// the category name is empty because StringRef() is serialized into
361-
// an empty string.
362-
guard !categoryName.isEmpty else {
363-
return nil
351+
let documentationPath = String(bridged: documentationPath)
352+
let documentationURL: String? = if !documentationPath.isEmpty {
353+
// If this looks doesn't look like a URL, prepend file://.
354+
documentationPath.looksLikeURL ? documentationPath : "file://\(documentationPath)"
355+
} else {
356+
nil
364357
}
365358

366-
let documentationURL = documentationPath.map { documentationPathPtr in
367-
let documentationPathBuffer = UnsafeBufferPointer(
368-
start: documentationPathPtr,
369-
count: documentationPathLength
359+
let categoryName = String(bridged: categoryName)
360+
// If the data comes from serialized diagnostics, it's possible that
361+
// the category name is empty because StringRef() is serialized into
362+
// an empty string.
363+
let category: DiagnosticCategory? = if !categoryName.isEmpty {
364+
DiagnosticCategory(
365+
name: categoryName,
366+
documentationURL: documentationURL
370367
)
371-
372-
let documentationPath = String(decoding: documentationPathBuffer, as: UTF8.self)
373-
374-
// If this looks doesn't look like a URL, prepend file://.
375-
if !documentationPath.looksLikeURL {
376-
return "file://\(documentationPath)"
377-
}
378-
379-
return documentationPath
368+
} else {
369+
nil
380370
}
381371

382-
return DiagnosticCategory(
383-
name: categoryName,
384-
documentationURL: documentationURL
385-
)
386-
}
387-
388372
// Note that we referenced this category.
389373
if let category {
390374
diagnosticState.pointee.referencedCategories.insert(category)
391375
}
392376

393-
let textBuffer = UnsafeBufferPointer(start: text, count: textLength)
394377
let diagnostic = Diagnostic(
395378
node: node,
396379
position: position,
397380
message: SimpleDiagnostic(
398-
message: String(decoding: textBuffer, as: UTF8.self),
381+
message: String(bridged: text),
399382
severity: severity.asSeverity,
400383
category: category
401384
),

0 commit comments

Comments
 (0)