Skip to content

Commit 5425dc1

Browse files
committed
Changed the way SourceFile is accessed
1 parent 0c397bc commit 5425dc1

File tree

3 files changed

+38
-45
lines changed

3 files changed

+38
-45
lines changed

include/swift/IDE/Utils.h

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -160,6 +160,7 @@ struct ResolvedCursorInfo {
160160
ValueDecl *ValueD = nullptr;
161161
TypeDecl *CtorTyRef = nullptr;
162162
ExtensionDecl *ExtTyRef = nullptr;
163+
SourceFile *SF = nullptr;
163164
ModuleEntity Mod;
164165
SourceLoc Loc;
165166
bool IsRef = true;
@@ -174,6 +175,7 @@ struct ResolvedCursorInfo {
174175
ResolvedCursorInfo(ValueDecl *ValueD,
175176
TypeDecl *CtorTyRef,
176177
ExtensionDecl *ExtTyRef,
178+
SourceFile *SF,
177179
SourceLoc Loc,
178180
bool IsRef,
179181
Type Ty,
@@ -182,21 +184,26 @@ struct ResolvedCursorInfo {
182184
ValueD(ValueD),
183185
CtorTyRef(CtorTyRef),
184186
ExtTyRef(ExtTyRef),
187+
SF(SF),
185188
Loc(Loc),
186189
IsRef(IsRef),
187190
Ty(Ty),
188191
DC(ValueD->getDeclContext()),
189192
ContainerType(ContainerType) {}
190193
ResolvedCursorInfo(ModuleEntity Mod,
194+
SourceFile *SF,
191195
SourceLoc Loc) :
192196
Kind(CursorInfoKind::ModuleRef),
197+
SF(SF),
193198
Mod(Mod),
194-
Loc(Loc) { }
195-
ResolvedCursorInfo(Stmt *TrailingStmt) :
199+
Loc(Loc) {}
200+
ResolvedCursorInfo(Stmt *TrailingStmt, SourceFile *SF) :
196201
Kind(CursorInfoKind::StmtStart),
202+
SF(SF),
197203
TrailingStmt(TrailingStmt) {}
198-
ResolvedCursorInfo(Expr* TrailingExpr) :
204+
ResolvedCursorInfo(Expr* TrailingExpr, SourceFile *SF) :
199205
Kind(CursorInfoKind::ExprStart),
206+
SF(SF),
200207
TrailingExpr(TrailingExpr) {}
201208
bool isValid() const { return !isInvalid(); }
202209
bool isInvalid() const { return Kind == CursorInfoKind::Invalid; }

lib/IDE/Refactoring.cpp

Lines changed: 23 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -633,15 +633,6 @@ class TokenBasedRefactoringAction : public RefactoringAction {
633633
}
634634
};
635635

636-
struct ApplicabilityContext {
637-
DiagnosticEngine &DiagEngine;
638-
SourceFile *SF;
639-
ApplicabilityContext(DiagnosticEngine &DiagEngine,
640-
SourceFile *SF):
641-
DiagEngine(DiagEngine),
642-
SF(SF) {}
643-
};
644-
645636
#define CURSOR_REFACTORING(KIND, NAME, ID) \
646637
class RefactoringAction##KIND: public TokenBasedRefactoringAction { \
647638
public: \
@@ -650,10 +641,9 @@ class RefactoringAction##KIND: public TokenBasedRefactoringAction { \
650641
DiagnosticConsumer &DiagConsumer) : \
651642
TokenBasedRefactoringAction(MD, Opts, EditConsumer, DiagConsumer) {} \
652643
bool performChange() override; \
653-
static bool isApplicable(ResolvedCursorInfo Tok, ApplicabilityContext &ACtx);\
644+
static bool isApplicable(ResolvedCursorInfo Tok, DiagnosticEngine &Diag); \
654645
bool isApplicable() { \
655-
auto Context = ApplicabilityContext(DiagEngine, TheFile); \
656-
return RefactoringAction##KIND::isApplicable(CursorInfo, Context) ; \
646+
return RefactoringAction##KIND::isApplicable(CursorInfo, DiagEngine) ; \
657647
} \
658648
};
659649
#include "swift/IDE/RefactoringKinds.def"
@@ -679,16 +669,15 @@ class RefactoringAction##KIND: public RangeBasedRefactoringAction { \
679669
DiagnosticConsumer &DiagConsumer) : \
680670
RangeBasedRefactoringAction(MD, Opts, EditConsumer, DiagConsumer) {} \
681671
bool performChange() override; \
682-
static bool isApplicable(ResolvedRangeInfo Info, ApplicabilityContext &ACtx);\
672+
static bool isApplicable(ResolvedRangeInfo Info, DiagnosticEngine &Diag); \
683673
bool isApplicable() { \
684-
auto Context = ApplicabilityContext(DiagEngine, TheFile); \
685-
return RefactoringAction##KIND::isApplicable(RangeInfo, Context) ; \
674+
return RefactoringAction##KIND::isApplicable(RangeInfo, DiagEngine) ; \
686675
} \
687676
};
688677
#include "swift/IDE/RefactoringKinds.def"
689678

690679
bool RefactoringActionLocalRename::
691-
isApplicable(ResolvedCursorInfo CursorInfo, ApplicabilityContext &ACtx) {
680+
isApplicable(ResolvedCursorInfo CursorInfo, DiagnosticEngine &Diag) {
692681
if (CursorInfo.Kind != CursorInfoKind::ValueRef)
693682
return false;
694683
auto RenameOp = getAvailableRenameForDecl(CursorInfo.ValueD);
@@ -887,7 +876,7 @@ ExtractCheckResult checkExtractConditions(ResolvedRangeInfo &RangeInfo,
887876
}
888877

889878
bool RefactoringActionExtractFunction::
890-
isApplicable(ResolvedRangeInfo Info, ApplicabilityContext &ACtx) {
879+
isApplicable(ResolvedRangeInfo Info, DiagnosticEngine &Diag) {
891880
switch (Info.Kind) {
892881
case RangeKind::PartOfExpression:
893882
case RangeKind::SingleDecl:
@@ -896,7 +885,7 @@ isApplicable(ResolvedRangeInfo Info, ApplicabilityContext &ACtx) {
896885
case RangeKind::SingleExpression:
897886
case RangeKind::SingleStatement:
898887
case RangeKind::MultiStatement: {
899-
return checkExtractConditions(Info, ACtx.DiagEngine).
888+
return checkExtractConditions(Info, Diag).
900889
success({CannotExtractReason::VoidType});
901890
}
902891
}
@@ -1414,14 +1403,14 @@ bool RefactoringActionExtractExprBase::performChange() {
14141403
}
14151404

14161405
bool RefactoringActionExtractExpr::
1417-
isApplicable(ResolvedRangeInfo Info, ApplicabilityContext &ACtx) {
1406+
isApplicable(ResolvedRangeInfo Info, DiagnosticEngine &Diag) {
14181407
switch (Info.Kind) {
14191408
case RangeKind::SingleExpression:
14201409
// We disallow extract literal expression for two reasons:
14211410
// (1) since we print the type for extracted expression, the type of a
14221411
// literal may print as "int2048" where it is not typically users' choice;
14231412
// (2) Extracting one literal provides little value for users.
1424-
return checkExtractConditions(Info, ACtx.DiagEngine).success();
1413+
return checkExtractConditions(Info, Diag).success();
14251414
case RangeKind::PartOfExpression:
14261415
case RangeKind::SingleDecl:
14271416
case RangeKind::SingleStatement:
@@ -1438,10 +1427,10 @@ bool RefactoringActionExtractExpr::performChange() {
14381427
}
14391428

14401429
bool RefactoringActionExtractRepeatedExpr::
1441-
isApplicable(ResolvedRangeInfo Info, ApplicabilityContext &ACtx) {
1430+
isApplicable(ResolvedRangeInfo Info, DiagnosticEngine &Diag) {
14421431
switch (Info.Kind) {
14431432
case RangeKind::SingleExpression:
1444-
return checkExtractConditions(Info, ACtx.DiagEngine).
1433+
return checkExtractConditions(Info, Diag).
14451434
success({CannotExtractReason::Literal});
14461435
case RangeKind::PartOfExpression:
14471436
case RangeKind::SingleDecl:
@@ -1532,7 +1521,7 @@ static CollapsibleNestedIfInfo findCollapseNestedIfTarget(ResolvedCursorInfo Cur
15321521
}
15331522

15341523
bool RefactoringActionCollapseNestedIfExpr::
1535-
isApplicable(ResolvedCursorInfo Tok, ApplicabilityContext &ACtx) {
1524+
isApplicable(ResolvedCursorInfo Tok, DiagnosticEngine &Diag) {
15361525
return findCollapseNestedIfTarget(Tok).isValid();
15371526
}
15381527

@@ -1649,7 +1638,7 @@ static void interpolatedExpressionForm(Expr *E, SourceManager &SM,
16491638
}
16501639

16511640
bool RefactoringActionConvertStringsConcatenationToInterpolation::
1652-
isApplicable(ResolvedRangeInfo Info, ApplicabilityContext &ACtx) {
1641+
isApplicable(ResolvedRangeInfo Info, DiagnosticEngine &Diag) {
16531642
auto RangeContext = Info.RangeContext;
16541643
if (RangeContext) {
16551644
auto &Ctx = Info.RangeContext->getASTContext();
@@ -1762,7 +1751,7 @@ getUnsatisfiedRequirements(const DeclContext *DC) {
17621751
}
17631752

17641753
bool RefactoringActionFillProtocolStub::
1765-
isApplicable(ResolvedCursorInfo Tok, ApplicabilityContext &ACtx) {
1754+
isApplicable(ResolvedCursorInfo Tok, DiagnosticEngine &Diag) {
17661755
return FillProtocolStubContext::getContextFromCursorInfo(Tok).canProceed();
17671756
};
17681757

@@ -1879,8 +1868,7 @@ static SwitchStmt* findEnclosingSwitchStmt(CaseStmt *CS,
18791868
}
18801869

18811870
bool RefactoringActionExpandDefault::
1882-
isApplicable(ResolvedCursorInfo CursorInfo, ApplicabilityContext &ACtx) {
1883-
auto &Diag = ACtx.DiagEngine;
1871+
isApplicable(ResolvedCursorInfo CursorInfo, DiagnosticEngine &Diag) {
18841872
auto Exit = [&](bool Applicable) {
18851873
if (!Applicable)
18861874
Diag.diagnose(SourceLoc(), diag::invalid_default_location);
@@ -1890,8 +1878,8 @@ isApplicable(ResolvedCursorInfo CursorInfo, ApplicabilityContext &ACtx) {
18901878
return Exit(false);
18911879
if (auto *CS = dyn_cast<CaseStmt>(CursorInfo.TrailingStmt)) {
18921880
auto EnclosingSwitchStmt = findEnclosingSwitchStmt(CS,
1893-
ACtx.SF,
1894-
ACtx.DiagEngine);
1881+
CursorInfo.SF,
1882+
Diag);
18951883
if (!EnclosingSwitchStmt)
18961884
return false;
18971885
auto EnumD = getEnumDeclFromSwitchStmt(EnclosingSwitchStmt);
@@ -1917,7 +1905,7 @@ bool RefactoringActionExpandDefault::performChange() {
19171905
}
19181906

19191907
bool RefactoringActionExpandSwitchCases::
1920-
isApplicable(ResolvedCursorInfo CursorInfo, ApplicabilityContext &ACtx) {
1908+
isApplicable(ResolvedCursorInfo CursorInfo, DiagnosticEngine &DiagEngine) {
19211909
if (!CursorInfo.TrailingStmt)
19221910
return false;
19231911
if (auto *Switch = dyn_cast<SwitchStmt>(CursorInfo.TrailingStmt)) {
@@ -1978,7 +1966,7 @@ static Expr *findLocalizeTarget(ResolvedCursorInfo CursorInfo) {
19781966
}
19791967

19801968
bool RefactoringActionLocalizeString::
1981-
isApplicable(ResolvedCursorInfo Tok, ApplicabilityContext &ACtx) {
1969+
isApplicable(ResolvedCursorInfo Tok, DiagnosticEngine &Diag) {
19821970
return findLocalizeTarget(Tok);
19831971
}
19841972

@@ -2022,7 +2010,7 @@ static CharSourceRange
20222010
}
20232011

20242012
bool RefactoringActionConvertToDoCatch::
2025-
isApplicable(ResolvedCursorInfo Tok, ApplicabilityContext &ACtx) {
2013+
isApplicable(ResolvedCursorInfo Tok, DiagnosticEngine &Diag) {
20262014
if (!Tok.TrailingExpr)
20272015
return false;
20282016
return isa<ForceTryExpr>(Tok.TrailingExpr);
@@ -2110,7 +2098,7 @@ static void insertUnderscoreInDigits(StringRef Digits,
21102098
}
21112099

21122100
bool RefactoringActionSimplifyNumberLiteral::
2113-
isApplicable(ResolvedCursorInfo Tok, ApplicabilityContext &ACtx) {
2101+
isApplicable(ResolvedCursorInfo Tok, DiagnosticEngine &Diag) {
21142102
if (auto *Literal = getTrailingNumberLiteral(Tok)) {
21152103
llvm::SmallString<64> Buffer;
21162104
llvm::raw_svector_ostream OS(Buffer);
@@ -2326,9 +2314,8 @@ collectAvailableRefactorings(SourceFile *SF,
23262314
}
23272315
}
23282316
DiagnosticEngine DiagEngine(SF->getASTContext().SourceMgr);
2329-
auto Context = ApplicabilityContext(DiagEngine, SF);
23302317
#define CURSOR_REFACTORING(KIND, NAME, ID) \
2331-
if (RefactoringAction##KIND::isApplicable(CursorInfo, Context)) \
2318+
if (RefactoringAction##KIND::isApplicable(CursorInfo, DiagEngine)) \
23322319
AllKinds.push_back(RefactoringKind::KIND);
23332320
#include "swift/IDE/RefactoringKinds.def"
23342321

@@ -2364,7 +2351,6 @@ collectAvailableRefactorings(SourceFile *SF, RangeConfig Range,
23642351
ASTContext &Ctx = SF->getASTContext();
23652352
SourceManager &SM = Ctx.SourceMgr;
23662353
DiagnosticEngine DiagEngine(SM);
2367-
auto Context = ApplicabilityContext(DiagEngine, SF);
23682354
std::for_each(DiagConsumers.begin(), DiagConsumers.end(),
23692355
[&](DiagnosticConsumer *Con) { DiagEngine.addConsumer(*Con); });
23702356

@@ -2373,7 +2359,7 @@ collectAvailableRefactorings(SourceFile *SF, RangeConfig Range,
23732359
ResolvedRangeInfo Result = Resolver.resolve();
23742360

23752361
#define RANGE_REFACTORING(KIND, NAME, ID) \
2376-
if (RefactoringAction##KIND::isApplicable(Result, Context)) \
2362+
if (RefactoringAction##KIND::isApplicable(Result, DiagEngine)) \
23772363
Scratch.push_back(RefactoringKind::KIND);
23782364
#include "swift/IDE/RefactoringKinds.def"
23792365

lib/IDE/SwiftSourceDocInfo.cpp

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -80,15 +80,15 @@ bool CursorInfoResolver::tryResolve(ValueDecl *D, TypeDecl *CtorTyRef,
8080
return false;
8181

8282
if (Loc == LocToResolve) {
83-
CursorInfo = { D, CtorTyRef, ExtTyRef, Loc, IsRef, Ty, ContainerType };
83+
CursorInfo = { D, CtorTyRef, ExtTyRef, &SrcFile, Loc, IsRef, Ty, ContainerType };
8484
return true;
8585
}
8686
return false;
8787
}
8888

8989
bool CursorInfoResolver::tryResolve(ModuleEntity Mod, SourceLoc Loc) {
9090
if (Loc == LocToResolve) {
91-
CursorInfo = { Mod, Loc };
91+
CursorInfo = { Mod, &SrcFile, Loc };
9292
return true;
9393
}
9494
return false;
@@ -97,13 +97,13 @@ bool CursorInfoResolver::tryResolve(ModuleEntity Mod, SourceLoc Loc) {
9797
bool CursorInfoResolver::tryResolve(Stmt *St) {
9898
if (auto *LST = dyn_cast<LabeledStmt>(St)) {
9999
if (LST->getStartLoc() == LocToResolve) {
100-
CursorInfo = { St };
100+
CursorInfo = { St, &SrcFile };
101101
return true;
102102
}
103103
}
104104
if (auto *CS = dyn_cast<CaseStmt>(St)) {
105105
if (CS->getStartLoc() == LocToResolve) {
106-
CursorInfo = { St };
106+
CursorInfo = { St, &SrcFile };
107107
return true;
108108
}
109109
}
@@ -211,7 +211,7 @@ bool CursorInfoResolver::walkToExprPost(Expr *E) {
211211
return false;
212212
if (!TrailingExprStack.empty() && TrailingExprStack.back() == E) {
213213
// We return the outtermost expression in the token info.
214-
CursorInfo = { TrailingExprStack.front() };
214+
CursorInfo = { TrailingExprStack.front(), &SrcFile };
215215
return false;
216216
}
217217
return true;

0 commit comments

Comments
 (0)