xref: /llvm-project/clang-tools-extra/clang-tidy/misc/NonCopyableObjects.cpp (revision 8db15e4a4d758ed0c2ceb17f9c0b4911f1a9221b)
1 //===--- NonCopyableObjects.cpp - clang-tidy-------------------------------===//
2 //
3 //                     The LLVM Compiler Infrastructure
4 //
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
7 //
8 //===----------------------------------------------------------------------===//
9 
10 #include "NonCopyableObjects.h"
11 #include "clang/AST/ASTContext.h"
12 #include "clang/ASTMatchers/ASTMatchFinder.h"
13 #include <algorithm>
14 
15 using namespace clang::ast_matchers;
16 
17 namespace clang {
18 namespace {
19 // FIXME: it would be good to make a list that is also user-configurable so that
20 // users can add their own elements to the list. However, it may require some
21 // extra thought since POSIX types and FILE types are usable in different ways.
22 bool isPOSIXTypeName(StringRef ClassName) {
23   static const char *TypeNames[] = {
24     "::pthread_cond_t",
25     "::pthread_mutex_t",
26     "pthread_cond_t",
27     "pthread_mutex_t"
28   };
29   return std::binary_search(std::begin(TypeNames), std::end(TypeNames),
30                             ClassName);
31 }
32 
33 bool isFILETypeName(StringRef ClassName) {
34   static const char *TypeNames[] = {
35     "::FILE",
36     "FILE",
37     "std::FILE"
38   };
39   return std::binary_search(std::begin(TypeNames), std::end(TypeNames),
40                             ClassName);
41 }
42 
43 AST_MATCHER(NamedDecl, isFILEType) {
44   return isFILETypeName(Node.getQualifiedNameAsString());
45 }
46 
47 AST_MATCHER(NamedDecl, isPOSIXType) {
48   return isPOSIXTypeName(Node.getQualifiedNameAsString());
49 }
50 } // namespace
51 
52 namespace tidy {
53 void NonCopyableObjectsCheck::registerMatchers(MatchFinder *Finder) {
54   // There are two ways to get into trouble with objects like FILE *:
55   // dereferencing the pointer type to be a non-pointer type, and declaring
56   // the type as a non-pointer type in the first place. While the declaration
57   // itself could technically be well-formed in the case where the type is not
58   // an opaque type, it's highly suspicious behavior.
59   //
60   // POSIX types are a bit different in that it's reasonable to declare a
61   // non-pointer variable or data member of the type, but it is not reasonable
62   // to dereference a pointer to the type, or declare a parameter of non-pointer
63   // type.
64   auto BadFILEType = hasType(namedDecl(isFILEType()).bind("type_decl"));
65   auto BadPOSIXType = hasType(namedDecl(isPOSIXType()).bind("type_decl"));
66   auto BadEitherType = anyOf(BadFILEType, BadPOSIXType);
67 
68   Finder->addMatcher(
69       namedDecl(anyOf(varDecl(BadFILEType), fieldDecl(BadFILEType)))
70           .bind("decl"),
71       this);
72   Finder->addMatcher(parmVarDecl(BadPOSIXType).bind("decl"), this);
73   Finder->addMatcher(
74       expr(unaryOperator(hasOperatorName("*"), BadEitherType)).bind("expr"),
75       this);
76 }
77 
78 void NonCopyableObjectsCheck::check(const MatchFinder::MatchResult &Result) {
79   const auto *D = Result.Nodes.getNodeAs<NamedDecl>("decl");
80   const auto *BD = Result.Nodes.getNodeAs<NamedDecl>("type_decl");
81   const auto *E = Result.Nodes.getNodeAs<Expr>("expr");
82 
83   if (D && BD)
84     diag(D->getLocation(), "'%0' declared as type '%1', which is unsafe to copy"
85          "; did you mean '%1 *'?")
86     << D->getName() << BD->getName();
87   else if (E)
88     diag(E->getExprLoc(),
89          "expression has opaque data structure type '%0'; type should only be "
90          "used as a pointer and not dereferenced")
91         << BD->getName();
92 }
93 
94 } // namespace tidy
95 } // namespace clang
96 
97