clang-tools 18.0.0git
bugprone/ExceptionEscapeCheck.cpp
Go to the documentation of this file.
1//===--- ExceptionEscapeCheck.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
10
11#include "clang/AST/ASTContext.h"
12#include "clang/ASTMatchers/ASTMatchFinder.h"
13#include "llvm/ADT/SmallSet.h"
14#include "llvm/ADT/StringSet.h"
15
16using namespace clang::ast_matchers;
17
18namespace clang::tidy::bugprone {
19namespace {
20
21AST_MATCHER_P(FunctionDecl, isEnabled, llvm::StringSet<>,
22 FunctionsThatShouldNotThrow) {
23 return FunctionsThatShouldNotThrow.count(Node.getNameAsString()) > 0;
24}
25
26AST_MATCHER(FunctionDecl, isExplicitThrow) {
27 return isExplicitThrowExceptionSpec(Node.getExceptionSpecType()) &&
28 Node.getExceptionSpecSourceRange().isValid();
29}
30
31} // namespace
32
34 ClangTidyContext *Context)
35 : ClangTidyCheck(Name, Context), RawFunctionsThatShouldNotThrow(Options.get(
36 "FunctionsThatShouldNotThrow", "")),
37 RawIgnoredExceptions(Options.get("IgnoredExceptions", "")) {
38 llvm::SmallVector<StringRef, 8> FunctionsThatShouldNotThrowVec,
39 IgnoredExceptionsVec;
40 StringRef(RawFunctionsThatShouldNotThrow)
41 .split(FunctionsThatShouldNotThrowVec, ",", -1, false);
42 FunctionsThatShouldNotThrow.insert(FunctionsThatShouldNotThrowVec.begin(),
43 FunctionsThatShouldNotThrowVec.end());
44
45 llvm::StringSet<> IgnoredExceptions;
46 StringRef(RawIgnoredExceptions).split(IgnoredExceptionsVec, ",", -1, false);
47 IgnoredExceptions.insert(IgnoredExceptionsVec.begin(),
48 IgnoredExceptionsVec.end());
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(isDefinition(),
62 anyOf(isNoThrow(),
63 allOf(anyOf(cxxDestructorDecl(),
64 cxxConstructorDecl(isMoveConstructor()),
65 cxxMethodDecl(isMoveAssignmentOperator()),
66 isMain(), hasName("swap")),
67 unless(isExplicitThrow())),
68 isEnabled(FunctionsThatShouldNotThrow)))
69 .bind("thrower"),
70 this);
71}
72
73void ExceptionEscapeCheck::check(const MatchFinder::MatchResult &Result) {
74 const auto *MatchedDecl = Result.Nodes.getNodeAs<FunctionDecl>("thrower");
75
76 if (!MatchedDecl)
77 return;
78
79 if (Tracer.analyze(MatchedDecl).getBehaviour() ==
81 // FIXME: We should provide more information about the exact location where
82 // the exception is thrown, maybe the full path the exception escapes
83 diag(MatchedDecl->getLocation(), "an exception may be thrown in function "
84 "%0 which should not throw exceptions")
85 << MatchedDecl;
86}
87
88} // namespace clang::tidy::bugprone
llvm::StringRef Name
::clang::DynTypedNode Node
void store(ClangTidyOptions::OptionMap &Options, StringRef LocalName, StringRef Value) const
Stores an option with the check-local name LocalName with string value Value to Options.
Base class for all clang-tidy checks.
DiagnosticBuilder diag(SourceLocation Loc, StringRef Description, DiagnosticIDs::Level Level=DiagnosticIDs::Warning)
Add a diagnostic with the check's name.
Every ClangTidyCheck reports errors through a DiagnosticsEngine provided by this context.
void check(const ast_matchers::MatchFinder::MatchResult &Result) override
ClangTidyChecks that register ASTMatchers should do the actual work in here.
void storeOptions(ClangTidyOptions::OptionMap &Opts) override
Should store all options supported by this check with their current values or default values for opti...
ExceptionEscapeCheck(StringRef Name, ClangTidyContext *Context)
void registerMatchers(ast_matchers::MatchFinder *Finder) override
Override this to register AST matchers with Finder.
void ignoreExceptions(llvm::StringSet<> ExceptionNames)
@ Throwing
The function can definitely throw given an AST.
ExceptionInfo analyze(const FunctionDecl *Func)
AST_MATCHER_P(FunctionDecl, parameterCountGE, unsigned, N)
Matches functions that have at least the specified amount of parameters.
AST_MATCHER(clang::VarDecl, hasConstantDeclaration)
llvm::StringMap< ClangTidyValue > OptionMap