1 //=- NSErrorChecker.cpp - Coding conventions for uses of NSError -*- C++ -*-==//
3 // The LLVM Compiler Infrastructure
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
8 //===----------------------------------------------------------------------===//
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.
14 // File under feature request PR 2600.
16 //===----------------------------------------------------------------------===//
18 #include "ClangSACheckers.h"
19 #include "clang/AST/Decl.h"
20 #include "clang/AST/DeclObjC.h"
21 #include "clang/StaticAnalyzer/Core/BugReporter/BugType.h"
22 #include "clang/StaticAnalyzer/Core/Checker.h"
23 #include "clang/StaticAnalyzer/Core/CheckerManager.h"
24 #include "clang/StaticAnalyzer/Core/PathSensitive/CheckerContext.h"
25 #include "clang/StaticAnalyzer/Core/PathSensitive/ProgramStateTrait.h"
26 #include "llvm/ADT/SmallString.h"
27 #include "llvm/Support/raw_ostream.h"
29 using namespace clang;
32 static bool IsNSError(QualType T, IdentifierInfo *II);
33 static bool IsCFError(QualType T, IdentifierInfo *II);
35 //===----------------------------------------------------------------------===//
36 // NSErrorMethodChecker
37 //===----------------------------------------------------------------------===//
40 class NSErrorMethodChecker
41 : public Checker< check::ASTDecl<ObjCMethodDecl> > {
42 mutable IdentifierInfo *II;
45 NSErrorMethodChecker() : II(0) { }
47 void checkASTDecl(const ObjCMethodDecl *D,
48 AnalysisManager &mgr, BugReporter &BR) const;
52 void NSErrorMethodChecker::checkASTDecl(const ObjCMethodDecl *D,
54 BugReporter &BR) const {
55 if (!D->isThisDeclarationADefinition())
57 if (!D->getResultType()->isVoidType())
61 II = &D->getASTContext().Idents.get("NSError");
63 bool hasNSError = false;
64 for (ObjCMethodDecl::param_const_iterator
65 I = D->param_begin(), E = D->param_end(); I != E; ++I) {
66 if (IsNSError((*I)->getType(), II)) {
73 const char *err = "Method accepting NSError** "
74 "should have a non-void return value to indicate whether or not an "
76 PathDiagnosticLocation L =
77 PathDiagnosticLocation::create(D, BR.getSourceManager());
78 BR.EmitBasicReport(D, "Bad return type when passing NSError**",
79 "Coding conventions (Apple)", err, L);
83 //===----------------------------------------------------------------------===//
84 // CFErrorFunctionChecker
85 //===----------------------------------------------------------------------===//
88 class CFErrorFunctionChecker
89 : public Checker< check::ASTDecl<FunctionDecl> > {
90 mutable IdentifierInfo *II;
93 CFErrorFunctionChecker() : II(0) { }
95 void checkASTDecl(const FunctionDecl *D,
96 AnalysisManager &mgr, BugReporter &BR) const;
100 void CFErrorFunctionChecker::checkASTDecl(const FunctionDecl *D,
101 AnalysisManager &mgr,
102 BugReporter &BR) const {
103 if (!D->doesThisDeclarationHaveABody())
105 if (!D->getResultType()->isVoidType())
109 II = &D->getASTContext().Idents.get("CFErrorRef");
111 bool hasCFError = false;
112 for (FunctionDecl::param_const_iterator
113 I = D->param_begin(), E = D->param_end(); I != E; ++I) {
114 if (IsCFError((*I)->getType(), II)) {
121 const char *err = "Function accepting CFErrorRef* "
122 "should have a non-void return value to indicate whether or not an "
124 PathDiagnosticLocation L =
125 PathDiagnosticLocation::create(D, BR.getSourceManager());
126 BR.EmitBasicReport(D, "Bad return type when passing CFErrorRef*",
127 "Coding conventions (Apple)", err, L);
131 //===----------------------------------------------------------------------===//
132 // NSOrCFErrorDerefChecker
133 //===----------------------------------------------------------------------===//
137 class NSErrorDerefBug : public BugType {
139 NSErrorDerefBug() : BugType("NSError** null dereference",
140 "Coding conventions (Apple)") {}
143 class CFErrorDerefBug : public BugType {
145 CFErrorDerefBug() : BugType("CFErrorRef* null dereference",
146 "Coding conventions (Apple)") {}
152 class NSOrCFErrorDerefChecker
153 : public Checker< check::Location,
154 check::Event<ImplicitNullDerefEvent> > {
155 mutable IdentifierInfo *NSErrorII, *CFErrorII;
157 bool ShouldCheckNSError, ShouldCheckCFError;
158 NSOrCFErrorDerefChecker() : NSErrorII(0), CFErrorII(0),
159 ShouldCheckNSError(0), ShouldCheckCFError(0) { }
161 void checkLocation(SVal loc, bool isLoad, const Stmt *S,
162 CheckerContext &C) const;
163 void checkEvent(ImplicitNullDerefEvent event) const;
167 typedef llvm::ImmutableMap<SymbolRef, unsigned> ErrorOutFlag;
168 REGISTER_TRAIT_WITH_PROGRAMSTATE(NSErrorOut, ErrorOutFlag)
169 REGISTER_TRAIT_WITH_PROGRAMSTATE(CFErrorOut, ErrorOutFlag)
171 template <typename T>
172 static bool hasFlag(SVal val, ProgramStateRef state) {
173 if (SymbolRef sym = val.getAsSymbol())
174 if (const unsigned *attachedFlags = state->get<T>(sym))
175 return *attachedFlags;
179 template <typename T>
180 static void setFlag(ProgramStateRef state, SVal val, CheckerContext &C) {
181 // We tag the symbol that the SVal wraps.
182 if (SymbolRef sym = val.getAsSymbol())
183 C.addTransition(state->set<T>(sym, true));
186 static QualType parameterTypeFromSVal(SVal val, CheckerContext &C) {
187 const StackFrameContext *
188 SFC = C.getLocationContext()->getCurrentStackFrame();
189 if (Optional<loc::MemRegionVal> X = val.getAs<loc::MemRegionVal>()) {
190 const MemRegion* R = X->getRegion();
191 if (const VarRegion *VR = R->getAs<VarRegion>())
192 if (const StackArgumentsSpaceRegion *
193 stackReg = dyn_cast<StackArgumentsSpaceRegion>(VR->getMemorySpace()))
194 if (stackReg->getStackFrame() == SFC)
195 return VR->getValueType();
201 void NSOrCFErrorDerefChecker::checkLocation(SVal loc, bool isLoad,
203 CheckerContext &C) const {
206 if (loc.isUndef() || !loc.getAs<Loc>())
209 ASTContext &Ctx = C.getASTContext();
210 ProgramStateRef state = C.getState();
212 // If we are loading from NSError**/CFErrorRef* parameter, mark the resulting
213 // SVal so that we can later check it when handling the
214 // ImplicitNullDerefEvent event.
215 // FIXME: Cumbersome! Maybe add hook at construction of SVals at start of
218 QualType parmT = parameterTypeFromSVal(loc, C);
223 NSErrorII = &Ctx.Idents.get("NSError");
225 CFErrorII = &Ctx.Idents.get("CFErrorRef");
227 if (ShouldCheckNSError && IsNSError(parmT, NSErrorII)) {
228 setFlag<NSErrorOut>(state, state->getSVal(loc.castAs<Loc>()), C);
232 if (ShouldCheckCFError && IsCFError(parmT, CFErrorII)) {
233 setFlag<CFErrorOut>(state, state->getSVal(loc.castAs<Loc>()), C);
238 void NSOrCFErrorDerefChecker::checkEvent(ImplicitNullDerefEvent event) const {
242 SVal loc = event.Location;
243 ProgramStateRef state = event.SinkNode->getState();
244 BugReporter &BR = *event.BR;
246 bool isNSError = hasFlag<NSErrorOut>(loc, state);
247 bool isCFError = false;
249 isCFError = hasFlag<CFErrorOut>(loc, state);
251 if (!(isNSError || isCFError))
254 // Storing to possible null NSError/CFErrorRef out parameter.
255 SmallString<128> Buf;
256 llvm::raw_svector_ostream os(Buf);
258 os << "Potential null dereference. According to coding standards ";
260 ? "in 'Creating and Returning NSError Objects' the parameter"
261 : "documented in CoreFoundation/CFError.h the parameter");
263 os << " may be null";
267 bug = new NSErrorDerefBug();
269 bug = new CFErrorDerefBug();
270 BugReport *report = new BugReport(*bug, os.str(),
272 BR.emitReport(report);
275 static bool IsNSError(QualType T, IdentifierInfo *II) {
277 const PointerType* PPT = T->getAs<PointerType>();
281 const ObjCObjectPointerType* PT =
282 PPT->getPointeeType()->getAs<ObjCObjectPointerType>();
287 const ObjCInterfaceDecl *ID = PT->getInterfaceDecl();
289 // FIXME: Can ID ever be NULL?
291 return II == ID->getIdentifier();
296 static bool IsCFError(QualType T, IdentifierInfo *II) {
297 const PointerType* PPT = T->getAs<PointerType>();
298 if (!PPT) return false;
300 const TypedefType* TT = PPT->getPointeeType()->getAs<TypedefType>();
301 if (!TT) return false;
303 return TT->getDecl()->getIdentifier() == II;
306 void ento::registerNSErrorChecker(CheckerManager &mgr) {
307 mgr.registerChecker<NSErrorMethodChecker>();
308 NSOrCFErrorDerefChecker *
309 checker = mgr.registerChecker<NSOrCFErrorDerefChecker>();
310 checker->ShouldCheckNSError = true;
313 void ento::registerCFErrorChecker(CheckerManager &mgr) {
314 mgr.registerChecker<CFErrorFunctionChecker>();
315 NSOrCFErrorDerefChecker *
316 checker = mgr.registerChecker<NSOrCFErrorDerefChecker>();
317 checker->ShouldCheckCFError = true;