12#include "clang/AST/ASTContext.h"
13#include "clang/ASTMatchers/ASTMatchFinder.h"
14#include "clang/Lex/Lexer.h"
17using ::clang::ast_matchers::internal::Matcher;
23 return Node.getBeginLoc().isMacroID() || Node.getEndLoc().isMacroID() ||
24 Node.getCatchLoc().isMacroID();
27AST_MATCHER_P(CXXCatchStmt, hasHandler, Matcher<Stmt>, InnerMatcher) {
28 const Stmt *Handler = Node.getHandlerBlock();
31 return InnerMatcher.matches(*Handler, Finder, Builder);
34AST_MATCHER_P(CXXCatchStmt, hasCaughtType, Matcher<QualType>, InnerMatcher) {
35 return InnerMatcher.matches(Node.getCaughtType(), Finder, Builder);
38AST_MATCHER_P(CompoundStmt, hasAnyTextFromList, std::vector<StringRef>, List) {
42 ASTContext &Context = Finder->getASTContext();
43 const SourceManager &SM = Context.getSourceManager();
44 StringRef
Text = Lexer::getSourceText(
45 CharSourceRange::getTokenRange(Node.getSourceRange()), SM,
46 Context.getLangOpts());
47 return llvm::any_of(List, [&](
const StringRef &Str) {
48 return Text.contains_insensitive(Str);
56 IgnoreCatchWithKeywords(
utils::options::parseStringList(
57 Options.get(
"IgnoreCatchWithKeywords",
"@TODO;@FIXME"))),
58 AllowEmptyCatchForExceptions(
utils::options::parseStringList(
59 Options.get(
"AllowEmptyCatchForExceptions",
""))) {}
62 Options.store(Opts,
"IgnoreCatchWithKeywords",
65 Opts,
"AllowEmptyCatchForExceptions",
70 const LangOptions &LangOpts)
const {
71 return LangOpts.CPlusPlus;
75 return TK_IgnoreUnlessSpelledInSource;
79 auto AllowedNamedExceptionDecl = namedDecl(
81 auto AllowedNamedExceptionTypes =
82 qualType(anyOf(hasDeclaration(AllowedNamedExceptionDecl),
83 references(AllowedNamedExceptionDecl),
84 pointsTo(AllowedNamedExceptionDecl)));
85 auto IgnoredExceptionType =
86 qualType(anyOf(AllowedNamedExceptionTypes,
87 hasCanonicalType(AllowedNamedExceptionTypes)));
90 cxxCatchStmt(unless(isInMacro()),
91 unless(hasCaughtType(IgnoredExceptionType)),
92 hasHandler(compoundStmt(
94 unless(hasAnyTextFromList(IgnoreCatchWithKeywords)))))
100 const auto *MatchedCatchStmt = Result.Nodes.getNodeAs<CXXCatchStmt>(
"catch");
103 MatchedCatchStmt->getCatchLoc(),
104 "empty catch statements hide issues; to handle exceptions appropriately, "
105 "consider re-throwing, handling, or avoiding catch altogether");
Every ClangTidyCheck reports errors through a DiagnosticsEngine provided by this context.
EmptyCatchCheck(StringRef Name, ClangTidyContext *Context)
std::optional< TraversalKind > getCheckTraversalKind() const override
void storeOptions(ClangTidyOptions::OptionMap &Opts) override
void registerMatchers(ast_matchers::MatchFinder *Finder) override
bool isLanguageVersionSupported(const LangOptions &LangOpts) const override
void check(const ast_matchers::MatchFinder::MatchResult &Result) override
AST_MATCHER_P(Stmt, isStatementIdenticalToBoundNode, std::string, ID)
AST_MATCHER(BinaryOperator, isRelationalOperator)
inline ::clang::ast_matchers::internal::Matcher< NamedDecl > matchesAnyListedRegexName(llvm::ArrayRef< StringRef > NameList)
std::string serializeStringList(ArrayRef< StringRef > Strings)
Serialize a sequence of names that can be parsed by parseStringList.
llvm::StringMap< ClangTidyValue > OptionMap