Skip to content

Commit da41b19

Browse files
committed
ASTBriding: Bridge swift::DiagnosticKind directly
1 parent dcb37d5 commit da41b19

File tree

8 files changed

+57
-56
lines changed

8 files changed

+57
-56
lines changed

include/swift/AST/ASTBridging.h

Lines changed: 2 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@
1919
// Pure bridging mode does not permit including any C++/llvm/swift headers.
2020
// See also the comments for `BRIDGING_MODE` in the top-level CMakeLists.txt file.
2121
//
22+
#include "swift/AST/DiagnosticKind.h"
2223
#include "swift/Basic/BasicBridging.h"
2324

2425
#ifdef USED_IN_CPP_SOURCE
@@ -565,15 +566,6 @@ class BridgedDiagnosticFixIt {
565566
BridgedDiagnosticFixIt(BridgedSourceLoc start, uint32_t length, BridgedStringRef text);
566567
};
567568

568-
/// Diagnostic severity when reporting diagnostics.
569-
enum ENUM_EXTENSIBILITY_ATTR(open) BridgedDiagnosticSeverity : size_t {
570-
BridgedFatalError,
571-
BridgedError,
572-
BridgedWarning,
573-
BridgedRemark,
574-
BridgedNote,
575-
};
576-
577569
class BridgedDiagnostic {
578570
public:
579571
struct Impl;
@@ -612,7 +604,7 @@ bool BridgedDiagnosticEngine_hadAnyError(BridgedDiagnosticEngine);
612604
SWIFT_NAME("BridgedDiagnostic.init(at:message:severity:engine:)")
613605
BridgedDiagnostic BridgedDiagnostic_create(BridgedSourceLoc cLoc,
614606
BridgedStringRef cText,
615-
BridgedDiagnosticSeverity severity,
607+
swift::DiagnosticKind severity,
616608
BridgedDiagnosticEngine cDiags);
617609

618610
/// Highlight a source range as part of the diagnostic.

include/swift/AST/DiagnosticConsumer.h

Lines changed: 1 addition & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@
1919
#ifndef SWIFT_BASIC_DIAGNOSTICCONSUMER_H
2020
#define SWIFT_BASIC_DIAGNOSTICCONSUMER_H
2121

22+
#include "swift/AST/DiagnosticKind.h"
2223
#include "swift/Basic/LLVM.h"
2324
#include "swift/Basic/SourceLoc.h"
2425
#include "llvm/Support/SourceMgr.h"
@@ -30,15 +31,6 @@ namespace swift {
3031
class SourceManager;
3132
enum class DiagID : uint32_t;
3233

33-
/// Describes the kind of diagnostic.
34-
///
35-
enum class DiagnosticKind : uint8_t {
36-
Error,
37-
Warning,
38-
Remark,
39-
Note
40-
};
41-
4234
/// Information about a diagnostic passed to DiagnosticConsumers.
4335
struct DiagnosticInfo {
4436
DiagID ID = DiagID(0);

include/swift/AST/DiagnosticKind.h

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
//===-- AST/DiagnosticKind.h ------------------------------------*- C++ -*-===//
2+
//
3+
// This source file is part of the Swift.org open source project
4+
//
5+
// Copyright (c) 2025 Apple Inc. and the Swift project authors
6+
// Licensed under Apache License v2.0 with Runtime Library Exception
7+
//
8+
// See https://swift.org/LICENSE.txt for license information
9+
// See https://swift.org/CONTRIBUTORS.txt for the list of Swift project authors
10+
//
11+
//===----------------------------------------------------------------------===//
12+
///
13+
/// \file
14+
/// This enum lives in its own header so that it can be included in
15+
/// `ASTBridging.h`.
16+
///
17+
//===----------------------------------------------------------------------===//
18+
19+
#ifndef SWIFT_AST_DIAGNOSTIC_KIND_H
20+
#define SWIFT_AST_DIAGNOSTIC_KIND_H
21+
22+
/// Be *very* careful with what you include here! See include caveats in
23+
/// `ASTBridging.h`.
24+
#include "swift/Basic/Compiler.h"
25+
#include "swift/Basic/SwiftBridging.h"
26+
#include <stdint.h>
27+
28+
namespace swift {
29+
30+
/// Describes the kind of diagnostic.
31+
enum class ENUM_EXTENSIBILITY_ATTR(open) DiagnosticKind : uint8_t {
32+
Error SWIFT_NAME("error"),
33+
Warning SWIFT_NAME("warning"),
34+
Remark SWIFT_NAME("remark"),
35+
Note SWIFT_NAME("note")
36+
};
37+
38+
} // namespace swift
39+
40+
#endif // SWIFT_AST_DIAGNOSTIC_KIND_H

include/swift/Bridging/ASTGen.h

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ void swift_ASTGen_addQueuedSourceFile(
2828
void swift_ASTGen_addQueuedDiagnostic(
2929
void *_Nonnull queued, void *_Nonnull state,
3030
BridgedStringRef text,
31-
BridgedDiagnosticSeverity severity,
31+
swift::DiagnosticKind severity,
3232
BridgedSourceLoc sourceLoc,
3333
BridgedStringRef categoryName,
3434
BridgedStringRef documentationPath,
@@ -38,7 +38,7 @@ void swift_ASTGen_addQueuedDiagnostic(
3838
void swift_ASTGen_renderSingleDiagnostic(
3939
void *_Nonnull state,
4040
BridgedStringRef text,
41-
BridgedDiagnosticSeverity severity,
41+
swift::DiagnosticKind severity,
4242
BridgedStringRef categoryName,
4343
BridgedStringRef documentationPath,
4444
ssize_t colorize,

lib/AST/Bridging/DiagnosticsBridging.cpp

Lines changed: 5 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -118,7 +118,7 @@ struct BridgedDiagnostic::Impl {
118118

119119
BridgedDiagnostic BridgedDiagnostic_create(BridgedSourceLoc cLoc,
120120
BridgedStringRef cText,
121-
BridgedDiagnosticSeverity severity,
121+
DiagnosticKind severity,
122122
BridgedDiagnosticEngine cDiags) {
123123
StringRef origText = cText.unbridged();
124124
BridgedDiagnostic::Impl::Allocator alloc;
@@ -128,19 +128,16 @@ BridgedDiagnostic BridgedDiagnostic_create(BridgedSourceLoc cLoc,
128128

129129
Diag<StringRef> diagID;
130130
switch (severity) {
131-
case BridgedDiagnosticSeverity::BridgedError:
131+
case DiagnosticKind::Error:
132132
diagID = diag::bridged_error;
133133
break;
134-
case BridgedDiagnosticSeverity::BridgedFatalError:
135-
diagID = diag::bridged_fatal_error;
136-
break;
137-
case BridgedDiagnosticSeverity::BridgedNote:
134+
case DiagnosticKind::Note:
138135
diagID = diag::bridged_note;
139136
break;
140-
case BridgedDiagnosticSeverity::BridgedRemark:
137+
case DiagnosticKind::Remark:
141138
diagID = diag::bridged_remark;
142139
break;
143-
case BridgedDiagnosticSeverity::BridgedWarning:
140+
case DiagnosticKind::Warning:
144141
diagID = diag::bridged_warning;
145142
break;
146143
}

lib/AST/DiagnosticBridge.cpp

Lines changed: 2 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -26,21 +26,6 @@
2626
using namespace swift;
2727

2828
#if SWIFT_BUILD_SWIFT_SYNTAX
29-
static BridgedDiagnosticSeverity bridgeDiagnosticSeverity(DiagnosticKind kind) {
30-
switch (kind) {
31-
case DiagnosticKind::Error:
32-
return BridgedDiagnosticSeverity::BridgedError;
33-
34-
case DiagnosticKind::Warning:
35-
return BridgedDiagnosticSeverity::BridgedWarning;
36-
37-
case DiagnosticKind::Remark:
38-
return BridgedDiagnosticSeverity::BridgedRemark;
39-
40-
case DiagnosticKind::Note:
41-
return BridgedDiagnosticSeverity::BridgedNote;
42-
}
43-
}
4429

4530
/// Enqueue a diagnostic with ASTGen's diagnostic rendering.
4631
static void addQueueDiagnostic(void *queuedDiagnostics,
@@ -53,8 +38,6 @@ static void addQueueDiagnostic(void *queuedDiagnostics,
5338
info.FormatArgs);
5439
}
5540

56-
BridgedDiagnosticSeverity severity = bridgeDiagnosticSeverity(info.Kind);
57-
5841
// Map the highlight ranges.
5942
SmallVector<BridgedCharSourceRange, 2> highlightRanges;
6043
for (const auto &range : info.Ranges) {
@@ -72,7 +55,7 @@ static void addQueueDiagnostic(void *queuedDiagnostics,
7255
swift_ASTGen_addQueuedDiagnostic(
7356
queuedDiagnostics, perFrontendState,
7457
text.str(),
75-
severity, info.Loc,
58+
info.Kind, info.Loc,
7659
info.Category,
7760
documentationPath,
7861
highlightRanges.data(), highlightRanges.size(),
@@ -102,11 +85,9 @@ void DiagnosticBridge::emitDiagnosticWithoutLocation(
10285
info.FormatArgs);
10386
}
10487

105-
BridgedDiagnosticSeverity severity = bridgeDiagnosticSeverity(info.Kind);
106-
10788
BridgedStringRef bridgedRenderedString{nullptr, 0};
10889
swift_ASTGen_renderSingleDiagnostic(
109-
perFrontendState, text.str(), severity, info.Category,
90+
perFrontendState, text.str(), info.Kind, info.Category,
11091
llvm::StringRef(info.CategoryDocumentationURL), forceColors ? 1 : 0,
11192
&bridgedRenderedString);
11293

lib/ASTGen/Sources/ASTGen/DiagnosticsBridge.swift

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -120,7 +120,7 @@ public func emitDiagnostic(
120120
}
121121

122122
extension DiagnosticSeverity {
123-
public var bridged: BridgedDiagnosticSeverity {
123+
public var bridged: swift.DiagnosticKind {
124124
switch self {
125125
case .error: return .error
126126
case .note: return .note
@@ -177,10 +177,9 @@ fileprivate struct SimpleDiagnostic: DiagnosticMessage {
177177
}
178178
}
179179

180-
extension BridgedDiagnosticSeverity {
180+
extension swift.DiagnosticKind {
181181
var asSeverity: DiagnosticSeverity {
182182
switch self {
183-
case .fatalError: return .error
184183
case .error: return .error
185184
case .warning: return .warning
186185
case .remark: return .remark
@@ -250,7 +249,7 @@ public func addQueuedDiagnostic(
250249
queuedDiagnosticsPtr: UnsafeMutableRawPointer,
251250
perFrontendDiagnosticStatePtr: UnsafeMutableRawPointer,
252251
text: BridgedStringRef,
253-
severity: BridgedDiagnosticSeverity,
252+
severity: swift.DiagnosticKind,
254253
cLoc: BridgedSourceLoc,
255254
categoryName: BridgedStringRef,
256255
documentationPath: BridgedStringRef,
@@ -430,7 +429,7 @@ public func addQueuedDiagnostic(
430429
public func renderSingleDiagnostic(
431430
perFrontendDiagnosticStatePtr: UnsafeMutableRawPointer,
432431
text: BridgedStringRef,
433-
severity: BridgedDiagnosticSeverity,
432+
severity: swift.DiagnosticKind,
434433
categoryName: BridgedStringRef,
435434
documentationPath: BridgedStringRef,
436435
colorize: Int,

lib/ASTGen/Sources/MacroEvaluation/PluginHost.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -262,7 +262,7 @@ class PluginDiagnosticsEngine {
262262
fixItChanges: [PluginMessage.Diagnostic.FixIt.Change] = []
263263
) {
264264
// Map severity
265-
let bridgedSeverity: BridgedDiagnosticSeverity
265+
let bridgedSeverity: swift.DiagnosticKind
266266
switch severity {
267267
case .error: bridgedSeverity = .error
268268
case .note: bridgedSeverity = .note

0 commit comments

Comments
 (0)