1//===--- NonCopyableObjects.cpp - clang-tidy-------------------------------===//
2//
3// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4// See https://llvm.org/LICENSE.txt for license information.
5// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6//
7//===----------------------------------------------------------------------===//
8
9#include "NonCopyableObjects.h"
10#include "clang/AST/ASTContext.h"
11#include "clang/ASTMatchers/ASTMatchFinder.h"
12
13using namespace clang::ast_matchers;
14
15namespace clang::tidy::misc {
16
17void NonCopyableObjectsCheck::registerMatchers(MatchFinder *Finder) {
18 // There are two ways to get into trouble with objects like FILE *:
19 // dereferencing the pointer type to be a non-pointer type, and declaring
20 // the type as a non-pointer type in the first place. While the declaration
21 // itself could technically be well-formed in the case where the type is not
22 // an opaque type, it's highly suspicious behavior.
23 //
24 // POSIX types are a bit different in that it's reasonable to declare a
25 // non-pointer variable or data member of the type, but it is not reasonable
26 // to dereference a pointer to the type, or declare a parameter of non-pointer
27 // type.
28 // FIXME: it would be good to make a list that is also user-configurable so
29 // that users can add their own elements to the list. However, it may require
30 // some extra thought since POSIX types and FILE types are usable in different
31 // ways.
32
33 auto BadFILEType = hasType(
34 InnerMatcher: namedDecl(hasAnyName("::FILE", "FILE", "std::FILE")).bind(ID: "type_decl"));
35 auto BadPOSIXType =
36 hasType(InnerMatcher: namedDecl(hasAnyName("::pthread_cond_t", "::pthread_mutex_t",
37 "pthread_cond_t", "pthread_mutex_t"))
38 .bind(ID: "type_decl"));
39 auto BadEitherType = anyOf(BadFILEType, BadPOSIXType);
40
41 Finder->addMatcher(
42 NodeMatch: namedDecl(anyOf(varDecl(BadFILEType), fieldDecl(BadFILEType)))
43 .bind(ID: "decl"),
44 Action: this);
45 Finder->addMatcher(NodeMatch: parmVarDecl(BadPOSIXType).bind(ID: "decl"), Action: this);
46 Finder->addMatcher(
47 NodeMatch: expr(unaryOperator(hasOperatorName(Name: "*"), BadEitherType)).bind(ID: "expr"),
48 Action: this);
49}
50
51void NonCopyableObjectsCheck::check(const MatchFinder::MatchResult &Result) {
52 const auto *D = Result.Nodes.getNodeAs<NamedDecl>(ID: "decl");
53 const auto *BD = Result.Nodes.getNodeAs<NamedDecl>(ID: "type_decl");
54 const auto *E = Result.Nodes.getNodeAs<Expr>(ID: "expr");
55
56 if (D && BD)
57 diag(D->getLocation(), "%0 declared as type '%1', which is unsafe to copy"
58 "; did you mean '%1 *'?")
59 << D << BD->getName();
60 else if (E)
61 diag(Loc: E->getExprLoc(),
62 Description: "expression has opaque data structure type %0; type should only be "
63 "used as a pointer and not dereferenced")
64 << BD;
65}
66
67} // namespace clang::tidy::misc
68

Provided by KDAB

Privacy Policy
Update your C++ knowledge – Modern C++11/14/17 Training
Find out more

source code of clang-tools-extra/clang-tidy/misc/NonCopyableObjects.cpp