[clang-tools-extra] [clang-tidy] Added check 'bugprone-function-visibility-change' (PR #140086)
Baranov Victor via cfe-commits
cfe-commits at lists.llvm.org
Fri May 23 19:33:59 PDT 2025
=?utf-8?q?Balázs_Kéri?= <balazs.keri at ericsson.com>,
=?utf-8?q?Balázs_Kéri?= <balazs.keri at ericsson.com>
Message-ID:
In-Reply-To: <llvm.org/llvm/llvm-project/pull/140086 at 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
More information about the cfe-commits
mailing list