1 //==- ObjCPropertyChecker.cpp - Check ObjC properties ------------*- C++ -*-==//
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
7 //===----------------------------------------------------------------------===//
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.
15 //===----------------------------------------------------------------------===//
17 #include "clang/StaticAnalyzer/Checkers/BuiltinCheckerRegistration.h"
18 #include "clang/StaticAnalyzer/Core/BugReporter/BugReporter.h"
19 #include "clang/StaticAnalyzer/Core/Checker.h"
21 using namespace clang
;
25 class ObjCPropertyChecker
26 : public Checker
<check::ASTDecl
<ObjCPropertyDecl
>> {
27 void checkCopyMutable(const ObjCPropertyDecl
*D
, BugReporter
&BR
) const;
30 void checkASTDecl(const ObjCPropertyDecl
*D
, AnalysisManager
&Mgr
,
31 BugReporter
&BR
) const;
33 } // end anonymous namespace.
35 void ObjCPropertyChecker::checkASTDecl(const ObjCPropertyDecl
*D
,
37 BugReporter
&BR
) const {
38 checkCopyMutable(D
, BR
);
41 void ObjCPropertyChecker::checkCopyMutable(const ObjCPropertyDecl
*D
,
42 BugReporter
&BR
) const {
43 if (D
->isReadOnly() || D
->getSetterKind() != ObjCPropertyDecl::Copy
)
46 QualType T
= D
->getType();
47 if (!T
->isObjCObjectPointerType())
50 const std::string
&PropTypeName(T
->getPointeeType().getCanonicalType()
53 if (!StringRef(PropTypeName
).startswith("NSMutable"))
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();
64 if (!ImplD
|| ImplD
->HasUserDeclaredSetterMethod(D
))
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";
73 D
, this, "Objective-C property misuse", "Logic error", OS
.str(),
74 PathDiagnosticLocation::createBegin(D
, BR
.getSourceManager()),
78 void ento::registerObjCPropertyChecker(CheckerManager
&Mgr
) {
79 Mgr
.registerChecker
<ObjCPropertyChecker
>();
82 bool ento::shouldRegisterObjCPropertyChecker(const CheckerManager
&mgr
) {