blob: d39fa0a86a2ac2dda7803c0215245876f9cc3e56 [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 {
30class VISIBILITY_HIDDEN NSErrorCheck : public BugTypeCacheLocation {
31
32 void EmitGRWarnings(GRBugReporter& BR);
Ted Kremenekf45d18c2008-09-18 06:33:41 +000033
Ted Kremenekcfdf9b42008-09-18 21:25:13 +000034 void CheckSignature(ObjCMethodDecl& MD, QualType& ResultTy,
35 llvm::SmallVectorImpl<VarDecl*>& Params,
36 IdentifierInfo* NSErrorII);
37
38 bool CheckArgument(QualType ArgTy, IdentifierInfo* NSErrorII);
39
Ted Kremenek7360fda2008-09-18 23:09:54 +000040 void CheckParamDeref(VarDecl* V, GRStateRef state, GRExprEngine& Eng,
41 GRBugReporter& BR);
42
43 const char* desc;
Ted Kremenekcfdf9b42008-09-18 21:25:13 +000044public:
Ted Kremenek7360fda2008-09-18 23:09:54 +000045 NSErrorCheck() : desc(0) {}
46
Ted Kremenekcfdf9b42008-09-18 21:25:13 +000047 void EmitWarnings(BugReporter& BR) { EmitGRWarnings(cast<GRBugReporter>(BR));}
48 const char* getName() const { return "NSError** null dereference"; }
Ted Kremenek7360fda2008-09-18 23:09:54 +000049 const char* getDescription() const { return desc; }
Ted Kremenekcfdf9b42008-09-18 21:25:13 +000050};
51
52} // end anonymous namespace
53
54BugType* clang::CreateNSErrorCheck() {
55 return new NSErrorCheck();
56}
57
58void NSErrorCheck::EmitGRWarnings(GRBugReporter& BR) {
59 // Get the analysis engine and the exploded analysis graph.
60 GRExprEngine& Eng = BR.getEngine();
61 GRExprEngine::GraphTy& G = Eng.getGraph();
62
63 // Get the declaration of the method/function that was analyzed.
64 Decl& CodeDecl = G.getCodeDecl();
65
66 ObjCMethodDecl* MD = dyn_cast<ObjCMethodDecl>(&CodeDecl);
67 if (!MD)
68 return;
69
70 // Get the ASTContext, which is useful for querying type information.
Ted Kremenekf45d18c2008-09-18 06:33:41 +000071 ASTContext &Ctx = BR.getContext();
Ted Kremenekcfdf9b42008-09-18 21:25:13 +000072
73 QualType ResultTy;
74 llvm::SmallVector<VarDecl*, 5> Params;
75 CheckSignature(*MD, ResultTy, Params, &Ctx.Idents.get("NSError"));
Ted Kremenekf45d18c2008-09-18 06:33:41 +000076
Ted Kremenekcfdf9b42008-09-18 21:25:13 +000077 if (Params.empty())
78 return;
79
80 if (ResultTy == Ctx.VoidTy) {
81 BR.EmitBasicReport("Bad return type when passing NSError**",
82 "Method accepting NSError** argument should have "
83 "non-void return value to indicate that an error occurred.",
84 CodeDecl.getLocation());
Ted Kremenekf45d18c2008-09-18 06:33:41 +000085
Ted Kremenekf45d18c2008-09-18 06:33:41 +000086 }
Ted Kremenek7360fda2008-09-18 23:09:54 +000087
88 // Scan the NSError** parameters for an implicit null dereference.
89 for (llvm::SmallVectorImpl<VarDecl*>::iterator I=Params.begin(),
90 E=Params.end(); I!=E; ++I)
91 for (GRExprEngine::GraphTy::roots_iterator RI=G.roots_begin(),
92 RE=G.roots_end(); RI!=RE; ++RI)
93 CheckParamDeref(*I, GRStateRef((*RI)->getState(), Eng.getStateManager()),
94 Eng, BR);
Ted Kremenekf45d18c2008-09-18 06:33:41 +000095}
Ted Kremenekcfdf9b42008-09-18 21:25:13 +000096
97void NSErrorCheck::CheckSignature(ObjCMethodDecl& M, QualType& ResultTy,
98 llvm::SmallVectorImpl<VarDecl*>& Params,
99 IdentifierInfo* NSErrorII) {
100
101 ResultTy = M.getResultType();
102
103 for (ObjCMethodDecl::param_iterator I=M.param_begin(),
104 E=M.param_end(); I!=E; ++I)
105 if (CheckArgument((*I)->getType(), NSErrorII))
106 Params.push_back(*I);
107}
108
109bool NSErrorCheck::CheckArgument(QualType ArgTy, IdentifierInfo* NSErrorII) {
110 const PointerType* PPT = ArgTy->getAsPointerType();
111 if (!PPT) return false;
112
113 const PointerType* PT = PPT->getPointeeType()->getAsPointerType();
114 if (!PT) return false;
115
116 const ObjCInterfaceType *IT =
117 PT->getPointeeType()->getAsObjCInterfaceType();
118
119 if (!IT) return false;
120 return IT->getDecl()->getIdentifier() == NSErrorII;
121}
Ted Kremenek7360fda2008-09-18 23:09:54 +0000122
123void NSErrorCheck::CheckParamDeref(VarDecl* Param, GRStateRef rootState,
124 GRExprEngine& Eng, GRBugReporter& BR) {
125
126 RVal ParamRVal = rootState.GetRVal(lval::DeclVal(Param));
127
128 // FIXME: For now assume that ParamRVal is symbolic. We need to generalize
129 // this later.
130 lval::SymbolVal* SV = dyn_cast<lval::SymbolVal>(&ParamRVal);
131 if (!SV) return;
132
133 // Iterate over the implicit-null dereferences.
134 for (GRExprEngine::null_deref_iterator I=Eng.implicit_null_derefs_begin(),
135 E=Eng.implicit_null_derefs_end(); I!=E; ++I) {
136
137 GRStateRef state = GRStateRef((*I)->getState(), Eng.getStateManager());
138 const RVal* X = state.get<GRState::NullDerefTag>();
139 const lval::SymbolVal* SVX = dyn_cast_or_null<lval::SymbolVal>(X);
140 if (!SVX || SVX->getSymbol() != SV->getSymbol()) continue;
141
142 // Emit an error.
143 BugReport R(*this, *I);
144
145 std::string msg;
146 llvm::raw_string_ostream os(msg);
147 os << "Potential null dereference. According to coding standards in "
148 "'Creating and Returning NSError Objects' the parameter '"
149 << Param->getName() << "' may be null.";
150 desc = os.str().c_str();
151
152 BR.EmitWarning(R);
153 }
154}