Merge topic 'clang-tidy-module-cmstrlen-check'

b4e8ddbc2f clang-tidy: enable cmStrLen() check and fix violations
43481a77f9 clang-tidy module: add test for cmStrLen() check
43a88b56af clang-tidy module: add check for cmStrLen()

Acked-by: Kitware Robot <kwrobot@kitware.com>
Merge-request: !7809
This commit is contained in:
Brad King
2022-10-20 14:04:10 +00:00
committed by Kitware Robot
11 changed files with 161 additions and 2 deletions
+1
View File
@@ -33,6 +33,7 @@ readability-*,\
-readability-redundant-member-init,\
-readability-suspicious-call-argument,\
-readability-uppercase-literal-suffix,\
cmake-*,\
"
HeaderFilterRegex: 'Source/cm[^/]*\.(h|hxx|cxx)$'
CheckOptions:
+1 -1
View File
@@ -5532,7 +5532,7 @@ cmGeneratorTarget::GetTargetSourceFileFlags(const cmSourceFile* sf) const
} else if (cmHasLiteralPrefix(*location, "Resources/")) {
flags.Type = cmGeneratorTarget::SourceFileTypeDeepResource;
if (stripResources) {
flags.MacFolder += strlen("Resources/");
flags.MacFolder += cmStrLen("Resources/");
}
} else {
flags.Type = cmGeneratorTarget::SourceFileTypeMacContent;
+3
View File
@@ -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 -1
View File
@@ -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");
}
};
@@ -9,3 +9,5 @@ function(add_run_clang_tidy_test check_name)
-P "${CMAKE_CURRENT_SOURCE_DIR}/RunClangTidy.cmake"
)
endfunction()
add_run_clang_tidy_test(cmake-use-cmstrlen)
@@ -0,0 +1,37 @@
#include <cstring>
template <size_t N>
constexpr size_t cmStrLen(const char (&/*str*/)[N])
{
return N - 1;
}
namespace ns1 {
using std::strlen;
}
namespace ns2 {
std::size_t strlen(const char* str)
{
return std::strlen(str);
}
}
int main()
{
// String variable used for calling strlen() on a variable
auto s0 = "howdy";
// Correction needed
(void)cmStrLen("Hello");
(void)cmStrLen("Goodbye");
(void)cmStrLen("Hola");
(void)cmStrLen("Bonjour");
// No correction needed
(void)ns2::strlen("Salve");
(void)cmStrLen("Konnichiwa");
(void)strlen(s0);
return 0;
}
@@ -0,0 +1,2 @@
4 warnings generated.
clang-tidy applied 4 of 4 suggested fixes.
@@ -0,0 +1,20 @@
cmake-use-cmstrlen.cxx:26:9: warning: use cmStrLen() for string literals [cmake-use-cmstrlen]
(void)strlen("Hello");
^~~~~~
cmStrLen
cmake-use-cmstrlen.cxx:26:9: note: FIX-IT applied suggested code changes
cmake-use-cmstrlen.cxx:27:9: warning: use cmStrLen() for string literals [cmake-use-cmstrlen]
(void)::strlen("Goodbye");
^~~~~~~~
cmStrLen
cmake-use-cmstrlen.cxx:27:9: note: FIX-IT applied suggested code changes
cmake-use-cmstrlen.cxx:28:9: warning: use cmStrLen() for string literals [cmake-use-cmstrlen]
(void)std::strlen("Hola");
^~~~~~~~~~~
cmStrLen
cmake-use-cmstrlen.cxx:28:9: note: FIX-IT applied suggested code changes
cmake-use-cmstrlen.cxx:29:9: warning: use cmStrLen() for string literals [cmake-use-cmstrlen]
(void)ns1::strlen("Bonjour");
^~~~~~~~~~~
cmStrLen
cmake-use-cmstrlen.cxx:29:9: note: FIX-IT applied suggested code changes
@@ -0,0 +1,37 @@
#include <cstring>
template <size_t N>
constexpr size_t cmStrLen(const char (&/*str*/)[N])
{
return N - 1;
}
namespace ns1 {
using std::strlen;
}
namespace ns2 {
std::size_t strlen(const char* str)
{
return std::strlen(str);
}
}
int main()
{
// String variable used for calling strlen() on a variable
auto s0 = "howdy";
// Correction needed
(void)strlen("Hello");
(void)::strlen("Goodbye");
(void)std::strlen("Hola");
(void)ns1::strlen("Bonjour");
// No correction needed
(void)ns2::strlen("Salve");
(void)cmStrLen("Konnichiwa");
(void)strlen(s0);
return 0;
}
@@ -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");
}
}
}
}
@@ -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;
};
}
}
}