clang-tools 10.0.0

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
9 #include "ExceptionEscapeCheck.h"
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
16 using namespace clang::ast_matchers;
17
18 namespace clang {
19 namespace {
20 AST_MATCHER_P(FunctionDecl, isEnabled, llvm::StringSet<>,
21 FunctionsThatShouldNotThrow) {
22 return FunctionsThatShouldNotThrow.count(Node.getNameAsString()) > 0;
23 }
24 } // namespace
25
26 namespace tidy {
27 namespace bugprone {
28 ExceptionEscapeCheck::ExceptionEscapeCheck(StringRef Name,
29 ClangTidyContext *Context)
30 : ClangTidyCheck(Name, Context), RawFunctionsThatShouldNotThrow(Options.get(
31 "FunctionsThatShouldNotThrow", "")),
32 RawIgnoredExceptions(Options.get("IgnoredExceptions", "")) {
33 llvm::SmallVector<StringRef, 8> FunctionsThatShouldNotThrowVec,
34 IgnoredExceptionsVec;
35 StringRef(RawFunctionsThatShouldNotThrow)
36 .split(FunctionsThatShouldNotThrowVec, ",", -1, false);
37 FunctionsThatShouldNotThrow.insert(FunctionsThatShouldNotThrowVec.begin(),
38 FunctionsThatShouldNotThrowVec.end());
39
40 llvm::StringSet<> IgnoredExceptions;
41 StringRef(RawIgnoredExceptions).split(IgnoredExceptionsVec, ",", -1, false);
42 IgnoredExceptions.insert(IgnoredExceptionsVec.begin(),
43 IgnoredExceptionsVec.end());
44 Tracer.ignoreExceptions(std::move(IgnoredExceptions));
45 Tracer.ignoreBadAlloc(true);
46 }
47
48 void ExceptionEscapeCheck::storeOptions(ClangTidyOptions::OptionMap &Opts) {
49 Options.store(Opts, "FunctionsThatShouldNotThrow",
50 RawFunctionsThatShouldNotThrow);
51 Options.store(Opts, "IgnoredExceptions", RawIgnoredExceptions);
52 }
53
54 void ExceptionEscapeCheck::registerMatchers(MatchFinder *Finder) {
55 if (!getLangOpts().CPlusPlus || !getLangOpts().CXXExceptions)
56 return;
57
58 Finder->addMatcher(
59 functionDecl(anyOf(isNoThrow(), cxxDestructorDecl(),
60 cxxConstructorDecl(isMoveConstructor()),
61 cxxMethodDecl(isMoveAssignmentOperator()),
62 hasName("main"), hasName("swap"),
63 isEnabled(FunctionsThatShouldNotThrow)))
64 .bind("thrower"),
65 this);
66 }
67
68 void ExceptionEscapeCheck::check(const MatchFinder::MatchResult &Result) {
69 const auto *MatchedDecl = Result.Nodes.getNodeAs<FunctionDecl>("thrower");
70
71 if (!MatchedDecl)
72 return;
73
74 if (Tracer.analyze(MatchedDecl).getBehaviour() ==
75 utils::ExceptionAnalyzer::State::Throwing)
76 // FIXME: We should provide more information about the exact location where
77 // the exception is thrown, maybe the full path the exception escapes
78 diag(MatchedDecl->getLocation(),
79 "an exception may be thrown in function %0 "
80
81 "which should not throw exceptions")
82 << MatchedDecl;
83 }
84
85 } // namespace bugprone
86 } // namespace tidy
87 } // namespace clang
clang::ast_matchers
Definition: AbseilMatcher.h:14
clang::tidy::ClangTidyCheck
Base class for all clang-tidy checks.
Definition: ClangTidyCheck.h:47
clang::tidy::ClangTidyCheck::getLangOpts
const LangOptions & getLangOpts() const
Returns the language options from the context.
Definition: ClangTidyCheck.h:188
clang::tidy::utils::ExceptionAnalyzer::ExceptionInfo::getBehaviour
State getBehaviour() const
Definition: ExceptionAnalyzer.h:61
clang::tidy::bugprone::ExceptionEscapeCheck::check
void check(const ast_matchers::MatchFinder::MatchResult &Result) override
ClangTidyChecks that register ASTMatchers should do the actual work in here.
clang::tidy::ClangTidyCheck::Options
OptionsView Options
Definition: ClangTidyCheck.h:184
clang::tidy::bugprone::ExceptionEscapeCheck::storeOptions
void storeOptions(ClangTidyOptions::OptionMap &Opts) override
Should store all options supported by this check with their current values or default values for opti...
Definition: bugprone/ExceptionEscapeCheck.cpp:48
clang::tidy::ClangTidyCheck::OptionsView::store
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.
Definition: ClangTidyCheck.cpp:58
Name
static constexpr llvm::StringLiteral Name
Definition: UppercaseLiteralSuffixCheck.cpp:27
clang::tidy::ClangTidyOptions::OptionMap
std::map< std::string, std::string > OptionMap
Definition: ClangTidyOptions.h:97
ExceptionEscapeCheck.h
clang::tidy::utils::ExceptionAnalyzer::State::Throwing
The function can definitely throw given an AST.
clang
===– Representation.cpp - ClangDoc Representation --------—*- C++ -*-===//
Definition: ApplyReplacements.h:27
clang::tidy::utils::ExceptionAnalyzer::analyze
ExceptionInfo analyze(const FunctionDecl *Func)
Definition: ExceptionAnalyzer.cpp:250
clang::tidy::utils::ExceptionAnalyzer::ignoreBadAlloc
void ignoreBadAlloc(bool ShallIgnore)
Definition: ExceptionAnalyzer.h:126
clang::tidy::ClangTidyContext
Every ClangTidyCheck reports errors through a DiagnosticsEngine provided by this context.
Definition: ClangTidyDiagnosticConsumer.h:77
clang::tidy::bugprone::ExceptionEscapeCheck::registerMatchers
void registerMatchers(ast_matchers::MatchFinder *Finder) override
Override this to register AST matchers with Finder.
clang::tidy::ClangTidyCheck::diag
DiagnosticBuilder diag(SourceLocation Loc, StringRef Description, DiagnosticIDs::Level Level=DiagnosticIDs::Warning)
Add a diagnostic with the check&#39;s name.
Definition: ClangTidyCheck.cpp:21
clang::tidy::utils::ExceptionAnalyzer::ignoreExceptions
void ignoreExceptions(llvm::StringSet<> ExceptionNames)
Definition: ExceptionAnalyzer.h:127
clang::tidy::readability::AST_MATCHER_P
AST_MATCHER_P(CXXMethodDecl, hasCanonicalDecl, ast_matchers::internal::Matcher< CXXMethodDecl >, InnerMatcher)
Definition: ConvertMemberFunctionsToStatic.cpp:52