@@ -8595,6 +8595,50 @@ bool RefactoringActionExpandMacro::isApplicable(const ResolvedCursorInfo &Info,
8595
8595
return !getMacroExpansionBuffers (Diag.SourceMgr , Info).empty ();
8596
8596
}
8597
8597
8598
+ // / Given the expanded code for a particular macro, perform whitespace
8599
+ // / adjustments to make the refactoring more.
8600
+ static StringRef adjustMacroExpansionWhitespace (
8601
+ GeneratedSourceInfo::Kind kind, StringRef expandedCode,
8602
+ llvm::SmallString<64 > &scratch
8603
+ ) {
8604
+ scratch.clear ();
8605
+
8606
+ switch (kind) {
8607
+ case GeneratedSourceInfo::ExpressionMacroExpansion:
8608
+ case GeneratedSourceInfo::FreestandingDeclMacroExpansion:
8609
+ return expandedCode;
8610
+
8611
+ case GeneratedSourceInfo::AccessorMacroExpansion:
8612
+ // For accessor macros, wrap curly braces around the buffer contents.
8613
+ scratch += " {\n " ;
8614
+ scratch += expandedCode;
8615
+ scratch += " \n }" ;
8616
+ return scratch;
8617
+
8618
+ case GeneratedSourceInfo::MemberAttributeMacroExpansion:
8619
+ // For member-attribute macros, add a space at the end.
8620
+ scratch += expandedCode;
8621
+ scratch += " " ;
8622
+ return scratch;
8623
+
8624
+ case GeneratedSourceInfo::PeerMacroExpansion:
8625
+ // For peers, add a newline to create some separation.
8626
+ scratch += " \n " ;
8627
+ LLVM_FALLTHROUGH;
8628
+
8629
+ case GeneratedSourceInfo::MemberMacroExpansion:
8630
+ // For members, add a newline.
8631
+ scratch += " \n " ;
8632
+ scratch += expandedCode;
8633
+ scratch += " \n " ;
8634
+ return scratch;
8635
+
8636
+ case GeneratedSourceInfo::ReplacedFunctionBody:
8637
+ case GeneratedSourceInfo::PrettyPrinted:
8638
+ return expandedCode;
8639
+ }
8640
+ }
8641
+
8598
8642
bool RefactoringActionExpandMacro::performChange () {
8599
8643
auto bufferIDs = getMacroExpansionBuffers (SM, CursorInfo);
8600
8644
if (bufferIDs.empty ())
@@ -8637,15 +8681,11 @@ bool RefactoringActionExpandMacro::performChange() {
8637
8681
8638
8682
auto afterLeftBraceLoc = Lexer::getLocForEndOfToken (SM, leftBraceLoc);
8639
8683
originalSourceRange = CharSourceRange (afterLeftBraceLoc, 0 );
8640
- } else if (generatedInfo->kind ==
8641
- GeneratedSourceInfo::AccessorMacroExpansion) {
8642
- // For accessor macros, wrap curly braces around the buffer contents.
8643
- scratchBuffer += " {\n " ;
8644
- scratchBuffer += rewrittenBuffer;
8645
- scratchBuffer += " \n }" ;
8646
- rewrittenBuffer = scratchBuffer;
8647
8684
}
8648
8685
8686
+ rewrittenBuffer = adjustMacroExpansionWhitespace (
8687
+ generatedInfo->kind , rewrittenBuffer, scratchBuffer);
8688
+
8649
8689
EditConsumer.accept (SM, originalSourceRange, rewrittenBuffer);
8650
8690
8651
8691
if (generatedInfo->attachedMacroCustomAttr && !attachedMacroAttr)
0 commit comments