diff --git a/CMakeLists.txt b/CMakeLists.txt index 0ba271b..d9196e6 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -1,5 +1,5 @@ cmake_minimum_required(VERSION 3.20) -project(tidyfox VERSION "0.0.1") +project(tidyfox VERSION "0.1.0") set(CMAKE_CXX_STANDARD 17) set(CMAKE_CXX_STANDARD_REQUIRED YES) diff --git a/README.md b/README.md index 708f985..918607e 100644 --- a/README.md +++ b/README.md @@ -8,8 +8,11 @@ If you aren't running nix, use the provided Justfile. Run `just release` to make a release build. You must have llvm and clang 16 dev packages in $PATH. (`FindClang.cmake` must exist) # Usage -Tidyfox lints are supplied under the `tidyfox-` namespace. Currently there are no lints. -The plugin can be loaded via `clang-tidy -load=/path/to/libtidyfox.so` +Tidyfox lints are supplied under the `tidyfox-` namespace. +The plugin can be loaded via `clang-tidy -load=/path/to/libtidyfox.so`. + +## Lints +`tidyfox-explicit-thisptr` - requires that `this->` is used unconditionally for class members. # Contributing There is a `shell.nix` file which will load all the necessary dependencies. diff --git a/src/explicit_thisptr.cpp b/src/explicit_thisptr.cpp new file mode 100644 index 0000000..5a28aa2 --- /dev/null +++ b/src/explicit_thisptr.cpp @@ -0,0 +1,24 @@ +#include "explicit_thisptr.hpp" + +#include +#include + +namespace clang::tidy::tidyfox { +using namespace ast_matchers; + +ExplicitThisptrCheck::ExplicitThisptrCheck(StringRef name, ClangTidyContext* context): + ClangTidyCheck(name, context) {} + +void ExplicitThisptrCheck::registerMatchers(MatchFinder* finder) { + finder->addMatcher(cxxThisExpr().bind("thisptr"), this); +} + +void ExplicitThisptrCheck::check(const MatchFinder::MatchResult& result) { + const auto* matched = result.Nodes.getNodeAs("thisptr"); + if (!matched->isImplicit()) return; + + this->diag(matched->getLocation(), "class member accesses must explicitly use thisptr") + << FixItHint::CreateInsertion(matched->getLocation(), "this->"); +} + +} // namespace clang::tidy::tidyfox diff --git a/src/explicit_thisptr.hpp b/src/explicit_thisptr.hpp new file mode 100644 index 0000000..3c90629 --- /dev/null +++ b/src/explicit_thisptr.hpp @@ -0,0 +1,15 @@ +#pragma once + +#include +#include + +namespace clang::tidy::tidyfox { + +class ExplicitThisptrCheck: public ClangTidyCheck { +public: + ExplicitThisptrCheck(StringRef name, ClangTidyContext* context); + void registerMatchers(ast_matchers::MatchFinder* finder) override; + void check(const ast_matchers::MatchFinder::MatchResult& result) override; +}; + +} // namespace clang::tidy::tidyfox diff --git a/src/lib.cpp b/src/lib.cpp index bba5432..0c884b2 100644 --- a/src/lib.cpp +++ b/src/lib.cpp @@ -3,11 +3,15 @@ #include #include +#include "explicit_thisptr.hpp" + namespace clang::tidy::tidyfox { class TidyfoxClangTidyModule: public ClangTidyModule { public: - void addCheckFactories([[maybe_unused]] ClangTidyCheckFactories& check_factories) override {} + void addCheckFactories(ClangTidyCheckFactories& check_factories) override { + check_factories.registerCheck("tidyfox-explicit-thisptr"); + } }; static const ClangTidyModuleRegistry::Add