blob: 9a8d02215ebd89999e88514ea9dbb3e0a1eb1c23 [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"
23#include "llvm/Support/Compiler.h"
Ted Kremenekf45d18c2008-09-18 06:33:41 +000024#include "clang/AST/DeclObjC.h"
Ted Kremenekcfdf9b42008-09-18 21:25:13 +000025#include "clang/AST/Decl.h"
26#include "llvm/ADT/SmallVector.h"
Ted Kremenekf45d18c2008-09-18 06:33:41 +000027
28using namespace clang;
29
Ted Kremenekcfdf9b42008-09-18 21:25:13 +000030namespace {
Ted Kremenek29e0ef22009-11-03 06:59:59 +000031class VISIBILITY_HIDDEN NSErrorChecker : public BugType {
Zhongxing Xu5ab128b2009-08-21 02:18:44 +000032 const Decl &CodeDecl;
Ted Kremenekcf118d42009-02-04 23:49:09 +000033 const bool isNSErrorWarning;
34 IdentifierInfo * const II;
35 GRExprEngine &Eng;
Mike Stump1eb44332009-09-09 15:08:12 +000036
Zhongxing Xu5ab128b2009-08-21 02:18:44 +000037 void CheckSignature(const ObjCMethodDecl& MD, QualType& ResultTy,
Ted Kremenekcf118d42009-02-04 23:49:09 +000038 llvm::SmallVectorImpl<VarDecl*>& ErrorParams);
Mike Stump1eb44332009-09-09 15:08:12 +000039
Zhongxing Xu5ab128b2009-08-21 02:18:44 +000040 void CheckSignature(const FunctionDecl& MD, QualType& ResultTy,
Ted Kremenekcf118d42009-02-04 23:49:09 +000041 llvm::SmallVectorImpl<VarDecl*>& ErrorParams);
Ted Kremenekcfdf9b42008-09-18 21:25:13 +000042
Ted Kremenekcf118d42009-02-04 23:49:09 +000043 bool CheckNSErrorArgument(QualType ArgTy);
44 bool CheckCFErrorArgument(QualType ArgTy);
Mike Stump1eb44332009-09-09 15:08:12 +000045
Ted Kremenekd17da2b2009-08-21 22:28:32 +000046 void CheckParamDeref(const VarDecl *V, const LocationContext *LC,
47 const GRState *state, BugReporter& BR);
Mike Stump1eb44332009-09-09 15:08:12 +000048
Zhongxing Xu5ab128b2009-08-21 02:18:44 +000049 void EmitRetTyWarning(BugReporter& BR, const Decl& CodeDecl);
Mike Stump1eb44332009-09-09 15:08:12 +000050
Ted Kremenekcfdf9b42008-09-18 21:25:13 +000051public:
Ted Kremenek29e0ef22009-11-03 06:59:59 +000052 NSErrorChecker(const Decl &D, bool isNSError, GRExprEngine& eng)
Mike Stump1eb44332009-09-09 15:08:12 +000053 : BugType(isNSError ? "NSError** null dereference"
Zhongxing Xu5ab128b2009-08-21 02:18:44 +000054 : "CFErrorRef* null dereference",
Ted Kremenek27a36e92009-09-01 00:17:12 +000055 "Coding conventions (Apple)"),
Zhongxing Xu5ab128b2009-08-21 02:18:44 +000056 CodeDecl(D),
Mike Stump1eb44332009-09-09 15:08:12 +000057 isNSErrorWarning(isNSError),
Ted Kremenekcf118d42009-02-04 23:49:09 +000058 II(&eng.getContext().Idents.get(isNSErrorWarning ? "NSError":"CFErrorRef")),
59 Eng(eng) {}
Mike Stump1eb44332009-09-09 15:08:12 +000060
Ted Kremenekcf118d42009-02-04 23:49:09 +000061 void FlushReports(BugReporter& BR);
Mike Stump1eb44332009-09-09 15:08:12 +000062};
63
Ted Kremenekcfdf9b42008-09-18 21:25:13 +000064} // end anonymous namespace
65
Mike Stump1eb44332009-09-09 15:08:12 +000066void clang::RegisterNSErrorChecks(BugReporter& BR, GRExprEngine &Eng,
Zhongxing Xu5ab128b2009-08-21 02:18:44 +000067 const Decl &D) {
Ted Kremenek29e0ef22009-11-03 06:59:59 +000068 BR.Register(new NSErrorChecker(D, true, Eng));
69 BR.Register(new NSErrorChecker(D, false, Eng));
Ted Kremenekcfdf9b42008-09-18 21:25:13 +000070}
71
Ted Kremenek29e0ef22009-11-03 06:59:59 +000072void NSErrorChecker::FlushReports(BugReporter& BR) {
Ted Kremenekcfdf9b42008-09-18 21:25:13 +000073 // Get the analysis engine and the exploded analysis graph.
Zhongxing Xu031ccc02009-08-06 12:48:26 +000074 ExplodedGraph& G = Eng.getGraph();
Mike Stump1eb44332009-09-09 15:08:12 +000075
Ted Kremenekcfdf9b42008-09-18 21:25:13 +000076 // Get the ASTContext, which is useful for querying type information.
Ted Kremenekf45d18c2008-09-18 06:33:41 +000077 ASTContext &Ctx = BR.getContext();
Ted Kremenekcfdf9b42008-09-18 21:25:13 +000078
79 QualType ResultTy;
Ted Kremenekcf118d42009-02-04 23:49:09 +000080 llvm::SmallVector<VarDecl*, 5> ErrorParams;
Ted Kremenekcc9ac412008-10-01 23:24:09 +000081
Zhongxing Xu5ab128b2009-08-21 02:18:44 +000082 if (const ObjCMethodDecl* MD = dyn_cast<ObjCMethodDecl>(&CodeDecl))
Ted Kremenekcf118d42009-02-04 23:49:09 +000083 CheckSignature(*MD, ResultTy, ErrorParams);
Zhongxing Xu5ab128b2009-08-21 02:18:44 +000084 else if (const FunctionDecl* FD = dyn_cast<FunctionDecl>(&CodeDecl))
Ted Kremenekcf118d42009-02-04 23:49:09 +000085 CheckSignature(*FD, ResultTy, ErrorParams);
Ted Kremenekcc9ac412008-10-01 23:24:09 +000086 else
Ted Kremenekcfdf9b42008-09-18 21:25:13 +000087 return;
Mike Stump1eb44332009-09-09 15:08:12 +000088
Ted Kremenekcf118d42009-02-04 23:49:09 +000089 if (ErrorParams.empty())
Ted Kremenekcc9ac412008-10-01 23:24:09 +000090 return;
Mike Stump1eb44332009-09-09 15:08:12 +000091
Ted Kremenekcf118d42009-02-04 23:49:09 +000092 if (ResultTy == Ctx.VoidTy) EmitRetTyWarning(BR, CodeDecl);
Mike Stump1eb44332009-09-09 15:08:12 +000093
94 for (ExplodedGraph::roots_iterator RI=G.roots_begin(), RE=G.roots_end();
Zhongxing Xu031ccc02009-08-06 12:48:26 +000095 RI!=RE; ++RI) {
Ted Kremenekcf118d42009-02-04 23:49:09 +000096 // Scan the parameters for an implicit null dereference.
97 for (llvm::SmallVectorImpl<VarDecl*>::iterator I=ErrorParams.begin(),
Mike Stump1eb44332009-09-09 15:08:12 +000098 E=ErrorParams.end(); I!=E; ++I)
Ted Kremenekd17da2b2009-08-21 22:28:32 +000099 CheckParamDeref(*I, (*RI)->getLocationContext(), (*RI)->getState(), BR);
Ted Kremenekcc9ac412008-10-01 23:24:09 +0000100 }
Ted Kremenekf45d18c2008-09-18 06:33:41 +0000101}
Ted Kremenekcfdf9b42008-09-18 21:25:13 +0000102
Ted Kremenek29e0ef22009-11-03 06:59:59 +0000103void NSErrorChecker::EmitRetTyWarning(BugReporter& BR, const Decl& CodeDecl) {
Ted Kremenekcf118d42009-02-04 23:49:09 +0000104 std::string sbuf;
105 llvm::raw_string_ostream os(sbuf);
Mike Stump1eb44332009-09-09 15:08:12 +0000106
Ted Kremenekcc9ac412008-10-01 23:24:09 +0000107 if (isa<ObjCMethodDecl>(CodeDecl))
108 os << "Method";
109 else
Mike Stump1eb44332009-09-09 15:08:12 +0000110 os << "Function";
111
Ted Kremenekcc9ac412008-10-01 23:24:09 +0000112 os << " accepting ";
113 os << (isNSErrorWarning ? "NSError**" : "CFErrorRef*");
114 os << " should have a non-void return value to indicate whether or not an "
Ted Kremenek355a6922009-08-06 05:01:36 +0000115 "error occurred";
Mike Stump1eb44332009-09-09 15:08:12 +0000116
Ted Kremeneke576af22009-11-24 01:33:10 +0000117 // FIXME: Remove when we migrate EmitBasicReport to StringRef.
118 std::string cat = getCategory().str();
119
Ted Kremenekcc9ac412008-10-01 23:24:09 +0000120 BR.EmitBasicReport(isNSErrorWarning
121 ? "Bad return type when passing NSError**"
122 : "Bad return type when passing CFError*",
Ted Kremeneke576af22009-11-24 01:33:10 +0000123 cat.c_str(), os.str().c_str(),
Ted Kremenekcf118d42009-02-04 23:49:09 +0000124 CodeDecl.getLocation());
Ted Kremenekcc9ac412008-10-01 23:24:09 +0000125}
126
127void
Ted Kremenek29e0ef22009-11-03 06:59:59 +0000128NSErrorChecker::CheckSignature(const ObjCMethodDecl& M, QualType& ResultTy,
Ted Kremenekcf118d42009-02-04 23:49:09 +0000129 llvm::SmallVectorImpl<VarDecl*>& ErrorParams) {
Ted Kremenekcfdf9b42008-09-18 21:25:13 +0000130
131 ResultTy = M.getResultType();
Mike Stump1eb44332009-09-09 15:08:12 +0000132
133 for (ObjCMethodDecl::param_iterator I=M.param_begin(),
Ted Kremenekcc9ac412008-10-01 23:24:09 +0000134 E=M.param_end(); I!=E; ++I) {
135
Mike Stump1eb44332009-09-09 15:08:12 +0000136 QualType T = (*I)->getType();
Ted Kremenekcc9ac412008-10-01 23:24:09 +0000137
Ted Kremenekcf118d42009-02-04 23:49:09 +0000138 if (isNSErrorWarning) {
139 if (CheckNSErrorArgument(T)) ErrorParams.push_back(*I);
140 }
141 else if (CheckCFErrorArgument(T))
142 ErrorParams.push_back(*I);
Ted Kremenekcc9ac412008-10-01 23:24:09 +0000143 }
Ted Kremenekcfdf9b42008-09-18 21:25:13 +0000144}
145
Ted Kremenekcc9ac412008-10-01 23:24:09 +0000146void
Ted Kremenek29e0ef22009-11-03 06:59:59 +0000147NSErrorChecker::CheckSignature(const FunctionDecl& F, QualType& ResultTy,
Ted Kremenekcf118d42009-02-04 23:49:09 +0000148 llvm::SmallVectorImpl<VarDecl*>& ErrorParams) {
Mike Stump1eb44332009-09-09 15:08:12 +0000149
Ted Kremenekcc9ac412008-10-01 23:24:09 +0000150 ResultTy = F.getResultType();
Mike Stump1eb44332009-09-09 15:08:12 +0000151
152 for (FunctionDecl::param_const_iterator I = F.param_begin(),
Zhongxing Xu5ab128b2009-08-21 02:18:44 +0000153 E = F.param_end(); I != E; ++I) {
Mike Stump1eb44332009-09-09 15:08:12 +0000154
155 QualType T = (*I)->getType();
156
Ted Kremenekcf118d42009-02-04 23:49:09 +0000157 if (isNSErrorWarning) {
158 if (CheckNSErrorArgument(T)) ErrorParams.push_back(*I);
159 }
160 else if (CheckCFErrorArgument(T))
161 ErrorParams.push_back(*I);
Ted Kremenekcc9ac412008-10-01 23:24:09 +0000162 }
163}
164
165
Ted Kremenek29e0ef22009-11-03 06:59:59 +0000166bool NSErrorChecker::CheckNSErrorArgument(QualType ArgTy) {
Mike Stump1eb44332009-09-09 15:08:12 +0000167
Ted Kremenek6217b802009-07-29 21:53:49 +0000168 const PointerType* PPT = ArgTy->getAs<PointerType>();
Steve Naroff14108da2009-07-10 23:34:53 +0000169 if (!PPT)
170 return false;
Mike Stump1eb44332009-09-09 15:08:12 +0000171
Steve Naroff14108da2009-07-10 23:34:53 +0000172 const ObjCObjectPointerType* PT =
John McCall183700f2009-09-21 23:43:11 +0000173 PPT->getPointeeType()->getAs<ObjCObjectPointerType>();
Steve Naroff14108da2009-07-10 23:34:53 +0000174
175 if (!PT)
176 return false;
Mike Stump1eb44332009-09-09 15:08:12 +0000177
Steve Naroff14108da2009-07-10 23:34:53 +0000178 const ObjCInterfaceDecl *ID = PT->getInterfaceDecl();
Mike Stump1eb44332009-09-09 15:08:12 +0000179
Steve Naroff14108da2009-07-10 23:34:53 +0000180 // FIXME: Can ID ever be NULL?
181 if (ID)
182 return II == ID->getIdentifier();
Mike Stump1eb44332009-09-09 15:08:12 +0000183
Steve Naroff14108da2009-07-10 23:34:53 +0000184 return false;
Ted Kremenekcfdf9b42008-09-18 21:25:13 +0000185}
Ted Kremenek7360fda2008-09-18 23:09:54 +0000186
Ted Kremenek29e0ef22009-11-03 06:59:59 +0000187bool NSErrorChecker::CheckCFErrorArgument(QualType ArgTy) {
Mike Stump1eb44332009-09-09 15:08:12 +0000188
Ted Kremenek6217b802009-07-29 21:53:49 +0000189 const PointerType* PPT = ArgTy->getAs<PointerType>();
Ted Kremenekcc9ac412008-10-01 23:24:09 +0000190 if (!PPT) return false;
Mike Stump1eb44332009-09-09 15:08:12 +0000191
John McCall183700f2009-09-21 23:43:11 +0000192 const TypedefType* TT = PPT->getPointeeType()->getAs<TypedefType>();
Ted Kremenekcc9ac412008-10-01 23:24:09 +0000193 if (!TT) return false;
194
Ted Kremenekcf118d42009-02-04 23:49:09 +0000195 return TT->getDecl()->getIdentifier() == II;
Ted Kremenekcc9ac412008-10-01 23:24:09 +0000196}
197
Ted Kremenek29e0ef22009-11-03 06:59:59 +0000198void NSErrorChecker::CheckParamDeref(const VarDecl *Param,
Ted Kremenekd17da2b2009-08-21 22:28:32 +0000199 const LocationContext *LC,
200 const GRState *rootState,
Ted Kremenekcf118d42009-02-04 23:49:09 +0000201 BugReporter& BR) {
Mike Stump1eb44332009-09-09 15:08:12 +0000202
Ted Kremenekd17da2b2009-08-21 22:28:32 +0000203 SVal ParamL = rootState->getLValue(Param, LC);
Ted Kremenek993f1c72008-10-17 20:28:54 +0000204 const MemRegion* ParamR = cast<loc::MemRegionVal>(ParamL).getRegionAs<VarRegion>();
205 assert (ParamR && "Parameters always have VarRegions.");
Ted Kremenekc8781382009-06-17 22:28:13 +0000206 SVal ParamSVal = rootState->getSVal(ParamR);
Mike Stump1eb44332009-09-09 15:08:12 +0000207
Zhongxing Xu1c96b242008-10-17 05:57:07 +0000208 // FIXME: For now assume that ParamSVal is symbolic. We need to generalize
Ted Kremenek7360fda2008-09-18 23:09:54 +0000209 // this later.
Ted Kremenek93e71452009-03-30 19:53:37 +0000210 SymbolRef ParamSym = ParamSVal.getAsLocSymbol();
211 if (!ParamSym)
212 return;
Mike Stump1eb44332009-09-09 15:08:12 +0000213
Ted Kremenek7360fda2008-09-18 23:09:54 +0000214 // Iterate over the implicit-null dereferences.
Ted Kremenekb4b817d2009-11-11 03:26:34 +0000215 ExplodedNode *const* I, *const* E;
216 llvm::tie(I, E) = GetImplicitNullDereferences(Eng);
217 for ( ; I != E; ++I) {
Ted Kremenekc8781382009-06-17 22:28:13 +0000218 const GRState *state = (*I)->getState();
Ted Kremenekb4b817d2009-11-11 03:26:34 +0000219 SVal location = state->getSVal((*I)->getLocationAs<StmtPoint>()->getStmt());
220 if (location.getAsSymbol() != ParamSym)
Ted Kremenek93e71452009-03-30 19:53:37 +0000221 continue;
Ted Kremenek7360fda2008-09-18 23:09:54 +0000222
223 // Emit an error.
Ted Kremenekcf118d42009-02-04 23:49:09 +0000224 std::string sbuf;
225 llvm::raw_string_ostream os(sbuf);
Ted Kremenekcc9ac412008-10-01 23:24:09 +0000226 os << "Potential null dereference. According to coding standards ";
Mike Stump1eb44332009-09-09 15:08:12 +0000227
Ted Kremenekcc9ac412008-10-01 23:24:09 +0000228 if (isNSErrorWarning)
229 os << "in 'Creating and Returning NSError Objects' the parameter '";
230 else
231 os << "documented in CoreFoundation/CFError.h the parameter '";
Mike Stump1eb44332009-09-09 15:08:12 +0000232
Chris Lattnerd9d22dd2008-11-24 05:29:24 +0000233 os << Param->getNameAsString() << "' may be null.";
Mike Stump1eb44332009-09-09 15:08:12 +0000234
Ted Kremenekcf118d42009-02-04 23:49:09 +0000235 BugReport *report = new BugReport(*this, os.str().c_str(), *I);
236 // FIXME: Notable symbols are now part of the report. We should
237 // add support for notable symbols in BugReport.
238 // BR.addNotableSymbol(SV->getSymbol());
239 BR.EmitReport(report);
Ted Kremenek7360fda2008-09-18 23:09:54 +0000240 }
241}