clang  6.0.0svn
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"
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->getLocStart(), 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 
75  GCCollectableCallsChecker(BodyCtx.getMigrationContext())
76  .TraverseStmt(BodyCtx.getTopStmt());
77 }
Defines the clang::ASTContext interface.
FunctionDecl - An instance of this class is created to represent a function declaration or definition...
Definition: Decl.h:1698
One of these records is kept for each identifier that is lexed.
DiagnosticBuilder report(SourceLocation loc, unsigned diagId, SourceRange range=SourceRange())
void reportError(StringRef error, SourceLocation loc, SourceRange range=SourceRange())
IdentifierTable & Idents
Definition: ASTContext.h:537
MigrationContext & getMigrationContext()
Definition: Transforms.h:55
A class that does preorder or postorder depth-first traversal on the entire Clang AST and visits each...
bool clearDiagnostic(ArrayRef< unsigned > IDs, SourceRange range)
Expr - This represents one expression.
Definition: Expr.h:106
Implements an efficient mapping from strings to IdentifierInfo nodes.
const Expr * getCallee() const
Definition: Expr.h:2249
QualType getType() const
Definition: Expr.h:128
IdentifierInfo & get(StringRef Name)
Return the identifier token info for the specified named identifier.
void traverseBody(BodyContext &BodyCtx) override
Dataflow Directional Tag Classes.
Expr * IgnoreParenImpCasts() LLVM_READONLY
IgnoreParenImpCasts - Ignore parentheses and implicit casts.
Definition: Expr.cpp:2550
SourceLocation getLocStart() const LLVM_READONLY
Definition: Expr.cpp:1326
void replace(SourceRange range, StringRef text)
TransformActions & TA
Definition: Internals.h:151
SourceRange getSourceRange() const LLVM_READONLY
SourceLocation tokens are not useful in isolation - they are low level value objects created/interpre...
Definition: Stmt.cpp:265
CallExpr - Represents a function call (C99 6.5.2.2, C++ [expr.call]).
Definition: Expr.h:2209
A reference to a declared variable, function, enum, etc.
Definition: Expr.h:956