clang 10.0.0git

TransGCCalls.cpp
Go to the documentation of this file.
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
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 IdentifierInfo *NSMakeCollectableII;
24 IdentifierInfo *CFMakeCollectableII;
25
26 public:
27 GCCollectableCallsChecker(MigrationContext &ctx)
28 : MigrateCtx(ctx) {
29 IdentifierTable &Ids = MigrateCtx.Pass.Ctx.Idents;
30 NSMakeCollectableII = &Ids.get("NSMakeCollectable");
31 CFMakeCollectableII = &Ids.get("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(E->getType())) {
40 TA.report(E->getBeginLoc(), diag::warn_arcmt_nsalloc_realloc,
41 E->getSourceRange());
42 return true;
43 }
44
45 Expr *CEE = E->getCallee()->IgnoreParenImpCasts();
46 if (DeclRefExpr *DRE = dyn_cast<DeclRefExpr>(CEE)) {
47 if (FunctionDecl *FD = dyn_cast_or_null<FunctionDecl>(DRE->getDecl())) {
48 if (!FD->getDeclContext()->getRedeclContext()->isFileContext())
49 return true;
50
51 if (FD->getIdentifier() == NSMakeCollectableII) {
52 Transaction Trans(TA);
53 TA.clearDiagnostic(diag::err_unavailable,
54 diag::err_unavailable_message,
55 diag::err_ovl_deleted_call, // ObjC++
56 DRE->getSourceRange());
57 TA.replace(DRE->getSourceRange(), "CFBridgingRelease");
58
59 } else if (FD->getIdentifier() == CFMakeCollectableII) {
60 TA.reportError("CFMakeCollectable will leak the object that it "
61 "receives in ARC", DRE->getLocation(),
62 DRE->getSourceRange());
63 }
64 }
65 }
66
67 return true;
68 }
69 };
70
71 } // anonymous namespace
72
73 void GCCollectableCallsTraverser::traverseBody(BodyContext &BodyCtx) {
74 GCCollectableCallsChecker(BodyCtx.getMigrationContext())
75 .TraverseStmt(BodyCtx.getTopStmt());
76 }
ASTContext.h
Defines the clang::ASTContext interface.
clang::FunctionDecl
Represents a function declaration or definition.
Definition: Decl.h:1783
clang::arcmt::trans::MigrationContext::isGCOwnedNonObjC
bool isGCOwnedNonObjC(QualType T)
Definition: Transforms.cpp:359
Internals.h
clang::IdentifierInfo
One of these records is kept for each identifier that is lexed.
Definition: IdentifierTable.h:57
clang::arcmt::TransformActions::report
DiagnosticBuilder report(SourceLocation loc, unsigned diagId, SourceRange range=SourceRange())
Definition: TransformActions.cpp:680
clang::arcmt::TransformActions::reportError
void reportError(StringRef error, SourceLocation loc, SourceRange range=SourceRange())
Definition: TransformActions.cpp:687
clang::ASTContext::Idents
IdentifierTable & Idents
Definition: ASTContext.h:580
clang::arcmt::trans::BodyContext::getMigrationContext
MigrationContext & getMigrationContext()
Definition: Transforms.h:54
clang::arcmt::MigrationPass::Ctx
ASTContext & Ctx
Definition: Internals.h:146
clang::RecursiveASTVisitor
A class that does preorder or postorder depth-first traversal on the entire Clang AST and visits each...
Definition: RecursiveASTVisitor.h:151
clang::arcmt::TransformActions::clearDiagnostic
bool clearDiagnostic(ArrayRef< unsigned > IDs, SourceRange range)
Definition: TransformActions.cpp:671
clang::arcmt::Transaction
Definition: Internals.h:122
clang::Expr
This represents one expression.
Definition: Expr.h:108
clang::IdentifierTable
Implements an efficient mapping from strings to IdentifierInfo nodes.
Definition: IdentifierTable.h:496
clang::CallExpr::getCallee
Expr * getCallee()
Definition: Expr.h:2663
clang::arcmt::trans::BodyContext
Definition: Transforms.h:45
clang::Expr::getType
QualType getType() const
Definition: Expr.h:137
Transforms.h
clang::arcmt::TransformActions
Definition: Internals.h:47
clang::IdentifierTable::get
IdentifierInfo & get(StringRef Name)
Return the identifier token info for the specified named identifier.
Definition: IdentifierTable.h:529
SemaDiagnostic.h
clang::arcmt::trans::GCCollectableCallsTraverser::traverseBody
void traverseBody(BodyContext &BodyCtx) override
Definition: TransGCCalls.cpp:73
clang::arcmt::trans::MigrationContext::Pass
MigrationPass & Pass
Definition: Transforms.h:84
clang
Dataflow Directional Tag Classes.
Definition: CFGReachabilityAnalysis.h:21
clang::Expr::IgnoreParenImpCasts
Expr * IgnoreParenImpCasts() LLVM_READONLY
Skip past any parentheses and implicit casts which might surround this expression until reaching a fi...
Definition: Expr.cpp:2990
clang::arcmt::trans::MigrationContext
Definition: Transforms.h:80
clang::arcmt::TransformActions::replace
void replace(SourceRange range, StringRef text)
Definition: TransformActions.cpp:646
clang::arcmt::MigrationPass::TA
TransformActions & TA
Definition: Internals.h:150
clang::Stmt::getSourceRange
SourceRange getSourceRange() const LLVM_READONLY
SourceLocation tokens are not useful in isolation - they are low level value objects created/interpre...
Definition: Stmt.cpp:263
clang::CallExpr
CallExpr - Represents a function call (C99 6.5.2.2, C++ [expr.call]).
Definition: Expr.h:2546
clang::DeclRefExpr
A reference to a declared variable, function, enum, etc.
Definition: Expr.h:1171
clang::CallExpr::getBeginLoc
SourceLocation getBeginLoc() const LLVM_READONLY
Definition: Expr.cpp:1531
clang::arcmt::trans::BodyContext::getTopStmt
Stmt * getTopStmt()
Definition: Transforms.h:56