blob: f6dd2d2471186b344c851ea74db9f6ef060238e7 [file] [log] [blame]
Ted Kremenek29e0ef22009-11-03 06:59:59 +00001//=- NSErrorCheckerer.cpp - Coding conventions for uses of NSError -*- C++ -*-==//
Ted Kremenekf45d18c2008-09-18 06:33:41 +00002//
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 file defines a CheckNSError, a flow-insenstive check
11// that determines if an Objective-C class interface correctly returns
12// a non-void return type.
13//
14// File under feature request PR 2600.
15//
16//===----------------------------------------------------------------------===//
17
18#include "clang/Analysis/LocalCheckers.h"
19#include "clang/Analysis/PathSensitive/BugReporter.h"
Ted Kremenekcfdf9b42008-09-18 21:25:13 +000020#include "clang/Analysis/PathSensitive/GRExprEngine.h"
Ted Kremenekdc998c12009-11-03 18:41:06 +000021#include "clang/Analysis/PathSensitive/Checkers/DereferenceChecker.h"
Ted Kremenekcfdf9b42008-09-18 21:25:13 +000022#include "BasicObjCFoundationChecks.h"
Ted Kremenekf45d18c2008-09-18 06:33:41 +000023#include "clang/AST/DeclObjC.h"
Ted Kremenekcfdf9b42008-09-18 21:25:13 +000024#include "clang/AST/Decl.h"
25#include "llvm/ADT/SmallVector.h"
Ted Kremenekf45d18c2008-09-18 06:33:41 +000026
27using namespace clang;
28
Ted Kremenekcfdf9b42008-09-18 21:25:13 +000029namespace {
Kovarththanan Rajaratnamba5fb5a2009-11-28 06:07:30 +000030class NSErrorChecker : public BugType {
Zhongxing Xu5ab128b2009-08-21 02:18:44 +000031 const Decl &CodeDecl;
Ted Kremenekcf118d42009-02-04 23:49:09 +000032 const bool isNSErrorWarning;
33 IdentifierInfo * const II;
34 GRExprEngine &Eng;
Mike Stump1eb44332009-09-09 15:08:12 +000035
Zhongxing Xu5ab128b2009-08-21 02:18:44 +000036 void CheckSignature(const ObjCMethodDecl& MD, QualType& ResultTy,
Ted Kremenekcf118d42009-02-04 23:49:09 +000037 llvm::SmallVectorImpl<VarDecl*>& ErrorParams);
Mike Stump1eb44332009-09-09 15:08:12 +000038
Zhongxing Xu5ab128b2009-08-21 02:18:44 +000039 void CheckSignature(const FunctionDecl& MD, QualType& ResultTy,
Ted Kremenekcf118d42009-02-04 23:49:09 +000040 llvm::SmallVectorImpl<VarDecl*>& ErrorParams);
Ted Kremenekcfdf9b42008-09-18 21:25:13 +000041
Ted Kremenekcf118d42009-02-04 23:49:09 +000042 bool CheckNSErrorArgument(QualType ArgTy);
43 bool CheckCFErrorArgument(QualType ArgTy);
Mike Stump1eb44332009-09-09 15:08:12 +000044
Ted Kremenekd17da2b2009-08-21 22:28:32 +000045 void CheckParamDeref(const VarDecl *V, const LocationContext *LC,
46 const GRState *state, BugReporter& BR);
Mike Stump1eb44332009-09-09 15:08:12 +000047
Zhongxing Xu5ab128b2009-08-21 02:18:44 +000048 void EmitRetTyWarning(BugReporter& BR, const Decl& CodeDecl);
Mike Stump1eb44332009-09-09 15:08:12 +000049
Ted Kremenekcfdf9b42008-09-18 21:25:13 +000050public:
Ted Kremenek29e0ef22009-11-03 06:59:59 +000051 NSErrorChecker(const Decl &D, bool isNSError, GRExprEngine& eng)
Mike Stump1eb44332009-09-09 15:08:12 +000052 : BugType(isNSError ? "NSError** null dereference"
Zhongxing Xu5ab128b2009-08-21 02:18:44 +000053 : "CFErrorRef* null dereference",
Ted Kremenek27a36e92009-09-01 00:17:12 +000054 "Coding conventions (Apple)"),
Zhongxing Xu5ab128b2009-08-21 02:18:44 +000055 CodeDecl(D),
Mike Stump1eb44332009-09-09 15:08:12 +000056 isNSErrorWarning(isNSError),
Ted Kremenekcf118d42009-02-04 23:49:09 +000057 II(&eng.getContext().Idents.get(isNSErrorWarning ? "NSError":"CFErrorRef")),
58 Eng(eng) {}
Mike Stump1eb44332009-09-09 15:08:12 +000059
Ted Kremenekcf118d42009-02-04 23:49:09 +000060 void FlushReports(BugReporter& BR);
Mike Stump1eb44332009-09-09 15:08:12 +000061};
62
Ted Kremenekcfdf9b42008-09-18 21:25:13 +000063} // end anonymous namespace
64
Mike Stump1eb44332009-09-09 15:08:12 +000065void clang::RegisterNSErrorChecks(BugReporter& BR, GRExprEngine &Eng,
Zhongxing Xu5ab128b2009-08-21 02:18:44 +000066 const Decl &D) {
Ted Kremenek29e0ef22009-11-03 06:59:59 +000067 BR.Register(new NSErrorChecker(D, true, Eng));
68 BR.Register(new NSErrorChecker(D, false, Eng));
Ted Kremenekcfdf9b42008-09-18 21:25:13 +000069}
70
Ted Kremenek29e0ef22009-11-03 06:59:59 +000071void NSErrorChecker::FlushReports(BugReporter& BR) {
Ted Kremenekcfdf9b42008-09-18 21:25:13 +000072 // Get the analysis engine and the exploded analysis graph.
Zhongxing Xu031ccc02009-08-06 12:48:26 +000073 ExplodedGraph& G = Eng.getGraph();
Mike Stump1eb44332009-09-09 15:08:12 +000074
Ted Kremenekcfdf9b42008-09-18 21:25:13 +000075 // Get the ASTContext, which is useful for querying type information.
Ted Kremenekf45d18c2008-09-18 06:33:41 +000076 ASTContext &Ctx = BR.getContext();
Ted Kremenekcfdf9b42008-09-18 21:25:13 +000077
78 QualType ResultTy;
Ted Kremenekcf118d42009-02-04 23:49:09 +000079 llvm::SmallVector<VarDecl*, 5> ErrorParams;
Ted Kremenekcc9ac412008-10-01 23:24:09 +000080
Zhongxing Xu5ab128b2009-08-21 02:18:44 +000081 if (const ObjCMethodDecl* MD = dyn_cast<ObjCMethodDecl>(&CodeDecl))
Ted Kremenekcf118d42009-02-04 23:49:09 +000082 CheckSignature(*MD, ResultTy, ErrorParams);
Zhongxing Xu5ab128b2009-08-21 02:18:44 +000083 else if (const FunctionDecl* FD = dyn_cast<FunctionDecl>(&CodeDecl))
Ted Kremenekcf118d42009-02-04 23:49:09 +000084 CheckSignature(*FD, ResultTy, ErrorParams);
Ted Kremenekcc9ac412008-10-01 23:24:09 +000085 else
Ted Kremenekcfdf9b42008-09-18 21:25:13 +000086 return;
Mike Stump1eb44332009-09-09 15:08:12 +000087
Ted Kremenekcf118d42009-02-04 23:49:09 +000088 if (ErrorParams.empty())
Ted Kremenekcc9ac412008-10-01 23:24:09 +000089 return;
Mike Stump1eb44332009-09-09 15:08:12 +000090
Ted Kremenekcf118d42009-02-04 23:49:09 +000091 if (ResultTy == Ctx.VoidTy) EmitRetTyWarning(BR, CodeDecl);
Mike Stump1eb44332009-09-09 15:08:12 +000092
93 for (ExplodedGraph::roots_iterator RI=G.roots_begin(), RE=G.roots_end();
Zhongxing Xu031ccc02009-08-06 12:48:26 +000094 RI!=RE; ++RI) {
Ted Kremenekcf118d42009-02-04 23:49:09 +000095 // Scan the parameters for an implicit null dereference.
96 for (llvm::SmallVectorImpl<VarDecl*>::iterator I=ErrorParams.begin(),
Mike Stump1eb44332009-09-09 15:08:12 +000097 E=ErrorParams.end(); I!=E; ++I)
Ted Kremenekd17da2b2009-08-21 22:28:32 +000098 CheckParamDeref(*I, (*RI)->getLocationContext(), (*RI)->getState(), BR);
Ted Kremenekcc9ac412008-10-01 23:24:09 +000099 }
Ted Kremenekf45d18c2008-09-18 06:33:41 +0000100}
Ted Kremenekcfdf9b42008-09-18 21:25:13 +0000101
Ted Kremenek29e0ef22009-11-03 06:59:59 +0000102void NSErrorChecker::EmitRetTyWarning(BugReporter& BR, const Decl& CodeDecl) {
Ted Kremenekcf118d42009-02-04 23:49:09 +0000103 std::string sbuf;
104 llvm::raw_string_ostream os(sbuf);
Mike Stump1eb44332009-09-09 15:08:12 +0000105
Ted Kremenekcc9ac412008-10-01 23:24:09 +0000106 if (isa<ObjCMethodDecl>(CodeDecl))
107 os << "Method";
108 else
Mike Stump1eb44332009-09-09 15:08:12 +0000109 os << "Function";
110
Ted Kremenekcc9ac412008-10-01 23:24:09 +0000111 os << " accepting ";
112 os << (isNSErrorWarning ? "NSError**" : "CFErrorRef*");
113 os << " should have a non-void return value to indicate whether or not an "
Ted Kremenek355a6922009-08-06 05:01:36 +0000114 "error occurred";
Mike Stump1eb44332009-09-09 15:08:12 +0000115
Ted Kremeneke576af22009-11-24 01:33:10 +0000116 // FIXME: Remove when we migrate EmitBasicReport to StringRef.
117 std::string cat = getCategory().str();
118
Ted Kremenekcc9ac412008-10-01 23:24:09 +0000119 BR.EmitBasicReport(isNSErrorWarning
120 ? "Bad return type when passing NSError**"
121 : "Bad return type when passing CFError*",
Ted Kremeneke576af22009-11-24 01:33:10 +0000122 cat.c_str(), os.str().c_str(),
Ted Kremenekcf118d42009-02-04 23:49:09 +0000123 CodeDecl.getLocation());
Ted Kremenekcc9ac412008-10-01 23:24:09 +0000124}
125
126void
Ted Kremenek29e0ef22009-11-03 06:59:59 +0000127NSErrorChecker::CheckSignature(const ObjCMethodDecl& M, QualType& ResultTy,
Ted Kremenekcf118d42009-02-04 23:49:09 +0000128 llvm::SmallVectorImpl<VarDecl*>& ErrorParams) {
Ted Kremenekcfdf9b42008-09-18 21:25:13 +0000129
130 ResultTy = M.getResultType();
Mike Stump1eb44332009-09-09 15:08:12 +0000131
132 for (ObjCMethodDecl::param_iterator I=M.param_begin(),
Ted Kremenekcc9ac412008-10-01 23:24:09 +0000133 E=M.param_end(); I!=E; ++I) {
134
Mike Stump1eb44332009-09-09 15:08:12 +0000135 QualType T = (*I)->getType();
Ted Kremenekcc9ac412008-10-01 23:24:09 +0000136
Ted Kremenekcf118d42009-02-04 23:49:09 +0000137 if (isNSErrorWarning) {
138 if (CheckNSErrorArgument(T)) ErrorParams.push_back(*I);
139 }
140 else if (CheckCFErrorArgument(T))
141 ErrorParams.push_back(*I);
Ted Kremenekcc9ac412008-10-01 23:24:09 +0000142 }
Ted Kremenekcfdf9b42008-09-18 21:25:13 +0000143}
144
Ted Kremenekcc9ac412008-10-01 23:24:09 +0000145void
Ted Kremenek29e0ef22009-11-03 06:59:59 +0000146NSErrorChecker::CheckSignature(const FunctionDecl& F, QualType& ResultTy,
Ted Kremenekcf118d42009-02-04 23:49:09 +0000147 llvm::SmallVectorImpl<VarDecl*>& ErrorParams) {
Mike Stump1eb44332009-09-09 15:08:12 +0000148
Ted Kremenekcc9ac412008-10-01 23:24:09 +0000149 ResultTy = F.getResultType();
Mike Stump1eb44332009-09-09 15:08:12 +0000150
151 for (FunctionDecl::param_const_iterator I = F.param_begin(),
Zhongxing Xu5ab128b2009-08-21 02:18:44 +0000152 E = F.param_end(); I != E; ++I) {
Mike Stump1eb44332009-09-09 15:08:12 +0000153
154 QualType T = (*I)->getType();
155
Ted Kremenekcf118d42009-02-04 23:49:09 +0000156 if (isNSErrorWarning) {
157 if (CheckNSErrorArgument(T)) ErrorParams.push_back(*I);
158 }
159 else if (CheckCFErrorArgument(T))
160 ErrorParams.push_back(*I);
Ted Kremenekcc9ac412008-10-01 23:24:09 +0000161 }
162}
163
164
Ted Kremenek29e0ef22009-11-03 06:59:59 +0000165bool NSErrorChecker::CheckNSErrorArgument(QualType ArgTy) {
Mike Stump1eb44332009-09-09 15:08:12 +0000166
Ted Kremenek6217b802009-07-29 21:53:49 +0000167 const PointerType* PPT = ArgTy->getAs<PointerType>();
Steve Naroff14108da2009-07-10 23:34:53 +0000168 if (!PPT)
169 return false;
Mike Stump1eb44332009-09-09 15:08:12 +0000170
Steve Naroff14108da2009-07-10 23:34:53 +0000171 const ObjCObjectPointerType* PT =
John McCall183700f2009-09-21 23:43:11 +0000172 PPT->getPointeeType()->getAs<ObjCObjectPointerType>();
Steve Naroff14108da2009-07-10 23:34:53 +0000173
174 if (!PT)
175 return false;
Mike Stump1eb44332009-09-09 15:08:12 +0000176
Steve Naroff14108da2009-07-10 23:34:53 +0000177 const ObjCInterfaceDecl *ID = PT->getInterfaceDecl();
Mike Stump1eb44332009-09-09 15:08:12 +0000178
Steve Naroff14108da2009-07-10 23:34:53 +0000179 // FIXME: Can ID ever be NULL?
180 if (ID)
181 return II == ID->getIdentifier();
Mike Stump1eb44332009-09-09 15:08:12 +0000182
Steve Naroff14108da2009-07-10 23:34:53 +0000183 return false;
Ted Kremenekcfdf9b42008-09-18 21:25:13 +0000184}
Ted Kremenek7360fda2008-09-18 23:09:54 +0000185
Ted Kremenek29e0ef22009-11-03 06:59:59 +0000186bool NSErrorChecker::CheckCFErrorArgument(QualType ArgTy) {
Mike Stump1eb44332009-09-09 15:08:12 +0000187
Ted Kremenek6217b802009-07-29 21:53:49 +0000188 const PointerType* PPT = ArgTy->getAs<PointerType>();
Ted Kremenekcc9ac412008-10-01 23:24:09 +0000189 if (!PPT) return false;
Mike Stump1eb44332009-09-09 15:08:12 +0000190
John McCall183700f2009-09-21 23:43:11 +0000191 const TypedefType* TT = PPT->getPointeeType()->getAs<TypedefType>();
Ted Kremenekcc9ac412008-10-01 23:24:09 +0000192 if (!TT) return false;
193
Ted Kremenekcf118d42009-02-04 23:49:09 +0000194 return TT->getDecl()->getIdentifier() == II;
Ted Kremenekcc9ac412008-10-01 23:24:09 +0000195}
196
Ted Kremenek29e0ef22009-11-03 06:59:59 +0000197void NSErrorChecker::CheckParamDeref(const VarDecl *Param,
Ted Kremenekd17da2b2009-08-21 22:28:32 +0000198 const LocationContext *LC,
199 const GRState *rootState,
Ted Kremenekcf118d42009-02-04 23:49:09 +0000200 BugReporter& BR) {
Mike Stump1eb44332009-09-09 15:08:12 +0000201
Ted Kremenekd17da2b2009-08-21 22:28:32 +0000202 SVal ParamL = rootState->getLValue(Param, LC);
Ted Kremenek993f1c72008-10-17 20:28:54 +0000203 const MemRegion* ParamR = cast<loc::MemRegionVal>(ParamL).getRegionAs<VarRegion>();
204 assert (ParamR && "Parameters always have VarRegions.");
Ted Kremenekc8781382009-06-17 22:28:13 +0000205 SVal ParamSVal = rootState->getSVal(ParamR);
Mike Stump1eb44332009-09-09 15:08:12 +0000206
Zhongxing Xu1c96b242008-10-17 05:57:07 +0000207 // FIXME: For now assume that ParamSVal is symbolic. We need to generalize
Ted Kremenek7360fda2008-09-18 23:09:54 +0000208 // this later.
Ted Kremenek93e71452009-03-30 19:53:37 +0000209 SymbolRef ParamSym = ParamSVal.getAsLocSymbol();
210 if (!ParamSym)
211 return;
Mike Stump1eb44332009-09-09 15:08:12 +0000212
Ted Kremenek7360fda2008-09-18 23:09:54 +0000213 // Iterate over the implicit-null dereferences.
Ted Kremenekb4b817d2009-11-11 03:26:34 +0000214 ExplodedNode *const* I, *const* E;
215 llvm::tie(I, E) = GetImplicitNullDereferences(Eng);
216 for ( ; I != E; ++I) {
Ted Kremenekc8781382009-06-17 22:28:13 +0000217 const GRState *state = (*I)->getState();
Ted Kremenekb4b817d2009-11-11 03:26:34 +0000218 SVal location = state->getSVal((*I)->getLocationAs<StmtPoint>()->getStmt());
219 if (location.getAsSymbol() != ParamSym)
Ted Kremenek93e71452009-03-30 19:53:37 +0000220 continue;
Ted Kremenek7360fda2008-09-18 23:09:54 +0000221
222 // Emit an error.
Ted Kremenekcf118d42009-02-04 23:49:09 +0000223 std::string sbuf;
224 llvm::raw_string_ostream os(sbuf);
Ted Kremenekcc9ac412008-10-01 23:24:09 +0000225 os << "Potential null dereference. According to coding standards ";
Mike Stump1eb44332009-09-09 15:08:12 +0000226
Ted Kremenekcc9ac412008-10-01 23:24:09 +0000227 if (isNSErrorWarning)
228 os << "in 'Creating and Returning NSError Objects' the parameter '";
229 else
230 os << "documented in CoreFoundation/CFError.h the parameter '";
Mike Stump1eb44332009-09-09 15:08:12 +0000231
Chris Lattnerd9d22dd2008-11-24 05:29:24 +0000232 os << Param->getNameAsString() << "' may be null.";
Mike Stump1eb44332009-09-09 15:08:12 +0000233
Ted Kremenekcf118d42009-02-04 23:49:09 +0000234 BugReport *report = new BugReport(*this, os.str().c_str(), *I);
235 // FIXME: Notable symbols are now part of the report. We should
236 // add support for notable symbols in BugReport.
237 // BR.addNotableSymbol(SV->getSymbol());
238 BR.EmitReport(report);
Ted Kremenek7360fda2008-09-18 23:09:54 +0000239 }
240}