blob: e3cf57fd0c1bdf65afa5965122e1494d3f055f92 [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 Kremenekcc9ac412008-10-01 23:24:09 +0000116 BR.EmitBasicReport(isNSErrorWarning
117 ? "Bad return type when passing NSError**"
118 : "Bad return type when passing CFError*",
Benjamin Kramerf0171732009-11-29 18:27:55 +0000119 getCategory(), os.str(),
Ted Kremenekcf118d42009-02-04 23:49:09 +0000120 CodeDecl.getLocation());
Ted Kremenekcc9ac412008-10-01 23:24:09 +0000121}
122
123void
Ted Kremenek29e0ef22009-11-03 06:59:59 +0000124NSErrorChecker::CheckSignature(const ObjCMethodDecl& M, QualType& ResultTy,
Ted Kremenekcf118d42009-02-04 23:49:09 +0000125 llvm::SmallVectorImpl<VarDecl*>& ErrorParams) {
Ted Kremenekcfdf9b42008-09-18 21:25:13 +0000126
127 ResultTy = M.getResultType();
Mike Stump1eb44332009-09-09 15:08:12 +0000128
129 for (ObjCMethodDecl::param_iterator I=M.param_begin(),
Ted Kremenekcc9ac412008-10-01 23:24:09 +0000130 E=M.param_end(); I!=E; ++I) {
131
Mike Stump1eb44332009-09-09 15:08:12 +0000132 QualType T = (*I)->getType();
Ted Kremenekcc9ac412008-10-01 23:24:09 +0000133
Ted Kremenekcf118d42009-02-04 23:49:09 +0000134 if (isNSErrorWarning) {
135 if (CheckNSErrorArgument(T)) ErrorParams.push_back(*I);
136 }
137 else if (CheckCFErrorArgument(T))
138 ErrorParams.push_back(*I);
Ted Kremenekcc9ac412008-10-01 23:24:09 +0000139 }
Ted Kremenekcfdf9b42008-09-18 21:25:13 +0000140}
141
Ted Kremenekcc9ac412008-10-01 23:24:09 +0000142void
Ted Kremenek29e0ef22009-11-03 06:59:59 +0000143NSErrorChecker::CheckSignature(const FunctionDecl& F, QualType& ResultTy,
Ted Kremenekcf118d42009-02-04 23:49:09 +0000144 llvm::SmallVectorImpl<VarDecl*>& ErrorParams) {
Mike Stump1eb44332009-09-09 15:08:12 +0000145
Ted Kremenekcc9ac412008-10-01 23:24:09 +0000146 ResultTy = F.getResultType();
Mike Stump1eb44332009-09-09 15:08:12 +0000147
148 for (FunctionDecl::param_const_iterator I = F.param_begin(),
Zhongxing Xu5ab128b2009-08-21 02:18:44 +0000149 E = F.param_end(); I != E; ++I) {
Mike Stump1eb44332009-09-09 15:08:12 +0000150
151 QualType T = (*I)->getType();
152
Ted Kremenekcf118d42009-02-04 23:49:09 +0000153 if (isNSErrorWarning) {
154 if (CheckNSErrorArgument(T)) ErrorParams.push_back(*I);
155 }
156 else if (CheckCFErrorArgument(T))
157 ErrorParams.push_back(*I);
Ted Kremenekcc9ac412008-10-01 23:24:09 +0000158 }
159}
160
161
Ted Kremenek29e0ef22009-11-03 06:59:59 +0000162bool NSErrorChecker::CheckNSErrorArgument(QualType ArgTy) {
Mike Stump1eb44332009-09-09 15:08:12 +0000163
Ted Kremenek6217b802009-07-29 21:53:49 +0000164 const PointerType* PPT = ArgTy->getAs<PointerType>();
Steve Naroff14108da2009-07-10 23:34:53 +0000165 if (!PPT)
166 return false;
Mike Stump1eb44332009-09-09 15:08:12 +0000167
Steve Naroff14108da2009-07-10 23:34:53 +0000168 const ObjCObjectPointerType* PT =
John McCall183700f2009-09-21 23:43:11 +0000169 PPT->getPointeeType()->getAs<ObjCObjectPointerType>();
Steve Naroff14108da2009-07-10 23:34:53 +0000170
171 if (!PT)
172 return false;
Mike Stump1eb44332009-09-09 15:08:12 +0000173
Steve Naroff14108da2009-07-10 23:34:53 +0000174 const ObjCInterfaceDecl *ID = PT->getInterfaceDecl();
Mike Stump1eb44332009-09-09 15:08:12 +0000175
Steve Naroff14108da2009-07-10 23:34:53 +0000176 // FIXME: Can ID ever be NULL?
177 if (ID)
178 return II == ID->getIdentifier();
Mike Stump1eb44332009-09-09 15:08:12 +0000179
Steve Naroff14108da2009-07-10 23:34:53 +0000180 return false;
Ted Kremenekcfdf9b42008-09-18 21:25:13 +0000181}
Ted Kremenek7360fda2008-09-18 23:09:54 +0000182
Ted Kremenek29e0ef22009-11-03 06:59:59 +0000183bool NSErrorChecker::CheckCFErrorArgument(QualType ArgTy) {
Mike Stump1eb44332009-09-09 15:08:12 +0000184
Ted Kremenek6217b802009-07-29 21:53:49 +0000185 const PointerType* PPT = ArgTy->getAs<PointerType>();
Ted Kremenekcc9ac412008-10-01 23:24:09 +0000186 if (!PPT) return false;
Mike Stump1eb44332009-09-09 15:08:12 +0000187
John McCall183700f2009-09-21 23:43:11 +0000188 const TypedefType* TT = PPT->getPointeeType()->getAs<TypedefType>();
Ted Kremenekcc9ac412008-10-01 23:24:09 +0000189 if (!TT) return false;
190
Ted Kremenekcf118d42009-02-04 23:49:09 +0000191 return TT->getDecl()->getIdentifier() == II;
Ted Kremenekcc9ac412008-10-01 23:24:09 +0000192}
193
Ted Kremenek29e0ef22009-11-03 06:59:59 +0000194void NSErrorChecker::CheckParamDeref(const VarDecl *Param,
Ted Kremenekd17da2b2009-08-21 22:28:32 +0000195 const LocationContext *LC,
196 const GRState *rootState,
Ted Kremenekcf118d42009-02-04 23:49:09 +0000197 BugReporter& BR) {
Mike Stump1eb44332009-09-09 15:08:12 +0000198
Ted Kremenekd17da2b2009-08-21 22:28:32 +0000199 SVal ParamL = rootState->getLValue(Param, LC);
Ted Kremenek993f1c72008-10-17 20:28:54 +0000200 const MemRegion* ParamR = cast<loc::MemRegionVal>(ParamL).getRegionAs<VarRegion>();
201 assert (ParamR && "Parameters always have VarRegions.");
Ted Kremenekc8781382009-06-17 22:28:13 +0000202 SVal ParamSVal = rootState->getSVal(ParamR);
Mike Stump1eb44332009-09-09 15:08:12 +0000203
Zhongxing Xu1c96b242008-10-17 05:57:07 +0000204 // FIXME: For now assume that ParamSVal is symbolic. We need to generalize
Ted Kremenek7360fda2008-09-18 23:09:54 +0000205 // this later.
Ted Kremenek93e71452009-03-30 19:53:37 +0000206 SymbolRef ParamSym = ParamSVal.getAsLocSymbol();
207 if (!ParamSym)
208 return;
Mike Stump1eb44332009-09-09 15:08:12 +0000209
Ted Kremenek7360fda2008-09-18 23:09:54 +0000210 // Iterate over the implicit-null dereferences.
Ted Kremenekb4b817d2009-11-11 03:26:34 +0000211 ExplodedNode *const* I, *const* E;
212 llvm::tie(I, E) = GetImplicitNullDereferences(Eng);
213 for ( ; I != E; ++I) {
Ted Kremenekc8781382009-06-17 22:28:13 +0000214 const GRState *state = (*I)->getState();
Ted Kremenekb4b817d2009-11-11 03:26:34 +0000215 SVal location = state->getSVal((*I)->getLocationAs<StmtPoint>()->getStmt());
216 if (location.getAsSymbol() != ParamSym)
Ted Kremenek93e71452009-03-30 19:53:37 +0000217 continue;
Ted Kremenek7360fda2008-09-18 23:09:54 +0000218
219 // Emit an error.
Ted Kremenekcf118d42009-02-04 23:49:09 +0000220 std::string sbuf;
221 llvm::raw_string_ostream os(sbuf);
Ted Kremenekcc9ac412008-10-01 23:24:09 +0000222 os << "Potential null dereference. According to coding standards ";
Mike Stump1eb44332009-09-09 15:08:12 +0000223
Ted Kremenekcc9ac412008-10-01 23:24:09 +0000224 if (isNSErrorWarning)
225 os << "in 'Creating and Returning NSError Objects' the parameter '";
226 else
227 os << "documented in CoreFoundation/CFError.h the parameter '";
Mike Stump1eb44332009-09-09 15:08:12 +0000228
Chris Lattnerd9d22dd2008-11-24 05:29:24 +0000229 os << Param->getNameAsString() << "' may be null.";
Mike Stump1eb44332009-09-09 15:08:12 +0000230
Benjamin Kramer4988a9a2009-11-29 18:03:28 +0000231 BugReport *report = new BugReport(*this, os.str(), *I);
Ted Kremenekcf118d42009-02-04 23:49:09 +0000232 // FIXME: Notable symbols are now part of the report. We should
233 // add support for notable symbols in BugReport.
234 // BR.addNotableSymbol(SV->getSymbol());
235 BR.EmitReport(report);
Ted Kremenek7360fda2008-09-18 23:09:54 +0000236 }
237}