blob: 3cfb044d6f89304f6bd510fd19a6b728167d785d [file] [log] [blame]
Ted Kremenekdf8016a2009-11-03 06:59:59 +00001//=- NSErrorCheckerer.cpp - Coding conventions for uses of NSError -*- C++ -*-==//
Ted Kremenekfc3abeb2008-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
Argyrios Kyrtzidis8d602a82010-12-22 18:51:49 +000018#include "clang/GR/Checkers/LocalCheckers.h"
19#include "clang/GR/BugReporter/BugType.h"
Argyrios Kyrtzidis1696f502010-12-22 18:53:44 +000020#include "clang/GR/PathSensitive/ExprEngine.h"
Argyrios Kyrtzidis8d602a82010-12-22 18:51:49 +000021#include "clang/GR/Checkers/DereferenceChecker.h"
Ted Kremenekf0673e42008-09-18 21:25:13 +000022#include "BasicObjCFoundationChecks.h"
Ted Kremenekfc3abeb2008-09-18 06:33:41 +000023#include "clang/AST/DeclObjC.h"
Ted Kremenekf0673e42008-09-18 21:25:13 +000024#include "clang/AST/Decl.h"
25#include "llvm/ADT/SmallVector.h"
Ted Kremenekfc3abeb2008-09-18 06:33:41 +000026
27using namespace clang;
Argyrios Kyrtzidisca08fba2010-12-22 18:53:20 +000028using namespace GR;
Ted Kremenekfc3abeb2008-09-18 06:33:41 +000029
Ted Kremenekf0673e42008-09-18 21:25:13 +000030namespace {
Kovarththanan Rajaratnam65c65662009-11-28 06:07:30 +000031class NSErrorChecker : public BugType {
Zhongxing Xu6be1a4e2009-08-21 02:18:44 +000032 const Decl &CodeDecl;
Ted Kremenekfc5d0672009-02-04 23:49:09 +000033 const bool isNSErrorWarning;
34 IdentifierInfo * const II;
Argyrios Kyrtzidis1696f502010-12-22 18:53:44 +000035 ExprEngine &Eng;
Mike Stump11289f42009-09-09 15:08:12 +000036
Zhongxing Xu6be1a4e2009-08-21 02:18:44 +000037 void CheckSignature(const ObjCMethodDecl& MD, QualType& ResultTy,
Ted Kremenekfc5d0672009-02-04 23:49:09 +000038 llvm::SmallVectorImpl<VarDecl*>& ErrorParams);
Mike Stump11289f42009-09-09 15:08:12 +000039
Zhongxing Xu6be1a4e2009-08-21 02:18:44 +000040 void CheckSignature(const FunctionDecl& MD, QualType& ResultTy,
Ted Kremenekfc5d0672009-02-04 23:49:09 +000041 llvm::SmallVectorImpl<VarDecl*>& ErrorParams);
Ted Kremenekf0673e42008-09-18 21:25:13 +000042
Ted Kremenekfc5d0672009-02-04 23:49:09 +000043 bool CheckNSErrorArgument(QualType ArgTy);
44 bool CheckCFErrorArgument(QualType ArgTy);
Mike Stump11289f42009-09-09 15:08:12 +000045
Ted Kremenek14536f62009-08-21 22:28:32 +000046 void CheckParamDeref(const VarDecl *V, const LocationContext *LC,
47 const GRState *state, BugReporter& BR);
Mike Stump11289f42009-09-09 15:08:12 +000048
Zhongxing Xu6be1a4e2009-08-21 02:18:44 +000049 void EmitRetTyWarning(BugReporter& BR, const Decl& CodeDecl);
Mike Stump11289f42009-09-09 15:08:12 +000050
Ted Kremenekf0673e42008-09-18 21:25:13 +000051public:
Argyrios Kyrtzidis1696f502010-12-22 18:53:44 +000052 NSErrorChecker(const Decl &D, bool isNSError, ExprEngine& eng)
Mike Stump11289f42009-09-09 15:08:12 +000053 : BugType(isNSError ? "NSError** null dereference"
Zhongxing Xu6be1a4e2009-08-21 02:18:44 +000054 : "CFErrorRef* null dereference",
Ted Kremenek3aff9202009-09-01 00:17:12 +000055 "Coding conventions (Apple)"),
Zhongxing Xu6be1a4e2009-08-21 02:18:44 +000056 CodeDecl(D),
Mike Stump11289f42009-09-09 15:08:12 +000057 isNSErrorWarning(isNSError),
Ted Kremenekfc5d0672009-02-04 23:49:09 +000058 II(&eng.getContext().Idents.get(isNSErrorWarning ? "NSError":"CFErrorRef")),
59 Eng(eng) {}
Mike Stump11289f42009-09-09 15:08:12 +000060
Ted Kremenekfc5d0672009-02-04 23:49:09 +000061 void FlushReports(BugReporter& BR);
Mike Stump11289f42009-09-09 15:08:12 +000062};
63
Ted Kremenekf0673e42008-09-18 21:25:13 +000064} // end anonymous namespace
65
Argyrios Kyrtzidis1696f502010-12-22 18:53:44 +000066void GR::RegisterNSErrorChecks(BugReporter& BR, ExprEngine &Eng,
Zhongxing Xu6be1a4e2009-08-21 02:18:44 +000067 const Decl &D) {
Ted Kremenekdf8016a2009-11-03 06:59:59 +000068 BR.Register(new NSErrorChecker(D, true, Eng));
69 BR.Register(new NSErrorChecker(D, false, Eng));
Ted Kremenekf0673e42008-09-18 21:25:13 +000070}
71
Ted Kremenekdf8016a2009-11-03 06:59:59 +000072void NSErrorChecker::FlushReports(BugReporter& BR) {
Ted Kremenekf0673e42008-09-18 21:25:13 +000073 // Get the analysis engine and the exploded analysis graph.
Zhongxing Xu107f7592009-08-06 12:48:26 +000074 ExplodedGraph& G = Eng.getGraph();
Mike Stump11289f42009-09-09 15:08:12 +000075
Ted Kremenekf0673e42008-09-18 21:25:13 +000076 // Get the ASTContext, which is useful for querying type information.
Ted Kremenekfc3abeb2008-09-18 06:33:41 +000077 ASTContext &Ctx = BR.getContext();
Ted Kremenekf0673e42008-09-18 21:25:13 +000078
79 QualType ResultTy;
Ted Kremenekfc5d0672009-02-04 23:49:09 +000080 llvm::SmallVector<VarDecl*, 5> ErrorParams;
Ted Kremenek3aa89a92008-10-01 23:24:09 +000081
Zhongxing Xu6be1a4e2009-08-21 02:18:44 +000082 if (const ObjCMethodDecl* MD = dyn_cast<ObjCMethodDecl>(&CodeDecl))
Ted Kremenekfc5d0672009-02-04 23:49:09 +000083 CheckSignature(*MD, ResultTy, ErrorParams);
Zhongxing Xu6be1a4e2009-08-21 02:18:44 +000084 else if (const FunctionDecl* FD = dyn_cast<FunctionDecl>(&CodeDecl))
Ted Kremenekfc5d0672009-02-04 23:49:09 +000085 CheckSignature(*FD, ResultTy, ErrorParams);
Ted Kremenek3aa89a92008-10-01 23:24:09 +000086 else
Ted Kremenekf0673e42008-09-18 21:25:13 +000087 return;
Mike Stump11289f42009-09-09 15:08:12 +000088
Ted Kremenekfc5d0672009-02-04 23:49:09 +000089 if (ErrorParams.empty())
Ted Kremenek3aa89a92008-10-01 23:24:09 +000090 return;
Mike Stump11289f42009-09-09 15:08:12 +000091
Ted Kremenekfc5d0672009-02-04 23:49:09 +000092 if (ResultTy == Ctx.VoidTy) EmitRetTyWarning(BR, CodeDecl);
Mike Stump11289f42009-09-09 15:08:12 +000093
94 for (ExplodedGraph::roots_iterator RI=G.roots_begin(), RE=G.roots_end();
Zhongxing Xu107f7592009-08-06 12:48:26 +000095 RI!=RE; ++RI) {
Ted Kremenekfc5d0672009-02-04 23:49:09 +000096 // Scan the parameters for an implicit null dereference.
97 for (llvm::SmallVectorImpl<VarDecl*>::iterator I=ErrorParams.begin(),
Mike Stump11289f42009-09-09 15:08:12 +000098 E=ErrorParams.end(); I!=E; ++I)
Ted Kremenek14536f62009-08-21 22:28:32 +000099 CheckParamDeref(*I, (*RI)->getLocationContext(), (*RI)->getState(), BR);
Ted Kremenek3aa89a92008-10-01 23:24:09 +0000100 }
Ted Kremenekfc3abeb2008-09-18 06:33:41 +0000101}
Ted Kremenekf0673e42008-09-18 21:25:13 +0000102
Ted Kremenekdf8016a2009-11-03 06:59:59 +0000103void NSErrorChecker::EmitRetTyWarning(BugReporter& BR, const Decl& CodeDecl) {
Ted Kremenekfc5d0672009-02-04 23:49:09 +0000104 std::string sbuf;
105 llvm::raw_string_ostream os(sbuf);
Mike Stump11289f42009-09-09 15:08:12 +0000106
Ted Kremenek3aa89a92008-10-01 23:24:09 +0000107 if (isa<ObjCMethodDecl>(CodeDecl))
108 os << "Method";
109 else
Mike Stump11289f42009-09-09 15:08:12 +0000110 os << "Function";
111
Ted Kremenek3aa89a92008-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 Kremenek54bd6372009-08-06 05:01:36 +0000115 "error occurred";
Mike Stump11289f42009-09-09 15:08:12 +0000116
Ted Kremenek3aa89a92008-10-01 23:24:09 +0000117 BR.EmitBasicReport(isNSErrorWarning
118 ? "Bad return type when passing NSError**"
119 : "Bad return type when passing CFError*",
Benjamin Kramer63415532009-11-29 18:27:55 +0000120 getCategory(), os.str(),
Ted Kremenekfc5d0672009-02-04 23:49:09 +0000121 CodeDecl.getLocation());
Ted Kremenek3aa89a92008-10-01 23:24:09 +0000122}
123
124void
Ted Kremenekdf8016a2009-11-03 06:59:59 +0000125NSErrorChecker::CheckSignature(const ObjCMethodDecl& M, QualType& ResultTy,
Ted Kremenekfc5d0672009-02-04 23:49:09 +0000126 llvm::SmallVectorImpl<VarDecl*>& ErrorParams) {
Ted Kremenekf0673e42008-09-18 21:25:13 +0000127
128 ResultTy = M.getResultType();
Mike Stump11289f42009-09-09 15:08:12 +0000129
130 for (ObjCMethodDecl::param_iterator I=M.param_begin(),
Ted Kremenek3aa89a92008-10-01 23:24:09 +0000131 E=M.param_end(); I!=E; ++I) {
132
Mike Stump11289f42009-09-09 15:08:12 +0000133 QualType T = (*I)->getType();
Ted Kremenek3aa89a92008-10-01 23:24:09 +0000134
Ted Kremenekfc5d0672009-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 Kremenek3aa89a92008-10-01 23:24:09 +0000140 }
Ted Kremenekf0673e42008-09-18 21:25:13 +0000141}
142
Ted Kremenek3aa89a92008-10-01 23:24:09 +0000143void
Ted Kremenekdf8016a2009-11-03 06:59:59 +0000144NSErrorChecker::CheckSignature(const FunctionDecl& F, QualType& ResultTy,
Ted Kremenekfc5d0672009-02-04 23:49:09 +0000145 llvm::SmallVectorImpl<VarDecl*>& ErrorParams) {
Mike Stump11289f42009-09-09 15:08:12 +0000146
Ted Kremenek3aa89a92008-10-01 23:24:09 +0000147 ResultTy = F.getResultType();
Mike Stump11289f42009-09-09 15:08:12 +0000148
149 for (FunctionDecl::param_const_iterator I = F.param_begin(),
Zhongxing Xu6be1a4e2009-08-21 02:18:44 +0000150 E = F.param_end(); I != E; ++I) {
Mike Stump11289f42009-09-09 15:08:12 +0000151
152 QualType T = (*I)->getType();
153
Ted Kremenekfc5d0672009-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 Kremenek3aa89a92008-10-01 23:24:09 +0000159 }
160}
161
162
Ted Kremenekdf8016a2009-11-03 06:59:59 +0000163bool NSErrorChecker::CheckNSErrorArgument(QualType ArgTy) {
Mike Stump11289f42009-09-09 15:08:12 +0000164
Ted Kremenekc23c7e62009-07-29 21:53:49 +0000165 const PointerType* PPT = ArgTy->getAs<PointerType>();
Steve Naroff7cae42b2009-07-10 23:34:53 +0000166 if (!PPT)
167 return false;
Mike Stump11289f42009-09-09 15:08:12 +0000168
Steve Naroff7cae42b2009-07-10 23:34:53 +0000169 const ObjCObjectPointerType* PT =
John McCall9dd450b2009-09-21 23:43:11 +0000170 PPT->getPointeeType()->getAs<ObjCObjectPointerType>();
Steve Naroff7cae42b2009-07-10 23:34:53 +0000171
172 if (!PT)
173 return false;
Mike Stump11289f42009-09-09 15:08:12 +0000174
Steve Naroff7cae42b2009-07-10 23:34:53 +0000175 const ObjCInterfaceDecl *ID = PT->getInterfaceDecl();
Mike Stump11289f42009-09-09 15:08:12 +0000176
Steve Naroff7cae42b2009-07-10 23:34:53 +0000177 // FIXME: Can ID ever be NULL?
178 if (ID)
179 return II == ID->getIdentifier();
Mike Stump11289f42009-09-09 15:08:12 +0000180
Steve Naroff7cae42b2009-07-10 23:34:53 +0000181 return false;
Ted Kremenekf0673e42008-09-18 21:25:13 +0000182}
Ted Kremenekb42f4822008-09-18 23:09:54 +0000183
Ted Kremenekdf8016a2009-11-03 06:59:59 +0000184bool NSErrorChecker::CheckCFErrorArgument(QualType ArgTy) {
Mike Stump11289f42009-09-09 15:08:12 +0000185
Ted Kremenekc23c7e62009-07-29 21:53:49 +0000186 const PointerType* PPT = ArgTy->getAs<PointerType>();
Ted Kremenek3aa89a92008-10-01 23:24:09 +0000187 if (!PPT) return false;
Mike Stump11289f42009-09-09 15:08:12 +0000188
John McCall9dd450b2009-09-21 23:43:11 +0000189 const TypedefType* TT = PPT->getPointeeType()->getAs<TypedefType>();
Ted Kremenek3aa89a92008-10-01 23:24:09 +0000190 if (!TT) return false;
191
Ted Kremenekfc5d0672009-02-04 23:49:09 +0000192 return TT->getDecl()->getIdentifier() == II;
Ted Kremenek3aa89a92008-10-01 23:24:09 +0000193}
194
Ted Kremenekdf8016a2009-11-03 06:59:59 +0000195void NSErrorChecker::CheckParamDeref(const VarDecl *Param,
Ted Kremenek14536f62009-08-21 22:28:32 +0000196 const LocationContext *LC,
197 const GRState *rootState,
Ted Kremenekfc5d0672009-02-04 23:49:09 +0000198 BugReporter& BR) {
Mike Stump11289f42009-09-09 15:08:12 +0000199
Ted Kremenek14536f62009-08-21 22:28:32 +0000200 SVal ParamL = rootState->getLValue(Param, LC);
Ted Kremenek8b103c62008-10-17 20:28:54 +0000201 const MemRegion* ParamR = cast<loc::MemRegionVal>(ParamL).getRegionAs<VarRegion>();
202 assert (ParamR && "Parameters always have VarRegions.");
Ted Kremenek57f09892010-02-08 16:18:51 +0000203 SVal ParamSVal = rootState->getSVal(ParamR);
Mike Stump11289f42009-09-09 15:08:12 +0000204
Zhongxing Xu27f17422008-10-17 05:57:07 +0000205 // FIXME: For now assume that ParamSVal is symbolic. We need to generalize
Ted Kremenekb42f4822008-09-18 23:09:54 +0000206 // this later.
Ted Kremenekac508982009-03-30 19:53:37 +0000207 SymbolRef ParamSym = ParamSVal.getAsLocSymbol();
208 if (!ParamSym)
209 return;
Mike Stump11289f42009-09-09 15:08:12 +0000210
Ted Kremenekb42f4822008-09-18 23:09:54 +0000211 // Iterate over the implicit-null dereferences.
Ted Kremenek5e1f78a2009-11-11 03:26:34 +0000212 ExplodedNode *const* I, *const* E;
213 llvm::tie(I, E) = GetImplicitNullDereferences(Eng);
214 for ( ; I != E; ++I) {
Ted Kremenekb35e2ca2009-06-17 22:28:13 +0000215 const GRState *state = (*I)->getState();
Ted Kremenek57f09892010-02-08 16:18:51 +0000216 SVal location = state->getSVal((*I)->getLocationAs<StmtPoint>()->getStmt());
Ted Kremenek5e1f78a2009-11-11 03:26:34 +0000217 if (location.getAsSymbol() != ParamSym)
Ted Kremenekac508982009-03-30 19:53:37 +0000218 continue;
Ted Kremenekb42f4822008-09-18 23:09:54 +0000219
220 // Emit an error.
Ted Kremenekfc5d0672009-02-04 23:49:09 +0000221 std::string sbuf;
222 llvm::raw_string_ostream os(sbuf);
Ted Kremenek3aa89a92008-10-01 23:24:09 +0000223 os << "Potential null dereference. According to coding standards ";
Mike Stump11289f42009-09-09 15:08:12 +0000224
Ted Kremenek3aa89a92008-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 Stump11289f42009-09-09 15:08:12 +0000229
Benjamin Kramerb11416d2010-04-17 09:33:03 +0000230 os << Param << "' may be null.";
Mike Stump11289f42009-09-09 15:08:12 +0000231
Benjamin Kramerff3750f2009-11-29 18:03:28 +0000232 BugReport *report = new BugReport(*this, os.str(), *I);
Ted Kremenekfc5d0672009-02-04 23:49:09 +0000233 // 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 Kremenekb42f4822008-09-18 23:09:54 +0000237 }
238}