Benjamin Kramer | accaf19 | 2012-11-14 15:08:31 +0000 | [diff] [blame] | 1 | //===--- TransGCCalls.cpp - Transformations to ARC mode -------------------===// |
Argyrios Kyrtzidis | e0ac745 | 2011-11-04 15:58:08 +0000 | [diff] [blame] | 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" |
Benjamin Kramer | 471c8b4 | 2012-07-04 20:19:54 +0000 | [diff] [blame] | 12 | #include "clang/AST/ASTContext.h" |
Argyrios Kyrtzidis | e0ac745 | 2011-11-04 15:58:08 +0000 | [diff] [blame] | 13 | #include "clang/Sema/SemaDiagnostic.h" |
| 14 | |
| 15 | using namespace clang; |
| 16 | using namespace arcmt; |
| 17 | using namespace trans; |
| 18 | |
| 19 | namespace { |
| 20 | |
| 21 | class GCCollectableCallsChecker : |
| 22 | public RecursiveASTVisitor<GCCollectableCallsChecker> { |
| 23 | MigrationContext &MigrateCtx; |
Argyrios Kyrtzidis | e0ac745 | 2011-11-04 15:58:08 +0000 | [diff] [blame] | 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: |
Benjamin Kramer | facde17 | 2012-06-06 17:32:50 +0000 | [diff] [blame] | 28 | GCCollectableCallsChecker(MigrationContext &ctx) |
| 29 | : MigrateCtx(ctx) { |
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())) { |
Fariborz Jahanian | b5c6bab | 2012-01-25 00:20:29 +0000 | [diff] [blame] | 41 | if (MigrateCtx.Pass.noNSAllocReallocError()) |
| 42 | TA.reportWarning("call returns pointer to GC managed memory; " |
| 43 | "it will become unmanaged in ARC", |
| 44 | E->getLocStart(), E->getSourceRange()); |
| 45 | else |
| 46 | TA.reportError("call returns pointer to GC managed memory; " |
| 47 | "it will become unmanaged in ARC", |
| 48 | E->getLocStart(), E->getSourceRange()); |
Argyrios Kyrtzidis | 1fe4203 | 2011-11-04 23:43:03 +0000 | [diff] [blame] | 49 | return true; |
| 50 | } |
| 51 | |
Argyrios Kyrtzidis | e0ac745 | 2011-11-04 15:58:08 +0000 | [diff] [blame] | 52 | Expr *CEE = E->getCallee()->IgnoreParenImpCasts(); |
| 53 | if (DeclRefExpr *DRE = dyn_cast<DeclRefExpr>(CEE)) { |
| 54 | if (FunctionDecl *FD = dyn_cast_or_null<FunctionDecl>(DRE->getDecl())) { |
Argyrios Kyrtzidis | 81eecde | 2011-11-04 15:58:17 +0000 | [diff] [blame] | 55 | if (!FD->getDeclContext()->getRedeclContext()->isFileContext()) |
| 56 | return true; |
| 57 | |
| 58 | if (FD->getIdentifier() == NSMakeCollectableII) { |
Argyrios Kyrtzidis | e0ac745 | 2011-11-04 15:58:08 +0000 | [diff] [blame] | 59 | Transaction Trans(TA); |
| 60 | TA.clearDiagnostic(diag::err_unavailable, |
| 61 | diag::err_unavailable_message, |
Argyrios Kyrtzidis | 0d579b6 | 2011-11-04 15:58:13 +0000 | [diff] [blame] | 62 | diag::err_ovl_deleted_call, // ObjC++ |
Argyrios Kyrtzidis | e0ac745 | 2011-11-04 15:58:08 +0000 | [diff] [blame] | 63 | DRE->getSourceRange()); |
| 64 | TA.replace(DRE->getSourceRange(), "CFBridgingRelease"); |
Argyrios Kyrtzidis | 81eecde | 2011-11-04 15:58:17 +0000 | [diff] [blame] | 65 | |
| 66 | } else if (FD->getIdentifier() == CFMakeCollectableII) { |
| 67 | TA.reportError("CFMakeCollectable will leak the object that it " |
| 68 | "receives in ARC", DRE->getLocation(), |
| 69 | DRE->getSourceRange()); |
Argyrios Kyrtzidis | e0ac745 | 2011-11-04 15:58:08 +0000 | [diff] [blame] | 70 | } |
| 71 | } |
| 72 | } |
| 73 | |
| 74 | return true; |
| 75 | } |
| 76 | }; |
| 77 | |
| 78 | } // anonymous namespace |
| 79 | |
| 80 | void GCCollectableCallsTraverser::traverseBody(BodyContext &BodyCtx) { |
Benjamin Kramer | facde17 | 2012-06-06 17:32:50 +0000 | [diff] [blame] | 81 | GCCollectableCallsChecker(BodyCtx.getMigrationContext()) |
Argyrios Kyrtzidis | e0ac745 | 2011-11-04 15:58:08 +0000 | [diff] [blame] | 82 | .TraverseStmt(BodyCtx.getTopStmt()); |
| 83 | } |