clang  3.9.0
SemaExceptionSpec.cpp
Go to the documentation of this file.
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 
17 #include "clang/AST/Expr.h"
18 #include "clang/AST/ExprCXX.h"
19 #include "clang/AST/TypeLoc.h"
20 #include "clang/Basic/Diagnostic.h"
22 #include "llvm/ADT/SmallPtrSet.h"
23 #include "llvm/ADT/SmallString.h"
24 
25 namespace clang {
26 
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.
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.
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.
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())
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  // In Microsoft mode, downgrade this to a warning.
114  unsigned DiagID = diag::err_incomplete_in_exception_spec;
115  bool ReturnValueOnError = true;
116  if (getLangOpts().MicrosoftExt) {
117  DiagID = diag::ext_incomplete_in_exception_spec;
118  ReturnValueOnError = false;
119  }
120  if (!(PointeeT->isRecordType() &&
121  PointeeT->getAs<RecordType>()->isBeingDefined()) &&
122  RequireCompleteType(Range.getBegin(), PointeeT, DiagID, Kind, Range))
123  return ReturnValueOnError;
124 
125  return false;
126 }
127 
128 /// CheckDistantExceptionSpec - Check if the given type is a pointer or pointer
129 /// to member to a function with an exception specification. This means that
130 /// it is invalid to add another level of indirection.
132  if (const PointerType *PT = T->getAs<PointerType>())
133  T = PT->getPointeeType();
134  else if (const MemberPointerType *PT = T->getAs<MemberPointerType>())
135  T = PT->getPointeeType();
136  else
137  return false;
138 
139  const FunctionProtoType *FnT = T->getAs<FunctionProtoType>();
140  if (!FnT)
141  return false;
142 
143  return FnT->hasExceptionSpec();
144 }
145 
146 const FunctionProtoType *
148  if (FPT->getExceptionSpecType() == EST_Unparsed) {
149  Diag(Loc, diag::err_exception_spec_not_parsed);
150  return nullptr;
151  }
152 
154  return FPT;
155 
156  FunctionDecl *SourceDecl = FPT->getExceptionSpecDecl();
157  const FunctionProtoType *SourceFPT =
158  SourceDecl->getType()->castAs<FunctionProtoType>();
159 
160  // If the exception specification has already been resolved, just return it.
162  return SourceFPT;
163 
164  // Compute or instantiate the exception specification now.
165  if (SourceFPT->getExceptionSpecType() == EST_Unevaluated)
166  EvaluateImplicitExceptionSpec(Loc, cast<CXXMethodDecl>(SourceDecl));
167  else
168  InstantiateExceptionSpec(Loc, SourceDecl);
169 
170  const FunctionProtoType *Proto =
171  SourceDecl->getType()->castAs<FunctionProtoType>();
172  if (Proto->getExceptionSpecType() == clang::EST_Unparsed) {
173  Diag(Loc, diag::err_exception_spec_not_parsed);
174  Proto = nullptr;
175  }
176  return Proto;
177 }
178 
179 void
182  // If we've fully resolved the exception specification, notify listeners.
184  if (auto *Listener = getASTMutationListener())
185  Listener->ResolvedExceptionSpec(FD);
186 
187  for (auto *Redecl : FD->redecls())
188  Context.adjustExceptionSpec(cast<FunctionDecl>(Redecl), ESI);
189 }
190 
191 /// Determine whether a function has an implicitly-generated exception
192 /// specification.
194  if (!isa<CXXDestructorDecl>(Decl) &&
195  Decl->getDeclName().getCXXOverloadedOperator() != OO_Delete &&
196  Decl->getDeclName().getCXXOverloadedOperator() != OO_Array_Delete)
197  return false;
198 
199  // For a function that the user didn't declare:
200  // - if this is a destructor, its exception specification is implicit.
201  // - if this is 'operator delete' or 'operator delete[]', the exception
202  // specification is as-if an explicit exception specification was given
203  // (per [basic.stc.dynamic]p2).
204  if (!Decl->getTypeSourceInfo())
205  return isa<CXXDestructorDecl>(Decl);
206 
207  const FunctionProtoType *Ty =
209  return !Ty->hasExceptionSpec();
210 }
211 
214  bool IsOperatorNew = OO == OO_New || OO == OO_Array_New;
215  bool MissingExceptionSpecification = false;
216  bool MissingEmptyExceptionSpecification = false;
217 
218  unsigned DiagID = diag::err_mismatched_exception_spec;
219  bool ReturnValueOnError = true;
220  if (getLangOpts().MicrosoftExt) {
221  DiagID = diag::ext_mismatched_exception_spec;
222  ReturnValueOnError = false;
223  }
224 
225  // Check the types as written: they must match before any exception
226  // specification adjustment is applied.
228  PDiag(DiagID), PDiag(diag::note_previous_declaration),
229  Old->getType()->getAs<FunctionProtoType>(), Old->getLocation(),
230  New->getType()->getAs<FunctionProtoType>(), New->getLocation(),
231  &MissingExceptionSpecification, &MissingEmptyExceptionSpecification,
232  /*AllowNoexceptAllMatchWithNoSpec=*/true, IsOperatorNew)) {
233  // C++11 [except.spec]p4 [DR1492]:
234  // If a declaration of a function has an implicit
235  // exception-specification, other declarations of the function shall
236  // not specify an exception-specification.
237  if (getLangOpts().CPlusPlus11 &&
239  Diag(New->getLocation(), diag::ext_implicit_exception_spec_mismatch)
240  << hasImplicitExceptionSpec(Old);
241  if (Old->getLocation().isValid())
242  Diag(Old->getLocation(), diag::note_previous_declaration);
243  }
244  return false;
245  }
246 
247  // The failure was something other than an missing exception
248  // specification; return an error, except in MS mode where this is a warning.
249  if (!MissingExceptionSpecification)
250  return ReturnValueOnError;
251 
252  const FunctionProtoType *NewProto =
253  New->getType()->castAs<FunctionProtoType>();
254 
255  // The new function declaration is only missing an empty exception
256  // specification "throw()". If the throw() specification came from a
257  // function in a system header that has C linkage, just add an empty
258  // exception specification to the "new" declaration. This is an
259  // egregious workaround for glibc, which adds throw() specifications
260  // to many libc functions as an optimization. Unfortunately, that
261  // optimization isn't permitted by the C++ standard, so we're forced
262  // to work around it here.
263  if (MissingEmptyExceptionSpecification && NewProto &&
264  (Old->getLocation().isInvalid() ||
265  Context.getSourceManager().isInSystemHeader(Old->getLocation())) &&
266  Old->isExternC()) {
268  NewProto->getReturnType(), NewProto->getParamTypes(),
270  return false;
271  }
272 
273  const FunctionProtoType *OldProto =
274  Old->getType()->castAs<FunctionProtoType>();
275 
277  if (ESI.Type == EST_Dynamic) {
278  ESI.Exceptions = OldProto->exceptions();
279  }
280 
281  if (ESI.Type == EST_ComputedNoexcept) {
282  // For computed noexcept, we can't just take the expression from the old
283  // prototype. It likely contains references to the old prototype's
284  // parameters.
285  New->setInvalidDecl();
286  } else {
287  // Update the type of the function with the appropriate exception
288  // specification.
290  NewProto->getReturnType(), NewProto->getParamTypes(),
291  NewProto->getExtProtoInfo().withExceptionSpec(ESI)));
292  }
293 
294  if (getLangOpts().MicrosoftExt && ESI.Type != EST_ComputedNoexcept) {
295  // Allow missing exception specifications in redeclarations as an extension.
296  DiagID = diag::ext_ms_missing_exception_specification;
297  ReturnValueOnError = false;
298  } else if (New->isReplaceableGlobalAllocationFunction() &&
299  ESI.Type != EST_ComputedNoexcept) {
300  // Allow missing exception specifications in redeclarations as an extension,
301  // when declaring a replaceable global allocation function.
302  DiagID = diag::ext_missing_exception_specification;
303  ReturnValueOnError = false;
304  } else {
305  DiagID = diag::err_missing_exception_specification;
306  ReturnValueOnError = true;
307  }
308 
309  // Warn about the lack of exception specification.
310  SmallString<128> ExceptionSpecString;
311  llvm::raw_svector_ostream OS(ExceptionSpecString);
312  switch (OldProto->getExceptionSpecType()) {
313  case EST_DynamicNone:
314  OS << "throw()";
315  break;
316 
317  case EST_Dynamic: {
318  OS << "throw(";
319  bool OnFirstException = true;
320  for (const auto &E : OldProto->exceptions()) {
321  if (OnFirstException)
322  OnFirstException = false;
323  else
324  OS << ", ";
325 
326  OS << E.getAsString(getPrintingPolicy());
327  }
328  OS << ")";
329  break;
330  }
331 
332  case EST_BasicNoexcept:
333  OS << "noexcept";
334  break;
335 
337  OS << "noexcept(";
338  assert(OldProto->getNoexceptExpr() != nullptr && "Expected non-null Expr");
339  OldProto->getNoexceptExpr()->printPretty(OS, nullptr, getPrintingPolicy());
340  OS << ")";
341  break;
342 
343  default:
344  llvm_unreachable("This spec type is compatible with none.");
345  }
346 
347  SourceLocation FixItLoc;
348  if (TypeSourceInfo *TSInfo = New->getTypeSourceInfo()) {
349  TypeLoc TL = TSInfo->getTypeLoc().IgnoreParens();
350  // FIXME: Preserve enough information so that we can produce a correct fixit
351  // location when there is a trailing return type.
352  if (auto FTLoc = TL.getAs<FunctionProtoTypeLoc>())
353  if (!FTLoc.getTypePtr()->hasTrailingReturn())
354  FixItLoc = getLocForEndOfToken(FTLoc.getLocalRangeEnd());
355  }
356 
357  if (FixItLoc.isInvalid())
358  Diag(New->getLocation(), DiagID)
359  << New << OS.str();
360  else {
361  Diag(New->getLocation(), DiagID)
362  << New << OS.str()
363  << FixItHint::CreateInsertion(FixItLoc, " " + OS.str().str());
364  }
365 
366  if (Old->getLocation().isValid())
367  Diag(Old->getLocation(), diag::note_previous_declaration);
368 
369  return ReturnValueOnError;
370 }
371 
372 /// CheckEquivalentExceptionSpec - Check if the two types have equivalent
373 /// exception specifications. Exception specifications are equivalent if
374 /// they allow exactly the same set of exception types. It does not matter how
375 /// that is achieved. See C++ [except.spec]p2.
377  const FunctionProtoType *Old, SourceLocation OldLoc,
378  const FunctionProtoType *New, SourceLocation NewLoc) {
379  unsigned DiagID = diag::err_mismatched_exception_spec;
380  if (getLangOpts().MicrosoftExt)
381  DiagID = diag::ext_mismatched_exception_spec;
383  PDiag(diag::note_previous_declaration), Old, OldLoc, New, NewLoc);
384 
385  // In Microsoft mode, mismatching exception specifications just cause a warning.
386  if (getLangOpts().MicrosoftExt)
387  return false;
388  return Result;
389 }
390 
391 /// CheckEquivalentExceptionSpec - Check if the two types have compatible
392 /// exception specifications. See C++ [except.spec]p3.
393 ///
394 /// \return \c false if the exception specifications match, \c true if there is
395 /// a problem. If \c true is returned, either a diagnostic has already been
396 /// produced or \c *MissingExceptionSpecification is set to \c true.
398  const PartialDiagnostic & NoteID,
399  const FunctionProtoType *Old,
400  SourceLocation OldLoc,
401  const FunctionProtoType *New,
402  SourceLocation NewLoc,
403  bool *MissingExceptionSpecification,
404  bool*MissingEmptyExceptionSpecification,
405  bool AllowNoexceptAllMatchWithNoSpec,
406  bool IsOperatorNew) {
407  // Just completely ignore this under -fno-exceptions.
408  if (!getLangOpts().CXXExceptions)
409  return false;
410 
411  if (MissingExceptionSpecification)
412  *MissingExceptionSpecification = false;
413 
414  if (MissingEmptyExceptionSpecification)
415  *MissingEmptyExceptionSpecification = false;
416 
417  Old = ResolveExceptionSpec(NewLoc, Old);
418  if (!Old)
419  return false;
420  New = ResolveExceptionSpec(NewLoc, New);
421  if (!New)
422  return false;
423 
424  // C++0x [except.spec]p3: Two exception-specifications are compatible if:
425  // - both are non-throwing, regardless of their form,
426  // - both have the form noexcept(constant-expression) and the constant-
427  // expressions are equivalent,
428  // - both are dynamic-exception-specifications that have the same set of
429  // adjusted types.
430  //
431  // C++0x [except.spec]p12: An exception-specification is non-throwing if it is
432  // of the form throw(), noexcept, or noexcept(constant-expression) where the
433  // constant-expression yields true.
434  //
435  // C++0x [except.spec]p4: If any declaration of a function has an exception-
436  // specifier that is not a noexcept-specification allowing all exceptions,
437  // all declarations [...] of that function shall have a compatible
438  // exception-specification.
439  //
440  // That last point basically means that noexcept(false) matches no spec.
441  // It's considered when AllowNoexceptAllMatchWithNoSpec is true.
442 
445 
446  assert(!isUnresolvedExceptionSpec(OldEST) &&
447  !isUnresolvedExceptionSpec(NewEST) &&
448  "Shouldn't see unknown exception specifications here");
449 
450  // Shortcut the case where both have no spec.
451  if (OldEST == EST_None && NewEST == EST_None)
452  return false;
453 
456  if (OldNR == FunctionProtoType::NR_BadNoexcept ||
458  return false;
459 
460  // Dependent noexcept specifiers are compatible with each other, but nothing
461  // else.
462  // One noexcept is compatible with another if the argument is the same
463  if (OldNR == NewNR &&
466  return false;
467  if (OldNR != NewNR &&
470  Diag(NewLoc, DiagID);
471  if (NoteID.getDiagID() != 0 && OldLoc.isValid())
472  Diag(OldLoc, NoteID);
473  return true;
474  }
475 
476  // The MS extension throw(...) is compatible with itself.
477  if (OldEST == EST_MSAny && NewEST == EST_MSAny)
478  return false;
479 
480  // It's also compatible with no spec.
481  if ((OldEST == EST_None && NewEST == EST_MSAny) ||
482  (OldEST == EST_MSAny && NewEST == EST_None))
483  return false;
484 
485  // It's also compatible with noexcept(false).
486  if (OldEST == EST_MSAny && NewNR == FunctionProtoType::NR_Throw)
487  return false;
488  if (NewEST == EST_MSAny && OldNR == FunctionProtoType::NR_Throw)
489  return false;
490 
491  // As described above, noexcept(false) matches no spec only for functions.
492  if (AllowNoexceptAllMatchWithNoSpec) {
493  if (OldEST == EST_None && NewNR == FunctionProtoType::NR_Throw)
494  return false;
495  if (NewEST == EST_None && OldNR == FunctionProtoType::NR_Throw)
496  return false;
497  }
498 
499  // Any non-throwing specifications are compatible.
500  bool OldNonThrowing = OldNR == FunctionProtoType::NR_Nothrow ||
501  OldEST == EST_DynamicNone;
502  bool NewNonThrowing = NewNR == FunctionProtoType::NR_Nothrow ||
503  NewEST == EST_DynamicNone;
504  if (OldNonThrowing && NewNonThrowing)
505  return false;
506 
507  // As a special compatibility feature, under C++0x we accept no spec and
508  // throw(std::bad_alloc) as equivalent for operator new and operator new[].
509  // This is because the implicit declaration changed, but old code would break.
510  if (getLangOpts().CPlusPlus11 && IsOperatorNew) {
511  const FunctionProtoType *WithExceptions = nullptr;
512  if (OldEST == EST_None && NewEST == EST_Dynamic)
513  WithExceptions = New;
514  else if (OldEST == EST_Dynamic && NewEST == EST_None)
515  WithExceptions = Old;
516  if (WithExceptions && WithExceptions->getNumExceptions() == 1) {
517  // One has no spec, the other throw(something). If that something is
518  // std::bad_alloc, all conditions are met.
519  QualType Exception = *WithExceptions->exception_begin();
520  if (CXXRecordDecl *ExRecord = Exception->getAsCXXRecordDecl()) {
521  IdentifierInfo* Name = ExRecord->getIdentifier();
522  if (Name && Name->getName() == "bad_alloc") {
523  // It's called bad_alloc, but is it in std?
524  if (ExRecord->isInStdNamespace()) {
525  return false;
526  }
527  }
528  }
529  }
530  }
531 
532  // At this point, the only remaining valid case is two matching dynamic
533  // specifications. We return here unless both specifications are dynamic.
534  if (OldEST != EST_Dynamic || NewEST != EST_Dynamic) {
535  if (MissingExceptionSpecification && Old->hasExceptionSpec() &&
536  !New->hasExceptionSpec()) {
537  // The old type has an exception specification of some sort, but
538  // the new type does not.
539  *MissingExceptionSpecification = true;
540 
541  if (MissingEmptyExceptionSpecification && OldNonThrowing) {
542  // The old type has a throw() or noexcept(true) exception specification
543  // and the new type has no exception specification, and the caller asked
544  // to handle this itself.
545  *MissingEmptyExceptionSpecification = true;
546  }
547 
548  return true;
549  }
550 
551  Diag(NewLoc, DiagID);
552  if (NoteID.getDiagID() != 0 && OldLoc.isValid())
553  Diag(OldLoc, NoteID);
554  return true;
555  }
556 
557  assert(OldEST == EST_Dynamic && NewEST == EST_Dynamic &&
558  "Exception compatibility logic error: non-dynamic spec slipped through.");
559 
560  bool Success = true;
561  // Both have a dynamic exception spec. Collect the first set, then compare
562  // to the second.
563  llvm::SmallPtrSet<CanQualType, 8> OldTypes, NewTypes;
564  for (const auto &I : Old->exceptions())
565  OldTypes.insert(Context.getCanonicalType(I).getUnqualifiedType());
566 
567  for (const auto &I : New->exceptions()) {
569  if(OldTypes.count(TypePtr))
570  NewTypes.insert(TypePtr);
571  else
572  Success = false;
573  }
574 
575  Success = Success && OldTypes.size() == NewTypes.size();
576 
577  if (Success) {
578  return false;
579  }
580  Diag(NewLoc, DiagID);
581  if (NoteID.getDiagID() != 0 && OldLoc.isValid())
582  Diag(OldLoc, NoteID);
583  return true;
584 }
585 
586 /// CheckExceptionSpecSubset - Check whether the second function type's
587 /// exception specification is a subset (or equivalent) of the first function
588 /// type. This is used by override and pointer assignment checks.
590  const PartialDiagnostic &DiagID, const PartialDiagnostic & NoteID,
591  const FunctionProtoType *Superset, SourceLocation SuperLoc,
592  const FunctionProtoType *Subset, SourceLocation SubLoc) {
593 
594  // Just auto-succeed under -fno-exceptions.
595  if (!getLangOpts().CXXExceptions)
596  return false;
597 
598  // FIXME: As usual, we could be more specific in our error messages, but
599  // that better waits until we've got types with source locations.
600 
601  if (!SubLoc.isValid())
602  SubLoc = SuperLoc;
603 
604  // Resolve the exception specifications, if needed.
605  Superset = ResolveExceptionSpec(SuperLoc, Superset);
606  if (!Superset)
607  return false;
608  Subset = ResolveExceptionSpec(SubLoc, Subset);
609  if (!Subset)
610  return false;
611 
612  ExceptionSpecificationType SuperEST = Superset->getExceptionSpecType();
613 
614  // If superset contains everything, we're done.
615  if (SuperEST == EST_None || SuperEST == EST_MSAny)
616  return CheckParamExceptionSpec(NoteID, Superset, SuperLoc, Subset, SubLoc);
617 
618  // If there are dependent noexcept specs, assume everything is fine. Unlike
619  // with the equivalency check, this is safe in this case, because we don't
620  // want to merge declarations. Checks after instantiation will catch any
621  // omissions we make here.
622  // We also shortcut checking if a noexcept expression was bad.
623 
625  if (SuperNR == FunctionProtoType::NR_BadNoexcept ||
627  return false;
628 
629  // Another case of the superset containing everything.
630  if (SuperNR == FunctionProtoType::NR_Throw)
631  return CheckParamExceptionSpec(NoteID, Superset, SuperLoc, Subset, SubLoc);
632 
634 
635  assert(!isUnresolvedExceptionSpec(SuperEST) &&
636  !isUnresolvedExceptionSpec(SubEST) &&
637  "Shouldn't see unknown exception specifications here");
638 
639  // It does not. If the subset contains everything, we've failed.
640  if (SubEST == EST_None || SubEST == EST_MSAny) {
641  Diag(SubLoc, DiagID);
642  if (NoteID.getDiagID() != 0)
643  Diag(SuperLoc, NoteID);
644  return true;
645  }
646 
648  if (SubNR == FunctionProtoType::NR_BadNoexcept ||
650  return false;
651 
652  // Another case of the subset containing everything.
653  if (SubNR == FunctionProtoType::NR_Throw) {
654  Diag(SubLoc, DiagID);
655  if (NoteID.getDiagID() != 0)
656  Diag(SuperLoc, NoteID);
657  return true;
658  }
659 
660  // If the subset contains nothing, we're done.
661  if (SubEST == EST_DynamicNone || SubNR == FunctionProtoType::NR_Nothrow)
662  return CheckParamExceptionSpec(NoteID, Superset, SuperLoc, Subset, SubLoc);
663 
664  // Otherwise, if the superset contains nothing, we've failed.
665  if (SuperEST == EST_DynamicNone || SuperNR == FunctionProtoType::NR_Nothrow) {
666  Diag(SubLoc, DiagID);
667  if (NoteID.getDiagID() != 0)
668  Diag(SuperLoc, NoteID);
669  return true;
670  }
671 
672  assert(SuperEST == EST_Dynamic && SubEST == EST_Dynamic &&
673  "Exception spec subset: non-dynamic case slipped through.");
674 
675  // Neither contains everything or nothing. Do a proper comparison.
676  for (const auto &SubI : Subset->exceptions()) {
677  // Take one type from the subset.
678  QualType CanonicalSubT = Context.getCanonicalType(SubI);
679  // Unwrap pointers and references so that we can do checks within a class
680  // hierarchy. Don't unwrap member pointers; they don't have hierarchy
681  // conversions on the pointee.
682  bool SubIsPointer = false;
683  if (const ReferenceType *RefTy = CanonicalSubT->getAs<ReferenceType>())
684  CanonicalSubT = RefTy->getPointeeType();
685  if (const PointerType *PtrTy = CanonicalSubT->getAs<PointerType>()) {
686  CanonicalSubT = PtrTy->getPointeeType();
687  SubIsPointer = true;
688  }
689  bool SubIsClass = CanonicalSubT->isRecordType();
690  CanonicalSubT = CanonicalSubT.getLocalUnqualifiedType();
691 
692  CXXBasePaths Paths(/*FindAmbiguities=*/true, /*RecordPaths=*/true,
693  /*DetectVirtual=*/false);
694 
695  bool Contained = false;
696  // Make sure it's in the superset.
697  for (const auto &SuperI : Superset->exceptions()) {
698  QualType CanonicalSuperT = Context.getCanonicalType(SuperI);
699  // SubT must be SuperT or derived from it, or pointer or reference to
700  // such types.
701  if (const ReferenceType *RefTy = CanonicalSuperT->getAs<ReferenceType>())
702  CanonicalSuperT = RefTy->getPointeeType();
703  if (SubIsPointer) {
704  if (const PointerType *PtrTy = CanonicalSuperT->getAs<PointerType>())
705  CanonicalSuperT = PtrTy->getPointeeType();
706  else {
707  continue;
708  }
709  }
710  CanonicalSuperT = CanonicalSuperT.getLocalUnqualifiedType();
711  // If the types are the same, move on to the next type in the subset.
712  if (CanonicalSubT == CanonicalSuperT) {
713  Contained = true;
714  break;
715  }
716 
717  // Otherwise we need to check the inheritance.
718  if (!SubIsClass || !CanonicalSuperT->isRecordType())
719  continue;
720 
721  Paths.clear();
722  if (!IsDerivedFrom(SubLoc, CanonicalSubT, CanonicalSuperT, Paths))
723  continue;
724 
725  if (Paths.isAmbiguous(Context.getCanonicalType(CanonicalSuperT)))
726  continue;
727 
728  // Do this check from a context without privileges.
730  CanonicalSuperT, CanonicalSubT,
731  Paths.front(),
732  /*Diagnostic*/ 0,
733  /*ForceCheck*/ true,
734  /*ForceUnprivileged*/ true)) {
735  case AR_accessible: break;
736  case AR_inaccessible: continue;
737  case AR_dependent:
738  llvm_unreachable("access check dependent for unprivileged context");
739  case AR_delayed:
740  llvm_unreachable("access check delayed in non-declaration");
741  }
742 
743  Contained = true;
744  break;
745  }
746  if (!Contained) {
747  Diag(SubLoc, DiagID);
748  if (NoteID.getDiagID() != 0)
749  Diag(SuperLoc, NoteID);
750  return true;
751  }
752  }
753  // We've run half the gauntlet.
754  return CheckParamExceptionSpec(NoteID, Superset, SuperLoc, Subset, SubLoc);
755 }
756 
758  const PartialDiagnostic &DiagID, const PartialDiagnostic & NoteID,
759  QualType Target, SourceLocation TargetLoc,
760  QualType Source, SourceLocation SourceLoc)
761 {
762  const FunctionProtoType *TFunc = GetUnderlyingFunction(Target);
763  if (!TFunc)
764  return false;
765  const FunctionProtoType *SFunc = GetUnderlyingFunction(Source);
766  if (!SFunc)
767  return false;
768 
769  return S.CheckEquivalentExceptionSpec(DiagID, NoteID, TFunc, TargetLoc,
770  SFunc, SourceLoc);
771 }
772 
773 /// CheckParamExceptionSpec - Check if the parameter and return types of the
774 /// two functions have equivalent exception specs. This is part of the
775 /// assignment and override compatibility check. We do not check the parameters
776 /// of parameter function pointers recursively, as no sane programmer would
777 /// even be able to write such a function type.
779  const FunctionProtoType *Target,
780  SourceLocation TargetLoc,
781  const FunctionProtoType *Source,
782  SourceLocation SourceLoc) {
784  *this, PDiag(diag::err_deep_exception_specs_differ) << 0, PDiag(),
785  Target->getReturnType(), TargetLoc, Source->getReturnType(),
786  SourceLoc))
787  return true;
788 
789  // We shouldn't even be testing this unless the arguments are otherwise
790  // compatible.
791  assert(Target->getNumParams() == Source->getNumParams() &&
792  "Functions have different argument counts.");
793  for (unsigned i = 0, E = Target->getNumParams(); i != E; ++i) {
795  *this, PDiag(diag::err_deep_exception_specs_differ) << 1, PDiag(),
796  Target->getParamType(i), TargetLoc, Source->getParamType(i),
797  SourceLoc))
798  return true;
799  }
800  return false;
801 }
802 
804  // First we check for applicability.
805  // Target type must be a function, function pointer or function reference.
806  const FunctionProtoType *ToFunc = GetUnderlyingFunction(ToType);
807  if (!ToFunc || ToFunc->hasDependentExceptionSpec())
808  return false;
809 
810  // SourceType must be a function or function pointer.
811  const FunctionProtoType *FromFunc = GetUnderlyingFunction(From->getType());
812  if (!FromFunc || FromFunc->hasDependentExceptionSpec())
813  return false;
814 
815  // Now we've got the correct types on both sides, check their compatibility.
816  // This means that the source of the conversion can only throw a subset of
817  // the exceptions of the target, and any exception specs on arguments or
818  // return types must be equivalent.
819  //
820  // FIXME: If there is a nested dependent exception specification, we should
821  // not be checking it here. This is fine:
822  // template<typename T> void f() {
823  // void (*p)(void (*) throw(T));
824  // void (*q)(void (*) throw(int)) = p;
825  // }
826  // ... because it might be instantiated with T=int.
827  return CheckExceptionSpecSubset(PDiag(diag::err_incompatible_exception_specs),
828  PDiag(), ToFunc,
829  From->getSourceRange().getBegin(),
830  FromFunc, SourceLocation());
831 }
832 
834  const CXXMethodDecl *Old) {
835  // If the new exception specification hasn't been parsed yet, skip the check.
836  // We'll get called again once it's been parsed.
837  if (New->getType()->castAs<FunctionProtoType>()->getExceptionSpecType() ==
838  EST_Unparsed)
839  return false;
840  if (getLangOpts().CPlusPlus11 && isa<CXXDestructorDecl>(New)) {
841  // Don't check uninstantiated template destructors at all. We can only
842  // synthesize correct specs after the template is instantiated.
843  if (New->getParent()->isDependentType())
844  return false;
845  if (New->getParent()->isBeingDefined()) {
846  // The destructor might be updated once the definition is finished. So
847  // remember it and check later.
848  DelayedExceptionSpecChecks.push_back(std::make_pair(New, Old));
849  return false;
850  }
851  }
852  // If the old exception specification hasn't been parsed yet, remember that
853  // we need to perform this check when we get to the end of the outermost
854  // lexically-surrounding class.
855  if (Old->getType()->castAs<FunctionProtoType>()->getExceptionSpecType() ==
856  EST_Unparsed) {
857  DelayedExceptionSpecChecks.push_back(std::make_pair(New, Old));
858  return false;
859  }
860  unsigned DiagID = diag::err_override_exception_spec;
861  if (getLangOpts().MicrosoftExt)
862  DiagID = diag::ext_override_exception_spec;
863  return CheckExceptionSpecSubset(PDiag(DiagID),
864  PDiag(diag::note_overridden_virtual_function),
865  Old->getType()->getAs<FunctionProtoType>(),
866  Old->getLocation(),
867  New->getType()->getAs<FunctionProtoType>(),
868  New->getLocation());
869 }
870 
873  for (const Stmt *SubStmt : E->children()) {
874  R = mergeCanThrow(R, S.canThrow(cast<Expr>(SubStmt)));
875  if (R == CT_Can)
876  break;
877  }
878  return R;
879 }
880 
881 static CanThrowResult canCalleeThrow(Sema &S, const Expr *E, const Decl *D) {
882  assert(D && "Expected decl");
883 
884  // See if we can get a function type from the decl somehow.
885  const ValueDecl *VD = dyn_cast<ValueDecl>(D);
886  if (!VD) // If we have no clue what we're calling, assume the worst.
887  return CT_Can;
888 
889  // As an extension, we assume that __attribute__((nothrow)) functions don't
890  // throw.
891  if (isa<FunctionDecl>(D) && D->hasAttr<NoThrowAttr>())
892  return CT_Cannot;
893 
894  QualType T = VD->getType();
895  const FunctionProtoType *FT;
896  if ((FT = T->getAs<FunctionProtoType>())) {
897  } else if (const PointerType *PT = T->getAs<PointerType>())
898  FT = PT->getPointeeType()->getAs<FunctionProtoType>();
899  else if (const ReferenceType *RT = T->getAs<ReferenceType>())
900  FT = RT->getPointeeType()->getAs<FunctionProtoType>();
901  else if (const MemberPointerType *MT = T->getAs<MemberPointerType>())
902  FT = MT->getPointeeType()->getAs<FunctionProtoType>();
903  else if (const BlockPointerType *BT = T->getAs<BlockPointerType>())
904  FT = BT->getPointeeType()->getAs<FunctionProtoType>();
905 
906  if (!FT)
907  return CT_Can;
908 
909  FT = S.ResolveExceptionSpec(E->getLocStart(), FT);
910  if (!FT)
911  return CT_Can;
912 
913  return FT->isNothrow(S.Context) ? CT_Cannot : CT_Can;
914 }
915 
917  if (DC->isTypeDependent())
918  return CT_Dependent;
919 
920  if (!DC->getTypeAsWritten()->isReferenceType())
921  return CT_Cannot;
922 
923  if (DC->getSubExpr()->isTypeDependent())
924  return CT_Dependent;
925 
926  return DC->getCastKind() == clang::CK_Dynamic? CT_Can : CT_Cannot;
927 }
928 
930  if (DC->isTypeOperand())
931  return CT_Cannot;
932 
933  Expr *Op = DC->getExprOperand();
934  if (Op->isTypeDependent())
935  return CT_Dependent;
936 
937  const RecordType *RT = Op->getType()->getAs<RecordType>();
938  if (!RT)
939  return CT_Cannot;
940 
941  if (!cast<CXXRecordDecl>(RT->getDecl())->isPolymorphic())
942  return CT_Cannot;
943 
944  if (Op->Classify(S.Context).isPRValue())
945  return CT_Cannot;
946 
947  return CT_Can;
948 }
949 
951  // C++ [expr.unary.noexcept]p3:
952  // [Can throw] if in a potentially-evaluated context the expression would
953  // contain:
954  switch (E->getStmtClass()) {
955  case Expr::CXXThrowExprClass:
956  // - a potentially evaluated throw-expression
957  return CT_Can;
958 
959  case Expr::CXXDynamicCastExprClass: {
960  // - a potentially evaluated dynamic_cast expression dynamic_cast<T>(v),
961  // where T is a reference type, that requires a run-time check
962  CanThrowResult CT = canDynamicCastThrow(cast<CXXDynamicCastExpr>(E));
963  if (CT == CT_Can)
964  return CT;
965  return mergeCanThrow(CT, canSubExprsThrow(*this, E));
966  }
967 
968  case Expr::CXXTypeidExprClass:
969  // - a potentially evaluated typeid expression applied to a glvalue
970  // expression whose type is a polymorphic class type
971  return canTypeidThrow(*this, cast<CXXTypeidExpr>(E));
972 
973  // - a potentially evaluated call to a function, member function, function
974  // pointer, or member function pointer that does not have a non-throwing
975  // exception-specification
976  case Expr::CallExprClass:
977  case Expr::CXXMemberCallExprClass:
978  case Expr::CXXOperatorCallExprClass:
979  case Expr::UserDefinedLiteralClass: {
980  const CallExpr *CE = cast<CallExpr>(E);
981  CanThrowResult CT;
982  if (E->isTypeDependent())
983  CT = CT_Dependent;
984  else if (isa<CXXPseudoDestructorExpr>(CE->getCallee()->IgnoreParens()))
985  CT = CT_Cannot;
986  else if (CE->getCalleeDecl())
987  CT = canCalleeThrow(*this, E, CE->getCalleeDecl());
988  else
989  CT = CT_Can;
990  if (CT == CT_Can)
991  return CT;
992  return mergeCanThrow(CT, canSubExprsThrow(*this, E));
993  }
994 
995  case Expr::CXXConstructExprClass:
996  case Expr::CXXTemporaryObjectExprClass: {
997  CanThrowResult CT = canCalleeThrow(*this, E,
998  cast<CXXConstructExpr>(E)->getConstructor());
999  if (CT == CT_Can)
1000  return CT;
1001  return mergeCanThrow(CT, canSubExprsThrow(*this, E));
1002  }
1003 
1004  case Expr::CXXInheritedCtorInitExprClass:
1005  return canCalleeThrow(*this, E,
1006  cast<CXXInheritedCtorInitExpr>(E)->getConstructor());
1007 
1008  case Expr::LambdaExprClass: {
1009  const LambdaExpr *Lambda = cast<LambdaExpr>(E);
1012  Cap = Lambda->capture_init_begin(),
1013  CapEnd = Lambda->capture_init_end();
1014  Cap != CapEnd; ++Cap)
1015  CT = mergeCanThrow(CT, canThrow(*Cap));
1016  return CT;
1017  }
1018 
1019  case Expr::CXXNewExprClass: {
1020  CanThrowResult CT;
1021  if (E->isTypeDependent())
1022  CT = CT_Dependent;
1023  else
1024  CT = canCalleeThrow(*this, E, cast<CXXNewExpr>(E)->getOperatorNew());
1025  if (CT == CT_Can)
1026  return CT;
1027  return mergeCanThrow(CT, canSubExprsThrow(*this, E));
1028  }
1029 
1030  case Expr::CXXDeleteExprClass: {
1031  CanThrowResult CT;
1032  QualType DTy = cast<CXXDeleteExpr>(E)->getDestroyedType();
1033  if (DTy.isNull() || DTy->isDependentType()) {
1034  CT = CT_Dependent;
1035  } else {
1036  CT = canCalleeThrow(*this, E,
1037  cast<CXXDeleteExpr>(E)->getOperatorDelete());
1038  if (const RecordType *RT = DTy->getAs<RecordType>()) {
1039  const CXXRecordDecl *RD = cast<CXXRecordDecl>(RT->getDecl());
1040  const CXXDestructorDecl *DD = RD->getDestructor();
1041  if (DD)
1042  CT = mergeCanThrow(CT, canCalleeThrow(*this, E, DD));
1043  }
1044  if (CT == CT_Can)
1045  return CT;
1046  }
1047  return mergeCanThrow(CT, canSubExprsThrow(*this, E));
1048  }
1049 
1050  case Expr::CXXBindTemporaryExprClass: {
1051  // The bound temporary has to be destroyed again, which might throw.
1052  CanThrowResult CT = canCalleeThrow(*this, E,
1053  cast<CXXBindTemporaryExpr>(E)->getTemporary()->getDestructor());
1054  if (CT == CT_Can)
1055  return CT;
1056  return mergeCanThrow(CT, canSubExprsThrow(*this, E));
1057  }
1058 
1059  // ObjC message sends are like function calls, but never have exception
1060  // specs.
1061  case Expr::ObjCMessageExprClass:
1062  case Expr::ObjCPropertyRefExprClass:
1063  case Expr::ObjCSubscriptRefExprClass:
1064  return CT_Can;
1065 
1066  // All the ObjC literals that are implemented as calls are
1067  // potentially throwing unless we decide to close off that
1068  // possibility.
1069  case Expr::ObjCArrayLiteralClass:
1070  case Expr::ObjCDictionaryLiteralClass:
1071  case Expr::ObjCBoxedExprClass:
1072  return CT_Can;
1073 
1074  // Many other things have subexpressions, so we have to test those.
1075  // Some are simple:
1076  case Expr::CoawaitExprClass:
1077  case Expr::ConditionalOperatorClass:
1078  case Expr::CompoundLiteralExprClass:
1079  case Expr::CoyieldExprClass:
1080  case Expr::CXXConstCastExprClass:
1081  case Expr::CXXReinterpretCastExprClass:
1082  case Expr::CXXStdInitializerListExprClass:
1083  case Expr::DesignatedInitExprClass:
1084  case Expr::DesignatedInitUpdateExprClass:
1085  case Expr::ExprWithCleanupsClass:
1086  case Expr::ExtVectorElementExprClass:
1087  case Expr::InitListExprClass:
1088  case Expr::MemberExprClass:
1089  case Expr::ObjCIsaExprClass:
1090  case Expr::ObjCIvarRefExprClass:
1091  case Expr::ParenExprClass:
1092  case Expr::ParenListExprClass:
1093  case Expr::ShuffleVectorExprClass:
1094  case Expr::ConvertVectorExprClass:
1095  case Expr::VAArgExprClass:
1096  return canSubExprsThrow(*this, E);
1097 
1098  // Some might be dependent for other reasons.
1099  case Expr::ArraySubscriptExprClass:
1100  case Expr::OMPArraySectionExprClass:
1101  case Expr::BinaryOperatorClass:
1102  case Expr::CompoundAssignOperatorClass:
1103  case Expr::CStyleCastExprClass:
1104  case Expr::CXXStaticCastExprClass:
1105  case Expr::CXXFunctionalCastExprClass:
1106  case Expr::ImplicitCastExprClass:
1107  case Expr::MaterializeTemporaryExprClass:
1108  case Expr::UnaryOperatorClass: {
1110  return mergeCanThrow(CT, canSubExprsThrow(*this, E));
1111  }
1112 
1113  // FIXME: We should handle StmtExpr, but that opens a MASSIVE can of worms.
1114  case Expr::StmtExprClass:
1115  return CT_Can;
1116 
1117  case Expr::CXXDefaultArgExprClass:
1118  return canThrow(cast<CXXDefaultArgExpr>(E)->getExpr());
1119 
1120  case Expr::CXXDefaultInitExprClass:
1121  return canThrow(cast<CXXDefaultInitExpr>(E)->getExpr());
1122 
1123  case Expr::ChooseExprClass:
1124  if (E->isTypeDependent() || E->isValueDependent())
1125  return CT_Dependent;
1126  return canThrow(cast<ChooseExpr>(E)->getChosenSubExpr());
1127 
1128  case Expr::GenericSelectionExprClass:
1129  if (cast<GenericSelectionExpr>(E)->isResultDependent())
1130  return CT_Dependent;
1131  return canThrow(cast<GenericSelectionExpr>(E)->getResultExpr());
1132 
1133  // Some expressions are always dependent.
1134  case Expr::CXXDependentScopeMemberExprClass:
1135  case Expr::CXXUnresolvedConstructExprClass:
1136  case Expr::DependentScopeDeclRefExprClass:
1137  case Expr::CXXFoldExprClass:
1138  return CT_Dependent;
1139 
1140  case Expr::AsTypeExprClass:
1141  case Expr::BinaryConditionalOperatorClass:
1142  case Expr::BlockExprClass:
1143  case Expr::CUDAKernelCallExprClass:
1144  case Expr::DeclRefExprClass:
1145  case Expr::ObjCBridgedCastExprClass:
1146  case Expr::ObjCIndirectCopyRestoreExprClass:
1147  case Expr::ObjCProtocolExprClass:
1148  case Expr::ObjCSelectorExprClass:
1149  case Expr::ObjCAvailabilityCheckExprClass:
1150  case Expr::OffsetOfExprClass:
1151  case Expr::PackExpansionExprClass:
1152  case Expr::PseudoObjectExprClass:
1153  case Expr::SubstNonTypeTemplateParmExprClass:
1154  case Expr::SubstNonTypeTemplateParmPackExprClass:
1155  case Expr::FunctionParmPackExprClass:
1156  case Expr::UnaryExprOrTypeTraitExprClass:
1157  case Expr::UnresolvedLookupExprClass:
1158  case Expr::UnresolvedMemberExprClass:
1159  case Expr::TypoExprClass:
1160  // FIXME: Can any of the above throw? If so, when?
1161  return CT_Cannot;
1162 
1163  case Expr::AddrLabelExprClass:
1164  case Expr::ArrayTypeTraitExprClass:
1165  case Expr::AtomicExprClass:
1166  case Expr::TypeTraitExprClass:
1167  case Expr::CXXBoolLiteralExprClass:
1168  case Expr::CXXNoexceptExprClass:
1169  case Expr::CXXNullPtrLiteralExprClass:
1170  case Expr::CXXPseudoDestructorExprClass:
1171  case Expr::CXXScalarValueInitExprClass:
1172  case Expr::CXXThisExprClass:
1173  case Expr::CXXUuidofExprClass:
1174  case Expr::CharacterLiteralClass:
1175  case Expr::ExpressionTraitExprClass:
1176  case Expr::FloatingLiteralClass:
1177  case Expr::GNUNullExprClass:
1178  case Expr::ImaginaryLiteralClass:
1179  case Expr::ImplicitValueInitExprClass:
1180  case Expr::IntegerLiteralClass:
1181  case Expr::NoInitExprClass:
1182  case Expr::ObjCEncodeExprClass:
1183  case Expr::ObjCStringLiteralClass:
1184  case Expr::ObjCBoolLiteralExprClass:
1185  case Expr::OpaqueValueExprClass:
1186  case Expr::PredefinedExprClass:
1187  case Expr::SizeOfPackExprClass:
1188  case Expr::StringLiteralClass:
1189  // These expressions can never throw.
1190  return CT_Cannot;
1191 
1192  case Expr::MSPropertyRefExprClass:
1193  case Expr::MSPropertySubscriptExprClass:
1194  llvm_unreachable("Invalid class for expression");
1195 
1196 #define STMT(CLASS, PARENT) case Expr::CLASS##Class:
1197 #define STMT_RANGE(Base, First, Last)
1198 #define LAST_STMT_RANGE(BASE, FIRST, LAST)
1199 #define EXPR(CLASS, PARENT)
1200 #define ABSTRACT_STMT(STMT)
1201 #include "clang/AST/StmtNodes.inc"
1202  case Expr::NoStmtClass:
1203  llvm_unreachable("Invalid class for expression");
1204  }
1205  llvm_unreachable("Bogus StmtClass");
1206 }
1207 
1208 } // end namespace clang
CanThrowResult canThrow(const Expr *E)
T getAs() const
Convert to the specified TypeLoc type, returning a null TypeLoc if this TypeLoc is not of the desired...
Definition: TypeLoc.h:64
CastKind getCastKind() const
Definition: Expr.h:2680
FunctionDecl - An instance of this class is created to represent a function declaration or definition...
Definition: Decl.h:1561
no exception specification
PointerType - C99 6.7.5.1 - Pointer Declarators.
Definition: Type.h:2179
A (possibly-)qualified type.
Definition: Type.h:598
bool CheckExceptionSpecCompatibility(Expr *From, QualType ToType)
bool isReplaceableGlobalAllocationFunction() const
Determines whether this function is one of the replaceable global allocation functions: void *operato...
Definition: Decl.cpp:2578
void UpdateExceptionSpec(FunctionDecl *FD, const FunctionProtoType::ExceptionSpecInfo &ESI)
void InstantiateExceptionSpec(SourceLocation PointOfInstantiation, FunctionDecl *Function)
FunctionDecl * getExceptionSpecDecl() const
If this function type has an exception specification which hasn't been determined yet (either because...
Definition: Type.h:3346
const FunctionProtoType * ResolveExceptionSpec(SourceLocation Loc, const FunctionProtoType *FPT)
IdentifierInfo * getIdentifier() const
getIdentifier - Get the identifier that names this declaration, if there is one.
Definition: Decl.h:232
const LangOptions & getLangOpts() const
Definition: Sema.h:1062
CanQual< T > getUnqualifiedType() const
Retrieve the unqualified form of this type.
Defines the SourceManager interface.
bool isRecordType() const
Definition: Type.h:5539
SemaDiagnosticBuilder Diag(SourceLocation Loc, unsigned DiagID)
Emit a diagnostic.
Definition: Sema.h:1139
void EvaluateImplicitExceptionSpec(SourceLocation Loc, CXXMethodDecl *MD)
Evaluate the implicit exception specification for a defaulted special member function.
SourceLocation getLocForEndOfToken(SourceLocation Loc, unsigned Offset=0)
Calls Lexer::getLocForEndOfToken()
Definition: Sema.cpp:46
A container of type source information.
Definition: Decl.h:62
Expr *const * const_capture_init_iterator
Const iterator that walks over the capture initialization arguments.
Definition: ExprCXX.h:1662
PartialDiagnostic PDiag(unsigned DiagID=0)
Build a partial diagnostic.
Definition: SemaInternal.h:25
The noexcept specifier has a bad expression.
Definition: Type.h:3324
Information about one declarator, including the parsed type information and the identifier.
Definition: DeclSpec.h:1624
const Expr * getCallee() const
Definition: Expr.h:2188
void clear()
Clear the base-paths results.
Defines the clang::Expr interface and subclasses for C++ expressions.
bool isVoidType() const
Definition: Type.h:5680
QualType getArrayDecayedType(QualType T) const
Return the properly qualified result of decaying the specified array type to a pointer.
Base wrapper for a particular "section" of type source info.
Definition: TypeLoc.h:40
unsigned getNumParams() const
Definition: Type.h:3271
bool isExternC() const
Determines whether this function is a function with external, C linkage.
Definition: Decl.cpp:2623
One of these records is kept for each identifier that is lexed.
class LLVM_ALIGNAS(8) DependentTemplateSpecializationType const IdentifierInfo * Name
Represents a template specialization type whose template cannot be resolved, e.g. ...
Definition: Type.h:4549
ArrayRef< QualType > getParamTypes() const
Definition: Type.h:3276
bool IsDerivedFrom(SourceLocation Loc, QualType Derived, QualType Base)
Determine whether the type Derived is a C++ class that is derived from the type Base.
bool isReferenceType() const
Definition: Type.h:5491
NoexceptResult
Result type of getNoexceptSpec().
Definition: Type.h:3322
bool isPRValue() const
Definition: Expr.h:351
SourceLocation getLocStart() const LLVM_READONLY
Definition: DeclSpec.h:1761
Expr * getSubExpr()
Definition: Expr.h:2684
A C++ typeid expression (C++ [expr.typeid]), which gets the type_info that corresponds to the supplie...
Definition: ExprCXX.h:575
Microsoft throw(...) extension.
unsigned getDiagID() const
QualType getReturnType() const
Definition: Type.h:3009
const CXXRecordDecl * getParent() const
Returns the parent of this method declaration, which is the class in which this method is defined...
Definition: DeclCXX.h:1838
bool CheckOverridingFunctionExceptionSpec(const CXXMethodDecl *New, const CXXMethodDecl *Old)
CheckOverridingFunctionExceptionSpec - Checks whether the exception spec is a subset of base spec...
capture_init_iterator capture_init_begin()
Retrieve the first initialization argument for this lambda expression (which initializes the first ca...
Definition: ExprCXX.h:1676
Expr * getExprOperand() const
Definition: ExprCXX.h:630
Expr * getNoexceptExpr() const
Definition: Type.h:3336
bool isValueDependent() const
isValueDependent - Determines whether this expression is value-dependent (C++ [temp.dep.constexpr]).
Definition: Expr.h:147
RecordDecl * getDecl() const
Definition: Type.h:3716
static bool CheckSpecForTypesEquivalent(Sema &S, const PartialDiagnostic &DiagID, const PartialDiagnostic &NoteID, QualType Target, SourceLocation TargetLoc, QualType Source, SourceLocation SourceLoc)
QualType getTypeAsWritten() const
getTypeAsWritten - Returns the type that this expression is casting to, as written in the source code...
Definition: Expr.h:2823
A C++ lambda expression, which produces a function object (of unspecified type) that can be invoked l...
Definition: ExprCXX.h:1503
detail::InMemoryDirectory::const_iterator I
QualType getType() const
Definition: Decl.h:599
bool isInvalid() const
Sema - This implements semantic analysis and AST building for C.
Definition: Sema.h:263
QualType getParamType(unsigned i) const
Definition: Type.h:3272
Represents a prototype with parameter type info, e.g.
Definition: Type.h:3073
ExceptionSpecificationType getExceptionSpecType() const
Get the kind of exception specification on this function.
Definition: Type.h:3304
CanThrowResult mergeCanThrow(CanThrowResult CT1, CanThrowResult CT2)
QualType getPointeeType() const
If this is a pointer, ObjC object pointer, or block pointer, this returns the respective pointee...
Definition: Type.cpp:415
ValueDecl - Represent the declaration of a variable (in which case it is an lvalue) a function (in wh...
Definition: Decl.h:590
Expr - This represents one expression.
Definition: Expr.h:105
StringRef getName() const
Return the actual identifier string.
Represents a C++ destructor within a class.
Definition: DeclCXX.h:2414
ExtProtoInfo getExtProtoInfo() const
Definition: Type.h:3280
ExtProtoInfo withExceptionSpec(const ExceptionSpecInfo &O)
Definition: Type.h:3176
Classification Classify(ASTContext &Ctx) const
Classify - Classify this expression according to the C++11 expression taxonomy.
Definition: Expr.h:373
bool isInSystemHeader(SourceLocation Loc) const
Returns if a SourceLocation is in a system header.
bool RequireCompleteType(SourceLocation Loc, QualType T, TypeDiagnoser &Diagnoser)
Ensure that the type T is a complete type.
Definition: SemaType.cpp:6826
Defines the clang::TypeLoc interface and its subclasses.
There is no noexcept specifier.
Definition: Type.h:3323
ASTMutationListener * getASTMutationListener() const
Definition: Sema.cpp:317
bool isDependentType() const
Whether this type is a dependent type, meaning that its definition somehow depends on a template para...
Definition: Type.h:1774
DeclarationName getDeclName() const
getDeclName - Get the actual, stored name of the declaration, which may be a special name...
Definition: Decl.h:258
The result type of a method or function.
TypeSourceInfo * getTypeSourceInfo() const
Definition: Decl.h:671
static CanThrowResult canDynamicCastThrow(const CXXDynamicCastExpr *DC)
bool isNothrow(const ASTContext &Ctx, bool ResultIfDependent=false) const
Determine whether this function type has a non-throwing exception specification.
Definition: Type.cpp:2775
CanThrowResult
Possible results from evaluation of a noexcept expression.
exception_iterator exception_begin() const
Definition: Type.h:3404
A C++ dynamic_cast expression (C++ [expr.dynamic.cast]).
Definition: ExprCXX.h:290
redecl_range redecls() const
Returns an iterator range for all the redeclarations of the same decl.
Definition: Redeclarable.h:236
Kind
ExceptionSpecificationType Type
The kind of exception specification this is.
Definition: Type.h:3153
Encodes a location in the source.
bool CheckDistantExceptionSpec(QualType T)
CheckDistantExceptionSpec - Check if the given type is a pointer or pointer to member to a function w...
bool isValid() const
Return true if this is a valid SourceLocation object.
OverloadedOperatorKind getCXXOverloadedOperator() const
getCXXOverloadedOperator - If this name is the name of an overloadable operator in C++ (e...
Represents a static or instance method of a struct/union/class.
Definition: DeclCXX.h:1736
static bool hasImplicitExceptionSpec(FunctionDecl *Decl)
Determine whether a function has an implicitly-generated exception specification. ...
The noexcept specifier evaluates to true.
Definition: Type.h:3327
static CanThrowResult canTypeidThrow(Sema &S, const CXXTypeidExpr *DC)
bool hasDependentExceptionSpec() const
Return whether this function has a dependent exception spec.
Definition: Type.cpp:2739
SourceLocation getBegin() const
bool isTypeDependent() const
isTypeDependent - Determines whether this expression is type-dependent (C++ [temp.dep.expr]), which means that its type could change from one template instantiation to the next.
Definition: Expr.h:165
const T * castAs() const
Member-template castAs<specific type>.
Definition: Type.h:5849
NoexceptResult getNoexceptSpec(const ASTContext &Ctx) const
Get the meaning of the noexcept spec on this function, if any.
Definition: Type.cpp:2752
bool isLibstdcxxEagerExceptionSpecHack(const Declarator &D)
Determine if we're in a case where we need to (incorrectly) eagerly parse an exception specification ...
bool isDependentType() const
Whether this declaration declares a type that is dependent, i.e., a type that somehow depends on temp...
Definition: Decl.h:2901
QualType getType() const
Return the type wrapped by this type source info.
Definition: Decl.h:70
QualType getFunctionType(QualType ResultTy, ArrayRef< QualType > Args, const FunctionProtoType::ExtProtoInfo &EPI) const
Return a normal function type with a typed argument list.
bool isStr(const char(&Str)[StrLen]) const
Return true if this is the identifier for the specified string.
QualType getType() const
Definition: Expr.h:126
not evaluated yet, for special member function
OverloadedOperatorKind
Enumeration specifying the different kinds of C++ overloaded operators.
Definition: OperatorKinds.h:22
CXXDestructorDecl * getDestructor() const
Returns the destructor decl for this class.
Definition: DeclCXX.cpp:1375
TypeLoc IgnoreParens() const
Definition: TypeLoc.h:1056
QualType getPointerType(QualType T) const
Return the uniqued reference to the type for a pointer to the specified type.
detail::InMemoryDirectory::const_iterator E
bool isAmbiguous(CanQualType BaseType)
Determine whether the path from the most-derived type to the given base type is ambiguous (i...
A pointer to member type per C++ 8.3.3 - Pointers to members.
Definition: Type.h:2401
CanQualType getCanonicalType(QualType T) const
Return the canonical (structural) type corresponding to the specified potentially non-canonical type ...
Definition: ASTContext.h:1966
Defines the Diagnostic-related interfaces.
Decl * getCalleeDecl()
Definition: Expr.cpp:1185
Pointer to a block type.
Definition: Type.h:2286
QualType getLocalUnqualifiedType() const
Return this type with all of the instance-specific qualifiers removed, but without removing any quali...
Definition: Type.h:831
A helper class that allows the use of isa/cast/dyncast to detect TagType objects of structs/unions/cl...
Definition: Type.h:3707
const T * getAs() const
Member-template getAs<specific type>'.
Definition: Type.h:5818
PrintingPolicy getPrintingPolicy() const
Retrieve a suitable printing policy.
Definition: Sema.h:1912
bool CheckExceptionSpecSubset(const PartialDiagnostic &DiagID, const PartialDiagnostic &NoteID, const FunctionProtoType *Superset, SourceLocation SuperLoc, const FunctionProtoType *Subset, SourceLocation SubLoc)
CheckExceptionSpecSubset - Check whether the second function type's exception specification is a subs...
bool isFunctionType() const
Definition: Type.h:5479
CXXBasePath & front()
The noexcept specifier evaluates to false.
Definition: Type.h:3326
Base for LValueReferenceType and RValueReferenceType.
Definition: Type.h:2319
CXXRecordDecl * getAsCXXRecordDecl() const
Retrieves the CXXRecordDecl that this type refers to, either because the type is a RecordType or beca...
Definition: Type.cpp:1528
ExceptionSpecificationType
The various types of exception specifications that exist in C++11.
static FixItHint CreateInsertion(SourceLocation InsertionLoc, StringRef Code, bool BeforePreviousInsertions=false)
Create a code modification hint that inserts the given code string at a specific location.
Definition: Diagnostic.h:78
SourceManager & getSourceManager()
Definition: ASTContext.h:561
IdentifierInfo * getIdentifier() const
Definition: DeclSpec.h:1972
bool CheckParamExceptionSpec(const PartialDiagnostic &NoteID, const FunctionProtoType *Target, SourceLocation TargetLoc, const FunctionProtoType *Source, SourceLocation SourceLoc)
CheckParamExceptionSpec - Check if the parameter and return types of the two functions have equivalen...
Represents a C++ struct/union/class.
Definition: DeclCXX.h:263
bool CheckSpecifiedExceptionType(QualType &T, SourceRange Range)
CheckSpecifiedExceptionType - Check if the given type is valid in an exception specification.
AccessResult CheckBaseClassAccess(SourceLocation AccessLoc, QualType Base, QualType Derived, const CXXBasePath &Path, unsigned DiagID, bool ForceCheck=false, bool ForceUnprivileged=false)
Checks access for a hierarchy conversion.
DeclContext * CurContext
CurContext - This is the current declaration context of parsing.
Definition: Sema.h:311
bool isArrayType() const
Definition: Type.h:5521
CallExpr - Represents a function call (C99 6.5.2.2, C++ [expr.call]).
Definition: Expr.h:2148
void adjustExceptionSpec(FunctionDecl *FD, const FunctionProtoType::ExceptionSpecInfo &ESI, bool AsWritten=false)
Change the exception specification on a function once it is delay-parsed, instantiated, or computed.
static CanThrowResult canCalleeThrow(Sema &S, const Expr *E, const Decl *D)
capture_init_iterator capture_init_end()
Retrieve the iterator pointing one past the last initialization argument for this lambda expression...
Definition: ExprCXX.h:1688
BasePaths - Represents the set of paths from a derived class to one of its (direct or indirect) bases...
bool hasExceptionSpec() const
Return whether this function has any kind of exception spec.
Definition: Type.h:3308
bool isUnresolvedExceptionSpec(ExceptionSpecificationType ESpecType)
A trivial tuple used to represent a source range.
ASTContext & Context
Definition: Sema.h:299
static CanThrowResult canSubExprsThrow(Sema &S, const Expr *E)
SmallVector< std::pair< const CXXMethodDecl *, const CXXMethodDecl * >, 2 > DelayedExceptionSpecChecks
All the overriding functions seen during a class definition that had their exception spec checks dela...
Definition: Sema.h:540
bool isNull() const
Return true if this QualType doesn't point to a type yet.
Definition: Type.h:665
bool isTypeOperand() const
Definition: ExprCXX.h:613
The noexcept specifier is dependent.
Definition: Type.h:3325
bool CheckEquivalentExceptionSpec(FunctionDecl *Old, FunctionDecl *New)
NamespaceDecl * getStdNamespace() const
void setType(QualType newType)
Definition: Decl.h:600
ArrayRef< QualType > exceptions() const
Definition: Type.h:3401
bool isBeingDefined() const
isBeingDefined - Return true if this decl is currently being defined.
Definition: Decl.h:2882
static const FunctionProtoType * GetUnderlyingFunction(QualType T)
Expr * IgnoreParens() LLVM_READONLY
IgnoreParens - Ignore parentheses.
Definition: Expr.cpp:2295
unsigned getNumExceptions() const
Definition: Type.h:3331