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 tidy {
19 namespace misc {
20 
21 void NonCopyableObjectsCheck::registerMatchers(MatchFinder *Finder) {
22   // There are two ways to get into trouble with objects like FILE *:
23   // dereferencing the pointer type to be a non-pointer type, and declaring
24   // the type as a non-pointer type in the first place. While the declaration
25   // itself could technically be well-formed in the case where the type is not
26   // an opaque type, it's highly suspicious behavior.
27   //
28   // POSIX types are a bit different in that it's reasonable to declare a
29   // non-pointer variable or data member of the type, but it is not reasonable
30   // to dereference a pointer to the type, or declare a parameter of non-pointer
31   // type.
32   // FIXME: it would be good to make a list that is also user-configurable so
33   // that users can add their own elements to the list. However, it may require
34   // some extra thought since POSIX types and FILE types are usable in different
35   // ways.
36 
37   auto BadFILEType = hasType(
38       namedDecl(hasAnyName("::FILE", "FILE", "std::FILE")).bind("type_decl"));
39   auto BadPOSIXType =
40       hasType(namedDecl(hasAnyName("::pthread_cond_t", "::pthread_mutex_t",
41                                    "pthread_cond_t", "pthread_mutex_t"))
42                   .bind("type_decl"));
43   auto BadEitherType = anyOf(BadFILEType, BadPOSIXType);
44 
45   Finder->addMatcher(
46       namedDecl(anyOf(varDecl(BadFILEType), fieldDecl(BadFILEType)))
47           .bind("decl"),
48       this);
49   Finder->addMatcher(parmVarDecl(BadPOSIXType).bind("decl"), this);
50   Finder->addMatcher(
51       expr(unaryOperator(hasOperatorName("*"), BadEitherType)).bind("expr"),
52       this);
53 }
54 
55 void NonCopyableObjectsCheck::check(const MatchFinder::MatchResult &Result) {
56   const auto *D = Result.Nodes.getNodeAs<NamedDecl>("decl");
57   const auto *BD = Result.Nodes.getNodeAs<NamedDecl>("type_decl");
58   const auto *E = Result.Nodes.getNodeAs<Expr>("expr");
59 
60   if (D && BD)
61     diag(D->getLocation(), "%0 declared as type '%1', which is unsafe to copy"
62                            "; did you mean '%1 *'?")
63         << D << BD->getName();
64   else if (E)
65     diag(E->getExprLoc(),
66          "expression has opaque data structure type %0; type should only be "
67          "used as a pointer and not dereferenced")
68         << BD;
69 }
70 
71 } // namespace misc
72 } // namespace tidy
73 } // namespace clang
74 
75