From 04638e7358f1c2738d3e07b4ac51b05f691221dc Mon Sep 17 00:00:00 2001 From: Joe Blaauboer Date: Mon, 14 Nov 2022 13:10:11 -0500 Subject: clang-tidy module: add check for ostringstream --- Utilities/ClangTidyModule/CMakeLists.txt | 2 + Utilities/ClangTidyModule/Module.cxx | 3 ++ .../OstringstreamUseCmstrcatCheck.cxx | 52 ++++++++++++++++++++++ .../OstringstreamUseCmstrcatCheck.h | 21 +++++++++ 4 files changed, 78 insertions(+) create mode 100644 Utilities/ClangTidyModule/OstringstreamUseCmstrcatCheck.cxx create mode 100644 Utilities/ClangTidyModule/OstringstreamUseCmstrcatCheck.h diff --git a/Utilities/ClangTidyModule/CMakeLists.txt b/Utilities/ClangTidyModule/CMakeLists.txt index 8e7b2bc..c51f43a 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 + OstringstreamUseCmstrcatCheck.cxx + OstringstreamUseCmstrcatCheck.h UseBespokeEnumClassCheck.cxx UseBespokeEnumClassCheck.h UseCmstrlenCheck.cxx diff --git a/Utilities/ClangTidyModule/Module.cxx b/Utilities/ClangTidyModule/Module.cxx index ca9a812..7ef8e7d 100644 --- a/Utilities/ClangTidyModule/Module.cxx +++ b/Utilities/ClangTidyModule/Module.cxx @@ -3,6 +3,7 @@ #include #include +#include "OstringstreamUseCmstrcatCheck.h" #include "UseBespokeEnumClassCheck.h" #include "UseCmstrlenCheck.h" #include "UseCmsysFstreamCheck.h" @@ -20,6 +21,8 @@ public: "cmake-use-cmsys-fstream"); CheckFactories.registerCheck( "cmake-use-bespoke-enum-class"); + CheckFactories.registerCheck( + "cmake-ostringstream-use-cmstrcat"); } }; diff --git a/Utilities/ClangTidyModule/OstringstreamUseCmstrcatCheck.cxx b/Utilities/ClangTidyModule/OstringstreamUseCmstrcatCheck.cxx new file mode 100644 index 0000000..920fdf3 --- /dev/null +++ b/Utilities/ClangTidyModule/OstringstreamUseCmstrcatCheck.cxx @@ -0,0 +1,52 @@ +/* Distributed under the OSI-approved BSD 3-Clause License. See accompanying + file Copyright.txt or https://cmake.org/licensing for details. */ +#include "OstringstreamUseCmstrcatCheck.h" + +#include +#include + +namespace clang { +namespace tidy { +namespace cmake { +using namespace ast_matchers; + +OstringstreamUseCmstrcatCheck::OstringstreamUseCmstrcatCheck( + StringRef Name, ClangTidyContext* Context) + : ClangTidyCheck(Name, Context) +{ +} + +void OstringstreamUseCmstrcatCheck::registerMatchers(MatchFinder* Finder) +{ + Finder->addMatcher( + typeLoc(unless(elaboratedTypeLoc()), + optionally(hasParent(elaboratedTypeLoc().bind("parentType"))), + loc(qualType( + hasDeclaration(namedDecl(hasName("::std::ostringstream")))))) + .bind("ostringstream"), + this); +} + +void OstringstreamUseCmstrcatCheck::check( + const MatchFinder::MatchResult& Result) +{ + const TypeLoc* ParentTypeNode = + Result.Nodes.getNodeAs("parentType"); + const TypeLoc* RootNode = Result.Nodes.getNodeAs("ostringstream"); + + if (ParentTypeNode != nullptr) { + if (ParentTypeNode->getBeginLoc().isValid()) { + this->diag(ParentTypeNode->getBeginLoc(), + "use strings and cmStrCat() instead of std::ostringstream"); + } + + } else if (RootNode != nullptr) { + if (RootNode->getBeginLoc().isValid()) { + this->diag(RootNode->getBeginLoc(), + "use strings and cmStrCat() instead of std::ostringstream"); + } + } +} +} +} +} diff --git a/Utilities/ClangTidyModule/OstringstreamUseCmstrcatCheck.h b/Utilities/ClangTidyModule/OstringstreamUseCmstrcatCheck.h new file mode 100644 index 0000000..ecb5616 --- /dev/null +++ b/Utilities/ClangTidyModule/OstringstreamUseCmstrcatCheck.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 +#include + +namespace clang { +namespace tidy { +namespace cmake { +class OstringstreamUseCmstrcatCheck : public ClangTidyCheck +{ +public: + OstringstreamUseCmstrcatCheck(StringRef Name, ClangTidyContext* Context); + void registerMatchers(ast_matchers::MatchFinder* Finder) override; + + void check(const ast_matchers::MatchFinder::MatchResult& Result) override; +}; +} +} +} -- cgit v0.12 From 75ab77ee19e20f3092e88da731288a95ebf86a3a Mon Sep 17 00:00:00 2001 From: Sean Orner Date: Mon, 14 Nov 2022 13:51:23 -0500 Subject: clang-tidy module: add test for ostringstream check --- Utilities/ClangTidyModule/Tests/CMakeLists.txt | 1 + .../Tests/cmake-ostringstream-use-cmstrcat-stdout.txt | 6 ++++++ .../ClangTidyModule/Tests/cmake-ostringstream-use-cmstrcat.cxx | 10 ++++++++++ 3 files changed, 17 insertions(+) create mode 100644 Utilities/ClangTidyModule/Tests/cmake-ostringstream-use-cmstrcat-stdout.txt create mode 100644 Utilities/ClangTidyModule/Tests/cmake-ostringstream-use-cmstrcat.cxx diff --git a/Utilities/ClangTidyModule/Tests/CMakeLists.txt b/Utilities/ClangTidyModule/Tests/CMakeLists.txt index 2fedfa1..5bf0e89 100644 --- a/Utilities/ClangTidyModule/Tests/CMakeLists.txt +++ b/Utilities/ClangTidyModule/Tests/CMakeLists.txt @@ -13,3 +13,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) +add_run_clang_tidy_test(cmake-ostringstream-use-cmstrcat) diff --git a/Utilities/ClangTidyModule/Tests/cmake-ostringstream-use-cmstrcat-stdout.txt b/Utilities/ClangTidyModule/Tests/cmake-ostringstream-use-cmstrcat-stdout.txt new file mode 100644 index 0000000..1b2d6e7 --- /dev/null +++ b/Utilities/ClangTidyModule/Tests/cmake-ostringstream-use-cmstrcat-stdout.txt @@ -0,0 +1,6 @@ +cmake-ostringstream-use-cmstrcat.cxx:5:3: warning: use strings and cmStrCat() instead of std::ostringstream [cmake-ostringstream-use-cmstrcat] + std::ostringstream test; + ^ +cmake-ostringstream-use-cmstrcat.cxx:8:13: warning: use strings and cmStrCat() instead of std::ostringstream [cmake-ostringstream-use-cmstrcat] +void check2(std::ostringstream& test2) + ^ diff --git a/Utilities/ClangTidyModule/Tests/cmake-ostringstream-use-cmstrcat.cxx b/Utilities/ClangTidyModule/Tests/cmake-ostringstream-use-cmstrcat.cxx new file mode 100644 index 0000000..ab749a6 --- /dev/null +++ b/Utilities/ClangTidyModule/Tests/cmake-ostringstream-use-cmstrcat.cxx @@ -0,0 +1,10 @@ +#include + +void check() +{ + std::ostringstream test; +} + +void check2(std::ostringstream& test2) +{ +} -- cgit v0.12 From 0880114ada2e300b3567d78c31cdefcc0c14ce32 Mon Sep 17 00:00:00 2001 From: Kyle Edwards Date: Mon, 14 Nov 2022 13:53:12 -0500 Subject: clang-tidy: disable ostringstream check CMake is not remotely ready for this yet... --- .clang-tidy | 1 + 1 file changed, 1 insertion(+) diff --git a/.clang-tidy b/.clang-tidy index 18aa86e..7ee8bf0 100644 --- a/.clang-tidy +++ b/.clang-tidy @@ -34,6 +34,7 @@ readability-*,\ -readability-suspicious-call-argument,\ -readability-uppercase-literal-suffix,\ cmake-*,\ +-cmake-ostringstream-use-cmstrcat,\ -cmake-use-bespoke-enum-class,\ " HeaderFilterRegex: 'Source/cm[^/]*\.(h|hxx|cxx)$' -- cgit v0.12