summaryrefslogtreecommitdiff
path: root/Utilities/ClangTidyModule
diff options
context:
space:
mode:
authorKyle Edwards <kyle.edwards@kitware.com>2022-09-14 14:24:16 -0400
committerKyle Edwards <kyle.edwards@kitware.com>2022-10-19 13:51:11 -0400
commit43a88b56afd9224d2810d9b6d22004e137dc02e6 (patch)
tree67ca4c08051a5b12fada0ecb66cef84b7c710ddf /Utilities/ClangTidyModule
parent76ab7db5a1b826a6df05daf32728816aecd25b7d (diff)
downloadcmake-43a88b56afd9224d2810d9b6d22004e137dc02e6.tar.gz
clang-tidy module: add check for cmStrLen()
Co-Authored-by: Joe Blaauboer <jblaauboer67@gmail.com>
Diffstat (limited to 'Utilities/ClangTidyModule')
-rw-r--r--Utilities/ClangTidyModule/CMakeLists.txt3
-rw-r--r--Utilities/ClangTidyModule/Module.cxx4
-rw-r--r--Utilities/ClangTidyModule/UseCmstrlenCheck.cxx34
-rw-r--r--Utilities/ClangTidyModule/UseCmstrlenCheck.h21
4 files changed, 61 insertions, 1 deletions
diff --git a/Utilities/ClangTidyModule/CMakeLists.txt b/Utilities/ClangTidyModule/CMakeLists.txt
index 8443d9e918..6be13d608d 100644
--- a/Utilities/ClangTidyModule/CMakeLists.txt
+++ b/Utilities/ClangTidyModule/CMakeLists.txt
@@ -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)
diff --git a/Utilities/ClangTidyModule/Module.cxx b/Utilities/ClangTidyModule/Module.cxx
index 4bb6dc0f94..a35c3367a3 100644
--- a/Utilities/ClangTidyModule/Module.cxx
+++ b/Utilities/ClangTidyModule/Module.cxx
@@ -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");
}
};
diff --git a/Utilities/ClangTidyModule/UseCmstrlenCheck.cxx b/Utilities/ClangTidyModule/UseCmstrlenCheck.cxx
new file mode 100644
index 0000000000..590d260b4e
--- /dev/null
+++ b/Utilities/ClangTidyModule/UseCmstrlenCheck.cxx
@@ -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");
+}
+}
+}
+}
diff --git a/Utilities/ClangTidyModule/UseCmstrlenCheck.h b/Utilities/ClangTidyModule/UseCmstrlenCheck.h
new file mode 100644
index 0000000000..08f77c23a9
--- /dev/null
+++ b/Utilities/ClangTidyModule/UseCmstrlenCheck.h
@@ -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;
+};
+}
+}
+}