Skip to content

Commit edb980e

Browse files
authored
Merge pull request #64095 from bnbarham/do-not-change-refactoring
[Macros] Do not edit macro buffer or position when refactoring
2 parents ffc998a + 80d2712 commit edb980e

File tree

5 files changed

+54
-92
lines changed

5 files changed

+54
-92
lines changed

lib/AST/Decl.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6127,10 +6127,10 @@ void AbstractStorageDecl::setAccessors(SourceLoc lbraceLoc,
61276127
ArrayRef<AccessorDecl *> accessors,
61286128
SourceLoc rbraceLoc) {
61296129
// This method is called after we've already recorded an accessors clause
6130-
// only on recovery paths and only when that clause was empty.
6130+
// only on recovery paths and only when that clause was empty, or when a
6131+
// macro expands to accessors (in which case, we may already have accessors).
61316132
auto record = Accessors.getPointer();
61326133
if (record) {
6133-
assert(record->getAllAccessors().empty());
61346134
for (auto accessor : accessors) {
61356135
(void) record->addOpaqueAccessor(accessor);
61366136
}

lib/ASTGen/Sources/ASTGen/Macros.swift

Lines changed: 42 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -523,22 +523,7 @@ func expandAttachedMacro(
523523
}
524524

525525
// Fixup the source.
526-
var expandedSource: String
527-
switch MacroRole(rawValue: rawMacroRole)! {
528-
case .Accessor:
529-
expandedSource = expandedSources.joined(separator: "\n\n")
530-
case .Member:
531-
expandedSource = expandedSources.joined(separator: "\n\n")
532-
case .MemberAttribute:
533-
expandedSource = expandedSources.joined(separator: " ")
534-
case .Peer:
535-
expandedSource = expandedSources.joined(separator: "\n\n")
536-
case .Conformance:
537-
expandedSource = expandedSources.joined(separator: "\n\n")
538-
case .Expression,
539-
.FreestandingDeclaration:
540-
fatalError("unreachable")
541-
}
526+
var expandedSource: String = collapse(expansions: expandedSources, for: MacroRole(rawValue: rawMacroRole)!, attachedTo: declarationNode)
542527

543528
// Form the result buffer for our caller.
544529
expandedSource.withUTF8 { utf8 in
@@ -837,3 +822,44 @@ func expandAttachedMacroInProcess(
837822

838823
return expandedSources
839824
}
825+
826+
fileprivate func collapse<Node: SyntaxProtocol>(
827+
expansions: [String],
828+
for role: MacroRole,
829+
attachedTo declarationNode: Node
830+
) -> String {
831+
var separator: String = "\n\n"
832+
var prefix: String = ""
833+
var suffix: String = ""
834+
835+
switch role {
836+
case .Accessor:
837+
if let varDecl = declarationNode.as(VariableDeclSyntax.self),
838+
let binding = varDecl.bindings.first,
839+
binding.accessor == nil {
840+
prefix = "{\n"
841+
suffix = "\n}"
842+
}
843+
case .Member:
844+
prefix = "\n\n"
845+
suffix = "\n"
846+
case .MemberAttribute:
847+
separator = " "
848+
suffix = " "
849+
case .Peer:
850+
prefix = "\n\n"
851+
suffix = "\n"
852+
case .Conformance:
853+
prefix = "\n\n"
854+
suffix = "\n"
855+
case .Expression,
856+
.FreestandingDeclaration:
857+
fatalError("unreachable")
858+
}
859+
860+
let separated = expansions.joined(separator: separator)
861+
if separated.isEmpty {
862+
return separated
863+
}
864+
return prefix + separated + suffix
865+
}

lib/Parse/ParseDecl.cpp

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7098,11 +7098,13 @@ void Parser::parseTopLevelAccessors(
70987098
staticLoc = binding->getStaticLoc();
70997099
}
71007100

7101+
bool hadLBrace = consumeIf(tok::l_brace);
7102+
71017103
ParserStatus status;
71027104
ParsedAccessors accessors;
71037105
bool hasEffectfulGet = false;
71047106
bool parsingLimitedSyntax = false;
7105-
while (!Tok.is(tok::eof)) {
7107+
while (!Tok.isAny(tok::r_brace, tok::eof)) {
71067108
DeclAttributes attributes;
71077109
AccessorKind kind = AccessorKind::Get;
71087110
SourceLoc loc;
@@ -7116,6 +7118,10 @@ void Parser::parseTopLevelAccessors(
71167118
);
71177119
}
71187120

7121+
if (hadLBrace && Tok.is(tok::r_brace)) {
7122+
consumeToken(tok::r_brace);
7123+
}
7124+
71197125
// Consume remaining tokens.
71207126
// FIXME: Emit a diagnostic here?
71217127
while (!Tok.is(tok::eof)) {

lib/Refactoring/Refactoring.cpp

Lines changed: 1 addition & 72 deletions
Original file line numberDiff line numberDiff line change
@@ -8617,51 +8617,6 @@ bool RefactoringActionExpandMacro::isApplicable(ResolvedCursorInfoPtr Info,
86178617
return !getMacroExpansionBuffers(Diag.SourceMgr, Info).empty();
86188618
}
86198619

8620-
/// Given the expanded code for a particular macro, perform whitespace
8621-
/// adjustments to make the refactoring more.
8622-
static StringRef adjustMacroExpansionWhitespace(
8623-
GeneratedSourceInfo::Kind kind, StringRef expandedCode,
8624-
llvm::SmallString<64> &scratch
8625-
) {
8626-
scratch.clear();
8627-
8628-
switch (kind) {
8629-
case GeneratedSourceInfo::ExpressionMacroExpansion:
8630-
case GeneratedSourceInfo::FreestandingDeclMacroExpansion:
8631-
return expandedCode;
8632-
8633-
case GeneratedSourceInfo::AccessorMacroExpansion:
8634-
// For accessor macros, wrap curly braces around the buffer contents.
8635-
scratch += "{\n";
8636-
scratch += expandedCode;
8637-
scratch += "\n}";
8638-
return scratch;
8639-
8640-
case GeneratedSourceInfo::MemberAttributeMacroExpansion:
8641-
// For member-attribute macros, add a space at the end.
8642-
scratch += expandedCode;
8643-
scratch += " ";
8644-
return scratch;
8645-
8646-
case GeneratedSourceInfo::PeerMacroExpansion:
8647-
case GeneratedSourceInfo::ConformanceMacroExpansion:
8648-
// For peers and conformances, add a newline to create some separation.
8649-
scratch += "\n";
8650-
LLVM_FALLTHROUGH;
8651-
8652-
case GeneratedSourceInfo::MemberMacroExpansion:
8653-
// For members, add a newline.
8654-
scratch += "\n";
8655-
scratch += expandedCode;
8656-
scratch += "\n";
8657-
return scratch;
8658-
8659-
case GeneratedSourceInfo::ReplacedFunctionBody:
8660-
case GeneratedSourceInfo::PrettyPrinted:
8661-
return expandedCode;
8662-
}
8663-
}
8664-
86658620
bool RefactoringActionExpandMacro::performChange() {
86668621
auto bufferIDs = getMacroExpansionBuffers(SM, CursorInfo);
86678622
if (bufferIDs.empty())
@@ -8682,33 +8637,6 @@ bool RefactoringActionExpandMacro::performChange() {
86828637
rewrittenBuffer.empty())
86838638
continue;
86848639

8685-
auto originalSourceRange = generatedInfo->originalSourceRange;
8686-
8687-
SmallString<64> scratchBuffer;
8688-
if (generatedInfo->kind == GeneratedSourceInfo::MemberMacroExpansion) {
8689-
// For member macros, adjust the source range from before-the-close-brace
8690-
// to after-the-open-brace.
8691-
ASTNode node = ASTNode::getFromOpaqueValue(generatedInfo->astNode);
8692-
auto decl = node.dyn_cast<Decl *>();
8693-
if (!decl)
8694-
continue;
8695-
8696-
SourceLoc leftBraceLoc;
8697-
if (auto nominal = dyn_cast<NominalTypeDecl>(decl)) {
8698-
leftBraceLoc = nominal->getBraces().Start;
8699-
} else if (auto ext = dyn_cast<ExtensionDecl>(decl)) {
8700-
leftBraceLoc = ext->getBraces().Start;
8701-
}
8702-
if (leftBraceLoc.isInvalid())
8703-
continue;
8704-
8705-
auto afterLeftBraceLoc = Lexer::getLocForEndOfToken(SM, leftBraceLoc);
8706-
originalSourceRange = CharSourceRange(afterLeftBraceLoc, 0);
8707-
}
8708-
8709-
rewrittenBuffer = adjustMacroExpansionWhitespace(
8710-
generatedInfo->kind, rewrittenBuffer, scratchBuffer);
8711-
87128640
// `TheFile` is the file of the actual expansion site, where as
87138641
// `OriginalFile` is the possibly enclosing buffer. Concretely:
87148642
// ```
@@ -8727,6 +8655,7 @@ bool RefactoringActionExpandMacro::performChange() {
87278655
// site (`@_someBufferName`). Thus, we need to include the path to the
87288656
// original source as well. Note that this path could itself be another
87298657
// expansion.
8658+
auto originalSourceRange = generatedInfo->originalSourceRange;
87308659
SourceFile *originalFile =
87318660
MD->getSourceFileContainingLocation(originalSourceRange.getStart());
87328661
StringRef originalPath;

test/SourceKit/Macros/macro_basic.swift

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -139,7 +139,8 @@ struct S4 { }
139139
// ATTACHED_EXPAND-NEXT: source.edit.kind.active:
140140
// ATTACHED_EXPAND-NEXT: 24:3-24:3 (@__swiftmacro_9MacroUser1SV13myTypeWrapperfMA0_.swift) "@accessViaStorage "
141141
// ATTACHED_EXPAND-NEXT: source.edit.kind.active:
142-
// ATTACHED_EXPAND-NEXT: 22:11-22:11 (@__swiftmacro_9MacroUser1SV13myTypeWrapperfMm_.swift) "
142+
// ATTACHED_EXPAND-NEXT: 25:1-25:1 (@__swiftmacro_9MacroUser1SV13myTypeWrapperfMm_.swift) "
143+
// ATTACHED_EXPAND-EMPTY:
143144
// ATTACHED_EXPAND-NEXT: private var _storage = _Storage()
144145
// ATTACHED_EXPAND-NEXT: "
145146
// ATTACHED_EXPAND-NEXT: source.edit.kind.active:

0 commit comments

Comments
 (0)