clang-tools 23.0.0git
UnsafeToAllowExceptionsCheck.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
11#include "clang/ASTMatchers/ASTMatchFinder.h"
12
13using namespace clang::ast_matchers;
14
15namespace clang::tidy::bugprone {
16namespace {
17
18AST_MATCHER(FunctionDecl, isExplicitThrow) {
19 return isExplicitThrowExceptionSpec(Node.getExceptionSpecType()) &&
20 Node.getExceptionSpecSourceRange().isValid();
21}
22
23} // namespace
24
26 StringRef Name, ClangTidyContext *Context)
27 : ClangTidyCheck(Name, Context),
28 CheckedSwapFunctions(utils::options::parseStringList(
29 Options.get("CheckedSwapFunctions", "swap;iter_swap;iter_move"))) {}
30
33 Options.store(Opts, "CheckedSwapFunctions",
34 utils::options::serializeStringList(CheckedSwapFunctions));
35}
36
38 Finder->addMatcher(
39 functionDecl(isDefinition(), isExplicitThrow(),
40 anyOf(cxxDestructorDecl(),
41 cxxConstructorDecl(isMoveConstructor()),
42 cxxMethodDecl(isMoveAssignmentOperator()),
43 allOf(hasAnyName(CheckedSwapFunctions),
44 unless(parameterCountIs(0))),
45 isMain()))
46 .bind("f"),
47 this);
48}
49
51 const MatchFinder::MatchResult &Result) {
52 const auto *MatchedDecl = Result.Nodes.getNodeAs<FunctionDecl>("f");
53 assert(MatchedDecl);
54
55 diag(MatchedDecl->getLocation(),
56 "function %0 should not throw exceptions but "
57 "it is still marked as potentially throwing")
58 << MatchedDecl;
59}
60
61} // 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 registerMatchers(ast_matchers::MatchFinder *Finder) override
void storeOptions(ClangTidyOptions::OptionMap &Opts) override
UnsafeToAllowExceptionsCheck(StringRef Name, ClangTidyContext *Context)
AST_MATCHER(BinaryOperator, isRelationalOperator)
std::string serializeStringList(ArrayRef< StringRef > Strings)
Serialize a sequence of names that can be parsed by parseStringList.
llvm::StringMap< ClangTidyValue > OptionMap