clang-tools 22.0.0git
bugprone/ExceptionEscapeCheck.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
10
11#include "clang/ASTMatchers/ASTMatchFinder.h"
12#include "llvm/ADT/StringSet.h"
13
14using namespace clang::ast_matchers;
15
16namespace clang::tidy::bugprone {
17namespace {
18
19AST_MATCHER_P(FunctionDecl, isEnabled, llvm::StringSet<>,
20 FunctionsThatShouldNotThrow) {
21 return FunctionsThatShouldNotThrow.contains(Node.getNameAsString());
22}
23
24AST_MATCHER(FunctionDecl, isExplicitThrow) {
25 return isExplicitThrowExceptionSpec(Node.getExceptionSpecType()) &&
26 Node.getExceptionSpecSourceRange().isValid();
27}
28
29AST_MATCHER(FunctionDecl, hasAtLeastOneParameter) {
30 return Node.getNumParams() > 0;
31}
32
33} // namespace
34
36 ClangTidyContext *Context)
37 : ClangTidyCheck(Name, Context), RawFunctionsThatShouldNotThrow(Options.get(
38 "FunctionsThatShouldNotThrow", "")),
39 RawIgnoredExceptions(Options.get("IgnoredExceptions", "")) {
40 llvm::SmallVector<StringRef, 8> FunctionsThatShouldNotThrowVec,
41 IgnoredExceptionsVec;
42 StringRef(RawFunctionsThatShouldNotThrow)
43 .split(FunctionsThatShouldNotThrowVec, ",", -1, false);
44 FunctionsThatShouldNotThrow.insert_range(FunctionsThatShouldNotThrowVec);
45
46 llvm::StringSet<> IgnoredExceptions;
47 StringRef(RawIgnoredExceptions).split(IgnoredExceptionsVec, ",", -1, false);
48 IgnoredExceptions.insert_range(IgnoredExceptionsVec);
49 Tracer.ignoreExceptions(std::move(IgnoredExceptions));
50 Tracer.ignoreBadAlloc(true);
51}
52
54 Options.store(Opts, "FunctionsThatShouldNotThrow",
55 RawFunctionsThatShouldNotThrow);
56 Options.store(Opts, "IgnoredExceptions", RawIgnoredExceptions);
57}
58
59void ExceptionEscapeCheck::registerMatchers(MatchFinder *Finder) {
60 Finder->addMatcher(
61 functionDecl(
62 isDefinition(),
63 anyOf(isNoThrow(),
64 allOf(anyOf(cxxDestructorDecl(),
65 cxxConstructorDecl(isMoveConstructor()),
66 cxxMethodDecl(isMoveAssignmentOperator()), isMain(),
67 allOf(hasAnyName("swap", "iter_swap", "iter_move"),
68 hasAtLeastOneParameter())),
69 unless(isExplicitThrow())),
70 isEnabled(FunctionsThatShouldNotThrow)))
71 .bind("thrower"),
72 this);
73}
74
75void ExceptionEscapeCheck::check(const MatchFinder::MatchResult &Result) {
76 const auto *MatchedDecl = Result.Nodes.getNodeAs<FunctionDecl>("thrower");
77
78 if (!MatchedDecl)
79 return;
80
82 Tracer.analyze(MatchedDecl);
83
84 if (Info.getBehaviour() != utils::ExceptionAnalyzer::State::Throwing)
85 return;
86
87 diag(MatchedDecl->getLocation(), "an exception may be thrown in function "
88 "%0 which should not throw exceptions")
89 << MatchedDecl;
90
91 const auto &[ThrowType, ThrowInfo] = *Info.getExceptions().begin();
92
93 if (ThrowInfo.Loc.isInvalid())
94 return;
95
96 const utils::ExceptionAnalyzer::CallStack &Stack = ThrowInfo.Stack;
97 diag(ThrowInfo.Loc,
98 "frame #0: unhandled exception of type %0 may be thrown in function %1 "
99 "here",
100 DiagnosticIDs::Note)
101 << QualType(ThrowType, 0U) << Stack.back().first;
102
103 size_t FrameNo = 1;
104 for (auto CurrIt = ++Stack.rbegin(), PrevIt = Stack.rbegin();
105 CurrIt != Stack.rend(); ++CurrIt, ++PrevIt) {
106 const FunctionDecl *CurrFunction = CurrIt->first;
107 const FunctionDecl *PrevFunction = PrevIt->first;
108 const SourceLocation PrevLocation = PrevIt->second;
109 if (PrevLocation.isValid()) {
110 diag(PrevLocation, "frame #%0: function %1 calls function %2 here",
111 DiagnosticIDs::Note)
112 << FrameNo << CurrFunction << PrevFunction;
113 } else {
114 diag(CurrFunction->getLocation(),
115 "frame #%0: function %1 calls function %2", DiagnosticIDs::Note)
116 << FrameNo << CurrFunction << PrevFunction;
117 }
118 ++FrameNo;
119 }
120}
121
122} // namespace clang::tidy::bugprone
Every ClangTidyCheck reports errors through a DiagnosticsEngine provided by this context.
void check(const ast_matchers::MatchFinder::MatchResult &Result) override
void storeOptions(ClangTidyOptions::OptionMap &Opts) override
ExceptionEscapeCheck(StringRef Name, ClangTidyContext *Context)
void registerMatchers(ast_matchers::MatchFinder *Finder) override
Bundle the gathered information about an entity like a function regarding it's exception behaviour.
@ Throwing
The function can definitely 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)
AST_MATCHER_P(Stmt, isStatementIdenticalToBoundNode, std::string, ID)
AST_MATCHER(BinaryOperator, isRelationalOperator)
llvm::StringMap< ClangTidyValue > OptionMap