Ted Kremenek | 29e0ef2 | 2009-11-03 06:59:59 +0000 | [diff] [blame] | 1 | //=- NSErrorCheckerer.cpp - Coding conventions for uses of NSError -*- C++ -*-==// |
Ted Kremenek | f45d18c | 2008-09-18 06:33:41 +0000 | [diff] [blame] | 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" |
Ted Kremenek | dc998c1 | 2009-11-03 18:41:06 +0000 | [diff] [blame] | 21 | #include "clang/Analysis/PathSensitive/Checkers/DereferenceChecker.h" |
Ted Kremenek | cfdf9b4 | 2008-09-18 21:25:13 +0000 | [diff] [blame] | 22 | #include "BasicObjCFoundationChecks.h" |
| 23 | #include "llvm/Support/Compiler.h" |
Ted Kremenek | f45d18c | 2008-09-18 06:33:41 +0000 | [diff] [blame] | 24 | #include "clang/AST/DeclObjC.h" |
Ted Kremenek | cfdf9b4 | 2008-09-18 21:25:13 +0000 | [diff] [blame] | 25 | #include "clang/AST/Decl.h" |
| 26 | #include "llvm/ADT/SmallVector.h" |
Ted Kremenek | f45d18c | 2008-09-18 06:33:41 +0000 | [diff] [blame] | 27 | |
| 28 | using namespace clang; |
| 29 | |
Ted Kremenek | cfdf9b4 | 2008-09-18 21:25:13 +0000 | [diff] [blame] | 30 | namespace { |
Ted Kremenek | 29e0ef2 | 2009-11-03 06:59:59 +0000 | [diff] [blame] | 31 | class VISIBILITY_HIDDEN NSErrorChecker : public BugType { |
Zhongxing Xu | 5ab128b | 2009-08-21 02:18:44 +0000 | [diff] [blame] | 32 | const Decl &CodeDecl; |
Ted Kremenek | cf118d4 | 2009-02-04 23:49:09 +0000 | [diff] [blame] | 33 | const bool isNSErrorWarning; |
| 34 | IdentifierInfo * const II; |
| 35 | GRExprEngine &Eng; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 36 | |
Zhongxing Xu | 5ab128b | 2009-08-21 02:18:44 +0000 | [diff] [blame] | 37 | void CheckSignature(const ObjCMethodDecl& MD, QualType& ResultTy, |
Ted Kremenek | cf118d4 | 2009-02-04 23:49:09 +0000 | [diff] [blame] | 38 | llvm::SmallVectorImpl<VarDecl*>& ErrorParams); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 39 | |
Zhongxing Xu | 5ab128b | 2009-08-21 02:18:44 +0000 | [diff] [blame] | 40 | void CheckSignature(const FunctionDecl& MD, QualType& ResultTy, |
Ted Kremenek | cf118d4 | 2009-02-04 23:49:09 +0000 | [diff] [blame] | 41 | llvm::SmallVectorImpl<VarDecl*>& ErrorParams); |
Ted Kremenek | cfdf9b4 | 2008-09-18 21:25:13 +0000 | [diff] [blame] | 42 | |
Ted Kremenek | cf118d4 | 2009-02-04 23:49:09 +0000 | [diff] [blame] | 43 | bool CheckNSErrorArgument(QualType ArgTy); |
| 44 | bool CheckCFErrorArgument(QualType ArgTy); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 45 | |
Ted Kremenek | d17da2b | 2009-08-21 22:28:32 +0000 | [diff] [blame] | 46 | void CheckParamDeref(const VarDecl *V, const LocationContext *LC, |
| 47 | const GRState *state, BugReporter& BR); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 48 | |
Zhongxing Xu | 5ab128b | 2009-08-21 02:18:44 +0000 | [diff] [blame] | 49 | void EmitRetTyWarning(BugReporter& BR, const Decl& CodeDecl); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 50 | |
Ted Kremenek | cfdf9b4 | 2008-09-18 21:25:13 +0000 | [diff] [blame] | 51 | public: |
Ted Kremenek | 29e0ef2 | 2009-11-03 06:59:59 +0000 | [diff] [blame] | 52 | NSErrorChecker(const Decl &D, bool isNSError, GRExprEngine& eng) |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 53 | : BugType(isNSError ? "NSError** null dereference" |
Zhongxing Xu | 5ab128b | 2009-08-21 02:18:44 +0000 | [diff] [blame] | 54 | : "CFErrorRef* null dereference", |
Ted Kremenek | 27a36e9 | 2009-09-01 00:17:12 +0000 | [diff] [blame] | 55 | "Coding conventions (Apple)"), |
Zhongxing Xu | 5ab128b | 2009-08-21 02:18:44 +0000 | [diff] [blame] | 56 | CodeDecl(D), |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 57 | isNSErrorWarning(isNSError), |
Ted Kremenek | cf118d4 | 2009-02-04 23:49:09 +0000 | [diff] [blame] | 58 | II(&eng.getContext().Idents.get(isNSErrorWarning ? "NSError":"CFErrorRef")), |
| 59 | Eng(eng) {} |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 60 | |
Ted Kremenek | cf118d4 | 2009-02-04 23:49:09 +0000 | [diff] [blame] | 61 | void FlushReports(BugReporter& BR); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 62 | }; |
| 63 | |
Ted Kremenek | cfdf9b4 | 2008-09-18 21:25:13 +0000 | [diff] [blame] | 64 | } // end anonymous namespace |
| 65 | |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 66 | void clang::RegisterNSErrorChecks(BugReporter& BR, GRExprEngine &Eng, |
Zhongxing Xu | 5ab128b | 2009-08-21 02:18:44 +0000 | [diff] [blame] | 67 | const Decl &D) { |
Ted Kremenek | 29e0ef2 | 2009-11-03 06:59:59 +0000 | [diff] [blame] | 68 | BR.Register(new NSErrorChecker(D, true, Eng)); |
| 69 | BR.Register(new NSErrorChecker(D, false, Eng)); |
Ted Kremenek | cfdf9b4 | 2008-09-18 21:25:13 +0000 | [diff] [blame] | 70 | } |
| 71 | |
Ted Kremenek | 29e0ef2 | 2009-11-03 06:59:59 +0000 | [diff] [blame] | 72 | void NSErrorChecker::FlushReports(BugReporter& BR) { |
Ted Kremenek | cfdf9b4 | 2008-09-18 21:25:13 +0000 | [diff] [blame] | 73 | // Get the analysis engine and the exploded analysis graph. |
Zhongxing Xu | 031ccc0 | 2009-08-06 12:48:26 +0000 | [diff] [blame] | 74 | ExplodedGraph& G = Eng.getGraph(); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 75 | |
Ted Kremenek | cfdf9b4 | 2008-09-18 21:25:13 +0000 | [diff] [blame] | 76 | // Get the ASTContext, which is useful for querying type information. |
Ted Kremenek | f45d18c | 2008-09-18 06:33:41 +0000 | [diff] [blame] | 77 | ASTContext &Ctx = BR.getContext(); |
Ted Kremenek | cfdf9b4 | 2008-09-18 21:25:13 +0000 | [diff] [blame] | 78 | |
| 79 | QualType ResultTy; |
Ted Kremenek | cf118d4 | 2009-02-04 23:49:09 +0000 | [diff] [blame] | 80 | llvm::SmallVector<VarDecl*, 5> ErrorParams; |
Ted Kremenek | cc9ac41 | 2008-10-01 23:24:09 +0000 | [diff] [blame] | 81 | |
Zhongxing Xu | 5ab128b | 2009-08-21 02:18:44 +0000 | [diff] [blame] | 82 | if (const ObjCMethodDecl* MD = dyn_cast<ObjCMethodDecl>(&CodeDecl)) |
Ted Kremenek | cf118d4 | 2009-02-04 23:49:09 +0000 | [diff] [blame] | 83 | CheckSignature(*MD, ResultTy, ErrorParams); |
Zhongxing Xu | 5ab128b | 2009-08-21 02:18:44 +0000 | [diff] [blame] | 84 | else if (const FunctionDecl* FD = dyn_cast<FunctionDecl>(&CodeDecl)) |
Ted Kremenek | cf118d4 | 2009-02-04 23:49:09 +0000 | [diff] [blame] | 85 | CheckSignature(*FD, ResultTy, ErrorParams); |
Ted Kremenek | cc9ac41 | 2008-10-01 23:24:09 +0000 | [diff] [blame] | 86 | else |
Ted Kremenek | cfdf9b4 | 2008-09-18 21:25:13 +0000 | [diff] [blame] | 87 | return; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 88 | |
Ted Kremenek | cf118d4 | 2009-02-04 23:49:09 +0000 | [diff] [blame] | 89 | if (ErrorParams.empty()) |
Ted Kremenek | cc9ac41 | 2008-10-01 23:24:09 +0000 | [diff] [blame] | 90 | return; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 91 | |
Ted Kremenek | cf118d4 | 2009-02-04 23:49:09 +0000 | [diff] [blame] | 92 | if (ResultTy == Ctx.VoidTy) EmitRetTyWarning(BR, CodeDecl); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 93 | |
| 94 | for (ExplodedGraph::roots_iterator RI=G.roots_begin(), RE=G.roots_end(); |
Zhongxing Xu | 031ccc0 | 2009-08-06 12:48:26 +0000 | [diff] [blame] | 95 | RI!=RE; ++RI) { |
Ted Kremenek | cf118d4 | 2009-02-04 23:49:09 +0000 | [diff] [blame] | 96 | // Scan the parameters for an implicit null dereference. |
| 97 | for (llvm::SmallVectorImpl<VarDecl*>::iterator I=ErrorParams.begin(), |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 98 | E=ErrorParams.end(); I!=E; ++I) |
Ted Kremenek | d17da2b | 2009-08-21 22:28:32 +0000 | [diff] [blame] | 99 | CheckParamDeref(*I, (*RI)->getLocationContext(), (*RI)->getState(), BR); |
Ted Kremenek | cc9ac41 | 2008-10-01 23:24:09 +0000 | [diff] [blame] | 100 | } |
Ted Kremenek | f45d18c | 2008-09-18 06:33:41 +0000 | [diff] [blame] | 101 | } |
Ted Kremenek | cfdf9b4 | 2008-09-18 21:25:13 +0000 | [diff] [blame] | 102 | |
Ted Kremenek | 29e0ef2 | 2009-11-03 06:59:59 +0000 | [diff] [blame] | 103 | void NSErrorChecker::EmitRetTyWarning(BugReporter& BR, const Decl& CodeDecl) { |
Ted Kremenek | cf118d4 | 2009-02-04 23:49:09 +0000 | [diff] [blame] | 104 | std::string sbuf; |
| 105 | llvm::raw_string_ostream os(sbuf); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 106 | |
Ted Kremenek | cc9ac41 | 2008-10-01 23:24:09 +0000 | [diff] [blame] | 107 | if (isa<ObjCMethodDecl>(CodeDecl)) |
| 108 | os << "Method"; |
| 109 | else |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 110 | os << "Function"; |
| 111 | |
Ted Kremenek | cc9ac41 | 2008-10-01 23:24:09 +0000 | [diff] [blame] | 112 | os << " accepting "; |
| 113 | os << (isNSErrorWarning ? "NSError**" : "CFErrorRef*"); |
| 114 | os << " should have a non-void return value to indicate whether or not an " |
Ted Kremenek | 355a692 | 2009-08-06 05:01:36 +0000 | [diff] [blame] | 115 | "error occurred"; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 116 | |
Ted Kremenek | cc9ac41 | 2008-10-01 23:24:09 +0000 | [diff] [blame] | 117 | BR.EmitBasicReport(isNSErrorWarning |
| 118 | ? "Bad return type when passing NSError**" |
| 119 | : "Bad return type when passing CFError*", |
Ted Kremenek | cf118d4 | 2009-02-04 23:49:09 +0000 | [diff] [blame] | 120 | getCategory().c_str(), os.str().c_str(), |
| 121 | CodeDecl.getLocation()); |
Ted Kremenek | cc9ac41 | 2008-10-01 23:24:09 +0000 | [diff] [blame] | 122 | } |
| 123 | |
| 124 | void |
Ted Kremenek | 29e0ef2 | 2009-11-03 06:59:59 +0000 | [diff] [blame] | 125 | NSErrorChecker::CheckSignature(const ObjCMethodDecl& M, QualType& ResultTy, |
Ted Kremenek | cf118d4 | 2009-02-04 23:49:09 +0000 | [diff] [blame] | 126 | llvm::SmallVectorImpl<VarDecl*>& ErrorParams) { |
Ted Kremenek | cfdf9b4 | 2008-09-18 21:25:13 +0000 | [diff] [blame] | 127 | |
| 128 | ResultTy = M.getResultType(); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 129 | |
| 130 | for (ObjCMethodDecl::param_iterator I=M.param_begin(), |
Ted Kremenek | cc9ac41 | 2008-10-01 23:24:09 +0000 | [diff] [blame] | 131 | E=M.param_end(); I!=E; ++I) { |
| 132 | |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 133 | QualType T = (*I)->getType(); |
Ted Kremenek | cc9ac41 | 2008-10-01 23:24:09 +0000 | [diff] [blame] | 134 | |
Ted Kremenek | cf118d4 | 2009-02-04 23:49:09 +0000 | [diff] [blame] | 135 | if (isNSErrorWarning) { |
| 136 | if (CheckNSErrorArgument(T)) ErrorParams.push_back(*I); |
| 137 | } |
| 138 | else if (CheckCFErrorArgument(T)) |
| 139 | ErrorParams.push_back(*I); |
Ted Kremenek | cc9ac41 | 2008-10-01 23:24:09 +0000 | [diff] [blame] | 140 | } |
Ted Kremenek | cfdf9b4 | 2008-09-18 21:25:13 +0000 | [diff] [blame] | 141 | } |
| 142 | |
Ted Kremenek | cc9ac41 | 2008-10-01 23:24:09 +0000 | [diff] [blame] | 143 | void |
Ted Kremenek | 29e0ef2 | 2009-11-03 06:59:59 +0000 | [diff] [blame] | 144 | NSErrorChecker::CheckSignature(const FunctionDecl& F, QualType& ResultTy, |
Ted Kremenek | cf118d4 | 2009-02-04 23:49:09 +0000 | [diff] [blame] | 145 | llvm::SmallVectorImpl<VarDecl*>& ErrorParams) { |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 146 | |
Ted Kremenek | cc9ac41 | 2008-10-01 23:24:09 +0000 | [diff] [blame] | 147 | ResultTy = F.getResultType(); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 148 | |
| 149 | for (FunctionDecl::param_const_iterator I = F.param_begin(), |
Zhongxing Xu | 5ab128b | 2009-08-21 02:18:44 +0000 | [diff] [blame] | 150 | E = F.param_end(); I != E; ++I) { |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 151 | |
| 152 | QualType T = (*I)->getType(); |
| 153 | |
Ted Kremenek | cf118d4 | 2009-02-04 23:49:09 +0000 | [diff] [blame] | 154 | if (isNSErrorWarning) { |
| 155 | if (CheckNSErrorArgument(T)) ErrorParams.push_back(*I); |
| 156 | } |
| 157 | else if (CheckCFErrorArgument(T)) |
| 158 | ErrorParams.push_back(*I); |
Ted Kremenek | cc9ac41 | 2008-10-01 23:24:09 +0000 | [diff] [blame] | 159 | } |
| 160 | } |
| 161 | |
| 162 | |
Ted Kremenek | 29e0ef2 | 2009-11-03 06:59:59 +0000 | [diff] [blame] | 163 | bool NSErrorChecker::CheckNSErrorArgument(QualType ArgTy) { |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 164 | |
Ted Kremenek | 6217b80 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 165 | const PointerType* PPT = ArgTy->getAs<PointerType>(); |
Steve Naroff | 14108da | 2009-07-10 23:34:53 +0000 | [diff] [blame] | 166 | if (!PPT) |
| 167 | return false; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 168 | |
Steve Naroff | 14108da | 2009-07-10 23:34:53 +0000 | [diff] [blame] | 169 | const ObjCObjectPointerType* PT = |
John McCall | 183700f | 2009-09-21 23:43:11 +0000 | [diff] [blame] | 170 | PPT->getPointeeType()->getAs<ObjCObjectPointerType>(); |
Steve Naroff | 14108da | 2009-07-10 23:34:53 +0000 | [diff] [blame] | 171 | |
| 172 | if (!PT) |
| 173 | return false; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 174 | |
Steve Naroff | 14108da | 2009-07-10 23:34:53 +0000 | [diff] [blame] | 175 | const ObjCInterfaceDecl *ID = PT->getInterfaceDecl(); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 176 | |
Steve Naroff | 14108da | 2009-07-10 23:34:53 +0000 | [diff] [blame] | 177 | // FIXME: Can ID ever be NULL? |
| 178 | if (ID) |
| 179 | return II == ID->getIdentifier(); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 180 | |
Steve Naroff | 14108da | 2009-07-10 23:34:53 +0000 | [diff] [blame] | 181 | return false; |
Ted Kremenek | cfdf9b4 | 2008-09-18 21:25:13 +0000 | [diff] [blame] | 182 | } |
Ted Kremenek | 7360fda | 2008-09-18 23:09:54 +0000 | [diff] [blame] | 183 | |
Ted Kremenek | 29e0ef2 | 2009-11-03 06:59:59 +0000 | [diff] [blame] | 184 | bool NSErrorChecker::CheckCFErrorArgument(QualType ArgTy) { |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 185 | |
Ted Kremenek | 6217b80 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 186 | const PointerType* PPT = ArgTy->getAs<PointerType>(); |
Ted Kremenek | cc9ac41 | 2008-10-01 23:24:09 +0000 | [diff] [blame] | 187 | if (!PPT) return false; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 188 | |
John McCall | 183700f | 2009-09-21 23:43:11 +0000 | [diff] [blame] | 189 | const TypedefType* TT = PPT->getPointeeType()->getAs<TypedefType>(); |
Ted Kremenek | cc9ac41 | 2008-10-01 23:24:09 +0000 | [diff] [blame] | 190 | if (!TT) return false; |
| 191 | |
Ted Kremenek | cf118d4 | 2009-02-04 23:49:09 +0000 | [diff] [blame] | 192 | return TT->getDecl()->getIdentifier() == II; |
Ted Kremenek | cc9ac41 | 2008-10-01 23:24:09 +0000 | [diff] [blame] | 193 | } |
| 194 | |
Ted Kremenek | 29e0ef2 | 2009-11-03 06:59:59 +0000 | [diff] [blame] | 195 | void NSErrorChecker::CheckParamDeref(const VarDecl *Param, |
Ted Kremenek | d17da2b | 2009-08-21 22:28:32 +0000 | [diff] [blame] | 196 | const LocationContext *LC, |
| 197 | const GRState *rootState, |
Ted Kremenek | cf118d4 | 2009-02-04 23:49:09 +0000 | [diff] [blame] | 198 | BugReporter& BR) { |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 199 | |
Ted Kremenek | d17da2b | 2009-08-21 22:28:32 +0000 | [diff] [blame] | 200 | SVal ParamL = rootState->getLValue(Param, LC); |
Ted Kremenek | 993f1c7 | 2008-10-17 20:28:54 +0000 | [diff] [blame] | 201 | const MemRegion* ParamR = cast<loc::MemRegionVal>(ParamL).getRegionAs<VarRegion>(); |
| 202 | assert (ParamR && "Parameters always have VarRegions."); |
Ted Kremenek | c878138 | 2009-06-17 22:28:13 +0000 | [diff] [blame] | 203 | SVal ParamSVal = rootState->getSVal(ParamR); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 204 | |
Zhongxing Xu | 1c96b24 | 2008-10-17 05:57:07 +0000 | [diff] [blame] | 205 | // FIXME: For now assume that ParamSVal is symbolic. We need to generalize |
Ted Kremenek | 7360fda | 2008-09-18 23:09:54 +0000 | [diff] [blame] | 206 | // this later. |
Ted Kremenek | 93e7145 | 2009-03-30 19:53:37 +0000 | [diff] [blame] | 207 | SymbolRef ParamSym = ParamSVal.getAsLocSymbol(); |
| 208 | if (!ParamSym) |
| 209 | return; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 210 | |
Ted Kremenek | 7360fda | 2008-09-18 23:09:54 +0000 | [diff] [blame] | 211 | // Iterate over the implicit-null dereferences. |
Ted Kremenek | b4b817d | 2009-11-11 03:26:34 +0000 | [diff] [blame^] | 212 | ExplodedNode *const* I, *const* E; |
| 213 | llvm::tie(I, E) = GetImplicitNullDereferences(Eng); |
| 214 | for ( ; I != E; ++I) { |
Ted Kremenek | c878138 | 2009-06-17 22:28:13 +0000 | [diff] [blame] | 215 | const GRState *state = (*I)->getState(); |
Ted Kremenek | b4b817d | 2009-11-11 03:26:34 +0000 | [diff] [blame^] | 216 | SVal location = state->getSVal((*I)->getLocationAs<StmtPoint>()->getStmt()); |
| 217 | if (location.getAsSymbol() != ParamSym) |
Ted Kremenek | 93e7145 | 2009-03-30 19:53:37 +0000 | [diff] [blame] | 218 | continue; |
Ted Kremenek | 7360fda | 2008-09-18 23:09:54 +0000 | [diff] [blame] | 219 | |
| 220 | // Emit an error. |
Ted Kremenek | cf118d4 | 2009-02-04 23:49:09 +0000 | [diff] [blame] | 221 | std::string sbuf; |
| 222 | llvm::raw_string_ostream os(sbuf); |
Ted Kremenek | cc9ac41 | 2008-10-01 23:24:09 +0000 | [diff] [blame] | 223 | os << "Potential null dereference. According to coding standards "; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 224 | |
Ted Kremenek | cc9ac41 | 2008-10-01 23:24:09 +0000 | [diff] [blame] | 225 | if (isNSErrorWarning) |
| 226 | os << "in 'Creating and Returning NSError Objects' the parameter '"; |
| 227 | else |
| 228 | os << "documented in CoreFoundation/CFError.h the parameter '"; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 229 | |
Chris Lattner | d9d22dd | 2008-11-24 05:29:24 +0000 | [diff] [blame] | 230 | os << Param->getNameAsString() << "' may be null."; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 231 | |
Ted Kremenek | cf118d4 | 2009-02-04 23:49:09 +0000 | [diff] [blame] | 232 | BugReport *report = new BugReport(*this, os.str().c_str(), *I); |
| 233 | // FIXME: Notable symbols are now part of the report. We should |
| 234 | // add support for notable symbols in BugReport. |
| 235 | // BR.addNotableSymbol(SV->getSymbol()); |
| 236 | BR.EmitReport(report); |
Ted Kremenek | 7360fda | 2008-09-18 23:09:54 +0000 | [diff] [blame] | 237 | } |
| 238 | } |