diff options
author | thegeorg <thegeorg@yandex-team.com> | 2024-03-13 13:58:24 +0300 |
---|---|---|
committer | thegeorg <thegeorg@yandex-team.com> | 2024-03-13 14:11:53 +0300 |
commit | 11a895b7e15d1c5a1f52706396b82e3f9db953cb (patch) | |
tree | fabc6d883b0f946151f61ae7865cee9f529a1fdd /contrib/libs/clang16/tools/extra/clang-tidy/misc/NonCopyableObjects.cpp | |
parent | 9685917341315774aad5733b1793b1e533a88bbb (diff) | |
download | ydb-11a895b7e15d1c5a1f52706396b82e3f9db953cb.tar.gz |
Export clang-format16 via ydblib project
6e6be3a95868fde888d801b7590af4044049563f
Diffstat (limited to 'contrib/libs/clang16/tools/extra/clang-tidy/misc/NonCopyableObjects.cpp')
-rw-r--r-- | contrib/libs/clang16/tools/extra/clang-tidy/misc/NonCopyableObjects.cpp | 68 |
1 files changed, 68 insertions, 0 deletions
diff --git a/contrib/libs/clang16/tools/extra/clang-tidy/misc/NonCopyableObjects.cpp b/contrib/libs/clang16/tools/extra/clang-tidy/misc/NonCopyableObjects.cpp new file mode 100644 index 0000000000..4c415a7fd2 --- /dev/null +++ b/contrib/libs/clang16/tools/extra/clang-tidy/misc/NonCopyableObjects.cpp @@ -0,0 +1,68 @@ +//===--- NonCopyableObjects.cpp - clang-tidy-------------------------------===// +// +// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. +// See https://llvm.org/LICENSE.txt for license information. +// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception +// +//===----------------------------------------------------------------------===// + +#include "NonCopyableObjects.h" +#include "clang/AST/ASTContext.h" +#include "clang/ASTMatchers/ASTMatchFinder.h" +#include <algorithm> + +using namespace clang::ast_matchers; + +namespace clang::tidy::misc { + +void NonCopyableObjectsCheck::registerMatchers(MatchFinder *Finder) { + // There are two ways to get into trouble with objects like FILE *: + // dereferencing the pointer type to be a non-pointer type, and declaring + // the type as a non-pointer type in the first place. While the declaration + // itself could technically be well-formed in the case where the type is not + // an opaque type, it's highly suspicious behavior. + // + // POSIX types are a bit different in that it's reasonable to declare a + // non-pointer variable or data member of the type, but it is not reasonable + // to dereference a pointer to the type, or declare a parameter of non-pointer + // type. + // FIXME: it would be good to make a list that is also user-configurable so + // that users can add their own elements to the list. However, it may require + // some extra thought since POSIX types and FILE types are usable in different + // ways. + + auto BadFILEType = hasType( + namedDecl(hasAnyName("::FILE", "FILE", "std::FILE")).bind("type_decl")); + auto BadPOSIXType = + hasType(namedDecl(hasAnyName("::pthread_cond_t", "::pthread_mutex_t", + "pthread_cond_t", "pthread_mutex_t")) + .bind("type_decl")); + auto BadEitherType = anyOf(BadFILEType, BadPOSIXType); + + Finder->addMatcher( + namedDecl(anyOf(varDecl(BadFILEType), fieldDecl(BadFILEType))) + .bind("decl"), + this); + Finder->addMatcher(parmVarDecl(BadPOSIXType).bind("decl"), this); + Finder->addMatcher( + expr(unaryOperator(hasOperatorName("*"), BadEitherType)).bind("expr"), + this); +} + +void NonCopyableObjectsCheck::check(const MatchFinder::MatchResult &Result) { + const auto *D = Result.Nodes.getNodeAs<NamedDecl>("decl"); + const auto *BD = Result.Nodes.getNodeAs<NamedDecl>("type_decl"); + const auto *E = Result.Nodes.getNodeAs<Expr>("expr"); + + if (D && BD) + diag(D->getLocation(), "%0 declared as type '%1', which is unsafe to copy" + "; did you mean '%1 *'?") + << D << BD->getName(); + else if (E) + diag(E->getExprLoc(), + "expression has opaque data structure type %0; type should only be " + "used as a pointer and not dereferenced") + << BD; +} + +} // namespace clang::tidy::misc |