Argyrios Kyrtzidis | e0ac745 | 2011-11-04 15:58:08 +0000 | [diff] [blame] | 1 | //===--- TransGCCalls.cpp - Tranformations to ARC mode --------------------===// |
| 2 | // |
| 3 | // The LLVM Compiler Infrastructure |
| 4 | // |
| 5 | // This file is distributed under the University of Illinois Open Source |
| 6 | // License. See LICENSE.TXT for details. |
| 7 | // |
| 8 | //===----------------------------------------------------------------------===// |
| 9 | |
| 10 | #include "Transforms.h" |
| 11 | #include "Internals.h" |
| 12 | #include "clang/Sema/SemaDiagnostic.h" |
| 13 | |
| 14 | using namespace clang; |
| 15 | using namespace arcmt; |
| 16 | using namespace trans; |
| 17 | |
| 18 | namespace { |
| 19 | |
| 20 | class GCCollectableCallsChecker : |
| 21 | public RecursiveASTVisitor<GCCollectableCallsChecker> { |
| 22 | MigrationContext &MigrateCtx; |
| 23 | ParentMap &PMap; |
| 24 | IdentifierInfo *NSMakeCollectableII; |
Argyrios Kyrtzidis | 81eecde | 2011-11-04 15:58:17 +0000 | [diff] [blame] | 25 | IdentifierInfo *CFMakeCollectableII; |
Argyrios Kyrtzidis | e0ac745 | 2011-11-04 15:58:08 +0000 | [diff] [blame] | 26 | |
| 27 | public: |
| 28 | GCCollectableCallsChecker(MigrationContext &ctx, ParentMap &map) |
| 29 | : MigrateCtx(ctx), PMap(map) { |
Argyrios Kyrtzidis | f38fa73 | 2011-11-06 18:58:03 +0000 | [diff] [blame] | 30 | IdentifierTable &Ids = MigrateCtx.Pass.Ctx.Idents; |
Argyrios Kyrtzidis | 81eecde | 2011-11-04 15:58:17 +0000 | [diff] [blame] | 31 | NSMakeCollectableII = &Ids.get("NSMakeCollectable"); |
| 32 | CFMakeCollectableII = &Ids.get("CFMakeCollectable"); |
Argyrios Kyrtzidis | e0ac745 | 2011-11-04 15:58:08 +0000 | [diff] [blame] | 33 | } |
| 34 | |
Argyrios Kyrtzidis | f38fa73 | 2011-11-06 18:58:03 +0000 | [diff] [blame] | 35 | bool shouldWalkTypesOfTypeLocs() const { return false; } |
| 36 | |
Argyrios Kyrtzidis | e0ac745 | 2011-11-04 15:58:08 +0000 | [diff] [blame] | 37 | bool VisitCallExpr(CallExpr *E) { |
Argyrios Kyrtzidis | f38fa73 | 2011-11-06 18:58:03 +0000 | [diff] [blame] | 38 | TransformActions &TA = MigrateCtx.Pass.TA; |
Argyrios Kyrtzidis | 81eecde | 2011-11-04 15:58:17 +0000 | [diff] [blame] | 39 | |
Argyrios Kyrtzidis | 1fe4203 | 2011-11-04 23:43:03 +0000 | [diff] [blame] | 40 | if (MigrateCtx.isGCOwnedNonObjC(E->getType())) { |
| 41 | TA.reportError("call returns pointer to GC managed memory; " |
| 42 | "it will become unmanaged in ARC", |
| 43 | E->getLocStart(), E->getSourceRange()); |
| 44 | return true; |
| 45 | } |
| 46 | |
Argyrios Kyrtzidis | e0ac745 | 2011-11-04 15:58:08 +0000 | [diff] [blame] | 47 | Expr *CEE = E->getCallee()->IgnoreParenImpCasts(); |
| 48 | if (DeclRefExpr *DRE = dyn_cast<DeclRefExpr>(CEE)) { |
| 49 | if (FunctionDecl *FD = dyn_cast_or_null<FunctionDecl>(DRE->getDecl())) { |
Argyrios Kyrtzidis | 81eecde | 2011-11-04 15:58:17 +0000 | [diff] [blame] | 50 | if (!FD->getDeclContext()->getRedeclContext()->isFileContext()) |
| 51 | return true; |
| 52 | |
| 53 | if (FD->getIdentifier() == NSMakeCollectableII) { |
Argyrios Kyrtzidis | e0ac745 | 2011-11-04 15:58:08 +0000 | [diff] [blame] | 54 | Transaction Trans(TA); |
| 55 | TA.clearDiagnostic(diag::err_unavailable, |
| 56 | diag::err_unavailable_message, |
Argyrios Kyrtzidis | 0d579b6 | 2011-11-04 15:58:13 +0000 | [diff] [blame] | 57 | diag::err_ovl_deleted_call, // ObjC++ |
Argyrios Kyrtzidis | e0ac745 | 2011-11-04 15:58:08 +0000 | [diff] [blame] | 58 | DRE->getSourceRange()); |
| 59 | TA.replace(DRE->getSourceRange(), "CFBridgingRelease"); |
Argyrios Kyrtzidis | 81eecde | 2011-11-04 15:58:17 +0000 | [diff] [blame] | 60 | |
| 61 | } else if (FD->getIdentifier() == CFMakeCollectableII) { |
| 62 | TA.reportError("CFMakeCollectable will leak the object that it " |
| 63 | "receives in ARC", DRE->getLocation(), |
| 64 | DRE->getSourceRange()); |
Argyrios Kyrtzidis | e0ac745 | 2011-11-04 15:58:08 +0000 | [diff] [blame] | 65 | } |
| 66 | } |
| 67 | } |
| 68 | |
| 69 | return true; |
| 70 | } |
| 71 | }; |
| 72 | |
| 73 | } // anonymous namespace |
| 74 | |
| 75 | void GCCollectableCallsTraverser::traverseBody(BodyContext &BodyCtx) { |
| 76 | GCCollectableCallsChecker(BodyCtx.getMigrationContext(), |
| 77 | BodyCtx.getParentMap()) |
| 78 | .TraverseStmt(BodyCtx.getTopStmt()); |
| 79 | } |