Coverage Report

Created: 2019-07-24 05:18

/Users/buildslave/jenkins/workspace/clang-stage2-coverage-R/llvm/tools/clang/lib/AST/ExprObjC.cpp
Line
Count
Source (jump to first uncovered line)
1
//===- ExprObjC.cpp - (ObjC) Expression AST Node Implementation -----------===//
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
// This file implements the subclesses of Expr class declared in ExprObjC.h
10
//
11
//===----------------------------------------------------------------------===//
12
13
#include "clang/AST/ExprObjC.h"
14
#include "clang/AST/ASTContext.h"
15
#include "clang/AST/SelectorLocationsKind.h"
16
#include "clang/AST/Type.h"
17
#include "clang/AST/TypeLoc.h"
18
#include "llvm/ADT/SmallVector.h"
19
#include "llvm/Support/ErrorHandling.h"
20
#include <algorithm>
21
#include <cassert>
22
#include <cstdint>
23
24
using namespace clang;
25
26
ObjCArrayLiteral::ObjCArrayLiteral(ArrayRef<Expr *> Elements, QualType T,
27
                                   ObjCMethodDecl *Method, SourceRange SR)
28
    : Expr(ObjCArrayLiteralClass, T, VK_RValue, OK_Ordinary, false, false,
29
           false, false),
30
180
      NumElements(Elements.size()), Range(SR), ArrayWithObjectsMethod(Method) {
31
180
  Expr **SaveElements = getElements();
32
383
  for (unsigned I = 0, N = Elements.size(); I != N; 
++I203
) {
33
203
    if (Elements[I]->isTypeDependent() || 
Elements[I]->isValueDependent()200
)
34
3
      ExprBits.ValueDependent = true;
35
203
    if (Elements[I]->isInstantiationDependent())
36
3
      ExprBits.InstantiationDependent = true;
37
203
    if (Elements[I]->containsUnexpandedParameterPack())
38
1
      ExprBits.ContainsUnexpandedParameterPack = true;
39
203
40
203
    SaveElements[I] = Elements[I];
41
203
  }
42
180
}
43
44
ObjCArrayLiteral *ObjCArrayLiteral::Create(const ASTContext &C,
45
                                           ArrayRef<Expr *> Elements,
46
                                           QualType T, ObjCMethodDecl *Method,
47
180
                                           SourceRange SR) {
48
180
  void *Mem = C.Allocate(totalSizeToAlloc<Expr *>(Elements.size()));
49
180
  return new (Mem) ObjCArrayLiteral(Elements, T, Method, SR);
50
180
}
51
52
ObjCArrayLiteral *ObjCArrayLiteral::CreateEmpty(const ASTContext &C,
53
4
                                                unsigned NumElements) {
54
4
  void *Mem = C.Allocate(totalSizeToAlloc<Expr *>(NumElements));
55
4
  return new (Mem) ObjCArrayLiteral(EmptyShell(), NumElements);
56
4
}
57
58
ObjCDictionaryLiteral::ObjCDictionaryLiteral(ArrayRef<ObjCDictionaryElement> VK,
59
                                             bool HasPackExpansions, QualType T,
60
                                             ObjCMethodDecl *method,
61
                                             SourceRange SR)
62
    : Expr(ObjCDictionaryLiteralClass, T, VK_RValue, OK_Ordinary, false, false,
63
           false, false),
64
      NumElements(VK.size()), HasPackExpansions(HasPackExpansions), Range(SR),
65
120
      DictWithObjectsMethod(method) {
66
120
  KeyValuePair *KeyValues = getTrailingObjects<KeyValuePair>();
67
120
  ExpansionData *Expansions =
68
120
      HasPackExpansions ? 
getTrailingObjects<ExpansionData>()2
:
nullptr118
;
69
259
  for (unsigned I = 0; I < NumElements; 
I++139
) {
70
139
    if (VK[I].Key->isTypeDependent() || 
VK[I].Key->isValueDependent()133
||
71
139
        
VK[I].Value->isTypeDependent()133
||
VK[I].Value->isValueDependent()132
)
72
7
      ExprBits.ValueDependent = true;
73
139
    if (VK[I].Key->isInstantiationDependent() ||
74
139
        
VK[I].Value->isInstantiationDependent()133
)
75
7
      ExprBits.InstantiationDependent = true;
76
139
    if (VK[I].EllipsisLoc.isInvalid() &&
77
139
        
(135
VK[I].Key->containsUnexpandedParameterPack()135
||
78
135
         
VK[I].Value->containsUnexpandedParameterPack()134
))
79
1
      ExprBits.ContainsUnexpandedParameterPack = true;
80
139
81
139
    KeyValues[I].Key = VK[I].Key;
82
139
    KeyValues[I].Value = VK[I].Value;
83
139
    if (Expansions) {
84
4
      Expansions[I].EllipsisLoc = VK[I].EllipsisLoc;
85
4
      if (VK[I].NumExpansions)
86
0
        Expansions[I].NumExpansionsPlusOne = *VK[I].NumExpansions + 1;
87
4
      else
88
4
        Expansions[I].NumExpansionsPlusOne = 0;
89
4
    }
90
139
  }
91
120
}
92
93
ObjCDictionaryLiteral *
94
ObjCDictionaryLiteral::Create(const ASTContext &C,
95
                              ArrayRef<ObjCDictionaryElement> VK,
96
                              bool HasPackExpansions, QualType T,
97
120
                              ObjCMethodDecl *method, SourceRange SR) {
98
120
  void *Mem = C.Allocate(totalSizeToAlloc<KeyValuePair, ExpansionData>(
99
120
      VK.size(), HasPackExpansions ? 
VK.size()2
:
0118
));
100
120
  return new (Mem) ObjCDictionaryLiteral(VK, HasPackExpansions, T, method, SR);
101
120
}
102
103
ObjCDictionaryLiteral *
104
ObjCDictionaryLiteral::CreateEmpty(const ASTContext &C, unsigned NumElements,
105
7
                                   bool HasPackExpansions) {
106
7
  void *Mem = C.Allocate(totalSizeToAlloc<KeyValuePair, ExpansionData>(
107
7
      NumElements, HasPackExpansions ? 
NumElements3
:
04
));
108
7
  return new (Mem)
109
7
      ObjCDictionaryLiteral(EmptyShell(), NumElements, HasPackExpansions);
110
7
}
111
112
3.23k
QualType ObjCPropertyRefExpr::getReceiverType(const ASTContext &ctx) const {
113
3.23k
  if (isClassReceiver())
114
173
    return ctx.getObjCInterfaceType(getClassReceiver());
115
3.06k
116
3.06k
  if (isSuperReceiver())
117
121
    return getSuperReceiverType();
118
2.94k
119
2.94k
  return getBase()->getType();
120
2.94k
}
121
122
ObjCMessageExpr::ObjCMessageExpr(QualType T, ExprValueKind VK,
123
                                 SourceLocation LBracLoc,
124
                                 SourceLocation SuperLoc, bool IsInstanceSuper,
125
                                 QualType SuperType, Selector Sel,
126
                                 ArrayRef<SourceLocation> SelLocs,
127
                                 SelectorLocationsKind SelLocsK,
128
                                 ObjCMethodDecl *Method, ArrayRef<Expr *> Args,
129
                                 SourceLocation RBracLoc, bool isImplicit)
130
    : Expr(ObjCMessageExprClass, T, VK, OK_Ordinary,
131
           /*TypeDependent=*/false, /*ValueDependent=*/false,
132
           /*InstantiationDependent=*/false,
133
           /*ContainsUnexpandedParameterPack=*/false),
134
      SelectorOrMethod(
135
          reinterpret_cast<uintptr_t>(Method ? Method : Sel.getAsOpaquePtr())),
136
      Kind(IsInstanceSuper ? SuperInstance : SuperClass),
137
      HasMethod(Method != nullptr), IsDelegateInitCall(false),
138
      IsImplicit(isImplicit), SuperLoc(SuperLoc), LBracLoc(LBracLoc),
139
884
      RBracLoc(RBracLoc) {
140
884
  initArgsAndSelLocs(Args, SelLocs, SelLocsK);
141
884
  setReceiverPointer(SuperType.getAsOpaquePtr());
142
884
}
143
144
ObjCMessageExpr::ObjCMessageExpr(QualType T, ExprValueKind VK,
145
                                 SourceLocation LBracLoc,
146
                                 TypeSourceInfo *Receiver, Selector Sel,
147
                                 ArrayRef<SourceLocation> SelLocs,
148
                                 SelectorLocationsKind SelLocsK,
149
                                 ObjCMethodDecl *Method, ArrayRef<Expr *> Args,
150
                                 SourceLocation RBracLoc, bool isImplicit)
151
    : Expr(ObjCMessageExprClass, T, VK, OK_Ordinary, T->isDependentType(),
152
           T->isDependentType(), T->isInstantiationDependentType(),
153
           T->containsUnexpandedParameterPack()),
154
      SelectorOrMethod(
155
          reinterpret_cast<uintptr_t>(Method ? Method : Sel.getAsOpaquePtr())),
156
      Kind(Class), HasMethod(Method != nullptr), IsDelegateInitCall(false),
157
3.39k
      IsImplicit(isImplicit), LBracLoc(LBracLoc), RBracLoc(RBracLoc) {
158
3.39k
  initArgsAndSelLocs(Args, SelLocs, SelLocsK);
159
3.39k
  setReceiverPointer(Receiver);
160
3.39k
}
161
162
ObjCMessageExpr::ObjCMessageExpr(QualType T, ExprValueKind VK,
163
                                 SourceLocation LBracLoc, Expr *Receiver,
164
                                 Selector Sel, ArrayRef<SourceLocation> SelLocs,
165
                                 SelectorLocationsKind SelLocsK,
166
                                 ObjCMethodDecl *Method, ArrayRef<Expr *> Args,
167
                                 SourceLocation RBracLoc, bool isImplicit)
168
    : Expr(ObjCMessageExprClass, T, VK, OK_Ordinary,
169
           Receiver->isTypeDependent(), Receiver->isTypeDependent(),
170
           Receiver->isInstantiationDependent(),
171
           Receiver->containsUnexpandedParameterPack()),
172
      SelectorOrMethod(
173
          reinterpret_cast<uintptr_t>(Method ? Method : Sel.getAsOpaquePtr())),
174
      Kind(Instance), HasMethod(Method != nullptr), IsDelegateInitCall(false),
175
9.32k
      IsImplicit(isImplicit), LBracLoc(LBracLoc), RBracLoc(RBracLoc) {
176
9.32k
  initArgsAndSelLocs(Args, SelLocs, SelLocsK);
177
9.32k
  setReceiverPointer(Receiver);
178
9.32k
}
179
180
void ObjCMessageExpr::initArgsAndSelLocs(ArrayRef<Expr *> Args,
181
                                         ArrayRef<SourceLocation> SelLocs,
182
13.6k
                                         SelectorLocationsKind SelLocsK) {
183
13.6k
  setNumArgs(Args.size());
184
13.6k
  Expr **MyArgs = getArgs();
185
20.9k
  for (unsigned I = 0; I != Args.size(); 
++I7.32k
) {
186
7.32k
    if (Args[I]->isTypeDependent())
187
17
      ExprBits.TypeDependent = true;
188
7.32k
    if (Args[I]->isValueDependent())
189
24
      ExprBits.ValueDependent = true;
190
7.32k
    if (Args[I]->isInstantiationDependent())
191
25
      ExprBits.InstantiationDependent = true;
192
7.32k
    if (Args[I]->containsUnexpandedParameterPack())
193
0
      ExprBits.ContainsUnexpandedParameterPack = true;
194
7.32k
195
7.32k
    MyArgs[I] = Args[I];
196
7.32k
  }
197
13.6k
198
13.6k
  SelLocsKind = SelLocsK;
199
13.6k
  if (!isImplicit()) {
200
11.0k
    if (SelLocsK == SelLoc_NonStandard)
201
570
      std::copy(SelLocs.begin(), SelLocs.end(), getStoredSelLocs());
202
11.0k
  }
203
13.6k
}
204
205
ObjCMessageExpr *
206
ObjCMessageExpr::Create(const ASTContext &Context, QualType T, ExprValueKind VK,
207
                        SourceLocation LBracLoc, SourceLocation SuperLoc,
208
                        bool IsInstanceSuper, QualType SuperType, Selector Sel,
209
                        ArrayRef<SourceLocation> SelLocs,
210
                        ObjCMethodDecl *Method, ArrayRef<Expr *> Args,
211
884
                        SourceLocation RBracLoc, bool isImplicit) {
212
884
  assert((!SelLocs.empty() || isImplicit) &&
213
884
         "No selector locs for non-implicit message");
214
884
  ObjCMessageExpr *Mem;
215
884
  SelectorLocationsKind SelLocsK = SelectorLocationsKind();
216
884
  if (isImplicit)
217
74
    Mem = alloc(Context, Args.size(), 0);
218
810
  else
219
810
    Mem = alloc(Context, Args, RBracLoc, SelLocs, Sel, SelLocsK);
220
884
  return new (Mem) ObjCMessageExpr(T, VK, LBracLoc, SuperLoc, IsInstanceSuper,
221
884
                                   SuperType, Sel, SelLocs, SelLocsK, Method,
222
884
                                   Args, RBracLoc, isImplicit);
223
884
}
224
225
ObjCMessageExpr *
226
ObjCMessageExpr::Create(const ASTContext &Context, QualType T, ExprValueKind VK,
227
                        SourceLocation LBracLoc, TypeSourceInfo *Receiver,
228
                        Selector Sel, ArrayRef<SourceLocation> SelLocs,
229
                        ObjCMethodDecl *Method, ArrayRef<Expr *> Args,
230
3.39k
                        SourceLocation RBracLoc, bool isImplicit) {
231
3.39k
  assert((!SelLocs.empty() || isImplicit) &&
232
3.39k
         "No selector locs for non-implicit message");
233
3.39k
  ObjCMessageExpr *Mem;
234
3.39k
  SelectorLocationsKind SelLocsK = SelectorLocationsKind();
235
3.39k
  if (isImplicit)
236
188
    Mem = alloc(Context, Args.size(), 0);
237
3.21k
  else
238
3.21k
    Mem = alloc(Context, Args, RBracLoc, SelLocs, Sel, SelLocsK);
239
3.39k
  return new (Mem)
240
3.39k
      ObjCMessageExpr(T, VK, LBracLoc, Receiver, Sel, SelLocs, SelLocsK, Method,
241
3.39k
                      Args, RBracLoc, isImplicit);
242
3.39k
}
243
244
ObjCMessageExpr *
245
ObjCMessageExpr::Create(const ASTContext &Context, QualType T, ExprValueKind VK,
246
                        SourceLocation LBracLoc, Expr *Receiver, Selector Sel,
247
                        ArrayRef<SourceLocation> SelLocs,
248
                        ObjCMethodDecl *Method, ArrayRef<Expr *> Args,
249
9.32k
                        SourceLocation RBracLoc, bool isImplicit) {
250
9.32k
  assert((!SelLocs.empty() || isImplicit) &&
251
9.32k
         "No selector locs for non-implicit message");
252
9.32k
  ObjCMessageExpr *Mem;
253
9.32k
  SelectorLocationsKind SelLocsK = SelectorLocationsKind();
254
9.32k
  if (isImplicit)
255
2.25k
    Mem = alloc(Context, Args.size(), 0);
256
7.07k
  else
257
7.07k
    Mem = alloc(Context, Args, RBracLoc, SelLocs, Sel, SelLocsK);
258
9.32k
  return new (Mem)
259
9.32k
      ObjCMessageExpr(T, VK, LBracLoc, Receiver, Sel, SelLocs, SelLocsK, Method,
260
9.32k
                      Args, RBracLoc, isImplicit);
261
9.32k
}
262
263
ObjCMessageExpr *ObjCMessageExpr::CreateEmpty(const ASTContext &Context,
264
                                              unsigned NumArgs,
265
29
                                              unsigned NumStoredSelLocs) {
266
29
  ObjCMessageExpr *Mem = alloc(Context, NumArgs, NumStoredSelLocs);
267
29
  return new (Mem) ObjCMessageExpr(EmptyShell(), NumArgs);
268
29
}
269
270
ObjCMessageExpr *ObjCMessageExpr::alloc(const ASTContext &C,
271
                                        ArrayRef<Expr *> Args,
272
                                        SourceLocation RBraceLoc,
273
                                        ArrayRef<SourceLocation> SelLocs,
274
                                        Selector Sel,
275
11.0k
                                        SelectorLocationsKind &SelLocsK) {
276
11.0k
  SelLocsK = hasStandardSelectorLocs(Sel, SelLocs, Args, RBraceLoc);
277
11.0k
  unsigned NumStoredSelLocs =
278
11.0k
      (SelLocsK == SelLoc_NonStandard) ? 
SelLocs.size()570
:
010.5k
;
279
11.0k
  return alloc(C, Args.size(), NumStoredSelLocs);
280
11.0k
}
281
282
ObjCMessageExpr *ObjCMessageExpr::alloc(const ASTContext &C, unsigned NumArgs,
283
13.6k
                                        unsigned NumStoredSelLocs) {
284
13.6k
  return (ObjCMessageExpr *)C.Allocate(
285
13.6k
      totalSizeToAlloc<void *, SourceLocation>(NumArgs + 1, NumStoredSelLocs),
286
13.6k
      alignof(ObjCMessageExpr));
287
13.6k
}
288
289
void ObjCMessageExpr::getSelectorLocs(
290
659
    SmallVectorImpl<SourceLocation> &SelLocs) const {
291
1.29k
  for (unsigned i = 0, e = getNumSelectorLocs(); i != e; 
++i640
)
292
640
    SelLocs.push_back(getSelectorLoc(i));
293
659
}
294
295
296
9
QualType ObjCMessageExpr::getCallReturnType(ASTContext &Ctx) const {
297
9
  if (const ObjCMethodDecl *MD = getMethodDecl()) {
298
9
    QualType QT = MD->getReturnType();
299
9
    if (QT == Ctx.getObjCInstanceType()) {
300
0
      // instancetype corresponds to expression types.
301
0
      return getType();
302
0
    }
303
9
    return QT;
304
9
  }
305
0
306
0
  // Expression type might be different from an expected call return type,
307
0
  // as expression type would never be a reference even if call returns a
308
0
  // reference. Reconstruct the original expression type.
309
0
  QualType QT = getType();
310
0
  switch (getValueKind()) {
311
0
  case VK_LValue:
312
0
    return Ctx.getLValueReferenceType(QT);
313
0
  case VK_XValue:
314
0
    return Ctx.getRValueReferenceType(QT);
315
0
  case VK_RValue:
316
0
    return QT;
317
0
  }
318
0
  llvm_unreachable("Unsupported ExprValueKind");
319
0
}
320
321
69
SourceRange ObjCMessageExpr::getReceiverRange() const {
322
69
  switch (getReceiverKind()) {
323
69
  case Instance:
324
69
    return getInstanceReceiver()->getSourceRange();
325
69
326
69
  case Class:
327
0
    return getClassReceiverTypeInfo()->getTypeLoc().getSourceRange();
328
69
329
69
  case SuperInstance:
330
0
  case SuperClass:
331
0
    return getSuperLoc();
332
0
  }
333
0
334
0
  llvm_unreachable("Invalid ReceiverKind!");
335
0
}
336
337
26.3k
Selector ObjCMessageExpr::getSelector() const {
338
26.3k
  if (HasMethod)
339
25.9k
    return reinterpret_cast<const ObjCMethodDecl *>(SelectorOrMethod)
340
25.9k
        ->getSelector();
341
345
  return Selector(SelectorOrMethod);
342
345
}
343
344
58.9k
QualType ObjCMessageExpr::getReceiverType() const {
345
58.9k
  switch (getReceiverKind()) {
346
58.9k
  case Instance:
347
49.5k
    return getInstanceReceiver()->getType();
348
58.9k
  case Class:
349
6.35k
    return getClassReceiver();
350
58.9k
  case SuperInstance:
351
3.07k
  case SuperClass:
352
3.07k
    return getSuperType();
353
0
  }
354
0
355
0
  llvm_unreachable("unexpected receiver kind");
356
0
}
357
358
58.6k
ObjCInterfaceDecl *ObjCMessageExpr::getReceiverInterface() const {
359
58.6k
  QualType T = getReceiverType();
360
58.6k
361
58.6k
  if (const ObjCObjectPointerType *Ptr = T->getAs<ObjCObjectPointerType>())
362
52.2k
    return Ptr->getInterfaceDecl();
363
6.47k
364
6.47k
  if (const ObjCObjectType *Ty = T->getAs<ObjCObjectType>())
365
6.36k
    return Ty->getInterface();
366
108
367
108
  return nullptr;
368
108
}
369
370
60.8k
Stmt::child_range ObjCMessageExpr::children() {
371
60.8k
  Stmt **begin;
372
60.8k
  if (getReceiverKind() == Instance)
373
42.1k
    begin = reinterpret_cast<Stmt **>(getTrailingObjects<void *>());
374
18.6k
  else
375
18.6k
    begin = reinterpret_cast<Stmt **>(getArgs());
376
60.8k
  return child_range(begin,
377
60.8k
                     reinterpret_cast<Stmt **>(getArgs() + getNumArgs()));
378
60.8k
}
379
380
0
Stmt::const_child_range ObjCMessageExpr::children() const {
381
0
  auto Children = const_cast<ObjCMessageExpr *>(this)->children();
382
0
  return const_child_range(Children.begin(), Children.end());
383
0
}
384
385
4
StringRef ObjCBridgedCastExpr::getBridgeKindName() const {
386
4
  switch (getBridgeKind()) {
387
4
  case OBC_Bridge:
388
4
    return "__bridge";
389
4
  case OBC_BridgeTransfer:
390
0
    return "__bridge_transfer";
391
4
  case OBC_BridgeRetained:
392
0
    return "__bridge_retained";
393
0
  }
394
0
395
0
  llvm_unreachable("Invalid BridgeKind!");
396
0
}