Skip to content

[ASTGen] Avoid including C standard libary headers in brigdging headers #65120

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 1 commit into from
Apr 13, 2023
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
41 changes: 22 additions & 19 deletions include/swift/AST/CASTBridging.h
Original file line number Diff line number Diff line change
Expand Up @@ -16,10 +16,10 @@
#include "swift/Basic/CBasicBridging.h"
#include "swift/Basic/Compiler.h"

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

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

typedef void* BridgedDiagnostic;

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

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

/// Add a Fix-It to replace a source range as part of the diagnostic.
void SwiftDiagnostic_fixItReplace(
BridgedDiagnostic diag,
const void *_Nullable replaceStartLoc,
const void *_Nullable replaceEndLoc,
const uint8_t *_Nullable newText, long newTextLen);
void SwiftDiagnostic_fixItReplace(BridgedDiagnostic diag,
const void *_Nullable replaceStartLoc,
const void *_Nullable replaceEndLoc,
const unsigned char *_Nullable newText,
long newTextLen);

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

BridgedIdentifier SwiftASTContext_getIdentifier(void *ctx,
const uint8_t *_Nullable str,
long len);
BridgedIdentifier
SwiftASTContext_getIdentifier(void *ctx, const unsigned char *_Nullable str,
long len);

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

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

void *SwiftStringLiteralExpr_create(void *ctx, const uint8_t *_Nullable string,
void *SwiftStringLiteralExpr_create(void *ctx,
const unsigned char *_Nullable string,
long len, void *TokenLoc);

void *SwiftIntegerLiteralExpr_create(void *ctx, const uint8_t *_Nullable string,
void *SwiftIntegerLiteralExpr_create(void *ctx,
const unsigned char *_Nullable string,
long len, void *TokenLoc);

void *SwiftBooleanLiteralExpr_create(void *ctx, _Bool value, void *TokenLoc);
Expand Down
51 changes: 27 additions & 24 deletions include/swift/Basic/CBasicBridging.h
Original file line number Diff line number Diff line change
Expand Up @@ -15,10 +15,10 @@

#include "swift/Basic/Compiler.h"

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

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

#ifdef __cplusplus
extern "C" {

#define _Bool bool

#endif

typedef struct BridgedData {
const char *_Nullable baseAddress;
size_t size;
unsigned long size;
} BridgedData;

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

bool JSON_value_getAsNull(void *valuePtr);
bool JSON_value_getAsBoolean(void *valuePtr, bool *result);
bool JSON_value_getAsString(void *valuePtr, BridgedData *result);
bool JSON_value_getAsDouble(void *valuePtr, double *result);
bool JSON_value_getAsInteger(void *valuePtr, int64_t *result);
bool JSON_value_getAsObject(void *valuePtr, void *_Nullable *_Nonnull result);
bool JSON_value_getAsArray(void *valuePtr, void *_Nullable *_Nonnull result);

size_t JSON_object_getSize(void *objectPtr);
BridgedData JSON_object_getKey(void *objectPtr, size_t i);
bool JSON_object_hasKey(void *objectPtr, const char *key);
_Bool JSON_value_getAsNull(void *valuePtr);
_Bool JSON_value_getAsBoolean(void *valuePtr, _Bool *result);
_Bool JSON_value_getAsString(void *valuePtr, BridgedData *result);
_Bool JSON_value_getAsDouble(void *valuePtr, double *result);
_Bool JSON_value_getAsInteger(void *valuePtr, long long *result);
_Bool JSON_value_getAsObject(void *valuePtr, void *_Nullable *_Nonnull result);
_Bool JSON_value_getAsArray(void *valuePtr, void *_Nullable *_Nonnull result);

unsigned long JSON_object_getSize(void *objectPtr);
BridgedData JSON_object_getKey(void *objectPtr, unsigned long i);
_Bool JSON_object_hasKey(void *objectPtr, const char *key);
void *JSON_object_getValue(void *objectPtr, const char *key);

int64_t JSON_array_getSize(void *arrayPtr);
void *JSON_array_getValue(void *arrayPtr, int64_t index);
long long JSON_array_getSize(void *arrayPtr);
void *JSON_array_getValue(void *arrayPtr, long long index);

void JSON_value_emplaceNull(void *valuePtr);
void JSON_value_emplaceBoolean(void *valuePtr, bool value);
void JSON_value_emplaceBoolean(void *valuePtr, _Bool value);
void JSON_value_emplaceString(void *valuePtr, const char *value);
void JSON_value_emplaceDouble(void *valuePtr, double value);
void JSON_value_emplaceInteger(void *valuePtr, int64_t value);
void JSON_value_emplaceInteger(void *valuePtr, long long value);
void *JSON_value_emplaceNewObject(void *valuePtr);
void *JSON_value_emplaceNewArray(void *valuePtr);

void JSON_object_setNull(void *objectPtr, const char *key);
void JSON_object_setBoolean(void *objectPtr, const char *key, bool value);
void JSON_object_setBoolean(void *objectPtr, const char *key, _Bool value);
void JSON_object_setString(void *objectPtr, const char *key, const char *value);
void JSON_object_setDouble(void *objectPtr, const char *key, double value);
void JSON_object_setInteger(void *objectPtr, const char *key, int64_t value);
void JSON_object_setInteger(void *objectPtr, const char *key, long long value);
void *JSON_object_setNewObject(void *objectPtr, const char *key);
void *JSON_object_setNewArray(void *objectPtr, const char *key);
void *JSON_object_setNewValue(void *objectPtr, const char *key);

void JSON_array_pushNull(void *arrayPtr);
void JSON_array_pushBoolean(void *arrayPtr, bool value);
void JSON_array_pushBoolean(void *arrayPtr, _Bool value);
void JSON_array_pushString(void *arrayPtr, const char *value);
void JSON_array_pushDouble(void *arrayPtr, double value);
void JSON_array_pushInteger(void *arrayPtr, int64_t value);
void JSON_array_pushInteger(void *arrayPtr, long long value);
void *JSON_array_pushNewObject(void *arrayPtr);
void *JSON_array_pushNewArray(void *arrayPtr);
void *JSON_array_pushNewValue(void *arrayPtr);
Expand Down
33 changes: 18 additions & 15 deletions lib/AST/CASTBridging.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -44,11 +44,11 @@ namespace {
};
}

BridgedDiagnostic SwiftDiagnostic_create(
void *diagnosticEngine, BridgedDiagnosticSeverity severity,
const void *sourceLocPtr,
const uint8_t *textPtr, long textLen
) {
BridgedDiagnostic SwiftDiagnostic_create(void *diagnosticEngine,
BridgedDiagnosticSeverity severity,
const void *sourceLocPtr,
const unsigned char *textPtr,
long textLen) {
StringRef origText{
reinterpret_cast<const char *>(textPtr), size_t(textLen)};
llvm::MallocAllocator mallocAlloc;
Expand Down Expand Up @@ -91,10 +91,11 @@ void SwiftDiagnostic_highlight(
}

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

SourceLoc startLoc = getSourceLocFromPointer(replaceStartLocPtr);
SourceLoc endLoc = getSourceLocFromPointer(replaceEndLocPtr);
Expand All @@ -115,9 +116,9 @@ void SwiftDiagnostic_finish(BridgedDiagnostic diagPtr) {
delete impl;
}

BridgedIdentifier SwiftASTContext_getIdentifier(void *ctx,
const uint8_t *_Nullable str,
long len) {
BridgedIdentifier
SwiftASTContext_getIdentifier(void *ctx, const unsigned char *_Nullable str,
long len) {
return const_cast<void *>(
static_cast<ASTContext *>(ctx)
->getIdentifier(
Expand Down Expand Up @@ -213,7 +214,8 @@ void *SwiftIdentifierExpr_create(void *ctx, BridgedIdentifier base, void *loc) {
return E;
}

void *SwiftStringLiteralExpr_create(void *ctx, const uint8_t *_Nullable string,
void *SwiftStringLiteralExpr_create(void *ctx,
const unsigned char *_Nullable string,
long len, void *TokenLoc) {
ASTContext &Context = *static_cast<ASTContext *>(ctx);
auto stringRef = Context.AllocateCopy(
Expand All @@ -222,7 +224,8 @@ void *SwiftStringLiteralExpr_create(void *ctx, const uint8_t *_Nullable string,
StringLiteralExpr(stringRef, getSourceLocFromPointer(TokenLoc));
}

void *SwiftIntegerLiteralExpr_create(void *ctx, const uint8_t *_Nullable string,
void *SwiftIntegerLiteralExpr_create(void *ctx,
const unsigned char *_Nullable string,
long len, void *TokenLoc) {
ASTContext &Context = *static_cast<ASTContext *>(ctx);
auto stringRef = Context.AllocateCopy(
Expand Down Expand Up @@ -687,6 +690,6 @@ bool Plugin_waitForNextMessage(PluginHandle handle, BridgedData *out) {
auto size = message.size();
auto outPtr = malloc(size);
memcpy(outPtr, message.data(), size);
*out = BridgedData{(const char *)outPtr, size};
*out = BridgedData{(const char *)outPtr, (unsigned long)size};
return false;
}
4 changes: 2 additions & 2 deletions lib/ASTGen/Sources/ASTGen/PluginHost.swift
Original file line number Diff line number Diff line change
Expand Up @@ -103,7 +103,7 @@ struct CompilerPlugin {

private func sendMessage(_ message: HostToPluginMessage) throws {
let hadError = try LLVMJSON.encoding(message) { (data) -> Bool in
return Plugin_sendMessage(opaqueHandle, BridgedData(baseAddress: data.baseAddress, size: data.count))
return Plugin_sendMessage(opaqueHandle, BridgedData(baseAddress: data.baseAddress, size: UInt(data.count)))
}
if hadError {
throw PluginError.failedToSendMessage
Expand All @@ -117,7 +117,7 @@ struct CompilerPlugin {
guard !hadError else {
throw PluginError.failedToReceiveMessage
}
let data = UnsafeBufferPointer(start: result.baseAddress, count: result.size)
let data = UnsafeBufferPointer(start: result.baseAddress, count: Int(result.size))
return try LLVMJSON.decode(PluginToHostMessage.self, from: data)
}

Expand Down
8 changes: 4 additions & 4 deletions lib/ASTGen/Sources/LLVMJSON/LLVMJSON.swift
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ import CBasicBridging

extension String {
init(_ data: BridgedData) {
let buffer = UnsafeBufferPointer(start: data.baseAddress, count: data.size)
let buffer = UnsafeBufferPointer(start: data.baseAddress, count: Int(data.size))
self = buffer.withMemoryRebound(to: UInt8.self) { buffer in
String(decoding: buffer, as: UTF8.self)
}
Expand All @@ -35,13 +35,13 @@ public struct LLVMJSON {
JSON_value_serialize(valuePtr, &data)
assert(data.baseAddress != nil)
defer { BridgedData_free(data) }
let buffer = UnsafeBufferPointer(start: data.baseAddress, count: data.size)
let buffer = UnsafeBufferPointer(start: data.baseAddress, count: Int(data.size))
return try body(buffer)
}

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

Expand Down Expand Up @@ -209,7 +209,7 @@ extension LLVMJSONDecoding.KeyedContainer: KeyedDecodingContainerProtocol {
var allKeys: [Key] {
var keys: [Key] = []
let size = JSON_object_getSize(objectPtr)
keys.reserveCapacity(size)
keys.reserveCapacity(Int(size))
for i in 0 ..< size {
let keyData = JSON_object_getKey(objectPtr, i)
if let key = Key(stringValue: String(keyData)) {
Expand Down
Loading