feat: add tidyfox-explicit-thisptr

Enforces explicit usage of the thisptr unconditionally
This commit is contained in:
outfoxxed 2023-10-27 03:10:01 -07:00
parent f5c43c4fb4
commit cb4ae697f7
Signed by: outfoxxed
GPG key ID: 4C88A185FB89301E
5 changed files with 50 additions and 4 deletions

View file

@ -1,5 +1,5 @@
cmake_minimum_required(VERSION 3.20) 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 17)
set(CMAKE_CXX_STANDARD_REQUIRED YES) set(CMAKE_CXX_STANDARD_REQUIRED YES)

View file

@ -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) Run `just release` to make a release build. You must have llvm and clang 16 dev packages in $PATH. (`FindClang.cmake` must exist)
# Usage # Usage
Tidyfox lints are supplied under the `tidyfox-` namespace. Currently there are no lints. Tidyfox lints are supplied under the `tidyfox-` namespace.
The plugin can be loaded via `clang-tidy -load=/path/to/libtidyfox.so` 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 # Contributing
There is a `shell.nix` file which will load all the necessary dependencies. There is a `shell.nix` file which will load all the necessary dependencies.

24
src/explicit_thisptr.cpp Normal file
View file

@ -0,0 +1,24 @@
#include "explicit_thisptr.hpp"
#include <clang/ASTMatchers/ASTMatchFinder.h>
#include <clang/ASTMatchers/ASTMatchers.h>
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<CXXThisExpr>("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

15
src/explicit_thisptr.hpp Normal file
View file

@ -0,0 +1,15 @@
#pragma once
#include <clang-tidy/ClangTidyCheck.h>
#include <clang/ASTMatchers/ASTMatchFinder.h>
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

View file

@ -3,11 +3,15 @@
#include <clang-tidy/ClangTidyModule.h> #include <clang-tidy/ClangTidyModule.h>
#include <clang-tidy/ClangTidyModuleRegistry.h> #include <clang-tidy/ClangTidyModuleRegistry.h>
#include "explicit_thisptr.hpp"
namespace clang::tidy::tidyfox { namespace clang::tidy::tidyfox {
class TidyfoxClangTidyModule: public ClangTidyModule { class TidyfoxClangTidyModule: public ClangTidyModule {
public: public:
void addCheckFactories([[maybe_unused]] ClangTidyCheckFactories& check_factories) override {} void addCheckFactories(ClangTidyCheckFactories& check_factories) override {
check_factories.registerCheck<ExplicitThisptrCheck>("tidyfox-explicit-thisptr");
}
}; };
static const ClangTidyModuleRegistry::Add<TidyfoxClangTidyModule> static const ClangTidyModuleRegistry::Add<TidyfoxClangTidyModule>