Skip to content

Commit 2cd2be5

Browse files
committed
Cope with ASTScope being disabled in statement checker.
Code completion still disables ASTScope lookup, so fall back to using the old state in such cases. We'll remove all of this code once code completion (and LLDB?) move off of the old lookup mechanisms.
1 parent 9709af5 commit 2cd2be5

File tree

1 file changed

+38
-13
lines changed

1 file changed

+38
-13
lines changed

lib/Sema/TypeCheckStmt.cpp

Lines changed: 38 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -302,6 +302,11 @@ static bool isDefer(Optional<AnyFunctionRef> enclosingFunc) {
302302
/// same label.
303303
static void checkLabeledStmtShadowing(
304304
ASTContext &ctx, SourceFile *sourceFile, LabeledStmt *ls) {
305+
// If ASTScope lookup is disabled, don't do this check at all.
306+
// FIXME: Enable ASTScope lookup everywhere.
307+
if (!ctx.LangOpts.EnableASTScopeLookup)
308+
return;
309+
305310
auto name = ls->getLabelInfo().Name;
306311
if (name.empty() || !sourceFile || ls->getStartLoc().isInvalid())
307312
return;
@@ -355,11 +360,23 @@ static LabeledStmt *findBreakOrContinueStmtTarget(
355360
ASTContext &ctx, SourceFile *sourceFile,
356361
SourceLoc loc, Identifier targetName, SourceLoc targetLoc,
357362
bool isContinue,
358-
Optional<AnyFunctionRef> enclosingFunc) {
363+
Optional<AnyFunctionRef> enclosingFunc,
364+
ArrayRef<LabeledStmt *> oldActiveLabeledStmts) {
359365
TopCollection<unsigned, LabeledStmt *> labelCorrections(3);
360366

367+
// Retrieve the active set of labeled statements.
368+
// FIXME: Once everything uses ASTScope lookup, \c oldActiveLabeledStmts
369+
// can go away.
370+
SmallVector<LabeledStmt *, 4> activeLabeledStmts;
371+
if (ctx.LangOpts.EnableASTScopeLookup) {
372+
activeLabeledStmts = ASTScope::lookupLabeledStmts(sourceFile, loc);
373+
} else {
374+
activeLabeledStmts.insert(
375+
activeLabeledStmts.end(),
376+
oldActiveLabeledStmts.rbegin(), oldActiveLabeledStmts.rend());
377+
}
378+
361379
// Pick the nearest break target that matches the specified name.
362-
auto activeLabeledStmts = ASTScope::lookupLabeledStmts(sourceFile, loc);
363380
if (targetName.empty()) {
364381
for (auto labeledStmt : activeLabeledStmts) {
365382
// 'break' with no label looks through non-loop structures
@@ -481,7 +498,9 @@ class StmtChecker : public StmtVisitor<StmtChecker, Stmt*> {
481498

482499
// Verify that the ASTScope-based query for active labeled statements
483500
// is equivalent to what we have here.
484-
if (LS->getStartLoc().isValid() && sourceFile) {
501+
if (LS->getStartLoc().isValid() && sourceFile &&
502+
SC.getASTContext().LangOpts.EnableASTScopeLookup &&
503+
!SC.getASTContext().Diags.hadAnyError()) {
485504
// The labeled statements from ASTScope lookup have the
486505
// innermost labeled statement first, so reverse it to
487506
// match the data structure maintained here.
@@ -858,7 +877,7 @@ class StmtChecker : public StmtVisitor<StmtChecker, Stmt*> {
858877
if (auto target = findBreakOrContinueStmtTarget(
859878
getASTContext(), DC->getParentSourceFile(), S->getLoc(),
860879
S->getTargetName(), S->getTargetLoc(), /*isContinue=*/false,
861-
TheFunc)) {
880+
TheFunc, ActiveLabeledStmts)) {
862881
S->setTarget(target);
863882
}
864883

@@ -869,7 +888,7 @@ class StmtChecker : public StmtVisitor<StmtChecker, Stmt*> {
869888
if (auto target = findBreakOrContinueStmtTarget(
870889
getASTContext(), DC->getParentSourceFile(), S->getLoc(),
871890
S->getTargetName(), S->getTargetLoc(), /*isContinue=*/true,
872-
TheFunc)) {
891+
TheFunc, ActiveLabeledStmts)) {
873892
S->setTarget(target);
874893
}
875894

@@ -878,15 +897,21 @@ class StmtChecker : public StmtVisitor<StmtChecker, Stmt*> {
878897

879898
Stmt *visitFallthroughStmt(FallthroughStmt *S) {
880899
auto sourceFile = DC->getParentSourceFile();
881-
auto activeLabeledStmts = ASTScope::lookupLabeledStmts(
882-
sourceFile, S->getLoc());
883-
auto numSwitches = llvm::count_if(activeLabeledStmts,
884-
[](LabeledStmt *labeledStmt) {
885-
return isa<SwitchStmt>(labeledStmt);
886-
});
887-
assert(numSwitches == SwitchLevel);
900+
bool hasAnySwitches;
901+
if (getASTContext().LangOpts.EnableASTScopeLookup) {
902+
auto activeLabeledStmts = ASTScope::lookupLabeledStmts(
903+
sourceFile, S->getLoc());
904+
auto numSwitches = llvm::count_if(activeLabeledStmts,
905+
[](LabeledStmt *labeledStmt) {
906+
return isa<SwitchStmt>(labeledStmt);
907+
});
908+
assert(numSwitches == SwitchLevel);
909+
hasAnySwitches = numSwitches > 0;
910+
} else {
911+
hasAnySwitches = SwitchLevel > 0;
912+
}
888913

889-
if (!numSwitches) {
914+
if (!hasAnySwitches) {
890915
getASTContext().Diags.diagnose(S->getLoc(),
891916
diag::fallthrough_outside_switch);
892917
return nullptr;

0 commit comments

Comments
 (0)