Skip to content

Commit 514d20a

Browse files
committed
[Refactoring] Move ReplaceBodiesWithFatalError to separate file
1 parent ec71836 commit 514d20a

File tree

3 files changed

+87
-72
lines changed

3 files changed

+87
-72
lines changed

lib/Refactoring/CMakeLists.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ add_swift_host_library(swiftRefactoring STATIC
22
CollapseNestedIfStmt.cpp
33
ConvertStringConcatenationToInterpolation.cpp
44
Refactoring.cpp
5+
ReplaceBodiesWithFatalError.cpp
56
)
67

78
target_link_libraries(swiftRefactoring PRIVATE

lib/Refactoring/Refactoring.cpp

Lines changed: 0 additions & 72 deletions
Original file line numberDiff line numberDiff line change
@@ -1774,78 +1774,6 @@ bool RefactoringActionMoveMembersToExtension::performChange() {
17741774
return false;
17751775
}
17761776

1777-
namespace {
1778-
// A SingleDecl range may not include all decls actually declared in that range:
1779-
// a var decl has accessors that aren't included. This will find those missing
1780-
// decls.
1781-
class FindAllSubDecls : public SourceEntityWalker {
1782-
SmallPtrSetImpl<Decl *> &Found;
1783-
public:
1784-
FindAllSubDecls(SmallPtrSetImpl<Decl *> &found)
1785-
: Found(found) {}
1786-
1787-
bool walkToDeclPre(Decl *D, CharSourceRange range) override {
1788-
// Record this Decl, and skip its contents if we've already touched it.
1789-
if (!Found.insert(D).second)
1790-
return false;
1791-
1792-
if (auto ASD = dyn_cast<AbstractStorageDecl>(D)) {
1793-
ASD->visitParsedAccessors([&](AccessorDecl *accessor) {
1794-
Found.insert(accessor);
1795-
});
1796-
}
1797-
return true;
1798-
}
1799-
};
1800-
}
1801-
bool RefactoringActionReplaceBodiesWithFatalError::isApplicable(
1802-
const ResolvedRangeInfo &Info, DiagnosticEngine &Diag) {
1803-
switch (Info.Kind) {
1804-
case RangeKind::SingleDecl:
1805-
case RangeKind::MultiTypeMemberDecl: {
1806-
SmallPtrSet<Decl *, 16> Found;
1807-
for (auto decl : Info.DeclaredDecls) {
1808-
FindAllSubDecls(Found).walk(decl.VD);
1809-
}
1810-
for (auto decl : Found) {
1811-
auto AFD = dyn_cast<AbstractFunctionDecl>(decl);
1812-
if (AFD && !AFD->isImplicit())
1813-
return true;
1814-
}
1815-
1816-
return false;
1817-
}
1818-
case RangeKind::SingleExpression:
1819-
case RangeKind::PartOfExpression:
1820-
case RangeKind::SingleStatement:
1821-
case RangeKind::MultiStatement:
1822-
case RangeKind::Invalid:
1823-
return false;
1824-
}
1825-
llvm_unreachable("unhandled kind");
1826-
}
1827-
1828-
bool RefactoringActionReplaceBodiesWithFatalError::performChange() {
1829-
const StringRef replacement = "{\nfatalError()\n}";
1830-
SmallPtrSet<Decl *, 16> Found;
1831-
for (auto decl : RangeInfo.DeclaredDecls) {
1832-
FindAllSubDecls(Found).walk(decl.VD);
1833-
}
1834-
for (auto decl : Found) {
1835-
auto AFD = dyn_cast<AbstractFunctionDecl>(decl);
1836-
if (!AFD || AFD->isImplicit())
1837-
continue;
1838-
1839-
auto range = AFD->getBodySourceRange();
1840-
// If we're in replacement mode (i.e. have an edit consumer), we can
1841-
// rewrite the function body.
1842-
auto charRange = Lexer::getCharSourceRangeFromSourceRange(SM, range);
1843-
EditConsumer.accept(SM, charRange, replacement);
1844-
1845-
}
1846-
return false;
1847-
}
1848-
18491777
/// Abstract helper class containing info about a TernaryExpr
18501778
/// that can be expanded into an IfStmt.
18511779
class ExpandableTernaryExprInfo {
Lines changed: 86 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,86 @@
1+
//===----------------------------------------------------------------------===//
2+
//
3+
// This source file is part of the Swift.org open source project
4+
//
5+
// Copyright (c) 2014 - 2017 Apple Inc. and the Swift project authors
6+
// Licensed under Apache License v2.0 with Runtime Library Exception
7+
//
8+
// See https://swift.org/LICENSE.txt for license information
9+
// See https://swift.org/CONTRIBUTORS.txt for the list of Swift project authors
10+
//
11+
//===----------------------------------------------------------------------===//
12+
13+
#include "RefactoringActions.h"
14+
15+
using namespace swift::refactoring;
16+
17+
namespace {
18+
// A SingleDecl range may not include all decls actually declared in that range:
19+
// a var decl has accessors that aren't included. This will find those missing
20+
// decls.
21+
class FindAllSubDecls : public SourceEntityWalker {
22+
SmallPtrSetImpl<Decl *> &Found;
23+
24+
public:
25+
FindAllSubDecls(SmallPtrSetImpl<Decl *> &found) : Found(found) {}
26+
27+
bool walkToDeclPre(Decl *D, CharSourceRange range) override {
28+
// Record this Decl, and skip its contents if we've already touched it.
29+
if (!Found.insert(D).second)
30+
return false;
31+
32+
if (auto ASD = dyn_cast<AbstractStorageDecl>(D)) {
33+
ASD->visitParsedAccessors(
34+
[&](AccessorDecl *accessor) { Found.insert(accessor); });
35+
}
36+
return true;
37+
}
38+
};
39+
} // namespace
40+
41+
bool RefactoringActionReplaceBodiesWithFatalError::isApplicable(
42+
const ResolvedRangeInfo &Info, DiagnosticEngine &Diag) {
43+
switch (Info.Kind) {
44+
case RangeKind::SingleDecl:
45+
case RangeKind::MultiTypeMemberDecl: {
46+
SmallPtrSet<Decl *, 16> Found;
47+
for (auto decl : Info.DeclaredDecls) {
48+
FindAllSubDecls(Found).walk(decl.VD);
49+
}
50+
for (auto decl : Found) {
51+
auto AFD = dyn_cast<AbstractFunctionDecl>(decl);
52+
if (AFD && !AFD->isImplicit())
53+
return true;
54+
}
55+
56+
return false;
57+
}
58+
case RangeKind::SingleExpression:
59+
case RangeKind::PartOfExpression:
60+
case RangeKind::SingleStatement:
61+
case RangeKind::MultiStatement:
62+
case RangeKind::Invalid:
63+
return false;
64+
}
65+
llvm_unreachable("unhandled kind");
66+
}
67+
68+
bool RefactoringActionReplaceBodiesWithFatalError::performChange() {
69+
const StringRef replacement = "{\nfatalError()\n}";
70+
SmallPtrSet<Decl *, 16> Found;
71+
for (auto decl : RangeInfo.DeclaredDecls) {
72+
FindAllSubDecls(Found).walk(decl.VD);
73+
}
74+
for (auto decl : Found) {
75+
auto AFD = dyn_cast<AbstractFunctionDecl>(decl);
76+
if (!AFD || AFD->isImplicit())
77+
continue;
78+
79+
auto range = AFD->getBodySourceRange();
80+
// If we're in replacement mode (i.e. have an edit consumer), we can
81+
// rewrite the function body.
82+
auto charRange = Lexer::getCharSourceRangeFromSourceRange(SM, range);
83+
EditConsumer.accept(SM, charRange, replacement);
84+
}
85+
return false;
86+
}

0 commit comments

Comments
 (0)