]> CyberLeo.Net >> Repos - FreeBSD/FreeBSD.git/blob - contrib/llvm/tools/clang/lib/Sema/SemaExceptionSpec.cpp
Merge from head
[FreeBSD/FreeBSD.git] / contrib / llvm / tools / clang / lib / Sema / SemaExceptionSpec.cpp
1 //===--- SemaExceptionSpec.cpp - C++ Exception Specifications ---*- C++ -*-===//
2 //
3 //                     The LLVM Compiler Infrastructure
4 //
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
7 //
8 //===----------------------------------------------------------------------===//
9 //
10 // This file provides Sema routines for C++ exception specification testing.
11 //
12 //===----------------------------------------------------------------------===//
13
14 #include "clang/Sema/SemaInternal.h"
15 #include "clang/AST/ASTMutationListener.h"
16 #include "clang/AST/CXXInheritance.h"
17 #include "clang/AST/Expr.h"
18 #include "clang/AST/ExprCXX.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 {
29   if (const PointerType *PtrTy = T->getAs<PointerType>())
30     T = PtrTy->getPointeeType();
31   else if (const ReferenceType *RefTy = T->getAs<ReferenceType>())
32     T = RefTy->getPointeeType();
33   else if (const MemberPointerType *MPTy = T->getAs<MemberPointerType>())
34     T = MPTy->getPointeeType();
35   return T->getAs<FunctionProtoType>();
36 }
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 bool Sema::isLibstdcxxEagerExceptionSpecHack(const Declarator &D) {
43   auto *RD = dyn_cast<CXXRecordDecl>(CurContext);
44
45   // All the problem cases are member functions named "swap" within class
46   // templates declared directly within namespace std.
47   if (!RD || RD->getEnclosingNamespaceContext() != getStdNamespace() ||
48       !RD->getIdentifier() || !RD->getDescribedClassTemplate() ||
49       !D.getIdentifier() || !D.getIdentifier()->isStr("swap"))
50     return false;
51
52   // Only apply this hack within a system header.
53   if (!Context.getSourceManager().isInSystemHeader(D.getLocStart()))
54     return false;
55
56   return llvm::StringSwitch<bool>(RD->getIdentifier()->getName())
57       .Case("array", true)
58       .Case("pair", true)
59       .Case("priority_queue", true)
60       .Case("stack", true)
61       .Case("queue", true)
62       .Default(false);
63 }
64
65 /// CheckSpecifiedExceptionType - Check if the given type is valid in an
66 /// exception specification. Incomplete types, or pointers to incomplete types
67 /// other than void are not allowed.
68 ///
69 /// \param[in,out] T  The exception type. This will be decayed to a pointer type
70 ///                   when the input is an array or a function type.
71 bool Sema::CheckSpecifiedExceptionType(QualType &T, const SourceRange &Range) {
72   // C++11 [except.spec]p2:
73   //   A type cv T, "array of T", or "function returning T" denoted
74   //   in an exception-specification is adjusted to type T, "pointer to T", or
75   //   "pointer to function returning T", respectively.
76   //
77   // We also apply this rule in C++98.
78   if (T->isArrayType())
79     T = Context.getArrayDecayedType(T);
80   else if (T->isFunctionType())
81     T = Context.getPointerType(T);
82
83   int Kind = 0;
84   QualType PointeeT = T;
85   if (const PointerType *PT = T->getAs<PointerType>()) {
86     PointeeT = PT->getPointeeType();
87     Kind = 1;
88
89     // cv void* is explicitly permitted, despite being a pointer to an
90     // incomplete type.
91     if (PointeeT->isVoidType())
92       return false;
93   } else if (const ReferenceType *RT = T->getAs<ReferenceType>()) {
94     PointeeT = RT->getPointeeType();
95     Kind = 2;
96
97     if (RT->isRValueReferenceType()) {
98       // C++11 [except.spec]p2:
99       //   A type denoted in an exception-specification shall not denote [...]
100       //   an rvalue reference type.
101       Diag(Range.getBegin(), diag::err_rref_in_exception_spec)
102         << T << Range;
103       return true;
104     }
105   }
106
107   // C++11 [except.spec]p2:
108   //   A type denoted in an exception-specification shall not denote an
109   //   incomplete type other than a class currently being defined [...].
110   //   A type denoted in an exception-specification shall not denote a
111   //   pointer or reference to an incomplete type, other than (cv) void* or a
112   //   pointer or reference to a class currently being defined.
113   if (!(PointeeT->isRecordType() &&
114         PointeeT->getAs<RecordType>()->isBeingDefined()) &&
115       RequireCompleteType(Range.getBegin(), PointeeT,
116                           diag::err_incomplete_in_exception_spec, Kind, Range))
117     return true;
118
119   return false;
120 }
121
122 /// CheckDistantExceptionSpec - Check if the given type is a pointer or pointer
123 /// to member to a function with an exception specification. This means that
124 /// it is invalid to add another level of indirection.
125 bool Sema::CheckDistantExceptionSpec(QualType T) {
126   if (const PointerType *PT = T->getAs<PointerType>())
127     T = PT->getPointeeType();
128   else if (const MemberPointerType *PT = T->getAs<MemberPointerType>())
129     T = PT->getPointeeType();
130   else
131     return false;
132
133   const FunctionProtoType *FnT = T->getAs<FunctionProtoType>();
134   if (!FnT)
135     return false;
136
137   return FnT->hasExceptionSpec();
138 }
139
140 const FunctionProtoType *
141 Sema::ResolveExceptionSpec(SourceLocation Loc, const FunctionProtoType *FPT) {
142   if (FPT->getExceptionSpecType() == EST_Unparsed) {
143     Diag(Loc, diag::err_exception_spec_not_parsed);
144     return nullptr;
145   }
146
147   if (!isUnresolvedExceptionSpec(FPT->getExceptionSpecType()))
148     return FPT;
149
150   FunctionDecl *SourceDecl = FPT->getExceptionSpecDecl();
151   const FunctionProtoType *SourceFPT =
152       SourceDecl->getType()->castAs<FunctionProtoType>();
153
154   // If the exception specification has already been resolved, just return it.
155   if (!isUnresolvedExceptionSpec(SourceFPT->getExceptionSpecType()))
156     return SourceFPT;
157
158   // Compute or instantiate the exception specification now.
159   if (SourceFPT->getExceptionSpecType() == EST_Unevaluated)
160     EvaluateImplicitExceptionSpec(Loc, cast<CXXMethodDecl>(SourceDecl));
161   else
162     InstantiateExceptionSpec(Loc, SourceDecl);
163
164   const FunctionProtoType *Proto =
165     SourceDecl->getType()->castAs<FunctionProtoType>();
166   if (Proto->getExceptionSpecType() == clang::EST_Unparsed) {
167     Diag(Loc, diag::err_exception_spec_not_parsed);
168     Proto = nullptr;
169   }
170   return Proto;
171 }
172
173 void
174 Sema::UpdateExceptionSpec(FunctionDecl *FD,
175                           const FunctionProtoType::ExceptionSpecInfo &ESI) {
176   // If we've fully resolved the exception specification, notify listeners.
177   if (!isUnresolvedExceptionSpec(ESI.Type))
178     if (auto *Listener = getASTMutationListener())
179       Listener->ResolvedExceptionSpec(FD);
180
181   for (auto *Redecl : FD->redecls())
182     Context.adjustExceptionSpec(cast<FunctionDecl>(Redecl), ESI);
183 }
184
185 /// Determine whether a function has an implicitly-generated exception
186 /// specification.
187 static bool hasImplicitExceptionSpec(FunctionDecl *Decl) {
188   if (!isa<CXXDestructorDecl>(Decl) &&
189       Decl->getDeclName().getCXXOverloadedOperator() != OO_Delete &&
190       Decl->getDeclName().getCXXOverloadedOperator() != OO_Array_Delete)
191     return false;
192
193   // For a function that the user didn't declare:
194   //  - if this is a destructor, its exception specification is implicit.
195   //  - if this is 'operator delete' or 'operator delete[]', the exception
196   //    specification is as-if an explicit exception specification was given
197   //    (per [basic.stc.dynamic]p2).
198   if (!Decl->getTypeSourceInfo())
199     return isa<CXXDestructorDecl>(Decl);
200
201   const FunctionProtoType *Ty =
202     Decl->getTypeSourceInfo()->getType()->getAs<FunctionProtoType>();
203   return !Ty->hasExceptionSpec();
204 }
205
206 bool Sema::CheckEquivalentExceptionSpec(FunctionDecl *Old, FunctionDecl *New) {
207   OverloadedOperatorKind OO = New->getDeclName().getCXXOverloadedOperator();
208   bool IsOperatorNew = OO == OO_New || OO == OO_Array_New;
209   bool MissingExceptionSpecification = false;
210   bool MissingEmptyExceptionSpecification = false;
211
212   unsigned DiagID = diag::err_mismatched_exception_spec;
213   bool ReturnValueOnError = true;
214   if (getLangOpts().MicrosoftExt) {
215     DiagID = diag::ext_mismatched_exception_spec;
216     ReturnValueOnError = false;
217   }
218
219   // Check the types as written: they must match before any exception
220   // specification adjustment is applied.
221   if (!CheckEquivalentExceptionSpec(
222         PDiag(DiagID), PDiag(diag::note_previous_declaration),
223         Old->getType()->getAs<FunctionProtoType>(), Old->getLocation(),
224         New->getType()->getAs<FunctionProtoType>(), New->getLocation(),
225         &MissingExceptionSpecification, &MissingEmptyExceptionSpecification,
226         /*AllowNoexceptAllMatchWithNoSpec=*/true, IsOperatorNew)) {
227     // C++11 [except.spec]p4 [DR1492]:
228     //   If a declaration of a function has an implicit
229     //   exception-specification, other declarations of the function shall
230     //   not specify an exception-specification.
231     if (getLangOpts().CPlusPlus11 &&
232         hasImplicitExceptionSpec(Old) != hasImplicitExceptionSpec(New)) {
233       Diag(New->getLocation(), diag::ext_implicit_exception_spec_mismatch)
234         << hasImplicitExceptionSpec(Old);
235       if (!Old->getLocation().isInvalid())
236         Diag(Old->getLocation(), diag::note_previous_declaration);
237     }
238     return false;
239   }
240
241   // The failure was something other than an missing exception
242   // specification; return an error, except in MS mode where this is a warning.
243   if (!MissingExceptionSpecification)
244     return ReturnValueOnError;
245
246   const FunctionProtoType *NewProto =
247     New->getType()->castAs<FunctionProtoType>();
248
249   // The new function declaration is only missing an empty exception
250   // specification "throw()". If the throw() specification came from a
251   // function in a system header that has C linkage, just add an empty
252   // exception specification to the "new" declaration. This is an
253   // egregious workaround for glibc, which adds throw() specifications
254   // to many libc functions as an optimization. Unfortunately, that
255   // optimization isn't permitted by the C++ standard, so we're forced
256   // to work around it here.
257   if (MissingEmptyExceptionSpecification && NewProto &&
258       (Old->getLocation().isInvalid() ||
259        Context.getSourceManager().isInSystemHeader(Old->getLocation())) &&
260       Old->isExternC()) {
261     New->setType(Context.getFunctionType(
262         NewProto->getReturnType(), NewProto->getParamTypes(),
263         NewProto->getExtProtoInfo().withExceptionSpec(EST_DynamicNone)));
264     return false;
265   }
266
267   const FunctionProtoType *OldProto =
268     Old->getType()->castAs<FunctionProtoType>();
269
270   FunctionProtoType::ExceptionSpecInfo ESI = OldProto->getExceptionSpecType();
271   if (ESI.Type == EST_Dynamic) {
272     ESI.Exceptions = OldProto->exceptions();
273   } else if (ESI.Type == EST_ComputedNoexcept) {
274     // FIXME: We can't just take the expression from the old prototype. It
275     // likely contains references to the old prototype's parameters.
276   }
277
278   // Update the type of the function with the appropriate exception
279   // specification.
280   New->setType(Context.getFunctionType(
281       NewProto->getReturnType(), NewProto->getParamTypes(),
282       NewProto->getExtProtoInfo().withExceptionSpec(ESI)));
283
284   // Warn about the lack of exception specification.
285   SmallString<128> ExceptionSpecString;
286   llvm::raw_svector_ostream OS(ExceptionSpecString);
287   switch (OldProto->getExceptionSpecType()) {
288   case EST_DynamicNone:
289     OS << "throw()";
290     break;
291
292   case EST_Dynamic: {
293     OS << "throw(";
294     bool OnFirstException = true;
295     for (const auto &E : OldProto->exceptions()) {
296       if (OnFirstException)
297         OnFirstException = false;
298       else
299         OS << ", ";
300       
301       OS << E.getAsString(getPrintingPolicy());
302     }
303     OS << ")";
304     break;
305   }
306
307   case EST_BasicNoexcept:
308     OS << "noexcept";
309     break;
310
311   case EST_ComputedNoexcept:
312     OS << "noexcept(";
313     assert(OldProto->getNoexceptExpr() != nullptr && "Expected non-null Expr");
314     OldProto->getNoexceptExpr()->printPretty(OS, nullptr, getPrintingPolicy());
315     OS << ")";
316     break;
317
318   default:
319     llvm_unreachable("This spec type is compatible with none.");
320   }
321   OS.flush();
322
323   SourceLocation FixItLoc;
324   if (TypeSourceInfo *TSInfo = New->getTypeSourceInfo()) {
325     TypeLoc TL = TSInfo->getTypeLoc().IgnoreParens();
326     if (FunctionTypeLoc FTLoc = TL.getAs<FunctionTypeLoc>())
327       FixItLoc = getLocForEndOfToken(FTLoc.getLocalRangeEnd());
328   }
329
330   if (FixItLoc.isInvalid())
331     Diag(New->getLocation(), diag::warn_missing_exception_specification)
332       << New << OS.str();
333   else {
334     // FIXME: This will get more complicated with C++0x
335     // late-specified return types.
336     Diag(New->getLocation(), diag::warn_missing_exception_specification)
337       << New << OS.str()
338       << FixItHint::CreateInsertion(FixItLoc, " " + OS.str().str());
339   }
340
341   if (!Old->getLocation().isInvalid())
342     Diag(Old->getLocation(), diag::note_previous_declaration);
343
344   return false;
345 }
346
347 /// CheckEquivalentExceptionSpec - Check if the two types have equivalent
348 /// exception specifications. Exception specifications are equivalent if
349 /// they allow exactly the same set of exception types. It does not matter how
350 /// that is achieved. See C++ [except.spec]p2.
351 bool Sema::CheckEquivalentExceptionSpec(
352     const FunctionProtoType *Old, SourceLocation OldLoc,
353     const FunctionProtoType *New, SourceLocation NewLoc) {
354   unsigned DiagID = diag::err_mismatched_exception_spec;
355   if (getLangOpts().MicrosoftExt)
356     DiagID = diag::ext_mismatched_exception_spec;
357   bool Result = CheckEquivalentExceptionSpec(PDiag(DiagID),
358       PDiag(diag::note_previous_declaration), Old, OldLoc, New, NewLoc);
359
360   // In Microsoft mode, mismatching exception specifications just cause a warning.
361   if (getLangOpts().MicrosoftExt)
362     return false;
363   return Result;
364 }
365
366 /// CheckEquivalentExceptionSpec - Check if the two types have compatible
367 /// exception specifications. See C++ [except.spec]p3.
368 ///
369 /// \return \c false if the exception specifications match, \c true if there is
370 /// a problem. If \c true is returned, either a diagnostic has already been
371 /// produced or \c *MissingExceptionSpecification is set to \c true.
372 bool Sema::CheckEquivalentExceptionSpec(const PartialDiagnostic &DiagID,
373                                         const PartialDiagnostic & NoteID,
374                                         const FunctionProtoType *Old,
375                                         SourceLocation OldLoc,
376                                         const FunctionProtoType *New,
377                                         SourceLocation NewLoc,
378                                         bool *MissingExceptionSpecification,
379                                         bool*MissingEmptyExceptionSpecification,
380                                         bool AllowNoexceptAllMatchWithNoSpec,
381                                         bool IsOperatorNew) {
382   // Just completely ignore this under -fno-exceptions.
383   if (!getLangOpts().CXXExceptions)
384     return false;
385
386   if (MissingExceptionSpecification)
387     *MissingExceptionSpecification = false;
388
389   if (MissingEmptyExceptionSpecification)
390     *MissingEmptyExceptionSpecification = false;
391
392   Old = ResolveExceptionSpec(NewLoc, Old);
393   if (!Old)
394     return false;
395   New = ResolveExceptionSpec(NewLoc, New);
396   if (!New)
397     return false;
398
399   // C++0x [except.spec]p3: Two exception-specifications are compatible if:
400   //   - both are non-throwing, regardless of their form,
401   //   - both have the form noexcept(constant-expression) and the constant-
402   //     expressions are equivalent,
403   //   - both are dynamic-exception-specifications that have the same set of
404   //     adjusted types.
405   //
406   // C++0x [except.spec]p12: An exception-specification is non-throwing if it is
407   //   of the form throw(), noexcept, or noexcept(constant-expression) where the
408   //   constant-expression yields true.
409   //
410   // C++0x [except.spec]p4: If any declaration of a function has an exception-
411   //   specifier that is not a noexcept-specification allowing all exceptions,
412   //   all declarations [...] of that function shall have a compatible
413   //   exception-specification.
414   //
415   // That last point basically means that noexcept(false) matches no spec.
416   // It's considered when AllowNoexceptAllMatchWithNoSpec is true.
417
418   ExceptionSpecificationType OldEST = Old->getExceptionSpecType();
419   ExceptionSpecificationType NewEST = New->getExceptionSpecType();
420
421   assert(!isUnresolvedExceptionSpec(OldEST) &&
422          !isUnresolvedExceptionSpec(NewEST) &&
423          "Shouldn't see unknown exception specifications here");
424
425   // Shortcut the case where both have no spec.
426   if (OldEST == EST_None && NewEST == EST_None)
427     return false;
428
429   FunctionProtoType::NoexceptResult OldNR = Old->getNoexceptSpec(Context);
430   FunctionProtoType::NoexceptResult NewNR = New->getNoexceptSpec(Context);
431   if (OldNR == FunctionProtoType::NR_BadNoexcept ||
432       NewNR == FunctionProtoType::NR_BadNoexcept)
433     return false;
434
435   // Dependent noexcept specifiers are compatible with each other, but nothing
436   // else.
437   // One noexcept is compatible with another if the argument is the same
438   if (OldNR == NewNR &&
439       OldNR != FunctionProtoType::NR_NoNoexcept &&
440       NewNR != FunctionProtoType::NR_NoNoexcept)
441     return false;
442   if (OldNR != NewNR &&
443       OldNR != FunctionProtoType::NR_NoNoexcept &&
444       NewNR != FunctionProtoType::NR_NoNoexcept) {
445     Diag(NewLoc, DiagID);
446     if (NoteID.getDiagID() != 0 && OldLoc.isValid())
447       Diag(OldLoc, NoteID);
448     return true;
449   }
450
451   // The MS extension throw(...) is compatible with itself.
452   if (OldEST == EST_MSAny && NewEST == EST_MSAny)
453     return false;
454
455   // It's also compatible with no spec.
456   if ((OldEST == EST_None && NewEST == EST_MSAny) ||
457       (OldEST == EST_MSAny && NewEST == EST_None))
458     return false;
459
460   // It's also compatible with noexcept(false).
461   if (OldEST == EST_MSAny && NewNR == FunctionProtoType::NR_Throw)
462     return false;
463   if (NewEST == EST_MSAny && OldNR == FunctionProtoType::NR_Throw)
464     return false;
465
466   // As described above, noexcept(false) matches no spec only for functions.
467   if (AllowNoexceptAllMatchWithNoSpec) {
468     if (OldEST == EST_None && NewNR == FunctionProtoType::NR_Throw)
469       return false;
470     if (NewEST == EST_None && OldNR == FunctionProtoType::NR_Throw)
471       return false;
472   }
473
474   // Any non-throwing specifications are compatible.
475   bool OldNonThrowing = OldNR == FunctionProtoType::NR_Nothrow ||
476                         OldEST == EST_DynamicNone;
477   bool NewNonThrowing = NewNR == FunctionProtoType::NR_Nothrow ||
478                         NewEST == EST_DynamicNone;
479   if (OldNonThrowing && NewNonThrowing)
480     return false;
481
482   // As a special compatibility feature, under C++0x we accept no spec and
483   // throw(std::bad_alloc) as equivalent for operator new and operator new[].
484   // This is because the implicit declaration changed, but old code would break.
485   if (getLangOpts().CPlusPlus11 && IsOperatorNew) {
486     const FunctionProtoType *WithExceptions = nullptr;
487     if (OldEST == EST_None && NewEST == EST_Dynamic)
488       WithExceptions = New;
489     else if (OldEST == EST_Dynamic && NewEST == EST_None)
490       WithExceptions = Old;
491     if (WithExceptions && WithExceptions->getNumExceptions() == 1) {
492       // One has no spec, the other throw(something). If that something is
493       // std::bad_alloc, all conditions are met.
494       QualType Exception = *WithExceptions->exception_begin();
495       if (CXXRecordDecl *ExRecord = Exception->getAsCXXRecordDecl()) {
496         IdentifierInfo* Name = ExRecord->getIdentifier();
497         if (Name && Name->getName() == "bad_alloc") {
498           // It's called bad_alloc, but is it in std?
499           if (ExRecord->isInStdNamespace()) {
500             return false;
501           }
502         }
503       }
504     }
505   }
506
507   // At this point, the only remaining valid case is two matching dynamic
508   // specifications. We return here unless both specifications are dynamic.
509   if (OldEST != EST_Dynamic || NewEST != EST_Dynamic) {
510     if (MissingExceptionSpecification && Old->hasExceptionSpec() &&
511         !New->hasExceptionSpec()) {
512       // The old type has an exception specification of some sort, but
513       // the new type does not.
514       *MissingExceptionSpecification = true;
515
516       if (MissingEmptyExceptionSpecification && OldNonThrowing) {
517         // The old type has a throw() or noexcept(true) exception specification
518         // and the new type has no exception specification, and the caller asked
519         // to handle this itself.
520         *MissingEmptyExceptionSpecification = true;
521       }
522
523       return true;
524     }
525
526     Diag(NewLoc, DiagID);
527     if (NoteID.getDiagID() != 0 && OldLoc.isValid())
528       Diag(OldLoc, NoteID);
529     return true;
530   }
531
532   assert(OldEST == EST_Dynamic && NewEST == EST_Dynamic &&
533       "Exception compatibility logic error: non-dynamic spec slipped through.");
534
535   bool Success = true;
536   // Both have a dynamic exception spec. Collect the first set, then compare
537   // to the second.
538   llvm::SmallPtrSet<CanQualType, 8> OldTypes, NewTypes;
539   for (const auto &I : Old->exceptions())
540     OldTypes.insert(Context.getCanonicalType(I).getUnqualifiedType());
541
542   for (const auto &I : New->exceptions()) {
543     CanQualType TypePtr = Context.getCanonicalType(I).getUnqualifiedType();
544     if(OldTypes.count(TypePtr))
545       NewTypes.insert(TypePtr);
546     else
547       Success = false;
548   }
549
550   Success = Success && OldTypes.size() == NewTypes.size();
551
552   if (Success) {
553     return false;
554   }
555   Diag(NewLoc, DiagID);
556   if (NoteID.getDiagID() != 0 && OldLoc.isValid())
557     Diag(OldLoc, NoteID);
558   return true;
559 }
560
561 /// CheckExceptionSpecSubset - Check whether the second function type's
562 /// exception specification is a subset (or equivalent) of the first function
563 /// type. This is used by override and pointer assignment checks.
564 bool Sema::CheckExceptionSpecSubset(
565     const PartialDiagnostic &DiagID, const PartialDiagnostic & NoteID,
566     const FunctionProtoType *Superset, SourceLocation SuperLoc,
567     const FunctionProtoType *Subset, SourceLocation SubLoc) {
568
569   // Just auto-succeed under -fno-exceptions.
570   if (!getLangOpts().CXXExceptions)
571     return false;
572
573   // FIXME: As usual, we could be more specific in our error messages, but
574   // that better waits until we've got types with source locations.
575
576   if (!SubLoc.isValid())
577     SubLoc = SuperLoc;
578
579   // Resolve the exception specifications, if needed.
580   Superset = ResolveExceptionSpec(SuperLoc, Superset);
581   if (!Superset)
582     return false;
583   Subset = ResolveExceptionSpec(SubLoc, Subset);
584   if (!Subset)
585     return false;
586
587   ExceptionSpecificationType SuperEST = Superset->getExceptionSpecType();
588
589   // If superset contains everything, we're done.
590   if (SuperEST == EST_None || SuperEST == EST_MSAny)
591     return CheckParamExceptionSpec(NoteID, Superset, SuperLoc, Subset, SubLoc);
592
593   // If there are dependent noexcept specs, assume everything is fine. Unlike
594   // with the equivalency check, this is safe in this case, because we don't
595   // want to merge declarations. Checks after instantiation will catch any
596   // omissions we make here.
597   // We also shortcut checking if a noexcept expression was bad.
598
599   FunctionProtoType::NoexceptResult SuperNR =Superset->getNoexceptSpec(Context);
600   if (SuperNR == FunctionProtoType::NR_BadNoexcept ||
601       SuperNR == FunctionProtoType::NR_Dependent)
602     return false;
603
604   // Another case of the superset containing everything.
605   if (SuperNR == FunctionProtoType::NR_Throw)
606     return CheckParamExceptionSpec(NoteID, Superset, SuperLoc, Subset, SubLoc);
607
608   ExceptionSpecificationType SubEST = Subset->getExceptionSpecType();
609
610   assert(!isUnresolvedExceptionSpec(SuperEST) &&
611          !isUnresolvedExceptionSpec(SubEST) &&
612          "Shouldn't see unknown exception specifications here");
613
614   // It does not. If the subset contains everything, we've failed.
615   if (SubEST == EST_None || SubEST == EST_MSAny) {
616     Diag(SubLoc, DiagID);
617     if (NoteID.getDiagID() != 0)
618       Diag(SuperLoc, NoteID);
619     return true;
620   }
621
622   FunctionProtoType::NoexceptResult SubNR = Subset->getNoexceptSpec(Context);
623   if (SubNR == FunctionProtoType::NR_BadNoexcept ||
624       SubNR == FunctionProtoType::NR_Dependent)
625     return false;
626
627   // Another case of the subset containing everything.
628   if (SubNR == FunctionProtoType::NR_Throw) {
629     Diag(SubLoc, DiagID);
630     if (NoteID.getDiagID() != 0)
631       Diag(SuperLoc, NoteID);
632     return true;
633   }
634
635   // If the subset contains nothing, we're done.
636   if (SubEST == EST_DynamicNone || SubNR == FunctionProtoType::NR_Nothrow)
637     return CheckParamExceptionSpec(NoteID, Superset, SuperLoc, Subset, SubLoc);
638
639   // Otherwise, if the superset contains nothing, we've failed.
640   if (SuperEST == EST_DynamicNone || SuperNR == FunctionProtoType::NR_Nothrow) {
641     Diag(SubLoc, DiagID);
642     if (NoteID.getDiagID() != 0)
643       Diag(SuperLoc, NoteID);
644     return true;
645   }
646
647   assert(SuperEST == EST_Dynamic && SubEST == EST_Dynamic &&
648          "Exception spec subset: non-dynamic case slipped through.");
649
650   // Neither contains everything or nothing. Do a proper comparison.
651   for (const auto &SubI : Subset->exceptions()) {
652     // Take one type from the subset.
653     QualType CanonicalSubT = Context.getCanonicalType(SubI);
654     // Unwrap pointers and references so that we can do checks within a class
655     // hierarchy. Don't unwrap member pointers; they don't have hierarchy
656     // conversions on the pointee.
657     bool SubIsPointer = false;
658     if (const ReferenceType *RefTy = CanonicalSubT->getAs<ReferenceType>())
659       CanonicalSubT = RefTy->getPointeeType();
660     if (const PointerType *PtrTy = CanonicalSubT->getAs<PointerType>()) {
661       CanonicalSubT = PtrTy->getPointeeType();
662       SubIsPointer = true;
663     }
664     bool SubIsClass = CanonicalSubT->isRecordType();
665     CanonicalSubT = CanonicalSubT.getLocalUnqualifiedType();
666
667     CXXBasePaths Paths(/*FindAmbiguities=*/true, /*RecordPaths=*/true,
668                        /*DetectVirtual=*/false);
669
670     bool Contained = false;
671     // Make sure it's in the superset.
672     for (const auto &SuperI : Superset->exceptions()) {
673       QualType CanonicalSuperT = Context.getCanonicalType(SuperI);
674       // SubT must be SuperT or derived from it, or pointer or reference to
675       // such types.
676       if (const ReferenceType *RefTy = CanonicalSuperT->getAs<ReferenceType>())
677         CanonicalSuperT = RefTy->getPointeeType();
678       if (SubIsPointer) {
679         if (const PointerType *PtrTy = CanonicalSuperT->getAs<PointerType>())
680           CanonicalSuperT = PtrTy->getPointeeType();
681         else {
682           continue;
683         }
684       }
685       CanonicalSuperT = CanonicalSuperT.getLocalUnqualifiedType();
686       // If the types are the same, move on to the next type in the subset.
687       if (CanonicalSubT == CanonicalSuperT) {
688         Contained = true;
689         break;
690       }
691
692       // Otherwise we need to check the inheritance.
693       if (!SubIsClass || !CanonicalSuperT->isRecordType())
694         continue;
695
696       Paths.clear();
697       if (!IsDerivedFrom(CanonicalSubT, CanonicalSuperT, Paths))
698         continue;
699
700       if (Paths.isAmbiguous(Context.getCanonicalType(CanonicalSuperT)))
701         continue;
702
703       // Do this check from a context without privileges.
704       switch (CheckBaseClassAccess(SourceLocation(),
705                                    CanonicalSuperT, CanonicalSubT,
706                                    Paths.front(),
707                                    /*Diagnostic*/ 0,
708                                    /*ForceCheck*/ true,
709                                    /*ForceUnprivileged*/ true)) {
710       case AR_accessible: break;
711       case AR_inaccessible: continue;
712       case AR_dependent:
713         llvm_unreachable("access check dependent for unprivileged context");
714       case AR_delayed:
715         llvm_unreachable("access check delayed in non-declaration");
716       }
717
718       Contained = true;
719       break;
720     }
721     if (!Contained) {
722       Diag(SubLoc, DiagID);
723       if (NoteID.getDiagID() != 0)
724         Diag(SuperLoc, NoteID);
725       return true;
726     }
727   }
728   // We've run half the gauntlet.
729   return CheckParamExceptionSpec(NoteID, Superset, SuperLoc, Subset, SubLoc);
730 }
731
732 static bool CheckSpecForTypesEquivalent(Sema &S,
733     const PartialDiagnostic &DiagID, const PartialDiagnostic & NoteID,
734     QualType Target, SourceLocation TargetLoc,
735     QualType Source, SourceLocation SourceLoc)
736 {
737   const FunctionProtoType *TFunc = GetUnderlyingFunction(Target);
738   if (!TFunc)
739     return false;
740   const FunctionProtoType *SFunc = GetUnderlyingFunction(Source);
741   if (!SFunc)
742     return false;
743
744   return S.CheckEquivalentExceptionSpec(DiagID, NoteID, TFunc, TargetLoc,
745                                         SFunc, SourceLoc);
746 }
747
748 /// CheckParamExceptionSpec - Check if the parameter and return types of the
749 /// two functions have equivalent exception specs. This is part of the
750 /// assignment and override compatibility check. We do not check the parameters
751 /// of parameter function pointers recursively, as no sane programmer would
752 /// even be able to write such a function type.
753 bool Sema::CheckParamExceptionSpec(const PartialDiagnostic &NoteID,
754                                    const FunctionProtoType *Target,
755                                    SourceLocation TargetLoc,
756                                    const FunctionProtoType *Source,
757                                    SourceLocation SourceLoc) {
758   if (CheckSpecForTypesEquivalent(
759           *this, PDiag(diag::err_deep_exception_specs_differ) << 0, PDiag(),
760           Target->getReturnType(), TargetLoc, Source->getReturnType(),
761           SourceLoc))
762     return true;
763
764   // We shouldn't even be testing this unless the arguments are otherwise
765   // compatible.
766   assert(Target->getNumParams() == Source->getNumParams() &&
767          "Functions have different argument counts.");
768   for (unsigned i = 0, E = Target->getNumParams(); i != E; ++i) {
769     if (CheckSpecForTypesEquivalent(
770             *this, PDiag(diag::err_deep_exception_specs_differ) << 1, PDiag(),
771             Target->getParamType(i), TargetLoc, Source->getParamType(i),
772             SourceLoc))
773       return true;
774   }
775   return false;
776 }
777
778 bool Sema::CheckExceptionSpecCompatibility(Expr *From, QualType ToType) {
779   // First we check for applicability.
780   // Target type must be a function, function pointer or function reference.
781   const FunctionProtoType *ToFunc = GetUnderlyingFunction(ToType);
782   if (!ToFunc || ToFunc->hasDependentExceptionSpec())
783     return false;
784
785   // SourceType must be a function or function pointer.
786   const FunctionProtoType *FromFunc = GetUnderlyingFunction(From->getType());
787   if (!FromFunc || FromFunc->hasDependentExceptionSpec())
788     return false;
789
790   // Now we've got the correct types on both sides, check their compatibility.
791   // This means that the source of the conversion can only throw a subset of
792   // the exceptions of the target, and any exception specs on arguments or
793   // return types must be equivalent.
794   //
795   // FIXME: If there is a nested dependent exception specification, we should
796   // not be checking it here. This is fine:
797   //   template<typename T> void f() {
798   //     void (*p)(void (*) throw(T));
799   //     void (*q)(void (*) throw(int)) = p;
800   //   }
801   // ... because it might be instantiated with T=int.
802   return CheckExceptionSpecSubset(PDiag(diag::err_incompatible_exception_specs),
803                                   PDiag(), ToFunc, 
804                                   From->getSourceRange().getBegin(),
805                                   FromFunc, SourceLocation());
806 }
807
808 bool Sema::CheckOverridingFunctionExceptionSpec(const CXXMethodDecl *New,
809                                                 const CXXMethodDecl *Old) {
810   // If the new exception specification hasn't been parsed yet, skip the check.
811   // We'll get called again once it's been parsed.
812   if (New->getType()->castAs<FunctionProtoType>()->getExceptionSpecType() ==
813       EST_Unparsed)
814     return false;
815   if (getLangOpts().CPlusPlus11 && isa<CXXDestructorDecl>(New)) {
816     // Don't check uninstantiated template destructors at all. We can only
817     // synthesize correct specs after the template is instantiated.
818     if (New->getParent()->isDependentType())
819       return false;
820     if (New->getParent()->isBeingDefined()) {
821       // The destructor might be updated once the definition is finished. So
822       // remember it and check later.
823       DelayedExceptionSpecChecks.push_back(std::make_pair(New, Old));
824       return false;
825     }
826   }
827   // If the old exception specification hasn't been parsed yet, remember that
828   // we need to perform this check when we get to the end of the outermost
829   // lexically-surrounding class.
830   if (Old->getType()->castAs<FunctionProtoType>()->getExceptionSpecType() ==
831       EST_Unparsed) {
832     DelayedExceptionSpecChecks.push_back(std::make_pair(New, Old));
833     return false;
834   }
835   unsigned DiagID = diag::err_override_exception_spec;
836   if (getLangOpts().MicrosoftExt)
837     DiagID = diag::ext_override_exception_spec;
838   return CheckExceptionSpecSubset(PDiag(DiagID),
839                                   PDiag(diag::note_overridden_virtual_function),
840                                   Old->getType()->getAs<FunctionProtoType>(),
841                                   Old->getLocation(),
842                                   New->getType()->getAs<FunctionProtoType>(),
843                                   New->getLocation());
844 }
845
846 static CanThrowResult canSubExprsThrow(Sema &S, const Expr *E) {
847   CanThrowResult R = CT_Cannot;
848   for (const Stmt *SubStmt : E->children()) {
849     R = mergeCanThrow(R, S.canThrow(cast<Expr>(SubStmt)));
850     if (R == CT_Can)
851       break;
852   }
853   return R;
854 }
855
856 static CanThrowResult canCalleeThrow(Sema &S, const Expr *E, const Decl *D) {
857   assert(D && "Expected decl");
858
859   // See if we can get a function type from the decl somehow.
860   const ValueDecl *VD = dyn_cast<ValueDecl>(D);
861   if (!VD) // If we have no clue what we're calling, assume the worst.
862     return CT_Can;
863
864   // As an extension, we assume that __attribute__((nothrow)) functions don't
865   // throw.
866   if (isa<FunctionDecl>(D) && D->hasAttr<NoThrowAttr>())
867     return CT_Cannot;
868
869   QualType T = VD->getType();
870   const FunctionProtoType *FT;
871   if ((FT = T->getAs<FunctionProtoType>())) {
872   } else if (const PointerType *PT = T->getAs<PointerType>())
873     FT = PT->getPointeeType()->getAs<FunctionProtoType>();
874   else if (const ReferenceType *RT = T->getAs<ReferenceType>())
875     FT = RT->getPointeeType()->getAs<FunctionProtoType>();
876   else if (const MemberPointerType *MT = T->getAs<MemberPointerType>())
877     FT = MT->getPointeeType()->getAs<FunctionProtoType>();
878   else if (const BlockPointerType *BT = T->getAs<BlockPointerType>())
879     FT = BT->getPointeeType()->getAs<FunctionProtoType>();
880
881   if (!FT)
882     return CT_Can;
883
884   FT = S.ResolveExceptionSpec(E->getLocStart(), FT);
885   if (!FT)
886     return CT_Can;
887
888   return FT->isNothrow(S.Context) ? CT_Cannot : CT_Can;
889 }
890
891 static CanThrowResult canDynamicCastThrow(const CXXDynamicCastExpr *DC) {
892   if (DC->isTypeDependent())
893     return CT_Dependent;
894
895   if (!DC->getTypeAsWritten()->isReferenceType())
896     return CT_Cannot;
897
898   if (DC->getSubExpr()->isTypeDependent())
899     return CT_Dependent;
900
901   return DC->getCastKind() == clang::CK_Dynamic? CT_Can : CT_Cannot;
902 }
903
904 static CanThrowResult canTypeidThrow(Sema &S, const CXXTypeidExpr *DC) {
905   if (DC->isTypeOperand())
906     return CT_Cannot;
907
908   Expr *Op = DC->getExprOperand();
909   if (Op->isTypeDependent())
910     return CT_Dependent;
911
912   const RecordType *RT = Op->getType()->getAs<RecordType>();
913   if (!RT)
914     return CT_Cannot;
915
916   if (!cast<CXXRecordDecl>(RT->getDecl())->isPolymorphic())
917     return CT_Cannot;
918
919   if (Op->Classify(S.Context).isPRValue())
920     return CT_Cannot;
921
922   return CT_Can;
923 }
924
925 CanThrowResult Sema::canThrow(const Expr *E) {
926   // C++ [expr.unary.noexcept]p3:
927   //   [Can throw] if in a potentially-evaluated context the expression would
928   //   contain:
929   switch (E->getStmtClass()) {
930   case Expr::CXXThrowExprClass:
931     //   - a potentially evaluated throw-expression
932     return CT_Can;
933
934   case Expr::CXXDynamicCastExprClass: {
935     //   - a potentially evaluated dynamic_cast expression dynamic_cast<T>(v),
936     //     where T is a reference type, that requires a run-time check
937     CanThrowResult CT = canDynamicCastThrow(cast<CXXDynamicCastExpr>(E));
938     if (CT == CT_Can)
939       return CT;
940     return mergeCanThrow(CT, canSubExprsThrow(*this, E));
941   }
942
943   case Expr::CXXTypeidExprClass:
944     //   - a potentially evaluated typeid expression applied to a glvalue
945     //     expression whose type is a polymorphic class type
946     return canTypeidThrow(*this, cast<CXXTypeidExpr>(E));
947
948     //   - a potentially evaluated call to a function, member function, function
949     //     pointer, or member function pointer that does not have a non-throwing
950     //     exception-specification
951   case Expr::CallExprClass:
952   case Expr::CXXMemberCallExprClass:
953   case Expr::CXXOperatorCallExprClass:
954   case Expr::UserDefinedLiteralClass: {
955     const CallExpr *CE = cast<CallExpr>(E);
956     CanThrowResult CT;
957     if (E->isTypeDependent())
958       CT = CT_Dependent;
959     else if (isa<CXXPseudoDestructorExpr>(CE->getCallee()->IgnoreParens()))
960       CT = CT_Cannot;
961     else if (CE->getCalleeDecl())
962       CT = canCalleeThrow(*this, E, CE->getCalleeDecl());
963     else
964       CT = CT_Can;
965     if (CT == CT_Can)
966       return CT;
967     return mergeCanThrow(CT, canSubExprsThrow(*this, E));
968   }
969
970   case Expr::CXXConstructExprClass:
971   case Expr::CXXTemporaryObjectExprClass: {
972     CanThrowResult CT = canCalleeThrow(*this, E,
973         cast<CXXConstructExpr>(E)->getConstructor());
974     if (CT == CT_Can)
975       return CT;
976     return mergeCanThrow(CT, canSubExprsThrow(*this, E));
977   }
978
979   case Expr::LambdaExprClass: {
980     const LambdaExpr *Lambda = cast<LambdaExpr>(E);
981     CanThrowResult CT = CT_Cannot;
982     for (LambdaExpr::capture_init_iterator Cap = Lambda->capture_init_begin(),
983                                         CapEnd = Lambda->capture_init_end();
984          Cap != CapEnd; ++Cap)
985       CT = mergeCanThrow(CT, canThrow(*Cap));
986     return CT;
987   }
988
989   case Expr::CXXNewExprClass: {
990     CanThrowResult CT;
991     if (E->isTypeDependent())
992       CT = CT_Dependent;
993     else
994       CT = canCalleeThrow(*this, E, cast<CXXNewExpr>(E)->getOperatorNew());
995     if (CT == CT_Can)
996       return CT;
997     return mergeCanThrow(CT, canSubExprsThrow(*this, E));
998   }
999
1000   case Expr::CXXDeleteExprClass: {
1001     CanThrowResult CT;
1002     QualType DTy = cast<CXXDeleteExpr>(E)->getDestroyedType();
1003     if (DTy.isNull() || DTy->isDependentType()) {
1004       CT = CT_Dependent;
1005     } else {
1006       CT = canCalleeThrow(*this, E,
1007                           cast<CXXDeleteExpr>(E)->getOperatorDelete());
1008       if (const RecordType *RT = DTy->getAs<RecordType>()) {
1009         const CXXRecordDecl *RD = cast<CXXRecordDecl>(RT->getDecl());
1010         const CXXDestructorDecl *DD = RD->getDestructor();
1011         if (DD)
1012           CT = mergeCanThrow(CT, canCalleeThrow(*this, E, DD));
1013       }
1014       if (CT == CT_Can)
1015         return CT;
1016     }
1017     return mergeCanThrow(CT, canSubExprsThrow(*this, E));
1018   }
1019
1020   case Expr::CXXBindTemporaryExprClass: {
1021     // The bound temporary has to be destroyed again, which might throw.
1022     CanThrowResult CT = canCalleeThrow(*this, E,
1023       cast<CXXBindTemporaryExpr>(E)->getTemporary()->getDestructor());
1024     if (CT == CT_Can)
1025       return CT;
1026     return mergeCanThrow(CT, canSubExprsThrow(*this, E));
1027   }
1028
1029     // ObjC message sends are like function calls, but never have exception
1030     // specs.
1031   case Expr::ObjCMessageExprClass:
1032   case Expr::ObjCPropertyRefExprClass:
1033   case Expr::ObjCSubscriptRefExprClass:
1034     return CT_Can;
1035
1036     // All the ObjC literals that are implemented as calls are
1037     // potentially throwing unless we decide to close off that
1038     // possibility.
1039   case Expr::ObjCArrayLiteralClass:
1040   case Expr::ObjCDictionaryLiteralClass:
1041   case Expr::ObjCBoxedExprClass:
1042     return CT_Can;
1043
1044     // Many other things have subexpressions, so we have to test those.
1045     // Some are simple:
1046   case Expr::ConditionalOperatorClass:
1047   case Expr::CompoundLiteralExprClass:
1048   case Expr::CXXConstCastExprClass:
1049   case Expr::CXXReinterpretCastExprClass:
1050   case Expr::CXXStdInitializerListExprClass:
1051   case Expr::DesignatedInitExprClass:
1052   case Expr::DesignatedInitUpdateExprClass:
1053   case Expr::ExprWithCleanupsClass:
1054   case Expr::ExtVectorElementExprClass:
1055   case Expr::InitListExprClass:
1056   case Expr::MemberExprClass:
1057   case Expr::ObjCIsaExprClass:
1058   case Expr::ObjCIvarRefExprClass:
1059   case Expr::ParenExprClass:
1060   case Expr::ParenListExprClass:
1061   case Expr::ShuffleVectorExprClass:
1062   case Expr::ConvertVectorExprClass:
1063   case Expr::VAArgExprClass:
1064     return canSubExprsThrow(*this, E);
1065
1066     // Some might be dependent for other reasons.
1067   case Expr::ArraySubscriptExprClass:
1068   case Expr::BinaryOperatorClass:
1069   case Expr::CompoundAssignOperatorClass:
1070   case Expr::CStyleCastExprClass:
1071   case Expr::CXXStaticCastExprClass:
1072   case Expr::CXXFunctionalCastExprClass:
1073   case Expr::ImplicitCastExprClass:
1074   case Expr::MaterializeTemporaryExprClass:
1075   case Expr::UnaryOperatorClass: {
1076     CanThrowResult CT = E->isTypeDependent() ? CT_Dependent : CT_Cannot;
1077     return mergeCanThrow(CT, canSubExprsThrow(*this, E));
1078   }
1079
1080     // FIXME: We should handle StmtExpr, but that opens a MASSIVE can of worms.
1081   case Expr::StmtExprClass:
1082     return CT_Can;
1083
1084   case Expr::CXXDefaultArgExprClass:
1085     return canThrow(cast<CXXDefaultArgExpr>(E)->getExpr());
1086
1087   case Expr::CXXDefaultInitExprClass:
1088     return canThrow(cast<CXXDefaultInitExpr>(E)->getExpr());
1089
1090   case Expr::ChooseExprClass:
1091     if (E->isTypeDependent() || E->isValueDependent())
1092       return CT_Dependent;
1093     return canThrow(cast<ChooseExpr>(E)->getChosenSubExpr());
1094
1095   case Expr::GenericSelectionExprClass:
1096     if (cast<GenericSelectionExpr>(E)->isResultDependent())
1097       return CT_Dependent;
1098     return canThrow(cast<GenericSelectionExpr>(E)->getResultExpr());
1099
1100     // Some expressions are always dependent.
1101   case Expr::CXXDependentScopeMemberExprClass:
1102   case Expr::CXXUnresolvedConstructExprClass:
1103   case Expr::DependentScopeDeclRefExprClass:
1104   case Expr::CXXFoldExprClass:
1105     return CT_Dependent;
1106
1107   case Expr::AsTypeExprClass:
1108   case Expr::BinaryConditionalOperatorClass:
1109   case Expr::BlockExprClass:
1110   case Expr::CUDAKernelCallExprClass:
1111   case Expr::DeclRefExprClass:
1112   case Expr::ObjCBridgedCastExprClass:
1113   case Expr::ObjCIndirectCopyRestoreExprClass:
1114   case Expr::ObjCProtocolExprClass:
1115   case Expr::ObjCSelectorExprClass:
1116   case Expr::OffsetOfExprClass:
1117   case Expr::PackExpansionExprClass:
1118   case Expr::PseudoObjectExprClass:
1119   case Expr::SubstNonTypeTemplateParmExprClass:
1120   case Expr::SubstNonTypeTemplateParmPackExprClass:
1121   case Expr::FunctionParmPackExprClass:
1122   case Expr::UnaryExprOrTypeTraitExprClass:
1123   case Expr::UnresolvedLookupExprClass:
1124   case Expr::UnresolvedMemberExprClass:
1125   case Expr::TypoExprClass:
1126     // FIXME: Can any of the above throw?  If so, when?
1127     return CT_Cannot;
1128
1129   case Expr::AddrLabelExprClass:
1130   case Expr::ArrayTypeTraitExprClass:
1131   case Expr::AtomicExprClass:
1132   case Expr::TypeTraitExprClass:
1133   case Expr::CXXBoolLiteralExprClass:
1134   case Expr::CXXNoexceptExprClass:
1135   case Expr::CXXNullPtrLiteralExprClass:
1136   case Expr::CXXPseudoDestructorExprClass:
1137   case Expr::CXXScalarValueInitExprClass:
1138   case Expr::CXXThisExprClass:
1139   case Expr::CXXUuidofExprClass:
1140   case Expr::CharacterLiteralClass:
1141   case Expr::ExpressionTraitExprClass:
1142   case Expr::FloatingLiteralClass:
1143   case Expr::GNUNullExprClass:
1144   case Expr::ImaginaryLiteralClass:
1145   case Expr::ImplicitValueInitExprClass:
1146   case Expr::IntegerLiteralClass:
1147   case Expr::NoInitExprClass:
1148   case Expr::ObjCEncodeExprClass:
1149   case Expr::ObjCStringLiteralClass:
1150   case Expr::ObjCBoolLiteralExprClass:
1151   case Expr::OpaqueValueExprClass:
1152   case Expr::PredefinedExprClass:
1153   case Expr::SizeOfPackExprClass:
1154   case Expr::StringLiteralClass:
1155     // These expressions can never throw.
1156     return CT_Cannot;
1157
1158   case Expr::MSPropertyRefExprClass:
1159     llvm_unreachable("Invalid class for expression");
1160
1161 #define STMT(CLASS, PARENT) case Expr::CLASS##Class:
1162 #define STMT_RANGE(Base, First, Last)
1163 #define LAST_STMT_RANGE(BASE, FIRST, LAST)
1164 #define EXPR(CLASS, PARENT)
1165 #define ABSTRACT_STMT(STMT)
1166 #include "clang/AST/StmtNodes.inc"
1167   case Expr::NoStmtClass:
1168     llvm_unreachable("Invalid class for expression");
1169   }
1170   llvm_unreachable("Bogus StmtClass");
1171 }
1172
1173 } // end namespace clang