mirror of
https://github.com/Kitware/CMake.git
synced 2026-01-11 16:32:14 -06:00
clang-tidy module: add check for cmStrLen()
Co-Authored-by: Joe Blaauboer <jblaauboer67@gmail.com>
This commit is contained in:
@@ -13,6 +13,9 @@ find_package(Clang REQUIRED)
|
||||
|
||||
add_library(cmake-clang-tidy-module MODULE
|
||||
Module.cxx
|
||||
|
||||
UseCmstrlenCheck.cxx
|
||||
UseCmstrlenCheck.h
|
||||
)
|
||||
target_include_directories(cmake-clang-tidy-module PRIVATE ${CLANG_INCLUDE_DIRS})
|
||||
target_link_libraries(cmake-clang-tidy-module PRIVATE clang-tidy)
|
||||
|
||||
@@ -3,6 +3,8 @@
|
||||
#include <clang-tidy/ClangTidyModule.h>
|
||||
#include <clang-tidy/ClangTidyModuleRegistry.h>
|
||||
|
||||
#include "UseCmstrlenCheck.h"
|
||||
|
||||
namespace clang {
|
||||
namespace tidy {
|
||||
namespace cmake {
|
||||
@@ -11,7 +13,7 @@ class CMakeClangTidyModule : public ClangTidyModule
|
||||
public:
|
||||
void addCheckFactories(ClangTidyCheckFactories& CheckFactories) override
|
||||
{
|
||||
// TODO
|
||||
CheckFactories.registerCheck<UseCmstrlenCheck>("cmake-use-cmstrlen");
|
||||
}
|
||||
};
|
||||
|
||||
|
||||
34
Utilities/ClangTidyModule/UseCmstrlenCheck.cxx
Normal file
34
Utilities/ClangTidyModule/UseCmstrlenCheck.cxx
Normal file
@@ -0,0 +1,34 @@
|
||||
/* Distributed under the OSI-approved BSD 3-Clause License. See accompanying
|
||||
file Copyright.txt or https://cmake.org/licensing for details. */
|
||||
#include "UseCmstrlenCheck.h"
|
||||
|
||||
#include <clang/ASTMatchers/ASTMatchFinder.h>
|
||||
|
||||
namespace clang {
|
||||
namespace tidy {
|
||||
namespace cmake {
|
||||
using namespace ast_matchers;
|
||||
|
||||
UseCmstrlenCheck::UseCmstrlenCheck(StringRef Name, ClangTidyContext* Context)
|
||||
: ClangTidyCheck(Name, Context)
|
||||
{
|
||||
}
|
||||
|
||||
void UseCmstrlenCheck::registerMatchers(MatchFinder* Finder)
|
||||
{
|
||||
Finder->addMatcher(callExpr(callee(functionDecl(hasName("::strlen"))),
|
||||
callee(expr().bind("callee")),
|
||||
hasArgument(0, stringLiteral())),
|
||||
this);
|
||||
}
|
||||
|
||||
void UseCmstrlenCheck::check(const MatchFinder::MatchResult& Result)
|
||||
{
|
||||
const Expr* Node = Result.Nodes.getNodeAs<Expr>("callee");
|
||||
|
||||
this->diag(Node->getBeginLoc(), "use cmStrLen() for string literals")
|
||||
<< FixItHint::CreateReplacement(Node->getSourceRange(), "cmStrLen");
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
21
Utilities/ClangTidyModule/UseCmstrlenCheck.h
Normal file
21
Utilities/ClangTidyModule/UseCmstrlenCheck.h
Normal file
@@ -0,0 +1,21 @@
|
||||
/* Distributed under the OSI-approved BSD 3-Clause License. See accompanying
|
||||
file Copyright.txt or https://cmake.org/licensing for details. */
|
||||
#pragma once
|
||||
|
||||
#include <clang-tidy/ClangTidyCheck.h>
|
||||
#include <clang/ASTMatchers/ASTMatchFinder.h>
|
||||
|
||||
namespace clang {
|
||||
namespace tidy {
|
||||
namespace cmake {
|
||||
class UseCmstrlenCheck : public ClangTidyCheck
|
||||
{
|
||||
public:
|
||||
UseCmstrlenCheck(StringRef Name, ClangTidyContext* Context);
|
||||
void registerMatchers(ast_matchers::MatchFinder* Finder) override;
|
||||
|
||||
void check(const ast_matchers::MatchFinder::MatchResult& Result) override;
|
||||
};
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user