From 60c8712e45cfcd8d58c0009d3e9806d2b9ee5d5b Mon Sep 17 00:00:00 2001 From: Aperzer Date: Wed, 28 Feb 2024 09:51:44 +0800 Subject: [PATCH] [clang-tidy] Support explicity cast checker. This commit mainly: 1. Supported the checker that will emit warning when there is a explicity cast of types, which defined in config file. This demand comes from GaussDB. --- .../clang-tidy/bsc/BSCTidyModule.cpp | 3 ++ .../clang-tidy/bsc/CMakeLists.txt | 1 + .../clang-tidy/bsc/ExplicitCastCheck.cpp | 48 +++++++++++++++++++ .../clang-tidy/bsc/ExplicitCastCheck.h | 43 +++++++++++++++++ clang-tools-extra/docs/ReleaseNotes.rst | 5 ++ .../clang-tidy/checks/bsc/explicit-cast.rst | 6 +++ .../docs/clang-tidy/checks/list.rst | 2 + .../clang-tidy/checkers/bsc/explicit-cast.c | 14 ++++++ 8 files changed, 122 insertions(+) create mode 100644 clang-tools-extra/clang-tidy/bsc/ExplicitCastCheck.cpp create mode 100644 clang-tools-extra/clang-tidy/bsc/ExplicitCastCheck.h create mode 100644 clang-tools-extra/docs/clang-tidy/checks/bsc/explicit-cast.rst create mode 100644 clang-tools-extra/test/clang-tidy/checkers/bsc/explicit-cast.c diff --git a/clang-tools-extra/clang-tidy/bsc/BSCTidyModule.cpp b/clang-tools-extra/clang-tidy/bsc/BSCTidyModule.cpp index 5036b12375ce..9f9c8bb7651e 100644 --- a/clang-tools-extra/clang-tidy/bsc/BSCTidyModule.cpp +++ b/clang-tools-extra/clang-tidy/bsc/BSCTidyModule.cpp @@ -10,6 +10,7 @@ #include "../ClangTidyModule.h" #include "../ClangTidyModuleRegistry.h" #include "AddNewFieldCheck.h" +#include "ExplicitCastCheck.h" namespace clang { namespace tidy { @@ -20,6 +21,8 @@ public: void addCheckFactories(ClangTidyCheckFactories &CheckFactories) override { CheckFactories.registerCheck( "bsc-add-new-field"); + CheckFactories.registerCheck( + "bsc-explicit-cast"); } }; diff --git a/clang-tools-extra/clang-tidy/bsc/CMakeLists.txt b/clang-tools-extra/clang-tidy/bsc/CMakeLists.txt index e946984341ef..915fdbefedb4 100644 --- a/clang-tools-extra/clang-tidy/bsc/CMakeLists.txt +++ b/clang-tools-extra/clang-tidy/bsc/CMakeLists.txt @@ -7,6 +7,7 @@ add_clang_library(clangTidyBscModule AddNewFieldCheck.cpp BSCTidyModule.cpp + ExplicitCastCheck.cpp LINK_LIBS clangTidy diff --git a/clang-tools-extra/clang-tidy/bsc/ExplicitCastCheck.cpp b/clang-tools-extra/clang-tidy/bsc/ExplicitCastCheck.cpp new file mode 100644 index 000000000000..61da6cfd4f9e --- /dev/null +++ b/clang-tools-extra/clang-tidy/bsc/ExplicitCastCheck.cpp @@ -0,0 +1,48 @@ +//===--- ExplicitCastCheck.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 "ExplicitCastCheck.h" +#include "clang/AST/ASTContext.h" +#include "clang/ASTMatchers/ASTMatchFinder.h" + +using namespace clang::ast_matchers; + +namespace clang { +namespace tidy { +namespace bsc { + +void ExplicitCastCheck::registerMatchers(MatchFinder *Finder) { + for (auto TargetStruct : TargetStructs) { + // Targets cast to others. + Finder->addMatcher(cStyleCastExpr(hasSourceExpression(hasType(asString(std::string(TargetStruct))))).bind("x"), this); + // Others cast to targets. + Finder->addMatcher(cStyleCastExpr(hasType(asString(std::string(TargetStruct)))).bind("y"), this); + } +} + +void ExplicitCastCheck::check(const MatchFinder::MatchResult &Result) { + const auto *CastFromTargetType = Result.Nodes.getNodeAs("x"); + const auto *CastToTargetType = Result.Nodes.getNodeAs("y"); + + if (CastFromTargetType) { + const Expr* SubExpr = CastFromTargetType->getSubExpr(); + std::string CastTypeName = SubExpr->getType().getAsString(); + SourceLocation SL = CastFromTargetType->getSourceRange().getEnd(); + diag(SL, "Find explicit cast from target type: " + CastTypeName, DiagnosticIDs::Warning); + } + + if (CastToTargetType) { + std::string CastTypeName = CastToTargetType->getType().getAsString(); + SourceLocation SL = CastToTargetType->getSourceRange().getEnd(); + diag(SL, "Find explicit cast to target type: " + CastTypeName, DiagnosticIDs::Warning); + } +} + +} // namespace bsc +} // namespace tidy +} // namespace clang diff --git a/clang-tools-extra/clang-tidy/bsc/ExplicitCastCheck.h b/clang-tools-extra/clang-tidy/bsc/ExplicitCastCheck.h new file mode 100644 index 000000000000..1fe603f15e3e --- /dev/null +++ b/clang-tools-extra/clang-tidy/bsc/ExplicitCastCheck.h @@ -0,0 +1,43 @@ +//===--- ExplicitCastCheck.h - clang-tidy -----------------*- C++ -*-===// +// +// 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 +// +//===----------------------------------------------------------------------===// + +#ifndef LLVM_CLANG_TOOLS_EXTRA_CLANG_TIDY_BSC_EXPLICITCASTCHECK_H +#define LLVM_CLANG_TOOLS_EXTRA_CLANG_TIDY_BSC_EXPLICITCASTCHECK_H + +#include "../ClangTidyCheck.h" + +namespace clang { +namespace tidy { +namespace bsc { + +/// FIXME: Write a short description. +/// +/// For the user-facing documentation see: +/// http://clang.llvm.org/extra/clang-tidy/checks/bsc/explicit-cast.html +class ExplicitCastCheck : public ClangTidyCheck { +public: + ExplicitCastCheck(StringRef Name, ClangTidyContext *Context) + : ClangTidyCheck(Name, Context), + TargetStructList(Options.get("TargetStructs", "TEMP_FAILURE_RETRY")) { + StringRef(TargetStructList).split(TargetStructs, ",", -1, false); + } + void storeOptions(ClangTidyOptions::OptionMap &Opts) { + Options.store(Opts, "TargetStructs", TargetStructList); + } + void registerMatchers(ast_matchers::MatchFinder *Finder) override; + void check(const ast_matchers::MatchFinder::MatchResult &Result) override; +private: + const StringRef TargetStructList; + SmallVector TargetStructs; +}; + +} // namespace bsc +} // namespace tidy +} // namespace clang + +#endif // LLVM_CLANG_TOOLS_EXTRA_CLANG_TIDY_BSC_EXPLICITCASTCHECK_H diff --git a/clang-tools-extra/docs/ReleaseNotes.rst b/clang-tools-extra/docs/ReleaseNotes.rst index 847fc538b98c..f9cb5ab64f0f 100644 --- a/clang-tools-extra/docs/ReleaseNotes.rst +++ b/clang-tools-extra/docs/ReleaseNotes.rst @@ -149,6 +149,11 @@ New checks FIXME: add release notes. +- New :doc:`bsc-explicit-cast + ` check. + + FIXME: add release notes. + - New :doc:`bugprone-shared-ptr-array-mismatch ` check. Finds initializations of C++ shared pointers to non-array type that are initialized with an array. diff --git a/clang-tools-extra/docs/clang-tidy/checks/bsc/explicit-cast.rst b/clang-tools-extra/docs/clang-tidy/checks/bsc/explicit-cast.rst new file mode 100644 index 000000000000..14a75321336c --- /dev/null +++ b/clang-tools-extra/docs/clang-tidy/checks/bsc/explicit-cast.rst @@ -0,0 +1,6 @@ +.. title:: clang-tidy - bsc-explicit-cast + +bsc-explicit-cast +======================== + +FIXME: Describe what patterns does the check detect and why. Give examples. diff --git a/clang-tools-extra/docs/clang-tidy/checks/list.rst b/clang-tools-extra/docs/clang-tidy/checks/list.rst index 123ed8dea6a4..3c73d54f0609 100644 --- a/clang-tools-extra/docs/clang-tidy/checks/list.rst +++ b/clang-tools-extra/docs/clang-tidy/checks/list.rst @@ -77,6 +77,8 @@ Clang-Tidy Checks `android-cloexec-socket `_, "Yes" `android-comparison-in-temp-failure-retry `_, `boost-use-to-string `_, "Yes" + `bsc-add-new-field `_, "Yes" + `bsc-explicit-cast `_, "Yes" `bugprone-argument-comment `_, "Yes" `bugprone-assert-side-effect `_, `bugprone-assignment-in-if-condition `_, diff --git a/clang-tools-extra/test/clang-tidy/checkers/bsc/explicit-cast.c b/clang-tools-extra/test/clang-tidy/checkers/bsc/explicit-cast.c new file mode 100644 index 000000000000..8789e72dbbf5 --- /dev/null +++ b/clang-tools-extra/test/clang-tidy/checkers/bsc/explicit-cast.c @@ -0,0 +1,14 @@ +// RUN: %check_clang_tidy %s bsc-explicit-cast %t + +// FIXME: Add something that triggers the check here. +void f(); +// CHECK-MESSAGES: :[[@LINE-1]]:6: warning: function 'f' is insufficiently awesome [bsc-explicit-cast] + +// FIXME: Verify the applied fix. +// * Make the CHECK patterns specific enough and try to make verified lines +// unique to avoid incorrect matches. +// * Use {{}} for regular expressions. +// CHECK-FIXES: {{^}}void awesome_f();{{$}} + +// FIXME: Add something that doesn't trigger the check here. +void awesome_f2(); -- Gitee