clang  14.0.0git
ObjCPropertyChecker.cpp
Go to the documentation of this file.
1 //==- ObjCPropertyChecker.cpp - Check ObjC properties ------------*- C++ -*-==//
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 // This checker finds issues with Objective-C properties.
10 // Currently finds only one kind of issue:
11 // - Find synthesized properties with copy attribute of mutable NS collection
12 // types. Calling -copy on such collections produces an immutable copy,
13 // which contradicts the type of the property.
14 //
15 //===----------------------------------------------------------------------===//
16 
20 
21 using namespace clang;
22 using namespace ento;
23 
24 namespace {
25 class ObjCPropertyChecker
26  : public Checker<check::ASTDecl<ObjCPropertyDecl>> {
27  void checkCopyMutable(const ObjCPropertyDecl *D, BugReporter &BR) const;
28 
29 public:
30  void checkASTDecl(const ObjCPropertyDecl *D, AnalysisManager &Mgr,
31  BugReporter &BR) const;
32 };
33 } // end anonymous namespace.
34 
35 void ObjCPropertyChecker::checkASTDecl(const ObjCPropertyDecl *D,
36  AnalysisManager &Mgr,
37  BugReporter &BR) const {
38  checkCopyMutable(D, BR);
39 }
40 
41 void ObjCPropertyChecker::checkCopyMutable(const ObjCPropertyDecl *D,
42  BugReporter &BR) const {
44  return;
45 
46  QualType T = D->getType();
47  if (!T->isObjCObjectPointerType())
48  return;
49 
50  const std::string &PropTypeName(T->getPointeeType().getCanonicalType()
52  .getAsString());
53  if (!StringRef(PropTypeName).startswith("NSMutable"))
54  return;
55 
56  const ObjCImplDecl *ImplD = nullptr;
57  if (const ObjCInterfaceDecl *IntD =
58  dyn_cast<ObjCInterfaceDecl>(D->getDeclContext())) {
59  ImplD = IntD->getImplementation();
60  } else if (auto *CatD = dyn_cast<ObjCCategoryDecl>(D->getDeclContext())) {
61  ImplD = CatD->getClassInterface()->getImplementation();
62  }
63 
64  if (!ImplD || ImplD->HasUserDeclaredSetterMethod(D))
65  return;
66 
67  SmallString<128> Str;
68  llvm::raw_svector_ostream OS(Str);
69  OS << "Property of mutable type '" << PropTypeName
70  << "' has 'copy' attribute; an immutable object will be stored instead";
71 
72  BR.EmitBasicReport(
73  D, this, "Objective-C property misuse", "Logic error", OS.str(),
74  PathDiagnosticLocation::createBegin(D, BR.getSourceManager()),
75  D->getSourceRange());
76 }
77 
78 void ento::registerObjCPropertyChecker(CheckerManager &Mgr) {
79  Mgr.registerChecker<ObjCPropertyChecker>();
80 }
81 
82 bool ento::shouldRegisterObjCPropertyChecker(const CheckerManager &mgr) {
83  return true;
84 }
clang::ObjCInterfaceDecl
Represents an ObjC class declaration.
Definition: DeclObjC.h:1151
clang::ObjCPropertyDecl::isReadOnly
bool isReadOnly() const
isReadOnly - Return true iff the property has a setter.
Definition: DeclObjC.h:838
clang::ObjCPropertyDecl::getSourceRange
SourceRange getSourceRange() const override LLVM_READONLY
Source range that this declaration covers.
Definition: DeclObjC.h:928
string
string(SUBSTRING ${CMAKE_CURRENT_BINARY_DIR} 0 ${PATH_LIB_START} PATH_HEAD) string(SUBSTRING $
Definition: CMakeLists.txt:22
clang::QualType
A (possibly-)qualified type.
Definition: Type.h:673
clang::QualType::getCanonicalType
QualType getCanonicalType() const
Definition: Type.h:6463
clang::QualType::getAsString
static std::string getAsString(SplitQualType split, const PrintingPolicy &Policy)
Definition: Type.h:1015
clang::ObjCContainerDecl::HasUserDeclaredSetterMethod
bool HasUserDeclaredSetterMethod(const ObjCPropertyDecl *P) const
This routine returns 'true' if a user declared setter method was found in the class,...
Definition: DeclObjC.cpp:124
clang::ObjCImplDecl::getClassInterface
const ObjCInterfaceDecl * getClassInterface() const
Definition: DeclObjC.h:2434
BuiltinCheckerRegistration.h
clang::ObjCInterfaceDecl::getImplementation
ObjCImplementationDecl * getImplementation() const
Definition: DeclObjC.cpp:1588
BugReporter.h
llvm::SmallString< 128 >
clang::Type::getPointeeType
QualType getPointeeType() const
If this is a pointer, ObjC object pointer, or block pointer, this returns the respective pointee.
Definition: Type.cpp:625
clang::ObjCPropertyDecl::getType
QualType getType() const
Definition: DeclObjC.h:804
clang::QualType::getUnqualifiedType
QualType getUnqualifiedType() const
Retrieve the unqualified variant of the given type, removing as little sugar as possible.
Definition: Type.h:6504
clang::Type::isObjCObjectPointerType
bool isObjCObjectPointerType() const
Definition: Type.h:6794
clang::ObjCPropertyDecl::getSetterKind
SetterKind getSetterKind() const
getSetterKind - Return the method used for doing assignment in the property setter.
Definition: DeclObjC.h:873
clang::ObjCPropertyDecl::Copy
@ Copy
Definition: DeclObjC.h:736
clang::ObjCImplDecl
Definition: DeclObjC.h:2419
clang::ObjCPropertyDecl
Represents one property declaration in an Objective-C interface.
Definition: DeclObjC.h:732
Checker.h
clang
Definition: CalledOnceCheck.h:17
clang::ento::PathDiagnosticLocation::createBegin
static PathDiagnosticLocation createBegin(const Decl *D, const SourceManager &SM)
Create a location for the beginning of the declaration.
Definition: PathDiagnostic.cpp:580
clang::ento::ObjKind::OS
@ OS
Indicates that the tracking object is a descendant of a referenced-counted OSObject,...
clang::Decl::getDeclContext
DeclContext * getDeclContext()
Definition: DeclBase.h:439