blob: a35497a6bfd88fe657dbc1e7c529bb5d9c383d11 [file] [log] [blame]
Ted Kremenekf45d18c2008-09-18 06:33:41 +00001//=- CheckNSError.cpp - Coding conventions for uses of NSError ---*- C++ -*-==//
2//
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"
21#include "BasicObjCFoundationChecks.h"
22#include "llvm/Support/Compiler.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 {
Ted Kremenekcf118d42009-02-04 23:49:09 +000030class VISIBILITY_HIDDEN NSErrorCheck : public BugType {
31 const bool isNSErrorWarning;
32 IdentifierInfo * const II;
33 GRExprEngine &Eng;
Ted Kremenekf45d18c2008-09-18 06:33:41 +000034
Ted Kremenekcfdf9b42008-09-18 21:25:13 +000035 void CheckSignature(ObjCMethodDecl& MD, QualType& ResultTy,
Ted Kremenekcf118d42009-02-04 23:49:09 +000036 llvm::SmallVectorImpl<VarDecl*>& ErrorParams);
Ted Kremenekcc9ac412008-10-01 23:24:09 +000037
38 void CheckSignature(FunctionDecl& MD, QualType& ResultTy,
Ted Kremenekcf118d42009-02-04 23:49:09 +000039 llvm::SmallVectorImpl<VarDecl*>& ErrorParams);
Ted Kremenekcfdf9b42008-09-18 21:25:13 +000040
Ted Kremenekcf118d42009-02-04 23:49:09 +000041 bool CheckNSErrorArgument(QualType ArgTy);
42 bool CheckCFErrorArgument(QualType ArgTy);
Ted Kremenekcfdf9b42008-09-18 21:25:13 +000043
Ted Kremenekcf118d42009-02-04 23:49:09 +000044 void CheckParamDeref(VarDecl* V, GRStateRef state, BugReporter& BR);
Ted Kremenekcc9ac412008-10-01 23:24:09 +000045
Ted Kremenekcf118d42009-02-04 23:49:09 +000046 void EmitRetTyWarning(BugReporter& BR, Decl& CodeDecl);
Ted Kremenek7360fda2008-09-18 23:09:54 +000047
Ted Kremenekcfdf9b42008-09-18 21:25:13 +000048public:
Ted Kremenekcf118d42009-02-04 23:49:09 +000049 NSErrorCheck(bool isNSError, GRExprEngine& eng)
50 : BugType(isNSError ? "NSError** null dereference"
51 : "CFErrorRef* null dereference",
52 "Coding Conventions (Apple)"),
53 isNSErrorWarning(isNSError),
54 II(&eng.getContext().Idents.get(isNSErrorWarning ? "NSError":"CFErrorRef")),
55 Eng(eng) {}
Ted Kremenek7360fda2008-09-18 23:09:54 +000056
Ted Kremenekcf118d42009-02-04 23:49:09 +000057 void FlushReports(BugReporter& BR);
Ted Kremenekcfdf9b42008-09-18 21:25:13 +000058};
59
60} // end anonymous namespace
61
Ted Kremenekcf118d42009-02-04 23:49:09 +000062void clang::RegisterNSErrorChecks(BugReporter& BR, GRExprEngine &Eng) {
63 BR.Register(new NSErrorCheck(true, Eng));
64 BR.Register(new NSErrorCheck(false, Eng));
Ted Kremenekcfdf9b42008-09-18 21:25:13 +000065}
66
Ted Kremenekcf118d42009-02-04 23:49:09 +000067void NSErrorCheck::FlushReports(BugReporter& BR) {
Ted Kremenekcfdf9b42008-09-18 21:25:13 +000068 // Get the analysis engine and the exploded analysis graph.
Ted Kremenekcfdf9b42008-09-18 21:25:13 +000069 GRExprEngine::GraphTy& G = Eng.getGraph();
70
71 // Get the declaration of the method/function that was analyzed.
72 Decl& CodeDecl = G.getCodeDecl();
Ted Kremenekcc9ac412008-10-01 23:24:09 +000073
Ted Kremenekcfdf9b42008-09-18 21:25:13 +000074 // Get the ASTContext, which is useful for querying type information.
Ted Kremenekf45d18c2008-09-18 06:33:41 +000075 ASTContext &Ctx = BR.getContext();
Ted Kremenekcfdf9b42008-09-18 21:25:13 +000076
77 QualType ResultTy;
Ted Kremenekcf118d42009-02-04 23:49:09 +000078 llvm::SmallVector<VarDecl*, 5> ErrorParams;
Ted Kremenekcc9ac412008-10-01 23:24:09 +000079
80 if (ObjCMethodDecl* MD = dyn_cast<ObjCMethodDecl>(&CodeDecl))
Ted Kremenekcf118d42009-02-04 23:49:09 +000081 CheckSignature(*MD, ResultTy, ErrorParams);
Ted Kremenekcc9ac412008-10-01 23:24:09 +000082 else if (FunctionDecl* FD = dyn_cast<FunctionDecl>(&CodeDecl))
Ted Kremenekcf118d42009-02-04 23:49:09 +000083 CheckSignature(*FD, ResultTy, ErrorParams);
Ted Kremenekcc9ac412008-10-01 23:24:09 +000084 else
Ted Kremenekcfdf9b42008-09-18 21:25:13 +000085 return;
86
Ted Kremenekcf118d42009-02-04 23:49:09 +000087 if (ErrorParams.empty())
Ted Kremenekcc9ac412008-10-01 23:24:09 +000088 return;
89
Ted Kremenekcf118d42009-02-04 23:49:09 +000090 if (ResultTy == Ctx.VoidTy) EmitRetTyWarning(BR, CodeDecl);
Ted Kremenek7360fda2008-09-18 23:09:54 +000091
Ted Kremenekcc9ac412008-10-01 23:24:09 +000092 for (GRExprEngine::GraphTy::roots_iterator RI=G.roots_begin(),
93 RE=G.roots_end(); RI!=RE; ++RI) {
Ted Kremenekcf118d42009-02-04 23:49:09 +000094 // Scan the parameters for an implicit null dereference.
95 for (llvm::SmallVectorImpl<VarDecl*>::iterator I=ErrorParams.begin(),
96 E=ErrorParams.end(); I!=E; ++I)
97 CheckParamDeref(*I, GRStateRef((*RI)->getState(),Eng.getStateManager()),
98 BR);
Ted Kremenekcc9ac412008-10-01 23:24:09 +000099
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 Kremenekcf118d42009-02-04 23:49:09 +0000103void NSErrorCheck::EmitRetTyWarning(BugReporter& BR, Decl& CodeDecl) {
104 std::string sbuf;
105 llvm::raw_string_ostream os(sbuf);
Ted Kremenekcc9ac412008-10-01 23:24:09 +0000106
107 if (isa<ObjCMethodDecl>(CodeDecl))
108 os << "Method";
109 else
110 os << "Function";
111
112 os << " accepting ";
113 os << (isNSErrorWarning ? "NSError**" : "CFErrorRef*");
114 os << " should have a non-void return value to indicate whether or not an "
115 "error occured.";
116
117 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
125NSErrorCheck::CheckSignature(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();
129
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
133 QualType T = (*I)->getType();
134
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
144NSErrorCheck::CheckSignature(FunctionDecl& F, QualType& ResultTy,
Ted Kremenekcf118d42009-02-04 23:49:09 +0000145 llvm::SmallVectorImpl<VarDecl*>& ErrorParams) {
Ted Kremenekcc9ac412008-10-01 23:24:09 +0000146
147 ResultTy = F.getResultType();
148
149 for (FunctionDecl::param_iterator I=F.param_begin(),
150 E=F.param_end(); I!=E; ++I) {
151
152 QualType T = (*I)->getType();
Ted Kremenekcf118d42009-02-04 23:49:09 +0000153
154 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 Kremenekcf118d42009-02-04 23:49:09 +0000163bool NSErrorCheck::CheckNSErrorArgument(QualType ArgTy) {
Ted Kremenekcc9ac412008-10-01 23:24:09 +0000164
Ted Kremenekcfdf9b42008-09-18 21:25:13 +0000165 const PointerType* PPT = ArgTy->getAsPointerType();
166 if (!PPT) return false;
167
168 const PointerType* PT = PPT->getPointeeType()->getAsPointerType();
169 if (!PT) return false;
170
171 const ObjCInterfaceType *IT =
172 PT->getPointeeType()->getAsObjCInterfaceType();
173
174 if (!IT) return false;
Ted Kremenekcf118d42009-02-04 23:49:09 +0000175 return IT->getDecl()->getIdentifier() == II;
Ted Kremenekcfdf9b42008-09-18 21:25:13 +0000176}
Ted Kremenek7360fda2008-09-18 23:09:54 +0000177
Ted Kremenekcf118d42009-02-04 23:49:09 +0000178bool NSErrorCheck::CheckCFErrorArgument(QualType ArgTy) {
Ted Kremenekcc9ac412008-10-01 23:24:09 +0000179
180 const PointerType* PPT = ArgTy->getAsPointerType();
181 if (!PPT) return false;
182
183 const TypedefType* TT = PPT->getPointeeType()->getAsTypedefType();
184 if (!TT) return false;
185
Ted Kremenekcf118d42009-02-04 23:49:09 +0000186 return TT->getDecl()->getIdentifier() == II;
Ted Kremenekcc9ac412008-10-01 23:24:09 +0000187}
188
Ted Kremenek7360fda2008-09-18 23:09:54 +0000189void NSErrorCheck::CheckParamDeref(VarDecl* Param, GRStateRef rootState,
Ted Kremenekcf118d42009-02-04 23:49:09 +0000190 BugReporter& BR) {
Ted Kremenek7360fda2008-09-18 23:09:54 +0000191
Ted Kremenek993f1c72008-10-17 20:28:54 +0000192 SVal ParamL = rootState.GetLValue(Param);
193 const MemRegion* ParamR = cast<loc::MemRegionVal>(ParamL).getRegionAs<VarRegion>();
194 assert (ParamR && "Parameters always have VarRegions.");
195 SVal ParamSVal = rootState.GetSVal(ParamR);
196
Ted Kremenek7360fda2008-09-18 23:09:54 +0000197
Zhongxing Xu1c96b242008-10-17 05:57:07 +0000198 // FIXME: For now assume that ParamSVal is symbolic. We need to generalize
Ted Kremenek7360fda2008-09-18 23:09:54 +0000199 // this later.
Zhongxing Xu1c96b242008-10-17 05:57:07 +0000200 loc::SymbolVal* SV = dyn_cast<loc::SymbolVal>(&ParamSVal);
Ted Kremenek7360fda2008-09-18 23:09:54 +0000201 if (!SV) return;
202
203 // Iterate over the implicit-null dereferences.
204 for (GRExprEngine::null_deref_iterator I=Eng.implicit_null_derefs_begin(),
205 E=Eng.implicit_null_derefs_end(); I!=E; ++I) {
206
207 GRStateRef state = GRStateRef((*I)->getState(), Eng.getStateManager());
Zhongxing Xu1c96b242008-10-17 05:57:07 +0000208 const SVal* X = state.get<GRState::NullDerefTag>();
209 const loc::SymbolVal* SVX = dyn_cast_or_null<loc::SymbolVal>(X);
Ted Kremenek7360fda2008-09-18 23:09:54 +0000210 if (!SVX || SVX->getSymbol() != SV->getSymbol()) continue;
211
212 // Emit an error.
Ted Kremenek7360fda2008-09-18 23:09:54 +0000213
Ted Kremenekcf118d42009-02-04 23:49:09 +0000214
215
216 std::string sbuf;
217 llvm::raw_string_ostream os(sbuf);
Ted Kremenekcc9ac412008-10-01 23:24:09 +0000218 os << "Potential null dereference. According to coding standards ";
219
220 if (isNSErrorWarning)
221 os << "in 'Creating and Returning NSError Objects' the parameter '";
222 else
223 os << "documented in CoreFoundation/CFError.h the parameter '";
224
Chris Lattnerd9d22dd2008-11-24 05:29:24 +0000225 os << Param->getNameAsString() << "' may be null.";
Ted Kremenekcf118d42009-02-04 23:49:09 +0000226
227 BugReport *report = new BugReport(*this, os.str().c_str(), *I);
228 // FIXME: Notable symbols are now part of the report. We should
229 // add support for notable symbols in BugReport.
230 // BR.addNotableSymbol(SV->getSymbol());
231 BR.EmitReport(report);
Ted Kremenek7360fda2008-09-18 23:09:54 +0000232 }
233}