clang 8.0.0

TransGCCalls.cpp
Go to the documentation of this file.
1 //===--- TransGCCalls.cpp - Transformations 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/AST/ASTContext.h"
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;
24 IdentifierInfo *NSMakeCollectableII;
25 IdentifierInfo *CFMakeCollectableII;
26
27 public:
28 GCCollectableCallsChecker(MigrationContext &ctx)
29 : MigrateCtx(ctx) {
30 IdentifierTable &Ids = MigrateCtx.Pass.Ctx.Idents;
31 NSMakeCollectableII = &Ids.get("NSMakeCollectable");
32 CFMakeCollectableII = &Ids.get("CFMakeCollectable");
33 }
34
35 bool shouldWalkTypesOfTypeLocs() const { return false; }
36
37 bool VisitCallExpr(CallExpr *E) {
38 TransformActions &TA = MigrateCtx.Pass.TA;
39
40 if (MigrateCtx.isGCOwnedNonObjC(E->getType())) {
41 TA.report(E->getBeginLoc(), diag::warn_arcmt_nsalloc_realloc,
42 E->getSourceRange());
43 return true;
44 }
45
46 Expr *CEE = E->getCallee()->IgnoreParenImpCasts();
47 if (DeclRefExpr *DRE = dyn_cast<DeclRefExpr>(CEE)) {
48 if (FunctionDecl *FD = dyn_cast_or_null<FunctionDecl>(DRE->getDecl())) {
49 if (!FD->getDeclContext()->getRedeclContext()->isFileContext())
50 return true;
51
52 if (FD->getIdentifier() == NSMakeCollectableII) {
53 Transaction Trans(TA);
54 TA.clearDiagnostic(diag::err_unavailable,
55 diag::err_unavailable_message,
56 diag::err_ovl_deleted_call, // ObjC++
57 DRE->getSourceRange());
58 TA.replace(DRE->getSourceRange(), "CFBridgingRelease");
59
60 } else if (FD->getIdentifier() == CFMakeCollectableII) {
61 TA.reportError("CFMakeCollectable will leak the object that it "
62 "receives in ARC", DRE->getLocation(),
63 DRE->getSourceRange());
64 }
65 }
66 }
67
68 return true;
69 }
70 };
71
72 } // anonymous namespace
73
74 void GCCollectableCallsTraverser::traverseBody(BodyContext &BodyCtx) {
75 GCCollectableCallsChecker(BodyCtx.getMigrationContext())
76 .TraverseStmt(BodyCtx.getTopStmt());
77 }
ASTContext.h
Defines the clang::ASTContext interface.
clang::FunctionDecl
Represents a function declaration or definition.
Definition: Decl.h:1738
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:58
clang::arcmt::TransformActions::report
DiagnosticBuilder report(SourceLocation loc, unsigned diagId, SourceRange range=SourceRange())
Definition: TransformActions.cpp:679
clang::arcmt::TransformActions::reportError
void reportError(StringRef error, SourceLocation loc, SourceRange range=SourceRange())
Definition: TransformActions.cpp:686
clang::ASTContext::Idents
IdentifierTable & Idents
Definition: ASTContext.h:566
clang::arcmt::trans::BodyContext::getMigrationContext
MigrationContext & getMigrationContext()
Definition: Transforms.h:55
clang::arcmt::MigrationPass::Ctx
ASTContext & Ctx
Definition: Internals.h:147
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:670
clang::arcmt::Transaction
Definition: Internals.h:123
clang::Expr
This represents one expression.
Definition: Expr.h:106
clang::IdentifierTable
Implements an efficient mapping from strings to IdentifierInfo nodes.
Definition: IdentifierTable.h:486
clang::CallExpr::getCallee
Expr * getCallee()
Definition: Expr.h:2514
clang::arcmt::trans::BodyContext
Definition: Transforms.h:46
clang::Expr::getType
QualType getType() const
Definition: Expr.h:128
Transforms.h
clang::arcmt::TransformActions
Definition: Internals.h:48
clang::IdentifierTable::get
IdentifierInfo & get(StringRef Name)
Return the identifier token info for the specified named identifier.
Definition: IdentifierTable.h:519
SemaDiagnostic.h
clang::arcmt::trans::GCCollectableCallsTraverser::traverseBody
void traverseBody(BodyContext &BodyCtx) override
Definition: TransGCCalls.cpp:74
clang::arcmt::trans::MigrationContext::Pass
MigrationPass & Pass
Definition: Transforms.h:85
clang
Dataflow Directional Tag Classes.
Definition: CFGReachabilityAnalysis.h:22
clang::Expr::IgnoreParenImpCasts
Expr * IgnoreParenImpCasts() LLVM_READONLY
IgnoreParenImpCasts - Ignore parentheses and implicit casts.
Definition: Expr.cpp:2693
clang::arcmt::trans::MigrationContext
Definition: Transforms.h:81
clang::arcmt::TransformActions::replace
void replace(SourceRange range, StringRef text)
Definition: TransformActions.cpp:645
clang::arcmt::MigrationPass::TA
TransformActions & TA
Definition: Internals.h:151
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:276
clang::CallExpr
CallExpr - Represents a function call (C99 6.5.2.2, C++ [expr.call]).
Definition: Expr.h:2396
clang::DeclRefExpr
A reference to a declared variable, function, enum, etc.
Definition: Expr.h:1041
clang::CallExpr::getBeginLoc
SourceLocation getBeginLoc() const LLVM_READONLY
Definition: Expr.cpp:1428
clang::arcmt::trans::BodyContext::getTopStmt
Stmt * getTopStmt()
Definition: Transforms.h:57