Skip to content

Commit 9979bb0

Browse files
committed
[Macros] Pass evaluated macro source back into the C++ part of the compiler.
Use this to print out the rewritten macro, for now.
1 parent 636e6d1 commit 9979bb0

File tree

2 files changed

+36
-9
lines changed

2 files changed

+36
-9
lines changed

lib/ASTGen/Sources/ASTGen/Macros.swift

Lines changed: 21 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -20,11 +20,17 @@ extension SyntaxProtocol {
2020
}
2121
}
2222

23-
@_cdecl("swift_ASTGen_printMacroResult")
24-
public func printMacroResult(
25-
sourceFilePtr: UnsafeRawPointer,
26-
sourceLocationPtr: UnsafePointer<UInt8>?
23+
@_cdecl("swift_ASTGen_evaluateMacro")
24+
public func evaluateMacro(
25+
sourceFilePtr: UnsafePointer<UInt8>,
26+
sourceLocationPtr: UnsafePointer<UInt8>?,
27+
expandedSourcePointer: UnsafeMutablePointer<UnsafePointer<UInt8>?>,
28+
expandedSourceLength: UnsafeMutablePointer<Int>
2729
) -> Int {
30+
// We didn't expand anything so far.
31+
expandedSourcePointer.pointee = nil
32+
expandedSourceLength.pointee = 0
33+
2834
guard let sourceLocationPtr = sourceLocationPtr else {
2935
print("NULL source location")
3036
return -1
@@ -68,7 +74,17 @@ public func printMacroResult(
6874
/* TODO: Report errors */
6975
}
7076

71-
print("Macro rewrite: \(parentSyntax.withoutTrivia()) --> \(evaluatedSyntax.withoutTrivia())")
77+
var evaluatedSyntaxStr = evaluatedSyntax.withoutTrivia().description
78+
evaluatedSyntaxStr.withUTF8 { utf8 in
79+
let evaluatedResultPtr = UnsafeMutablePointer<UInt8>.allocate(capacity: utf8.count + 1)
80+
if let baseAddress = utf8.baseAddress {
81+
evaluatedResultPtr.initialize(from: baseAddress, count: utf8.count)
82+
}
83+
evaluatedResultPtr[utf8.count] = 0
84+
85+
expandedSourcePointer.pointee = UnsafePointer(evaluatedResultPtr)
86+
expandedSourceLength.pointee = utf8.count + 1
87+
}
7288

7389
return 0
7490
}

lib/Sema/CSApply.cpp

Lines changed: 15 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -54,8 +54,9 @@
5454
using namespace swift;
5555
using namespace constraints;
5656

57-
extern "C" ptrdiff_t swift_ASTGen_printMacroResult(
58-
void *sourceFile, const void *sourceLocation);
57+
extern "C" ptrdiff_t swift_ASTGen_evaluateMacro(
58+
void *sourceFile, const void *sourceLocation,
59+
const char **evaluatedSource, ptrdiff_t *evaluatedSourceLength);
5960

6061
bool Solution::hasFixedType(TypeVariableType *typeVar) const {
6162
auto knownBinding = typeBindings.find(typeVar);
@@ -2978,8 +2979,18 @@ namespace {
29782979
if (ctx.LangOpts.hasFeature(Feature::BuiltinMacros)) {
29792980
if (auto sf = dc->getParentSourceFile()) {
29802981
if (auto astGenSF = sf->exportedSourceFile) {
2981-
swift_ASTGen_printMacroResult(
2982-
astGenSF, expr->getStartLoc().getOpaquePointerValue());
2982+
const char *evaluatedSource;
2983+
ptrdiff_t evaluatedSourceLength;
2984+
swift_ASTGen_evaluateMacro(
2985+
astGenSF, expr->getStartLoc().getOpaquePointerValue(),
2986+
&evaluatedSource, &evaluatedSourceLength);
2987+
if (evaluatedSource) {
2988+
llvm::outs() << "Macro rewrite: "
2989+
<< MagicIdentifierLiteralExpr::getKindString(expr->getKind())
2990+
<< " --> " << StringRef(evaluatedSource, evaluatedSourceLength)
2991+
<< "\n";
2992+
free((void*)evaluatedSource);
2993+
}
29832994
}
29842995
}
29852996
}

0 commit comments

Comments
 (0)