=?utf-8?q?Balázs_Kéri?= <balazs.k...@ericsson.com>, =?utf-8?q?Balázs_Kéri?= <balazs.k...@ericsson.com> Message-ID: In-Reply-To: <llvm.org/llvm/llvm-project/pull/140...@github.com>
================ @@ -0,0 +1,136 @@ +//===--- FunctionVisibilityChangeCheck.cpp - clang-tidy -------------------===// +// +// 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 "FunctionVisibilityChangeCheck.h" +#include "../utils/Matchers.h" +#include "../utils/OptionsUtils.h" +#include "clang/ASTMatchers/ASTMatchFinder.h" + +using namespace clang::ast_matchers; + +namespace clang::tidy { + +template <> +struct OptionEnumMapping<bugprone::FunctionVisibilityChangeCheck::ChangeKind> { + static llvm::ArrayRef< + std::pair<bugprone::FunctionVisibilityChangeCheck::ChangeKind, StringRef>> + getEnumMapping() { + static constexpr std::pair< + bugprone::FunctionVisibilityChangeCheck::ChangeKind, StringRef> + Mapping[] = { + {bugprone::FunctionVisibilityChangeCheck::ChangeKind::Any, "any"}, + {bugprone::FunctionVisibilityChangeCheck::ChangeKind::Widening, + "widening"}, + {bugprone::FunctionVisibilityChangeCheck::ChangeKind::Narrowing, + "narrowing"}, + }; + return {Mapping}; + } +}; + +namespace bugprone { + +FunctionVisibilityChangeCheck::FunctionVisibilityChangeCheck( + StringRef Name, ClangTidyContext *Context) + : ClangTidyCheck(Name, Context), + DetectVisibilityChange( + Options.get("DisallowedVisibilityChange", ChangeKind::Any)), + CheckDestructors(Options.get("CheckDestructors", false)), + CheckOperators(Options.get("CheckOperators", false)), + IgnoredFunctions(utils::options::parseStringList( + Options.get("IgnoredFunctions", ""))) {} + +void FunctionVisibilityChangeCheck::storeOptions( + ClangTidyOptions::OptionMap &Opts) { + Options.store(Opts, "DisallowedVisibilityChange", DetectVisibilityChange); + Options.store(Opts, "CheckDestructors", CheckDestructors); + Options.store(Opts, "CheckOperators", CheckOperators); + Options.store(Opts, "IgnoredFunctions", + utils::options::serializeStringList(IgnoredFunctions)); +} + +void FunctionVisibilityChangeCheck::registerMatchers(MatchFinder *Finder) { + auto IgnoredDecl = + namedDecl(matchers::matchesAnyListedName(IgnoredFunctions)); + Finder->addMatcher( + cxxMethodDecl( + isVirtual(), + ofClass( + cxxRecordDecl(unless(isExpansionInSystemHeader())).bind("class")), + forEachOverridden(cxxMethodDecl(ofClass(cxxRecordDecl().bind("base")), + unless(IgnoredDecl)) + .bind("base_func"))) + .bind("func"), + this); +} + +void FunctionVisibilityChangeCheck::check( + const MatchFinder::MatchResult &Result) { + const auto *MatchedFunction = Result.Nodes.getNodeAs<FunctionDecl>("func"); + if (!MatchedFunction->isCanonicalDecl()) + return; + DeclarationName::NameKind NK = MatchedFunction->getDeclName().getNameKind(); + if (!CheckDestructors && NK == DeclarationName::CXXDestructorName) + return; + if (!CheckOperators && NK != DeclarationName::Identifier && + NK != DeclarationName::CXXConstructorName && + NK != DeclarationName::CXXDestructorName) + return; ---------------- vbvictor wrote: This can possibly go into the ast-matchers part. You can look for checks that use options inside mathers with "? unless", "? is" or "? has" strings. https://github.com/llvm/llvm-project/pull/140086 _______________________________________________ cfe-commits mailing list cfe-commits@lists.llvm.org https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits