Skip to content

Implement extractInlinableText with swift-syntax #76187

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
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
2 changes: 1 addition & 1 deletion lib/AST/ASTPrinter.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -4516,7 +4516,7 @@ void PrintAST::visitMacroDecl(MacroDecl *decl) {
ASTContext &ctx = decl->getASTContext();
SmallString<64> scratch;
Printer << " = "
<< extractInlinableText(ctx.SourceMgr, decl->definition, scratch);
<< extractInlinableText(ctx, decl->definition, scratch);
} else {
auto def = decl->getDefinition();
switch (def.kind) {
Expand Down
15 changes: 6 additions & 9 deletions lib/AST/Decl.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2225,9 +2225,9 @@ StringRef PatternBindingEntry::getInitStringRepresentation(
if (InitContextAndFlags.getInt().contains(PatternFlags::IsText) &&
!InitStringRepresentation.empty())
return InitStringRepresentation;
auto &sourceMgr = getAnchoringVarDecl()->getASTContext().SourceMgr;
auto &ctx = getAnchoringVarDecl()->getASTContext();
auto init = getOriginalInit();
return extractInlinableText(sourceMgr, init, scratch);
return extractInlinableText(ctx, init, scratch);
}

SourceRange PatternBindingDecl::getSourceRange() const {
Expand Down Expand Up @@ -9022,7 +9022,7 @@ ParamDecl::getDefaultValueStringRepresentation(

assert(hasDefaultExpr()
&& "Normal default argument with no default expression?!");
return extractInlinableText(getASTContext().SourceMgr,
return extractInlinableText(getASTContext(),
getStructuralDefaultExpr(), scratch);
}
case DefaultArgumentKind::StoredProperty: {
Expand Down Expand Up @@ -9066,8 +9066,7 @@ ParamDecl::getDefaultValueStringRepresentation(
return ".init()";
}

auto &sourceMgr = getASTContext().SourceMgr;
return extractInlinableText(sourceMgr, wrappedValue, scratch);
return extractInlinableText(getASTContext(), wrappedValue, scratch);
}
}

Expand All @@ -9087,9 +9086,7 @@ ParamDecl::getDefaultValueStringRepresentation(
return "<<empty>>";
}

return extractInlinableText(getASTContext().SourceMgr,
init,
scratch);
return extractInlinableText(getASTContext(), init, scratch);
}
case DefaultArgumentKind::Inherited: return "super";
#define MAGIC_IDENTIFIER(NAME, STRING, SYNTAX_KIND) \
Expand Down Expand Up @@ -10275,7 +10272,7 @@ StringRef AbstractFunctionDecl::getInlinableBodyText(
return BodyStringRepresentation;

auto body = getBody();
return extractInlinableText(getASTContext().SourceMgr, body, scratch);
return extractInlinableText(getASTContext(), body, scratch);
}

/// A uniqued list of derivative function configurations.
Expand Down
25 changes: 24 additions & 1 deletion lib/AST/InlinableText.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -10,13 +10,15 @@
//
//===----------------------------------------------------------------------===//
#include "InlinableText.h"
#include "swift/AST/ASTBridging.h"
#include "swift/AST/ASTContext.h"
#include "swift/AST/ASTNode.h"
#include "swift/AST/ASTVisitor.h"
#include "swift/AST/ASTWalker.h"
#include "swift/AST/Decl.h"
#include "swift/AST/Expr.h"
#include "swift/Basic/Assertions.h"
#include "swift/Bridging/ASTGen.h"
#include "swift/Parse/Lexer.h"

#include "llvm/ADT/SmallVector.h"
Expand Down Expand Up @@ -291,8 +293,28 @@ static void appendRange(
}
}

StringRef swift::extractInlinableText(SourceManager &sourceMgr, ASTNode node,
extern "C"
BridgedStringRef swift_ASTGen_extractInlinableText(
BridgedASTContext ctx, BridgedStringRef sourceText);

StringRef swift::extractInlinableText(ASTContext &ctx, ASTNode node,
SmallVectorImpl<char> &scratch) {
SourceManager &sourceMgr = ctx.SourceMgr;

#if SWIFT_BUILD_SWIFT_SYNTAX
CharSourceRange sourceTextRange =
Lexer::getCharSourceRangeFromSourceRange(
sourceMgr, node.getSourceRange());
StringRef sourceText = sourceMgr.extractText(sourceTextRange);
auto resultText = swift_ASTGen_extractInlinableText(ctx, sourceText);

scratch.clear();
scratch.insert(scratch.begin(),
resultText.unbridged().begin(),
resultText.unbridged().end());
swift_ASTGen_freeBridgedString(resultText);
return { scratch.data(), scratch.size() };
#else
// Extract inactive ranges from the text of the node.
ExtractInactiveRanges extractor(sourceMgr);
node.walk(extractor);
Expand All @@ -316,4 +338,5 @@ StringRef swift::extractInlinableText(SourceManager &sourceMgr, ASTNode node,
}

return { scratch.data(), scratch.size() };
#endif
}
4 changes: 2 additions & 2 deletions lib/AST/InlinableText.h
Original file line number Diff line number Diff line change
Expand Up @@ -19,11 +19,11 @@
#include "llvm/ADT/SmallVector.h"

namespace swift {
class SourceManager;
class ASTContext;

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

} // end namespace swift
Expand Down
24 changes: 24 additions & 0 deletions lib/ASTGen/Sources/ASTGen/CompilerBuildConfiguration.swift
Original file line number Diff line number Diff line change
Expand Up @@ -370,3 +370,27 @@ public func evaluatePoundIfCondition(

return (isActive ? 0x1 : 0) | (syntaxErrorsAllowed ? 0x2 : 0)
}

@_cdecl("swift_ASTGen_extractInlinableText")
public func extractInlinableText(
astContext: BridgedASTContext,
sourceText: BridgedStringRef
) -> BridgedStringRef {
let textBuffer = UnsafeBufferPointer<UInt8>(start: sourceText.data, count: sourceText.count)
var parser = Parser(textBuffer)
let syntax = SourceFileSyntax.parse(from: &parser)

let configuration = CompilerBuildConfiguration(
ctx: astContext,
sourceBuffer: textBuffer
)

// Remove any inactive #if regions.
let syntaxWithoutInactive = syntax.removingInactive(
in: configuration,
retainFeatureCheckIfConfigs: true
).result

// Remove comments and return the result.
return allocateBridgedString(syntaxWithoutInactive.descriptionWithoutCommentsAndSourceLocations)
}
3 changes: 1 addition & 2 deletions lib/Sema/TypeCheckMacros.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -797,8 +797,7 @@ static std::string expandMacroDefinition(
if (isExpressionReplacement) {
auto argExpr = args->getArgExprs()[replacement.parameterIndex];
SmallString<32> argTextBuffer;
auto argText =
extractInlinableText(ctx.SourceMgr, argExpr, argTextBuffer);
auto argText = extractInlinableText(ctx, argExpr, argTextBuffer);
expandedResult.append(argText);
} else {
auto typeArgType = subs.getReplacementTypes()[replacement.parameterIndex];
Expand Down
2 changes: 1 addition & 1 deletion test/ModuleInterface/if-configs.swift
Original file line number Diff line number Diff line change
Expand Up @@ -178,4 +178,4 @@ public func hasIfCompilerCheck(_ x: () -> Bool = {
return false
#endif
}) {
}
}