Skip to content

Commit a73711e

Browse files
committed
Implement extractInlinableText with swift-syntax
Replace the existing C++ implementation of extractInlinableText with a new implementation based on swift-syntax. It uses SwiftIfConfig to remove inactive regions (with a special mode), and a new compiler-only entrypoint in the library to remove comments and `#sourceLocation`.
1 parent 6a31007 commit a73711e

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
@@ -2225,9 +2225,9 @@ StringRef PatternBindingEntry::getInitStringRepresentation(
22252225
if (InitContextAndFlags.getInt().contains(PatternFlags::IsText) &&
22262226
!InitStringRepresentation.empty())
22272227
return InitStringRepresentation;
2228-
auto &sourceMgr = getAnchoringVarDecl()->getASTContext().SourceMgr;
2228+
auto &ctx = getAnchoringVarDecl()->getASTContext();
22292229
auto init = getOriginalInit();
2230-
return extractInlinableText(sourceMgr, init, scratch);
2230+
return extractInlinableText(ctx, init, scratch);
22312231
}
22322232

22332233
SourceRange PatternBindingDecl::getSourceRange() const {
@@ -9022,7 +9022,7 @@ ParamDecl::getDefaultValueStringRepresentation(
90229022

90239023
assert(hasDefaultExpr()
90249024
&& "Normal default argument with no default expression?!");
9025-
return extractInlinableText(getASTContext().SourceMgr,
9025+
return extractInlinableText(getASTContext(),
90269026
getStructuralDefaultExpr(), scratch);
90279027
}
90289028
case DefaultArgumentKind::StoredProperty: {
@@ -9066,8 +9066,7 @@ ParamDecl::getDefaultValueStringRepresentation(
90669066
return ".init()";
90679067
}
90689068

9069-
auto &sourceMgr = getASTContext().SourceMgr;
9070-
return extractInlinableText(sourceMgr, wrappedValue, scratch);
9069+
return extractInlinableText(getASTContext(), wrappedValue, scratch);
90719070
}
90729071
}
90739072

@@ -9087,9 +9086,7 @@ ParamDecl::getDefaultValueStringRepresentation(
90879086
return "<<empty>>";
90889087
}
90899088

9090-
return extractInlinableText(getASTContext().SourceMgr,
9091-
init,
9092-
scratch);
9089+
return extractInlinableText(getASTContext(), init, scratch);
90939090
}
90949091
case DefaultArgumentKind::Inherited: return "super";
90959092
#define MAGIC_IDENTIFIER(NAME, STRING, SYNTAX_KIND) \
@@ -10275,7 +10272,7 @@ StringRef AbstractFunctionDecl::getInlinableBodyText(
1027510272
return BodyStringRepresentation;
1027610273

1027710274
auto body = getBody();
10278-
return extractInlinableText(getASTContext().SourceMgr, body, scratch);
10275+
return extractInlinableText(getASTContext(), body, scratch);
1027910276
}
1028010277

1028110278
/// 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)