Ted Kremenek | f45d18c | 2008-09-18 06:33:41 +0000 | [diff] [blame] | 1 | //=- 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 Kremenek | cfdf9b4 | 2008-09-18 21:25:13 +0000 | [diff] [blame] | 20 | #include "clang/Analysis/PathSensitive/GRExprEngine.h" |
| 21 | #include "BasicObjCFoundationChecks.h" |
| 22 | #include "llvm/Support/Compiler.h" |
Ted Kremenek | f45d18c | 2008-09-18 06:33:41 +0000 | [diff] [blame] | 23 | #include "clang/AST/DeclObjC.h" |
Ted Kremenek | cfdf9b4 | 2008-09-18 21:25:13 +0000 | [diff] [blame] | 24 | #include "clang/AST/Decl.h" |
| 25 | #include "llvm/ADT/SmallVector.h" |
Ted Kremenek | f45d18c | 2008-09-18 06:33:41 +0000 | [diff] [blame] | 26 | |
| 27 | using namespace clang; |
| 28 | |
Ted Kremenek | cfdf9b4 | 2008-09-18 21:25:13 +0000 | [diff] [blame] | 29 | namespace { |
| 30 | class VISIBILITY_HIDDEN NSErrorCheck : public BugTypeCacheLocation { |
| 31 | |
| 32 | void EmitGRWarnings(GRBugReporter& BR); |
Ted Kremenek | f45d18c | 2008-09-18 06:33:41 +0000 | [diff] [blame] | 33 | |
Ted Kremenek | cfdf9b4 | 2008-09-18 21:25:13 +0000 | [diff] [blame] | 34 | void CheckSignature(ObjCMethodDecl& MD, QualType& ResultTy, |
Ted Kremenek | cc9ac41 | 2008-10-01 23:24:09 +0000 | [diff] [blame] | 35 | llvm::SmallVectorImpl<VarDecl*>& NSErrorParams, |
| 36 | llvm::SmallVectorImpl<VarDecl*>& CFErrorParams, |
| 37 | IdentifierInfo* NSErrorII, |
| 38 | IdentifierInfo* CFErrorII); |
| 39 | |
| 40 | void CheckSignature(FunctionDecl& MD, QualType& ResultTy, |
| 41 | llvm::SmallVectorImpl<VarDecl*>& NSErrorParams, |
| 42 | llvm::SmallVectorImpl<VarDecl*>& CFErrorParams, |
| 43 | IdentifierInfo* NSErrorII, |
| 44 | IdentifierInfo* CFErrorII); |
Ted Kremenek | cfdf9b4 | 2008-09-18 21:25:13 +0000 | [diff] [blame] | 45 | |
Ted Kremenek | cc9ac41 | 2008-10-01 23:24:09 +0000 | [diff] [blame] | 46 | bool CheckNSErrorArgument(QualType ArgTy, IdentifierInfo* NSErrorII); |
| 47 | bool CheckCFErrorArgument(QualType ArgTy, IdentifierInfo* CFErrorII); |
Ted Kremenek | cfdf9b4 | 2008-09-18 21:25:13 +0000 | [diff] [blame] | 48 | |
Ted Kremenek | 7360fda | 2008-09-18 23:09:54 +0000 | [diff] [blame] | 49 | void CheckParamDeref(VarDecl* V, GRStateRef state, GRExprEngine& Eng, |
Ted Kremenek | cc9ac41 | 2008-10-01 23:24:09 +0000 | [diff] [blame] | 50 | GRBugReporter& BR, bool isNErrorWarning); |
| 51 | |
| 52 | void EmitRetTyWarning(BugReporter& BR, Decl& CodeDecl, bool isNSErrorWarning); |
Ted Kremenek | 7360fda | 2008-09-18 23:09:54 +0000 | [diff] [blame] | 53 | |
| 54 | const char* desc; |
Ted Kremenek | cc9ac41 | 2008-10-01 23:24:09 +0000 | [diff] [blame] | 55 | const char* name; |
Ted Kremenek | cfdf9b4 | 2008-09-18 21:25:13 +0000 | [diff] [blame] | 56 | public: |
Ted Kremenek | 7360fda | 2008-09-18 23:09:54 +0000 | [diff] [blame] | 57 | NSErrorCheck() : desc(0) {} |
| 58 | |
Ted Kremenek | cfdf9b4 | 2008-09-18 21:25:13 +0000 | [diff] [blame] | 59 | void EmitWarnings(BugReporter& BR) { EmitGRWarnings(cast<GRBugReporter>(BR));} |
Ted Kremenek | cc9ac41 | 2008-10-01 23:24:09 +0000 | [diff] [blame] | 60 | const char* getName() const { return name; } |
Ted Kremenek | 7360fda | 2008-09-18 23:09:54 +0000 | [diff] [blame] | 61 | const char* getDescription() const { return desc; } |
Ted Kremenek | 62059e8 | 2008-09-21 06:57:40 +0000 | [diff] [blame] | 62 | const char* getCategory() const { return "Coding Conventions (Apple)"; } |
Ted Kremenek | cfdf9b4 | 2008-09-18 21:25:13 +0000 | [diff] [blame] | 63 | }; |
| 64 | |
| 65 | } // end anonymous namespace |
| 66 | |
| 67 | BugType* clang::CreateNSErrorCheck() { |
| 68 | return new NSErrorCheck(); |
| 69 | } |
| 70 | |
| 71 | void NSErrorCheck::EmitGRWarnings(GRBugReporter& BR) { |
| 72 | // Get the analysis engine and the exploded analysis graph. |
| 73 | GRExprEngine& Eng = BR.getEngine(); |
| 74 | GRExprEngine::GraphTy& G = Eng.getGraph(); |
| 75 | |
| 76 | // Get the declaration of the method/function that was analyzed. |
| 77 | Decl& CodeDecl = G.getCodeDecl(); |
Ted Kremenek | cc9ac41 | 2008-10-01 23:24:09 +0000 | [diff] [blame] | 78 | |
Ted Kremenek | cfdf9b4 | 2008-09-18 21:25:13 +0000 | [diff] [blame] | 79 | // Get the ASTContext, which is useful for querying type information. |
Ted Kremenek | f45d18c | 2008-09-18 06:33:41 +0000 | [diff] [blame] | 80 | ASTContext &Ctx = BR.getContext(); |
Ted Kremenek | cfdf9b4 | 2008-09-18 21:25:13 +0000 | [diff] [blame] | 81 | |
| 82 | QualType ResultTy; |
Ted Kremenek | cc9ac41 | 2008-10-01 23:24:09 +0000 | [diff] [blame] | 83 | llvm::SmallVector<VarDecl*, 5> NSErrorParams; |
| 84 | llvm::SmallVector<VarDecl*, 5> CFErrorParams; |
| 85 | |
| 86 | if (ObjCMethodDecl* MD = dyn_cast<ObjCMethodDecl>(&CodeDecl)) |
| 87 | CheckSignature(*MD, ResultTy, NSErrorParams, CFErrorParams, |
| 88 | &Ctx.Idents.get("NSError"), &Ctx.Idents.get("CFErrorRef")); |
| 89 | else if (FunctionDecl* FD = dyn_cast<FunctionDecl>(&CodeDecl)) |
| 90 | CheckSignature(*FD, ResultTy, NSErrorParams, CFErrorParams, |
| 91 | &Ctx.Idents.get("NSError"), &Ctx.Idents.get("CFErrorRef")); |
| 92 | else |
Ted Kremenek | cfdf9b4 | 2008-09-18 21:25:13 +0000 | [diff] [blame] | 93 | return; |
| 94 | |
Ted Kremenek | cc9ac41 | 2008-10-01 23:24:09 +0000 | [diff] [blame] | 95 | if (NSErrorParams.empty() && CFErrorParams.empty()) |
| 96 | return; |
| 97 | |
| 98 | if (ResultTy == Ctx.VoidTy) { |
| 99 | if (!NSErrorParams.empty()) |
| 100 | EmitRetTyWarning(BR, CodeDecl, true); |
| 101 | if (!CFErrorParams.empty()) |
| 102 | EmitRetTyWarning(BR, CodeDecl, false); |
Ted Kremenek | f45d18c | 2008-09-18 06:33:41 +0000 | [diff] [blame] | 103 | } |
Ted Kremenek | 7360fda | 2008-09-18 23:09:54 +0000 | [diff] [blame] | 104 | |
Ted Kremenek | cc9ac41 | 2008-10-01 23:24:09 +0000 | [diff] [blame] | 105 | for (GRExprEngine::GraphTy::roots_iterator RI=G.roots_begin(), |
| 106 | RE=G.roots_end(); RI!=RE; ++RI) { |
| 107 | |
| 108 | // Scan the NSError** parameters for an implicit null dereference. |
| 109 | for (llvm::SmallVectorImpl<VarDecl*>::iterator I=NSErrorParams.begin(), |
| 110 | E=NSErrorParams.end(); I!=E; ++I) |
| 111 | CheckParamDeref(*I, GRStateRef((*RI)->getState(), Eng.getStateManager()), |
| 112 | Eng, BR, true); |
| 113 | |
| 114 | // Scan the CFErrorRef* parameters for an implicit null dereference. |
| 115 | for (llvm::SmallVectorImpl<VarDecl*>::iterator I=CFErrorParams.begin(), |
| 116 | E=CFErrorParams.end(); I!=E; ++I) |
Ted Kremenek | 7360fda | 2008-09-18 23:09:54 +0000 | [diff] [blame] | 117 | CheckParamDeref(*I, GRStateRef((*RI)->getState(), Eng.getStateManager()), |
Ted Kremenek | cc9ac41 | 2008-10-01 23:24:09 +0000 | [diff] [blame] | 118 | Eng, BR, false); |
| 119 | } |
Ted Kremenek | f45d18c | 2008-09-18 06:33:41 +0000 | [diff] [blame] | 120 | } |
Ted Kremenek | cfdf9b4 | 2008-09-18 21:25:13 +0000 | [diff] [blame] | 121 | |
Ted Kremenek | cc9ac41 | 2008-10-01 23:24:09 +0000 | [diff] [blame] | 122 | void NSErrorCheck::EmitRetTyWarning(BugReporter& BR, Decl& CodeDecl, |
| 123 | bool isNSErrorWarning) { |
| 124 | |
| 125 | std::string msg; |
| 126 | llvm::raw_string_ostream os(msg); |
| 127 | |
| 128 | if (isa<ObjCMethodDecl>(CodeDecl)) |
| 129 | os << "Method"; |
| 130 | else |
| 131 | os << "Function"; |
| 132 | |
| 133 | os << " accepting "; |
| 134 | os << (isNSErrorWarning ? "NSError**" : "CFErrorRef*"); |
| 135 | os << " should have a non-void return value to indicate whether or not an " |
| 136 | "error occured."; |
| 137 | |
| 138 | BR.EmitBasicReport(isNSErrorWarning |
| 139 | ? "Bad return type when passing NSError**" |
| 140 | : "Bad return type when passing CFError*", |
| 141 | getCategory(), os.str().c_str(), CodeDecl.getLocation()); |
| 142 | } |
| 143 | |
| 144 | void |
| 145 | NSErrorCheck::CheckSignature(ObjCMethodDecl& M, QualType& ResultTy, |
| 146 | llvm::SmallVectorImpl<VarDecl*>& NSErrorParams, |
| 147 | llvm::SmallVectorImpl<VarDecl*>& CFErrorParams, |
| 148 | IdentifierInfo* NSErrorII, |
| 149 | IdentifierInfo* CFErrorII) { |
Ted Kremenek | cfdf9b4 | 2008-09-18 21:25:13 +0000 | [diff] [blame] | 150 | |
| 151 | ResultTy = M.getResultType(); |
| 152 | |
| 153 | for (ObjCMethodDecl::param_iterator I=M.param_begin(), |
Ted Kremenek | cc9ac41 | 2008-10-01 23:24:09 +0000 | [diff] [blame] | 154 | E=M.param_end(); I!=E; ++I) { |
| 155 | |
| 156 | QualType T = (*I)->getType(); |
| 157 | |
| 158 | if (CheckNSErrorArgument(T, NSErrorII)) |
| 159 | NSErrorParams.push_back(*I); |
| 160 | else if (CheckCFErrorArgument(T, CFErrorII)) |
| 161 | CFErrorParams.push_back(*I); |
| 162 | } |
Ted Kremenek | cfdf9b4 | 2008-09-18 21:25:13 +0000 | [diff] [blame] | 163 | } |
| 164 | |
Ted Kremenek | cc9ac41 | 2008-10-01 23:24:09 +0000 | [diff] [blame] | 165 | void |
| 166 | NSErrorCheck::CheckSignature(FunctionDecl& F, QualType& ResultTy, |
| 167 | llvm::SmallVectorImpl<VarDecl*>& NSErrorParams, |
| 168 | llvm::SmallVectorImpl<VarDecl*>& CFErrorParams, |
| 169 | IdentifierInfo* NSErrorII, |
| 170 | IdentifierInfo* CFErrorII) { |
| 171 | |
| 172 | ResultTy = F.getResultType(); |
| 173 | |
| 174 | for (FunctionDecl::param_iterator I=F.param_begin(), |
| 175 | E=F.param_end(); I!=E; ++I) { |
| 176 | |
| 177 | QualType T = (*I)->getType(); |
| 178 | |
| 179 | if (CheckNSErrorArgument(T, NSErrorII)) |
| 180 | NSErrorParams.push_back(*I); |
| 181 | else if (CheckCFErrorArgument(T, CFErrorII)) |
| 182 | CFErrorParams.push_back(*I); |
| 183 | } |
| 184 | } |
| 185 | |
| 186 | |
| 187 | bool NSErrorCheck::CheckNSErrorArgument(QualType ArgTy, |
| 188 | IdentifierInfo* NSErrorII) { |
| 189 | |
Ted Kremenek | cfdf9b4 | 2008-09-18 21:25:13 +0000 | [diff] [blame] | 190 | const PointerType* PPT = ArgTy->getAsPointerType(); |
| 191 | if (!PPT) return false; |
| 192 | |
| 193 | const PointerType* PT = PPT->getPointeeType()->getAsPointerType(); |
| 194 | if (!PT) return false; |
| 195 | |
| 196 | const ObjCInterfaceType *IT = |
| 197 | PT->getPointeeType()->getAsObjCInterfaceType(); |
| 198 | |
| 199 | if (!IT) return false; |
| 200 | return IT->getDecl()->getIdentifier() == NSErrorII; |
| 201 | } |
Ted Kremenek | 7360fda | 2008-09-18 23:09:54 +0000 | [diff] [blame] | 202 | |
Ted Kremenek | cc9ac41 | 2008-10-01 23:24:09 +0000 | [diff] [blame] | 203 | bool NSErrorCheck::CheckCFErrorArgument(QualType ArgTy, |
| 204 | IdentifierInfo* CFErrorII) { |
| 205 | |
| 206 | const PointerType* PPT = ArgTy->getAsPointerType(); |
| 207 | if (!PPT) return false; |
| 208 | |
| 209 | const TypedefType* TT = PPT->getPointeeType()->getAsTypedefType(); |
| 210 | if (!TT) return false; |
| 211 | |
| 212 | return TT->getDecl()->getIdentifier() == CFErrorII; |
| 213 | } |
| 214 | |
Ted Kremenek | 7360fda | 2008-09-18 23:09:54 +0000 | [diff] [blame] | 215 | void NSErrorCheck::CheckParamDeref(VarDecl* Param, GRStateRef rootState, |
Ted Kremenek | cc9ac41 | 2008-10-01 23:24:09 +0000 | [diff] [blame] | 216 | GRExprEngine& Eng, GRBugReporter& BR, |
| 217 | bool isNSErrorWarning) { |
Ted Kremenek | 7360fda | 2008-09-18 23:09:54 +0000 | [diff] [blame] | 218 | |
Ted Kremenek | 9e24049 | 2008-10-04 05:50:14 +0000 | [diff] [blame] | 219 | RVal ParamRVal = rootState.GetRVal(Eng.getLVal(Param)); |
Ted Kremenek | 7360fda | 2008-09-18 23:09:54 +0000 | [diff] [blame] | 220 | |
| 221 | // FIXME: For now assume that ParamRVal is symbolic. We need to generalize |
| 222 | // this later. |
| 223 | lval::SymbolVal* SV = dyn_cast<lval::SymbolVal>(&ParamRVal); |
| 224 | if (!SV) return; |
| 225 | |
| 226 | // Iterate over the implicit-null dereferences. |
| 227 | for (GRExprEngine::null_deref_iterator I=Eng.implicit_null_derefs_begin(), |
| 228 | E=Eng.implicit_null_derefs_end(); I!=E; ++I) { |
| 229 | |
| 230 | GRStateRef state = GRStateRef((*I)->getState(), Eng.getStateManager()); |
| 231 | const RVal* X = state.get<GRState::NullDerefTag>(); |
| 232 | const lval::SymbolVal* SVX = dyn_cast_or_null<lval::SymbolVal>(X); |
| 233 | if (!SVX || SVX->getSymbol() != SV->getSymbol()) continue; |
| 234 | |
| 235 | // Emit an error. |
| 236 | BugReport R(*this, *I); |
Ted Kremenek | cc9ac41 | 2008-10-01 23:24:09 +0000 | [diff] [blame] | 237 | |
| 238 | name = isNSErrorWarning ? "NSError** null dereference" |
| 239 | : "CFErrorRef* null dereference"; |
Ted Kremenek | 7360fda | 2008-09-18 23:09:54 +0000 | [diff] [blame] | 240 | |
| 241 | std::string msg; |
| 242 | llvm::raw_string_ostream os(msg); |
Ted Kremenek | cc9ac41 | 2008-10-01 23:24:09 +0000 | [diff] [blame] | 243 | os << "Potential null dereference. According to coding standards "; |
| 244 | |
| 245 | if (isNSErrorWarning) |
| 246 | os << "in 'Creating and Returning NSError Objects' the parameter '"; |
| 247 | else |
| 248 | os << "documented in CoreFoundation/CFError.h the parameter '"; |
| 249 | |
| 250 | os << Param->getName() << "' may be null."; |
Ted Kremenek | 7360fda | 2008-09-18 23:09:54 +0000 | [diff] [blame] | 251 | desc = os.str().c_str(); |
| 252 | |
Ted Kremenek | 37fc826 | 2008-09-18 23:23:19 +0000 | [diff] [blame] | 253 | BR.addNotableSymbol(SV->getSymbol()); |
Ted Kremenek | cc9ac41 | 2008-10-01 23:24:09 +0000 | [diff] [blame] | 254 | BR.EmitWarning(R); |
Ted Kremenek | 7360fda | 2008-09-18 23:09:54 +0000 | [diff] [blame] | 255 | } |
| 256 | } |