SemaExceptionSpec.cpp 40 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086
  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. #include "clang/Sema/SemaInternal.h"
  14. #include "clang/AST/CXXInheritance.h"
  15. #include "clang/AST/Expr.h"
  16. #include "clang/AST/ExprCXX.h"
  17. #include "clang/AST/TypeLoc.h"
  18. #include "clang/Lex/Preprocessor.h"
  19. #include "clang/Basic/Diagnostic.h"
  20. #include "clang/Basic/SourceManager.h"
  21. #include "llvm/ADT/SmallPtrSet.h"
  22. #include "llvm/ADT/SmallString.h"
  23. namespace clang {
  24. static const FunctionProtoType *GetUnderlyingFunction(QualType T)
  25. {
  26. if (const PointerType *PtrTy = T->getAs<PointerType>())
  27. T = PtrTy->getPointeeType();
  28. else if (const ReferenceType *RefTy = T->getAs<ReferenceType>())
  29. T = RefTy->getPointeeType();
  30. else if (const MemberPointerType *MPTy = T->getAs<MemberPointerType>())
  31. T = MPTy->getPointeeType();
  32. return T->getAs<FunctionProtoType>();
  33. }
  34. /// CheckSpecifiedExceptionType - Check if the given type is valid in an
  35. /// exception specification. Incomplete types, or pointers to incomplete types
  36. /// other than void are not allowed.
  37. bool Sema::CheckSpecifiedExceptionType(QualType T, const SourceRange &Range) {
  38. // This check (and the similar one below) deals with issue 437, that changes
  39. // C++ 9.2p2 this way:
  40. // Within the class member-specification, the class is regarded as complete
  41. // within function bodies, default arguments, exception-specifications, and
  42. // constructor ctor-initializers (including such things in nested classes).
  43. if (T->isRecordType() && T->getAs<RecordType>()->isBeingDefined())
  44. return false;
  45. // C++ 15.4p2: A type denoted in an exception-specification shall not denote
  46. // an incomplete type.
  47. if (RequireCompleteType(Range.getBegin(), T,
  48. PDiag(diag::err_incomplete_in_exception_spec) << /*direct*/0 << Range))
  49. return true;
  50. // C++ 15.4p2: A type denoted in an exception-specification shall not denote
  51. // an incomplete type a pointer or reference to an incomplete type, other
  52. // than (cv) void*.
  53. int kind;
  54. if (const PointerType* IT = T->getAs<PointerType>()) {
  55. T = IT->getPointeeType();
  56. kind = 1;
  57. } else if (const ReferenceType* IT = T->getAs<ReferenceType>()) {
  58. T = IT->getPointeeType();
  59. kind = 2;
  60. } else
  61. return false;
  62. // Again as before
  63. if (T->isRecordType() && T->getAs<RecordType>()->isBeingDefined())
  64. return false;
  65. if (!T->isVoidType() && RequireCompleteType(Range.getBegin(), T,
  66. PDiag(diag::err_incomplete_in_exception_spec) << kind << Range))
  67. return true;
  68. return false;
  69. }
  70. /// CheckDistantExceptionSpec - Check if the given type is a pointer or pointer
  71. /// to member to a function with an exception specification. This means that
  72. /// it is invalid to add another level of indirection.
  73. bool Sema::CheckDistantExceptionSpec(QualType T) {
  74. if (const PointerType *PT = T->getAs<PointerType>())
  75. T = PT->getPointeeType();
  76. else if (const MemberPointerType *PT = T->getAs<MemberPointerType>())
  77. T = PT->getPointeeType();
  78. else
  79. return false;
  80. const FunctionProtoType *FnT = T->getAs<FunctionProtoType>();
  81. if (!FnT)
  82. return false;
  83. return FnT->hasExceptionSpec();
  84. }
  85. const FunctionProtoType *
  86. Sema::ResolveExceptionSpec(SourceLocation Loc, const FunctionProtoType *FPT) {
  87. // FIXME: If FD is a special member, we should delay computing its exception
  88. // specification until this point.
  89. if (FPT->getExceptionSpecType() != EST_Uninstantiated)
  90. return FPT;
  91. FunctionDecl *SourceDecl = FPT->getExceptionSpecDecl();
  92. const FunctionProtoType *SourceFPT =
  93. SourceDecl->getType()->castAs<FunctionProtoType>();
  94. if (SourceFPT->getExceptionSpecType() != EST_Uninstantiated)
  95. return SourceFPT;
  96. // Instantiate the exception specification now.
  97. InstantiateExceptionSpec(Loc, SourceDecl);
  98. return SourceDecl->getType()->castAs<FunctionProtoType>();
  99. }
  100. bool Sema::CheckEquivalentExceptionSpec(FunctionDecl *Old, FunctionDecl *New) {
  101. OverloadedOperatorKind OO = New->getDeclName().getCXXOverloadedOperator();
  102. bool IsOperatorNew = OO == OO_New || OO == OO_Array_New;
  103. bool MissingExceptionSpecification = false;
  104. bool MissingEmptyExceptionSpecification = false;
  105. unsigned DiagID = diag::err_mismatched_exception_spec;
  106. if (getLangOpts().MicrosoftExt)
  107. DiagID = diag::warn_mismatched_exception_spec;
  108. if (!CheckEquivalentExceptionSpec(PDiag(DiagID),
  109. PDiag(diag::note_previous_declaration),
  110. Old->getType()->getAs<FunctionProtoType>(),
  111. Old->getLocation(),
  112. New->getType()->getAs<FunctionProtoType>(),
  113. New->getLocation(),
  114. &MissingExceptionSpecification,
  115. &MissingEmptyExceptionSpecification,
  116. /*AllowNoexceptAllMatchWithNoSpec=*/true,
  117. IsOperatorNew))
  118. return false;
  119. // The failure was something other than an empty exception
  120. // specification; return an error.
  121. if (!MissingExceptionSpecification && !MissingEmptyExceptionSpecification)
  122. return true;
  123. const FunctionProtoType *NewProto
  124. = New->getType()->getAs<FunctionProtoType>();
  125. // The new function declaration is only missing an empty exception
  126. // specification "throw()". If the throw() specification came from a
  127. // function in a system header that has C linkage, just add an empty
  128. // exception specification to the "new" declaration. This is an
  129. // egregious workaround for glibc, which adds throw() specifications
  130. // to many libc functions as an optimization. Unfortunately, that
  131. // optimization isn't permitted by the C++ standard, so we're forced
  132. // to work around it here.
  133. if (MissingEmptyExceptionSpecification && NewProto &&
  134. (Old->getLocation().isInvalid() ||
  135. Context.getSourceManager().isInSystemHeader(Old->getLocation())) &&
  136. Old->isExternC()) {
  137. FunctionProtoType::ExtProtoInfo EPI = NewProto->getExtProtoInfo();
  138. EPI.ExceptionSpecType = EST_DynamicNone;
  139. QualType NewType = Context.getFunctionType(NewProto->getResultType(),
  140. NewProto->arg_type_begin(),
  141. NewProto->getNumArgs(),
  142. EPI);
  143. New->setType(NewType);
  144. return false;
  145. }
  146. if (MissingExceptionSpecification && NewProto) {
  147. const FunctionProtoType *OldProto
  148. = Old->getType()->getAs<FunctionProtoType>();
  149. FunctionProtoType::ExtProtoInfo EPI = NewProto->getExtProtoInfo();
  150. EPI.ExceptionSpecType = OldProto->getExceptionSpecType();
  151. if (EPI.ExceptionSpecType == EST_Dynamic) {
  152. EPI.NumExceptions = OldProto->getNumExceptions();
  153. EPI.Exceptions = OldProto->exception_begin();
  154. } else if (EPI.ExceptionSpecType == EST_ComputedNoexcept) {
  155. // FIXME: We can't just take the expression from the old prototype. It
  156. // likely contains references to the old prototype's parameters.
  157. }
  158. // Update the type of the function with the appropriate exception
  159. // specification.
  160. QualType NewType = Context.getFunctionType(NewProto->getResultType(),
  161. NewProto->arg_type_begin(),
  162. NewProto->getNumArgs(),
  163. EPI);
  164. New->setType(NewType);
  165. // If exceptions are disabled, suppress the warning about missing
  166. // exception specifications for new and delete operators.
  167. if (!getLangOpts().CXXExceptions) {
  168. switch (New->getDeclName().getCXXOverloadedOperator()) {
  169. case OO_New:
  170. case OO_Array_New:
  171. case OO_Delete:
  172. case OO_Array_Delete:
  173. if (New->getDeclContext()->isTranslationUnit())
  174. return false;
  175. break;
  176. default:
  177. break;
  178. }
  179. }
  180. // Warn about the lack of exception specification.
  181. SmallString<128> ExceptionSpecString;
  182. llvm::raw_svector_ostream OS(ExceptionSpecString);
  183. switch (OldProto->getExceptionSpecType()) {
  184. case EST_DynamicNone:
  185. OS << "throw()";
  186. break;
  187. case EST_Dynamic: {
  188. OS << "throw(";
  189. bool OnFirstException = true;
  190. for (FunctionProtoType::exception_iterator E = OldProto->exception_begin(),
  191. EEnd = OldProto->exception_end();
  192. E != EEnd;
  193. ++E) {
  194. if (OnFirstException)
  195. OnFirstException = false;
  196. else
  197. OS << ", ";
  198. OS << E->getAsString(getPrintingPolicy());
  199. }
  200. OS << ")";
  201. break;
  202. }
  203. case EST_BasicNoexcept:
  204. OS << "noexcept";
  205. break;
  206. case EST_ComputedNoexcept:
  207. OS << "noexcept(";
  208. OldProto->getNoexceptExpr()->printPretty(OS, Context, 0,
  209. getPrintingPolicy());
  210. OS << ")";
  211. break;
  212. default:
  213. llvm_unreachable("This spec type is compatible with none.");
  214. }
  215. OS.flush();
  216. SourceLocation FixItLoc;
  217. if (TypeSourceInfo *TSInfo = New->getTypeSourceInfo()) {
  218. TypeLoc TL = TSInfo->getTypeLoc().IgnoreParens();
  219. if (const FunctionTypeLoc *FTLoc = dyn_cast<FunctionTypeLoc>(&TL))
  220. FixItLoc = PP.getLocForEndOfToken(FTLoc->getLocalRangeEnd());
  221. }
  222. if (FixItLoc.isInvalid())
  223. Diag(New->getLocation(), diag::warn_missing_exception_specification)
  224. << New << OS.str();
  225. else {
  226. // FIXME: This will get more complicated with C++0x
  227. // late-specified return types.
  228. Diag(New->getLocation(), diag::warn_missing_exception_specification)
  229. << New << OS.str()
  230. << FixItHint::CreateInsertion(FixItLoc, " " + OS.str().str());
  231. }
  232. if (!Old->getLocation().isInvalid())
  233. Diag(Old->getLocation(), diag::note_previous_declaration);
  234. return false;
  235. }
  236. Diag(New->getLocation(), DiagID);
  237. Diag(Old->getLocation(), diag::note_previous_declaration);
  238. return true;
  239. }
  240. /// CheckEquivalentExceptionSpec - Check if the two types have equivalent
  241. /// exception specifications. Exception specifications are equivalent if
  242. /// they allow exactly the same set of exception types. It does not matter how
  243. /// that is achieved. See C++ [except.spec]p2.
  244. bool Sema::CheckEquivalentExceptionSpec(
  245. const FunctionProtoType *Old, SourceLocation OldLoc,
  246. const FunctionProtoType *New, SourceLocation NewLoc) {
  247. unsigned DiagID = diag::err_mismatched_exception_spec;
  248. if (getLangOpts().MicrosoftExt)
  249. DiagID = diag::warn_mismatched_exception_spec;
  250. return CheckEquivalentExceptionSpec(
  251. PDiag(DiagID),
  252. PDiag(diag::note_previous_declaration),
  253. Old, OldLoc, New, NewLoc);
  254. }
  255. /// CheckEquivalentExceptionSpec - Check if the two types have compatible
  256. /// exception specifications. See C++ [except.spec]p3.
  257. bool Sema::CheckEquivalentExceptionSpec(const PartialDiagnostic &DiagID,
  258. const PartialDiagnostic & NoteID,
  259. const FunctionProtoType *Old,
  260. SourceLocation OldLoc,
  261. const FunctionProtoType *New,
  262. SourceLocation NewLoc,
  263. bool *MissingExceptionSpecification,
  264. bool*MissingEmptyExceptionSpecification,
  265. bool AllowNoexceptAllMatchWithNoSpec,
  266. bool IsOperatorNew) {
  267. // Just completely ignore this under -fno-exceptions.
  268. if (!getLangOpts().CXXExceptions)
  269. return false;
  270. if (MissingExceptionSpecification)
  271. *MissingExceptionSpecification = false;
  272. if (MissingEmptyExceptionSpecification)
  273. *MissingEmptyExceptionSpecification = false;
  274. Old = ResolveExceptionSpec(NewLoc, Old);
  275. if (!Old)
  276. return false;
  277. New = ResolveExceptionSpec(NewLoc, New);
  278. if (!New)
  279. return false;
  280. // C++0x [except.spec]p3: Two exception-specifications are compatible if:
  281. // - both are non-throwing, regardless of their form,
  282. // - both have the form noexcept(constant-expression) and the constant-
  283. // expressions are equivalent,
  284. // - both are dynamic-exception-specifications that have the same set of
  285. // adjusted types.
  286. //
  287. // C++0x [except.spec]p12: An exception-specifcation is non-throwing if it is
  288. // of the form throw(), noexcept, or noexcept(constant-expression) where the
  289. // constant-expression yields true.
  290. //
  291. // C++0x [except.spec]p4: If any declaration of a function has an exception-
  292. // specifier that is not a noexcept-specification allowing all exceptions,
  293. // all declarations [...] of that function shall have a compatible
  294. // exception-specification.
  295. //
  296. // That last point basically means that noexcept(false) matches no spec.
  297. // It's considered when AllowNoexceptAllMatchWithNoSpec is true.
  298. ExceptionSpecificationType OldEST = Old->getExceptionSpecType();
  299. ExceptionSpecificationType NewEST = New->getExceptionSpecType();
  300. assert(OldEST != EST_Delayed && NewEST != EST_Delayed &&
  301. OldEST != EST_Uninstantiated && NewEST != EST_Uninstantiated &&
  302. "Shouldn't see unknown exception specifications here");
  303. // Shortcut the case where both have no spec.
  304. if (OldEST == EST_None && NewEST == EST_None)
  305. return false;
  306. FunctionProtoType::NoexceptResult OldNR = Old->getNoexceptSpec(Context);
  307. FunctionProtoType::NoexceptResult NewNR = New->getNoexceptSpec(Context);
  308. if (OldNR == FunctionProtoType::NR_BadNoexcept ||
  309. NewNR == FunctionProtoType::NR_BadNoexcept)
  310. return false;
  311. // Dependent noexcept specifiers are compatible with each other, but nothing
  312. // else.
  313. // One noexcept is compatible with another if the argument is the same
  314. if (OldNR == NewNR &&
  315. OldNR != FunctionProtoType::NR_NoNoexcept &&
  316. NewNR != FunctionProtoType::NR_NoNoexcept)
  317. return false;
  318. if (OldNR != NewNR &&
  319. OldNR != FunctionProtoType::NR_NoNoexcept &&
  320. NewNR != FunctionProtoType::NR_NoNoexcept) {
  321. Diag(NewLoc, DiagID);
  322. if (NoteID.getDiagID() != 0)
  323. Diag(OldLoc, NoteID);
  324. return true;
  325. }
  326. // The MS extension throw(...) is compatible with itself.
  327. if (OldEST == EST_MSAny && NewEST == EST_MSAny)
  328. return false;
  329. // It's also compatible with no spec.
  330. if ((OldEST == EST_None && NewEST == EST_MSAny) ||
  331. (OldEST == EST_MSAny && NewEST == EST_None))
  332. return false;
  333. // It's also compatible with noexcept(false).
  334. if (OldEST == EST_MSAny && NewNR == FunctionProtoType::NR_Throw)
  335. return false;
  336. if (NewEST == EST_MSAny && OldNR == FunctionProtoType::NR_Throw)
  337. return false;
  338. // As described above, noexcept(false) matches no spec only for functions.
  339. if (AllowNoexceptAllMatchWithNoSpec) {
  340. if (OldEST == EST_None && NewNR == FunctionProtoType::NR_Throw)
  341. return false;
  342. if (NewEST == EST_None && OldNR == FunctionProtoType::NR_Throw)
  343. return false;
  344. }
  345. // Any non-throwing specifications are compatible.
  346. bool OldNonThrowing = OldNR == FunctionProtoType::NR_Nothrow ||
  347. OldEST == EST_DynamicNone;
  348. bool NewNonThrowing = NewNR == FunctionProtoType::NR_Nothrow ||
  349. NewEST == EST_DynamicNone;
  350. if (OldNonThrowing && NewNonThrowing)
  351. return false;
  352. // As a special compatibility feature, under C++0x we accept no spec and
  353. // throw(std::bad_alloc) as equivalent for operator new and operator new[].
  354. // This is because the implicit declaration changed, but old code would break.
  355. if (getLangOpts().CPlusPlus0x && IsOperatorNew) {
  356. const FunctionProtoType *WithExceptions = 0;
  357. if (OldEST == EST_None && NewEST == EST_Dynamic)
  358. WithExceptions = New;
  359. else if (OldEST == EST_Dynamic && NewEST == EST_None)
  360. WithExceptions = Old;
  361. if (WithExceptions && WithExceptions->getNumExceptions() == 1) {
  362. // One has no spec, the other throw(something). If that something is
  363. // std::bad_alloc, all conditions are met.
  364. QualType Exception = *WithExceptions->exception_begin();
  365. if (CXXRecordDecl *ExRecord = Exception->getAsCXXRecordDecl()) {
  366. IdentifierInfo* Name = ExRecord->getIdentifier();
  367. if (Name && Name->getName() == "bad_alloc") {
  368. // It's called bad_alloc, but is it in std?
  369. DeclContext* DC = ExRecord->getDeclContext();
  370. DC = DC->getEnclosingNamespaceContext();
  371. if (NamespaceDecl* NS = dyn_cast<NamespaceDecl>(DC)) {
  372. IdentifierInfo* NSName = NS->getIdentifier();
  373. DC = DC->getParent();
  374. if (NSName && NSName->getName() == "std" &&
  375. DC->getEnclosingNamespaceContext()->isTranslationUnit()) {
  376. return false;
  377. }
  378. }
  379. }
  380. }
  381. }
  382. }
  383. // At this point, the only remaining valid case is two matching dynamic
  384. // specifications. We return here unless both specifications are dynamic.
  385. if (OldEST != EST_Dynamic || NewEST != EST_Dynamic) {
  386. if (MissingExceptionSpecification && Old->hasExceptionSpec() &&
  387. !New->hasExceptionSpec()) {
  388. // The old type has an exception specification of some sort, but
  389. // the new type does not.
  390. *MissingExceptionSpecification = true;
  391. if (MissingEmptyExceptionSpecification && OldNonThrowing) {
  392. // The old type has a throw() or noexcept(true) exception specification
  393. // and the new type has no exception specification, and the caller asked
  394. // to handle this itself.
  395. *MissingEmptyExceptionSpecification = true;
  396. }
  397. return true;
  398. }
  399. Diag(NewLoc, DiagID);
  400. if (NoteID.getDiagID() != 0)
  401. Diag(OldLoc, NoteID);
  402. return true;
  403. }
  404. assert(OldEST == EST_Dynamic && NewEST == EST_Dynamic &&
  405. "Exception compatibility logic error: non-dynamic spec slipped through.");
  406. bool Success = true;
  407. // Both have a dynamic exception spec. Collect the first set, then compare
  408. // to the second.
  409. llvm::SmallPtrSet<CanQualType, 8> OldTypes, NewTypes;
  410. for (FunctionProtoType::exception_iterator I = Old->exception_begin(),
  411. E = Old->exception_end(); I != E; ++I)
  412. OldTypes.insert(Context.getCanonicalType(*I).getUnqualifiedType());
  413. for (FunctionProtoType::exception_iterator I = New->exception_begin(),
  414. E = New->exception_end(); I != E && Success; ++I) {
  415. CanQualType TypePtr = Context.getCanonicalType(*I).getUnqualifiedType();
  416. if(OldTypes.count(TypePtr))
  417. NewTypes.insert(TypePtr);
  418. else
  419. Success = false;
  420. }
  421. Success = Success && OldTypes.size() == NewTypes.size();
  422. if (Success) {
  423. return false;
  424. }
  425. Diag(NewLoc, DiagID);
  426. if (NoteID.getDiagID() != 0)
  427. Diag(OldLoc, NoteID);
  428. return true;
  429. }
  430. /// CheckExceptionSpecSubset - Check whether the second function type's
  431. /// exception specification is a subset (or equivalent) of the first function
  432. /// type. This is used by override and pointer assignment checks.
  433. bool Sema::CheckExceptionSpecSubset(
  434. const PartialDiagnostic &DiagID, const PartialDiagnostic & NoteID,
  435. const FunctionProtoType *Superset, SourceLocation SuperLoc,
  436. const FunctionProtoType *Subset, SourceLocation SubLoc) {
  437. // Just auto-succeed under -fno-exceptions.
  438. if (!getLangOpts().CXXExceptions)
  439. return false;
  440. // FIXME: As usual, we could be more specific in our error messages, but
  441. // that better waits until we've got types with source locations.
  442. if (!SubLoc.isValid())
  443. SubLoc = SuperLoc;
  444. // Resolve the exception specifications, if needed.
  445. Superset = ResolveExceptionSpec(SuperLoc, Superset);
  446. if (!Superset)
  447. return false;
  448. Subset = ResolveExceptionSpec(SubLoc, Subset);
  449. if (!Subset)
  450. return false;
  451. ExceptionSpecificationType SuperEST = Superset->getExceptionSpecType();
  452. // If superset contains everything, we're done.
  453. if (SuperEST == EST_None || SuperEST == EST_MSAny)
  454. return CheckParamExceptionSpec(NoteID, Superset, SuperLoc, Subset, SubLoc);
  455. // If there are dependent noexcept specs, assume everything is fine. Unlike
  456. // with the equivalency check, this is safe in this case, because we don't
  457. // want to merge declarations. Checks after instantiation will catch any
  458. // omissions we make here.
  459. // We also shortcut checking if a noexcept expression was bad.
  460. FunctionProtoType::NoexceptResult SuperNR =Superset->getNoexceptSpec(Context);
  461. if (SuperNR == FunctionProtoType::NR_BadNoexcept ||
  462. SuperNR == FunctionProtoType::NR_Dependent)
  463. return false;
  464. // Another case of the superset containing everything.
  465. if (SuperNR == FunctionProtoType::NR_Throw)
  466. return CheckParamExceptionSpec(NoteID, Superset, SuperLoc, Subset, SubLoc);
  467. ExceptionSpecificationType SubEST = Subset->getExceptionSpecType();
  468. assert(SuperEST != EST_Delayed && SubEST != EST_Delayed &&
  469. SuperEST != EST_Uninstantiated && SubEST != EST_Uninstantiated &&
  470. "Shouldn't see unknown exception specifications here");
  471. // It does not. If the subset contains everything, we've failed.
  472. if (SubEST == EST_None || SubEST == EST_MSAny) {
  473. Diag(SubLoc, DiagID);
  474. if (NoteID.getDiagID() != 0)
  475. Diag(SuperLoc, NoteID);
  476. return true;
  477. }
  478. FunctionProtoType::NoexceptResult SubNR = Subset->getNoexceptSpec(Context);
  479. if (SubNR == FunctionProtoType::NR_BadNoexcept ||
  480. SubNR == FunctionProtoType::NR_Dependent)
  481. return false;
  482. // Another case of the subset containing everything.
  483. if (SubNR == FunctionProtoType::NR_Throw) {
  484. Diag(SubLoc, DiagID);
  485. if (NoteID.getDiagID() != 0)
  486. Diag(SuperLoc, NoteID);
  487. return true;
  488. }
  489. // If the subset contains nothing, we're done.
  490. if (SubEST == EST_DynamicNone || SubNR == FunctionProtoType::NR_Nothrow)
  491. return CheckParamExceptionSpec(NoteID, Superset, SuperLoc, Subset, SubLoc);
  492. // Otherwise, if the superset contains nothing, we've failed.
  493. if (SuperEST == EST_DynamicNone || SuperNR == FunctionProtoType::NR_Nothrow) {
  494. Diag(SubLoc, DiagID);
  495. if (NoteID.getDiagID() != 0)
  496. Diag(SuperLoc, NoteID);
  497. return true;
  498. }
  499. assert(SuperEST == EST_Dynamic && SubEST == EST_Dynamic &&
  500. "Exception spec subset: non-dynamic case slipped through.");
  501. // Neither contains everything or nothing. Do a proper comparison.
  502. for (FunctionProtoType::exception_iterator SubI = Subset->exception_begin(),
  503. SubE = Subset->exception_end(); SubI != SubE; ++SubI) {
  504. // Take one type from the subset.
  505. QualType CanonicalSubT = Context.getCanonicalType(*SubI);
  506. // Unwrap pointers and references so that we can do checks within a class
  507. // hierarchy. Don't unwrap member pointers; they don't have hierarchy
  508. // conversions on the pointee.
  509. bool SubIsPointer = false;
  510. if (const ReferenceType *RefTy = CanonicalSubT->getAs<ReferenceType>())
  511. CanonicalSubT = RefTy->getPointeeType();
  512. if (const PointerType *PtrTy = CanonicalSubT->getAs<PointerType>()) {
  513. CanonicalSubT = PtrTy->getPointeeType();
  514. SubIsPointer = true;
  515. }
  516. bool SubIsClass = CanonicalSubT->isRecordType();
  517. CanonicalSubT = CanonicalSubT.getLocalUnqualifiedType();
  518. CXXBasePaths Paths(/*FindAmbiguities=*/true, /*RecordPaths=*/true,
  519. /*DetectVirtual=*/false);
  520. bool Contained = false;
  521. // Make sure it's in the superset.
  522. for (FunctionProtoType::exception_iterator SuperI =
  523. Superset->exception_begin(), SuperE = Superset->exception_end();
  524. SuperI != SuperE; ++SuperI) {
  525. QualType CanonicalSuperT = Context.getCanonicalType(*SuperI);
  526. // SubT must be SuperT or derived from it, or pointer or reference to
  527. // such types.
  528. if (const ReferenceType *RefTy = CanonicalSuperT->getAs<ReferenceType>())
  529. CanonicalSuperT = RefTy->getPointeeType();
  530. if (SubIsPointer) {
  531. if (const PointerType *PtrTy = CanonicalSuperT->getAs<PointerType>())
  532. CanonicalSuperT = PtrTy->getPointeeType();
  533. else {
  534. continue;
  535. }
  536. }
  537. CanonicalSuperT = CanonicalSuperT.getLocalUnqualifiedType();
  538. // If the types are the same, move on to the next type in the subset.
  539. if (CanonicalSubT == CanonicalSuperT) {
  540. Contained = true;
  541. break;
  542. }
  543. // Otherwise we need to check the inheritance.
  544. if (!SubIsClass || !CanonicalSuperT->isRecordType())
  545. continue;
  546. Paths.clear();
  547. if (!IsDerivedFrom(CanonicalSubT, CanonicalSuperT, Paths))
  548. continue;
  549. if (Paths.isAmbiguous(Context.getCanonicalType(CanonicalSuperT)))
  550. continue;
  551. // Do this check from a context without privileges.
  552. switch (CheckBaseClassAccess(SourceLocation(),
  553. CanonicalSuperT, CanonicalSubT,
  554. Paths.front(),
  555. /*Diagnostic*/ 0,
  556. /*ForceCheck*/ true,
  557. /*ForceUnprivileged*/ true)) {
  558. case AR_accessible: break;
  559. case AR_inaccessible: continue;
  560. case AR_dependent:
  561. llvm_unreachable("access check dependent for unprivileged context");
  562. case AR_delayed:
  563. llvm_unreachable("access check delayed in non-declaration");
  564. }
  565. Contained = true;
  566. break;
  567. }
  568. if (!Contained) {
  569. Diag(SubLoc, DiagID);
  570. if (NoteID.getDiagID() != 0)
  571. Diag(SuperLoc, NoteID);
  572. return true;
  573. }
  574. }
  575. // We've run half the gauntlet.
  576. return CheckParamExceptionSpec(NoteID, Superset, SuperLoc, Subset, SubLoc);
  577. }
  578. static bool CheckSpecForTypesEquivalent(Sema &S,
  579. const PartialDiagnostic &DiagID, const PartialDiagnostic & NoteID,
  580. QualType Target, SourceLocation TargetLoc,
  581. QualType Source, SourceLocation SourceLoc)
  582. {
  583. const FunctionProtoType *TFunc = GetUnderlyingFunction(Target);
  584. if (!TFunc)
  585. return false;
  586. const FunctionProtoType *SFunc = GetUnderlyingFunction(Source);
  587. if (!SFunc)
  588. return false;
  589. return S.CheckEquivalentExceptionSpec(DiagID, NoteID, TFunc, TargetLoc,
  590. SFunc, SourceLoc);
  591. }
  592. /// CheckParamExceptionSpec - Check if the parameter and return types of the
  593. /// two functions have equivalent exception specs. This is part of the
  594. /// assignment and override compatibility check. We do not check the parameters
  595. /// of parameter function pointers recursively, as no sane programmer would
  596. /// even be able to write such a function type.
  597. bool Sema::CheckParamExceptionSpec(const PartialDiagnostic & NoteID,
  598. const FunctionProtoType *Target, SourceLocation TargetLoc,
  599. const FunctionProtoType *Source, SourceLocation SourceLoc)
  600. {
  601. if (CheckSpecForTypesEquivalent(*this,
  602. PDiag(diag::err_deep_exception_specs_differ) << 0,
  603. PDiag(),
  604. Target->getResultType(), TargetLoc,
  605. Source->getResultType(), SourceLoc))
  606. return true;
  607. // We shouldn't even be testing this unless the arguments are otherwise
  608. // compatible.
  609. assert(Target->getNumArgs() == Source->getNumArgs() &&
  610. "Functions have different argument counts.");
  611. for (unsigned i = 0, E = Target->getNumArgs(); i != E; ++i) {
  612. if (CheckSpecForTypesEquivalent(*this,
  613. PDiag(diag::err_deep_exception_specs_differ) << 1,
  614. PDiag(),
  615. Target->getArgType(i), TargetLoc,
  616. Source->getArgType(i), SourceLoc))
  617. return true;
  618. }
  619. return false;
  620. }
  621. bool Sema::CheckExceptionSpecCompatibility(Expr *From, QualType ToType)
  622. {
  623. // First we check for applicability.
  624. // Target type must be a function, function pointer or function reference.
  625. const FunctionProtoType *ToFunc = GetUnderlyingFunction(ToType);
  626. if (!ToFunc)
  627. return false;
  628. // SourceType must be a function or function pointer.
  629. const FunctionProtoType *FromFunc = GetUnderlyingFunction(From->getType());
  630. if (!FromFunc)
  631. return false;
  632. // Now we've got the correct types on both sides, check their compatibility.
  633. // This means that the source of the conversion can only throw a subset of
  634. // the exceptions of the target, and any exception specs on arguments or
  635. // return types must be equivalent.
  636. return CheckExceptionSpecSubset(PDiag(diag::err_incompatible_exception_specs),
  637. PDiag(), ToFunc,
  638. From->getSourceRange().getBegin(),
  639. FromFunc, SourceLocation());
  640. }
  641. bool Sema::CheckOverridingFunctionExceptionSpec(const CXXMethodDecl *New,
  642. const CXXMethodDecl *Old) {
  643. if (getLangOpts().CPlusPlus0x && isa<CXXDestructorDecl>(New)) {
  644. // Don't check uninstantiated template destructors at all. We can only
  645. // synthesize correct specs after the template is instantiated.
  646. if (New->getParent()->isDependentType())
  647. return false;
  648. if (New->getParent()->isBeingDefined()) {
  649. // The destructor might be updated once the definition is finished. So
  650. // remember it and check later.
  651. DelayedDestructorExceptionSpecChecks.push_back(std::make_pair(
  652. cast<CXXDestructorDecl>(New), cast<CXXDestructorDecl>(Old)));
  653. return false;
  654. }
  655. }
  656. unsigned DiagID = diag::err_override_exception_spec;
  657. if (getLangOpts().MicrosoftExt)
  658. DiagID = diag::warn_override_exception_spec;
  659. return CheckExceptionSpecSubset(PDiag(DiagID),
  660. PDiag(diag::note_overridden_virtual_function),
  661. Old->getType()->getAs<FunctionProtoType>(),
  662. Old->getLocation(),
  663. New->getType()->getAs<FunctionProtoType>(),
  664. New->getLocation());
  665. }
  666. static CanThrowResult canSubExprsThrow(Sema &S, const Expr *CE) {
  667. Expr *E = const_cast<Expr*>(CE);
  668. CanThrowResult R = CT_Cannot;
  669. for (Expr::child_range I = E->children(); I && R != CT_Can; ++I)
  670. R = mergeCanThrow(R, S.canThrow(cast<Expr>(*I)));
  671. return R;
  672. }
  673. static CanThrowResult canCalleeThrow(Sema &S, const Expr *E,
  674. const Decl *D,
  675. bool NullThrows = true) {
  676. if (!D)
  677. return NullThrows ? CT_Can : CT_Cannot;
  678. // See if we can get a function type from the decl somehow.
  679. const ValueDecl *VD = dyn_cast<ValueDecl>(D);
  680. if (!VD) // If we have no clue what we're calling, assume the worst.
  681. return CT_Can;
  682. // As an extension, we assume that __attribute__((nothrow)) functions don't
  683. // throw.
  684. if (isa<FunctionDecl>(D) && D->hasAttr<NoThrowAttr>())
  685. return CT_Cannot;
  686. QualType T = VD->getType();
  687. const FunctionProtoType *FT;
  688. if ((FT = T->getAs<FunctionProtoType>())) {
  689. } else if (const PointerType *PT = T->getAs<PointerType>())
  690. FT = PT->getPointeeType()->getAs<FunctionProtoType>();
  691. else if (const ReferenceType *RT = T->getAs<ReferenceType>())
  692. FT = RT->getPointeeType()->getAs<FunctionProtoType>();
  693. else if (const MemberPointerType *MT = T->getAs<MemberPointerType>())
  694. FT = MT->getPointeeType()->getAs<FunctionProtoType>();
  695. else if (const BlockPointerType *BT = T->getAs<BlockPointerType>())
  696. FT = BT->getPointeeType()->getAs<FunctionProtoType>();
  697. if (!FT)
  698. return CT_Can;
  699. FT = S.ResolveExceptionSpec(E->getLocStart(), FT);
  700. if (!FT)
  701. return CT_Can;
  702. if (FT->getExceptionSpecType() == EST_Delayed) {
  703. // FIXME: Try to resolve a delayed exception spec in ResolveExceptionSpec.
  704. assert(isa<CXXConstructorDecl>(D) &&
  705. "only constructor exception specs can be unknown");
  706. S.Diag(E->getLocStart(), diag::err_exception_spec_unknown)
  707. << E->getSourceRange();
  708. return CT_Can;
  709. }
  710. return FT->isNothrow(S.Context) ? CT_Cannot : CT_Can;
  711. }
  712. static CanThrowResult canDynamicCastThrow(const CXXDynamicCastExpr *DC) {
  713. if (DC->isTypeDependent())
  714. return CT_Dependent;
  715. if (!DC->getTypeAsWritten()->isReferenceType())
  716. return CT_Cannot;
  717. if (DC->getSubExpr()->isTypeDependent())
  718. return CT_Dependent;
  719. return DC->getCastKind() == clang::CK_Dynamic? CT_Can : CT_Cannot;
  720. }
  721. static CanThrowResult canTypeidThrow(Sema &S, const CXXTypeidExpr *DC) {
  722. if (DC->isTypeOperand())
  723. return CT_Cannot;
  724. Expr *Op = DC->getExprOperand();
  725. if (Op->isTypeDependent())
  726. return CT_Dependent;
  727. const RecordType *RT = Op->getType()->getAs<RecordType>();
  728. if (!RT)
  729. return CT_Cannot;
  730. if (!cast<CXXRecordDecl>(RT->getDecl())->isPolymorphic())
  731. return CT_Cannot;
  732. if (Op->Classify(S.Context).isPRValue())
  733. return CT_Cannot;
  734. return CT_Can;
  735. }
  736. CanThrowResult Sema::canThrow(const Expr *E) {
  737. // C++ [expr.unary.noexcept]p3:
  738. // [Can throw] if in a potentially-evaluated context the expression would
  739. // contain:
  740. switch (E->getStmtClass()) {
  741. case Expr::CXXThrowExprClass:
  742. // - a potentially evaluated throw-expression
  743. return CT_Can;
  744. case Expr::CXXDynamicCastExprClass: {
  745. // - a potentially evaluated dynamic_cast expression dynamic_cast<T>(v),
  746. // where T is a reference type, that requires a run-time check
  747. CanThrowResult CT = canDynamicCastThrow(cast<CXXDynamicCastExpr>(E));
  748. if (CT == CT_Can)
  749. return CT;
  750. return mergeCanThrow(CT, canSubExprsThrow(*this, E));
  751. }
  752. case Expr::CXXTypeidExprClass:
  753. // - a potentially evaluated typeid expression applied to a glvalue
  754. // expression whose type is a polymorphic class type
  755. return canTypeidThrow(*this, cast<CXXTypeidExpr>(E));
  756. // - a potentially evaluated call to a function, member function, function
  757. // pointer, or member function pointer that does not have a non-throwing
  758. // exception-specification
  759. case Expr::CallExprClass:
  760. case Expr::CXXMemberCallExprClass:
  761. case Expr::CXXOperatorCallExprClass:
  762. case Expr::UserDefinedLiteralClass: {
  763. const CallExpr *CE = cast<CallExpr>(E);
  764. CanThrowResult CT;
  765. if (E->isTypeDependent())
  766. CT = CT_Dependent;
  767. else if (isa<CXXPseudoDestructorExpr>(CE->getCallee()->IgnoreParens()))
  768. CT = CT_Cannot;
  769. else
  770. CT = canCalleeThrow(*this, E, CE->getCalleeDecl());
  771. if (CT == CT_Can)
  772. return CT;
  773. return mergeCanThrow(CT, canSubExprsThrow(*this, E));
  774. }
  775. case Expr::CXXConstructExprClass:
  776. case Expr::CXXTemporaryObjectExprClass: {
  777. CanThrowResult CT = canCalleeThrow(*this, E,
  778. cast<CXXConstructExpr>(E)->getConstructor());
  779. if (CT == CT_Can)
  780. return CT;
  781. return mergeCanThrow(CT, canSubExprsThrow(*this, E));
  782. }
  783. case Expr::LambdaExprClass: {
  784. const LambdaExpr *Lambda = cast<LambdaExpr>(E);
  785. CanThrowResult CT = CT_Cannot;
  786. for (LambdaExpr::capture_init_iterator Cap = Lambda->capture_init_begin(),
  787. CapEnd = Lambda->capture_init_end();
  788. Cap != CapEnd; ++Cap)
  789. CT = mergeCanThrow(CT, canThrow(*Cap));
  790. return CT;
  791. }
  792. case Expr::CXXNewExprClass: {
  793. CanThrowResult CT;
  794. if (E->isTypeDependent())
  795. CT = CT_Dependent;
  796. else
  797. CT = canCalleeThrow(*this, E, cast<CXXNewExpr>(E)->getOperatorNew());
  798. if (CT == CT_Can)
  799. return CT;
  800. return mergeCanThrow(CT, canSubExprsThrow(*this, E));
  801. }
  802. case Expr::CXXDeleteExprClass: {
  803. CanThrowResult CT;
  804. QualType DTy = cast<CXXDeleteExpr>(E)->getDestroyedType();
  805. if (DTy.isNull() || DTy->isDependentType()) {
  806. CT = CT_Dependent;
  807. } else {
  808. CT = canCalleeThrow(*this, E,
  809. cast<CXXDeleteExpr>(E)->getOperatorDelete());
  810. if (const RecordType *RT = DTy->getAs<RecordType>()) {
  811. const CXXRecordDecl *RD = cast<CXXRecordDecl>(RT->getDecl());
  812. CT = mergeCanThrow(CT, canCalleeThrow(*this, E, RD->getDestructor()));
  813. }
  814. if (CT == CT_Can)
  815. return CT;
  816. }
  817. return mergeCanThrow(CT, canSubExprsThrow(*this, E));
  818. }
  819. case Expr::CXXBindTemporaryExprClass: {
  820. // The bound temporary has to be destroyed again, which might throw.
  821. CanThrowResult CT = canCalleeThrow(*this, E,
  822. cast<CXXBindTemporaryExpr>(E)->getTemporary()->getDestructor());
  823. if (CT == CT_Can)
  824. return CT;
  825. return mergeCanThrow(CT, canSubExprsThrow(*this, E));
  826. }
  827. // ObjC message sends are like function calls, but never have exception
  828. // specs.
  829. case Expr::ObjCMessageExprClass:
  830. case Expr::ObjCPropertyRefExprClass:
  831. case Expr::ObjCSubscriptRefExprClass:
  832. return CT_Can;
  833. // All the ObjC literals that are implemented as calls are
  834. // potentially throwing unless we decide to close off that
  835. // possibility.
  836. case Expr::ObjCArrayLiteralClass:
  837. case Expr::ObjCDictionaryLiteralClass:
  838. case Expr::ObjCBoxedExprClass:
  839. return CT_Can;
  840. // Many other things have subexpressions, so we have to test those.
  841. // Some are simple:
  842. case Expr::ConditionalOperatorClass:
  843. case Expr::CompoundLiteralExprClass:
  844. case Expr::CXXConstCastExprClass:
  845. case Expr::CXXDefaultArgExprClass:
  846. case Expr::CXXReinterpretCastExprClass:
  847. case Expr::DesignatedInitExprClass:
  848. case Expr::ExprWithCleanupsClass:
  849. case Expr::ExtVectorElementExprClass:
  850. case Expr::InitListExprClass:
  851. case Expr::MemberExprClass:
  852. case Expr::ObjCIsaExprClass:
  853. case Expr::ObjCIvarRefExprClass:
  854. case Expr::ParenExprClass:
  855. case Expr::ParenListExprClass:
  856. case Expr::ShuffleVectorExprClass:
  857. case Expr::VAArgExprClass:
  858. return canSubExprsThrow(*this, E);
  859. // Some might be dependent for other reasons.
  860. case Expr::ArraySubscriptExprClass:
  861. case Expr::BinaryOperatorClass:
  862. case Expr::CompoundAssignOperatorClass:
  863. case Expr::CStyleCastExprClass:
  864. case Expr::CXXStaticCastExprClass:
  865. case Expr::CXXFunctionalCastExprClass:
  866. case Expr::ImplicitCastExprClass:
  867. case Expr::MaterializeTemporaryExprClass:
  868. case Expr::UnaryOperatorClass: {
  869. CanThrowResult CT = E->isTypeDependent() ? CT_Dependent : CT_Cannot;
  870. return mergeCanThrow(CT, canSubExprsThrow(*this, E));
  871. }
  872. // FIXME: We should handle StmtExpr, but that opens a MASSIVE can of worms.
  873. case Expr::StmtExprClass:
  874. return CT_Can;
  875. case Expr::ChooseExprClass:
  876. if (E->isTypeDependent() || E->isValueDependent())
  877. return CT_Dependent;
  878. return canThrow(cast<ChooseExpr>(E)->getChosenSubExpr(Context));
  879. case Expr::GenericSelectionExprClass:
  880. if (cast<GenericSelectionExpr>(E)->isResultDependent())
  881. return CT_Dependent;
  882. return canThrow(cast<GenericSelectionExpr>(E)->getResultExpr());
  883. // Some expressions are always dependent.
  884. case Expr::CXXDependentScopeMemberExprClass:
  885. case Expr::CXXUnresolvedConstructExprClass:
  886. case Expr::DependentScopeDeclRefExprClass:
  887. return CT_Dependent;
  888. case Expr::AsTypeExprClass:
  889. case Expr::BinaryConditionalOperatorClass:
  890. case Expr::BlockExprClass:
  891. case Expr::CUDAKernelCallExprClass:
  892. case Expr::DeclRefExprClass:
  893. case Expr::ObjCBridgedCastExprClass:
  894. case Expr::ObjCIndirectCopyRestoreExprClass:
  895. case Expr::ObjCProtocolExprClass:
  896. case Expr::ObjCSelectorExprClass:
  897. case Expr::OffsetOfExprClass:
  898. case Expr::PackExpansionExprClass:
  899. case Expr::PseudoObjectExprClass:
  900. case Expr::SubstNonTypeTemplateParmExprClass:
  901. case Expr::SubstNonTypeTemplateParmPackExprClass:
  902. case Expr::UnaryExprOrTypeTraitExprClass:
  903. case Expr::UnresolvedLookupExprClass:
  904. case Expr::UnresolvedMemberExprClass:
  905. // FIXME: Can any of the above throw? If so, when?
  906. return CT_Cannot;
  907. case Expr::AddrLabelExprClass:
  908. case Expr::ArrayTypeTraitExprClass:
  909. case Expr::AtomicExprClass:
  910. case Expr::BinaryTypeTraitExprClass:
  911. case Expr::TypeTraitExprClass:
  912. case Expr::CXXBoolLiteralExprClass:
  913. case Expr::CXXNoexceptExprClass:
  914. case Expr::CXXNullPtrLiteralExprClass:
  915. case Expr::CXXPseudoDestructorExprClass:
  916. case Expr::CXXScalarValueInitExprClass:
  917. case Expr::CXXThisExprClass:
  918. case Expr::CXXUuidofExprClass:
  919. case Expr::CharacterLiteralClass:
  920. case Expr::ExpressionTraitExprClass:
  921. case Expr::FloatingLiteralClass:
  922. case Expr::GNUNullExprClass:
  923. case Expr::ImaginaryLiteralClass:
  924. case Expr::ImplicitValueInitExprClass:
  925. case Expr::IntegerLiteralClass:
  926. case Expr::ObjCEncodeExprClass:
  927. case Expr::ObjCStringLiteralClass:
  928. case Expr::ObjCBoolLiteralExprClass:
  929. case Expr::OpaqueValueExprClass:
  930. case Expr::PredefinedExprClass:
  931. case Expr::SizeOfPackExprClass:
  932. case Expr::StringLiteralClass:
  933. case Expr::UnaryTypeTraitExprClass:
  934. // These expressions can never throw.
  935. return CT_Cannot;
  936. #define STMT(CLASS, PARENT) case Expr::CLASS##Class:
  937. #define STMT_RANGE(Base, First, Last)
  938. #define LAST_STMT_RANGE(BASE, FIRST, LAST)
  939. #define EXPR(CLASS, PARENT)
  940. #define ABSTRACT_STMT(STMT)
  941. #include "clang/AST/StmtNodes.inc"
  942. case Expr::NoStmtClass:
  943. llvm_unreachable("Invalid class for expression");
  944. }
  945. llvm_unreachable("Bogus StmtClass");
  946. }
  947. } // end namespace clang