blob: d4697aa7cd07f84a0a57de56f32c5f29991849df [file] [log] [blame]
Ted Kremenek35335b62008-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 Kremenekb3538ab2008-09-18 21:25:13 +000020#include "clang/Analysis/PathSensitive/GRExprEngine.h"
21#include "BasicObjCFoundationChecks.h"
22#include "llvm/Support/Compiler.h"
Ted Kremenek35335b62008-09-18 06:33:41 +000023#include "clang/AST/DeclObjC.h"
Ted Kremenekb3538ab2008-09-18 21:25:13 +000024#include "clang/AST/Decl.h"
25#include "llvm/ADT/SmallVector.h"
Ted Kremenek35335b62008-09-18 06:33:41 +000026
27using namespace clang;
28
Ted Kremenekb3538ab2008-09-18 21:25:13 +000029namespace {
30class VISIBILITY_HIDDEN NSErrorCheck : public BugTypeCacheLocation {
31
32 void EmitGRWarnings(GRBugReporter& BR);
Ted Kremenek35335b62008-09-18 06:33:41 +000033
Ted Kremenekb3538ab2008-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 Kremenekbb7a3d92008-09-18 23:09:54 +000040 void CheckParamDeref(VarDecl* V, GRStateRef state, GRExprEngine& Eng,
41 GRBugReporter& BR);
42
43 const char* desc;
Ted Kremenekb3538ab2008-09-18 21:25:13 +000044public:
Ted Kremenekbb7a3d92008-09-18 23:09:54 +000045 NSErrorCheck() : desc(0) {}
46
Ted Kremenekb3538ab2008-09-18 21:25:13 +000047 void EmitWarnings(BugReporter& BR) { EmitGRWarnings(cast<GRBugReporter>(BR));}
48 const char* getName() const { return "NSError** null dereference"; }
Ted Kremenekbb7a3d92008-09-18 23:09:54 +000049 const char* getDescription() const { return desc; }
Ted Kremenek1d090a42008-09-21 06:57:40 +000050 const char* getCategory() const { return "Coding Conventions (Apple)"; }
Ted Kremenekb3538ab2008-09-18 21:25:13 +000051};
52
53} // end anonymous namespace
54
55BugType* clang::CreateNSErrorCheck() {
56 return new NSErrorCheck();
57}
58
59void NSErrorCheck::EmitGRWarnings(GRBugReporter& BR) {
60 // Get the analysis engine and the exploded analysis graph.
61 GRExprEngine& Eng = BR.getEngine();
62 GRExprEngine::GraphTy& G = Eng.getGraph();
63
64 // Get the declaration of the method/function that was analyzed.
65 Decl& CodeDecl = G.getCodeDecl();
66
67 ObjCMethodDecl* MD = dyn_cast<ObjCMethodDecl>(&CodeDecl);
68 if (!MD)
69 return;
70
71 // Get the ASTContext, which is useful for querying type information.
Ted Kremenek35335b62008-09-18 06:33:41 +000072 ASTContext &Ctx = BR.getContext();
Ted Kremenekb3538ab2008-09-18 21:25:13 +000073
74 QualType ResultTy;
75 llvm::SmallVector<VarDecl*, 5> Params;
76 CheckSignature(*MD, ResultTy, Params, &Ctx.Idents.get("NSError"));
Ted Kremenek35335b62008-09-18 06:33:41 +000077
Ted Kremenekb3538ab2008-09-18 21:25:13 +000078 if (Params.empty())
79 return;
80
81 if (ResultTy == Ctx.VoidTy) {
82 BR.EmitBasicReport("Bad return type when passing NSError**",
Ted Kremenek1d090a42008-09-21 06:57:40 +000083 getCategory(),
Ted Kremenekb3538ab2008-09-18 21:25:13 +000084 "Method accepting NSError** argument should have "
85 "non-void return value to indicate that an error occurred.",
86 CodeDecl.getLocation());
Ted Kremenek35335b62008-09-18 06:33:41 +000087
Ted Kremenek35335b62008-09-18 06:33:41 +000088 }
Ted Kremenekbb7a3d92008-09-18 23:09:54 +000089
90 // Scan the NSError** parameters for an implicit null dereference.
91 for (llvm::SmallVectorImpl<VarDecl*>::iterator I=Params.begin(),
92 E=Params.end(); I!=E; ++I)
93 for (GRExprEngine::GraphTy::roots_iterator RI=G.roots_begin(),
94 RE=G.roots_end(); RI!=RE; ++RI)
95 CheckParamDeref(*I, GRStateRef((*RI)->getState(), Eng.getStateManager()),
96 Eng, BR);
Ted Kremenek35335b62008-09-18 06:33:41 +000097}
Ted Kremenekb3538ab2008-09-18 21:25:13 +000098
99void NSErrorCheck::CheckSignature(ObjCMethodDecl& M, QualType& ResultTy,
100 llvm::SmallVectorImpl<VarDecl*>& Params,
101 IdentifierInfo* NSErrorII) {
102
103 ResultTy = M.getResultType();
104
105 for (ObjCMethodDecl::param_iterator I=M.param_begin(),
106 E=M.param_end(); I!=E; ++I)
107 if (CheckArgument((*I)->getType(), NSErrorII))
108 Params.push_back(*I);
109}
110
111bool NSErrorCheck::CheckArgument(QualType ArgTy, IdentifierInfo* NSErrorII) {
112 const PointerType* PPT = ArgTy->getAsPointerType();
113 if (!PPT) return false;
114
115 const PointerType* PT = PPT->getPointeeType()->getAsPointerType();
116 if (!PT) return false;
117
118 const ObjCInterfaceType *IT =
119 PT->getPointeeType()->getAsObjCInterfaceType();
120
121 if (!IT) return false;
122 return IT->getDecl()->getIdentifier() == NSErrorII;
123}
Ted Kremenekbb7a3d92008-09-18 23:09:54 +0000124
125void NSErrorCheck::CheckParamDeref(VarDecl* Param, GRStateRef rootState,
126 GRExprEngine& Eng, GRBugReporter& BR) {
127
128 RVal ParamRVal = rootState.GetRVal(lval::DeclVal(Param));
129
130 // FIXME: For now assume that ParamRVal is symbolic. We need to generalize
131 // this later.
132 lval::SymbolVal* SV = dyn_cast<lval::SymbolVal>(&ParamRVal);
133 if (!SV) return;
134
135 // Iterate over the implicit-null dereferences.
136 for (GRExprEngine::null_deref_iterator I=Eng.implicit_null_derefs_begin(),
137 E=Eng.implicit_null_derefs_end(); I!=E; ++I) {
138
139 GRStateRef state = GRStateRef((*I)->getState(), Eng.getStateManager());
140 const RVal* X = state.get<GRState::NullDerefTag>();
141 const lval::SymbolVal* SVX = dyn_cast_or_null<lval::SymbolVal>(X);
142 if (!SVX || SVX->getSymbol() != SV->getSymbol()) continue;
143
144 // Emit an error.
145 BugReport R(*this, *I);
146
147 std::string msg;
148 llvm::raw_string_ostream os(msg);
149 os << "Potential null dereference. According to coding standards in "
150 "'Creating and Returning NSError Objects' the parameter '"
151 << Param->getName() << "' may be null.";
152 desc = os.str().c_str();
153
Ted Kremenek2e52cca2008-09-18 23:23:19 +0000154 BR.addNotableSymbol(SV->getSymbol());
Ted Kremenekbb7a3d92008-09-18 23:09:54 +0000155 BR.EmitWarning(R);
156 }
157}