clang  10.0.0svn
TransAPIUses.cpp
Go to the documentation of this file.
1 //===--- TransAPIUses.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 // checkAPIUses:
10 //
11 // Emits error/fix with some API uses that are obsolete or not safe in ARC mode:
12 //
13 // - NSInvocation's [get/set]ReturnValue and [get/set]Argument are only safe
14 // with __unsafe_unretained objects.
15 // - Calling -zone gets replaced with 'nil'.
16 //
17 //===----------------------------------------------------------------------===//
18 
19 #include "Transforms.h"
20 #include "Internals.h"
21 #include "clang/AST/ASTContext.h"
23 
24 using namespace clang;
25 using namespace arcmt;
26 using namespace trans;
27 
28 namespace {
29 
30 class APIChecker : public RecursiveASTVisitor<APIChecker> {
31  MigrationPass &Pass;
32 
33  Selector getReturnValueSel, setReturnValueSel;
34  Selector getArgumentSel, setArgumentSel;
35 
36  Selector zoneSel;
37 public:
38  APIChecker(MigrationPass &pass) : Pass(pass) {
39  SelectorTable &sels = Pass.Ctx.Selectors;
40  IdentifierTable &ids = Pass.Ctx.Idents;
41  getReturnValueSel = sels.getUnarySelector(&ids.get("getReturnValue"));
42  setReturnValueSel = sels.getUnarySelector(&ids.get("setReturnValue"));
43 
44  IdentifierInfo *selIds[2];
45  selIds[0] = &ids.get("getArgument");
46  selIds[1] = &ids.get("atIndex");
47  getArgumentSel = sels.getSelector(2, selIds);
48  selIds[0] = &ids.get("setArgument");
49  setArgumentSel = sels.getSelector(2, selIds);
50 
51  zoneSel = sels.getNullarySelector(&ids.get("zone"));
52  }
53 
54  bool VisitObjCMessageExpr(ObjCMessageExpr *E) {
55  // NSInvocation.
56  if (E->isInstanceMessage() &&
57  E->getReceiverInterface() &&
58  E->getReceiverInterface()->getName() == "NSInvocation") {
59  StringRef selName;
60  if (E->getSelector() == getReturnValueSel)
61  selName = "getReturnValue";
62  else if (E->getSelector() == setReturnValueSel)
63  selName = "setReturnValue";
64  else if (E->getSelector() == getArgumentSel)
65  selName = "getArgument";
66  else if (E->getSelector() == setArgumentSel)
67  selName = "setArgument";
68  else
69  return true;
70 
71  Expr *parm = E->getArg(0)->IgnoreParenCasts();
72  QualType pointee = parm->getType()->getPointeeType();
73  if (pointee.isNull())
74  return true;
75 
77  Pass.TA.report(parm->getBeginLoc(),
78  diag::err_arcmt_nsinvocation_ownership,
79  parm->getSourceRange())
80  << selName;
81 
82  return true;
83  }
84 
85  // -zone.
86  if (E->isInstanceMessage() &&
87  E->getInstanceReceiver() &&
88  E->getSelector() == zoneSel &&
89  Pass.TA.hasDiagnostic(diag::err_unavailable,
90  diag::err_unavailable_message,
91  E->getSelectorLoc(0))) {
92  // Calling -zone is meaningless in ARC, change it to nil.
93  Transaction Trans(Pass.TA);
94  Pass.TA.clearDiagnostic(diag::err_unavailable,
95  diag::err_unavailable_message,
96  E->getSelectorLoc(0));
97  Pass.TA.replace(E->getSourceRange(), getNilString(Pass));
98  }
99  return true;
100  }
101 };
102 
103 } // anonymous namespace
104 
106  APIChecker(pass).TraverseDecl(pass.Ctx.getTranslationUnitDecl());
107 }
Defines the clang::ASTContext interface.
Smart pointer class that efficiently represents Objective-C method names.
A (possibly-)qualified type.
Definition: Type.h:643
Selector getSelector() const
Definition: ExprObjC.cpp:337
QualType getPointeeType() const
If this is a pointer, ObjC object pointer, or block pointer, this returns the respective pointee...
Definition: Type.cpp:505
ObjCInterfaceDecl * getReceiverInterface() const
Retrieve the Objective-C interface to which this message is being directed, if known.
Definition: ExprObjC.cpp:358
Selector getUnarySelector(IdentifierInfo *ID)
One of these records is kept for each identifier that is lexed.
This table allows us to fully hide how we implement multi-keyword caching.
SourceLocation getBeginLoc() const LLVM_READONLY
Definition: Stmt.cpp:263
DiagnosticBuilder report(SourceLocation loc, unsigned diagId, SourceRange range=SourceRange())
IdentifierTable & Idents
Definition: ASTContext.h:569
Selector getNullarySelector(IdentifierInfo *ID)
Expr * IgnoreParenCasts() LLVM_READONLY
Skip past any parentheses and casts which might surround this expression until reaching a fixed point...
Definition: Expr.cpp:2947
A class that does preorder or postorder depth-first traversal on the entire Clang AST and visits each...
This object can be modified without requiring retains or releases.
Definition: Type.h:158
StringRef getNilString(MigrationPass &Pass)
Returns "nil" or "0" if &#39;nil&#39; macro is not actually defined.
Definition: Transforms.cpp:208
bool clearDiagnostic(ArrayRef< unsigned > IDs, SourceRange range)
This represents one expression.
Definition: Expr.h:108
Implements an efficient mapping from strings to IdentifierInfo nodes.
void checkAPIUses(MigrationPass &pass)
QualType getType() const
Definition: Expr.h:137
An expression that sends a message to the given Objective-C object or class.
Definition: ExprObjC.h:950
bool isNull() const
Return true if this QualType doesn&#39;t point to a type yet.
Definition: Type.h:708
Expr * getArg(unsigned Arg)
getArg - Return the specified argument.
Definition: ExprObjC.h:1395
SelectorTable & Selectors
Definition: ASTContext.h:570
IdentifierInfo & get(StringRef Name)
Return the identifier token info for the specified named identifier.
SourceLocation getSelectorLoc(unsigned Index) const
Definition: ExprObjC.h:1425
Expr * getInstanceReceiver()
Returns the object expression (receiver) for an instance message, or null for a message that is not a...
Definition: ExprObjC.h:1260
Dataflow Directional Tag Classes.
Selector getSelector(unsigned NumArgs, IdentifierInfo **IIV)
Can create any sort of selector.
TranslationUnitDecl * getTranslationUnitDecl() const
Definition: ASTContext.h:1007
void replace(SourceRange range, StringRef text)
TransformActions & TA
Definition: Internals.h:150
SourceRange getSourceRange() const LLVM_READONLY
SourceLocation tokens are not useful in isolation - they are low level value objects created/interpre...
Definition: Stmt.cpp:251
StringRef getName() const
Get the name of identifier for this declaration as a StringRef.
Definition: Decl.h:275
bool isInstanceMessage() const
Determine whether this is an instance message to either a computed object or to super.
Definition: ExprObjC.h:1248
bool hasDiagnostic(unsigned ID, SourceRange range)
Definition: Internals.h:87
Qualifiers::ObjCLifetime getObjCLifetime() const
Returns lifetime attribute of this type.
Definition: Type.h:1063