[Macros] Do not edit macro buffer or position when refactoring

Rather than editing the macro buffer in refactoring, add appropriate
padding and braces when creating the macro.

Don't edit the insertion location - we should update this in a later PR
as well.
This commit is contained in:
Ben Barham
2023-03-03 10:58:21 -08:00
parent 2a7aa37cd5
commit 80d27128d8
5 changed files with 54 additions and 92 deletions

View File

@@ -8617,51 +8617,6 @@ bool RefactoringActionExpandMacro::isApplicable(ResolvedCursorInfoPtr Info,
return !getMacroExpansionBuffers(Diag.SourceMgr, Info).empty();
}
/// Given the expanded code for a particular macro, perform whitespace
/// adjustments to make the refactoring more.
static StringRef adjustMacroExpansionWhitespace(
GeneratedSourceInfo::Kind kind, StringRef expandedCode,
llvm::SmallString<64> &scratch
) {
scratch.clear();
switch (kind) {
case GeneratedSourceInfo::ExpressionMacroExpansion:
case GeneratedSourceInfo::FreestandingDeclMacroExpansion:
return expandedCode;
case GeneratedSourceInfo::AccessorMacroExpansion:
// For accessor macros, wrap curly braces around the buffer contents.
scratch += "{\n";
scratch += expandedCode;
scratch += "\n}";
return scratch;
case GeneratedSourceInfo::MemberAttributeMacroExpansion:
// For member-attribute macros, add a space at the end.
scratch += expandedCode;
scratch += " ";
return scratch;
case GeneratedSourceInfo::PeerMacroExpansion:
case GeneratedSourceInfo::ConformanceMacroExpansion:
// For peers and conformances, add a newline to create some separation.
scratch += "\n";
LLVM_FALLTHROUGH;
case GeneratedSourceInfo::MemberMacroExpansion:
// For members, add a newline.
scratch += "\n";
scratch += expandedCode;
scratch += "\n";
return scratch;
case GeneratedSourceInfo::ReplacedFunctionBody:
case GeneratedSourceInfo::PrettyPrinted:
return expandedCode;
}
}
bool RefactoringActionExpandMacro::performChange() {
auto bufferIDs = getMacroExpansionBuffers(SM, CursorInfo);
if (bufferIDs.empty())
@@ -8682,33 +8637,6 @@ bool RefactoringActionExpandMacro::performChange() {
rewrittenBuffer.empty())
continue;
auto originalSourceRange = generatedInfo->originalSourceRange;
SmallString<64> scratchBuffer;
if (generatedInfo->kind == GeneratedSourceInfo::MemberMacroExpansion) {
// For member macros, adjust the source range from before-the-close-brace
// to after-the-open-brace.
ASTNode node = ASTNode::getFromOpaqueValue(generatedInfo->astNode);
auto decl = node.dyn_cast<Decl *>();
if (!decl)
continue;
SourceLoc leftBraceLoc;
if (auto nominal = dyn_cast<NominalTypeDecl>(decl)) {
leftBraceLoc = nominal->getBraces().Start;
} else if (auto ext = dyn_cast<ExtensionDecl>(decl)) {
leftBraceLoc = ext->getBraces().Start;
}
if (leftBraceLoc.isInvalid())
continue;
auto afterLeftBraceLoc = Lexer::getLocForEndOfToken(SM, leftBraceLoc);
originalSourceRange = CharSourceRange(afterLeftBraceLoc, 0);
}
rewrittenBuffer = adjustMacroExpansionWhitespace(
generatedInfo->kind, rewrittenBuffer, scratchBuffer);
// `TheFile` is the file of the actual expansion site, where as
// `OriginalFile` is the possibly enclosing buffer. Concretely:
// ```
@@ -8727,6 +8655,7 @@ bool RefactoringActionExpandMacro::performChange() {
// site (`@_someBufferName`). Thus, we need to include the path to the
// original source as well. Note that this path could itself be another
// expansion.
auto originalSourceRange = generatedInfo->originalSourceRange;
SourceFile *originalFile =
MD->getSourceFileContainingLocation(originalSourceRange.getStart());
StringRef originalPath;