summaryrefslogtreecommitdiff
path: root/Utilities/ClangTidyModule
diff options
context:
space:
mode:
authorJoe Blaauboer <jblaauboer67@gmail.com>2022-11-14 13:10:11 -0500
committerKyle Edwards <kyle.edwards@kitware.com>2022-11-14 13:51:59 -0500
commit04638e7358f1c2738d3e07b4ac51b05f691221dc (patch)
treec56c9bf9393d7dba514a9a3ab2a282e1c3b51b67 /Utilities/ClangTidyModule
parentf8107e7c6bc007d6b00a2ba11dfd685a4606824a (diff)
downloadcmake-04638e7358f1c2738d3e07b4ac51b05f691221dc.tar.gz
clang-tidy module: add check for ostringstream
Diffstat (limited to 'Utilities/ClangTidyModule')
-rw-r--r--Utilities/ClangTidyModule/CMakeLists.txt2
-rw-r--r--Utilities/ClangTidyModule/Module.cxx3
-rw-r--r--Utilities/ClangTidyModule/OstringstreamUseCmstrcatCheck.cxx52
-rw-r--r--Utilities/ClangTidyModule/OstringstreamUseCmstrcatCheck.h21
4 files changed, 78 insertions, 0 deletions
diff --git a/Utilities/ClangTidyModule/CMakeLists.txt b/Utilities/ClangTidyModule/CMakeLists.txt
index 8e7b2bc5d2..c51f43a6a6 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 ca9a81201a..7ef8e7dabe 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 "OstringstreamUseCmstrcatCheck.h"
#include "UseBespokeEnumClassCheck.h"
#include "UseCmstrlenCheck.h"
#include "UseCmsysFstreamCheck.h"
@@ -20,6 +21,8 @@ public:
"cmake-use-cmsys-fstream");
CheckFactories.registerCheck<UseBespokeEnumClassCheck>(
"cmake-use-bespoke-enum-class");
+ CheckFactories.registerCheck<OstringstreamUseCmstrcatCheck>(
+ "cmake-ostringstream-use-cmstrcat");
}
};
diff --git a/Utilities/ClangTidyModule/OstringstreamUseCmstrcatCheck.cxx b/Utilities/ClangTidyModule/OstringstreamUseCmstrcatCheck.cxx
new file mode 100644
index 0000000000..920fdf3a90
--- /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 <clang/AST/Type.h>
+#include <clang/ASTMatchers/ASTMatchFinder.h>
+
+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<TypeLoc>("parentType");
+ const TypeLoc* RootNode = Result.Nodes.getNodeAs<TypeLoc>("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 0000000000..ecb5616cfc
--- /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 <clang-tidy/ClangTidyCheck.h>
+#include <clang/ASTMatchers/ASTMatchFinder.h>
+
+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;
+};
+}
+}
+}