Coverage Report

Created: 2020-09-19 12:23

/Users/buildslave/jenkins/workspace/coverage/llvm-project/clang/lib/Sema/SemaExceptionSpec.cpp
Line
Count
Source (jump to first uncovered line)
1
//===--- SemaExceptionSpec.cpp - C++ Exception Specifications ---*- 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 provides Sema routines for C++ exception specification testing.
10
//
11
//===----------------------------------------------------------------------===//
12
13
#include "clang/Sema/SemaInternal.h"
14
#include "clang/AST/ASTMutationListener.h"
15
#include "clang/AST/CXXInheritance.h"
16
#include "clang/AST/Expr.h"
17
#include "clang/AST/ExprCXX.h"
18
#include "clang/AST/StmtObjC.h"
19
#include "clang/AST/TypeLoc.h"
20
#include "clang/Basic/Diagnostic.h"
21
#include "clang/Basic/SourceManager.h"
22
#include "llvm/ADT/SmallPtrSet.h"
23
#include "llvm/ADT/SmallString.h"
24
25
namespace clang {
26
27
static const FunctionProtoType *GetUnderlyingFunction(QualType T)
28
4.18M
{
29
4.18M
  if (const PointerType *PtrTy = T->getAs<PointerType>())
30
494k
    T = PtrTy->getPointeeType();
31
3.68M
  else if (const ReferenceType *RefTy = T->getAs<ReferenceType>())
32
2.25k
    T = RefTy->getPointeeType();
33
3.68M
  else if (const MemberPointerType *MPTy = T->getAs<MemberPointerType>())
34
3.98k
    T = MPTy->getPointeeType();
35
4.18M
  return T->getAs<FunctionProtoType>();
36
4.18M
}
37
38
/// HACK: libstdc++ has a bug where it shadows std::swap with a member
39
/// swap function then tries to call std::swap unqualified from the exception
40
/// specification of that function. This function detects whether we're in
41
/// such a case and turns off delay-parsing of exception specifications.
42
940k
bool Sema::isLibstdcxxEagerExceptionSpecHack(const Declarator &D) {
43
940k
  auto *RD = dyn_cast<CXXRecordDecl>(CurContext);
44
940k
45
  // All the problem cases are member functions named "swap" within class
46
  // templates declared directly within namespace std or std::__debug or
47
  // std::__profile.
48
940k
  if (!RD || 
!RD->getIdentifier()940k
||
!RD->getDescribedClassTemplate()940k
||
49
482k
      !D.getIdentifier() || 
!D.getIdentifier()->isStr("swap")257k
)
50
935k
    return false;
51
5.34k
52
5.34k
  auto *ND = dyn_cast<NamespaceDecl>(RD->getDeclContext());
53
5.34k
  if (!ND)
54
1
    return false;
55
5.34k
56
5.34k
  bool IsInStd = ND->isStdNamespace();
57
5.34k
  if (!IsInStd) {
58
    // This isn't a direct member of namespace std, but it might still be
59
    // libstdc++'s std::__debug::array or std::__profile::array.
60
21
    IdentifierInfo *II = ND->getIdentifier();
61
21
    if (!II || !(II->isStr("__debug") || 
II->isStr("__profile")20
) ||
62
2
        !ND->isInStdNamespace())
63
19
      return false;
64
5.32k
  }
65
5.32k
66
  // Only apply this hack within a system header.
67
5.32k
  if (!Context.getSourceManager().isInSystemHeader(D.getBeginLoc()))
68
0
    return false;
69
5.32k
70
5.32k
  return llvm::StringSwitch<bool>(RD->getIdentifier()->getName())
71
5.32k
      .Case("array", true)
72
5.32k
      .Case("pair", IsInStd)
73
5.32k
      .Case("priority_queue", IsInStd)
74
5.32k
      .Case("stack", IsInStd)
75
5.32k
      .Case("queue", IsInStd)
76
5.32k
      .Default(false);
77
5.32k
}
78
79
ExprResult Sema::ActOnNoexceptSpec(SourceLocation NoexceptLoc,
80
                                   Expr *NoexceptExpr,
81
50.1k
                                   ExceptionSpecificationType &EST) {
82
  // FIXME: This is bogus, a noexcept expression is not a condition.
83
50.1k
  ExprResult Converted = CheckBooleanCondition(NoexceptLoc, NoexceptExpr);
84
50.1k
  if (Converted.isInvalid()) {
85
10
    EST = EST_NoexceptFalse;
86
10
87
    // Fill in an expression of 'false' as a fixup.
88
10
    auto *BoolExpr = new (Context)
89
10
        CXXBoolLiteralExpr(false, Context.BoolTy, NoexceptExpr->getBeginLoc());
90
10
    llvm::APSInt Value{1};
91
10
    Value = 0;
92
10
    return ConstantExpr::Create(Context, BoolExpr, APValue{Value});
93
10
  }
94
50.1k
95
50.1k
  if (Converted.get()->isValueDependent()) {
96
43.9k
    EST = EST_DependentNoexcept;
97
43.9k
    return Converted;
98
43.9k
  }
99
6.22k
100
6.22k
  llvm::APSInt Result;
101
6.22k
  Converted = VerifyIntegerConstantExpression(
102
6.22k
      Converted.get(), &Result,
103
6.22k
      diag::err_noexcept_needs_constant_expression,
104
6.22k
      /*AllowFold*/ false);
105
6.22k
  if (!Converted.isInvalid())
106
6.22k
    EST = !Result ? 
EST_NoexceptFalse769
:
EST_NoexceptTrue5.45k
;
107
6.22k
  return Converted;
108
6.22k
}
109
110
/// CheckSpecifiedExceptionType - Check if the given type is valid in an
111
/// exception specification. Incomplete types, or pointers to incomplete types
112
/// other than void are not allowed.
113
///
114
/// \param[in,out] T  The exception type. This will be decayed to a pointer type
115
///                   when the input is an array or a function type.
116
777
bool Sema::CheckSpecifiedExceptionType(QualType &T, SourceRange Range) {
117
  // C++11 [except.spec]p2:
118
  //   A type cv T, "array of T", or "function returning T" denoted
119
  //   in an exception-specification is adjusted to type T, "pointer to T", or
120
  //   "pointer to function returning T", respectively.
121
  //
122
  // We also apply this rule in C++98.
123
777
  if (T->isArrayType())
124
14
    T = Context.getArrayDecayedType(T);
125
763
  else if (T->isFunctionType())
126
4
    T = Context.getPointerType(T);
127
777
128
777
  int Kind = 0;
129
777
  QualType PointeeT = T;
130
777
  if (const PointerType *PT = T->getAs<PointerType>()) {
131
96
    PointeeT = PT->getPointeeType();
132
96
    Kind = 1;
133
96
134
    // cv void* is explicitly permitted, despite being a pointer to an
135
    // incomplete type.
136
96
    if (PointeeT->isVoidType())
137
5
      return false;
138
681
  } else if (const ReferenceType *RT = T->getAs<ReferenceType>()) {
139
56
    PointeeT = RT->getPointeeType();
140
56
    Kind = 2;
141
56
142
56
    if (RT->isRValueReferenceType()) {
143
      // C++11 [except.spec]p2:
144
      //   A type denoted in an exception-specification shall not denote [...]
145
      //   an rvalue reference type.
146
3
      Diag(Range.getBegin(), diag::err_rref_in_exception_spec)
147
3
        << T << Range;
148
3
      return true;
149
3
    }
150
769
  }
151
769
152
  // C++11 [except.spec]p2:
153
  //   A type denoted in an exception-specification shall not denote an
154
  //   incomplete type other than a class currently being defined [...].
155
  //   A type denoted in an exception-specification shall not denote a
156
  //   pointer or reference to an incomplete type, other than (cv) void* or a
157
  //   pointer or reference to a class currently being defined.
158
  // In Microsoft mode, downgrade this to a warning.
159
769
  unsigned DiagID = diag::err_incomplete_in_exception_spec;
160
769
  bool ReturnValueOnError = true;
161
769
  if (getLangOpts().MSVCCompat) {
162
25
    DiagID = diag::ext_incomplete_in_exception_spec;
163
25
    ReturnValueOnError = false;
164
25
  }
165
769
  if (!(PointeeT->isRecordType() &&
166
247
        PointeeT->castAs<RecordType>()->isBeingDefined()) &&
167
766
      RequireCompleteType(Range.getBegin(), PointeeT, DiagID, Kind, Range))
168
17
    return ReturnValueOnError;
169
752
170
  // The MSVC compatibility mode doesn't extend to sizeless types,
171
  // so diagnose them separately.
172
752
  if (PointeeT->isSizelessType() && 
Kind != 16
) {
173
4
    Diag(Range.getBegin(), diag::err_sizeless_in_exception_spec)
174
2
        << (Kind == 2 ? 1 : 0) << PointeeT << Range;
175
4
    return true;
176
4
  }
177
748
178
748
  return false;
179
748
}
180
181
/// CheckDistantExceptionSpec - Check if the given type is a pointer or pointer
182
/// to member to a function with an exception specification. This means that
183
/// it is invalid to add another level of indirection.
184
3.10M
bool Sema::CheckDistantExceptionSpec(QualType T) {
185
  // C++17 removes this rule in favor of putting exception specifications into
186
  // the type system.
187
3.10M
  if (getLangOpts().CPlusPlus17)
188
77.8k
    return false;
189
3.03M
190
3.03M
  if (const PointerType *PT = T->getAs<PointerType>())
191
85.6k
    T = PT->getPointeeType();
192
2.94M
  else if (const MemberPointerType *PT = T->getAs<MemberPointerType>())
193
187
    T = PT->getPointeeType();
194
2.94M
  else
195
2.94M
    return false;
196
85.8k
197
85.8k
  const FunctionProtoType *FnT = T->getAs<FunctionProtoType>();
198
85.8k
  if (!FnT)
199
85.2k
    return false;
200
591
201
591
  return FnT->hasExceptionSpec();
202
591
}
203
204
const FunctionProtoType *
205
4.01M
Sema::ResolveExceptionSpec(SourceLocation Loc, const FunctionProtoType *FPT) {
206
4.01M
  if (FPT->getExceptionSpecType() == EST_Unparsed) {
207
1
    Diag(Loc, diag::err_exception_spec_not_parsed);
208
1
    return nullptr;
209
1
  }
210
4.01M
211
4.01M
  if (!isUnresolvedExceptionSpec(FPT->getExceptionSpecType()))
212
3.82M
    return FPT;
213
185k
214
185k
  FunctionDecl *SourceDecl = FPT->getExceptionSpecDecl();
215
185k
  const FunctionProtoType *SourceFPT =
216
185k
      SourceDecl->getType()->castAs<FunctionProtoType>();
217
185k
218
  // If the exception specification has already been resolved, just return it.
219
185k
  if (!isUnresolvedExceptionSpec(SourceFPT->getExceptionSpecType()))
220
7.21k
    return SourceFPT;
221
178k
222
  // Compute or instantiate the exception specification now.
223
178k
  if (SourceFPT->getExceptionSpecType() == EST_Unevaluated)
224
172k
    EvaluateImplicitExceptionSpec(Loc, SourceDecl);
225
5.78k
  else
226
5.78k
    InstantiateExceptionSpec(Loc, SourceDecl);
227
178k
228
178k
  const FunctionProtoType *Proto =
229
178k
    SourceDecl->getType()->castAs<FunctionProtoType>();
230
178k
  if (Proto->getExceptionSpecType() == clang::EST_Unparsed) {
231
2
    Diag(Loc, diag::err_exception_spec_not_parsed);
232
2
    Proto = nullptr;
233
2
  }
234
178k
  return Proto;
235
178k
}
236
237
void
238
Sema::UpdateExceptionSpec(FunctionDecl *FD,
239
521k
                          const FunctionProtoType::ExceptionSpecInfo &ESI) {
240
  // If we've fully resolved the exception specification, notify listeners.
241
521k
  if (!isUnresolvedExceptionSpec(ESI.Type))
242
521k
    if (auto *Listener = getASTMutationListener())
243
35.4k
      Listener->ResolvedExceptionSpec(FD);
244
521k
245
521k
  for (FunctionDecl *Redecl : FD->redecls())
246
521k
    Context.adjustExceptionSpec(Redecl, ESI);
247
521k
}
248
249
480k
static bool exceptionSpecNotKnownYet(const FunctionDecl *FD) {
250
480k
  auto *MD = dyn_cast<CXXMethodDecl>(FD);
251
480k
  if (!MD)
252
108k
    return false;
253
371k
254
371k
  auto EST = MD->getType()->castAs<FunctionProtoType>()->getExceptionSpecType();
255
371k
  return EST == EST_Unparsed ||
256
371k
         (EST == EST_Unevaluated && 
MD->getParent()->isBeingDefined()19.2k
);
257
371k
}
258
259
static bool CheckEquivalentExceptionSpecImpl(
260
    Sema &S, const PartialDiagnostic &DiagID, const PartialDiagnostic &NoteID,
261
    const FunctionProtoType *Old, SourceLocation OldLoc,
262
    const FunctionProtoType *New, SourceLocation NewLoc,
263
    bool *MissingExceptionSpecification = nullptr,
264
    bool *MissingEmptyExceptionSpecification = nullptr,
265
    bool AllowNoexceptAllMatchWithNoSpec = false, bool IsOperatorNew = false);
266
267
/// Determine whether a function has an implicitly-generated exception
268
/// specification.
269
393k
static bool hasImplicitExceptionSpec(FunctionDecl *Decl) {
270
393k
  if (!isa<CXXDestructorDecl>(Decl) &&
271
386k
      Decl->getDeclName().getCXXOverloadedOperator() != OO_Delete &&
272
385k
      Decl->getDeclName().getCXXOverloadedOperator() != OO_Array_Delete)
273
384k
    return false;
274
8.56k
275
  // For a function that the user didn't declare:
276
  //  - if this is a destructor, its exception specification is implicit.
277
  //  - if this is 'operator delete' or 'operator delete[]', the exception
278
  //    specification is as-if an explicit exception specification was given
279
  //    (per [basic.stc.dynamic]p2).
280
8.56k
  if (!Decl->getTypeSourceInfo())
281
825
    return isa<CXXDestructorDecl>(Decl);
282
7.74k
283
7.74k
  auto *Ty = Decl->getTypeSourceInfo()->getType()->castAs<FunctionProtoType>();
284
7.74k
  return !Ty->hasExceptionSpec();
285
7.74k
}
286
287
220k
bool Sema::CheckEquivalentExceptionSpec(FunctionDecl *Old, FunctionDecl *New) {
288
  // Just completely ignore this under -fno-exceptions prior to C++17.
289
  // In C++17 onwards, the exception specification is part of the type and
290
  // we will diagnose mismatches anyway, so it's better to check for them here.
291
220k
  if (!getLangOpts().CXXExceptions && 
!getLangOpts().CPlusPlus1722.5k
)
292
18.6k
    return false;
293
201k
294
201k
  OverloadedOperatorKind OO = New->getDeclName().getCXXOverloadedOperator();
295
201k
  bool IsOperatorNew = OO == OO_New || 
OO == OO_Array_New200k
;
296
201k
  bool MissingExceptionSpecification = false;
297
201k
  bool MissingEmptyExceptionSpecification = false;
298
201k
299
201k
  unsigned DiagID = diag::err_mismatched_exception_spec;
300
201k
  bool ReturnValueOnError = true;
301
201k
  if (getLangOpts().MSVCCompat) {
302
210
    DiagID = diag::ext_mismatched_exception_spec;
303
210
    ReturnValueOnError = false;
304
210
  }
305
201k
306
  // If we're befriending a member function of a class that's currently being
307
  // defined, we might not be able to work out its exception specification yet.
308
  // If not, defer the check until later.
309
201k
  if (exceptionSpecNotKnownYet(Old) || 
exceptionSpecNotKnownYet(New)201k
) {
310
7
    DelayedEquivalentExceptionSpecChecks.push_back({New, Old});
311
7
    return false;
312
7
  }
313
201k
314
  // Check the types as written: they must match before any exception
315
  // specification adjustment is applied.
316
201k
  if (!CheckEquivalentExceptionSpecImpl(
317
201k
        *this, PDiag(DiagID), PDiag(diag::note_previous_declaration),
318
201k
        Old->getType()->getAs<FunctionProtoType>(), Old->getLocation(),
319
201k
        New->getType()->getAs<FunctionProtoType>(), New->getLocation(),
320
201k
        &MissingExceptionSpecification, &MissingEmptyExceptionSpecification,
321
201k
        /*AllowNoexceptAllMatchWithNoSpec=*/true, IsOperatorNew)) {
322
    // C++11 [except.spec]p4 [DR1492]:
323
    //   If a declaration of a function has an implicit
324
    //   exception-specification, other declarations of the function shall
325
    //   not specify an exception-specification.
326
201k
    if (getLangOpts().CPlusPlus11 && 
getLangOpts().CXXExceptions200k
&&
327
196k
        hasImplicitExceptionSpec(Old) != hasImplicitExceptionSpec(New)) {
328
23
      Diag(New->getLocation(), diag::ext_implicit_exception_spec_mismatch)
329
23
        << hasImplicitExceptionSpec(Old);
330
23
      if (Old->getLocation().isValid())
331
11
        Diag(Old->getLocation(), diag::note_previous_declaration);
332
23
    }
333
201k
    return false;
334
201k
  }
335
310
336
  // The failure was something other than an missing exception
337
  // specification; return an error, except in MS mode where this is a warning.
338
310
  if (!MissingExceptionSpecification)
339
43
    return ReturnValueOnError;
340
267
341
267
  const FunctionProtoType *NewProto =
342
267
    New->getType()->castAs<FunctionProtoType>();
343
267
344
  // The new function declaration is only missing an empty exception
345
  // specification "throw()". If the throw() specification came from a
346
  // function in a system header that has C linkage, just add an empty
347
  // exception specification to the "new" declaration. Note that C library
348
  // implementations are permitted to add these nothrow exception
349
  // specifications.
350
  //
351
  // Likewise if the old function is a builtin.
352
267
  if (MissingEmptyExceptionSpecification && 
NewProto262
&&
353
262
      (Old->getLocation().isInvalid() ||
354
260
       Context.getSourceManager().isInSystemHeader(Old->getLocation()) ||
355
21
       Old->getBuiltinID()) &&
356
244
      Old->isExternC()) {
357
242
    New->setType(Context.getFunctionType(
358
242
        NewProto->getReturnType(), NewProto->getParamTypes(),
359
242
        NewProto->getExtProtoInfo().withExceptionSpec(EST_DynamicNone)));
360
242
    return false;
361
242
  }
362
25
363
25
  const FunctionProtoType *OldProto =
364
25
    Old->getType()->castAs<FunctionProtoType>();
365
25
366
25
  FunctionProtoType::ExceptionSpecInfo ESI = OldProto->getExceptionSpecType();
367
25
  if (ESI.Type == EST_Dynamic) {
368
    // FIXME: What if the exceptions are described in terms of the old
369
    // prototype's parameters?
370
4
    ESI.Exceptions = OldProto->exceptions();
371
4
  }
372
25
373
25
  if (ESI.Type == EST_NoexceptFalse)
374
0
    ESI.Type = EST_None;
375
25
  if (ESI.Type == EST_NoexceptTrue)
376
1
    ESI.Type = EST_BasicNoexcept;
377
25
378
  // For dependent noexcept, we can't just take the expression from the old
379
  // prototype. It likely contains references to the old prototype's parameters.
380
25
  if (ESI.Type == EST_DependentNoexcept) {
381
1
    New->setInvalidDecl();
382
24
  } else {
383
    // Update the type of the function with the appropriate exception
384
    // specification.
385
24
    New->setType(Context.getFunctionType(
386
24
        NewProto->getReturnType(), NewProto->getParamTypes(),
387
24
        NewProto->getExtProtoInfo().withExceptionSpec(ESI)));
388
24
  }
389
25
390
25
  if (getLangOpts().MSVCCompat && 
ESI.Type != EST_DependentNoexcept3
) {
391
    // Allow missing exception specifications in redeclarations as an extension.
392
3
    DiagID = diag::ext_ms_missing_exception_specification;
393
3
    ReturnValueOnError = false;
394
22
  } else if (New->isReplaceableGlobalAllocationFunction() &&
395
4
             ESI.Type != EST_DependentNoexcept) {
396
    // Allow missing exception specifications in redeclarations as an extension,
397
    // when declaring a replaceable global allocation function.
398
4
    DiagID = diag::ext_missing_exception_specification;
399
4
    ReturnValueOnError = false;
400
18
  } else if (ESI.Type == EST_NoThrow) {
401
    // Allow missing attribute 'nothrow' in redeclarations, since this is a very
402
    // common omission.
403
5
    DiagID = diag::ext_missing_exception_specification;
404
5
    ReturnValueOnError = false;
405
13
  } else {
406
13
    DiagID = diag::err_missing_exception_specification;
407
13
    ReturnValueOnError = true;
408
13
  }
409
25
410
  // Warn about the lack of exception specification.
411
25
  SmallString<128> ExceptionSpecString;
412
25
  llvm::raw_svector_ostream OS(ExceptionSpecString);
413
25
  switch (OldProto->getExceptionSpecType()) {
414
10
  case EST_DynamicNone:
415
10
    OS << "throw()";
416
10
    break;
417
0
418
4
  case EST_Dynamic: {
419
4
    OS << "throw(";
420
4
    bool OnFirstException = true;
421
4
    for (const auto &E : OldProto->exceptions()) {
422
4
      if (OnFirstException)
423
4
        OnFirstException = false;
424
0
      else
425
0
        OS << ", ";
426
4
427
4
      OS << E.getAsString(getPrintingPolicy());
428
4
    }
429
4
    OS << ")";
430
4
    break;
431
0
  }
432
0
433
4
  case EST_BasicNoexcept:
434
4
    OS << "noexcept";
435
4
    break;
436
0
437
2
  case EST_DependentNoexcept:
438
2
  case EST_NoexceptFalse:
439
2
  case EST_NoexceptTrue:
440
2
    OS << "noexcept(";
441
2
    assert(OldProto->getNoexceptExpr() != nullptr && "Expected non-null Expr");
442
2
    OldProto->getNoexceptExpr()->printPretty(OS, nullptr, getPrintingPolicy());
443
2
    OS << ")";
444
2
    break;
445
5
  case EST_NoThrow:
446
5
    OS <<"__attribute__((nothrow))";
447
5
    break;
448
0
  case EST_None:
449
0
  case EST_MSAny:
450
0
  case EST_Unevaluated:
451
0
  case EST_Uninstantiated:
452
0
  case EST_Unparsed:
453
0
    llvm_unreachable("This spec type is compatible with none.");
454
25
  }
455
25
456
25
  SourceLocation FixItLoc;
457
25
  if (TypeSourceInfo *TSInfo = New->getTypeSourceInfo()) {
458
25
    TypeLoc TL = TSInfo->getTypeLoc().IgnoreParens();
459
    // FIXME: Preserve enough information so that we can produce a correct fixit
460
    // location when there is a trailing return type.
461
25
    if (auto FTLoc = TL.getAs<FunctionProtoTypeLoc>())
462
25
      if (!FTLoc.getTypePtr()->hasTrailingReturn())
463
25
        FixItLoc = getLocForEndOfToken(FTLoc.getLocalRangeEnd());
464
25
  }
465
25
466
25
  if (FixItLoc.isInvalid())
467
0
    Diag(New->getLocation(), DiagID)
468
0
      << New << OS.str();
469
25
  else {
470
25
    Diag(New->getLocation(), DiagID)
471
25
      << New << OS.str()
472
25
      << FixItHint::CreateInsertion(FixItLoc, " " + OS.str().str());
473
25
  }
474
25
475
25
  if (Old->getLocation().isValid())
476
22
    Diag(Old->getLocation(), diag::note_previous_declaration);
477
25
478
25
  return ReturnValueOnError;
479
25
}
480
481
/// CheckEquivalentExceptionSpec - Check if the two types have equivalent
482
/// exception specifications. Exception specifications are equivalent if
483
/// they allow exactly the same set of exception types. It does not matter how
484
/// that is achieved. See C++ [except.spec]p2.
485
bool Sema::CheckEquivalentExceptionSpec(
486
    const FunctionProtoType *Old, SourceLocation OldLoc,
487
1.83k
    const FunctionProtoType *New, SourceLocation NewLoc) {
488
1.83k
  if (!getLangOpts().CXXExceptions)
489
1.06k
    return false;
490
763
491
763
  unsigned DiagID = diag::err_mismatched_exception_spec;
492
763
  if (getLangOpts().MSVCCompat)
493
0
    DiagID = diag::ext_mismatched_exception_spec;
494
763
  bool Result = CheckEquivalentExceptionSpecImpl(
495
763
      *this, PDiag(DiagID), PDiag(diag::note_previous_declaration),
496
763
      Old, OldLoc, New, NewLoc);
497
763
498
  // In Microsoft mode, mismatching exception specifications just cause a warning.
499
763
  if (getLangOpts().MSVCCompat)
500
0
    return false;
501
763
  return Result;
502
763
}
503
504
/// CheckEquivalentExceptionSpec - Check if the two types have compatible
505
/// exception specifications. See C++ [except.spec]p3.
506
///
507
/// \return \c false if the exception specifications match, \c true if there is
508
/// a problem. If \c true is returned, either a diagnostic has already been
509
/// produced or \c *MissingExceptionSpecification is set to \c true.
510
static bool CheckEquivalentExceptionSpecImpl(
511
    Sema &S, const PartialDiagnostic &DiagID, const PartialDiagnostic &NoteID,
512
    const FunctionProtoType *Old, SourceLocation OldLoc,
513
    const FunctionProtoType *New, SourceLocation NewLoc,
514
    bool *MissingExceptionSpecification,
515
    bool *MissingEmptyExceptionSpecification,
516
202k
    bool AllowNoexceptAllMatchWithNoSpec, bool IsOperatorNew) {
517
202k
  if (MissingExceptionSpecification)
518
201k
    *MissingExceptionSpecification = false;
519
202k
520
202k
  if (MissingEmptyExceptionSpecification)
521
201k
    *MissingEmptyExceptionSpecification = false;
522
202k
523
202k
  Old = S.ResolveExceptionSpec(NewLoc, Old);
524
202k
  if (!Old)
525
0
    return false;
526
202k
  New = S.ResolveExceptionSpec(NewLoc, New);
527
202k
  if (!New)
528
0
    return false;
529
202k
530
  // C++0x [except.spec]p3: Two exception-specifications are compatible if:
531
  //   - both are non-throwing, regardless of their form,
532
  //   - both have the form noexcept(constant-expression) and the constant-
533
  //     expressions are equivalent,
534
  //   - both are dynamic-exception-specifications that have the same set of
535
  //     adjusted types.
536
  //
537
  // C++0x [except.spec]p12: An exception-specification is non-throwing if it is
538
  //   of the form throw(), noexcept, or noexcept(constant-expression) where the
539
  //   constant-expression yields true.
540
  //
541
  // C++0x [except.spec]p4: If any declaration of a function has an exception-
542
  //   specifier that is not a noexcept-specification allowing all exceptions,
543
  //   all declarations [...] of that function shall have a compatible
544
  //   exception-specification.
545
  //
546
  // That last point basically means that noexcept(false) matches no spec.
547
  // It's considered when AllowNoexceptAllMatchWithNoSpec is true.
548
202k
549
202k
  ExceptionSpecificationType OldEST = Old->getExceptionSpecType();
550
202k
  ExceptionSpecificationType NewEST = New->getExceptionSpecType();
551
202k
552
202k
  assert(!isUnresolvedExceptionSpec(OldEST) &&
553
202k
         !isUnresolvedExceptionSpec(NewEST) &&
554
202k
         "Shouldn't see unknown exception specifications here");
555
202k
556
202k
  CanThrowResult OldCanThrow = Old->canThrow();
557
202k
  CanThrowResult NewCanThrow = New->canThrow();
558
202k
559
  // Any non-throwing specifications are compatible.
560
202k
  if (OldCanThrow == CT_Cannot && 
NewCanThrow == CT_Cannot52.7k
)
561
52.4k
    return false;
562
149k
563
  // Any throws-anything specifications are usually compatible.
564
149k
  if (OldCanThrow == CT_Can && 
OldEST != EST_Dynamic143k
&&
565
143k
      NewCanThrow == CT_Can && 
NewEST != EST_Dynamic143k
) {
566
    // The exception is that the absence of an exception specification only
567
    // matches noexcept(false) for functions, as described above.
568
143k
    if (!AllowNoexceptAllMatchWithNoSpec &&
569
745
        ((OldEST == EST_None && 
NewEST == EST_NoexceptFalse743
) ||
570
744
         (OldEST == EST_NoexceptFalse && 
NewEST == EST_None2
))) {
571
      // This is the disallowed case.
572
143k
    } else {
573
143k
      return false;
574
143k
    }
575
5.96k
  }
576
5.96k
577
  // C++14 [except.spec]p3:
578
  //   Two exception-specifications are compatible if [...] both have the form
579
  //   noexcept(constant-expression) and the constant-expressions are equivalent
580
5.96k
  if (OldEST == EST_DependentNoexcept && 
NewEST == EST_DependentNoexcept5.60k
) {
581
5.60k
    llvm::FoldingSetNodeID OldFSN, NewFSN;
582
5.60k
    Old->getNoexceptExpr()->Profile(OldFSN, S.Context, true);
583
5.60k
    New->getNoexceptExpr()->Profile(NewFSN, S.Context, true);
584
5.60k
    if (OldFSN == NewFSN)
585
5.60k
      return false;
586
363
  }
587
363
588
  // Dynamic exception specifications with the same set of adjusted types
589
  // are compatible.
590
363
  if (OldEST == EST_Dynamic && 
NewEST == EST_Dynamic46
) {
591
29
    bool Success = true;
592
    // Both have a dynamic exception spec. Collect the first set, then compare
593
    // to the second.
594
29
    llvm::SmallPtrSet<CanQualType, 8> OldTypes, NewTypes;
595
29
    for (const auto &I : Old->exceptions())
596
34
      OldTypes.insert(S.Context.getCanonicalType(I).getUnqualifiedType());
597
29
598
36
    for (const auto &I : New->exceptions()) {
599
36
      CanQualType TypePtr = S.Context.getCanonicalType(I).getUnqualifiedType();
600
36
      if (OldTypes.count(TypePtr))
601
26
        NewTypes.insert(TypePtr);
602
10
      else {
603
10
        Success = false;
604
10
        break;
605
10
      }
606
36
    }
607
29
608
29
    if (Success && 
OldTypes.size() == NewTypes.size()19
)
609
19
      return false;
610
344
  }
611
344
612
  // As a special compatibility feature, under C++0x we accept no spec and
613
  // throw(std::bad_alloc) as equivalent for operator new and operator new[].
614
  // This is because the implicit declaration changed, but old code would break.
615
344
  if (S.getLangOpts().CPlusPlus11 && 
IsOperatorNew335
) {
616
12
    const FunctionProtoType *WithExceptions = nullptr;
617
12
    if (OldEST == EST_None && 
NewEST == EST_Dynamic9
)
618
6
      WithExceptions = New;
619
6
    else if (OldEST == EST_Dynamic && 
NewEST == EST_None3
)
620
3
      WithExceptions = Old;
621
12
    if (WithExceptions && 
WithExceptions->getNumExceptions() == 19
) {
622
      // One has no spec, the other throw(something). If that something is
623
      // std::bad_alloc, all conditions are met.
624
9
      QualType Exception = *WithExceptions->exception_begin();
625
9
      if (CXXRecordDecl *ExRecord = Exception->getAsCXXRecordDecl()) {
626
9
        IdentifierInfo* Name = ExRecord->getIdentifier();
627
9
        if (Name && Name->getName() == "bad_alloc") {
628
          // It's called bad_alloc, but is it in std?
629
9
          if (ExRecord->isInStdNamespace()) {
630
9
            return false;
631
9
          }
632
335
        }
633
9
      }
634
9
    }
635
12
  }
636
335
637
  // If the caller wants to handle the case that the new function is
638
  // incompatible due to a missing exception specification, let it.
639
335
  if (MissingExceptionSpecification && 
OldEST != EST_None310
&&
640
298
      NewEST == EST_None) {
641
    // The old type has an exception specification of some sort, but
642
    // the new type does not.
643
267
    *MissingExceptionSpecification = true;
644
267
645
267
    if (MissingEmptyExceptionSpecification && OldCanThrow == CT_Cannot) {
646
      // The old type has a throw() or noexcept(true) exception specification
647
      // and the new type has no exception specification, and the caller asked
648
      // to handle this itself.
649
262
      *MissingEmptyExceptionSpecification = true;
650
262
    }
651
267
652
267
    return true;
653
267
  }
654
68
655
68
  S.Diag(NewLoc, DiagID);
656
68
  if (NoteID.getDiagID() != 0 && 
OldLoc.isValid()64
)
657
61
    S.Diag(OldLoc, NoteID);
658
68
  return true;
659
68
}
660
661
bool Sema::CheckEquivalentExceptionSpec(const PartialDiagnostic &DiagID,
662
                                        const PartialDiagnostic &NoteID,
663
                                        const FunctionProtoType *Old,
664
                                        SourceLocation OldLoc,
665
                                        const FunctionProtoType *New,
666
17
                                        SourceLocation NewLoc) {
667
17
  if (!getLangOpts().CXXExceptions)
668
0
    return false;
669
17
  return CheckEquivalentExceptionSpecImpl(*this, DiagID, NoteID, Old, OldLoc,
670
17
                                          New, NewLoc);
671
17
}
672
673
226
bool Sema::handlerCanCatch(QualType HandlerType, QualType ExceptionType) {
674
  // [except.handle]p3:
675
  //   A handler is a match for an exception object of type E if:
676
226
677
  // HandlerType must be ExceptionType or derived from it, or pointer or
678
  // reference to such types.
679
226
  const ReferenceType *RefTy = HandlerType->getAs<ReferenceType>();
680
226
  if (RefTy)
681
45
    HandlerType = RefTy->getPointeeType();
682
226
683
  //   -- the handler is of type cv T or cv T& and E and T are the same type
684
226
  if (Context.hasSameUnqualifiedType(ExceptionType, HandlerType))
685
89
    return true;
686
137
687
  // FIXME: ObjC pointer types?
688
137
  if (HandlerType->isPointerType() || 
HandlerType->isMemberPointerType()77
) {
689
62
    if (RefTy && 
(16
!HandlerType.isConstQualified()16
||
690
12
                  HandlerType.isVolatileQualified()))
691
6
      return false;
692
56
693
    // -- the handler is of type cv T or const T& where T is a pointer or
694
    //    pointer to member type and E is std::nullptr_t
695
56
    if (ExceptionType->isNullPtrType())
696
8
      return true;
697
48
698
    // -- the handler is of type cv T or const T& where T is a pointer or
699
    //    pointer to member type and E is a pointer or pointer to member type
700
    //    that can be converted to T by one or more of
701
    //    -- a qualification conversion
702
    //    -- a function pointer conversion
703
48
    bool LifetimeConv;
704
48
    QualType Result;
705
    // FIXME: Should we treat the exception as catchable if a lifetime
706
    // conversion is required?
707
48
    if (IsQualificationConversion(ExceptionType, HandlerType, false,
708
48
                                  LifetimeConv) ||
709
43
        IsFunctionConversion(ExceptionType, HandlerType, Result))
710
7
      return true;
711
41
712
    //    -- a standard pointer conversion [...]
713
41
    if (!ExceptionType->isPointerType() || 
!HandlerType->isPointerType()37
)
714
4
      return false;
715
37
716
    // Handle the "qualification conversion" portion.
717
37
    Qualifiers EQuals, HQuals;
718
37
    ExceptionType = Context.getUnqualifiedArrayType(
719
37
        ExceptionType->getPointeeType(), EQuals);
720
37
    HandlerType = Context.getUnqualifiedArrayType(
721
37
        HandlerType->getPointeeType(), HQuals);
722
37
    if (!HQuals.compatiblyIncludes(EQuals))
723
3
      return false;
724
34
725
34
    if (HandlerType->isVoidType() && 
ExceptionType->isObjectType()5
)
726
5
      return true;
727
104
728
    // The only remaining case is a derived-to-base conversion.
729
34
  }
730
104
731
  //   -- the handler is of type cg T or cv T& and T is an unambiguous public
732
  //      base class of E
733
104
  if (!ExceptionType->isRecordType() || 
!HandlerType->isRecordType()71
)
734
36
    return false;
735
68
  CXXBasePaths Paths(/*FindAmbiguities=*/true, /*RecordPaths=*/true,
736
68
                     /*DetectVirtual=*/false);
737
68
  if (!IsDerivedFrom(SourceLocation(), ExceptionType, HandlerType, Paths) ||
738
56
      Paths.isAmbiguous(Context.getCanonicalType(HandlerType)))
739
22
    return false;
740
46
741
  // Do this check from a context without privileges.
742
46
  switch (CheckBaseClassAccess(SourceLocation(), HandlerType, ExceptionType,
743
46
                               Paths.front(),
744
46
                               /*Diagnostic*/ 0,
745
46
                               /*ForceCheck*/ true,
746
46
                               /*ForceUnprivileged*/ true)) {
747
31
  case AR_accessible: return true;
748
15
  case AR_inaccessible: return false;
749
0
  case AR_dependent:
750
0
    llvm_unreachable("access check dependent for unprivileged context");
751
0
  case AR_delayed:
752
0
    llvm_unreachable("access check delayed in non-declaration");
753
0
  }
754
0
  llvm_unreachable("unexpected access check result");
755
0
}
756
757
/// CheckExceptionSpecSubset - Check whether the second function type's
758
/// exception specification is a subset (or equivalent) of the first function
759
/// type. This is used by override and pointer assignment checks.
760
bool Sema::CheckExceptionSpecSubset(const PartialDiagnostic &DiagID,
761
                                    const PartialDiagnostic &NestedDiagID,
762
                                    const PartialDiagnostic &NoteID,
763
                                    const PartialDiagnostic &NoThrowDiagID,
764
                                    const FunctionProtoType *Superset,
765
                                    SourceLocation SuperLoc,
766
                                    const FunctionProtoType *Subset,
767
46.9k
                                    SourceLocation SubLoc) {
768
46.9k
769
  // Just auto-succeed under -fno-exceptions.
770
46.9k
  if (!getLangOpts().CXXExceptions)
771
7.80k
    return false;
772
39.1k
773
  // FIXME: As usual, we could be more specific in our error messages, but
774
  // that better waits until we've got types with source locations.
775
39.1k
776
39.1k
  if (!SubLoc.isValid())
777
12.1k
    SubLoc = SuperLoc;
778
39.1k
779
  // Resolve the exception specifications, if needed.
780
39.1k
  Superset = ResolveExceptionSpec(SuperLoc, Superset);
781
39.1k
  if (!Superset)
782
0
    return false;
783
39.1k
  Subset = ResolveExceptionSpec(SubLoc, Subset);
784
39.1k
  if (!Subset)
785
0
    return false;
786
39.1k
787
39.1k
  ExceptionSpecificationType SuperEST = Superset->getExceptionSpecType();
788
39.1k
  ExceptionSpecificationType SubEST = Subset->getExceptionSpecType();
789
39.1k
  assert(!isUnresolvedExceptionSpec(SuperEST) &&
790
39.1k
         !isUnresolvedExceptionSpec(SubEST) &&
791
39.1k
         "Shouldn't see unknown exception specifications here");
792
39.1k
793
  // If there are dependent noexcept specs, assume everything is fine. Unlike
794
  // with the equivalency check, this is safe in this case, because we don't
795
  // want to merge declarations. Checks after instantiation will catch any
796
  // omissions we make here.
797
39.1k
  if (SuperEST == EST_DependentNoexcept || SubEST == EST_DependentNoexcept)
798
1
    return false;
799
39.1k
800
39.1k
  CanThrowResult SuperCanThrow = Superset->canThrow();
801
39.1k
  CanThrowResult SubCanThrow = Subset->canThrow();
802
39.1k
803
  // If the superset contains everything or the subset contains nothing, we're
804
  // done.
805
39.1k
  if ((SuperCanThrow == CT_Can && 
SuperEST != EST_Dynamic20.0k
) ||
806
19.3k
      SubCanThrow == CT_Cannot)
807
38.9k
    return CheckParamExceptionSpec(NestedDiagID, NoteID, Superset, SuperLoc,
808
38.9k
                                   Subset, SubLoc);
809
196
810
  // Allow __declspec(nothrow) to be missing on redeclaration as an extension in
811
  // some cases.
812
196
  if (NoThrowDiagID.getDiagID() != 0 && 
SubCanThrow == CT_Can98
&&
813
98
      SuperCanThrow == CT_Cannot && 
SuperEST == EST_NoThrow11
) {
814
2
    Diag(SubLoc, NoThrowDiagID);
815
2
    if (NoteID.getDiagID() != 0)
816
2
      Diag(SuperLoc, NoteID);
817
2
    return true;
818
2
  }
819
194
820
  // If the subset contains everything or the superset contains nothing, we've
821
  // failed.
822
194
  if ((SubCanThrow == CT_Can && SubEST != EST_Dynamic) ||
823
177
      SuperCanThrow == CT_Cannot) {
824
52
    Diag(SubLoc, DiagID);
825
52
    if (NoteID.getDiagID() != 0)
826
11
      Diag(SuperLoc, NoteID);
827
52
    return true;
828
52
  }
829
142
830
142
  assert(SuperEST == EST_Dynamic && SubEST == EST_Dynamic &&
831
142
         "Exception spec subset: non-dynamic case slipped through.");
832
142
833
  // Neither contains everything or nothing. Do a proper comparison.
834
163
  for (QualType SubI : Subset->exceptions()) {
835
163
    if (const ReferenceType *RefTy = SubI->getAs<ReferenceType>())
836
23
      SubI = RefTy->getPointeeType();
837
163
838
    // Make sure it's in the superset.
839
163
    bool Contained = false;
840
176
    for (QualType SuperI : Superset->exceptions()) {
841
      // [except.spec]p5:
842
      //   the target entity shall allow at least the exceptions allowed by the
843
      //   source
844
      //
845
      // We interpret this as meaning that a handler for some target type would
846
      // catch an exception of each source type.
847
176
      if (handlerCanCatch(SuperI, SubI)) {
848
112
        Contained = true;
849
112
        break;
850
112
      }
851
176
    }
852
163
    if (!Contained) {
853
51
      Diag(SubLoc, DiagID);
854
51
      if (NoteID.getDiagID() != 0)
855
33
        Diag(SuperLoc, NoteID);
856
51
      return true;
857
51
    }
858
163
  }
859
  // We've run half the gauntlet.
860
91
  return CheckParamExceptionSpec(NestedDiagID, NoteID, Superset, SuperLoc,
861
91
                                 Subset, SubLoc);
862
142
}
863
864
static bool
865
CheckSpecForTypesEquivalent(Sema &S, const PartialDiagnostic &DiagID,
866
                            const PartialDiagnostic &NoteID, QualType Target,
867
                            SourceLocation TargetLoc, QualType Source,
868
60.0k
                            SourceLocation SourceLoc) {
869
60.0k
  const FunctionProtoType *TFunc = GetUnderlyingFunction(Target);
870
60.0k
  if (!TFunc)
871
60.0k
    return false;
872
9
  const FunctionProtoType *SFunc = GetUnderlyingFunction(Source);
873
9
  if (!SFunc)
874
0
    return false;
875
9
876
9
  return S.CheckEquivalentExceptionSpec(DiagID, NoteID, TFunc, TargetLoc,
877
9
                                        SFunc, SourceLoc);
878
9
}
879
880
/// CheckParamExceptionSpec - Check if the parameter and return types of the
881
/// two functions have equivalent exception specs. This is part of the
882
/// assignment and override compatibility check. We do not check the parameters
883
/// of parameter function pointers recursively, as no sane programmer would
884
/// even be able to write such a function type.
885
bool Sema::CheckParamExceptionSpec(const PartialDiagnostic &DiagID,
886
                                   const PartialDiagnostic &NoteID,
887
                                   const FunctionProtoType *Target,
888
                                   SourceLocation TargetLoc,
889
                                   const FunctionProtoType *Source,
890
39.0k
                                   SourceLocation SourceLoc) {
891
39.0k
  auto RetDiag = DiagID;
892
39.0k
  RetDiag << 0;
893
39.0k
  if (CheckSpecForTypesEquivalent(
894
39.0k
          *this, RetDiag, PDiag(),
895
39.0k
          Target->getReturnType(), TargetLoc, Source->getReturnType(),
896
39.0k
          SourceLoc))
897
2
    return true;
898
39.0k
899
  // We shouldn't even be testing this unless the arguments are otherwise
900
  // compatible.
901
39.0k
  assert(Target->getNumParams() == Source->getNumParams() &&
902
39.0k
         "Functions have different argument counts.");
903
60.0k
  for (unsigned i = 0, E = Target->getNumParams(); i != E; 
++i20.9k
) {
904
20.9k
    auto ParamDiag = DiagID;
905
20.9k
    ParamDiag << 1;
906
20.9k
    if (CheckSpecForTypesEquivalent(
907
20.9k
            *this, ParamDiag, PDiag(),
908
20.9k
            Target->getParamType(i), TargetLoc, Source->getParamType(i),
909
20.9k
            SourceLoc))
910
2
      return true;
911
20.9k
  }
912
39.0k
  return false;
913
39.0k
}
914
915
4.10M
bool Sema::CheckExceptionSpecCompatibility(Expr *From, QualType ToType) {
916
  // First we check for applicability.
917
  // Target type must be a function, function pointer or function reference.
918
4.10M
  const FunctionProtoType *ToFunc = GetUnderlyingFunction(ToType);
919
4.10M
  if (!ToFunc || 
ToFunc->hasDependentExceptionSpec()16.6k
)
920
4.09M
    return false;
921
16.6k
922
  // SourceType must be a function or function pointer.
923
16.6k
  const FunctionProtoType *FromFunc = GetUnderlyingFunction(From->getType());
924
16.6k
  if (!FromFunc || 
FromFunc->hasDependentExceptionSpec()16.5k
)
925
133
    return false;
926
16.5k
927
16.5k
  unsigned DiagID = diag::err_incompatible_exception_specs;
928
16.5k
  unsigned NestedDiagID = diag::err_deep_exception_specs_differ;
929
  // This is not an error in C++17 onwards, unless the noexceptness doesn't
930
  // match, but in that case we have a full-on type mismatch, not just a
931
  // type sugar mismatch.
932
16.5k
  if (getLangOpts().CPlusPlus17) {
933
739
    DiagID = diag::warn_incompatible_exception_specs;
934
739
    NestedDiagID = diag::warn_deep_exception_specs_differ;
935
739
  }
936
16.5k
937
  // Now we've got the correct types on both sides, check their compatibility.
938
  // This means that the source of the conversion can only throw a subset of
939
  // the exceptions of the target, and any exception specs on arguments or
940
  // return types must be equivalent.
941
  //
942
  // FIXME: If there is a nested dependent exception specification, we should
943
  // not be checking it here. This is fine:
944
  //   template<typename T> void f() {
945
  //     void (*p)(void (*) throw(T));
946
  //     void (*q)(void (*) throw(int)) = p;
947
  //   }
948
  // ... because it might be instantiated with T=int.
949
16.5k
  return CheckExceptionSpecSubset(
950
16.5k
             PDiag(DiagID), PDiag(NestedDiagID), PDiag(), PDiag(), ToFunc,
951
16.5k
             From->getSourceRange().getBegin(), FromFunc, SourceLocation()) &&
952
63
         !getLangOpts().CPlusPlus17;
953
16.5k
}
954
955
bool Sema::CheckOverridingFunctionExceptionSpec(const CXXMethodDecl *New,
956
39.9k
                                                const CXXMethodDecl *Old) {
957
  // If the new exception specification hasn't been parsed yet, skip the check.
958
  // We'll get called again once it's been parsed.
959
39.9k
  if (New->getType()->castAs<FunctionProtoType>()->getExceptionSpecType() ==
960
39.9k
      EST_Unparsed)
961
239
    return false;
962
39.6k
963
  // Don't check uninstantiated template destructors at all. We can only
964
  // synthesize correct specs after the template is instantiated.
965
39.6k
  if (isa<CXXDestructorDecl>(New) && 
New->getParent()->isDependentType()23.8k
)
966
932
    return false;
967
38.7k
968
  // If the old exception specification hasn't been parsed yet, or the new
969
  // exception specification can't be computed yet, remember that we need to
970
  // perform this check when we get to the end of the outermost
971
  // lexically-surrounding class.
972
38.7k
  if (exceptionSpecNotKnownYet(Old) || 
exceptionSpecNotKnownYet(New)38.7k
) {
973
8.27k
    DelayedOverridingExceptionSpecChecks.push_back({New, Old});
974
8.27k
    return false;
975
8.27k
  }
976
30.4k
977
30.4k
  unsigned DiagID = diag::err_override_exception_spec;
978
30.4k
  if (getLangOpts().MSVCCompat)
979
89
    DiagID = diag::ext_override_exception_spec;
980
30.4k
  return CheckExceptionSpecSubset(PDiag(DiagID),
981
30.4k
                                  PDiag(diag::err_deep_exception_specs_differ),
982
30.4k
                                  PDiag(diag::note_overridden_virtual_function),
983
30.4k
                                  PDiag(diag::ext_override_exception_spec),
984
30.4k
                                  Old->getType()->castAs<FunctionProtoType>(),
985
30.4k
                                  Old->getLocation(),
986
30.4k
                                  New->getType()->castAs<FunctionProtoType>(),
987
30.4k
                                  New->getLocation());
988
30.4k
}
989
990
45.1k
static CanThrowResult canSubStmtsThrow(Sema &Self, const Stmt *S) {
991
45.1k
  CanThrowResult R = CT_Cannot;
992
68.8k
  for (const Stmt *SubStmt : S->children()) {
993
68.8k
    if (!SubStmt)
994
5
      continue;
995
68.8k
    R = mergeCanThrow(R, Self.canThrow(SubStmt));
996
68.8k
    if (R == CT_Can)
997
672
      break;
998
68.8k
  }
999
45.1k
  return R;
1000
45.1k
}
1001
1002
CanThrowResult Sema::canCalleeThrow(Sema &S, const Expr *E, const Decl *D,
1003
7.89k
                                    SourceLocation Loc) {
1004
  // As an extension, we assume that __attribute__((nothrow)) functions don't
1005
  // throw.
1006
7.89k
  if (D && 
isa<FunctionDecl>(D)7.72k
&&
D->hasAttr<NoThrowAttr>()7.68k
)
1007
209
    return CT_Cannot;
1008
7.68k
1009
7.68k
  QualType T;
1010
7.68k
1011
  // In C++1z, just look at the function type of the callee.
1012
7.68k
  if (S.getLangOpts().CPlusPlus17 && 
E736
&&
isa<CallExpr>(E)716
) {
1013
421
    E = cast<CallExpr>(E)->getCallee();
1014
421
    T = E->getType();
1015
421
    if (T->isSpecificPlaceholderType(BuiltinType::BoundMember)) {
1016
      // Sadly we don't preserve the actual type as part of the "bound member"
1017
      // placeholder, so we need to reconstruct it.
1018
156
      E = E->IgnoreParenImpCasts();
1019
156
1020
      // Could be a call to a pointer-to-member or a plain member access.
1021
156
      if (auto *Op = dyn_cast<BinaryOperator>(E)) {
1022
0
        assert(Op->getOpcode() == BO_PtrMemD || Op->getOpcode() == BO_PtrMemI);
1023
0
        T = Op->getRHS()->getType()
1024
0
              ->castAs<MemberPointerType>()->getPointeeType();
1025
156
      } else {
1026
156
        T = cast<MemberExpr>(E)->getMemberDecl()->getType();
1027
156
      }
1028
156
    }
1029
7.26k
  } else if (const ValueDecl *VD = dyn_cast_or_null<ValueDecl>(D))
1030
7.08k
    T = VD->getType();
1031
175
  else
1032
    // If we have no clue what we're calling, assume the worst.
1033
175
    return CT_Can;
1034
7.51k
1035
7.51k
  const FunctionProtoType *FT;
1036
7.51k
  if ((FT = T->getAs<FunctionProtoType>())) {
1037
287
  } else if (const PointerType *PT = T->getAs<PointerType>())
1038
277
    FT = PT->getPointeeType()->getAs<FunctionProtoType>();
1039
10
  else if (const ReferenceType *RT = T->getAs<ReferenceType>())
1040
5
    FT = RT->getPointeeType()->getAs<FunctionProtoType>();
1041
5
  else if (const MemberPointerType *MT = T->getAs<MemberPointerType>())
1042
4
    FT = MT->getPointeeType()->getAs<FunctionProtoType>();
1043
1
  else if (const BlockPointerType *BT = T->getAs<BlockPointerType>())
1044
0
    FT = BT->getPointeeType()->getAs<FunctionProtoType>();
1045
7.51k
1046
7.51k
  if (!FT)
1047
5
    return CT_Can;
1048
7.50k
1049
7.50k
  if (Loc.isValid() || (Loc.isInvalid() && E))
1050
7.21k
    FT = S.ResolveExceptionSpec(Loc.isInvalid() ? E->getBeginLoc() : 
Loc0
, FT);
1051
7.50k
  if (!FT)
1052
0
    return CT_Can;
1053
7.50k
1054
7.50k
  return FT->canThrow();
1055
7.50k
}
1056
1057
62
static CanThrowResult canVarDeclThrow(Sema &Self, const VarDecl *VD) {
1058
62
  CanThrowResult CT = CT_Cannot;
1059
62
1060
  // Initialization might throw.
1061
62
  if (!VD->isUsableInConstantExpressions(Self.Context))
1062
62
    if (const Expr *Init = VD->getInit())
1063
62
      CT = mergeCanThrow(CT, Self.canThrow(Init));
1064
62
1065
  // Destructor might throw.
1066
62
  if (VD->needsDestruction(Self.Context) == QualType::DK_cxx_destructor) {
1067
0
    if (auto *RD =
1068
0
            VD->getType()->getBaseElementTypeUnsafe()->getAsCXXRecordDecl()) {
1069
0
      if (auto *Dtor = RD->getDestructor()) {
1070
0
        CT = mergeCanThrow(
1071
0
            CT, Sema::canCalleeThrow(Self, nullptr, Dtor, VD->getLocation()));
1072
0
      }
1073
0
    }
1074
0
  }
1075
62
1076
  // If this is a decomposition declaration, bindings might throw.
1077
62
  if (auto *DD = dyn_cast<DecompositionDecl>(VD))
1078
0
    for (auto *B : DD->bindings())
1079
0
      if (auto *HD = B->getHoldingVar())
1080
0
        CT = mergeCanThrow(CT, canVarDeclThrow(Self, HD));
1081
62
1082
62
  return CT;
1083
62
}
1084
1085
8
static CanThrowResult canDynamicCastThrow(const CXXDynamicCastExpr *DC) {
1086
8
  if (DC->isTypeDependent())
1087
0
    return CT_Dependent;
1088
8
1089
8
  if (!DC->getTypeAsWritten()->isReferenceType())
1090
4
    return CT_Cannot;
1091
4
1092
4
  if (DC->getSubExpr()->isTypeDependent())
1093
1
    return CT_Dependent;
1094
3
1095
3
  return DC->getCastKind() == clang::CK_Dynamic? 
CT_Can1
:
CT_Cannot2
;
1096
3
}
1097
1098
11
static CanThrowResult canTypeidThrow(Sema &S, const CXXTypeidExpr *DC) {
1099
11
  if (DC->isTypeOperand())
1100
1
    return CT_Cannot;
1101
10
1102
10
  Expr *Op = DC->getExprOperand();
1103
10
  if (Op->isTypeDependent())
1104
1
    return CT_Dependent;
1105
9
1106
9
  const RecordType *RT = Op->getType()->getAs<RecordType>();
1107
9
  if (!RT)
1108
2
    return CT_Cannot;
1109
7
1110
7
  if (!cast<CXXRecordDecl>(RT->getDecl())->isPolymorphic())
1111
3
    return CT_Cannot;
1112
4
1113
4
  if (Op->Classify(S.Context).isPRValue())
1114
0
    return CT_Cannot;
1115
4
1116
4
  return CT_Can;
1117
4
}
1118
1119
87.7k
CanThrowResult Sema::canThrow(const Stmt *S) {
1120
  // C++ [expr.unary.noexcept]p3:
1121
  //   [Can throw] if in a potentially-evaluated context the expression would
1122
  //   contain:
1123
87.7k
  switch (S->getStmtClass()) {
1124
3
  case Expr::ConstantExprClass:
1125
3
    return canThrow(cast<ConstantExpr>(S)->getSubExpr());
1126
0
1127
12
  case Expr::CXXThrowExprClass:
1128
    //   - a potentially evaluated throw-expression
1129
12
    return CT_Can;
1130
0
1131
8
  case Expr::CXXDynamicCastExprClass: {
1132
    //   - a potentially evaluated dynamic_cast expression dynamic_cast<T>(v),
1133
    //     where T is a reference type, that requires a run-time check
1134
8
    auto *CE = cast<CXXDynamicCastExpr>(S);
1135
    // FIXME: Properly determine whether a variably-modified type can throw.
1136
8
    if (CE->getType()->isVariablyModifiedType())
1137
0
      return CT_Can;
1138
8
    CanThrowResult CT = canDynamicCastThrow(CE);
1139
8
    if (CT == CT_Can)
1140
1
      return CT;
1141
7
    return mergeCanThrow(CT, canSubStmtsThrow(*this, CE));
1142
7
  }
1143
7
1144
11
  case Expr::CXXTypeidExprClass:
1145
    //   - a potentially evaluated typeid expression applied to a glvalue
1146
    //     expression whose type is a polymorphic class type
1147
11
    return canTypeidThrow(*this, cast<CXXTypeidExpr>(S));
1148
7
1149
    //   - a potentially evaluated call to a function, member function, function
1150
    //     pointer, or member function pointer that does not have a non-throwing
1151
    //     exception-specification
1152
20.8k
  case Expr::CallExprClass:
1153
20.8k
  case Expr::CXXMemberCallExprClass:
1154
20.8k
  case Expr::CXXOperatorCallExprClass:
1155
20.8k
  case Expr::UserDefinedLiteralClass: {
1156
20.8k
    const CallExpr *CE = cast<CallExpr>(S);
1157
20.8k
    CanThrowResult CT;
1158
20.8k
    if (CE->isTypeDependent())
1159
19.4k
      CT = CT_Dependent;
1160
1.40k
    else if (isa<CXXPseudoDestructorExpr>(CE->getCallee()->IgnoreParens()))
1161
1
      CT = CT_Cannot;
1162
1.40k
    else
1163
1.40k
      CT = canCalleeThrow(*this, CE, CE->getCalleeDecl());
1164
20.8k
    if (CT == CT_Can)
1165
692
      return CT;
1166
20.1k
    return mergeCanThrow(CT, canSubStmtsThrow(*this, CE));
1167
20.1k
  }
1168
20.1k
1169
3.85k
  case Expr::CXXConstructExprClass:
1170
3.85k
  case Expr::CXXTemporaryObjectExprClass: {
1171
3.85k
    auto *CE = cast<CXXConstructExpr>(S);
1172
    // FIXME: Properly determine whether a variably-modified type can throw.
1173
3.85k
    if (CE->getType()->isVariablyModifiedType())
1174
0
      return CT_Can;
1175
3.85k
    CanThrowResult CT = canCalleeThrow(*this, CE, CE->getConstructor());
1176
3.85k
    if (CT == CT_Can)
1177
166
      return CT;
1178
3.68k
    return mergeCanThrow(CT, canSubStmtsThrow(*this, CE));
1179
3.68k
  }
1180
3.68k
1181
0
  case Expr::CXXInheritedCtorInitExprClass: {
1182
0
    auto *ICIE = cast<CXXInheritedCtorInitExpr>(S);
1183
0
    return canCalleeThrow(*this, ICIE, ICIE->getConstructor());
1184
3.68k
  }
1185
3.68k
1186
6
  case Expr::LambdaExprClass: {
1187
6
    const LambdaExpr *Lambda = cast<LambdaExpr>(S);
1188
6
    CanThrowResult CT = CT_Cannot;
1189
6
    for (LambdaExpr::const_capture_init_iterator
1190
6
             Cap = Lambda->capture_init_begin(),
1191
6
             CapEnd = Lambda->capture_init_end();
1192
6
         Cap != CapEnd; 
++Cap0
)
1193
0
      CT = mergeCanThrow(CT, canThrow(*Cap));
1194
6
    return CT;
1195
3.68k
  }
1196
3.68k
1197
27
  case Expr::CXXNewExprClass: {
1198
27
    auto *NE = cast<CXXNewExpr>(S);
1199
27
    CanThrowResult CT;
1200
27
    if (NE->isTypeDependent())
1201
4
      CT = CT_Dependent;
1202
23
    else
1203
23
      CT = canCalleeThrow(*this, NE, NE->getOperatorNew());
1204
27
    if (CT == CT_Can)
1205
2
      return CT;
1206
25
    return mergeCanThrow(CT, canSubStmtsThrow(*this, NE));
1207
25
  }
1208
25
1209
16
  case Expr::CXXDeleteExprClass: {
1210
16
    auto *DE = cast<CXXDeleteExpr>(S);
1211
16
    CanThrowResult CT;
1212
16
    QualType DTy = DE->getDestroyedType();
1213
16
    if (DTy.isNull() || 
DTy->isDependentType()15
) {
1214
2
      CT = CT_Dependent;
1215
14
    } else {
1216
14
      CT = canCalleeThrow(*this, DE, DE->getOperatorDelete());
1217
14
      if (const RecordType *RT = DTy->getAs<RecordType>()) {
1218
12
        const CXXRecordDecl *RD = cast<CXXRecordDecl>(RT->getDecl());
1219
12
        const CXXDestructorDecl *DD = RD->getDestructor();
1220
12
        if (DD)
1221
11
          CT = mergeCanThrow(CT, canCalleeThrow(*this, DE, DD));
1222
12
      }
1223
14
      if (CT == CT_Can)
1224
6
        return CT;
1225
10
    }
1226
10
    return mergeCanThrow(CT, canSubStmtsThrow(*this, DE));
1227
10
  }
1228
10
1229
767
  case Expr::CXXBindTemporaryExprClass: {
1230
767
    auto *BTE = cast<CXXBindTemporaryExpr>(S);
1231
    // The bound temporary has to be destroyed again, which might throw.
1232
767
    CanThrowResult CT =
1233
767
        canCalleeThrow(*this, BTE, BTE->getTemporary()->getDestructor());
1234
767
    if (CT == CT_Can)
1235
11
      return CT;
1236
756
    return mergeCanThrow(CT, canSubStmtsThrow(*this, BTE));
1237
756
  }
1238
756
1239
15
  case Expr::PseudoObjectExprClass: {
1240
15
    auto *POE = cast<PseudoObjectExpr>(S);
1241
15
    CanThrowResult CT = CT_Cannot;
1242
45
    for (const Expr *E : POE->semantics()) {
1243
45
      CT = mergeCanThrow(CT, canThrow(E));
1244
45
      if (CT == CT_Can)
1245
13
        break;
1246
45
    }
1247
15
    return CT;
1248
756
  }
1249
756
1250
    // ObjC message sends are like function calls, but never have exception
1251
    // specs.
1252
1
  case Expr::ObjCMessageExprClass:
1253
1
  case Expr::ObjCPropertyRefExprClass:
1254
1
  case Expr::ObjCSubscriptRefExprClass:
1255
1
    return CT_Can;
1256
1
1257
    // All the ObjC literals that are implemented as calls are
1258
    // potentially throwing unless we decide to close off that
1259
    // possibility.
1260
0
  case Expr::ObjCArrayLiteralClass:
1261
0
  case Expr::ObjCDictionaryLiteralClass:
1262
0
  case Expr::ObjCBoxedExprClass:
1263
0
    return CT_Can;
1264
0
1265
    // Many other things have subexpressions, so we have to test those.
1266
    // Some are simple:
1267
5.57k
  case Expr::CoawaitExprClass:
1268
5.57k
  case Expr::ConditionalOperatorClass:
1269
5.57k
  case Expr::CoyieldExprClass:
1270
5.57k
  case Expr::CXXRewrittenBinaryOperatorClass:
1271
5.57k
  case Expr::CXXStdInitializerListExprClass:
1272
5.57k
  case Expr::DesignatedInitExprClass:
1273
5.57k
  case Expr::DesignatedInitUpdateExprClass:
1274
5.57k
  case Expr::ExprWithCleanupsClass:
1275
5.57k
  case Expr::ExtVectorElementExprClass:
1276
5.57k
  case Expr::InitListExprClass:
1277
5.57k
  case Expr::ArrayInitLoopExprClass:
1278
5.57k
  case Expr::MemberExprClass:
1279
5.57k
  case Expr::ObjCIsaExprClass:
1280
5.57k
  case Expr::ObjCIvarRefExprClass:
1281
5.57k
  case Expr::ParenExprClass:
1282
5.57k
  case Expr::ParenListExprClass:
1283
5.57k
  case Expr::ShuffleVectorExprClass:
1284
5.57k
  case Expr::StmtExprClass:
1285
5.57k
  case Expr::ConvertVectorExprClass:
1286
5.57k
  case Expr::VAArgExprClass:
1287
5.57k
    return canSubStmtsThrow(*this, S);
1288
5.57k
1289
3
  case Expr::CompoundLiteralExprClass:
1290
3
  case Expr::CXXConstCastExprClass:
1291
3
  case Expr::CXXAddrspaceCastExprClass:
1292
3
  case Expr::CXXReinterpretCastExprClass:
1293
3
  case Expr::BuiltinBitCastExprClass:
1294
      // FIXME: Properly determine whether a variably-modified type can throw.
1295
3
    if (cast<Expr>(S)->getType()->isVariablyModifiedType())
1296
2
      return CT_Can;
1297
1
    return canSubStmtsThrow(*this, S);
1298
1
1299
    // Some might be dependent for other reasons.
1300
14.5k
  case Expr::ArraySubscriptExprClass:
1301
14.5k
  case Expr::MatrixSubscriptExprClass:
1302
14.5k
  case Expr::OMPArraySectionExprClass:
1303
14.5k
  case Expr::OMPArrayShapingExprClass:
1304
14.5k
  case Expr::OMPIteratorExprClass:
1305
14.5k
  case Expr::BinaryOperatorClass:
1306
14.5k
  case Expr::DependentCoawaitExprClass:
1307
14.5k
  case Expr::CompoundAssignOperatorClass:
1308
14.5k
  case Expr::CStyleCastExprClass:
1309
14.5k
  case Expr::CXXStaticCastExprClass:
1310
14.5k
  case Expr::CXXFunctionalCastExprClass:
1311
14.5k
  case Expr::ImplicitCastExprClass:
1312
14.5k
  case Expr::MaterializeTemporaryExprClass:
1313
14.5k
  case Expr::UnaryOperatorClass: {
1314
    // FIXME: Properly determine whether a variably-modified type can throw.
1315
14.5k
    if (auto *CE = dyn_cast<CastExpr>(S))
1316
4.59k
      if (CE->getType()->isVariablyModifiedType())
1317
3
        return CT_Can;
1318
14.5k
    CanThrowResult CT =
1319
8.64k
        cast<Expr>(S)->isTypeDependent() ? CT_Dependent : 
CT_Cannot5.88k
;
1320
14.5k
    return mergeCanThrow(CT, canSubStmtsThrow(*this, S));
1321
14.5k
  }
1322
14.5k
1323
23
  case Expr::CXXDefaultArgExprClass:
1324
23
    return canThrow(cast<CXXDefaultArgExpr>(S)->getExpr());
1325
14.5k
1326
126
  case Expr::CXXDefaultInitExprClass:
1327
126
    return canThrow(cast<CXXDefaultInitExpr>(S)->getExpr());
1328
14.5k
1329
0
  case Expr::ChooseExprClass: {
1330
0
    auto *CE = cast<ChooseExpr>(S);
1331
0
    if (CE->isTypeDependent() || CE->isValueDependent())
1332
0
      return CT_Dependent;
1333
0
    return canThrow(CE->getChosenSubExpr());
1334
0
  }
1335
0
1336
0
  case Expr::GenericSelectionExprClass:
1337
0
    if (cast<GenericSelectionExpr>(S)->isResultDependent())
1338
0
      return CT_Dependent;
1339
0
    return canThrow(cast<GenericSelectionExpr>(S)->getResultExpr());
1340
0
1341
    // Some expressions are always dependent.
1342
2.91k
  case Expr::CXXDependentScopeMemberExprClass:
1343
2.91k
  case Expr::CXXUnresolvedConstructExprClass:
1344
2.91k
  case Expr::DependentScopeDeclRefExprClass:
1345
2.91k
  case Expr::CXXFoldExprClass:
1346
2.91k
  case Expr::RecoveryExprClass:
1347
2.91k
    return CT_Dependent;
1348
2.91k
1349
29.5k
  case Expr::AsTypeExprClass:
1350
29.5k
  case Expr::BinaryConditionalOperatorClass:
1351
29.5k
  case Expr::BlockExprClass:
1352
29.5k
  case Expr::CUDAKernelCallExprClass:
1353
29.5k
  case Expr::DeclRefExprClass:
1354
29.5k
  case Expr::ObjCBridgedCastExprClass:
1355
29.5k
  case Expr::ObjCIndirectCopyRestoreExprClass:
1356
29.5k
  case Expr::ObjCProtocolExprClass:
1357
29.5k
  case Expr::ObjCSelectorExprClass:
1358
29.5k
  case Expr::ObjCAvailabilityCheckExprClass:
1359
29.5k
  case Expr::OffsetOfExprClass:
1360
29.5k
  case Expr::PackExpansionExprClass:
1361
29.5k
  case Expr::SubstNonTypeTemplateParmExprClass:
1362
29.5k
  case Expr::SubstNonTypeTemplateParmPackExprClass:
1363
29.5k
  case Expr::FunctionParmPackExprClass:
1364
29.5k
  case Expr::UnaryExprOrTypeTraitExprClass:
1365
29.5k
  case Expr::UnresolvedLookupExprClass:
1366
29.5k
  case Expr::UnresolvedMemberExprClass:
1367
29.5k
  case Expr::TypoExprClass:
1368
    // FIXME: Many of the above can throw.
1369
29.5k
    return CT_Cannot;
1370
29.5k
1371
8.97k
  case Expr::AddrLabelExprClass:
1372
8.97k
  case Expr::ArrayTypeTraitExprClass:
1373
8.97k
  case Expr::AtomicExprClass:
1374
8.97k
  case Expr::TypeTraitExprClass:
1375
8.97k
  case Expr::CXXBoolLiteralExprClass:
1376
8.97k
  case Expr::CXXNoexceptExprClass:
1377
8.97k
  case Expr::CXXNullPtrLiteralExprClass:
1378
8.97k
  case Expr::CXXPseudoDestructorExprClass:
1379
8.97k
  case Expr::CXXScalarValueInitExprClass:
1380
8.97k
  case Expr::CXXThisExprClass:
1381
8.97k
  case Expr::CXXUuidofExprClass:
1382
8.97k
  case Expr::CharacterLiteralClass:
1383
8.97k
  case Expr::ExpressionTraitExprClass:
1384
8.97k
  case Expr::FloatingLiteralClass:
1385
8.97k
  case Expr::GNUNullExprClass:
1386
8.97k
  case Expr::ImaginaryLiteralClass:
1387
8.97k
  case Expr::ImplicitValueInitExprClass:
1388
8.97k
  case Expr::IntegerLiteralClass:
1389
8.97k
  case Expr::FixedPointLiteralClass:
1390
8.97k
  case Expr::ArrayInitIndexExprClass:
1391
8.97k
  case Expr::NoInitExprClass:
1392
8.97k
  case Expr::ObjCEncodeExprClass:
1393
8.97k
  case Expr::ObjCStringLiteralClass:
1394
8.97k
  case Expr::ObjCBoolLiteralExprClass:
1395
8.97k
  case Expr::OpaqueValueExprClass:
1396
8.97k
  case Expr::PredefinedExprClass:
1397
8.97k
  case Expr::SizeOfPackExprClass:
1398
8.97k
  case Expr::StringLiteralClass:
1399
8.97k
  case Expr::SourceLocExprClass:
1400
8.97k
  case Expr::ConceptSpecializationExprClass:
1401
8.97k
  case Expr::RequiresExprClass:
1402
    // These expressions can never throw.
1403
8.97k
    return CT_Cannot;
1404
8.97k
1405
0
  case Expr::MSPropertyRefExprClass:
1406
0
  case Expr::MSPropertySubscriptExprClass:
1407
0
    llvm_unreachable("Invalid class for expression");
1408
0
1409
    // Most statements can throw if any substatement can throw.
1410
357
  case Stmt::AttributedStmtClass:
1411
357
  case Stmt::BreakStmtClass:
1412
357
  case Stmt::CapturedStmtClass:
1413
357
  case Stmt::CaseStmtClass:
1414
357
  case Stmt::CompoundStmtClass:
1415
357
  case Stmt::ContinueStmtClass:
1416
357
  case Stmt::CoreturnStmtClass:
1417
357
  case Stmt::CoroutineBodyStmtClass:
1418
357
  case Stmt::CXXCatchStmtClass:
1419
357
  case Stmt::CXXForRangeStmtClass:
1420
357
  case Stmt::DefaultStmtClass:
1421
357
  case Stmt::DoStmtClass:
1422
357
  case Stmt::ForStmtClass:
1423
357
  case Stmt::GCCAsmStmtClass:
1424
357
  case Stmt::GotoStmtClass:
1425
357
  case Stmt::IndirectGotoStmtClass:
1426
357
  case Stmt::LabelStmtClass:
1427
357
  case Stmt::MSAsmStmtClass:
1428
357
  case Stmt::MSDependentExistsStmtClass:
1429
357
  case Stmt::NullStmtClass:
1430
357
  case Stmt::ObjCAtCatchStmtClass:
1431
357
  case Stmt::ObjCAtFinallyStmtClass:
1432
357
  case Stmt::ObjCAtSynchronizedStmtClass:
1433
357
  case Stmt::ObjCAutoreleasePoolStmtClass:
1434
357
  case Stmt::ObjCForCollectionStmtClass:
1435
357
  case Stmt::OMPAtomicDirectiveClass:
1436
357
  case Stmt::OMPBarrierDirectiveClass:
1437
357
  case Stmt::OMPCancelDirectiveClass:
1438
357
  case Stmt::OMPCancellationPointDirectiveClass:
1439
357
  case Stmt::OMPCriticalDirectiveClass:
1440
357
  case Stmt::OMPDistributeDirectiveClass:
1441
357
  case Stmt::OMPDistributeParallelForDirectiveClass:
1442
357
  case Stmt::OMPDistributeParallelForSimdDirectiveClass:
1443
357
  case Stmt::OMPDistributeSimdDirectiveClass:
1444
357
  case Stmt::OMPFlushDirectiveClass:
1445
357
  case Stmt::OMPDepobjDirectiveClass:
1446
357
  case Stmt::OMPScanDirectiveClass:
1447
357
  case Stmt::OMPForDirectiveClass:
1448
357
  case Stmt::OMPForSimdDirectiveClass:
1449
357
  case Stmt::OMPMasterDirectiveClass:
1450
357
  case Stmt::OMPMasterTaskLoopDirectiveClass:
1451
357
  case Stmt::OMPMasterTaskLoopSimdDirectiveClass:
1452
357
  case Stmt::OMPOrderedDirectiveClass:
1453
357
  case Stmt::OMPParallelDirectiveClass:
1454
357
  case Stmt::OMPParallelForDirectiveClass:
1455
357
  case Stmt::OMPParallelForSimdDirectiveClass:
1456
357
  case Stmt::OMPParallelMasterDirectiveClass:
1457
357
  case Stmt::OMPParallelMasterTaskLoopDirectiveClass:
1458
357
  case Stmt::OMPParallelMasterTaskLoopSimdDirectiveClass:
1459
357
  case Stmt::OMPParallelSectionsDirectiveClass:
1460
357
  case Stmt::OMPSectionDirectiveClass:
1461
357
  case Stmt::OMPSectionsDirectiveClass:
1462
357
  case Stmt::OMPSimdDirectiveClass:
1463
357
  case Stmt::OMPSingleDirectiveClass:
1464
357
  case Stmt::OMPTargetDataDirectiveClass:
1465
357
  case Stmt::OMPTargetDirectiveClass:
1466
357
  case Stmt::OMPTargetEnterDataDirectiveClass:
1467
357
  case Stmt::OMPTargetExitDataDirectiveClass:
1468
357
  case Stmt::OMPTargetParallelDirectiveClass:
1469
357
  case Stmt::OMPTargetParallelForDirectiveClass:
1470
357
  case Stmt::OMPTargetParallelForSimdDirectiveClass:
1471
357
  case Stmt::OMPTargetSimdDirectiveClass:
1472
357
  case Stmt::OMPTargetTeamsDirectiveClass:
1473
357
  case Stmt::OMPTargetTeamsDistributeDirectiveClass:
1474
357
  case Stmt::OMPTargetTeamsDistributeParallelForDirectiveClass:
1475
357
  case Stmt::OMPTargetTeamsDistributeParallelForSimdDirectiveClass:
1476
357
  case Stmt::OMPTargetTeamsDistributeSimdDirectiveClass:
1477
357
  case Stmt::OMPTargetUpdateDirectiveClass:
1478
357
  case Stmt::OMPTaskDirectiveClass:
1479
357
  case Stmt::OMPTaskgroupDirectiveClass:
1480
357
  case Stmt::OMPTaskLoopDirectiveClass:
1481
357
  case Stmt::OMPTaskLoopSimdDirectiveClass:
1482
357
  case Stmt::OMPTaskwaitDirectiveClass:
1483
357
  case Stmt::OMPTaskyieldDirectiveClass:
1484
357
  case Stmt::OMPTeamsDirectiveClass:
1485
357
  case Stmt::OMPTeamsDistributeDirectiveClass:
1486
357
  case Stmt::OMPTeamsDistributeParallelForDirectiveClass:
1487
357
  case Stmt::OMPTeamsDistributeParallelForSimdDirectiveClass:
1488
357
  case Stmt::OMPTeamsDistributeSimdDirectiveClass:
1489
357
  case Stmt::ReturnStmtClass:
1490
357
  case Stmt::SEHExceptStmtClass:
1491
357
  case Stmt::SEHFinallyStmtClass:
1492
357
  case Stmt::SEHLeaveStmtClass:
1493
357
  case Stmt::SEHTryStmtClass:
1494
357
  case Stmt::SwitchStmtClass:
1495
357
  case Stmt::WhileStmtClass:
1496
357
    return canSubStmtsThrow(*this, S);
1497
357
1498
62
  case Stmt::DeclStmtClass: {
1499
62
    CanThrowResult CT = CT_Cannot;
1500
62
    for (const Decl *D : cast<DeclStmt>(S)->decls()) {
1501
62
      if (auto *VD = dyn_cast<VarDecl>(D))
1502
62
        CT = mergeCanThrow(CT, canVarDeclThrow(*this, VD));
1503
62
1504
      // FIXME: Properly determine whether a variably-modified type can throw.
1505
62
      if (auto *TND = dyn_cast<TypedefNameDecl>(D))
1506
0
        if (TND->getUnderlyingType()->isVariablyModifiedType())
1507
0
          return CT_Can;
1508
62
      if (auto *VD = dyn_cast<ValueDecl>(D))
1509
62
        if (VD->getType()->isVariablyModifiedType())
1510
0
          return CT_Can;
1511
62
    }
1512
62
    return CT;
1513
62
  }
1514
62
1515
68
  case Stmt::IfStmtClass: {
1516
68
    auto *IS = cast<IfStmt>(S);
1517
68
    CanThrowResult CT = CT_Cannot;
1518
68
    if (const Stmt *Init = IS->getInit())
1519
57
      CT = mergeCanThrow(CT, canThrow(Init));
1520
68
    if (const Stmt *CondDS = IS->getConditionVariableDeclStmt())
1521
0
      CT = mergeCanThrow(CT, canThrow(CondDS));
1522
68
    CT = mergeCanThrow(CT, canThrow(IS->getCond()));
1523
68
1524
    // For 'if constexpr', consider only the non-discarded case.
1525
    // FIXME: We should add a DiscardedStmt marker to the AST.
1526
68
    if (Optional<const Stmt *> Case = IS->getNondiscardedCase(Context))
1527
3
      return *Case ? 
mergeCanThrow(CT, canThrow(*Case))2
:
CT1
;
1528
65
1529
65
    CanThrowResult Then = canThrow(IS->getThen());
1530
65
    CanThrowResult Else = IS->getElse() ? 
canThrow(IS->getElse())0
: CT_Cannot;
1531
65
    if (Then == Else)
1532
64
      return mergeCanThrow(CT, Then);
1533
1
1534
    // For a dependent 'if constexpr', the result is dependent if it depends on
1535
    // the value of the condition.
1536
1
    return mergeCanThrow(CT, IS->isConstexpr() ? 
CT_Dependent0
1537
1
                                               : mergeCanThrow(Then, Else));
1538
1
  }
1539
1
1540
3
  case Stmt::CXXTryStmtClass: {
1541
3
    auto *TS = cast<CXXTryStmt>(S);
1542
    // try /*...*/ catch (...) { H } can throw only if H can throw.
1543
    // Any other try-catch can throw if any substatement can throw.
1544
3
    const CXXCatchStmt *FinalHandler = TS->getHandler(TS->getNumHandlers() - 1);
1545
3
    if (!FinalHandler->getExceptionDecl())
1546
2
      return canThrow(FinalHandler->getHandlerBlock());
1547
1
    return canSubStmtsThrow(*this, S);
1548
1
  }
1549
1
1550
0
  case Stmt::ObjCAtThrowStmtClass:
1551
0
    return CT_Can;
1552
1
1553
0
  case Stmt::ObjCAtTryStmtClass: {
1554
0
    auto *TS = cast<ObjCAtTryStmt>(S);
1555
0
1556
    // @catch(...) need not be last in Objective-C. Walk backwards until we
1557
    // see one or hit the @try.
1558
0
    CanThrowResult CT = CT_Cannot;
1559
0
    if (const Stmt *Finally = TS->getFinallyStmt())
1560
0
      CT = mergeCanThrow(CT, canThrow(Finally));
1561
0
    for (unsigned I = TS->getNumCatchStmts(); I != 0; --I) {
1562
0
      const ObjCAtCatchStmt *Catch = TS->getCatchStmt(I - 1);
1563
0
      CT = mergeCanThrow(CT, canThrow(Catch));
1564
      // If we reach a @catch(...), no earlier exceptions can escape.
1565
0
      if (Catch->hasEllipsis())
1566
0
        return CT;
1567
0
    }
1568
0
1569
    // Didn't find an @catch(...). Exceptions from the @try body can escape.
1570
0
    return mergeCanThrow(CT, canThrow(TS->getTryBody()));
1571
0
  }
1572
0
1573
0
  case Stmt::NoStmtClass:
1574
0
    llvm_unreachable("Invalid class for statement");
1575
0
  }
1576
0
  llvm_unreachable("Bogus StmtClass");
1577
0
}
1578
1579
} // end namespace clang