Skip to content

Commit

Permalink
[IDE] Check whether an AST node contains the IDE inspection point bas…
Browse files Browse the repository at this point in the history
…ed on `CharSourceRange`

This fixes an issue if the range ends with a string literal that contains the IDE inspection target. In that case the end of the range will point to the start of the string literal but the IDE inspection target is inside the string literal and thus after the range’s end.
  • Loading branch information
ahoppen committed Feb 20, 2023
1 parent f210a8e commit 8bdf68d
Show file tree
Hide file tree
Showing 9 changed files with 64 additions and 28 deletions.
16 changes: 12 additions & 4 deletions include/swift/Basic/SourceManager.h
Original file line number Diff line number Diff line change
Expand Up @@ -214,17 +214,25 @@ class SourceManager {
(isBeforeInBuffer(R.Start, Loc) && isBeforeInBuffer(Loc, R.End));
}

/// Returns true if range \c R contains the location \c Loc. The location
/// \c Loc should point at the beginning of the token.
bool rangeContainsTokenLoc(CharSourceRange R, SourceLoc Loc) const {
return Loc == R.getStart() || (isBeforeInBuffer(R.getStart(), Loc) &&
isBeforeInBuffer(Loc, R.getEnd()));
}

/// Returns true if range \c Enclosing contains the range \c Inner.
bool rangeContains(SourceRange Enclosing, SourceRange Inner) const {
return rangeContainsTokenLoc(Enclosing, Inner.Start) &&
rangeContainsTokenLoc(Enclosing, Inner.End);
}

/// Returns true if range \p R contains the code-completion location, if any.
bool rangeContainsIDEInspectionTarget(SourceRange R) const {
return IDEInspectionTargetBufferID
? rangeContainsTokenLoc(R, getIDEInspectionTargetLoc())
: false;
bool rangeContainsIDEInspectionTarget(CharSourceRange R) const {
if (!IDEInspectionTargetBufferID) {
return false;
}
return rangeContainsTokenLoc(R, getIDEInspectionTargetLoc());
}

/// Returns the buffer ID for the specified *valid* location.
Expand Down
8 changes: 8 additions & 0 deletions include/swift/Sema/CompletionContextFinder.h
Original file line number Diff line number Diff line change
Expand Up @@ -126,6 +126,14 @@ class CompletionContextFinder : public ASTWalker {
}
};


/// Returns \c true if \p range is valid and contains the IDE inspection
/// target. This performs the underlying check based on \c CharSourceRange
/// to make sure we correctly return \c true if the ide inspection target
/// is inside a string literal that's the last token in \p range.
bool containsIDEInspectionTarget(SourceRange range,
const SourceManager &SourceMgr);

} // end namespace swift

#endif // SWIFT_SEMA_COMPLETIONCONTEXTFINDER_H
9 changes: 8 additions & 1 deletion lib/AST/Decl.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -8166,8 +8166,15 @@ BraceStmt *AbstractFunctionDecl::getBody(bool canSynthesize) const {

// Don't allow getBody() to trigger parsing of an unparsed body containing the
// IDE inspection location.
// FIXME: We should be properly constructing the range of the the body as a
// CharSourceRange but we can't because we don't have access to the lexer
// here. Using the end location of the SourceRange works good enough here
// because the last token is a '}' and the IDE inspection point is not inside
// the closing brace.
if (getBodyKind() == BodyKind::Unparsed &&
ctx.SourceMgr.rangeContainsIDEInspectionTarget(getBodySourceRange())) {
ctx.SourceMgr.rangeContainsIDEInspectionTarget(
CharSourceRange(ctx.SourceMgr, getBodySourceRange().Start,
getBodySourceRange().End))) {
return nullptr;
}

Expand Down
4 changes: 3 additions & 1 deletion lib/Parse/ParseDecl.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -7986,8 +7986,10 @@ void Parser::parseAbstractFunctionBody(AbstractFunctionDecl *AFD) {
auto BodyPreviousLoc = PreviousLoc;
SourceRange BodyRange(Tok.getLoc());
auto setIDEInspectionDelayedDeclStateIfNeeded = [&] {
auto CharBodyRange =
Lexer::getCharSourceRangeFromSourceRange(SourceMgr, BodyRange);
if (!isIDEInspectionFirstPass() ||
!SourceMgr.rangeContainsIDEInspectionTarget(BodyRange)) {
!SourceMgr.rangeContainsIDEInspectionTarget(CharBodyRange)) {
return;
}
if (State->hasIDEInspectionDelayedDeclState())
Expand Down
8 changes: 6 additions & 2 deletions lib/Parse/ParseExpr.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1433,7 +1433,9 @@ Parser::parseExprPostfixSuffix(ParserResult<Expr> Result, bool isExprBasic,
assert(activeElements.size() == 1 && activeElements[0].is<Expr *>());
auto expr = activeElements[0].get<Expr *>();
ParserStatus status(ICD);
if (SourceMgr.rangeContainsIDEInspectionTarget(expr->getSourceRange()) &&
auto charRange = Lexer::getCharSourceRangeFromSourceRange(
SourceMgr, expr->getSourceRange());
if (SourceMgr.rangeContainsIDEInspectionTarget(charRange) &&
L->isCodeCompletion())
status.setHasCodeCompletion();
hasBindOptional |= exprsWithBindOptional.contains(expr);
Expand Down Expand Up @@ -2826,7 +2828,9 @@ ParserResult<Expr> Parser::parseExprClosure() {
SmallVector<ASTNode, 4> bodyElements;
Status |= parseBraceItems(bodyElements, BraceItemListKind::Brace);

if (SourceMgr.rangeContainsIDEInspectionTarget({leftBrace, PreviousLoc})) {
if (SourceMgr.rangeContainsIDEInspectionTarget(
Lexer::getCharSourceRangeFromSourceRange(SourceMgr,
{leftBrace, PreviousLoc}))) {
// Ignore 'IDEInspectionDelayedDeclState' inside closures.
// Completions inside functions body inside closures at top level should
// be considered top-level completions.
Expand Down
6 changes: 3 additions & 3 deletions lib/Sema/BuilderTransform.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -16,9 +16,8 @@
//===----------------------------------------------------------------------===//

#include "MiscDiagnostics.h"
#include "TypeChecker.h"
#include "TypeCheckAvailability.h"
#include "swift/Sema/IDETypeChecking.h"
#include "TypeChecker.h"
#include "swift/AST/ASTPrinter.h"
#include "swift/AST/ASTVisitor.h"
#include "swift/AST/ASTWalker.h"
Expand All @@ -27,14 +26,15 @@
#include "swift/AST/ParameterList.h"
#include "swift/AST/TypeCheckRequests.h"
#include "swift/Sema/ConstraintSystem.h"
#include "swift/Sema/IDETypeChecking.h"
#include "swift/Sema/SolutionResult.h"
#include "llvm/ADT/DenseMap.h"
#include "llvm/ADT/SmallVector.h"
#include <iterator>
#include <map>
#include <memory>
#include <utility>
#include <tuple>
#include <utility>

using namespace swift;
using namespace constraints;
Expand Down
9 changes: 9 additions & 0 deletions lib/Sema/CompletionContextFinder.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
//===----------------------------------------------------------------------===//

#include "swift/Sema/CompletionContextFinder.h"
#include "swift/Parse/Lexer.h"

using namespace swift;
using Fallback = CompletionContextFinder::Fallback;
Expand Down Expand Up @@ -132,3 +133,11 @@ Optional<Fallback> CompletionContextFinder::getFallbackCompletionExpr() const {
return Fallback{getCompletionExpr(), fallbackDC, separatePrecheck};
return None;
}

bool swift::containsIDEInspectionTarget(SourceRange range,
const SourceManager &SourceMgr) {
if (range.isInvalid())
return false;
auto charRange = Lexer::getCharSourceRangeFromSourceRange(SourceMgr, range);
return SourceMgr.rangeContainsIDEInspectionTarget(charRange);
}
13 changes: 5 additions & 8 deletions lib/Sema/ConstraintSystem.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@
#include "swift/Basic/Statistic.h"
#include "swift/Sema/CSFix.h"
#include "swift/Sema/ConstraintGraph.h"
#include "swift/Sema/IDETypeChecking.h"
#include "swift/Sema/SolutionResult.h"
#include "llvm/ADT/SetVector.h"
#include "llvm/ADT/SmallSet.h"
Expand Down Expand Up @@ -353,18 +354,14 @@ ConstraintSystem::getAlternativeLiteralTypes(KnownProtocolKind kind,
}

bool ConstraintSystem::containsIDEInspectionTarget(ASTNode node) const {
SourceRange range = node.getSourceRange();
if (range.isInvalid())
return false;
return Context.SourceMgr.rangeContainsIDEInspectionTarget(range);
return swift::containsIDEInspectionTarget(node.getSourceRange(),
Context.SourceMgr);
}

bool ConstraintSystem::containsIDEInspectionTarget(
const ArgumentList *args) const {
SourceRange range = args->getSourceRange();
if (range.isInvalid())
return false;
return Context.SourceMgr.rangeContainsIDEInspectionTarget(range);
return swift::containsIDEInspectionTarget(args->getSourceRange(),
Context.SourceMgr);
}

ConstraintLocator *ConstraintSystem::getConstraintLocator(
Expand Down
19 changes: 10 additions & 9 deletions lib/Sema/TypeCheckCodeCompletion.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -14,14 +14,13 @@
//
//===----------------------------------------------------------------------===//

#include "swift/Subsystems.h"
#include "TypeChecker.h"
#include "TypeCheckObjC.h"
#include "TypeCheckType.h"
#include "CodeSynthesis.h"
#include "MiscDiagnostics.h"
#include "swift/AST/ASTWalker.h"
#include "TypeCheckObjC.h"
#include "TypeCheckType.h"
#include "TypeChecker.h"
#include "swift/AST/ASTVisitor.h"
#include "swift/AST/ASTWalker.h"
#include "swift/AST/Attr.h"
#include "swift/AST/DiagnosticSuppression.h"
#include "swift/AST/ExistentialLayout.h"
Expand All @@ -37,13 +36,15 @@
#include "swift/AST/Type.h"
#include "swift/AST/TypeCheckRequests.h"
#include "swift/Basic/Defer.h"
#include "swift/Basic/Statistic.h"
#include "swift/Basic/STLExtras.h"
#include "swift/Basic/Statistic.h"
#include "swift/Parse/IDEInspectionCallbacks.h"
#include "swift/Parse/Lexer.h"
#include "swift/Sema/IDETypeChecking.h"
#include "swift/Sema/ConstraintSystem.h"
#include "swift/Sema/CompletionContextFinder.h"
#include "swift/Sema/ConstraintSystem.h"
#include "swift/Sema/IDETypeChecking.h"
#include "swift/Strings.h"
#include "swift/Subsystems.h"
#include "llvm/ADT/DenseMap.h"
#include "llvm/ADT/PointerUnion.h"
#include "llvm/ADT/SmallSet.h"
Expand Down Expand Up @@ -565,7 +566,7 @@ bool TypeChecker::typeCheckForCodeCompletion(
{
auto range = target.getSourceRange();
if (range.isInvalid() ||
!Context.SourceMgr.rangeContainsIDEInspectionTarget(range))
!containsIDEInspectionTarget(range, Context.SourceMgr))
return false;
}

Expand Down

0 comments on commit 8bdf68d

Please sign in to comment.