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

0880114ada clang-tidy: disable ostringstream check
75ab77ee19 clang-tidy module: add test for ostringstream check
04638e7358 clang-tidy module: add check for ostringstream

Acked-by: Kitware Robot <kwrobot@kitware.com>
Merge-request: !7911
This commit is contained in:
Brad King
2022-11-15 14:12:43 +00:00
committed by Kitware Robot
8 changed files with 96 additions and 0 deletions

View File

@@ -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)$'

View File

@@ -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

View File

@@ -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");
}
};

View File

@@ -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");
}
}
}
}
}
}

View File

@@ -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;
};
}
}
}

View File

@@ -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)

View File

@@ -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)
^

View File

@@ -0,0 +1,10 @@
#include <sstream>
void check()
{
std::ostringstream test;
}
void check2(std::ostringstream& test2)
{
}