Skip to content

Commit

Permalink
Initial commit
Browse files Browse the repository at this point in the history
  • Loading branch information
BrettDong committed Nov 18, 2023
0 parents commit 1a86119
Show file tree
Hide file tree
Showing 8 changed files with 75 additions and 0 deletions.
2 changes: 2 additions & 0 deletions .clang-format
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
BasedOnStyle: LLVM
UseTab: Never
1 change: 1 addition & 0 deletions .gitattributes
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
*.h linguist-language=C++
4 changes: 4 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
build/
.idea/
.vscode/
.DS_Store
8 changes: 8 additions & 0 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -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)
23 changes: 23 additions & 0 deletions MyCheck.cpp
Original file line number Diff line number Diff line change
@@ -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<CallExpr>("fooCall");
if (!fooCall) {
return;
}
diag(fooCall->getBeginLoc(), "Calling deprecated API foo() is discouraged");
}

} // namespace clang::tidy::MyPlugin
15 changes: 15 additions & 0 deletions MyCheck.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
#pragma once

#include <clang-tidy/ClangTidyCheck.h>

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
18 changes: 18 additions & 0 deletions MyClangTidyPluginModule.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
#include <clang-tidy/ClangTidyModule.h>
#include <clang-tidy/ClangTidyModuleRegistry.h>

#include "MyCheck.h"

namespace clang::tidy::MyPlugin {

class MyClangTidyPluginModule : public ClangTidyModule {
public:
void addCheckFactories(ClangTidyCheckFactories &CheckFactories) override {
CheckFactories.registerCheck<MyCheck>("my-check");
}
};

[[maybe_unused]] static ClangTidyModuleRegistry::Add<MyClangTidyPluginModule>
X("my-module", "Adds checks in my plugin.");

} // namespace clang::tidy::MyPlugin
4 changes: 4 additions & 0 deletions test/foo.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
void foo();
void bar();

void host() { foo(); }

0 comments on commit 1a86119

Please sign in to comment.