From 1a861193cc3db78a6c6689c235605abfc4be574d Mon Sep 17 00:00:00 2001 From: Binrui Dong Date: Sat, 18 Nov 2023 11:04:25 +0800 Subject: [PATCH] Initial commit --- .clang-format | 2 ++ .gitattributes | 1 + .gitignore | 4 ++++ CMakeLists.txt | 8 ++++++++ MyCheck.cpp | 23 +++++++++++++++++++++++ MyCheck.h | 15 +++++++++++++++ MyClangTidyPluginModule.cpp | 18 ++++++++++++++++++ test/foo.cpp | 4 ++++ 8 files changed, 75 insertions(+) create mode 100644 .clang-format create mode 100644 .gitattributes create mode 100644 .gitignore create mode 100644 CMakeLists.txt create mode 100644 MyCheck.cpp create mode 100644 MyCheck.h create mode 100644 MyClangTidyPluginModule.cpp create mode 100644 test/foo.cpp diff --git a/.clang-format b/.clang-format new file mode 100644 index 0000000..84d6640 --- /dev/null +++ b/.clang-format @@ -0,0 +1,2 @@ +BasedOnStyle: LLVM +UseTab: Never \ No newline at end of file diff --git a/.gitattributes b/.gitattributes new file mode 100644 index 0000000..62b6800 --- /dev/null +++ b/.gitattributes @@ -0,0 +1 @@ +*.h linguist-language=C++ \ No newline at end of file diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..6d915a5 --- /dev/null +++ b/.gitignore @@ -0,0 +1,4 @@ +build/ +.idea/ +.vscode/ +.DS_Store \ No newline at end of file diff --git a/CMakeLists.txt b/CMakeLists.txt new file mode 100644 index 0000000..ba6256b --- /dev/null +++ b/CMakeLists.txt @@ -0,0 +1,8 @@ +cmake_minimum_required(VERSION 3.20) +project(ClangTidyPlugin C CXX) +find_package(LLVM REQUIRED) +find_package(Clang REQUIRED) +add_library(ClangTidyPlugin MODULE MyClangTidyPluginModule.cpp MyCheck.cpp) +target_compile_features(ClangTidyPlugin PRIVATE cxx_std_17) +target_include_directories(ClangTidyPlugin PRIVATE ${LLVM_INCLUDE_DIRS} ${CLANG_INCLUDE_DIRS}) +target_link_libraries(ClangTidyPlugin PRIVATE clangTidy) diff --git a/MyCheck.cpp b/MyCheck.cpp new file mode 100644 index 0000000..d543d64 --- /dev/null +++ b/MyCheck.cpp @@ -0,0 +1,23 @@ +#include "MyCheck.h" + +namespace clang::tidy::MyPlugin { + +using namespace ast_matchers; + +MyCheck::MyCheck(StringRef Name, ClangTidyContext *Context) + : ClangTidyCheck(Name, Context) {} + +void MyCheck::registerMatchers(MatchFinder *Finder) { + Finder->addMatcher( + callExpr(callee(functionDecl(hasName("foo")))).bind("fooCall"), this); +} + +void MyCheck::check(const MatchFinder::MatchResult &Result) { + const auto *fooCall = Result.Nodes.getNodeAs("fooCall"); + if (!fooCall) { + return; + } + diag(fooCall->getBeginLoc(), "Calling deprecated API foo() is discouraged"); +} + +} // namespace clang::tidy::MyPlugin diff --git a/MyCheck.h b/MyCheck.h new file mode 100644 index 0000000..305d314 --- /dev/null +++ b/MyCheck.h @@ -0,0 +1,15 @@ +#pragma once + +#include + +namespace clang::tidy::MyPlugin { + +class MyCheck : public ClangTidyCheck { +public: + explicit MyCheck(StringRef Name, ClangTidyContext *Context); + + void registerMatchers(ast_matchers::MatchFinder *Finder) override; + void check(const ast_matchers::MatchFinder::MatchResult &Result) override; +}; + +} // namespace clang::tidy::MyPlugin diff --git a/MyClangTidyPluginModule.cpp b/MyClangTidyPluginModule.cpp new file mode 100644 index 0000000..6a475f8 --- /dev/null +++ b/MyClangTidyPluginModule.cpp @@ -0,0 +1,18 @@ +#include +#include + +#include "MyCheck.h" + +namespace clang::tidy::MyPlugin { + +class MyClangTidyPluginModule : public ClangTidyModule { +public: + void addCheckFactories(ClangTidyCheckFactories &CheckFactories) override { + CheckFactories.registerCheck("my-check"); + } +}; + +[[maybe_unused]] static ClangTidyModuleRegistry::Add + X("my-module", "Adds checks in my plugin."); + +} // namespace clang::tidy::MyPlugin diff --git a/test/foo.cpp b/test/foo.cpp new file mode 100644 index 0000000..02dae79 --- /dev/null +++ b/test/foo.cpp @@ -0,0 +1,4 @@ +void foo(); +void bar(); + +void host() { foo(); }