diff options
author | Brad King <brad.king@kitware.com> | 2022-11-08 14:21:09 (GMT) |
---|---|---|
committer | Kitware Robot <kwrobot@kitware.com> | 2022-11-08 14:21:18 (GMT) |
commit | 814ded48b0b15ff4edbfca3e98ec6b5a2a2aada8 (patch) | |
tree | d324b3c3071f3cb03a128d04fd4fe770920911bd | |
parent | 7e0c00c25117c85e98666fc07eb07a2e53a59e33 (diff) | |
parent | 5f97772fcbae1280c0183f54a91eff60f514a414 (diff) | |
download | CMake-814ded48b0b15ff4edbfca3e98ec6b5a2a2aada8.zip CMake-814ded48b0b15ff4edbfca3e98ec6b5a2a2aada8.tar.gz CMake-814ded48b0b15ff4edbfca3e98ec6b5a2a2aada8.tar.bz2 |
Merge topic 'clang-tidy-module-use-bespoke-enum-class-check'
5f97772fcb clang-tidy: disable bespoke enum class check
eab8e38961 clang-tidy module: add check for boolean parameters
Acked-by: Kitware Robot <kwrobot@kitware.com>
Merge-request: !7883
-rw-r--r-- | .clang-tidy | 1 | ||||
-rw-r--r-- | Utilities/ClangTidyModule/CMakeLists.txt | 2 | ||||
-rw-r--r-- | Utilities/ClangTidyModule/Module.cxx | 3 | ||||
-rw-r--r-- | Utilities/ClangTidyModule/Tests/CMakeLists.txt | 1 | ||||
-rw-r--r-- | Utilities/ClangTidyModule/Tests/cmake-use-bespoke-enum-class-stdout.txt | 18 | ||||
-rw-r--r-- | Utilities/ClangTidyModule/Tests/cmake-use-bespoke-enum-class.cxx | 63 | ||||
-rw-r--r-- | Utilities/ClangTidyModule/UseBespokeEnumClassCheck.cxx | 35 | ||||
-rw-r--r-- | Utilities/ClangTidyModule/UseBespokeEnumClassCheck.h | 21 |
8 files changed, 144 insertions, 0 deletions
diff --git a/.clang-tidy b/.clang-tidy index 1e9b78a..18aa86e 100644 --- a/.clang-tidy +++ b/.clang-tidy @@ -34,6 +34,7 @@ readability-*,\ -readability-suspicious-call-argument,\ -readability-uppercase-literal-suffix,\ cmake-*,\ +-cmake-use-bespoke-enum-class,\ " HeaderFilterRegex: 'Source/cm[^/]*\.(h|hxx|cxx)$' CheckOptions: diff --git a/Utilities/ClangTidyModule/CMakeLists.txt b/Utilities/ClangTidyModule/CMakeLists.txt index 51603aa..8e7b2bc 100644 --- a/Utilities/ClangTidyModule/CMakeLists.txt +++ b/Utilities/ClangTidyModule/CMakeLists.txt @@ -14,6 +14,8 @@ find_package(Clang REQUIRED) add_library(cmake-clang-tidy-module MODULE Module.cxx + UseBespokeEnumClassCheck.cxx + UseBespokeEnumClassCheck.h UseCmstrlenCheck.cxx UseCmstrlenCheck.h UseCmsysFstreamCheck.cxx diff --git a/Utilities/ClangTidyModule/Module.cxx b/Utilities/ClangTidyModule/Module.cxx index a9d344f..ca9a812 100644 --- a/Utilities/ClangTidyModule/Module.cxx +++ b/Utilities/ClangTidyModule/Module.cxx @@ -3,6 +3,7 @@ #include <clang-tidy/ClangTidyModule.h> #include <clang-tidy/ClangTidyModuleRegistry.h> +#include "UseBespokeEnumClassCheck.h" #include "UseCmstrlenCheck.h" #include "UseCmsysFstreamCheck.h" @@ -17,6 +18,8 @@ public: CheckFactories.registerCheck<UseCmstrlenCheck>("cmake-use-cmstrlen"); CheckFactories.registerCheck<UseCmsysFstreamCheck>( "cmake-use-cmsys-fstream"); + CheckFactories.registerCheck<UseBespokeEnumClassCheck>( + "cmake-use-bespoke-enum-class"); } }; diff --git a/Utilities/ClangTidyModule/Tests/CMakeLists.txt b/Utilities/ClangTidyModule/Tests/CMakeLists.txt index a66eaa8..2fedfa1 100644 --- a/Utilities/ClangTidyModule/Tests/CMakeLists.txt +++ b/Utilities/ClangTidyModule/Tests/CMakeLists.txt @@ -12,3 +12,4 @@ endfunction() add_run_clang_tidy_test(cmake-use-cmstrlen) add_run_clang_tidy_test(cmake-use-cmsys-fstream) +add_run_clang_tidy_test(cmake-use-bespoke-enum-class) diff --git a/Utilities/ClangTidyModule/Tests/cmake-use-bespoke-enum-class-stdout.txt b/Utilities/ClangTidyModule/Tests/cmake-use-bespoke-enum-class-stdout.txt new file mode 100644 index 0000000..5e0acdd --- /dev/null +++ b/Utilities/ClangTidyModule/Tests/cmake-use-bespoke-enum-class-stdout.txt @@ -0,0 +1,18 @@ +cmake-use-bespoke-enum-class.cxx:3:16: warning: use a bespoke enum class instead of booleans as parameters [cmake-use-bespoke-enum-class] +bool function1(bool i) + ^ +cmake-use-bespoke-enum-class.cxx:8:15: warning: use a bespoke enum class instead of booleans as parameters [cmake-use-bespoke-enum-class] +int function2(bool i) + ^ +cmake-use-bespoke-enum-class.cxx:13:16: warning: use a bespoke enum class instead of booleans as parameters [cmake-use-bespoke-enum-class] +char function3(bool i) + ^ +cmake-use-bespoke-enum-class.cxx:18:16: warning: use a bespoke enum class instead of booleans as parameters [cmake-use-bespoke-enum-class] +void function4(bool i) + ^ +cmake-use-bespoke-enum-class.cxx:22:17: warning: use a bespoke enum class instead of booleans as parameters [cmake-use-bespoke-enum-class] +float function5(bool i) + ^ +cmake-use-bespoke-enum-class.cxx:27:18: warning: use a bespoke enum class instead of booleans as parameters [cmake-use-bespoke-enum-class] +double function6(bool i) + ^ diff --git a/Utilities/ClangTidyModule/Tests/cmake-use-bespoke-enum-class.cxx b/Utilities/ClangTidyModule/Tests/cmake-use-bespoke-enum-class.cxx new file mode 100644 index 0000000..2913e6a --- /dev/null +++ b/Utilities/ClangTidyModule/Tests/cmake-use-bespoke-enum-class.cxx @@ -0,0 +1,63 @@ +// Correction needed + +bool function1(bool i) +{ + return true; +} + +int function2(bool i) +{ + return 0; +} + +char function3(bool i) +{ + return 'a'; +} + +void function4(bool i) +{ +} + +float function5(bool i) +{ + return 1.0; +} + +double function6(bool i) +{ + return 0; +} + +// No correction needed +bool global; + +bool function7(int i) +{ + bool l; + return true; +} + +int function8(int i) +{ + return i; +} + +char function9(char i) +{ + return i; +} + +void function10() +{ +} + +float function11(float i) +{ + return i; +} + +double function12(double i) +{ + return i; +} diff --git a/Utilities/ClangTidyModule/UseBespokeEnumClassCheck.cxx b/Utilities/ClangTidyModule/UseBespokeEnumClassCheck.cxx new file mode 100644 index 0000000..26f3749 --- /dev/null +++ b/Utilities/ClangTidyModule/UseBespokeEnumClassCheck.cxx @@ -0,0 +1,35 @@ +/* Distributed under the OSI-approved BSD 3-Clause License. See accompanying + file Copyright.txt or https://cmake.org/licensing for details. */ +#include "UseBespokeEnumClassCheck.h" + +#include <clang/AST/Type.h> +#include <clang/ASTMatchers/ASTMatchFinder.h> + +namespace clang { +namespace tidy { +namespace cmake { +using namespace ast_matchers; + +UseBespokeEnumClassCheck::UseBespokeEnumClassCheck(StringRef Name, + ClangTidyContext* Context) + : ClangTidyCheck(Name, Context) +{ +} + +void UseBespokeEnumClassCheck::registerMatchers(MatchFinder* Finder) +{ + Finder->addMatcher( + parmVarDecl( + hasTypeLoc(typeLoc(loc(qualType(asString("_Bool")))).bind("type"))), + this); +} + +void UseBespokeEnumClassCheck::check(const MatchFinder::MatchResult& Result) +{ + const TypeLoc* Node = Result.Nodes.getNodeAs<TypeLoc>("type"); + this->diag(Node->getBeginLoc(), + "use a bespoke enum class instead of booleans as parameters"); +} +} +} +} diff --git a/Utilities/ClangTidyModule/UseBespokeEnumClassCheck.h b/Utilities/ClangTidyModule/UseBespokeEnumClassCheck.h new file mode 100644 index 0000000..be76db0 --- /dev/null +++ b/Utilities/ClangTidyModule/UseBespokeEnumClassCheck.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 UseBespokeEnumClassCheck : public ClangTidyCheck +{ +public: + UseBespokeEnumClassCheck(StringRef Name, ClangTidyContext* Context); + void registerMatchers(ast_matchers::MatchFinder* Finder) override; + + void check(const ast_matchers::MatchFinder::MatchResult& Result) override; +}; +} +} +} |