Skip to content

[Clang] [NFC] Migrate visitors in ARCMigrate #116792

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 3 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
115 changes: 59 additions & 56 deletions clang/lib/ARCMigrate/ObjCMT.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -7,16 +7,16 @@
//===----------------------------------------------------------------------===//

#include "Transforms.h"
#include "clang/Analysis/RetainSummaryManager.h"
#include "clang/ARCMigrate/ARCMT.h"
#include "clang/ARCMigrate/ARCMTActions.h"
#include "clang/AST/ASTConsumer.h"
#include "clang/AST/ASTContext.h"
#include "clang/AST/Attr.h"
#include "clang/AST/DynamicRecursiveASTVisitor.h"
#include "clang/AST/NSAPI.h"
#include "clang/AST/ParentMap.h"
#include "clang/AST/RecursiveASTVisitor.h"
#include "clang/Analysis/DomainSpecific/CocoaConventions.h"
#include "clang/Analysis/RetainSummaryManager.h"
#include "clang/Basic/FileManager.h"
#include "clang/Edit/Commit.h"
#include "clang/Edit/EditedSource.h"
Expand Down Expand Up @@ -309,67 +309,68 @@ namespace {
return true;
}

class ObjCMigrator : public RecursiveASTVisitor<ObjCMigrator> {
ObjCMigrateASTConsumer &Consumer;
ParentMap &PMap;

public:
ObjCMigrator(ObjCMigrateASTConsumer &consumer, ParentMap &PMap)
: Consumer(consumer), PMap(PMap) { }

bool shouldVisitTemplateInstantiations() const { return false; }
bool shouldWalkTypesOfTypeLocs() const { return false; }
class ObjCMigrator : public DynamicRecursiveASTVisitor {
ObjCMigrateASTConsumer &Consumer;
ParentMap &PMap;

bool VisitObjCMessageExpr(ObjCMessageExpr *E) {
if (Consumer.ASTMigrateActions & FrontendOptions::ObjCMT_Literals) {
edit::Commit commit(*Consumer.Editor);
edit::rewriteToObjCLiteralSyntax(E, *Consumer.NSAPIObj, commit, &PMap);
Consumer.Editor->commit(commit);
public:
ObjCMigrator(ObjCMigrateASTConsumer &consumer, ParentMap &PMap)
: Consumer(consumer), PMap(PMap) {
ShouldVisitTemplateInstantiations = false;
ShouldWalkTypesOfTypeLocs = false;
}

if (Consumer.ASTMigrateActions & FrontendOptions::ObjCMT_Subscripting) {
edit::Commit commit(*Consumer.Editor);
edit::rewriteToObjCSubscriptSyntax(E, *Consumer.NSAPIObj, commit);
Consumer.Editor->commit(commit);
}
bool VisitObjCMessageExpr(ObjCMessageExpr *E) override {
if (Consumer.ASTMigrateActions & FrontendOptions::ObjCMT_Literals) {
edit::Commit commit(*Consumer.Editor);
edit::rewriteToObjCLiteralSyntax(E, *Consumer.NSAPIObj, commit, &PMap);
Consumer.Editor->commit(commit);
}

if (Consumer.ASTMigrateActions & FrontendOptions::ObjCMT_PropertyDotSyntax) {
edit::Commit commit(*Consumer.Editor);
rewriteToPropertyDotSyntax(E, Consumer.PP, *Consumer.NSAPIObj,
commit, &PMap);
Consumer.Editor->commit(commit);
}
if (Consumer.ASTMigrateActions & FrontendOptions::ObjCMT_Subscripting) {
edit::Commit commit(*Consumer.Editor);
edit::rewriteToObjCSubscriptSyntax(E, *Consumer.NSAPIObj, commit);
Consumer.Editor->commit(commit);
}

return true;
}
if (Consumer.ASTMigrateActions &
FrontendOptions::ObjCMT_PropertyDotSyntax) {
edit::Commit commit(*Consumer.Editor);
rewriteToPropertyDotSyntax(E, Consumer.PP, *Consumer.NSAPIObj, commit,
&PMap);
Consumer.Editor->commit(commit);
}

bool TraverseObjCMessageExpr(ObjCMessageExpr *E) {
// Do depth first; we want to rewrite the subexpressions first so that if
// we have to move expressions we will move them already rewritten.
for (Stmt *SubStmt : E->children())
if (!TraverseStmt(SubStmt))
return false;
return true;
}

return WalkUpFromObjCMessageExpr(E);
}
};
bool TraverseObjCMessageExpr(ObjCMessageExpr *E) override {
// Do depth first; we want to rewrite the subexpressions first so that if
// we have to move expressions we will move them already rewritten.
for (Stmt *SubStmt : E->children())
if (!TraverseStmt(SubStmt))
return false;

class BodyMigrator : public RecursiveASTVisitor<BodyMigrator> {
ObjCMigrateASTConsumer &Consumer;
std::unique_ptr<ParentMap> PMap;
return WalkUpFromObjCMessageExpr(E);
}
};

public:
BodyMigrator(ObjCMigrateASTConsumer &consumer) : Consumer(consumer) { }
class BodyMigrator : public DynamicRecursiveASTVisitor {
ObjCMigrateASTConsumer &Consumer;
std::unique_ptr<ParentMap> PMap;

bool shouldVisitTemplateInstantiations() const { return false; }
bool shouldWalkTypesOfTypeLocs() const { return false; }
public:
BodyMigrator(ObjCMigrateASTConsumer &consumer) : Consumer(consumer) {
ShouldVisitTemplateInstantiations = false;
ShouldWalkTypesOfTypeLocs = false;
}

bool TraverseStmt(Stmt *S) {
PMap.reset(new ParentMap(S));
ObjCMigrator(Consumer, *PMap).TraverseStmt(S);
return true;
}
};
bool TraverseStmt(Stmt *S) override {
PMap.reset(new ParentMap(S));
ObjCMigrator(Consumer, *PMap).TraverseStmt(S);
return true;
}
};
} // end anonymous namespace

void ObjCMigrateASTConsumer::migrateDecl(Decl *D) {
Expand Down Expand Up @@ -1672,12 +1673,14 @@ void ObjCMigrateASTConsumer::migrateAddMethodAnnotation(
}

namespace {
class SuperInitChecker : public RecursiveASTVisitor<SuperInitChecker> {
class SuperInitChecker : public DynamicRecursiveASTVisitor {
public:
bool shouldVisitTemplateInstantiations() const { return false; }
bool shouldWalkTypesOfTypeLocs() const { return false; }
SuperInitChecker() {
ShouldVisitTemplateInstantiations = false;
ShouldWalkTypesOfTypeLocs = false;
}

bool VisitObjCMessageExpr(ObjCMessageExpr *E) {
bool VisitObjCMessageExpr(ObjCMessageExpr *E) override {
if (E->getReceiverKind() == ObjCMessageExpr::SuperInstance) {
if (E->getMethodFamily() == OMF_init)
return false;
Expand Down
7 changes: 4 additions & 3 deletions clang/lib/ARCMigrate/TransAPIUses.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -16,9 +16,10 @@
//
//===----------------------------------------------------------------------===//

#include "Transforms.h"
#include "Internals.h"
#include "Transforms.h"
#include "clang/AST/ASTContext.h"
#include "clang/AST/DynamicRecursiveASTVisitor.h"
#include "clang/Sema/SemaDiagnostic.h"

using namespace clang;
Expand All @@ -27,7 +28,7 @@ using namespace trans;

namespace {

class APIChecker : public RecursiveASTVisitor<APIChecker> {
class APIChecker : public DynamicRecursiveASTVisitor {
MigrationPass &Pass;

Selector getReturnValueSel, setReturnValueSel;
Expand All @@ -51,7 +52,7 @@ class APIChecker : public RecursiveASTVisitor<APIChecker> {
zoneSel = sels.getNullarySelector(&ids.get("zone"));
}

bool VisitObjCMessageExpr(ObjCMessageExpr *E) {
bool VisitObjCMessageExpr(ObjCMessageExpr *E) override {
// NSInvocation.
if (E->isInstanceMessage() &&
E->getReceiverInterface() &&
Expand Down
7 changes: 4 additions & 3 deletions clang/lib/ARCMigrate/TransARCAssign.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -20,9 +20,10 @@
//
//===----------------------------------------------------------------------===//

#include "Transforms.h"
#include "Internals.h"
#include "Transforms.h"
#include "clang/AST/ASTContext.h"
#include "clang/AST/DynamicRecursiveASTVisitor.h"
#include "clang/Sema/SemaDiagnostic.h"

using namespace clang;
Expand All @@ -31,14 +32,14 @@ using namespace trans;

namespace {

class ARCAssignChecker : public RecursiveASTVisitor<ARCAssignChecker> {
class ARCAssignChecker : public DynamicRecursiveASTVisitor {
MigrationPass &Pass;
llvm::DenseSet<VarDecl *> ModifiedVars;

public:
ARCAssignChecker(MigrationPass &pass) : Pass(pass) { }

bool VisitBinaryOperator(BinaryOperator *Exp) {
bool VisitBinaryOperator(BinaryOperator *Exp) override {
if (Exp->getType()->isDependentType())
return true;

Expand Down
39 changes: 22 additions & 17 deletions clang/lib/ARCMigrate/TransAutoreleasePool.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -26,9 +26,10 @@
//
//===----------------------------------------------------------------------===//

#include "Transforms.h"
#include "Internals.h"
#include "Transforms.h"
#include "clang/AST/ASTContext.h"
#include "clang/AST/DynamicRecursiveASTVisitor.h"
#include "clang/Basic/SourceManager.h"
#include "clang/Sema/SemaDiagnostic.h"
#include <map>
Expand All @@ -39,15 +40,15 @@ using namespace trans;

namespace {

class ReleaseCollector : public RecursiveASTVisitor<ReleaseCollector> {
class ReleaseCollector : public DynamicRecursiveASTVisitor {
Decl *Dcl;
SmallVectorImpl<ObjCMessageExpr *> &Releases;

public:
ReleaseCollector(Decl *D, SmallVectorImpl<ObjCMessageExpr *> &releases)
: Dcl(D), Releases(releases) { }

bool VisitObjCMessageExpr(ObjCMessageExpr *E) {
bool VisitObjCMessageExpr(ObjCMessageExpr *E) override {
if (!E->isInstanceMessage())
return true;
if (E->getMethodFamily() != OMF_release)
Expand All @@ -60,27 +61,32 @@ class ReleaseCollector : public RecursiveASTVisitor<ReleaseCollector> {
return true;
}
};

}

namespace {

class AutoreleasePoolRewriter
: public RecursiveASTVisitor<AutoreleasePoolRewriter> {
class AutoreleasePoolRewriter : public BodyTransform {
bool TraversingBody = false;

public:
AutoreleasePoolRewriter(MigrationPass &pass)
: Body(nullptr), Pass(pass) {
: BodyTransform(pass), Body(nullptr) {
PoolII = &pass.Ctx.Idents.get("NSAutoreleasePool");
DrainSel = pass.Ctx.Selectors.getNullarySelector(
&pass.Ctx.Idents.get("drain"));
}

void transformBody(Stmt *body, Decl *ParentD) {
bool TraverseStmt(Stmt *body) override {
if (TraversingBody)
return BodyTransform::TraverseStmt(body);

llvm::SaveAndRestore Restore{TraversingBody, true};
Body = body;
TraverseStmt(body);
BodyTransform::TraverseStmt(body);
return true;
}

~AutoreleasePoolRewriter() {
~AutoreleasePoolRewriter() override {
SmallVector<VarDecl *, 8> VarsToHandle;

for (std::map<VarDecl *, PoolVarInfo>::iterator
Expand Down Expand Up @@ -160,7 +166,7 @@ class AutoreleasePoolRewriter
}
}

bool VisitCompoundStmt(CompoundStmt *S) {
bool VisitCompoundStmt(CompoundStmt *S) override {
SmallVector<PoolScope, 4> Scopes;

for (Stmt::child_iterator
Expand Down Expand Up @@ -245,7 +251,7 @@ class AutoreleasePoolRewriter
}
};

class NameReferenceChecker : public RecursiveASTVisitor<NameReferenceChecker>{
class NameReferenceChecker : public DynamicRecursiveASTVisitor {
ASTContext &Ctx;
SourceRange ScopeRange;
SourceLocation &referenceLoc, &declarationLoc;
Expand All @@ -260,15 +266,15 @@ class AutoreleasePoolRewriter
(*scope.End)->getBeginLoc());
}

bool VisitDeclRefExpr(DeclRefExpr *E) {
bool VisitDeclRefExpr(DeclRefExpr *E) override {
return checkRef(E->getLocation(), E->getDecl()->getLocation());
}

bool VisitTypedefTypeLoc(TypedefTypeLoc TL) {
bool VisitTypedefTypeLoc(TypedefTypeLoc TL) override {
return checkRef(TL.getBeginLoc(), TL.getTypedefNameDecl()->getLocation());
}

bool VisitTagTypeLoc(TagTypeLoc TL) {
bool VisitTagTypeLoc(TagTypeLoc TL) override {
return checkRef(TL.getBeginLoc(), TL.getDecl()->getLocation());
}

Expand Down Expand Up @@ -411,7 +417,6 @@ class AutoreleasePoolRewriter
}

Stmt *Body;
MigrationPass &Pass;

IdentifierInfo *PoolII;
Selector DrainSel;
Expand All @@ -430,6 +435,6 @@ class AutoreleasePoolRewriter
} // anonymous namespace

void trans::rewriteAutoreleasePool(MigrationPass &pass) {
BodyTransform<AutoreleasePoolRewriter> trans(pass);
AutoreleasePoolRewriter trans(pass);
trans.TraverseDecl(pass.Ctx.getTranslationUnitDecl());
}
Loading
Loading