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

Provided by KDAB

Privacy Policy
Update your C++ knowledge – Modern C++11/14/17 Training
Find out more

source code of clang-tools-extra/clang-tidy/utils/ExceptionAnalyzer.cpp