summaryrefslogtreecommitdiffstats
path: root/lib/StaticAnalyzer/Core/ObjCMessage.cpp
blob: c000600474980c70a433cc87a77dd870cd381fe1 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
//===- ObjCMessage.cpp - Wrapper for ObjC messages and dot syntax -*- C++ -*--//
//
//                     The LLVM Compiler Infrastructure
//
// This file is distributed under the University of Illinois Open Source
// License. See LICENSE.TXT for details.
//
//===----------------------------------------------------------------------===//
//
// This file defines ObjCMessage which serves as a common wrapper for ObjC
// message expressions or implicit messages for loading/storing ObjC properties.
//
//===----------------------------------------------------------------------===//

#include "clang/StaticAnalyzer/Core/PathSensitive/ObjCMessage.h"

using namespace clang;
using namespace ento;

QualType ObjCMessage::getType(ASTContext &ctx) const {
  assert(isValid() && "This ObjCMessage is uninitialized!");
  if (const ObjCMessageExpr *msgE = dyn_cast<ObjCMessageExpr>(MsgOrPropE))
    return msgE->getType();
  const ObjCPropertyRefExpr *propE = cast<ObjCPropertyRefExpr>(MsgOrPropE);
  if (isPropertySetter())
    return ctx.VoidTy;
  return propE->getType();
}

Selector ObjCMessage::getSelector() const {
  assert(isValid() && "This ObjCMessage is uninitialized!");
  if (const ObjCMessageExpr *msgE = dyn_cast<ObjCMessageExpr>(MsgOrPropE))
    return msgE->getSelector();
  const ObjCPropertyRefExpr *propE = cast<ObjCPropertyRefExpr>(MsgOrPropE);
  if (isPropertySetter())
    return propE->getSetterSelector();
  return propE->getGetterSelector();
}

ObjCMethodFamily ObjCMessage::getMethodFamily() const {
  assert(isValid() && "This ObjCMessage is uninitialized!");
  // Case 1.  Explicit message send.
  if (const ObjCMessageExpr *msgE = dyn_cast<ObjCMessageExpr>(MsgOrPropE))
    return msgE->getMethodFamily();

  const ObjCPropertyRefExpr *propE = cast<ObjCPropertyRefExpr>(MsgOrPropE);

  // Case 2.  Reference to implicit property.
  if (propE->isImplicitProperty()) {
    if (isPropertySetter())
      return propE->getImplicitPropertySetter()->getMethodFamily();
    else
      return propE->getImplicitPropertyGetter()->getMethodFamily();
  }

  // Case 3.  Reference to explicit property.
  const ObjCPropertyDecl *prop = propE->getExplicitProperty();
  if (isPropertySetter()) {
    if (prop->getSetterMethodDecl())
      return prop->getSetterMethodDecl()->getMethodFamily();
    return prop->getSetterName().getMethodFamily();
  } else {
    if (prop->getGetterMethodDecl())
      return prop->getGetterMethodDecl()->getMethodFamily();
    return prop->getGetterName().getMethodFamily();
  }
}

const ObjCMethodDecl *ObjCMessage::getMethodDecl() const {
  assert(isValid() && "This ObjCMessage is uninitialized!");
  if (const ObjCMessageExpr *msgE = dyn_cast<ObjCMessageExpr>(MsgOrPropE))
    return msgE->getMethodDecl();
  const ObjCPropertyRefExpr *propE = cast<ObjCPropertyRefExpr>(MsgOrPropE);
  if (propE->isImplicitProperty())
    return isPropertySetter() ? propE->getImplicitPropertySetter()
                              : propE->getImplicitPropertyGetter();
  return 0;
}

const ObjCInterfaceDecl *ObjCMessage::getReceiverInterface() const {
  assert(isValid() && "This ObjCMessage is uninitialized!");
  if (const ObjCMessageExpr *msgE = dyn_cast<ObjCMessageExpr>(MsgOrPropE))
    return msgE->getReceiverInterface();
  const ObjCPropertyRefExpr *propE = cast<ObjCPropertyRefExpr>(MsgOrPropE);
  if (propE->isClassReceiver())
    return propE->getClassReceiver();
  QualType recT;
  if (const Expr *recE = getInstanceReceiver())
    recT = recE->getType();
  else {
    assert(propE->isSuperReceiver());
    recT = propE->getSuperReceiverType();
  }
  if (const ObjCObjectPointerType *Ptr = recT->getAs<ObjCObjectPointerType>())
    return Ptr->getInterfaceDecl();
  return 0;
}

const Expr *ObjCMessage::getArgExpr(unsigned i) const {
  assert(isValid() && "This ObjCMessage is uninitialized!");
  assert(i < getNumArgs() && "Invalid index for argument");
  if (const ObjCMessageExpr *msgE = dyn_cast<ObjCMessageExpr>(MsgOrPropE))
    return msgE->getArg(i);
  assert(isPropertySetter());
  if (const BinaryOperator *bop = dyn_cast<BinaryOperator>(OriginE))
    if (bop->isAssignmentOp())
      return bop->getRHS();
  return 0;
}

QualType CallOrObjCMessage::getResultType(ASTContext &ctx) const {
  QualType resultTy;
  bool isLVal = false;

  if (CallE) {
    isLVal = CallE->isLValue();
    const Expr *Callee = CallE->getCallee();
    if (const FunctionDecl *FD = State->getSVal(Callee).getAsFunctionDecl())
      resultTy = FD->getResultType();
    else
      resultTy = CallE->getType();
  }
  else {
    isLVal = isa<ObjCMessageExpr>(Msg.getOriginExpr()) &&
             Msg.getOriginExpr()->isLValue();
    resultTy = Msg.getResultType(ctx);
  }

  if (isLVal)
    resultTy = ctx.getPointerType(resultTy);

  return resultTy;
}

SVal CallOrObjCMessage::getArgSValAsScalarOrLoc(unsigned i) const {
  assert(i < getNumArgs());
  if (CallE) return State->getSValAsScalarOrLoc(CallE->getArg(i));
  QualType argT = Msg.getArgType(i);
  if (Loc::isLocType(argT) || argT->isIntegerType())
    return Msg.getArgSVal(i, State);
  return UnknownVal();
}

SVal CallOrObjCMessage::getFunctionCallee() const {
  assert(isFunctionCall());
  assert(!isCXXCall());
  const Expr *callee = CallE->getCallee()->IgnoreParenCasts();
  return State->getSVal(callee);
}

SVal CallOrObjCMessage::getCXXCallee() const {
  assert(isCXXCall());
  const Expr *callee =
    cast<CXXMemberCallExpr>(CallE)->getImplicitObjectArgument();
  return State->getSVal(callee);  
}
OpenPOWER on IntegriCloud