Debian Codegraph @

llvm-project/clang-tools-extra/clang-tidy/bugprone/ExceptionEscapeCheck.cpp

raw
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::bugprone {
27 ExceptionEscapeCheck::ExceptionEscapeCheck(StringRef Name,
28 ClangTidyContext *Context)
29 : ClangTidyCheck(Name, Context), RawFunctionsThatShouldNotThrow(Options.get(
30 "FunctionsThatShouldNotThrow", "")),
31 RawIgnoredExceptions(Options.get("IgnoredExceptions", "")) {
32 llvm::SmallVector<StringRef, 8> FunctionsThatShouldNotThrowVec,
33 IgnoredExceptionsVec;
34 StringRef(RawFunctionsThatShouldNotThrow)
35 .split(FunctionsThatShouldNotThrowVec, ",", -1, false);
36 FunctionsThatShouldNotThrow.insert(FunctionsThatShouldNotThrowVec.begin(),
37 FunctionsThatShouldNotThrowVec.end());
38
39 llvm::StringSet<> IgnoredExceptions;
40 StringRef(RawIgnoredExceptions).split(IgnoredExceptionsVec, ",", -1, false);
41 IgnoredExceptions.insert(IgnoredExceptionsVec.begin(),
42 IgnoredExceptionsVec.end());
43 Tracer.ignoreExceptions(std::move(IgnoredExceptions));
44 Tracer.ignoreBadAlloc(true);
45 }
46
47 void ExceptionEscapeCheck::storeOptions(ClangTidyOptions::OptionMap &Opts) {
48 Options.store(Opts, "FunctionsThatShouldNotThrow",
49 RawFunctionsThatShouldNotThrow);
50 Options.store(Opts, "IgnoredExceptions", RawIgnoredExceptions);
51 }
52
53 void ExceptionEscapeCheck::registerMatchers(MatchFinder *Finder) {
54 Finder->addMatcher(
55 functionDecl(anyOf(isNoThrow(), cxxDestructorDecl(),
56 cxxConstructorDecl(isMoveConstructor()),
57 cxxMethodDecl(isMoveAssignmentOperator()),
58 hasName("main"), hasName("swap"),
59 isEnabled(FunctionsThatShouldNotThrow)))
60 .bind("thrower"),
61 this);
62 }
63
64 void ExceptionEscapeCheck::check(const MatchFinder::MatchResult &Result) {
65 const auto *MatchedDecl = Result.Nodes.getNodeAs<FunctionDecl>("thrower");
66
67 if (!MatchedDecl)
68 return;
69
70 if (Tracer.analyze(MatchedDecl).getBehaviour() ==
71 utils::ExceptionAnalyzer::State::Throwing)
72 // FIXME: We should provide more information about the exact location where
73 // the exception is thrown, maybe the full path the exception escapes
74 diag(MatchedDecl->getLocation(), "an exception may be thrown in function "
75 "%0 which should not throw exceptions")
76 << MatchedDecl;
77 }
78
79 } // namespace tidy::bugprone
80 } // namespace clang