Skip to content

[cxx-interop][SwiftCompilerSources] Use swift::SourceLoc instead of BridgedSourceLoc #58699

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 2 commits into from
Jun 22, 2022
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
4 changes: 4 additions & 0 deletions SwiftCompilerSources/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -165,6 +165,10 @@ function(add_swift_compiler_modules_library name)
"-emit-module-path" "${build_dir}/${module}.swiftmodule"
"-parse-as-library" ${sources}
"-wmo" ${swift_compile_options}
# LLVM modules and headers.
"-Xcc" "-I" "-Xcc" "${LLVM_MAIN_INCLUDE_DIR}"
# Generated LLVM headers.
"-Xcc" "-I" "-Xcc" "${LLVM_INCLUDE_DIR}"
# Bridging modules and headers.
"-Xcc" "-I" "-Xcc" "${SWIFT_SOURCE_DIR}/include"
# Generated C headers.
Expand Down
2 changes: 2 additions & 0 deletions SwiftCompilerSources/Package.swift
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,8 @@ private extension Target {
"-Xfrontend", "-enable-cxx-interop",
// Bridging modules and headers
"-Xcc", "-I", "-Xcc", "../include",
// LLVM modules and headers
"-Xcc", "-I", "-Xcc", "../../llvm-project/llvm/include",
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I also added the LLVM include paths to the SwiftPM manifest.

"-cross-module-optimization"
]),
]
Expand Down
2 changes: 1 addition & 1 deletion SwiftCompilerSources/Sources/AST/DiagnosticEngine.swift
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,7 @@ public struct DiagnosticEngine {
highlight: CharSourceRange? = nil,
fixIts: [DiagnosticFixIt] = []) {

let bridgedSourceLoc: BridgedSourceLoc = position.bridged
let bridgedSourceLoc: swift.SourceLoc = position.bridged
let bridgedHighlightRange: BridgedCharSourceRange = highlight.bridged
var bridgedArgs: [BridgedDiagnosticArgument] = []
var bridgedFixIts: [BridgedDiagnosticFixIt] = []
Expand Down
16 changes: 8 additions & 8 deletions SwiftCompilerSources/Sources/Basic/SourceLoc.swift
Original file line number Diff line number Diff line change
Expand Up @@ -21,15 +21,15 @@ public struct SourceLoc {
self.locationInFile = locationInFile
}

public init?(bridged: BridgedSourceLoc) {
guard let locationInFile = bridged.pointer else {
public init?(bridged: swift.SourceLoc) {
guard bridged.isValid() else {
return nil
}
self.init(locationInFile: locationInFile)
self.locationInFile = bridged.getOpaquePointerValue().assumingMemoryBound(to: UInt8.self)
}

public var bridged: BridgedSourceLoc {
.init(pointer: locationInFile)
public var bridged: swift.SourceLoc {
.init(llvm.SMLoc.getFromPointer(locationInFile))
}
}

Expand All @@ -40,8 +40,8 @@ extension SourceLoc {
}

extension Optional where Wrapped == SourceLoc {
public var bridged: BridgedSourceLoc {
self?.bridged ?? .init(pointer: nil)
public var bridged: swift.SourceLoc {
self?.bridged ?? .init()
}
}

Expand All @@ -68,6 +68,6 @@ public struct CharSourceRange {

extension Optional where Wrapped == CharSourceRange {
public var bridged: BridgedCharSourceRange {
self?.bridged ?? .init(start: .init(pointer: nil), byteLength: 0)
self?.bridged ?? .init(start: .init(), byteLength: 0)
}
}
2 changes: 1 addition & 1 deletion SwiftCompilerSources/Sources/Parse/Regex.swift
Original file line number Diff line number Diff line change
Expand Up @@ -92,7 +92,7 @@ public func _RegexLiteralParsingFn(
_ versionOut: UnsafeMutablePointer<CUnsignedInt>,
_ captureStructureOut: UnsafeMutableRawPointer,
_ captureStructureSize: CUnsignedInt,
_ bridgedDiagnosticBaseLoc: BridgedSourceLoc,
_ bridgedDiagnosticBaseLoc: swift.SourceLoc,
_ bridgedDiagnosticEngine: BridgedDiagnosticEngine
) -> Bool {
let str = String(cString: inputPtr)
Expand Down
12 changes: 2 additions & 10 deletions include/swift/AST/ASTBridging.h
Original file line number Diff line number Diff line change
Expand Up @@ -18,10 +18,6 @@
#include <stdbool.h>
#include <stddef.h>

#ifdef __cplusplus
extern "C" {
#endif

SWIFT_BEGIN_NULLABILITY_ANNOTATIONS

//===----------------------------------------------------------------------===//
Expand Down Expand Up @@ -56,7 +52,7 @@ typedef struct {
} BridgedDiagnosticArgument;

typedef struct {
BridgedSourceLoc start;
swift::SourceLoc start;
SwiftInt byteLength;
BridgedStringRef text;
} BridgedDiagnosticFixIt;
Expand All @@ -70,7 +66,7 @@ typedef struct {
} BridgedOptionalDiagnosticEngine;

// FIXME: Can we bridge InFlightDiagnostic?
void DiagnosticEngine_diagnose(BridgedDiagnosticEngine, BridgedSourceLoc loc,
void DiagnosticEngine_diagnose(BridgedDiagnosticEngine, swift::SourceLoc loc,
BridgedDiagID diagID, BridgedArrayRef arguments,
BridgedCharSourceRange highlight,
BridgedArrayRef fixIts);
Expand All @@ -79,8 +75,4 @@ bool DiagnosticEngine_hadAnyError(BridgedDiagnosticEngine);

SWIFT_END_NULLABILITY_ANNOTATIONS

#ifdef __cplusplus
} // extern "C"
#endif

#endif // SWIFT_AST_ASTBRIDGING_H
15 changes: 2 additions & 13 deletions include/swift/Basic/BasicBridging.h
Original file line number Diff line number Diff line change
Expand Up @@ -14,12 +14,9 @@
#define SWIFT_BASIC_BASICBRIDGING_H

#include "swift/Basic/BridgedSwiftObject.h"
#include "swift/Basic/SourceLoc.h"
#include <stddef.h>

#ifdef __cplusplus
extern "C" {
#endif

SWIFT_BEGIN_NULLABILITY_ANNOTATIONS

typedef intptr_t SwiftInt;
Expand Down Expand Up @@ -48,18 +45,10 @@ void freeBridgedStringRef(BridgedStringRef str);
//===----------------------------------------------------------------------===//

typedef struct {
const unsigned char * _Nullable pointer;
} BridgedSourceLoc;

typedef struct {
BridgedSourceLoc start;
swift::SourceLoc start;
SwiftInt byteLength;
} BridgedCharSourceRange;

SWIFT_END_NULLABILITY_ANNOTATIONS

#ifdef __cplusplus
} // extern "C"
#endif

#endif
15 changes: 2 additions & 13 deletions include/swift/Basic/BridgingUtils.h
Original file line number Diff line number Diff line change
Expand Up @@ -36,25 +36,14 @@ inline llvm::ArrayRef<T> getArrayRef(BridgedArrayRef bridged) {
return {static_cast<const T *>(bridged.data), bridged.numElements};
}

inline SourceLoc getSourceLoc(const BridgedSourceLoc &bridged) {
return SourceLoc(
llvm::SMLoc::getFromPointer((const char *)bridged.pointer));
}

inline BridgedSourceLoc getBridgedSourceLoc(const SourceLoc &loc) {
return {static_cast<const unsigned char *>(loc.getOpaquePointerValue())};
}

inline CharSourceRange
getCharSourceRange(const BridgedCharSourceRange &bridged) {
auto start = getSourceLoc(bridged.start);
return CharSourceRange(start, bridged.byteLength);
return CharSourceRange(bridged.start, bridged.byteLength);
}

inline BridgedCharSourceRange
getBridgedCharSourceRange(const CharSourceRange &range) {
auto start = getBridgedSourceLoc(range.getStart());
return {start, range.getByteLength()};
return {range.getStart(), range.getByteLength()};
}

/// Copies the string in an malloc'ed memory and the caller is responsible for
Expand Down
10 changes: 1 addition & 9 deletions include/swift/Parse/RegexParserBridging.h
Original file line number Diff line number Diff line change
Expand Up @@ -17,10 +17,6 @@
#include "swift/AST/ASTBridging.h"
#include <stdbool.h>

#ifdef __cplusplus
extern "C" {
#endif

/// Attempt to lex a regex literal string. Takes the following arguments:
///
/// - CurPtrPtr: A pointer to the current pointer of lexer, which should be the
Expand Down Expand Up @@ -60,12 +56,8 @@ typedef bool (*RegexLiteralParsingFn)(/*InputPtr*/ const char *_Nonnull,
/*VersionOut*/ unsigned *_Nonnull,
/*CaptureStructureOut*/ void *_Nonnull,
/*CaptureStructureSize*/ unsigned,
/*DiagnosticBaseLoc*/ BridgedSourceLoc,
/*DiagnosticBaseLoc*/ swift::SourceLoc,
BridgedDiagnosticEngine);
void Parser_registerRegexLiteralParsingFn(RegexLiteralParsingFn _Nullable fn);

#ifdef __cplusplus
} // extern "C"
#endif

#endif // REGEX_PARSER_BRIDGING
5 changes: 2 additions & 3 deletions lib/AST/ASTBridging.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -38,14 +38,13 @@ getDiagnosticArgument(const BridgedDiagnosticArgument &bridged) {
} // namespace

void DiagnosticEngine_diagnose(
BridgedDiagnosticEngine bridgedEngine, BridgedSourceLoc bridgedLoc,
BridgedDiagnosticEngine bridgedEngine, SourceLoc loc,
BridgedDiagID bridgedDiagID,
BridgedArrayRef /*BridgedDiagnosticArgument*/ bridgedArguments,
BridgedCharSourceRange bridgedHighlight,
BridgedArrayRef /*BridgedDiagnosticFixIt*/ bridgedFixIts) {
auto *D = getDiagnosticEngine(bridgedEngine);

auto loc = getSourceLoc(bridgedLoc);
auto diagID = static_cast<DiagID>(bridgedDiagID);
SmallVector<DiagnosticArgument, 2> arguments;
for (auto bridgedArg :
Expand All @@ -62,7 +61,7 @@ void DiagnosticEngine_diagnose(

// Add fix-its.
for (auto bridgedFixIt : getArrayRef<BridgedDiagnosticFixIt>(bridgedFixIts)) {
auto range = CharSourceRange(getSourceLoc(bridgedFixIt.start),
auto range = CharSourceRange(bridgedFixIt.start,
bridgedFixIt.byteLength);
auto text = getStringRef(bridgedFixIt.text);
inflight.fixItReplaceChars(range.getStart(), range.getEnd(), text);
Expand Down
2 changes: 1 addition & 1 deletion lib/Parse/ParseRegex.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ ParserResult<Expr> Parser::parseExprRegexLiteral() {
regexLiteralParsingFn(regexText.str().c_str(), &version,
/*captureStructureOut*/ capturesBuf.data(),
/*captureStructureSize*/ capturesBuf.size(),
/*diagBaseLoc*/ getBridgedSourceLoc(Tok.getLoc()),
/*diagBaseLoc*/ Tok.getLoc(),
getBridgedDiagnosticEngine(&Diags));
auto loc = consumeToken();
SourceMgr.recordRegexLiteralStartLoc(loc);
Expand Down