clang  6.0.0svn
ObjCPropertyChecker.cpp
Go to the documentation of this file.
1 //==- ObjCPropertyChecker.cpp - Check ObjC properties ------------*- C++ -*-==//
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 // This checker finds issues with Objective-C properties.
11 // Currently finds only one kind of issue:
12 // - Find synthesized properties with copy attribute of mutable NS collection
13 // types. Calling -copy on such collections produces an immutable copy,
14 // which contradicts the type of the property.
15 //
16 //===----------------------------------------------------------------------===//
17 
18 #include "ClangSACheckers.h"
21 
22 using namespace clang;
23 using namespace ento;
24 
25 namespace {
26 class ObjCPropertyChecker
27  : public Checker<check::ASTDecl<ObjCPropertyDecl>> {
28  void checkCopyMutable(const ObjCPropertyDecl *D, BugReporter &BR) const;
29 
30 public:
31  void checkASTDecl(const ObjCPropertyDecl *D, AnalysisManager &Mgr,
32  BugReporter &BR) const;
33 };
34 } // end anonymous namespace.
35 
36 void ObjCPropertyChecker::checkASTDecl(const ObjCPropertyDecl *D,
37  AnalysisManager &Mgr,
38  BugReporter &BR) const {
39  checkCopyMutable(D, BR);
40 }
41 
42 void ObjCPropertyChecker::checkCopyMutable(const ObjCPropertyDecl *D,
43  BugReporter &BR) const {
45  return;
46 
47  QualType T = D->getType();
48  if (!T->isObjCObjectPointerType())
49  return;
50 
51  const std::string &PropTypeName(T->getPointeeType().getCanonicalType()
53  .getAsString());
54  if (!StringRef(PropTypeName).startswith("NSMutable"))
55  return;
56 
57  const ObjCImplDecl *ImplD = nullptr;
58  if (const ObjCInterfaceDecl *IntD =
59  dyn_cast<ObjCInterfaceDecl>(D->getDeclContext())) {
60  ImplD = IntD->getImplementation();
61  } else if (auto *CatD = dyn_cast<ObjCCategoryDecl>(D->getDeclContext())) {
62  ImplD = CatD->getClassInterface()->getImplementation();
63  }
64 
65  if (!ImplD || ImplD->HasUserDeclaredSetterMethod(D))
66  return;
67 
68  SmallString<128> Str;
69  llvm::raw_svector_ostream OS(Str);
70  OS << "Property of mutable type '" << PropTypeName
71  << "' has 'copy' attribute; an immutable object will be stored instead";
72 
73  BR.EmitBasicReport(
74  D, this, "Objective-C property misuse", "Logic error", OS.str(),
76  D->getSourceRange());
77 }
78 
79 void ento::registerObjCPropertyChecker(CheckerManager &Mgr) {
80  Mgr.registerChecker<ObjCPropertyChecker>();
81 }
A (possibly-)qualified type.
Definition: Type.h:653
QualType getPointeeType() const
If this is a pointer, ObjC object pointer, or block pointer, this returns the respective pointee...
Definition: Type.cpp:456
SetterKind getSetterKind() const
getSetterKind - Return the method used for doing assignment in the property setter.
Definition: DeclObjC.h:911
std::string getAsString() const
Definition: Type.h:979
bool isReadOnly() const
isReadOnly - Return true iff the property has a setter.
Definition: DeclObjC.h:880
Represents an ObjC class declaration.
Definition: DeclObjC.h:1191
bool HasUserDeclaredSetterMethod(const ObjCPropertyDecl *P) const
This routine returns &#39;true&#39; if a user declared setter method was found in the class, its protocols, its super classes or categories.
Definition: DeclObjC.cpp:117
const FunctionProtoType * T
DeclContext * getDeclContext()
Definition: DeclBase.h:425
QualType getType() const
Definition: DeclObjC.h:846
BugReporter is a utility class for generating PathDiagnostics for analysis.
Definition: BugReporter.h:403
static PathDiagnosticLocation createBegin(const Decl *D, const SourceManager &SM)
Create a location for the beginning of the declaration.
QualType getCanonicalType() const
Definition: Type.h:5759
CHECKER * registerChecker()
Used to register checkers.
void EmitBasicReport(const Decl *DeclWithIssue, const CheckerBase *Checker, StringRef BugName, StringRef BugCategory, StringRef BugStr, PathDiagnosticLocation Loc, ArrayRef< SourceRange > Ranges=None)
bool isObjCObjectPointerType() const
Definition: Type.h:6041
Represents one property declaration in an Objective-C interface.
Definition: DeclObjC.h:746
ObjCImplementationDecl * getImplementation() const
Definition: DeclObjC.cpp:1513
const ObjCInterfaceDecl * getClassInterface() const
Definition: DeclObjC.h:2459
Dataflow Directional Tag Classes.
SourceRange getSourceRange() const override LLVM_READONLY
Source range that this declaration covers.
Definition: DeclObjC.h:966
SourceManager & getSourceManager()
Definition: BugReporter.h:463
QualType getUnqualifiedType() const
Retrieve the unqualified variant of the given type, removing as little sugar as possible.
Definition: Type.h:5800