clang-tools 22.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 assert(ExceptionType != nullptr && "Only valid types are accepted");
16 Behaviour = State::Throwing;
17 ThrownExceptions.insert({ExceptionType, ThrowInfo});
18}
19
21 const Throwables &Exceptions) {
22 if (Exceptions.empty())
23 return;
24 Behaviour = State::Throwing;
25 ThrownExceptions.insert_range(Exceptions);
26}
27
30 // Only the following two cases require an update to the local
31 // 'Behaviour'. If the local entity is already throwing there will be no
32 // change and if the other entity is throwing the merged entity will throw
33 // as well.
34 // If one of both entities is 'Unknown' and the other one does not throw
35 // the merged entity is 'Unknown' as well.
36 if (Other.Behaviour == State::Throwing)
37 Behaviour = State::Throwing;
38 else if (Other.Behaviour == State::Unknown && Behaviour == State::NotThrowing)
39 Behaviour = State::Unknown;
40
41 ContainsUnknown = ContainsUnknown || Other.ContainsUnknown;
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
197 // [N4659 7.5 (1)]
198 // A cv-decomposition of a type T is a sequence of cv_i and P_i such that T is
199 // cv_0 P_0 cv_1 P_1 ... cv_n−1 P_n−1 cv_n U” for n > 0,
200 // where each cv_i is a set of cv-qualifiers, and each P_i is “pointer to”,
201 // “pointer to member of class C_i of type”, “array of N_i”, or
202 // “array of unknown bound of”.
203 //
204 // If P_i designates an array, the cv-qualifiers cv_i+1 on the element type
205 // are also taken as the cv-qualifiers cvi of the array.
206 //
207 // The n-tuple of cv-qualifiers after the first one in the longest
208 // cv-decomposition of T, that is, cv_1, cv_2, ... , cv_n, is called the
209 // cv-qualification signature of T.
210
211 // NOLINTNEXTLINE (readability-identifier-naming): Preserve original notation
212 auto IsValidP_i = [](QualType P) {
213 return P->isPointerType() || P->isMemberPointerType() ||
214 P->isConstantArrayType() || P->isIncompleteArrayType();
215 };
216
217 // NOLINTNEXTLINE (readability-identifier-naming): Preserve original notation
218 auto IsSameP_i = [](QualType P1, QualType P2) {
219 if (P1->isPointerType())
220 return P2->isPointerType();
221
222 if (P1->isMemberPointerType())
223 return P2->isMemberPointerType() &&
224 P1->getAs<MemberPointerType>()->getMostRecentCXXRecordDecl() ==
225 P2->getAs<MemberPointerType>()->getMostRecentCXXRecordDecl();
226
227 if (P1->isConstantArrayType())
228 return P2->isConstantArrayType() &&
229 cast<ConstantArrayType>(P1)->getSize() ==
230 cast<ConstantArrayType>(P2)->getSize();
231
232 if (P1->isIncompleteArrayType())
233 return P2->isIncompleteArrayType();
234
235 return false;
236 };
237
238 // (2)
239 // Two types From and To are similar if they have cv-decompositions with the
240 // same n such that corresponding P_i components are the same [(added by
241 // N4849 7.3.5) or one is “array of N_i” and the other is “array of unknown
242 // bound of”], and the types denoted by U are the same.
243 //
244 // (3)
245 // A prvalue expression of type From can be converted to type To if the
246 // following conditions are satisfied:
247 // - From and To are similar
248 // - For every i > 0, if const is in cv_i of From then const is in cv_i of
249 // To, and similarly for volatile.
250 // - [(derived from addition by N4849 7.3.5) If P_i of From is “array of
251 // unknown bound of”, P_i of To is “array of unknown bound of”.]
252 // - If the cv_i of From and cv_i of To are different, then const is in every
253 // cv_k of To for 0 < k < i.
254
255 int I = 0;
256 bool ConstUntilI = true;
257 auto SatisfiesCVRules = [&I, &ConstUntilI](const QualType &From,
258 const QualType &To) {
259 if (I > 1) {
260 if (From.getQualifiers() != To.getQualifiers() && !ConstUntilI)
261 return false;
262 }
263
264 if (I > 0) {
265 if (From.isConstQualified() && !To.isConstQualified())
266 return false;
267
268 if (From.isVolatileQualified() && !To.isVolatileQualified())
269 return false;
270
271 ConstUntilI = To.isConstQualified();
272 }
273
274 return true;
275 };
276
277 while (IsValidP_i(From) && IsValidP_i(To)) {
278 // Remove every sugar.
279 From = From.getCanonicalType();
280 To = To.getCanonicalType();
281
282 if (!SatisfiesCVRules(From, To))
283 return false;
284
285 if (!IsSameP_i(From, To)) {
286 if (LangOpts.CPlusPlus20) {
287 if (From->isConstantArrayType() && !To->isIncompleteArrayType())
288 return false;
289
290 if (From->isIncompleteArrayType() && !To->isIncompleteArrayType())
291 return false;
292
293 } else {
294 return false;
295 }
296 }
297
298 ++I;
299 std::optional<QualType> FromPointeeOrElem =
301 std::optional<QualType> ToPointeeOrElem =
303
304 assert(FromPointeeOrElem &&
305 "From pointer or array has no pointee or element!");
306 assert(ToPointeeOrElem && "To pointer or array has no pointee or element!");
307
308 From = *FromPointeeOrElem;
309 To = *ToPointeeOrElem;
310 }
311
312 // In this case the length (n) of From and To are not the same.
313 if (IsValidP_i(From) || IsValidP_i(To))
314 return false;
315
316 // We hit U.
317 if (!SatisfiesCVRules(From, To))
318 return false;
319
320 return From.getTypePtr() == To.getTypePtr();
321}
322
323static bool canThrow(const FunctionDecl *Func) {
324 // consteval specifies that every call to the function must produce a
325 // compile-time constant, which cannot evaluate a throw expression without
326 // producing a compilation error.
327 if (Func->isConsteval())
328 return false;
329
330 const auto *FunProto = Func->getType()->getAs<FunctionProtoType>();
331 if (!FunProto)
332 return true;
333
334 switch (FunProto->canThrow()) {
335 case CT_Cannot:
336 return false;
337 case CT_Dependent: {
338 const Expr *NoexceptExpr = FunProto->getNoexceptExpr();
339 if (!NoexceptExpr)
340 return true; // no noexcept - can throw
341
342 if (NoexceptExpr->isValueDependent())
343 return true; // depend on template - some instance can throw
344
345 bool Result = false;
346 if (!NoexceptExpr->EvaluateAsBooleanCondition(Result, Func->getASTContext(),
347 /*InConstantContext=*/true))
348 return true; // complex X condition in noexcept(X), cannot validate,
349 // assume that may throw
350 return !Result; // noexcept(false) - can throw
351 }
352 default:
353 return true;
354 };
355}
356
359 const ASTContext &Context) {
360 llvm::SmallVector<const Type *, 8> TypesToDelete;
361 for (const auto &ThrownException : ThrownExceptions) {
362 const Type *ExceptionTy = ThrownException.getFirst();
363 const CanQualType ExceptionCanTy =
364 ExceptionTy->getCanonicalTypeUnqualified();
365 const CanQualType HandlerCanTy = HandlerTy->getCanonicalTypeUnqualified();
366
367 // The handler is of type cv T or cv T& and E and T are the same type
368 // (ignoring the top-level cv-qualifiers) ...
369 if (ExceptionCanTy == HandlerCanTy) {
370 TypesToDelete.push_back(ExceptionTy);
371 }
372
373 // The handler is of type cv T or cv T& and T is an unambiguous public base
374 // class of E ...
375 else if (isUnambiguousPublicBaseClass(ExceptionCanTy->getTypePtr(),
376 HandlerCanTy->getTypePtr())) {
377 TypesToDelete.push_back(ExceptionTy);
378 }
379
380 if (HandlerCanTy->getTypeClass() == Type::RValueReference ||
381 (HandlerCanTy->getTypeClass() == Type::LValueReference &&
382 !HandlerCanTy->getTypePtr()->getPointeeType().isConstQualified()))
383 continue;
384 // The handler is of type cv T or const T& where T is a pointer or
385 // pointer-to-member type and E is a pointer or pointer-to-member type that
386 // can be converted to T by one or more of ...
387 if (isPointerOrPointerToMember(HandlerCanTy->getTypePtr()) &&
388 isPointerOrPointerToMember(ExceptionCanTy->getTypePtr())) {
389 // A standard pointer conversion not involving conversions to pointers to
390 // private or protected or ambiguous classes ...
391 if (isStandardPointerConvertible(ExceptionCanTy, HandlerCanTy)) {
392 TypesToDelete.push_back(ExceptionTy);
393 }
394 // A function pointer conversion ...
395 else if (isFunctionPointerConvertible(ExceptionCanTy, HandlerCanTy)) {
396 TypesToDelete.push_back(ExceptionTy);
397 }
398 // A a qualification conversion ...
399 else if (isQualificationConvertiblePointer(ExceptionCanTy, HandlerCanTy,
400 Context.getLangOpts())) {
401 TypesToDelete.push_back(ExceptionTy);
402 }
403 }
404
405 // The handler is of type cv T or const T& where T is a pointer or
406 // pointer-to-member type and E is std::nullptr_t.
407 else if (isPointerOrPointerToMember(HandlerCanTy->getTypePtr()) &&
408 ExceptionCanTy->isNullPtrType()) {
409 TypesToDelete.push_back(ExceptionTy);
410 }
411 }
412
413 Throwables DeletedExceptions;
414
415 for (const Type *TypeToDelete : TypesToDelete) {
416 const auto DeleteIt = ThrownExceptions.find(TypeToDelete);
417 if (DeleteIt != ThrownExceptions.end()) {
418 DeletedExceptions.insert(*DeleteIt);
419 ThrownExceptions.erase(DeleteIt);
420 }
421 }
422
423 reevaluateBehaviour();
424 return DeletedExceptions;
425}
426
429 const llvm::StringSet<> &IgnoredTypes, bool IgnoreBadAlloc) {
430 llvm::SmallVector<const Type *, 8> TypesToDelete;
431 // Note: Using a 'SmallSet' with 'llvm::remove_if()' is not possible.
432 // Therefore this slightly hacky implementation is required.
433 for (const auto &ThrownException : ThrownExceptions) {
434 const Type *T = ThrownException.getFirst();
435 if (const auto *TD = T->getAsTagDecl()) {
436 if (TD->getDeclName().isIdentifier()) {
437 if ((IgnoreBadAlloc &&
438 (TD->getName() == "bad_alloc" && TD->isInStdNamespace())) ||
439 (IgnoredTypes.contains(TD->getName())))
440 TypesToDelete.push_back(T);
441 }
442 }
443 }
444 for (const Type *T : TypesToDelete)
445 ThrownExceptions.erase(T);
446
447 reevaluateBehaviour();
448 return *this;
449}
450
452 Behaviour = State::NotThrowing;
453 ContainsUnknown = false;
454 ThrownExceptions.clear();
455}
456
457void ExceptionAnalyzer::ExceptionInfo::reevaluateBehaviour() {
458 if (ThrownExceptions.empty())
459 if (ContainsUnknown)
460 Behaviour = State::Unknown;
461 else
462 Behaviour = State::NotThrowing;
463 else
464 Behaviour = State::Throwing;
465}
466ExceptionAnalyzer::ExceptionInfo ExceptionAnalyzer::throwsException(
467 const FunctionDecl *Func, const ExceptionInfo::Throwables &Caught,
468 CallStack &CallStack, SourceLocation CallLoc) {
469 if (!Func || CallStack.contains(Func) ||
470 (!CallStack.empty() && !canThrow(Func)))
472
473 if (const Stmt *Body = Func->getBody()) {
474 CallStack.insert({Func, CallLoc});
475 ExceptionInfo Result = throwsException(Body, Caught, CallStack);
476
477 // For a constructor, we also have to check the initializers.
478 if (const auto *Ctor = dyn_cast<CXXConstructorDecl>(Func)) {
479 for (const CXXCtorInitializer *Init : Ctor->inits()) {
480 const ExceptionInfo Excs =
481 throwsException(Init->getInit(), Caught, CallStack);
482 Result.merge(Excs);
483 }
484 }
485
486 CallStack.erase(Func);
487 return Result;
488 }
489
490 auto Result = ExceptionInfo::createUnknown();
491 if (const auto *FPT = Func->getType()->getAs<FunctionProtoType>()) {
492 for (const QualType &Ex : FPT->exceptions()) {
493 CallStack.insert({Func, CallLoc});
494 Result.registerException(
495 Ex.getTypePtr(),
496 {Func->getExceptionSpecSourceRange().getBegin(), CallStack});
497 CallStack.erase(Func);
498 }
499 }
500 return Result;
501}
502
503/// Analyzes a single statement on it's throwing behaviour. This is in principle
504/// possible except some 'Unknown' functions are called.
505ExceptionAnalyzer::ExceptionInfo
506ExceptionAnalyzer::throwsException(const Stmt *St,
507 const ExceptionInfo::Throwables &Caught,
509 auto Results = ExceptionInfo::createNonThrowing();
510 if (!St)
511 return Results;
512
513 if (const auto *Throw = dyn_cast<CXXThrowExpr>(St)) {
514 if (const auto *ThrownExpr = Throw->getSubExpr()) {
515 const auto *ThrownType =
516 ThrownExpr->getType()->getUnqualifiedDesugaredType();
517 if (ThrownType->isReferenceType())
518 ThrownType = ThrownType->castAs<ReferenceType>()
519 ->getPointeeType()
520 ->getUnqualifiedDesugaredType();
521 Results.registerException(
522 ThrownExpr->getType()->getUnqualifiedDesugaredType(),
523 {Throw->getBeginLoc(), CallStack});
524 } else
525 // A rethrow of a caught exception happens which makes it possible
526 // to throw all exception that are caught in the 'catch' clause of
527 // the parent try-catch block.
528 Results.registerExceptions(Caught);
529 } else if (const auto *Try = dyn_cast<CXXTryStmt>(St)) {
530 ExceptionInfo Uncaught =
531 throwsException(Try->getTryBlock(), Caught, CallStack);
532 for (unsigned I = 0; I < Try->getNumHandlers(); ++I) {
533 const CXXCatchStmt *Catch = Try->getHandler(I);
534
535 // Everything is caught through 'catch(...)'.
536 if (!Catch->getExceptionDecl()) {
537 const ExceptionInfo Rethrown = throwsException(
538 Catch->getHandlerBlock(), Uncaught.getExceptions(), CallStack);
539 Results.merge(Rethrown);
540 Uncaught.clear();
541 } else {
542 const auto *CaughtType =
543 Catch->getCaughtType()->getUnqualifiedDesugaredType();
544 if (CaughtType->isReferenceType()) {
545 CaughtType = CaughtType->castAs<ReferenceType>()
546 ->getPointeeType()
547 ->getUnqualifiedDesugaredType();
548 }
549
550 // If the caught exception will catch multiple previously potential
551 // thrown types (because it's sensitive to inheritance) the throwing
552 // situation changes. First of all filter the exception types and
553 // analyze if the baseclass-exception is rethrown.
554 const ExceptionInfo::Throwables FilteredExceptions =
555 Uncaught.filterByCatch(CaughtType,
556 Catch->getExceptionDecl()->getASTContext());
557 if (!FilteredExceptions.empty()) {
558 const ExceptionInfo Rethrown = throwsException(
559 Catch->getHandlerBlock(), FilteredExceptions, CallStack);
560 Results.merge(Rethrown);
561 }
562 }
563 }
564 Results.merge(Uncaught);
565 } else if (const auto *DefaultInit = dyn_cast<CXXDefaultInitExpr>(St)) {
566 const ExceptionInfo Excs =
567 throwsException(DefaultInit->getExpr(), Caught, CallStack);
568 Results.merge(Excs);
569 } else if (const auto *Coro = dyn_cast<CoroutineBodyStmt>(St)) {
570 for (const Stmt *Child : Coro->childrenExclBody()) {
571 if (Child != Coro->getExceptionHandler()) {
572 const ExceptionInfo Excs = throwsException(Child, Caught, CallStack);
573 Results.merge(Excs);
574 }
575 }
576 const ExceptionInfo Excs =
577 throwsException(Coro->getBody(), Caught, CallStack);
578 Results.merge(throwsException(Coro->getExceptionHandler(),
579 Excs.getExceptions(), CallStack));
580 for (const auto &Exception : Excs.getExceptions()) {
581 const Type *ExcType = Exception.getFirst();
582 if (const CXXRecordDecl *ThrowableRec = ExcType->getAsCXXRecordDecl()) {
583 const ExceptionInfo DestructorExcs = throwsException(
584 ThrowableRec->getDestructor(), Caught, CallStack, SourceLocation{});
585 Results.merge(DestructorExcs);
586 }
587 }
588 } else if (const auto *Lambda = dyn_cast<LambdaExpr>(St)) {
589 for (const Stmt *Init : Lambda->capture_inits()) {
590 const ExceptionInfo Excs = throwsException(Init, Caught, CallStack);
591 Results.merge(Excs);
592 }
593 } else {
594 // Check whether any of this node's subexpressions throws.
595 for (const Stmt *Child : St->children()) {
596 const ExceptionInfo Excs = throwsException(Child, Caught, CallStack);
597 Results.merge(Excs);
598 }
599
600 // If this node is a call to a function or constructor, also check
601 // whether the call itself throws.
602 if (const auto *Call = dyn_cast<CallExpr>(St)) {
603 if (const FunctionDecl *Func = Call->getDirectCallee()) {
604 ExceptionInfo Excs =
605 throwsException(Func, Caught, CallStack, Call->getBeginLoc());
606 Results.merge(Excs);
607 }
608 } else if (const auto *Construct = dyn_cast<CXXConstructExpr>(St)) {
609 ExceptionInfo Excs = throwsException(Construct->getConstructor(), Caught,
610 CallStack, Construct->getBeginLoc());
611 Results.merge(Excs);
612 }
613 }
614 return Results;
615}
616
617ExceptionAnalyzer::ExceptionInfo
618ExceptionAnalyzer::analyzeImpl(const FunctionDecl *Func) {
619 ExceptionInfo ExceptionList;
620
621 // Check if the function has already been analyzed and reuse that result.
622 const auto CacheEntry = FunctionCache.find(Func);
623 if (CacheEntry == FunctionCache.end()) {
625 ExceptionList = throwsException(Func, ExceptionInfo::Throwables(),
626 CallStack, Func->getLocation());
627
628 // Cache the result of the analysis. This is done prior to filtering
629 // because it is best to keep as much information as possible.
630 // The results here might be relevant to different analysis passes
631 // with different needs as well.
632 FunctionCache.try_emplace(Func, ExceptionList);
633 } else
634 ExceptionList = CacheEntry->getSecond();
635
636 return ExceptionList;
637}
638
639ExceptionAnalyzer::ExceptionInfo
640ExceptionAnalyzer::analyzeImpl(const Stmt *Stmt) {
642 return throwsException(Stmt, ExceptionInfo::Throwables(), CallStack);
643}
644
645template <typename T>
646ExceptionAnalyzer::ExceptionInfo
647ExceptionAnalyzer::analyzeDispatch(const T *Node) {
648 ExceptionInfo ExceptionList = analyzeImpl(Node);
649
650 if (ExceptionList.getBehaviour() == State::NotThrowing ||
651 ExceptionList.getBehaviour() == State::Unknown)
652 return ExceptionList;
653
654 // Remove all ignored exceptions from the list of exceptions that can be
655 // thrown.
656 ExceptionList.filterIgnoredExceptions(IgnoredExceptions, IgnoreBadAlloc);
657
658 return ExceptionList;
659}
660
661ExceptionAnalyzer::ExceptionInfo
662ExceptionAnalyzer::analyze(const FunctionDecl *Func) {
663 return analyzeDispatch(Func);
664}
665
667 return analyzeDispatch(Stmt);
668}
669
670} // 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:1678
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.