=?utf-8?q?Balázs_Kéri?= <[email protected]>, =?utf-8?q?Balázs_Kéri?= <[email protected]>, =?utf-8?q?Balázs_Kéri?= <[email protected]>, =?utf-8?q?Balázs_Kéri?= <[email protected]>, =?utf-8?q?Balázs_Kéri?= <[email protected]> Message-ID: In-Reply-To: <llvm.org/llvm/llvm-project/pull/[email protected]>
================ @@ -0,0 +1,71 @@ +//===----------------------------------------------------------------------===// +// +// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. +// See https://llvm.org/LICENSE.txt for license information. +// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception +// +//===----------------------------------------------------------------------===// + +#include "UnsafeToAllowExceptionsCheck.h" + +#include "clang/ASTMatchers/ASTMatchFinder.h" +#include "llvm/ADT/StringSet.h" + +using namespace clang::ast_matchers; + +namespace clang::tidy::bugprone { +namespace { + +AST_MATCHER_P(FunctionDecl, hasAnyName, llvm::StringSet<>, Names) { + return Names.contains(Node.getNameAsString()); +} + +AST_MATCHER(FunctionDecl, isExplicitThrow) { + return isExplicitThrowExceptionSpec(Node.getExceptionSpecType()) && + Node.getExceptionSpecSourceRange().isValid(); +} + +} // namespace + +UnsafeToAllowExceptionsCheck::UnsafeToAllowExceptionsCheck( + StringRef Name, ClangTidyContext *Context) + : ClangTidyCheck(Name, Context), + RawCheckedSwapFunctions( + Options.get("CheckedSwapFunctions", "swap;iter_swap;iter_move")) { + llvm::SmallVector<StringRef, 4> CheckedSwapFunctionsVec; + RawCheckedSwapFunctions.split(CheckedSwapFunctionsVec, ";", -1, false); + CheckedSwapFunctions.insert_range(CheckedSwapFunctionsVec); +} + +void UnsafeToAllowExceptionsCheck::storeOptions( + ClangTidyOptions::OptionMap &Opts) { + Options.store(Opts, "CheckedSwapFunctions", RawCheckedSwapFunctions); +} + +void UnsafeToAllowExceptionsCheck::registerMatchers(MatchFinder *Finder) { + Finder->addMatcher( + functionDecl(allOf(isDefinition(), isExplicitThrow(), + anyOf(cxxDestructorDecl(), + cxxConstructorDecl(isMoveConstructor()), + cxxMethodDecl(isMoveAssignmentOperator()), + allOf(hasAnyName(CheckedSwapFunctions), + unless(parameterCountIs(0))), + isMain()))) + .bind("f"), + this); +} + +void UnsafeToAllowExceptionsCheck::check( + const MatchFinder::MatchResult &Result) { + const auto *MatchedDecl = Result.Nodes.getNodeAs<FunctionDecl>("f"); + + if (!MatchedDecl) + return; ---------------- vbvictor wrote: ```suggestion assert(MatchedDecl); ``` We expect ASTMatchers to work https://github.com/llvm/llvm-project/pull/176430 _______________________________________________ cfe-commits mailing list [email protected] https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits
