Path: blob/main/contrib/llvm-project/clang/lib/ARCMigrate/TransGCCalls.cpp
35236 views
//===--- TransGCCalls.cpp - Transformations to ARC mode -------------------===//1//2// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.3// See https://llvm.org/LICENSE.txt for license information.4// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception5//6//===----------------------------------------------------------------------===//78#include "Transforms.h"9#include "Internals.h"10#include "clang/AST/ASTContext.h"11#include "clang/Sema/SemaDiagnostic.h"1213using namespace clang;14using namespace arcmt;15using namespace trans;1617namespace {1819class GCCollectableCallsChecker :20public RecursiveASTVisitor<GCCollectableCallsChecker> {21MigrationContext &MigrateCtx;22IdentifierInfo *NSMakeCollectableII;23IdentifierInfo *CFMakeCollectableII;2425public:26GCCollectableCallsChecker(MigrationContext &ctx)27: MigrateCtx(ctx) {28IdentifierTable &Ids = MigrateCtx.Pass.Ctx.Idents;29NSMakeCollectableII = &Ids.get("NSMakeCollectable");30CFMakeCollectableII = &Ids.get("CFMakeCollectable");31}3233bool shouldWalkTypesOfTypeLocs() const { return false; }3435bool VisitCallExpr(CallExpr *E) {36TransformActions &TA = MigrateCtx.Pass.TA;3738if (MigrateCtx.isGCOwnedNonObjC(E->getType())) {39TA.report(E->getBeginLoc(), diag::warn_arcmt_nsalloc_realloc,40E->getSourceRange());41return true;42}4344Expr *CEE = E->getCallee()->IgnoreParenImpCasts();45if (DeclRefExpr *DRE = dyn_cast<DeclRefExpr>(CEE)) {46if (FunctionDecl *FD = dyn_cast_or_null<FunctionDecl>(DRE->getDecl())) {47if (!FD->getDeclContext()->getRedeclContext()->isFileContext())48return true;4950if (FD->getIdentifier() == NSMakeCollectableII) {51Transaction Trans(TA);52TA.clearDiagnostic(diag::err_unavailable,53diag::err_unavailable_message,54diag::err_ovl_deleted_call, // ObjC++55DRE->getSourceRange());56TA.replace(DRE->getSourceRange(), "CFBridgingRelease");5758} else if (FD->getIdentifier() == CFMakeCollectableII) {59TA.reportError("CFMakeCollectable will leak the object that it "60"receives in ARC", DRE->getLocation(),61DRE->getSourceRange());62}63}64}6566return true;67}68};6970} // anonymous namespace7172void GCCollectableCallsTraverser::traverseBody(BodyContext &BodyCtx) {73GCCollectableCallsChecker(BodyCtx.getMigrationContext())74.TraverseStmt(BodyCtx.getTopStmt());75}767778