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