1//===--- EmptyCatchCheck.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 "EmptyCatchCheck.h"
10#include "../utils/Matchers.h"
11#include "../utils/OptionsUtils.h"
12#include "clang/AST/ASTContext.h"
13#include "clang/ASTMatchers/ASTMatchFinder.h"
14#include "clang/Lex/Lexer.h"
15#include <algorithm>
16
17using namespace clang::ast_matchers;
18using ::clang::ast_matchers::internal::Matcher;
19
20namespace clang::tidy::bugprone {
21
22namespace {
23AST_MATCHER(CXXCatchStmt, isInMacro) {
24 return Node.getBeginLoc().isMacroID() || Node.getEndLoc().isMacroID() ||
25 Node.getCatchLoc().isMacroID();
26}
27
28AST_MATCHER_P(CXXCatchStmt, hasHandler, Matcher<Stmt>, InnerMatcher) {
29 Stmt *Handler = Node.getHandlerBlock();
30 if (!Handler)
31 return false;
32 return InnerMatcher.matches(Node: *Handler, Finder, Builder);
33}
34
35AST_MATCHER_P(CXXCatchStmt, hasCaughtType, Matcher<QualType>, InnerMatcher) {
36 return InnerMatcher.matches(Node: Node.getCaughtType(), Finder, Builder);
37}
38
39AST_MATCHER_P(CompoundStmt, hasAnyTextFromList, std::vector<llvm::StringRef>,
40 List) {
41 if (List.empty())
42 return false;
43
44 ASTContext &Context = Finder->getASTContext();
45 SourceManager &SM = Context.getSourceManager();
46 StringRef Text = Lexer::getSourceText(
47 Range: CharSourceRange::getTokenRange(Node.getSourceRange()), SM,
48 LangOpts: Context.getLangOpts());
49 return llvm::any_of(Range: List, P: [&](const StringRef &Str) {
50 return Text.contains_insensitive(Str);
51 });
52}
53
54} // namespace
55
56EmptyCatchCheck::EmptyCatchCheck(StringRef Name, ClangTidyContext *Context)
57 : ClangTidyCheck(Name, Context),
58 IgnoreCatchWithKeywords(utils::options::parseStringList(
59 Option: Options.get(LocalName: "IgnoreCatchWithKeywords", Default: "@TODO;@FIXME"))),
60 AllowEmptyCatchForExceptions(utils::options::parseStringList(
61 Option: Options.get(LocalName: "AllowEmptyCatchForExceptions", Default: ""))) {}
62
63void EmptyCatchCheck::storeOptions(ClangTidyOptions::OptionMap &Opts) {
64 Options.store(Options&: Opts, LocalName: "IgnoreCatchWithKeywords",
65 Value: utils::options::serializeStringList(Strings: IgnoreCatchWithKeywords));
66 Options.store(
67 Options&: Opts, LocalName: "AllowEmptyCatchForExceptions",
68 Value: utils::options::serializeStringList(Strings: AllowEmptyCatchForExceptions));
69}
70
71bool EmptyCatchCheck::isLanguageVersionSupported(
72 const LangOptions &LangOpts) const {
73 return LangOpts.CPlusPlus;
74}
75
76std::optional<TraversalKind> EmptyCatchCheck::getCheckTraversalKind() const {
77 return TK_IgnoreUnlessSpelledInSource;
78}
79
80void EmptyCatchCheck::registerMatchers(MatchFinder *Finder) {
81 auto AllowedNamedExceptionDecl =
82 namedDecl(matchers::matchesAnyListedName(NameList: AllowEmptyCatchForExceptions));
83 auto AllowedNamedExceptionTypes =
84 qualType(anyOf(hasDeclaration(InnerMatcher: AllowedNamedExceptionDecl),
85 references(InnerMatcher: AllowedNamedExceptionDecl),
86 pointsTo(InnerMatcher: AllowedNamedExceptionDecl)));
87 auto IgnoredExceptionType =
88 qualType(anyOf(AllowedNamedExceptionTypes,
89 hasCanonicalType(InnerMatcher: AllowedNamedExceptionTypes)));
90
91 Finder->addMatcher(
92 NodeMatch: cxxCatchStmt(unless(isExpansionInSystemHeader()), unless(isInMacro()),
93 unless(hasCaughtType(InnerMatcher: IgnoredExceptionType)),
94 hasHandler(InnerMatcher: compoundStmt(
95 statementCountIs(N: 0),
96 unless(hasAnyTextFromList(List: IgnoreCatchWithKeywords)))))
97 .bind(ID: "catch"),
98 Action: this);
99}
100
101void EmptyCatchCheck::check(const MatchFinder::MatchResult &Result) {
102 const auto *MatchedCatchStmt = Result.Nodes.getNodeAs<CXXCatchStmt>(ID: "catch");
103
104 diag(
105 Loc: MatchedCatchStmt->getCatchLoc(),
106 Description: "empty catch statements hide issues; to handle exceptions appropriately, "
107 "consider re-throwing, handling, or avoiding catch altogether");
108}
109
110} // namespace clang::tidy::bugprone
111

source code of clang-tools-extra/clang-tidy/bugprone/EmptyCatchCheck.cpp