1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318
|
//===--- DebuggerTestingTransform.cpp - Transform for debugger testing ----===//
//
// This source file is part of the Swift.org open source project
//
// Copyright (c) 2014 - 2018 Apple Inc. and the Swift project authors
// Licensed under Apache License v2.0 with Runtime Library Exception
//
// See https://swift.org/LICENSE.txt for license information
// See https://swift.org/CONTRIBUTORS.txt for the list of Swift project authors
//
//===----------------------------------------------------------------------===//
///
/// \file This transform inserts instrumentation which the debugger can use to
/// test its expression evaluation facilities.
///
//===----------------------------------------------------------------------===//
#include "CodeSynthesis.h"
#include "swift/AST/ASTContext.h"
#include "swift/AST/ASTNode.h"
#include "swift/AST/ASTWalker.h"
#include "swift/AST/Decl.h"
#include "swift/AST/DeclContext.h"
#include "swift/AST/Expr.h"
#include "swift/AST/Module.h"
#include "swift/AST/ParameterList.h"
#include "swift/AST/SourceFile.h"
#include "swift/AST/Stmt.h"
#include "swift/Subsystems.h"
#include "TypeChecker.h"
using namespace swift;
ASTWalker::PostWalkResult<Expr *>
DiscriminatorFinder::walkToExprPost(Expr *E) {
auto *ACE = dyn_cast<AbstractClosureExpr>(E);
if (!ACE)
return Action::Continue(E);
unsigned Discriminator = ACE->getDiscriminator();
assert(Discriminator != AbstractClosureExpr::InvalidDiscriminator &&
"Existing closures should have valid discriminators");
if (Discriminator >= NextDiscriminator)
NextDiscriminator = Discriminator + 1;
if (FirstDiscriminator == AbstractClosureExpr::InvalidDiscriminator ||
Discriminator < FirstDiscriminator)
FirstDiscriminator = Discriminator;
return Action::Continue(E);
}
unsigned DiscriminatorFinder::getNextDiscriminator() {
if (NextDiscriminator == AbstractClosureExpr::InvalidDiscriminator)
llvm::report_fatal_error("Out of valid closure discriminators");
return NextDiscriminator++;
}
namespace {
/// Instrument decls with soundness-checks which the debugger can evaluate.
class DebuggerTestingTransform : public ASTWalker {
ASTContext &Ctx;
DiscriminatorFinder &DF;
std::vector<DeclContext *> LocalDeclContextStack;
const DeclNameRef StringForPrintObjectName;
const DeclNameRef DebuggerTestingCheckExpectName;
public:
DebuggerTestingTransform(ASTContext &Ctx, DiscriminatorFinder &DF)
: Ctx(Ctx), DF(DF),
StringForPrintObjectName(Ctx.getIdentifier("_stringForPrintObject")),
DebuggerTestingCheckExpectName(
Ctx.getIdentifier("_debuggerTestingCheckExpect")) {}
MacroWalking getMacroWalkingBehavior() const override {
return MacroWalking::Expansion;
}
PreWalkAction walkToDeclPre(Decl *D) override {
pushLocalDeclContext(D);
// Skip implicit decls, because the debugger isn't used to step through
// these.
if (D->isImplicit())
return Action::SkipNode();
// Whitelist the kinds of decls to transform.
// TODO: Expand the set of decls visited here.
if (auto *FD = dyn_cast<AbstractFunctionDecl>(D))
return Action::VisitNodeIf(FD->getTypecheckedBody());
if (auto *TLCD = dyn_cast<TopLevelCodeDecl>(D))
return Action::VisitNodeIf(TLCD->getBody());
if (isa<NominalTypeDecl>(D))
return Action::Continue();
return Action::SkipNode();
}
PostWalkAction walkToDeclPost(Decl *D) override {
popLocalDeclContext(D);
return Action::Continue();
}
PreWalkResult<Expr *> walkToExprPre(Expr *E) override {
pushLocalDeclContext(E);
// Whitelist the kinds of exprs to transform.
// TODO: Expand the set of exprs visited here.
if (auto *AE = dyn_cast<AssignExpr>(E))
return insertCheckExpect(AE, AE->getDest());
return Action::Continue(E);
}
PostWalkResult<Expr *> walkToExprPost(Expr *E) override {
popLocalDeclContext(E);
return Action::Continue(E);
}
private:
/// Return N as a local DeclContext if possible, or return nullptr.
DeclContext *getLocalDeclContext(ASTNode N) const {
DeclContext *DC = N.getAsDeclContext();
return (DC && DC->isLocalContext()) ? DC : nullptr;
}
/// If N is a local DeclContext, push it onto the context stack.
void pushLocalDeclContext(ASTNode N) {
if (auto *LDC = getLocalDeclContext(N))
LocalDeclContextStack.push_back(LDC);
}
/// If N is a local DeclContext, pop it off the context stack.
void popLocalDeclContext(ASTNode N) {
if (getLocalDeclContext(N))
LocalDeclContextStack.pop_back();
}
/// Get the current local DeclContext. This is used to create closures in the
/// right context.
DeclContext *getCurrentDeclContext() const {
assert(!LocalDeclContextStack.empty() && "Missing decl context");
return LocalDeclContextStack.back();
}
/// Try to extract a DeclRefExpr or MemberRefExpr from the expression.
Expr *extractDeclOrMemberRef(Expr *E) {
while (!isa<DeclRefExpr>(E) && !isa<MemberRefExpr>(E)) {
// TODO: Try more ways to extract interesting decl refs.
if (auto *Subscript = dyn_cast<SubscriptExpr>(E))
E = Subscript->getBase();
else if (auto *InOut = dyn_cast<InOutExpr>(E))
E = InOut->getSubExpr();
else if (auto *MemberRef = dyn_cast<MemberRefExpr>(E))
E = MemberRef->getBase();
else
return nullptr;
}
return E;
}
/// Attempt to create a functionally-equivalent replacement for OriginalExpr,
/// given that DstExpr identifies the target of some mutating update, and that
/// DstExpr is a subexpression of OriginalExpr.
///
/// The return value contains 1) a flag indicating whether or not to
/// recursively transform the children of the transformed expression, and 2)
/// the transformed expression itself.
PreWalkResult<Expr *> insertCheckExpect(Expr *OriginalExpr, Expr *DstExpr) {
auto *DstRef = extractDeclOrMemberRef(DstExpr);
if (!DstRef)
return Action::Continue(OriginalExpr);
ValueDecl *DstDecl;
if (auto *DRE = dyn_cast<DeclRefExpr>(DstRef))
DstDecl = DRE->getDecl();
else {
auto *MRE = cast<MemberRefExpr>(DstRef);
DstDecl = MRE->getMember().getDecl();
}
if (!DstDecl->hasName())
return Action::Continue(OriginalExpr);
// Don't capture variables which aren't default-initialized.
if (auto *VD = dyn_cast<VarDecl>(DstDecl))
if (!VD->isParentExecutabledInitialized() &&
!(isa<ParamDecl>(VD) &&
cast<ParamDecl>(VD)->isInOut()))
return Action::Continue(OriginalExpr);
// Rewrite the original expression into this:
// call
// closure {
// $OriginalExpr
// checkExpect("$Varname", _stringForPrintObject($Varname))
// }
// Create "$Varname".
llvm::SmallString<256> DstNameBuf;
const DeclName DstDN = DstDecl->getName();
StringRef DstName = Ctx.AllocateCopy(DstDN.getString(DstNameBuf));
assert(!DstName.empty() && "Varname must be non-empty");
Expr *Varname = new (Ctx) StringLiteralExpr(DstName, SourceRange());
Varname->setImplicit(true);
// Create _stringForPrintObject($Varname).
auto *PODeclRef = new (Ctx)
UnresolvedDeclRefExpr(StringForPrintObjectName,
DeclRefKind::Ordinary, DeclNameLoc());
std::function<DeclRefExpr *(DeclRefExpr *)> cloneDeclRef =
[&](DeclRefExpr *DRE) {
auto *ref = new (Ctx) DeclRefExpr(
DRE->getDeclRef(),
/*Loc=*/DeclNameLoc(),
/*Implicit=*/true, DRE->getAccessSemantics(), DRE->getType());
if (auto hopTarget = DRE->isImplicitlyAsync())
ref->setImplicitlyAsync(*hopTarget);
ref->setImplicitlyThrows(DRE->isImplicitlyThrows());
return ref;
};
std::function<MemberRefExpr *(MemberRefExpr *)> cloneMemberRef =
[&](MemberRefExpr *M) {
auto *base = M->getBase();
if (auto *DRE = dyn_cast<DeclRefExpr>(base))
base = cloneDeclRef(DRE);
else if (auto *M = dyn_cast<MemberRefExpr>(base))
base = cloneMemberRef(M);
auto *ref = new (Ctx)
MemberRefExpr(base, /*dotLoc=*/SourceLoc(), M->getDecl(),
/*loc=*/DeclNameLoc(),
/*Implicit=*/true, M->getAccessSemantics());
ref->setType(M->getType());
return ref;
};
// Let's make a copy of either decl or member ref without source
// information. It's invalid to have decl reference expressions
// reused, each reference should get a fresh expression.
if (auto *DRE = dyn_cast<DeclRefExpr>(DstRef)) {
DstRef = cloneDeclRef(DRE);
} else {
DstRef = cloneMemberRef(cast<MemberRefExpr>(DstRef));
}
auto *POArgList = ArgumentList::forImplicitUnlabeled(Ctx, {DstRef});
auto *POCall = CallExpr::createImplicit(Ctx, PODeclRef, POArgList);
POCall->setThrows(nullptr);
// Create the call to checkExpect.
UnresolvedDeclRefExpr *CheckExpectDRE = new (Ctx)
UnresolvedDeclRefExpr(DebuggerTestingCheckExpectName,
DeclRefKind::Ordinary, DeclNameLoc());
auto *CheckArgList =
ArgumentList::forImplicitUnlabeled(Ctx, {Varname, POCall});
auto *CheckExpectExpr =
CallExpr::createImplicit(Ctx, CheckExpectDRE, CheckArgList);
CheckExpectExpr->setThrows(nullptr);
// Create the closure.
auto *Params = ParameterList::createEmpty(Ctx);
auto *Closure = new (Ctx)
ClosureExpr(DeclAttributes(), SourceRange(), nullptr, Params,
SourceLoc(), SourceLoc(), /*thrownType=*/nullptr,
SourceLoc(), SourceLoc(), nullptr,
getCurrentDeclContext());
Closure->setImplicit(true);
Closure->setDiscriminator(DF.getNextDiscriminator());
// TODO: Save and return the value of $OriginalExpr.
ASTNode ClosureElements[] = {OriginalExpr, CheckExpectExpr};
auto *ClosureBody = BraceStmt::create(Ctx, SourceLoc(), ClosureElements,
SourceLoc(), /*Implicit=*/true);
Closure->setBody(ClosureBody);
// Call the closure.
auto *ClosureCall = CallExpr::createImplicitEmpty(Ctx, Closure);
ClosureCall->setThrows(nullptr);
// TODO: typeCheckExpression() seems to assign types to everything here,
// but may not be sufficient in some cases.
Expr *FinalExpr = ClosureCall;
if (!TypeChecker::typeCheckExpression(FinalExpr, getCurrentDeclContext(),
/*contextualInfo=*/{}))
llvm::report_fatal_error("Could not type-check instrumentation");
// Captures have to be computed after the closure is type-checked. This
// ensures that the type checker can infer <noescape> for captured values.
TypeChecker::computeCaptures(Closure);
return Action::SkipNode(FinalExpr);
}
};
} // end anonymous namespace
void swift::performDebuggerTestingTransform(SourceFile &SF) {
// Walk over all decls in the file to find the next available closure
// discriminator.
DiscriminatorFinder DF;
for (Decl *D : SF.getTopLevelDecls())
D->walk(DF);
// Instrument the decls with checkExpect() soundness-checks.
for (Decl *D : SF.getTopLevelDecls()) {
DebuggerTestingTransform Transform{D->getASTContext(), DF};
D->walk(Transform);
swift::verify(D);
}
}
|