1//===--- TransGCCalls.cpp - Transformations to ARC mode -------------------===//
2//
3// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4// See https://llvm.org/LICENSE.txt for license information.
5// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6//
7//===----------------------------------------------------------------------===//
8
9#include "Transforms.h"
10#include "Internals.h"
11#include "clang/AST/ASTContext.h"
12#include "clang/Sema/SemaDiagnostic.h"
13
14using namespace clang;
15using namespace arcmt;
16using namespace trans;
17
18namespace {
19
20class GCCollectableCallsChecker :
21 public RecursiveASTVisitor<GCCollectableCallsChecker> {
22 MigrationContext &MigrateCtx;
23 IdentifierInfo *NSMakeCollectableII;
24 IdentifierInfo *CFMakeCollectableII;
25
26public:
27 GCCollectableCallsChecker(MigrationContext &ctx)
28 : MigrateCtx(ctx) {
29 IdentifierTable &Ids = MigrateCtx.Pass.Ctx.Idents;
30 NSMakeCollectableII = &Ids.get(Name: "NSMakeCollectable");
31 CFMakeCollectableII = &Ids.get(Name: "CFMakeCollectable");
32 }
33
34 bool shouldWalkTypesOfTypeLocs() const { return false; }
35
36 bool VisitCallExpr(CallExpr *E) {
37 TransformActions &TA = MigrateCtx.Pass.TA;
38
39 if (MigrateCtx.isGCOwnedNonObjC(T: E->getType())) {
40 TA.report(loc: E->getBeginLoc(), diagId: diag::warn_arcmt_nsalloc_realloc,
41 range: E->getSourceRange());
42 return true;
43 }
44
45 Expr *CEE = E->getCallee()->IgnoreParenImpCasts();
46 if (DeclRefExpr *DRE = dyn_cast<DeclRefExpr>(Val: CEE)) {
47 if (FunctionDecl *FD = dyn_cast_or_null<FunctionDecl>(Val: DRE->getDecl())) {
48 if (!FD->getDeclContext()->getRedeclContext()->isFileContext())
49 return true;
50
51 if (FD->getIdentifier() == NSMakeCollectableII) {
52 Transaction Trans(TA);
53 TA.clearDiagnostic(ID1: diag::err_unavailable,
54 ID2: diag::err_unavailable_message,
55 ID3: diag::err_ovl_deleted_call, // ObjC++
56 range: DRE->getSourceRange());
57 TA.replace(range: DRE->getSourceRange(), text: "CFBridgingRelease");
58
59 } else if (FD->getIdentifier() == CFMakeCollectableII) {
60 TA.reportError(error: "CFMakeCollectable will leak the object that it "
61 "receives in ARC", loc: DRE->getLocation(),
62 range: DRE->getSourceRange());
63 }
64 }
65 }
66
67 return true;
68 }
69};
70
71} // anonymous namespace
72
73void GCCollectableCallsTraverser::traverseBody(BodyContext &BodyCtx) {
74 GCCollectableCallsChecker(BodyCtx.getMigrationContext())
75 .TraverseStmt(S: BodyCtx.getTopStmt());
76}
77