blob: 93b617b115d92925ba6b4de1ffaf3665b53d8c1a [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 Kremenekcc9ac412008-10-01 23:24:09 +0000117 BR.EmitBasicReport(isNSErrorWarning
118 ? "Bad return type when passing NSError**"
119 : "Bad return type when passing CFError*",
Ted Kremenekcf118d42009-02-04 23:49:09 +0000120 getCategory().c_str(), os.str().c_str(),
121 CodeDecl.getLocation());
Ted Kremenekcc9ac412008-10-01 23:24:09 +0000122}
123
124void
Ted Kremenek29e0ef22009-11-03 06:59:59 +0000125NSErrorChecker::CheckSignature(const ObjCMethodDecl& M, QualType& ResultTy,
Ted Kremenekcf118d42009-02-04 23:49:09 +0000126 llvm::SmallVectorImpl<VarDecl*>& ErrorParams) {
Ted Kremenekcfdf9b42008-09-18 21:25:13 +0000127
128 ResultTy = M.getResultType();
Mike Stump1eb44332009-09-09 15:08:12 +0000129
130 for (ObjCMethodDecl::param_iterator I=M.param_begin(),
Ted Kremenekcc9ac412008-10-01 23:24:09 +0000131 E=M.param_end(); I!=E; ++I) {
132
Mike Stump1eb44332009-09-09 15:08:12 +0000133 QualType T = (*I)->getType();
Ted Kremenekcc9ac412008-10-01 23:24:09 +0000134
Ted Kremenekcf118d42009-02-04 23:49:09 +0000135 if (isNSErrorWarning) {
136 if (CheckNSErrorArgument(T)) ErrorParams.push_back(*I);
137 }
138 else if (CheckCFErrorArgument(T))
139 ErrorParams.push_back(*I);
Ted Kremenekcc9ac412008-10-01 23:24:09 +0000140 }
Ted Kremenekcfdf9b42008-09-18 21:25:13 +0000141}
142
Ted Kremenekcc9ac412008-10-01 23:24:09 +0000143void
Ted Kremenek29e0ef22009-11-03 06:59:59 +0000144NSErrorChecker::CheckSignature(const FunctionDecl& F, QualType& ResultTy,
Ted Kremenekcf118d42009-02-04 23:49:09 +0000145 llvm::SmallVectorImpl<VarDecl*>& ErrorParams) {
Mike Stump1eb44332009-09-09 15:08:12 +0000146
Ted Kremenekcc9ac412008-10-01 23:24:09 +0000147 ResultTy = F.getResultType();
Mike Stump1eb44332009-09-09 15:08:12 +0000148
149 for (FunctionDecl::param_const_iterator I = F.param_begin(),
Zhongxing Xu5ab128b2009-08-21 02:18:44 +0000150 E = F.param_end(); I != E; ++I) {
Mike Stump1eb44332009-09-09 15:08:12 +0000151
152 QualType T = (*I)->getType();
153
Ted Kremenekcf118d42009-02-04 23:49:09 +0000154 if (isNSErrorWarning) {
155 if (CheckNSErrorArgument(T)) ErrorParams.push_back(*I);
156 }
157 else if (CheckCFErrorArgument(T))
158 ErrorParams.push_back(*I);
Ted Kremenekcc9ac412008-10-01 23:24:09 +0000159 }
160}
161
162
Ted Kremenek29e0ef22009-11-03 06:59:59 +0000163bool NSErrorChecker::CheckNSErrorArgument(QualType ArgTy) {
Mike Stump1eb44332009-09-09 15:08:12 +0000164
Ted Kremenek6217b802009-07-29 21:53:49 +0000165 const PointerType* PPT = ArgTy->getAs<PointerType>();
Steve Naroff14108da2009-07-10 23:34:53 +0000166 if (!PPT)
167 return false;
Mike Stump1eb44332009-09-09 15:08:12 +0000168
Steve Naroff14108da2009-07-10 23:34:53 +0000169 const ObjCObjectPointerType* PT =
John McCall183700f2009-09-21 23:43:11 +0000170 PPT->getPointeeType()->getAs<ObjCObjectPointerType>();
Steve Naroff14108da2009-07-10 23:34:53 +0000171
172 if (!PT)
173 return false;
Mike Stump1eb44332009-09-09 15:08:12 +0000174
Steve Naroff14108da2009-07-10 23:34:53 +0000175 const ObjCInterfaceDecl *ID = PT->getInterfaceDecl();
Mike Stump1eb44332009-09-09 15:08:12 +0000176
Steve Naroff14108da2009-07-10 23:34:53 +0000177 // FIXME: Can ID ever be NULL?
178 if (ID)
179 return II == ID->getIdentifier();
Mike Stump1eb44332009-09-09 15:08:12 +0000180
Steve Naroff14108da2009-07-10 23:34:53 +0000181 return false;
Ted Kremenekcfdf9b42008-09-18 21:25:13 +0000182}
Ted Kremenek7360fda2008-09-18 23:09:54 +0000183
Ted Kremenek29e0ef22009-11-03 06:59:59 +0000184bool NSErrorChecker::CheckCFErrorArgument(QualType ArgTy) {
Mike Stump1eb44332009-09-09 15:08:12 +0000185
Ted Kremenek6217b802009-07-29 21:53:49 +0000186 const PointerType* PPT = ArgTy->getAs<PointerType>();
Ted Kremenekcc9ac412008-10-01 23:24:09 +0000187 if (!PPT) return false;
Mike Stump1eb44332009-09-09 15:08:12 +0000188
John McCall183700f2009-09-21 23:43:11 +0000189 const TypedefType* TT = PPT->getPointeeType()->getAs<TypedefType>();
Ted Kremenekcc9ac412008-10-01 23:24:09 +0000190 if (!TT) return false;
191
Ted Kremenekcf118d42009-02-04 23:49:09 +0000192 return TT->getDecl()->getIdentifier() == II;
Ted Kremenekcc9ac412008-10-01 23:24:09 +0000193}
194
Ted Kremenek29e0ef22009-11-03 06:59:59 +0000195void NSErrorChecker::CheckParamDeref(const VarDecl *Param,
Ted Kremenekd17da2b2009-08-21 22:28:32 +0000196 const LocationContext *LC,
197 const GRState *rootState,
Ted Kremenekcf118d42009-02-04 23:49:09 +0000198 BugReporter& BR) {
Mike Stump1eb44332009-09-09 15:08:12 +0000199
Ted Kremenekd17da2b2009-08-21 22:28:32 +0000200 SVal ParamL = rootState->getLValue(Param, LC);
Ted Kremenek993f1c72008-10-17 20:28:54 +0000201 const MemRegion* ParamR = cast<loc::MemRegionVal>(ParamL).getRegionAs<VarRegion>();
202 assert (ParamR && "Parameters always have VarRegions.");
Ted Kremenekc8781382009-06-17 22:28:13 +0000203 SVal ParamSVal = rootState->getSVal(ParamR);
Mike Stump1eb44332009-09-09 15:08:12 +0000204
Zhongxing Xu1c96b242008-10-17 05:57:07 +0000205 // FIXME: For now assume that ParamSVal is symbolic. We need to generalize
Ted Kremenek7360fda2008-09-18 23:09:54 +0000206 // this later.
Ted Kremenek93e71452009-03-30 19:53:37 +0000207 SymbolRef ParamSym = ParamSVal.getAsLocSymbol();
208 if (!ParamSym)
209 return;
Mike Stump1eb44332009-09-09 15:08:12 +0000210
Ted Kremenek7360fda2008-09-18 23:09:54 +0000211 // Iterate over the implicit-null dereferences.
Ted Kremenekb4b817d2009-11-11 03:26:34 +0000212 ExplodedNode *const* I, *const* E;
213 llvm::tie(I, E) = GetImplicitNullDereferences(Eng);
214 for ( ; I != E; ++I) {
Ted Kremenekc8781382009-06-17 22:28:13 +0000215 const GRState *state = (*I)->getState();
Ted Kremenekb4b817d2009-11-11 03:26:34 +0000216 SVal location = state->getSVal((*I)->getLocationAs<StmtPoint>()->getStmt());
217 if (location.getAsSymbol() != ParamSym)
Ted Kremenek93e71452009-03-30 19:53:37 +0000218 continue;
Ted Kremenek7360fda2008-09-18 23:09:54 +0000219
220 // Emit an error.
Ted Kremenekcf118d42009-02-04 23:49:09 +0000221 std::string sbuf;
222 llvm::raw_string_ostream os(sbuf);
Ted Kremenekcc9ac412008-10-01 23:24:09 +0000223 os << "Potential null dereference. According to coding standards ";
Mike Stump1eb44332009-09-09 15:08:12 +0000224
Ted Kremenekcc9ac412008-10-01 23:24:09 +0000225 if (isNSErrorWarning)
226 os << "in 'Creating and Returning NSError Objects' the parameter '";
227 else
228 os << "documented in CoreFoundation/CFError.h the parameter '";
Mike Stump1eb44332009-09-09 15:08:12 +0000229
Chris Lattnerd9d22dd2008-11-24 05:29:24 +0000230 os << Param->getNameAsString() << "' may be null.";
Mike Stump1eb44332009-09-09 15:08:12 +0000231
Ted Kremenekcf118d42009-02-04 23:49:09 +0000232 BugReport *report = new BugReport(*this, os.str().c_str(), *I);
233 // FIXME: Notable symbols are now part of the report. We should
234 // add support for notable symbols in BugReport.
235 // BR.addNotableSymbol(SV->getSymbol());
236 BR.EmitReport(report);
Ted Kremenek7360fda2008-09-18 23:09:54 +0000237 }
238}