Skip to content

Commit af2e962

Browse files
committed
[ASTGen] Avoid including C standard libary headers in brigdging headers
C stdlib headers are part of "Darwin"/"Glibc" clang module. If a Swift file imports a bridging headers and that has '#include' C stdlib headers, Swift compiler implicitly imports "Darwin"/"Glibc" overlay modules. That violates dependency layering. I.e. Compiler depends on Darwin overlay, Darwin overlay is created by the compiler. rdar://107957117
1 parent 2bcef82 commit af2e962

File tree

8 files changed

+94
-89
lines changed

8 files changed

+94
-89
lines changed

include/swift/AST/CASTBridging.h

Lines changed: 22 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -16,10 +16,10 @@
1616
#include "swift/Basic/CBasicBridging.h"
1717
#include "swift/Basic/Compiler.h"
1818

19-
#include <inttypes.h>
20-
#include <stdbool.h>
21-
#include <stddef.h>
22-
#include <stdint.h>
19+
// NOTE: DO NOT #include any stdlib headers here. e.g. <stdint.h>. Those are
20+
// part of "Darwin"/"Glibc" module, so when a Swift file imports this header,
21+
// it causes importing the "Darwin"/"Glibc" overlay module. That violates
22+
// layering. i.e. Darwin overlay is created by Swift compiler.
2323

2424
#if __clang__
2525
// Provide macros to temporarily suppress warning about the use of
@@ -102,7 +102,7 @@ typedef enum ENUM_EXTENSIBILITY_ATTR(open) BridgedDiagnosticSeverity : long {
102102

103103
typedef void* BridgedDiagnostic;
104104

105-
typedef enum ENUM_EXTENSIBILITY_ATTR(open) BridgedMacroDefinitionKind : ptrdiff_t {
105+
typedef enum ENUM_EXTENSIBILITY_ATTR(open) BridgedMacroDefinitionKind : long {
106106
/// An expanded macro.
107107
BridgedExpandedMacro = 0,
108108
/// An external macro, spelled with either the old spelling (Module.Type)
@@ -126,28 +126,29 @@ extern "C" {
126126
///
127127
/// \returns a diagnostic instance that can be extended with additional
128128
/// information and then must be finished via \c SwiftDiagnostic_finish.
129-
BridgedDiagnostic SwiftDiagnostic_create(
130-
void *diagnosticEngine, BridgedDiagnosticSeverity severity,
131-
const void *_Nullable sourceLoc,
132-
const uint8_t *_Nullable text, long textLen);
129+
BridgedDiagnostic SwiftDiagnostic_create(void *diagnosticEngine,
130+
BridgedDiagnosticSeverity severity,
131+
const void *_Nullable sourceLoc,
132+
const unsigned char *_Nullable text,
133+
long textLen);
133134

134135
/// Highlight a source range as part of the diagnostic.
135136
void SwiftDiagnostic_highlight(
136137
BridgedDiagnostic diag, const void *_Nullable startLoc, const void *_Nullable endLoc);
137138

138139
/// Add a Fix-It to replace a source range as part of the diagnostic.
139-
void SwiftDiagnostic_fixItReplace(
140-
BridgedDiagnostic diag,
141-
const void *_Nullable replaceStartLoc,
142-
const void *_Nullable replaceEndLoc,
143-
const uint8_t *_Nullable newText, long newTextLen);
140+
void SwiftDiagnostic_fixItReplace(BridgedDiagnostic diag,
141+
const void *_Nullable replaceStartLoc,
142+
const void *_Nullable replaceEndLoc,
143+
const unsigned char *_Nullable newText,
144+
long newTextLen);
144145

145146
/// Finish the given diagnostic and emit it.
146147
void SwiftDiagnostic_finish(BridgedDiagnostic diag);
147148

148-
BridgedIdentifier SwiftASTContext_getIdentifier(void *ctx,
149-
const uint8_t *_Nullable str,
150-
long len);
149+
BridgedIdentifier
150+
SwiftASTContext_getIdentifier(void *ctx, const unsigned char *_Nullable str,
151+
long len);
151152

152153
void *SwiftImportDecl_create(void *, void *, void *, char, void *,
153154
BridgedArrayRef, BridgedArrayRef);
@@ -170,10 +171,12 @@ void *SwiftFunctionCallExpr_create(void *ctx, void *fn, void *args);
170171

171172
void *SwiftIdentifierExpr_create(void *ctx, BridgedIdentifier base, void *loc);
172173

173-
void *SwiftStringLiteralExpr_create(void *ctx, const uint8_t *_Nullable string,
174+
void *SwiftStringLiteralExpr_create(void *ctx,
175+
const unsigned char *_Nullable string,
174176
long len, void *TokenLoc);
175177

176-
void *SwiftIntegerLiteralExpr_create(void *ctx, const uint8_t *_Nullable string,
178+
void *SwiftIntegerLiteralExpr_create(void *ctx,
179+
const unsigned char *_Nullable string,
177180
long len, void *TokenLoc);
178181

179182
void *SwiftBooleanLiteralExpr_create(void *ctx, _Bool value, void *TokenLoc);

include/swift/Basic/CBasicBridging.h

Lines changed: 27 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -15,10 +15,10 @@
1515

1616
#include "swift/Basic/Compiler.h"
1717

18-
#include <inttypes.h>
19-
#include <stdbool.h>
20-
#include <stddef.h>
21-
#include <stdint.h>
18+
// NOTE: DO NOT #include any stdlib headers here. e.g. <stdint.h>. Those are
19+
// part of "Darwin"/"Glibc" module, so when a Swift file imports this header,
20+
// it causes importing the "Darwin"/"Glibc" overlay module. That violates
21+
// layering. i.e. Darwin overlay is created by Swift compiler.
2222

2323
#if __clang__
2424
// Provide macros to temporarily suppress warning about the use of
@@ -41,11 +41,14 @@ SWIFT_BEGIN_NULLABILITY_ANNOTATIONS
4141

4242
#ifdef __cplusplus
4343
extern "C" {
44+
45+
#define _Bool bool
46+
4447
#endif
4548

4649
typedef struct BridgedData {
4750
const char *_Nullable baseAddress;
48-
size_t size;
51+
unsigned long size;
4952
} BridgedData;
5053

5154
void BridgedData_free(BridgedData data);
@@ -70,44 +73,44 @@ void JSON_value_serialize(void *valuePtr, BridgedData *result);
7073
/// \c JSON_newValue() or \c JSON_deserializedValue() .
7174
void JSON_value_delete(void *valuePtr);
7275

73-
bool JSON_value_getAsNull(void *valuePtr);
74-
bool JSON_value_getAsBoolean(void *valuePtr, bool *result);
75-
bool JSON_value_getAsString(void *valuePtr, BridgedData *result);
76-
bool JSON_value_getAsDouble(void *valuePtr, double *result);
77-
bool JSON_value_getAsInteger(void *valuePtr, int64_t *result);
78-
bool JSON_value_getAsObject(void *valuePtr, void *_Nullable *_Nonnull result);
79-
bool JSON_value_getAsArray(void *valuePtr, void *_Nullable *_Nonnull result);
80-
81-
size_t JSON_object_getSize(void *objectPtr);
82-
BridgedData JSON_object_getKey(void *objectPtr, size_t i);
83-
bool JSON_object_hasKey(void *objectPtr, const char *key);
76+
_Bool JSON_value_getAsNull(void *valuePtr);
77+
_Bool JSON_value_getAsBoolean(void *valuePtr, _Bool *result);
78+
_Bool JSON_value_getAsString(void *valuePtr, BridgedData *result);
79+
_Bool JSON_value_getAsDouble(void *valuePtr, double *result);
80+
_Bool JSON_value_getAsInteger(void *valuePtr, long long *result);
81+
_Bool JSON_value_getAsObject(void *valuePtr, void *_Nullable *_Nonnull result);
82+
_Bool JSON_value_getAsArray(void *valuePtr, void *_Nullable *_Nonnull result);
83+
84+
unsigned long JSON_object_getSize(void *objectPtr);
85+
BridgedData JSON_object_getKey(void *objectPtr, unsigned long i);
86+
_Bool JSON_object_hasKey(void *objectPtr, const char *key);
8487
void *JSON_object_getValue(void *objectPtr, const char *key);
8588

86-
int64_t JSON_array_getSize(void *arrayPtr);
87-
void *JSON_array_getValue(void *arrayPtr, int64_t index);
89+
long long JSON_array_getSize(void *arrayPtr);
90+
void *JSON_array_getValue(void *arrayPtr, long long index);
8891

8992
void JSON_value_emplaceNull(void *valuePtr);
90-
void JSON_value_emplaceBoolean(void *valuePtr, bool value);
93+
void JSON_value_emplaceBoolean(void *valuePtr, _Bool value);
9194
void JSON_value_emplaceString(void *valuePtr, const char *value);
9295
void JSON_value_emplaceDouble(void *valuePtr, double value);
93-
void JSON_value_emplaceInteger(void *valuePtr, int64_t value);
96+
void JSON_value_emplaceInteger(void *valuePtr, long long value);
9497
void *JSON_value_emplaceNewObject(void *valuePtr);
9598
void *JSON_value_emplaceNewArray(void *valuePtr);
9699

97100
void JSON_object_setNull(void *objectPtr, const char *key);
98-
void JSON_object_setBoolean(void *objectPtr, const char *key, bool value);
101+
void JSON_object_setBoolean(void *objectPtr, const char *key, _Bool value);
99102
void JSON_object_setString(void *objectPtr, const char *key, const char *value);
100103
void JSON_object_setDouble(void *objectPtr, const char *key, double value);
101-
void JSON_object_setInteger(void *objectPtr, const char *key, int64_t value);
104+
void JSON_object_setInteger(void *objectPtr, const char *key, long long value);
102105
void *JSON_object_setNewObject(void *objectPtr, const char *key);
103106
void *JSON_object_setNewArray(void *objectPtr, const char *key);
104107
void *JSON_object_setNewValue(void *objectPtr, const char *key);
105108

106109
void JSON_array_pushNull(void *arrayPtr);
107-
void JSON_array_pushBoolean(void *arrayPtr, bool value);
110+
void JSON_array_pushBoolean(void *arrayPtr, _Bool value);
108111
void JSON_array_pushString(void *arrayPtr, const char *value);
109112
void JSON_array_pushDouble(void *arrayPtr, double value);
110-
void JSON_array_pushInteger(void *arrayPtr, int64_t value);
113+
void JSON_array_pushInteger(void *arrayPtr, long long value);
111114
void *JSON_array_pushNewObject(void *arrayPtr);
112115
void *JSON_array_pushNewArray(void *arrayPtr);
113116
void *JSON_array_pushNewValue(void *arrayPtr);

lib/AST/CASTBridging.cpp

Lines changed: 17 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -44,11 +44,11 @@ namespace {
4444
};
4545
}
4646

47-
BridgedDiagnostic SwiftDiagnostic_create(
48-
void *diagnosticEngine, BridgedDiagnosticSeverity severity,
49-
const void *sourceLocPtr,
50-
const uint8_t *textPtr, long textLen
51-
) {
47+
BridgedDiagnostic SwiftDiagnostic_create(void *diagnosticEngine,
48+
BridgedDiagnosticSeverity severity,
49+
const void *sourceLocPtr,
50+
const unsigned char *textPtr,
51+
long textLen) {
5252
StringRef origText{
5353
reinterpret_cast<const char *>(textPtr), size_t(textLen)};
5454
llvm::MallocAllocator mallocAlloc;
@@ -91,10 +91,11 @@ void SwiftDiagnostic_highlight(
9191
}
9292

9393
/// Add a Fix-It to replace a source range as part of the diagnostic.
94-
void SwiftDiagnostic_fixItReplace(
95-
BridgedDiagnostic diagPtr,
96-
const void *replaceStartLocPtr, const void *replaceEndLocPtr,
97-
const uint8_t *newTextPtr, long newTextLen) {
94+
void SwiftDiagnostic_fixItReplace(BridgedDiagnostic diagPtr,
95+
const void *replaceStartLocPtr,
96+
const void *replaceEndLocPtr,
97+
const unsigned char *newTextPtr,
98+
long newTextLen) {
9899

99100
SourceLoc startLoc = getSourceLocFromPointer(replaceStartLocPtr);
100101
SourceLoc endLoc = getSourceLocFromPointer(replaceEndLocPtr);
@@ -115,9 +116,9 @@ void SwiftDiagnostic_finish(BridgedDiagnostic diagPtr) {
115116
delete impl;
116117
}
117118

118-
BridgedIdentifier SwiftASTContext_getIdentifier(void *ctx,
119-
const uint8_t *_Nullable str,
120-
long len) {
119+
BridgedIdentifier
120+
SwiftASTContext_getIdentifier(void *ctx, const unsigned char *_Nullable str,
121+
long len) {
121122
return const_cast<void *>(
122123
static_cast<ASTContext *>(ctx)
123124
->getIdentifier(
@@ -213,7 +214,8 @@ void *SwiftIdentifierExpr_create(void *ctx, BridgedIdentifier base, void *loc) {
213214
return E;
214215
}
215216

216-
void *SwiftStringLiteralExpr_create(void *ctx, const uint8_t *_Nullable string,
217+
void *SwiftStringLiteralExpr_create(void *ctx,
218+
const unsigned char *_Nullable string,
217219
long len, void *TokenLoc) {
218220
ASTContext &Context = *static_cast<ASTContext *>(ctx);
219221
auto stringRef = Context.AllocateCopy(
@@ -222,7 +224,8 @@ void *SwiftStringLiteralExpr_create(void *ctx, const uint8_t *_Nullable string,
222224
StringLiteralExpr(stringRef, getSourceLocFromPointer(TokenLoc));
223225
}
224226

225-
void *SwiftIntegerLiteralExpr_create(void *ctx, const uint8_t *_Nullable string,
227+
void *SwiftIntegerLiteralExpr_create(void *ctx,
228+
const unsigned char *_Nullable string,
226229
long len, void *TokenLoc) {
227230
ASTContext &Context = *static_cast<ASTContext *>(ctx);
228231
auto stringRef = Context.AllocateCopy(

lib/ASTGen/Sources/ASTGen/PluginHost.swift

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -103,7 +103,7 @@ struct CompilerPlugin {
103103

104104
private func sendMessage(_ message: HostToPluginMessage) throws {
105105
let hadError = try LLVMJSON.encoding(message) { (data) -> Bool in
106-
return Plugin_sendMessage(opaqueHandle, BridgedData(baseAddress: data.baseAddress, size: data.count))
106+
return Plugin_sendMessage(opaqueHandle, BridgedData(baseAddress: data.baseAddress, size: UInt(data.count)))
107107
}
108108
if hadError {
109109
throw PluginError.failedToSendMessage
@@ -117,7 +117,7 @@ struct CompilerPlugin {
117117
guard !hadError else {
118118
throw PluginError.failedToReceiveMessage
119119
}
120-
let data = UnsafeBufferPointer(start: result.baseAddress, count: result.size)
120+
let data = UnsafeBufferPointer(start: result.baseAddress, count: Int(result.size))
121121
return try LLVMJSON.decode(PluginToHostMessage.self, from: data)
122122
}
123123

lib/ASTGen/Sources/LLVMJSON/LLVMJSON.swift

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ import CBasicBridging
1414

1515
extension String {
1616
init(_ data: BridgedData) {
17-
let buffer = UnsafeBufferPointer(start: data.baseAddress, count: data.size)
17+
let buffer = UnsafeBufferPointer(start: data.baseAddress, count: Int(data.size))
1818
self = buffer.withMemoryRebound(to: UInt8.self) { buffer in
1919
String(decoding: buffer, as: UTF8.self)
2020
}
@@ -35,13 +35,13 @@ public struct LLVMJSON {
3535
JSON_value_serialize(valuePtr, &data)
3636
assert(data.baseAddress != nil)
3737
defer { BridgedData_free(data) }
38-
let buffer = UnsafeBufferPointer(start: data.baseAddress, count: data.size)
38+
let buffer = UnsafeBufferPointer(start: data.baseAddress, count: Int(data.size))
3939
return try body(buffer)
4040
}
4141

4242
/// Decode a JSON data to a Swift value.
4343
public static func decode<T: Decodable>(_ type: T.Type, from json: UnsafeBufferPointer<Int8>) throws -> T {
44-
let data = BridgedData(baseAddress: json.baseAddress, size: json.count)
44+
let data = BridgedData(baseAddress: json.baseAddress, size: UInt(json.count))
4545
let valuePtr = JSON_deserializedValue(data)
4646
defer { JSON_value_delete(valuePtr) }
4747

@@ -209,7 +209,7 @@ extension LLVMJSONDecoding.KeyedContainer: KeyedDecodingContainerProtocol {
209209
var allKeys: [Key] {
210210
var keys: [Key] = []
211211
let size = JSON_object_getSize(objectPtr)
212-
keys.reserveCapacity(size)
212+
keys.reserveCapacity(Int(size))
213213
for i in 0 ..< size {
214214
let keyData = JSON_object_getKey(objectPtr, i)
215215
if let key = Key(stringValue: String(keyData)) {

0 commit comments

Comments
 (0)