Coverage Report

Created: 2019-07-24 05:18

/Users/buildslave/jenkins/workspace/clang-stage2-coverage-R/llvm/tools/clang/include/clang/AST/StmtCXX.h
Line
Count
Source (jump to first uncovered line)
1
//===--- StmtCXX.h - Classes for representing C++ statements ----*- C++ -*-===//
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 defines the C++ statement AST node classes.
10
//
11
//===----------------------------------------------------------------------===//
12
13
#ifndef LLVM_CLANG_AST_STMTCXX_H
14
#define LLVM_CLANG_AST_STMTCXX_H
15
16
#include "clang/AST/DeclarationName.h"
17
#include "clang/AST/Expr.h"
18
#include "clang/AST/NestedNameSpecifier.h"
19
#include "clang/AST/Stmt.h"
20
#include "llvm/Support/Compiler.h"
21
22
namespace clang {
23
24
class VarDecl;
25
26
/// CXXCatchStmt - This represents a C++ catch block.
27
///
28
class CXXCatchStmt : public Stmt {
29
  SourceLocation CatchLoc;
30
  /// The exception-declaration of the type.
31
  VarDecl *ExceptionDecl;
32
  /// The handler block.
33
  Stmt *HandlerBlock;
34
35
public:
36
  CXXCatchStmt(SourceLocation catchLoc, VarDecl *exDecl, Stmt *handlerBlock)
37
  : Stmt(CXXCatchStmtClass), CatchLoc(catchLoc), ExceptionDecl(exDecl),
38
11.8k
    HandlerBlock(handlerBlock) {}
39
40
  CXXCatchStmt(EmptyShell Empty)
41
4
  : Stmt(CXXCatchStmtClass), ExceptionDecl(nullptr), HandlerBlock(nullptr) {}
42
43
518
  SourceLocation getBeginLoc() const LLVM_READONLY { return CatchLoc; }
44
641
  SourceLocation getEndLoc() const LLVM_READONLY {
45
641
    return HandlerBlock->getEndLoc();
46
641
  }
47
48
299
  SourceLocation getCatchLoc() const { return CatchLoc; }
49
14.7k
  VarDecl *getExceptionDecl() const { return ExceptionDecl; }
50
  QualType getCaughtType() const;
51
1.89k
  Stmt *getHandlerBlock() const { return HandlerBlock; }
52
53
65
  static bool classof(const Stmt *T) {
54
65
    return T->getStmtClass() == CXXCatchStmtClass;
55
65
  }
56
57
793
  child_range children() { return child_range(&HandlerBlock, &HandlerBlock+1); }
58
59
0
  const_child_range children() const {
60
0
    return const_child_range(&HandlerBlock, &HandlerBlock + 1);
61
0
  }
62
63
  friend class ASTStmtReader;
64
};
65
66
/// CXXTryStmt - A C++ try block, including all handlers.
67
///
68
class CXXTryStmt final : public Stmt,
69
                         private llvm::TrailingObjects<CXXTryStmt, Stmt *> {
70
71
  friend TrailingObjects;
72
  friend class ASTStmtReader;
73
74
  SourceLocation TryLoc;
75
  unsigned NumHandlers;
76
0
  size_t numTrailingObjects(OverloadToken<Stmt *>) const { return NumHandlers; }
77
78
  CXXTryStmt(SourceLocation tryLoc, Stmt *tryBlock, ArrayRef<Stmt*> handlers);
79
  CXXTryStmt(EmptyShell Empty, unsigned numHandlers)
80
4
    : Stmt(CXXTryStmtClass), NumHandlers(numHandlers) { }
81
82
2.19k
  Stmt *const *getStmts() const { return getTrailingObjects<Stmt *>(); }
83
15.1k
  Stmt **getStmts() { return getTrailingObjects<Stmt *>(); }
84
85
public:
86
  static CXXTryStmt *Create(const ASTContext &C, SourceLocation tryLoc,
87
                            Stmt *tryBlock, ArrayRef<Stmt*> handlers);
88
89
  static CXXTryStmt *Create(const ASTContext &C, EmptyShell Empty,
90
                            unsigned numHandlers);
91
92
332
  SourceLocation getBeginLoc() const LLVM_READONLY { return getTryLoc(); }
93
94
656
  SourceLocation getTryLoc() const { return TryLoc; }
95
378
  SourceLocation getEndLoc() const {
96
378
    return getStmts()[NumHandlers]->getEndLoc();
97
378
  }
98
99
1.07k
  CompoundStmt *getTryBlock() {
100
1.07k
    return cast<CompoundStmt>(getStmts()[0]);
101
1.07k
  }
102
523
  const CompoundStmt *getTryBlock() const {
103
523
    return cast<CompoundStmt>(getStmts()[0]);
104
523
  }
105
106
2.71k
  unsigned getNumHandlers() const { return NumHandlers; }
107
1.12k
  CXXCatchStmt *getHandler(unsigned i) {
108
1.12k
    return cast<CXXCatchStmt>(getStmts()[i + 1]);
109
1.12k
  }
110
1.29k
  const CXXCatchStmt *getHandler(unsigned i) const {
111
1.29k
    return cast<CXXCatchStmt>(getStmts()[i + 1]);
112
1.29k
  }
113
114
493k
  static bool classof(const Stmt *T) {
115
493k
    return T->getStmtClass() == CXXTryStmtClass;
116
493k
  }
117
118
661
  child_range children() {
119
661
    return child_range(getStmts(), getStmts() + getNumHandlers() + 1);
120
661
  }
121
122
0
  const_child_range children() const {
123
0
    return const_child_range(getStmts(), getStmts() + getNumHandlers() + 1);
124
0
  }
125
};
126
127
/// CXXForRangeStmt - This represents C++0x [stmt.ranged]'s ranged for
128
/// statement, represented as 'for (range-declarator : range-expression)'
129
/// or 'for (init-statement range-declarator : range-expression)'.
130
///
131
/// This is stored in a partially-desugared form to allow full semantic
132
/// analysis of the constituent components. The original syntactic components
133
/// can be extracted using getLoopVariable and getRangeInit.
134
class CXXForRangeStmt : public Stmt {
135
  SourceLocation ForLoc;
136
  enum { INIT, RANGE, BEGINSTMT, ENDSTMT, COND, INC, LOOPVAR, BODY, END };
137
  // SubExprs[RANGE] is an expression or declstmt.
138
  // SubExprs[COND] and SubExprs[INC] are expressions.
139
  Stmt *SubExprs[END];
140
  SourceLocation CoawaitLoc;
141
  SourceLocation ColonLoc;
142
  SourceLocation RParenLoc;
143
144
  friend class ASTStmtReader;
145
public:
146
  CXXForRangeStmt(Stmt *InitStmt, DeclStmt *Range, DeclStmt *Begin,
147
                  DeclStmt *End, Expr *Cond, Expr *Inc, DeclStmt *LoopVar,
148
                  Stmt *Body, SourceLocation FL, SourceLocation CAL,
149
                  SourceLocation CL, SourceLocation RPL);
150
9
  CXXForRangeStmt(EmptyShell Empty) : Stmt(CXXForRangeStmtClass, Empty) { }
151
152
2.39k
  Stmt *getInit() { return SubExprs[INIT]; }
153
  VarDecl *getLoopVariable();
154
  Expr *getRangeInit();
155
156
864
  const Stmt *getInit() const { return SubExprs[INIT]; }
157
  const VarDecl *getLoopVariable() const;
158
  const Expr *getRangeInit() const;
159
160
161
4.61k
  DeclStmt *getRangeStmt() { return cast<DeclStmt>(SubExprs[RANGE]); }
162
4.36k
  DeclStmt *getBeginStmt() {
163
4.36k
    return cast_or_null<DeclStmt>(SubExprs[BEGINSTMT]);
164
4.36k
  }
165
4.36k
  DeclStmt *getEndStmt() { return cast_or_null<DeclStmt>(SubExprs[ENDSTMT]); }
166
6.52k
  Expr *getCond() { return cast_or_null<Expr>(SubExprs[COND]); }
167
2.24k
  Expr *getInc() { return cast_or_null<Expr>(SubExprs[INC]); }
168
4.56k
  DeclStmt *getLoopVarStmt() { return cast<DeclStmt>(SubExprs[LOOPVAR]); }
169
2.36k
  Stmt *getBody() { return SubExprs[BODY]; }
170
171
928
  const DeclStmt *getRangeStmt() const {
172
928
    return cast<DeclStmt>(SubExprs[RANGE]);
173
928
  }
174
858
  const DeclStmt *getBeginStmt() const {
175
858
    return cast_or_null<DeclStmt>(SubExprs[BEGINSTMT]);
176
858
  }
177
858
  const DeclStmt *getEndStmt() const {
178
858
    return cast_or_null<DeclStmt>(SubExprs[ENDSTMT]);
179
858
  }
180
1.89k
  const Expr *getCond() const {
181
1.89k
    return cast_or_null<Expr>(SubExprs[COND]);
182
1.89k
  }
183
992
  const Expr *getInc() const {
184
992
    return cast_or_null<Expr>(SubExprs[INC]);
185
992
  }
186
955
  const DeclStmt *getLoopVarStmt() const {
187
955
    return cast<DeclStmt>(SubExprs[LOOPVAR]);
188
955
  }
189
1.82k
  const Stmt *getBody() const { return SubExprs[BODY]; }
190
191
9
  void setInit(Stmt *S) { SubExprs[INIT] = S; }
192
0
  void setRangeInit(Expr *E) { SubExprs[RANGE] = reinterpret_cast<Stmt*>(E); }
193
9
  void setRangeStmt(Stmt *S) { SubExprs[RANGE] = S; }
194
9
  void setBeginStmt(Stmt *S) { SubExprs[BEGINSTMT] = S; }
195
9
  void setEndStmt(Stmt *S) { SubExprs[ENDSTMT] = S; }
196
9
  void setCond(Expr *E) { SubExprs[COND] = reinterpret_cast<Stmt*>(E); }
197
9
  void setInc(Expr *E) { SubExprs[INC] = reinterpret_cast<Stmt*>(E); }
198
9
  void setLoopVarStmt(Stmt *S) { SubExprs[LOOPVAR] = S; }
199
2.84k
  void setBody(Stmt *S) { SubExprs[BODY] = S; }
200
201
116
  SourceLocation getForLoc() const { return ForLoc; }
202
116
  SourceLocation getCoawaitLoc() const { return CoawaitLoc; }
203
138
  SourceLocation getColonLoc() const { return ColonLoc; }
204
2.95k
  SourceLocation getRParenLoc() const { return RParenLoc; }
205
206
12.5k
  SourceLocation getBeginLoc() const LLVM_READONLY { return ForLoc; }
207
2.82k
  SourceLocation getEndLoc() const LLVM_READONLY {
208
2.82k
    return SubExprs[BODY]->getEndLoc();
209
2.82k
  }
210
211
43.3k
  static bool classof(const Stmt *T) {
212
43.3k
    return T->getStmtClass() == CXXForRangeStmtClass;
213
43.3k
  }
214
215
  // Iterators
216
1.18k
  child_range children() {
217
1.18k
    return child_range(&SubExprs[0], &SubExprs[END]);
218
1.18k
  }
219
220
0
  const_child_range children() const {
221
0
    return const_child_range(&SubExprs[0], &SubExprs[END]);
222
0
  }
223
};
224
225
/// Representation of a Microsoft __if_exists or __if_not_exists
226
/// statement with a dependent name.
227
///
228
/// The __if_exists statement can be used to include a sequence of statements
229
/// in the program only when a particular dependent name does not exist. For
230
/// example:
231
///
232
/// \code
233
/// template<typename T>
234
/// void call_foo(T &t) {
235
///   __if_exists (T::foo) {
236
///     t.foo(); // okay: only called when T::foo exists.
237
///   }
238
/// }
239
/// \endcode
240
///
241
/// Similarly, the __if_not_exists statement can be used to include the
242
/// statements when a particular name does not exist.
243
///
244
/// Note that this statement only captures __if_exists and __if_not_exists
245
/// statements whose name is dependent. All non-dependent cases are handled
246
/// directly in the parser, so that they don't introduce a new scope. Clang
247
/// introduces scopes in the dependent case to keep names inside the compound
248
/// statement from leaking out into the surround statements, which would
249
/// compromise the template instantiation model. This behavior differs from
250
/// Visual C++ (which never introduces a scope), but is a fairly reasonable
251
/// approximation of the VC++ behavior.
252
class MSDependentExistsStmt : public Stmt {
253
  SourceLocation KeywordLoc;
254
  bool IsIfExists;
255
  NestedNameSpecifierLoc QualifierLoc;
256
  DeclarationNameInfo NameInfo;
257
  Stmt *SubStmt;
258
259
  friend class ASTReader;
260
  friend class ASTStmtReader;
261
262
public:
263
  MSDependentExistsStmt(SourceLocation KeywordLoc, bool IsIfExists,
264
                        NestedNameSpecifierLoc QualifierLoc,
265
                        DeclarationNameInfo NameInfo,
266
                        CompoundStmt *SubStmt)
267
  : Stmt(MSDependentExistsStmtClass),
268
    KeywordLoc(KeywordLoc), IsIfExists(IsIfExists),
269
    QualifierLoc(QualifierLoc), NameInfo(NameInfo),
270
8
    SubStmt(reinterpret_cast<Stmt *>(SubStmt)) { }
271
272
  /// Retrieve the location of the __if_exists or __if_not_exists
273
  /// keyword.
274
6
  SourceLocation getKeywordLoc() const { return KeywordLoc; }
275
276
  /// Determine whether this is an __if_exists statement.
277
8
  bool isIfExists() const { return IsIfExists; }
278
279
  /// Determine whether this is an __if_exists statement.
280
4
  bool isIfNotExists() const { return !IsIfExists; }
281
282
  /// Retrieve the nested-name-specifier that qualifies this name, if
283
  /// any.
284
30
  NestedNameSpecifierLoc getQualifierLoc() const { return QualifierLoc; }
285
286
  /// Retrieve the name of the entity we're testing for, along with
287
  /// location information
288
14
  DeclarationNameInfo getNameInfo() const { return NameInfo; }
289
290
  /// Retrieve the compound statement that will be included in the
291
  /// program only if the existence of the symbol matches the initial keyword.
292
8
  CompoundStmt *getSubStmt() const {
293
8
    return reinterpret_cast<CompoundStmt *>(SubStmt);
294
8
  }
295
296
34
  SourceLocation getBeginLoc() const LLVM_READONLY { return KeywordLoc; }
297
34
  SourceLocation getEndLoc() const LLVM_READONLY {
298
34
    return SubStmt->getEndLoc();
299
34
  }
300
301
2
  child_range children() {
302
2
    return child_range(&SubStmt, &SubStmt+1);
303
2
  }
304
305
0
  const_child_range children() const {
306
0
    return const_child_range(&SubStmt, &SubStmt + 1);
307
0
  }
308
309
0
  static bool classof(const Stmt *T) {
310
0
    return T->getStmtClass() == MSDependentExistsStmtClass;
311
0
  }
312
};
313
314
/// Represents the body of a coroutine. This wraps the normal function
315
/// body and holds the additional semantic context required to set up and tear
316
/// down the coroutine frame.
317
class CoroutineBodyStmt final
318
    : public Stmt,
319
      private llvm::TrailingObjects<CoroutineBodyStmt, Stmt *> {
320
  enum SubStmt {
321
    Body,          ///< The body of the coroutine.
322
    Promise,       ///< The promise statement.
323
    InitSuspend,   ///< The initial suspend statement, run before the body.
324
    FinalSuspend,  ///< The final suspend statement, run after the body.
325
    OnException,   ///< Handler for exceptions thrown in the body.
326
    OnFallthrough, ///< Handler for control flow falling off the body.
327
    Allocate,      ///< Coroutine frame memory allocation.
328
    Deallocate,    ///< Coroutine frame memory deallocation.
329
    ReturnValue,   ///< Return value for thunk function: p.get_return_object().
330
    ResultDecl,    ///< Declaration holding the result of get_return_object.
331
    ReturnStmt,    ///< Return statement for the thunk function.
332
    ReturnStmtOnAllocFailure, ///< Return statement if allocation failed.
333
    FirstParamMove ///< First offset for move construction of parameter copies.
334
  };
335
  unsigned NumParams;
336
337
  friend class ASTStmtReader;
338
  friend class ASTReader;
339
  friend TrailingObjects;
340
341
241
  Stmt **getStoredStmts() { return getTrailingObjects<Stmt *>(); }
342
343
1.90k
  Stmt *const *getStoredStmts() const { return getTrailingObjects<Stmt *>(); }
344
345
public:
346
347
  struct CtorArgs {
348
    Stmt *Body = nullptr;
349
    Stmt *Promise = nullptr;
350
    Expr *InitialSuspend = nullptr;
351
    Expr *FinalSuspend = nullptr;
352
    Stmt *OnException = nullptr;
353
    Stmt *OnFallthrough = nullptr;
354
    Expr *Allocate = nullptr;
355
    Expr *Deallocate = nullptr;
356
    Expr *ReturnValue = nullptr;
357
    Stmt *ResultDecl = nullptr;
358
    Stmt *ReturnStmt = nullptr;
359
    Stmt *ReturnStmtOnAllocFailure = nullptr;
360
    ArrayRef<Stmt *> ParamMoves;
361
  };
362
363
private:
364
365
  CoroutineBodyStmt(CtorArgs const& Args);
366
367
public:
368
  static CoroutineBodyStmt *Create(const ASTContext &C, CtorArgs const &Args);
369
  static CoroutineBodyStmt *Create(const ASTContext &C, EmptyShell,
370
                                   unsigned NumParams);
371
372
44
  bool hasDependentPromiseType() const {
373
44
    return getPromiseDecl()->getType()->isDependentType();
374
44
  }
375
376
  /// Retrieve the body of the coroutine as written. This will be either
377
  /// a CompoundStmt or a TryStmt.
378
850
  Stmt *getBody() const {
379
850
    return getStoredStmts()[SubStmt::Body];
380
850
  }
381
382
178
  Stmt *getPromiseDeclStmt() const {
383
178
    return getStoredStmts()[SubStmt::Promise];
384
178
  }
385
137
  VarDecl *getPromiseDecl() const {
386
137
    return cast<VarDecl>(cast<DeclStmt>(getPromiseDeclStmt())->getSingleDecl());
387
137
  }
388
389
93
  Stmt *getInitSuspendStmt() const {
390
93
    return getStoredStmts()[SubStmt::InitSuspend];
391
93
  }
392
91
  Stmt *getFinalSuspendStmt() const {
393
91
    return getStoredStmts()[SubStmt::FinalSuspend];
394
91
  }
395
396
46
  Stmt *getExceptionHandler() const {
397
46
    return getStoredStmts()[SubStmt::OnException];
398
46
  }
399
83
  Stmt *getFallthroughHandler() const {
400
83
    return getStoredStmts()[SubStmt::OnFallthrough];
401
83
  }
402
403
46
  Expr *getAllocate() const {
404
46
    return cast_or_null<Expr>(getStoredStmts()[SubStmt::Allocate]);
405
46
  }
406
46
  Expr *getDeallocate() const {
407
46
    return cast_or_null<Expr>(getStoredStmts()[SubStmt::Deallocate]);
408
46
  }
409
44
  Expr *getReturnValueInit() const {
410
44
    return cast<Expr>(getStoredStmts()[SubStmt::ReturnValue]);
411
44
  }
412
68
  Stmt *getResultDecl() const { return getStoredStmts()[SubStmt::ResultDecl]; }
413
46
  Stmt *getReturnStmt() const { return getStoredStmts()[SubStmt::ReturnStmt]; }
414
46
  Stmt *getReturnStmtOnAllocFailure() const {
415
46
    return getStoredStmts()[SubStmt::ReturnStmtOnAllocFailure];
416
46
  }
417
263
  ArrayRef<Stmt const *> getParamMoves() const {
418
263
    return {getStoredStmts() + SubStmt::FirstParamMove, NumParams};
419
263
  }
420
421
73
  SourceLocation getBeginLoc() const LLVM_READONLY {
422
73
    return getBody() ? getBody()->getBeginLoc()
423
73
                     : 
getPromiseDecl()->getBeginLoc()0
;
424
73
  }
425
295
  SourceLocation getEndLoc() const LLVM_READONLY {
426
295
    return getBody() ? getBody()->getEndLoc() : 
getPromiseDecl()->getEndLoc()0
;
427
295
  }
428
429
9
  child_range children() {
430
9
    return child_range(getStoredStmts(),
431
9
                       getStoredStmts() + SubStmt::FirstParamMove + NumParams);
432
9
  }
433
434
0
  const_child_range children() const {
435
0
    return const_child_range(getStoredStmts(), getStoredStmts() +
436
0
                                                   SubStmt::FirstParamMove +
437
0
                                                   NumParams);
438
0
  }
439
440
2.12M
  static bool classof(const Stmt *T) {
441
2.12M
    return T->getStmtClass() == CoroutineBodyStmtClass;
442
2.12M
  }
443
};
444
445
/// Represents a 'co_return' statement in the C++ Coroutines TS.
446
///
447
/// This statament models the initialization of the coroutine promise
448
/// (encapsulating the eventual notional return value) from an expression
449
/// (or braced-init-list), followed by termination of the coroutine.
450
///
451
/// This initialization is modeled by the evaluation of the operand
452
/// followed by a call to one of:
453
///   <promise>.return_value(<operand>)
454
///   <promise>.return_void()
455
/// which we name the "promise call".
456
class CoreturnStmt : public Stmt {
457
  SourceLocation CoreturnLoc;
458
459
  enum SubStmt { Operand, PromiseCall, Count };
460
  Stmt *SubStmts[SubStmt::Count];
461
462
  bool IsImplicit : 1;
463
464
  friend class ASTStmtReader;
465
public:
466
  CoreturnStmt(SourceLocation CoreturnLoc, Stmt *Operand, Stmt *PromiseCall,
467
               bool IsImplicit = false)
468
      : Stmt(CoreturnStmtClass), CoreturnLoc(CoreturnLoc),
469
218
        IsImplicit(IsImplicit) {
470
218
    SubStmts[SubStmt::Operand] = Operand;
471
218
    SubStmts[SubStmt::PromiseCall] = PromiseCall;
472
218
  }
473
474
2
  CoreturnStmt(EmptyShell) : CoreturnStmt({}, {}, {}) {}
Unexecuted instantiation: clang::CoreturnStmt::CoreturnStmt(clang::Stmt::EmptyShell)
clang::CoreturnStmt::CoreturnStmt(clang::Stmt::EmptyShell)
Line
Count
Source
474
2
  CoreturnStmt(EmptyShell) : CoreturnStmt({}, {}, {}) {}
475
476
18
  SourceLocation getKeywordLoc() const { return CoreturnLoc; }
477
478
  /// Retrieve the operand of the 'co_return' statement. Will be nullptr
479
  /// if none was specified.
480
86
  Expr *getOperand() const { return static_cast<Expr*>(SubStmts[Operand]); }
481
482
  /// Retrieve the promise call that results from this 'co_return'
483
  /// statement. Will be nullptr if either the coroutine has not yet been
484
  /// finalized or the coroutine has no eventual return type.
485
41
  Expr *getPromiseCall() const {
486
41
    return static_cast<Expr*>(SubStmts[PromiseCall]);
487
41
  }
488
489
18
  bool isImplicit() const { return IsImplicit; }
490
0
  void setIsImplicit(bool value = true) { IsImplicit = value; }
491
492
22
  SourceLocation getBeginLoc() const LLVM_READONLY { return CoreturnLoc; }
493
9
  SourceLocation getEndLoc() const LLVM_READONLY {
494
9
    return getOperand() ? 
getOperand()->getEndLoc()0
: getBeginLoc();
495
9
  }
496
497
20
  child_range children() {
498
20
    if (!getOperand())
499
7
      return child_range(SubStmts + SubStmt::PromiseCall,
500
7
                         SubStmts + SubStmt::Count);
501
13
    return child_range(SubStmts, SubStmts + SubStmt::Count);
502
13
  }
503
504
0
  const_child_range children() const {
505
0
    if (!getOperand())
506
0
      return const_child_range(SubStmts + SubStmt::PromiseCall,
507
0
                               SubStmts + SubStmt::Count);
508
0
    return const_child_range(SubStmts, SubStmts + SubStmt::Count);
509
0
  }
510
511
286
  static bool classof(const Stmt *T) {
512
286
    return T->getStmtClass() == CoreturnStmtClass;
513
286
  }
514
};
515
516
}  // end namespace clang
517
518
#endif