Skip to content

Commit 601a5c3

Browse files
authored
Merge pull request #76187 from DougGregor/extract-inlinable-text-swift-syntax
Implement extractInlinableText with swift-syntax
2 parents 947910d + a73711e commit 601a5c3

File tree

7 files changed

+59
-16
lines changed

7 files changed

+59
-16
lines changed

lib/AST/ASTPrinter.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4516,7 +4516,7 @@ void PrintAST::visitMacroDecl(MacroDecl *decl) {
45164516
ASTContext &ctx = decl->getASTContext();
45174517
SmallString<64> scratch;
45184518
Printer << " = "
4519-
<< extractInlinableText(ctx.SourceMgr, decl->definition, scratch);
4519+
<< extractInlinableText(ctx, decl->definition, scratch);
45204520
} else {
45214521
auto def = decl->getDefinition();
45224522
switch (def.kind) {

lib/AST/Decl.cpp

Lines changed: 6 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -2224,9 +2224,9 @@ StringRef PatternBindingEntry::getInitStringRepresentation(
22242224
if (InitContextAndFlags.getInt().contains(PatternFlags::IsText) &&
22252225
!InitStringRepresentation.empty())
22262226
return InitStringRepresentation;
2227-
auto &sourceMgr = getAnchoringVarDecl()->getASTContext().SourceMgr;
2227+
auto &ctx = getAnchoringVarDecl()->getASTContext();
22282228
auto init = getOriginalInit();
2229-
return extractInlinableText(sourceMgr, init, scratch);
2229+
return extractInlinableText(ctx, init, scratch);
22302230
}
22312231

22322232
SourceRange PatternBindingDecl::getSourceRange() const {
@@ -9039,7 +9039,7 @@ ParamDecl::getDefaultValueStringRepresentation(
90399039

90409040
assert(hasDefaultExpr()
90419041
&& "Normal default argument with no default expression?!");
9042-
return extractInlinableText(getASTContext().SourceMgr,
9042+
return extractInlinableText(getASTContext(),
90439043
getStructuralDefaultExpr(), scratch);
90449044
}
90459045
case DefaultArgumentKind::StoredProperty: {
@@ -9083,8 +9083,7 @@ ParamDecl::getDefaultValueStringRepresentation(
90839083
return ".init()";
90849084
}
90859085

9086-
auto &sourceMgr = getASTContext().SourceMgr;
9087-
return extractInlinableText(sourceMgr, wrappedValue, scratch);
9086+
return extractInlinableText(getASTContext(), wrappedValue, scratch);
90889087
}
90899088
}
90909089

@@ -9104,9 +9103,7 @@ ParamDecl::getDefaultValueStringRepresentation(
91049103
return "<<empty>>";
91059104
}
91069105

9107-
return extractInlinableText(getASTContext().SourceMgr,
9108-
init,
9109-
scratch);
9106+
return extractInlinableText(getASTContext(), init, scratch);
91109107
}
91119108
case DefaultArgumentKind::Inherited: return "super";
91129109
#define MAGIC_IDENTIFIER(NAME, STRING, SYNTAX_KIND) \
@@ -10292,7 +10289,7 @@ StringRef AbstractFunctionDecl::getInlinableBodyText(
1029210289
return BodyStringRepresentation;
1029310290

1029410291
auto body = getBody();
10295-
return extractInlinableText(getASTContext().SourceMgr, body, scratch);
10292+
return extractInlinableText(getASTContext(), body, scratch);
1029610293
}
1029710294

1029810295
/// A uniqued list of derivative function configurations.

lib/AST/InlinableText.cpp

Lines changed: 24 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,13 +10,15 @@
1010
//
1111
//===----------------------------------------------------------------------===//
1212
#include "InlinableText.h"
13+
#include "swift/AST/ASTBridging.h"
1314
#include "swift/AST/ASTContext.h"
1415
#include "swift/AST/ASTNode.h"
1516
#include "swift/AST/ASTVisitor.h"
1617
#include "swift/AST/ASTWalker.h"
1718
#include "swift/AST/Decl.h"
1819
#include "swift/AST/Expr.h"
1920
#include "swift/Basic/Assertions.h"
21+
#include "swift/Bridging/ASTGen.h"
2022
#include "swift/Parse/Lexer.h"
2123

2224
#include "llvm/ADT/SmallVector.h"
@@ -291,8 +293,28 @@ static void appendRange(
291293
}
292294
}
293295

294-
StringRef swift::extractInlinableText(SourceManager &sourceMgr, ASTNode node,
296+
extern "C"
297+
BridgedStringRef swift_ASTGen_extractInlinableText(
298+
BridgedASTContext ctx, BridgedStringRef sourceText);
299+
300+
StringRef swift::extractInlinableText(ASTContext &ctx, ASTNode node,
295301
SmallVectorImpl<char> &scratch) {
302+
SourceManager &sourceMgr = ctx.SourceMgr;
303+
304+
#if SWIFT_BUILD_SWIFT_SYNTAX
305+
CharSourceRange sourceTextRange =
306+
Lexer::getCharSourceRangeFromSourceRange(
307+
sourceMgr, node.getSourceRange());
308+
StringRef sourceText = sourceMgr.extractText(sourceTextRange);
309+
auto resultText = swift_ASTGen_extractInlinableText(ctx, sourceText);
310+
311+
scratch.clear();
312+
scratch.insert(scratch.begin(),
313+
resultText.unbridged().begin(),
314+
resultText.unbridged().end());
315+
swift_ASTGen_freeBridgedString(resultText);
316+
return { scratch.data(), scratch.size() };
317+
#else
296318
// Extract inactive ranges from the text of the node.
297319
ExtractInactiveRanges extractor(sourceMgr);
298320
node.walk(extractor);
@@ -316,4 +338,5 @@ StringRef swift::extractInlinableText(SourceManager &sourceMgr, ASTNode node,
316338
}
317339

318340
return { scratch.data(), scratch.size() };
341+
#endif
319342
}

lib/AST/InlinableText.h

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -19,11 +19,11 @@
1919
#include "llvm/ADT/SmallVector.h"
2020

2121
namespace swift {
22-
class SourceManager;
22+
class ASTContext;
2323

2424
/// Extracts the text of this ASTNode from the source buffer, ignoring
2525
/// all #if declarations and clauses except the elements that are active.
26-
StringRef extractInlinableText(SourceManager &sourceMgr, ASTNode node,
26+
StringRef extractInlinableText(ASTContext &ctx, ASTNode node,
2727
SmallVectorImpl<char> &scratch);
2828

2929
} // end namespace swift

lib/ASTGen/Sources/ASTGen/CompilerBuildConfiguration.swift

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -370,3 +370,27 @@ public func evaluatePoundIfCondition(
370370

371371
return (isActive ? 0x1 : 0) | (syntaxErrorsAllowed ? 0x2 : 0)
372372
}
373+
374+
@_cdecl("swift_ASTGen_extractInlinableText")
375+
public func extractInlinableText(
376+
astContext: BridgedASTContext,
377+
sourceText: BridgedStringRef
378+
) -> BridgedStringRef {
379+
let textBuffer = UnsafeBufferPointer<UInt8>(start: sourceText.data, count: sourceText.count)
380+
var parser = Parser(textBuffer)
381+
let syntax = SourceFileSyntax.parse(from: &parser)
382+
383+
let configuration = CompilerBuildConfiguration(
384+
ctx: astContext,
385+
sourceBuffer: textBuffer
386+
)
387+
388+
// Remove any inactive #if regions.
389+
let syntaxWithoutInactive = syntax.removingInactive(
390+
in: configuration,
391+
retainFeatureCheckIfConfigs: true
392+
).result
393+
394+
// Remove comments and return the result.
395+
return allocateBridgedString(syntaxWithoutInactive.descriptionWithoutCommentsAndSourceLocations)
396+
}

lib/Sema/TypeCheckMacros.cpp

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -797,8 +797,7 @@ static std::string expandMacroDefinition(
797797
if (isExpressionReplacement) {
798798
auto argExpr = args->getArgExprs()[replacement.parameterIndex];
799799
SmallString<32> argTextBuffer;
800-
auto argText =
801-
extractInlinableText(ctx.SourceMgr, argExpr, argTextBuffer);
800+
auto argText = extractInlinableText(ctx, argExpr, argTextBuffer);
802801
expandedResult.append(argText);
803802
} else {
804803
auto typeArgType = subs.getReplacementTypes()[replacement.parameterIndex];

test/ModuleInterface/if-configs.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -178,4 +178,4 @@ public func hasIfCompilerCheck(_ x: () -> Bool = {
178178
return false
179179
#endif
180180
}) {
181-
}
181+
}

0 commit comments

Comments
 (0)