blob: c91442b5e82915a09599fe224eadcc1fbe4ab8ff [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 Kremenekc8781382009-06-17 22:28:13 +000044 void CheckParamDeref(VarDecl* V, const GRState *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)
Ted Kremenekc8781382009-06-17 22:28:13 +000097 CheckParamDeref(*I, (*RI)->getState(), BR);
Ted Kremenekcc9ac412008-10-01 23:24:09 +000098
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 Kremenekcf118d42009-02-04 23:49:09 +0000102void NSErrorCheck::EmitRetTyWarning(BugReporter& BR, Decl& CodeDecl) {
103 std::string sbuf;
104 llvm::raw_string_ostream os(sbuf);
Ted Kremenekcc9ac412008-10-01 23:24:09 +0000105
106 if (isa<ObjCMethodDecl>(CodeDecl))
107 os << "Method";
108 else
109 os << "Function";
110
111 os << " accepting ";
112 os << (isNSErrorWarning ? "NSError**" : "CFErrorRef*");
113 os << " should have a non-void return value to indicate whether or not an "
114 "error occured.";
115
116 BR.EmitBasicReport(isNSErrorWarning
117 ? "Bad return type when passing NSError**"
118 : "Bad return type when passing CFError*",
Ted Kremenekcf118d42009-02-04 23:49:09 +0000119 getCategory().c_str(), os.str().c_str(),
120 CodeDecl.getLocation());
Ted Kremenekcc9ac412008-10-01 23:24:09 +0000121}
122
123void
124NSErrorCheck::CheckSignature(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();
128
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
132 QualType T = (*I)->getType();
133
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
143NSErrorCheck::CheckSignature(FunctionDecl& F, QualType& ResultTy,
Ted Kremenekcf118d42009-02-04 23:49:09 +0000144 llvm::SmallVectorImpl<VarDecl*>& ErrorParams) {
Ted Kremenekcc9ac412008-10-01 23:24:09 +0000145
146 ResultTy = F.getResultType();
147
148 for (FunctionDecl::param_iterator I=F.param_begin(),
149 E=F.param_end(); I!=E; ++I) {
150
151 QualType T = (*I)->getType();
Ted Kremenekcf118d42009-02-04 23:49:09 +0000152
153 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 Kremenekcf118d42009-02-04 23:49:09 +0000162bool NSErrorCheck::CheckNSErrorArgument(QualType ArgTy) {
Ted Kremenekcc9ac412008-10-01 23:24:09 +0000163
Ted Kremenekcfdf9b42008-09-18 21:25:13 +0000164 const PointerType* PPT = ArgTy->getAsPointerType();
165 if (!PPT) return false;
166
167 const PointerType* PT = PPT->getPointeeType()->getAsPointerType();
168 if (!PT) return false;
169
170 const ObjCInterfaceType *IT =
171 PT->getPointeeType()->getAsObjCInterfaceType();
172
173 if (!IT) return false;
Ted Kremenekcf118d42009-02-04 23:49:09 +0000174 return IT->getDecl()->getIdentifier() == II;
Ted Kremenekcfdf9b42008-09-18 21:25:13 +0000175}
Ted Kremenek7360fda2008-09-18 23:09:54 +0000176
Ted Kremenekcf118d42009-02-04 23:49:09 +0000177bool NSErrorCheck::CheckCFErrorArgument(QualType ArgTy) {
Ted Kremenekcc9ac412008-10-01 23:24:09 +0000178
179 const PointerType* PPT = ArgTy->getAsPointerType();
180 if (!PPT) return false;
181
182 const TypedefType* TT = PPT->getPointeeType()->getAsTypedefType();
183 if (!TT) return false;
184
Ted Kremenekcf118d42009-02-04 23:49:09 +0000185 return TT->getDecl()->getIdentifier() == II;
Ted Kremenekcc9ac412008-10-01 23:24:09 +0000186}
187
Ted Kremenekc8781382009-06-17 22:28:13 +0000188void NSErrorCheck::CheckParamDeref(VarDecl* Param, const GRState *rootState,
Ted Kremenekcf118d42009-02-04 23:49:09 +0000189 BugReporter& BR) {
Ted Kremenek7360fda2008-09-18 23:09:54 +0000190
Ted Kremenekc8781382009-06-17 22:28:13 +0000191 SVal ParamL = rootState->getLValue(Param);
Ted Kremenek993f1c72008-10-17 20:28:54 +0000192 const MemRegion* ParamR = cast<loc::MemRegionVal>(ParamL).getRegionAs<VarRegion>();
193 assert (ParamR && "Parameters always have VarRegions.");
Ted Kremenekc8781382009-06-17 22:28:13 +0000194 SVal ParamSVal = rootState->getSVal(ParamR);
Ted Kremenek993f1c72008-10-17 20:28:54 +0000195
Zhongxing Xu1c96b242008-10-17 05:57:07 +0000196 // FIXME: For now assume that ParamSVal is symbolic. We need to generalize
Ted Kremenek7360fda2008-09-18 23:09:54 +0000197 // this later.
Ted Kremenek93e71452009-03-30 19:53:37 +0000198 SymbolRef ParamSym = ParamSVal.getAsLocSymbol();
199 if (!ParamSym)
200 return;
Ted Kremenek7360fda2008-09-18 23:09:54 +0000201
202 // Iterate over the implicit-null dereferences.
203 for (GRExprEngine::null_deref_iterator I=Eng.implicit_null_derefs_begin(),
204 E=Eng.implicit_null_derefs_end(); I!=E; ++I) {
205
Ted Kremenekc8781382009-06-17 22:28:13 +0000206 const GRState *state = (*I)->getState();
207 const SVal* X = state->get<GRState::NullDerefTag>();
Ted Kremenek93e71452009-03-30 19:53:37 +0000208
209 if (!X || X->getAsSymbol() != ParamSym)
210 continue;
Ted Kremenek7360fda2008-09-18 23:09:54 +0000211
212 // Emit an error.
Ted Kremenekcf118d42009-02-04 23:49:09 +0000213 std::string sbuf;
214 llvm::raw_string_ostream os(sbuf);
Ted Kremenekcc9ac412008-10-01 23:24:09 +0000215 os << "Potential null dereference. According to coding standards ";
216
217 if (isNSErrorWarning)
218 os << "in 'Creating and Returning NSError Objects' the parameter '";
219 else
220 os << "documented in CoreFoundation/CFError.h the parameter '";
221
Chris Lattnerd9d22dd2008-11-24 05:29:24 +0000222 os << Param->getNameAsString() << "' may be null.";
Ted Kremenekcf118d42009-02-04 23:49:09 +0000223
224 BugReport *report = new BugReport(*this, os.str().c_str(), *I);
225 // FIXME: Notable symbols are now part of the report. We should
226 // add support for notable symbols in BugReport.
227 // BR.addNotableSymbol(SV->getSymbol());
228 BR.EmitReport(report);
Ted Kremenek7360fda2008-09-18 23:09:54 +0000229 }
230}