clang-tools 23.0.0git
ExceptionAnalyzer.cpp
Go to the documentation of this file.
1//===----------------------------------------------------------------------===//
2//
3// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4// See https://llvm.org/LICENSE.txt for license information.
5// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6//
7//===----------------------------------------------------------------------===//
8
9#include "ExceptionAnalyzer.h"
10
11namespace clang::tidy::utils {
12
14 const Type *ExceptionType, const ThrowInfo &ThrowInfo) {
15 Behaviour = State::Throwing;
16 ThrownExceptions.insert({ExceptionType, ThrowInfo});
17}
18
20 const Throwables &Exceptions) {
21 if (Exceptions.empty())
22 return;
23 Behaviour = State::Throwing;
24 ThrownExceptions.insert_range(Exceptions);
25}
26
29 // Only the following two cases require an update to the local
30 // 'Behaviour'. If the local entity is already throwing there will be no
31 // change and if the other entity is throwing the merged entity will throw
32 // as well.
33 // If one of both entities is 'Unknown' and the other one does not throw
34 // the merged entity is 'Unknown' as well.
35 if (Other.Behaviour == State::Throwing)
36 Behaviour = State::Throwing;
37 else if (Other.Behaviour == State::Unknown && Behaviour == State::NotThrowing)
38 Behaviour = State::Unknown;
39
40 ContainsUnknown = ContainsUnknown || Other.ContainsUnknown;
41 ThrowsUnknown = ThrowsUnknown || Other.ThrowsUnknown;
42 ThrownExceptions.insert_range(Other.ThrownExceptions);
43 return *this;
44}
45
46// FIXME: This could be ported to clang later.
47
48static bool isUnambiguousPublicBaseClass(const Type *DerivedType,
49 const Type *BaseType) {
50 const auto *DerivedClass =
51 DerivedType->getCanonicalTypeUnqualified()->getAsCXXRecordDecl();
52 const auto *BaseClass =
53 BaseType->getCanonicalTypeUnqualified()->getAsCXXRecordDecl();
54 if (!DerivedClass || !BaseClass)
55 return false;
56
57 CXXBasePaths Paths;
58 Paths.setOrigin(DerivedClass);
59
60 bool IsPublicBaseClass = false;
61 DerivedClass->lookupInBases(
62 [&BaseClass, &IsPublicBaseClass](const CXXBaseSpecifier *BS,
63 CXXBasePath &) {
64 if (BS->getType()
65 ->getCanonicalTypeUnqualified()
66 ->getAsCXXRecordDecl() == BaseClass &&
67 BS->getAccessSpecifier() == AS_public) {
68 IsPublicBaseClass = true;
69 return true;
70 }
71
72 return false;
73 },
74 Paths);
75
76 return !Paths.isAmbiguous(BaseType->getCanonicalTypeUnqualified()) &&
77 IsPublicBaseClass;
78}
79
80static bool isPointerOrPointerToMember(const Type *T) {
81 return T->isPointerType() || T->isMemberPointerType();
82}
83
84static std::optional<QualType> getPointeeOrArrayElementQualType(QualType T) {
85 if (T->isAnyPointerType() || T->isMemberPointerType())
86 return T->getPointeeType();
87
88 if (T->isArrayType())
89 return T->getAsArrayTypeUnsafe()->getElementType();
90
91 return std::nullopt;
92}
93
94static bool isBaseOf(const Type *DerivedType, const Type *BaseType) {
95 const auto *DerivedClass = DerivedType->getAsCXXRecordDecl();
96 const auto *BaseClass = BaseType->getAsCXXRecordDecl();
97 if (!DerivedClass || !BaseClass)
98 return false;
99
100 return !DerivedClass->forallBases(
101 [BaseClass](const CXXRecordDecl *Cur) { return Cur != BaseClass; });
102}
103
104// Check if T1 is more or Equally qualified than T2.
105static bool moreOrEquallyQualified(QualType T1, QualType T2) {
106 return T1.getQualifiers().isStrictSupersetOf(T2.getQualifiers()) ||
107 T1.getQualifiers() == T2.getQualifiers();
108}
109
110static bool isStandardPointerConvertible(QualType From, QualType To) {
111 assert((From->isPointerType() || From->isMemberPointerType()) &&
112 (To->isPointerType() || To->isMemberPointerType()) &&
113 "Pointer conversion should be performed on pointer types only.");
114
115 if (!moreOrEquallyQualified(To->getPointeeType(), From->getPointeeType()))
116 return false;
117
118 // (1)
119 // A null pointer constant can be converted to a pointer type ...
120 // The conversion of a null pointer constant to a pointer to cv-qualified type
121 // is a single conversion, and not the sequence of a pointer conversion
122 // followed by a qualification conversion. A null pointer constant of integral
123 // type can be converted to a prvalue of type std::nullptr_t
124 if (To->isPointerType() && From->isNullPtrType())
125 return true;
126
127 // (2)
128 // A prvalue of type “pointer to cv T”, where T is an object type, can be
129 // converted to a prvalue of type “pointer to cv void”.
130 if (To->isVoidPointerType() && From->isObjectPointerType())
131 return true;
132
133 // (3)
134 // A prvalue of type “pointer to cv D”, where D is a complete class type, can
135 // be converted to a prvalue of type “pointer to cv B”, where B is a base
136 // class of D. If B is an inaccessible or ambiguous base class of D, a program
137 // that necessitates this conversion is ill-formed.
138 if (const auto *RD = From->getPointeeCXXRecordDecl()) {
139 if (RD->isCompleteDefinition() &&
140 isBaseOf(From->getPointeeType().getTypePtr(),
141 To->getPointeeType().getTypePtr())) {
142 // If B is an inaccessible or ambiguous base class of D, a program
143 // that necessitates this conversion is ill-formed
144 return isUnambiguousPublicBaseClass(From->getPointeeType().getTypePtr(),
145 To->getPointeeType().getTypePtr());
146 }
147 }
148
149 return false;
150}
151
152static bool isFunctionPointerConvertible(QualType From, QualType To) {
153 if (!From->isFunctionPointerType() && !From->isFunctionType() &&
154 !From->isMemberFunctionPointerType())
155 return false;
156
157 if (!To->isFunctionPointerType() && !To->isMemberFunctionPointerType())
158 return false;
159
160 if (To->isFunctionPointerType()) {
161 if (From->isFunctionPointerType())
162 return To->getPointeeType() == From->getPointeeType();
163
164 if (From->isFunctionType())
165 return To->getPointeeType() == From;
166
167 return false;
168 }
169
170 if (To->isMemberFunctionPointerType()) {
171 if (!From->isMemberFunctionPointerType())
172 return false;
173
174 const auto *FromMember = cast<MemberPointerType>(From);
175 const auto *ToMember = cast<MemberPointerType>(To);
176
177 // Note: converting Derived::* to Base::* is a different kind of conversion,
178 // called Pointer-to-member conversion.
179 return FromMember->getQualifier() == ToMember->getQualifier() &&
180 FromMember->getMostRecentCXXRecordDecl() ==
181 ToMember->getMostRecentCXXRecordDecl() &&
182 FromMember->getPointeeType() == ToMember->getPointeeType();
183 }
184
185 return false;
186}
187
188// Checks if From is qualification convertible to To based on the current
189// LangOpts. If From is any array, we perform the array to pointer conversion
190// first. The function only performs checks based on C++ rules, which can differ
191// from the C rules.
192//
193// The function should only be called in C++ mode.
194static bool isQualificationConvertiblePointer(QualType From, QualType To,
195 const LangOptions &LangOpts) {
196 // [N4659 7.5 (1)]
197 // A cv-decomposition of a type T is a sequence of cv_i and P_i such that T is
198 // cv_0 P_0 cv_1 P_1 ... cv_n−1 P_n−1 cv_n U” for n > 0,
199 // where each cv_i is a set of cv-qualifiers, and each P_i is “pointer to”,
200 // “pointer to member of class C_i of type”, “array of N_i”, or
201 // “array of unknown bound of”.
202 //
203 // If P_i designates an array, the cv-qualifiers cv_i+1 on the element type
204 // are also taken as the cv-qualifiers cvi of the array.
205 //
206 // The n-tuple of cv-qualifiers after the first one in the longest
207 // cv-decomposition of T, that is, cv_1, cv_2, ... , cv_n, is called the
208 // cv-qualification signature of T.
209
210 // NOLINTNEXTLINE (readability-identifier-naming): Preserve original notation
211 auto IsValidP_i = [](QualType P) {
212 return P->isPointerType() || P->isMemberPointerType() ||
213 P->isConstantArrayType() || P->isIncompleteArrayType();
214 };
215
216 // NOLINTNEXTLINE (readability-identifier-naming): Preserve original notation
217 auto IsSameP_i = [](QualType P1, QualType P2) {
218 if (P1->isPointerType())
219 return P2->isPointerType();
220
221 if (P1->isMemberPointerType())
222 return P2->isMemberPointerType() &&
223 P1->getAs<MemberPointerType>()->getMostRecentCXXRecordDecl() ==
224 P2->getAs<MemberPointerType>()->getMostRecentCXXRecordDecl();
225
226 if (P1->isConstantArrayType())
227 return P2->isConstantArrayType() &&
228 cast<ConstantArrayType>(P1)->getSize() ==
229 cast<ConstantArrayType>(P2)->getSize();
230
231 if (P1->isIncompleteArrayType())
232 return P2->isIncompleteArrayType();
233
234 return false;
235 };
236
237 // (2)
238 // Two types From and To are similar if they have cv-decompositions with the
239 // same n such that corresponding P_i components are the same [(added by
240 // N4849 7.3.5) or one is “array of N_i” and the other is “array of unknown
241 // bound of”], and the types denoted by U are the same.
242 //
243 // (3)
244 // A prvalue expression of type From can be converted to type To if the
245 // following conditions are satisfied:
246 // - From and To are similar
247 // - For every i > 0, if const is in cv_i of From then const is in cv_i of
248 // To, and similarly for volatile.
249 // - [(derived from addition by N4849 7.3.5) If P_i of From is “array of
250 // unknown bound of”, P_i of To is “array of unknown bound of”.]
251 // - If the cv_i of From and cv_i of To are different, then const is in every
252 // cv_k of To for 0 < k < i.
253
254 int I = 0;
255 bool ConstUntilI = true;
256 auto SatisfiesCVRules = [&I, &ConstUntilI](const QualType &From,
257 const QualType &To) {
258 if (I > 1) {
259 if (From.getQualifiers() != To.getQualifiers() && !ConstUntilI)
260 return false;
261 }
262
263 if (I > 0) {
264 if (From.isConstQualified() && !To.isConstQualified())
265 return false;
266
267 if (From.isVolatileQualified() && !To.isVolatileQualified())
268 return false;
269
270 ConstUntilI = To.isConstQualified();
271 }
272
273 return true;
274 };
275
276 while (IsValidP_i(From) && IsValidP_i(To)) {
277 // Remove every sugar.
278 From = From.getCanonicalType();
279 To = To.getCanonicalType();
280
281 if (!SatisfiesCVRules(From, To))
282 return false;
283
284 if (!IsSameP_i(From, To)) {
285 if (LangOpts.CPlusPlus20) {
286 if (From->isConstantArrayType() && !To->isIncompleteArrayType())
287 return false;
288
289 if (From->isIncompleteArrayType() && !To->isIncompleteArrayType())
290 return false;
291
292 } else {
293 return false;
294 }
295 }
296
297 ++I;
298 std::optional<QualType> FromPointeeOrElem =
300 std::optional<QualType> ToPointeeOrElem =
302
303 assert(FromPointeeOrElem &&
304 "From pointer or array has no pointee or element!");
305 assert(ToPointeeOrElem && "To pointer or array has no pointee or element!");
306
307 From = *FromPointeeOrElem;
308 To = *ToPointeeOrElem;
309 }
310
311 // In this case the length (n) of From and To are not the same.
312 if (IsValidP_i(From) || IsValidP_i(To))
313 return false;
314
315 // We hit U.
316 if (!SatisfiesCVRules(From, To))
317 return false;
318
319 return From.getTypePtr() == To.getTypePtr();
320}
321
322static bool canThrow(const FunctionDecl *Func) {
323 // consteval specifies that every call to the function must produce a
324 // compile-time constant, which cannot evaluate a throw expression without
325 // producing a compilation error.
326 if (Func->isConsteval())
327 return false;
328
329 const auto *FunProto = Func->getType()->getAs<FunctionProtoType>();
330 if (!FunProto)
331 return true;
332
333 // Clang evaluates unresolved exception specs before generating any call to
334 // the function, so these functions cannot appear at a call site and cannot
335 // throw.
336 if (isUnresolvedExceptionSpec(FunProto->getExceptionSpecType()))
337 return false;
338
339 switch (FunProto->canThrow()) {
340 case CT_Cannot:
341 return false;
342 case CT_Dependent: {
343 const Expr *NoexceptExpr = FunProto->getNoexceptExpr();
344 if (!NoexceptExpr)
345 return true; // no noexcept - can throw
346
347 if (NoexceptExpr->isValueDependent())
348 return true; // depend on template - some instance can throw
349
350 bool Result = false;
351 if (!NoexceptExpr->EvaluateAsBooleanCondition(Result, Func->getASTContext(),
352 /*InConstantContext=*/true))
353 return true; // complex X condition in noexcept(X), cannot validate,
354 // assume that may throw
355 return !Result; // noexcept(false) - can throw
356 }
357 default:
358 return true;
359 };
360}
361
364 const ASTContext &Context) {
365 SmallVector<const Type *, 8> TypesToDelete;
366 for (const auto &ThrownException : ThrownExceptions) {
367 const Type *ExceptionTy = ThrownException.getFirst();
368 const CanQualType ExceptionCanTy =
369 ExceptionTy->getCanonicalTypeUnqualified();
370 const CanQualType HandlerCanTy = HandlerTy->getCanonicalTypeUnqualified();
371
372 // The handler is of type cv T or cv T& and E and T are the same type
373 // (ignoring the top-level cv-qualifiers) ...
374 if (ExceptionCanTy == HandlerCanTy) {
375 TypesToDelete.push_back(ExceptionTy);
376 }
377
378 // The handler is of type cv T or cv T& and T is an unambiguous public base
379 // class of E ...
380 else if (isUnambiguousPublicBaseClass(ExceptionCanTy->getTypePtr(),
381 HandlerCanTy->getTypePtr())) {
382 TypesToDelete.push_back(ExceptionTy);
383 }
384
385 if (HandlerCanTy->getTypeClass() == Type::RValueReference ||
386 (HandlerCanTy->getTypeClass() == Type::LValueReference &&
387 !HandlerCanTy->getTypePtr()->getPointeeType().isConstQualified()))
388 continue;
389 // The handler is of type cv T or const T& where T is a pointer or
390 // pointer-to-member type and E is a pointer or pointer-to-member type that
391 // can be converted to T by one or more of ...
392 if (isPointerOrPointerToMember(HandlerCanTy->getTypePtr()) &&
393 isPointerOrPointerToMember(ExceptionCanTy->getTypePtr())) {
394 // A standard pointer conversion not involving conversions to pointers to
395 // private or protected or ambiguous classes ...
396 if (isStandardPointerConvertible(ExceptionCanTy, HandlerCanTy)) {
397 TypesToDelete.push_back(ExceptionTy);
398 }
399 // A function pointer conversion ...
400 else if (isFunctionPointerConvertible(ExceptionCanTy, HandlerCanTy)) {
401 TypesToDelete.push_back(ExceptionTy);
402 }
403 // A a qualification conversion ...
404 else if (isQualificationConvertiblePointer(ExceptionCanTy, HandlerCanTy,
405 Context.getLangOpts())) {
406 TypesToDelete.push_back(ExceptionTy);
407 }
408 }
409
410 // The handler is of type cv T or const T& where T is a pointer or
411 // pointer-to-member type and E is std::nullptr_t.
412 else if (isPointerOrPointerToMember(HandlerCanTy->getTypePtr()) &&
413 ExceptionCanTy->isNullPtrType()) {
414 TypesToDelete.push_back(ExceptionTy);
415 }
416 }
417
418 Throwables DeletedExceptions;
419
420 for (const Type *TypeToDelete : TypesToDelete) {
421 const auto DeleteIt = ThrownExceptions.find(TypeToDelete);
422 if (DeleteIt != ThrownExceptions.end()) {
423 DeletedExceptions.insert(*DeleteIt);
424 ThrownExceptions.erase(DeleteIt);
425 }
426 }
427
428 reevaluateBehaviour();
429 return DeletedExceptions;
430}
431
434 const llvm::StringSet<> &IgnoredTypes, bool IgnoreBadAlloc) {
435 SmallVector<const Type *, 8> TypesToDelete;
436 // Note: Using a 'SmallSet' with 'llvm::remove_if()' is not possible.
437 // Therefore this slightly hacky implementation is required.
438 for (const auto &ThrownException : ThrownExceptions) {
439 const Type *T = ThrownException.getFirst();
440 if (!T)
441 continue;
442 if (const auto *TD = T->getAsTagDecl()) {
443 if (TD->getDeclName().isIdentifier()) {
444 if ((IgnoreBadAlloc &&
445 (TD->getName() == "bad_alloc" && TD->isInStdNamespace())) ||
446 (IgnoredTypes.contains(TD->getName())))
447 TypesToDelete.push_back(T);
448 }
449 }
450 }
451 for (const Type *T : TypesToDelete)
452 ThrownExceptions.erase(T);
453
454 reevaluateBehaviour();
455 return *this;
456}
457
459 Behaviour = State::NotThrowing;
460 ContainsUnknown = false;
461 ThrowsUnknown = false;
462 ThrownExceptions.clear();
463}
464
465void ExceptionAnalyzer::ExceptionInfo::reevaluateBehaviour() {
466 if (ThrownExceptions.empty() && !ThrowsUnknown)
467 if (ContainsUnknown)
468 Behaviour = State::Unknown;
469 else
470 Behaviour = State::NotThrowing;
471 else
472 Behaviour = State::Throwing;
473}
474ExceptionAnalyzer::ExceptionInfo ExceptionAnalyzer::throwsException(
475 const FunctionDecl *Func, const ExceptionInfo::Throwables &Caught,
476 CallStack &CallStack, SourceLocation CallLoc) {
477 if (!Func || CallStack.contains(Func) ||
478 (!CallStack.empty() && !canThrow(Func)))
480
481 if (const Stmt *Body = Func->getBody()) {
482 CallStack.insert({Func, CallLoc});
483 ExceptionInfo Result = throwsException(Body, Caught, CallStack);
484
485 // For a constructor, we also have to check the initializers.
486 if (const auto *Ctor = dyn_cast<CXXConstructorDecl>(Func)) {
487 for (const CXXCtorInitializer *Init : Ctor->inits()) {
488 const ExceptionInfo Excs =
489 throwsException(Init->getInit(), Caught, CallStack);
490 Result.merge(Excs);
491 }
492 }
493
494 // Optionally treat unannotated functions as potentially throwing if they
495 // are not explicitly non-throwing and no throw was discovered.
496 if (AssumeUnannotatedFunctionsAsThrowing &&
497 Result.getBehaviour() == State::NotThrowing && canThrow(Func)) {
498 Result.registerException(nullptr, {Func->getLocation(), CallStack});
499 }
500
501 CallStack.erase(Func);
502 return Result;
503 }
504
505 auto Result = ExceptionInfo::createUnknown();
506
507 if (const auto *FPT = Func->getType()->getAs<FunctionProtoType>()) {
508 if (isUnresolvedExceptionSpec(FPT->getExceptionSpecType()))
510
511 for (const QualType &Ex : FPT->exceptions()) {
512 CallStack.insert({Func, CallLoc});
513 Result.registerException(
514 Ex.getTypePtr(),
515 {Func->getExceptionSpecSourceRange().getBegin(), CallStack});
516 CallStack.erase(Func);
517 }
518 }
519
520 if (AssumeMissingDefinitionsFunctionsAsThrowing &&
521 Result.getBehaviour() == State::Unknown) {
522 CallStack.insert({Func, CallLoc});
523 Result.registerException(nullptr, {Func->getLocation(), CallStack});
524 CallStack.erase(Func);
525 }
526
527 return Result;
528}
529
530/// Analyzes a single statement on it's throwing behaviour. This is in principle
531/// possible except some 'Unknown' functions are called.
532ExceptionAnalyzer::ExceptionInfo
533ExceptionAnalyzer::throwsException(const Stmt *St,
534 const ExceptionInfo::Throwables &Caught,
536 auto Results = ExceptionInfo::createNonThrowing();
537 if (!St)
538 return Results;
539
540 if (const auto *Throw = dyn_cast<CXXThrowExpr>(St)) {
541 if (const auto *ThrownExpr = Throw->getSubExpr()) {
542 const auto *ThrownType =
543 ThrownExpr->getType()->getUnqualifiedDesugaredType();
544 if (ThrownType->isReferenceType())
545 ThrownType = ThrownType->castAs<ReferenceType>()
546 ->getPointeeType()
547 ->getUnqualifiedDesugaredType();
548 Results.registerException(
549 ThrownExpr->getType()->getUnqualifiedDesugaredType(),
550 {Throw->getBeginLoc(), CallStack});
551 } else {
552 // A rethrow of a caught exception happens which makes it possible
553 // to throw all exception that are caught in the 'catch' clause of
554 // the parent try-catch block.
555 Results.registerExceptions(Caught);
556 }
557 } else if (const auto *Try = dyn_cast<CXXTryStmt>(St)) {
558 ExceptionInfo Uncaught =
559 throwsException(Try->getTryBlock(), Caught, CallStack);
560 for (unsigned I = 0; I < Try->getNumHandlers(); ++I) {
561 const CXXCatchStmt *Catch = Try->getHandler(I);
562
563 // Everything is caught through 'catch(...)'.
564 if (!Catch->getExceptionDecl()) {
565 const ExceptionInfo Rethrown = throwsException(
566 Catch->getHandlerBlock(), Uncaught.getExceptions(), CallStack);
567 Results.merge(Rethrown);
568 Uncaught.clear();
569 } else {
570 const auto *CaughtType =
571 Catch->getCaughtType()->getUnqualifiedDesugaredType();
572 if (CaughtType->isReferenceType()) {
573 CaughtType = CaughtType->castAs<ReferenceType>()
574 ->getPointeeType()
575 ->getUnqualifiedDesugaredType();
576 }
577
578 // If the caught exception will catch multiple previously potential
579 // thrown types (because it's sensitive to inheritance) the throwing
580 // situation changes. First of all filter the exception types and
581 // analyze if the baseclass-exception is rethrown.
582 const ExceptionInfo::Throwables FilteredExceptions =
583 Uncaught.filterByCatch(CaughtType,
584 Catch->getExceptionDecl()->getASTContext());
585 if (!FilteredExceptions.empty()) {
586 const ExceptionInfo Rethrown = throwsException(
587 Catch->getHandlerBlock(), FilteredExceptions, CallStack);
588 Results.merge(Rethrown);
589 }
590 }
591 }
592 Results.merge(Uncaught);
593 } else if (const auto *DefaultInit = dyn_cast<CXXDefaultInitExpr>(St)) {
594 const ExceptionInfo Excs =
595 throwsException(DefaultInit->getExpr(), Caught, CallStack);
596 Results.merge(Excs);
597 } else if (const auto *Coro = dyn_cast<CoroutineBodyStmt>(St)) {
598 for (const Stmt *Child : Coro->childrenExclBody()) {
599 if (Child != Coro->getExceptionHandler()) {
600 const ExceptionInfo Excs = throwsException(Child, Caught, CallStack);
601 Results.merge(Excs);
602 }
603 }
604 const ExceptionInfo Excs =
605 throwsException(Coro->getBody(), Caught, CallStack);
606 Results.merge(throwsException(Coro->getExceptionHandler(),
607 Excs.getExceptions(), CallStack));
608 for (const auto &Exception : Excs.getExceptions()) {
609 const Type *ExcType = Exception.getFirst();
610 if (const CXXRecordDecl *ThrowableRec = ExcType->getAsCXXRecordDecl()) {
611 const ExceptionInfo DestructorExcs = throwsException(
612 ThrowableRec->getDestructor(), Caught, CallStack, SourceLocation{});
613 Results.merge(DestructorExcs);
614 }
615 }
616 } else if (const auto *Lambda = dyn_cast<LambdaExpr>(St)) {
617 for (const Stmt *Init : Lambda->capture_inits()) {
618 const ExceptionInfo Excs = throwsException(Init, Caught, CallStack);
619 Results.merge(Excs);
620 }
621 } else {
622 // Check whether any of this node's subexpressions throws.
623 for (const Stmt *Child : St->children()) {
624 const ExceptionInfo Excs = throwsException(Child, Caught, CallStack);
625 Results.merge(Excs);
626 }
627
628 // If this node is a call to a function or constructor, also check
629 // whether the call itself throws.
630 if (const auto *Call = dyn_cast<CallExpr>(St)) {
631 if (const FunctionDecl *Func = Call->getDirectCallee()) {
632 const ExceptionInfo Excs =
633 throwsException(Func, Caught, CallStack, Call->getBeginLoc());
634 Results.merge(Excs);
635 }
636 } else if (const auto *Construct = dyn_cast<CXXConstructExpr>(St)) {
637 const ExceptionInfo Excs =
638 throwsException(Construct->getConstructor(), Caught, CallStack,
639 Construct->getBeginLoc());
640 Results.merge(Excs);
641 }
642 }
643 return Results;
644}
645
646ExceptionAnalyzer::ExceptionInfo
647ExceptionAnalyzer::analyzeImpl(const FunctionDecl *Func) {
648 ExceptionInfo ExceptionList;
649
650 // Check if the function has already been analyzed and reuse that result.
651 const auto CacheEntry = FunctionCache.find(Func);
652 if (CacheEntry == FunctionCache.end()) {
654 ExceptionList = throwsException(Func, ExceptionInfo::Throwables(),
655 CallStack, Func->getLocation());
656
657 // Cache the result of the analysis. This is done prior to filtering
658 // because it is best to keep as much information as possible.
659 // The results here might be relevant to different analysis passes
660 // with different needs as well.
661 FunctionCache.try_emplace(Func, ExceptionList);
662 } else {
663 ExceptionList = CacheEntry->getSecond();
664 }
665
666 return ExceptionList;
667}
668
669ExceptionAnalyzer::ExceptionInfo
670ExceptionAnalyzer::analyzeImpl(const Stmt *Stmt) {
672 return throwsException(Stmt, ExceptionInfo::Throwables(), CallStack);
673}
674
675template <typename T>
676ExceptionAnalyzer::ExceptionInfo
677ExceptionAnalyzer::analyzeDispatch(const T *Node) {
678 ExceptionInfo ExceptionList = analyzeImpl(Node);
679
680 if (ExceptionList.getBehaviour() == State::NotThrowing ||
681 ExceptionList.getBehaviour() == State::Unknown)
682 return ExceptionList;
683
684 // Remove all ignored exceptions from the list of exceptions that can be
685 // thrown.
686 ExceptionList.filterIgnoredExceptions(IgnoredExceptions, IgnoreBadAlloc);
687
688 return ExceptionList;
689}
690
691ExceptionAnalyzer::ExceptionInfo
692ExceptionAnalyzer::analyze(const FunctionDecl *Func) {
693 return analyzeDispatch(Func);
694}
695
697 return analyzeDispatch(Stmt);
698}
699
700} // namespace clang::tidy::utils
Bundle the gathered information about an entity like a function regarding it's exception behaviour.
void clear()
Clear the state to 'NonThrowing' to make the corresponding entity neutral.
llvm::SmallDenseMap< const Type *, ThrowInfo, 2 > Throwables
ExceptionInfo & filterIgnoredExceptions(const llvm::StringSet<> &IgnoredTypes, bool IgnoreBadAlloc)
Filter the set of thrown exception type against a set of ignored types that shall not be considered i...
Throwables filterByCatch(const Type *HandlerTy, const ASTContext &Context)
This method is useful in case 'catch' clauses are analyzed as it is possible to catch multiple except...
void registerExceptions(const Throwables &Exceptions)
Registers a SmallVector of exception types as recognized potential exceptions to be thrown.
ExceptionInfo & merge(const ExceptionInfo &Other)
Updates the local state according to the other state.
void registerException(const Type *ExceptionType, const ThrowInfo &ThrowInfo)
Register a single exception type as recognized potential exception to be thrown.
@ Throwing
The function can definitely throw given an AST.
@ Unknown
This can happen for extern functions without available definition.
@ NotThrowing
This function can not throw, given an AST.
llvm::MapVector< const FunctionDecl *, SourceLocation > CallStack
We use a MapVector to preserve the order of the functions in the call stack as well as have fast look...
ExceptionInfo analyze(const FunctionDecl *Func)
@ Type
An inlay hint that for a type annotation.
Definition Protocol.h:1710
static std::optional< QualType > getPointeeOrArrayElementQualType(QualType T)
static bool isQualificationConvertiblePointer(QualType From, QualType To, const LangOptions &LangOpts)
static bool canThrow(const FunctionDecl *Func)
static bool moreOrEquallyQualified(QualType T1, QualType T2)
static bool isPointerOrPointerToMember(const Type *T)
static bool isUnambiguousPublicBaseClass(const Type *DerivedType, const Type *BaseType)
static bool isStandardPointerConvertible(QualType From, QualType To)
static bool isBaseOf(const Type *DerivedType, const Type *BaseType)
static bool isFunctionPointerConvertible(QualType From, QualType To)
cppcoreguidelines::ProBoundsAvoidUncheckedContainerAccessCheck P
Holds information about where an exception is thrown.