Kristof Umann | 30f0865 | 2018-06-18 11:50:17 +0000 | [diff] [blame] | 1 | //===----- UninitializedObjectChecker.cpp ------------------------*- 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 checker that reports uninitialized fields in objects |
| 11 | // created after a constructor call. |
| 12 | // |
Kristof Umann | 6cec6c4 | 2018-09-14 09:39:26 +0000 | [diff] [blame^] | 13 | // To read about command line options and how the checker works, refer to the |
| 14 | // top of the file and inline comments in UninitializedObject.h. |
Kristof Umann | 56963ae | 2018-08-13 18:17:05 +0000 | [diff] [blame] | 15 | // |
| 16 | // Some of the logic is implemented in UninitializedPointee.cpp, to reduce the |
| 17 | // complexity of this file. |
| 18 | // |
Kristof Umann | 30f0865 | 2018-06-18 11:50:17 +0000 | [diff] [blame] | 19 | //===----------------------------------------------------------------------===// |
| 20 | |
Richard Smith | 651d683 | 2018-08-13 22:07:11 +0000 | [diff] [blame] | 21 | #include "../ClangSACheckers.h" |
Kristof Umann | a37bba4 | 2018-08-13 18:22:22 +0000 | [diff] [blame] | 22 | #include "UninitializedObject.h" |
Kristof Umann | 30f0865 | 2018-06-18 11:50:17 +0000 | [diff] [blame] | 23 | #include "clang/StaticAnalyzer/Core/BugReporter/BugType.h" |
| 24 | #include "clang/StaticAnalyzer/Core/Checker.h" |
| 25 | #include "clang/StaticAnalyzer/Core/PathSensitive/CheckerContext.h" |
Kristof Umann | ef9af05 | 2018-08-08 13:18:53 +0000 | [diff] [blame] | 26 | #include "clang/StaticAnalyzer/Core/PathSensitive/DynamicTypeMap.h" |
Kristof Umann | 30f0865 | 2018-06-18 11:50:17 +0000 | [diff] [blame] | 27 | |
| 28 | using namespace clang; |
| 29 | using namespace clang::ento; |
| 30 | |
| 31 | namespace { |
| 32 | |
| 33 | class UninitializedObjectChecker : public Checker<check::EndFunction> { |
| 34 | std::unique_ptr<BuiltinBug> BT_uninitField; |
| 35 | |
| 36 | public: |
Kristof Umann | 6cec6c4 | 2018-09-14 09:39:26 +0000 | [diff] [blame^] | 37 | // The fields of this struct will be initialized when registering the checker. |
| 38 | UninitObjCheckerOptions Opts; |
Kristof Umann | 30f0865 | 2018-06-18 11:50:17 +0000 | [diff] [blame] | 39 | |
| 40 | UninitializedObjectChecker() |
| 41 | : BT_uninitField(new BuiltinBug(this, "Uninitialized fields")) {} |
Reka Kovacs | ed8c05c | 2018-07-16 20:47:45 +0000 | [diff] [blame] | 42 | void checkEndFunction(const ReturnStmt *RS, CheckerContext &C) const; |
Kristof Umann | 30f0865 | 2018-06-18 11:50:17 +0000 | [diff] [blame] | 43 | }; |
| 44 | |
Kristof Umann | 015b059 | 2018-08-13 18:43:08 +0000 | [diff] [blame] | 45 | /// A basic field type, that is not a pointer or a reference, it's dynamic and |
| 46 | /// static type is the same. |
Richard Smith | 651d683 | 2018-08-13 22:07:11 +0000 | [diff] [blame] | 47 | class RegularField final : public FieldNode { |
Kristof Umann | 015b059 | 2018-08-13 18:43:08 +0000 | [diff] [blame] | 48 | public: |
| 49 | RegularField(const FieldRegion *FR) : FieldNode(FR) {} |
| 50 | |
| 51 | virtual void printNoteMsg(llvm::raw_ostream &Out) const override { |
| 52 | Out << "uninitialized field "; |
| 53 | } |
| 54 | |
| 55 | virtual void printPrefix(llvm::raw_ostream &Out) const override {} |
| 56 | |
Richard Smith | 651d683 | 2018-08-13 22:07:11 +0000 | [diff] [blame] | 57 | virtual void printNode(llvm::raw_ostream &Out) const override { |
Kristof Umann | 015b059 | 2018-08-13 18:43:08 +0000 | [diff] [blame] | 58 | Out << getVariableName(getDecl()); |
| 59 | } |
| 60 | |
| 61 | virtual void printSeparator(llvm::raw_ostream &Out) const override { |
| 62 | Out << '.'; |
| 63 | } |
| 64 | }; |
| 65 | |
Kristof Umann | b59b45e | 2018-08-21 12:16:59 +0000 | [diff] [blame] | 66 | /// Represents that the FieldNode that comes after this is declared in a base |
Kristof Umann | ceb5f65 | 2018-09-14 09:07:40 +0000 | [diff] [blame] | 67 | /// of the previous FieldNode. As such, this descendant doesn't wrap a |
| 68 | /// FieldRegion, and is purely a tool to describe a relation between two other |
| 69 | /// FieldRegion wrapping descendants. |
Kristof Umann | b59b45e | 2018-08-21 12:16:59 +0000 | [diff] [blame] | 70 | class BaseClass final : public FieldNode { |
| 71 | const QualType BaseClassT; |
| 72 | |
| 73 | public: |
| 74 | BaseClass(const QualType &T) : FieldNode(nullptr), BaseClassT(T) { |
| 75 | assert(!T.isNull()); |
| 76 | assert(T->getAsCXXRecordDecl()); |
| 77 | } |
| 78 | |
| 79 | virtual void printNoteMsg(llvm::raw_ostream &Out) const override { |
| 80 | llvm_unreachable("This node can never be the final node in the " |
| 81 | "fieldchain!"); |
| 82 | } |
| 83 | |
| 84 | virtual void printPrefix(llvm::raw_ostream &Out) const override {} |
| 85 | |
| 86 | virtual void printNode(llvm::raw_ostream &Out) const override { |
| 87 | Out << BaseClassT->getAsCXXRecordDecl()->getName() << "::"; |
| 88 | } |
| 89 | |
| 90 | virtual void printSeparator(llvm::raw_ostream &Out) const override {} |
| 91 | |
Kristof Umann | 06209cb | 2018-08-21 15:09:22 +0000 | [diff] [blame] | 92 | virtual bool isBase() const override { return true; } |
Kristof Umann | b59b45e | 2018-08-21 12:16:59 +0000 | [diff] [blame] | 93 | }; |
| 94 | |
Kristof Umann | cc85244 | 2018-07-12 13:13:46 +0000 | [diff] [blame] | 95 | } // end of anonymous namespace |
| 96 | |
Kristof Umann | 30f0865 | 2018-06-18 11:50:17 +0000 | [diff] [blame] | 97 | // Utility function declarations. |
| 98 | |
| 99 | /// Returns the object that was constructed by CtorDecl, or None if that isn't |
| 100 | /// possible. |
Kristof Umann | 0735cfb | 2018-08-08 12:23:02 +0000 | [diff] [blame] | 101 | // TODO: Refactor this function so that it returns the constructed object's |
| 102 | // region. |
Kristof Umann | cc85244 | 2018-07-12 13:13:46 +0000 | [diff] [blame] | 103 | static Optional<nonloc::LazyCompoundVal> |
Kristof Umann | 30f0865 | 2018-06-18 11:50:17 +0000 | [diff] [blame] | 104 | getObjectVal(const CXXConstructorDecl *CtorDecl, CheckerContext &Context); |
| 105 | |
Kristof Umann | 0735cfb | 2018-08-08 12:23:02 +0000 | [diff] [blame] | 106 | /// Checks whether the object constructed by \p Ctor will be analyzed later |
| 107 | /// (e.g. if the object is a field of another object, in which case we'd check |
| 108 | /// it multiple times). |
| 109 | static bool willObjectBeAnalyzedLater(const CXXConstructorDecl *Ctor, |
Kristof Umann | a37bba4 | 2018-08-13 18:22:22 +0000 | [diff] [blame] | 110 | CheckerContext &Context); |
Kristof Umann | 30f0865 | 2018-06-18 11:50:17 +0000 | [diff] [blame] | 111 | |
Kristof Umann | 30f0865 | 2018-06-18 11:50:17 +0000 | [diff] [blame] | 112 | //===----------------------------------------------------------------------===// |
| 113 | // Methods for UninitializedObjectChecker. |
| 114 | //===----------------------------------------------------------------------===// |
| 115 | |
| 116 | void UninitializedObjectChecker::checkEndFunction( |
Reka Kovacs | ed8c05c | 2018-07-16 20:47:45 +0000 | [diff] [blame] | 117 | const ReturnStmt *RS, CheckerContext &Context) const { |
Kristof Umann | 30f0865 | 2018-06-18 11:50:17 +0000 | [diff] [blame] | 118 | |
| 119 | const auto *CtorDecl = dyn_cast_or_null<CXXConstructorDecl>( |
| 120 | Context.getLocationContext()->getDecl()); |
| 121 | if (!CtorDecl) |
| 122 | return; |
| 123 | |
| 124 | if (!CtorDecl->isUserProvided()) |
| 125 | return; |
| 126 | |
| 127 | if (CtorDecl->getParent()->isUnion()) |
| 128 | return; |
| 129 | |
| 130 | // This avoids essentially the same error being reported multiple times. |
Kristof Umann | 0735cfb | 2018-08-08 12:23:02 +0000 | [diff] [blame] | 131 | if (willObjectBeAnalyzedLater(CtorDecl, Context)) |
Kristof Umann | 30f0865 | 2018-06-18 11:50:17 +0000 | [diff] [blame] | 132 | return; |
| 133 | |
| 134 | Optional<nonloc::LazyCompoundVal> Object = getObjectVal(CtorDecl, Context); |
| 135 | if (!Object) |
| 136 | return; |
| 137 | |
Kristof Umann | 6cec6c4 | 2018-09-14 09:39:26 +0000 | [diff] [blame^] | 138 | FindUninitializedFields F(Context.getState(), Object->getRegion(), Opts); |
Kristof Umann | 30f0865 | 2018-06-18 11:50:17 +0000 | [diff] [blame] | 139 | |
Kristof Umann | 015b059 | 2018-08-13 18:43:08 +0000 | [diff] [blame] | 140 | const UninitFieldMap &UninitFields = F.getUninitFields(); |
Kristof Umann | 30f0865 | 2018-06-18 11:50:17 +0000 | [diff] [blame] | 141 | |
| 142 | if (UninitFields.empty()) |
| 143 | return; |
| 144 | |
| 145 | // There are uninitialized fields in the record. |
| 146 | |
| 147 | ExplodedNode *Node = Context.generateNonFatalErrorNode(Context.getState()); |
| 148 | if (!Node) |
| 149 | return; |
| 150 | |
| 151 | PathDiagnosticLocation LocUsedForUniqueing; |
| 152 | const Stmt *CallSite = Context.getStackFrame()->getCallSite(); |
| 153 | if (CallSite) |
| 154 | LocUsedForUniqueing = PathDiagnosticLocation::createBegin( |
| 155 | CallSite, Context.getSourceManager(), Node->getLocationContext()); |
| 156 | |
Kristof Umann | 9bd4439 | 2018-06-29 11:25:24 +0000 | [diff] [blame] | 157 | // For Plist consumers that don't support notes just yet, we'll convert notes |
| 158 | // to warnings. |
Kristof Umann | 6cec6c4 | 2018-09-14 09:39:26 +0000 | [diff] [blame^] | 159 | if (Opts.ShouldConvertNotesToWarnings) { |
Kristof Umann | 015b059 | 2018-08-13 18:43:08 +0000 | [diff] [blame] | 160 | for (const auto &Pair : UninitFields) { |
Kristof Umann | 9bd4439 | 2018-06-29 11:25:24 +0000 | [diff] [blame] | 161 | |
| 162 | auto Report = llvm::make_unique<BugReport>( |
Kristof Umann | 015b059 | 2018-08-13 18:43:08 +0000 | [diff] [blame] | 163 | *BT_uninitField, Pair.second, Node, LocUsedForUniqueing, |
Kristof Umann | 9bd4439 | 2018-06-29 11:25:24 +0000 | [diff] [blame] | 164 | Node->getLocationContext()->getDecl()); |
| 165 | Context.emitReport(std::move(Report)); |
| 166 | } |
| 167 | return; |
| 168 | } |
| 169 | |
Kristof Umann | 30f0865 | 2018-06-18 11:50:17 +0000 | [diff] [blame] | 170 | SmallString<100> WarningBuf; |
| 171 | llvm::raw_svector_ostream WarningOS(WarningBuf); |
| 172 | WarningOS << UninitFields.size() << " uninitialized field" |
| 173 | << (UninitFields.size() == 1 ? "" : "s") |
| 174 | << " at the end of the constructor call"; |
| 175 | |
| 176 | auto Report = llvm::make_unique<BugReport>( |
| 177 | *BT_uninitField, WarningOS.str(), Node, LocUsedForUniqueing, |
| 178 | Node->getLocationContext()->getDecl()); |
| 179 | |
Kristof Umann | 015b059 | 2018-08-13 18:43:08 +0000 | [diff] [blame] | 180 | for (const auto &Pair : UninitFields) { |
| 181 | Report->addNote(Pair.second, |
| 182 | PathDiagnosticLocation::create(Pair.first->getDecl(), |
Kristof Umann | 30f0865 | 2018-06-18 11:50:17 +0000 | [diff] [blame] | 183 | Context.getSourceManager())); |
| 184 | } |
Kristof Umann | 30f0865 | 2018-06-18 11:50:17 +0000 | [diff] [blame] | 185 | Context.emitReport(std::move(Report)); |
| 186 | } |
| 187 | |
| 188 | //===----------------------------------------------------------------------===// |
| 189 | // Methods for FindUninitializedFields. |
| 190 | //===----------------------------------------------------------------------===// |
| 191 | |
| 192 | FindUninitializedFields::FindUninitializedFields( |
Kristof Umann | 23ca966 | 2018-08-13 18:48:34 +0000 | [diff] [blame] | 193 | ProgramStateRef State, const TypedValueRegion *const R, |
Kristof Umann | 6cec6c4 | 2018-09-14 09:39:26 +0000 | [diff] [blame^] | 194 | const UninitObjCheckerOptions &Opts) |
| 195 | : State(State), ObjectR(R), Opts(Opts) { |
Kristof Umann | 30f0865 | 2018-06-18 11:50:17 +0000 | [diff] [blame] | 196 | |
Kristof Umann | 015b059 | 2018-08-13 18:43:08 +0000 | [diff] [blame] | 197 | isNonUnionUninit(ObjectR, FieldChainInfo(ChainFactory)); |
Kristof Umann | 6cec6c4 | 2018-09-14 09:39:26 +0000 | [diff] [blame^] | 198 | |
| 199 | // In non-pedantic mode, if ObjectR doesn't contain a single initialized |
| 200 | // field, we'll assume that Object was intentionally left uninitialized. |
| 201 | if (!Opts.IsPedantic && !isAnyFieldInitialized()) |
| 202 | UninitFields.clear(); |
Kristof Umann | 30f0865 | 2018-06-18 11:50:17 +0000 | [diff] [blame] | 203 | } |
| 204 | |
| 205 | bool FindUninitializedFields::addFieldToUninits(FieldChainInfo Chain) { |
| 206 | if (State->getStateManager().getContext().getSourceManager().isInSystemHeader( |
Kristof Umann | 015b059 | 2018-08-13 18:43:08 +0000 | [diff] [blame] | 207 | Chain.getUninitRegion()->getDecl()->getLocation())) |
Kristof Umann | 30f0865 | 2018-06-18 11:50:17 +0000 | [diff] [blame] | 208 | return false; |
| 209 | |
Kristof Umann | 015b059 | 2018-08-13 18:43:08 +0000 | [diff] [blame] | 210 | UninitFieldMap::mapped_type NoteMsgBuf; |
| 211 | llvm::raw_svector_ostream OS(NoteMsgBuf); |
| 212 | Chain.printNoteMsg(OS); |
| 213 | return UninitFields |
| 214 | .insert(std::make_pair(Chain.getUninitRegion(), std::move(NoteMsgBuf))) |
| 215 | .second; |
Kristof Umann | 30f0865 | 2018-06-18 11:50:17 +0000 | [diff] [blame] | 216 | } |
| 217 | |
| 218 | bool FindUninitializedFields::isNonUnionUninit(const TypedValueRegion *R, |
| 219 | FieldChainInfo LocalChain) { |
| 220 | assert(R->getValueType()->isRecordType() && |
| 221 | !R->getValueType()->isUnionType() && |
| 222 | "This method only checks non-union record objects!"); |
| 223 | |
Kristof Umann | f0dd101 | 2018-09-14 08:58:21 +0000 | [diff] [blame] | 224 | const RecordDecl *RD = R->getValueType()->getAsRecordDecl()->getDefinition(); |
| 225 | |
| 226 | if (!RD) { |
| 227 | IsAnyFieldInitialized = true; |
| 228 | return true; |
| 229 | } |
Kristof Umann | 30f0865 | 2018-06-18 11:50:17 +0000 | [diff] [blame] | 230 | |
| 231 | bool ContainsUninitField = false; |
| 232 | |
| 233 | // Are all of this non-union's fields initialized? |
| 234 | for (const FieldDecl *I : RD->fields()) { |
| 235 | |
| 236 | const auto FieldVal = |
| 237 | State->getLValue(I, loc::MemRegionVal(R)).castAs<loc::MemRegionVal>(); |
| 238 | const auto *FR = FieldVal.getRegionAs<FieldRegion>(); |
| 239 | QualType T = I->getType(); |
| 240 | |
| 241 | // If LocalChain already contains FR, then we encountered a cyclic |
| 242 | // reference. In this case, region FR is already under checking at an |
| 243 | // earlier node in the directed tree. |
| 244 | if (LocalChain.contains(FR)) |
| 245 | return false; |
| 246 | |
| 247 | if (T->isStructureOrClassType()) { |
Kristof Umann | 015b059 | 2018-08-13 18:43:08 +0000 | [diff] [blame] | 248 | if (isNonUnionUninit(FR, LocalChain.add(RegularField(FR)))) |
Kristof Umann | 30f0865 | 2018-06-18 11:50:17 +0000 | [diff] [blame] | 249 | ContainsUninitField = true; |
| 250 | continue; |
| 251 | } |
| 252 | |
| 253 | if (T->isUnionType()) { |
| 254 | if (isUnionUninit(FR)) { |
Kristof Umann | 015b059 | 2018-08-13 18:43:08 +0000 | [diff] [blame] | 255 | if (addFieldToUninits(LocalChain.add(RegularField(FR)))) |
Kristof Umann | 30f0865 | 2018-06-18 11:50:17 +0000 | [diff] [blame] | 256 | ContainsUninitField = true; |
| 257 | } else |
| 258 | IsAnyFieldInitialized = true; |
| 259 | continue; |
| 260 | } |
| 261 | |
| 262 | if (T->isArrayType()) { |
| 263 | IsAnyFieldInitialized = true; |
| 264 | continue; |
| 265 | } |
| 266 | |
Kristof Umann | f0dd101 | 2018-09-14 08:58:21 +0000 | [diff] [blame] | 267 | if (isDereferencableType(T)) { |
Kristof Umann | ceb5f65 | 2018-09-14 09:07:40 +0000 | [diff] [blame] | 268 | if (isDereferencableUninit(FR, LocalChain)) |
Kristof Umann | 30f0865 | 2018-06-18 11:50:17 +0000 | [diff] [blame] | 269 | ContainsUninitField = true; |
| 270 | continue; |
| 271 | } |
| 272 | |
Kristof Umann | 20e85ba | 2018-06-19 08:35:02 +0000 | [diff] [blame] | 273 | if (isPrimitiveType(T)) { |
| 274 | SVal V = State->getSVal(FieldVal); |
Kristof Umann | 30f0865 | 2018-06-18 11:50:17 +0000 | [diff] [blame] | 275 | |
Kristof Umann | 20e85ba | 2018-06-19 08:35:02 +0000 | [diff] [blame] | 276 | if (isPrimitiveUninit(V)) { |
Kristof Umann | 015b059 | 2018-08-13 18:43:08 +0000 | [diff] [blame] | 277 | if (addFieldToUninits(LocalChain.add(RegularField(FR)))) |
Kristof Umann | 20e85ba | 2018-06-19 08:35:02 +0000 | [diff] [blame] | 278 | ContainsUninitField = true; |
| 279 | } |
| 280 | continue; |
Kristof Umann | 30f0865 | 2018-06-18 11:50:17 +0000 | [diff] [blame] | 281 | } |
Kristof Umann | 20e85ba | 2018-06-19 08:35:02 +0000 | [diff] [blame] | 282 | |
| 283 | llvm_unreachable("All cases are handled!"); |
Kristof Umann | 30f0865 | 2018-06-18 11:50:17 +0000 | [diff] [blame] | 284 | } |
| 285 | |
Kristof Umann | ceb5f65 | 2018-09-14 09:07:40 +0000 | [diff] [blame] | 286 | // Checking bases. The checker will regard inherited data members as direct |
| 287 | // fields. |
Kristof Umann | 30f0865 | 2018-06-18 11:50:17 +0000 | [diff] [blame] | 288 | const auto *CXXRD = dyn_cast<CXXRecordDecl>(RD); |
| 289 | if (!CXXRD) |
| 290 | return ContainsUninitField; |
| 291 | |
| 292 | for (const CXXBaseSpecifier &BaseSpec : CXXRD->bases()) { |
| 293 | const auto *BaseRegion = State->getLValue(BaseSpec, R) |
| 294 | .castAs<loc::MemRegionVal>() |
| 295 | .getRegionAs<TypedValueRegion>(); |
| 296 | |
Kristof Umann | b59b45e | 2018-08-21 12:16:59 +0000 | [diff] [blame] | 297 | // If the head of the list is also a BaseClass, we'll overwrite it to avoid |
| 298 | // note messages like 'this->A::B::x'. |
| 299 | if (!LocalChain.isEmpty() && LocalChain.getHead().isBase()) { |
| 300 | if (isNonUnionUninit(BaseRegion, LocalChain.replaceHead( |
| 301 | BaseClass(BaseSpec.getType())))) |
| 302 | ContainsUninitField = true; |
| 303 | } else { |
| 304 | if (isNonUnionUninit(BaseRegion, |
| 305 | LocalChain.add(BaseClass(BaseSpec.getType())))) |
| 306 | ContainsUninitField = true; |
| 307 | } |
Kristof Umann | 30f0865 | 2018-06-18 11:50:17 +0000 | [diff] [blame] | 308 | } |
| 309 | |
| 310 | return ContainsUninitField; |
| 311 | } |
| 312 | |
| 313 | bool FindUninitializedFields::isUnionUninit(const TypedValueRegion *R) { |
| 314 | assert(R->getValueType()->isUnionType() && |
| 315 | "This method only checks union objects!"); |
| 316 | // TODO: Implement support for union fields. |
| 317 | return false; |
| 318 | } |
| 319 | |
Kristof Umann | 30f0865 | 2018-06-18 11:50:17 +0000 | [diff] [blame] | 320 | bool FindUninitializedFields::isPrimitiveUninit(const SVal &V) { |
| 321 | if (V.isUndef()) |
| 322 | return true; |
| 323 | |
| 324 | IsAnyFieldInitialized = true; |
| 325 | return false; |
| 326 | } |
| 327 | |
| 328 | //===----------------------------------------------------------------------===// |
| 329 | // Methods for FieldChainInfo. |
| 330 | //===----------------------------------------------------------------------===// |
| 331 | |
Kristof Umann | 015b059 | 2018-08-13 18:43:08 +0000 | [diff] [blame] | 332 | const FieldRegion *FieldChainInfo::getUninitRegion() const { |
Kristof Umann | 30f0865 | 2018-06-18 11:50:17 +0000 | [diff] [blame] | 333 | assert(!Chain.isEmpty() && "Empty fieldchain!"); |
Kristof Umann | ceb5f65 | 2018-09-14 09:07:40 +0000 | [diff] [blame] | 334 | |
| 335 | // ImmutableList::getHead() isn't a const method, hence the not too nice |
| 336 | // implementation. |
Kristof Umann | 015b059 | 2018-08-13 18:43:08 +0000 | [diff] [blame] | 337 | return (*Chain.begin()).getRegion(); |
Kristof Umann | 30f0865 | 2018-06-18 11:50:17 +0000 | [diff] [blame] | 338 | } |
| 339 | |
Kristof Umann | 015b059 | 2018-08-13 18:43:08 +0000 | [diff] [blame] | 340 | bool FieldChainInfo::contains(const FieldRegion *FR) const { |
| 341 | for (const FieldNode &Node : Chain) { |
| 342 | if (Node.isSameRegion(FR)) |
| 343 | return true; |
| 344 | } |
| 345 | return false; |
Kristof Umann | 30f0865 | 2018-06-18 11:50:17 +0000 | [diff] [blame] | 346 | } |
| 347 | |
Kristof Umann | a37bba4 | 2018-08-13 18:22:22 +0000 | [diff] [blame] | 348 | /// Prints every element except the last to `Out`. Since ImmutableLists store |
| 349 | /// elements in reverse order, and have no reverse iterators, we use a |
| 350 | /// recursive function to print the fieldchain correctly. The last element in |
Kristof Umann | ceb5f65 | 2018-09-14 09:07:40 +0000 | [diff] [blame] | 351 | /// the chain is to be printed by `FieldChainInfo::print`. |
Kristof Umann | a37bba4 | 2018-08-13 18:22:22 +0000 | [diff] [blame] | 352 | static void printTail(llvm::raw_ostream &Out, |
| 353 | const FieldChainInfo::FieldChainImpl *L); |
| 354 | |
Kristof Umann | ceb5f65 | 2018-09-14 09:07:40 +0000 | [diff] [blame] | 355 | // FIXME: This function constructs an incorrect string in the following case: |
Kristof Umann | 30f0865 | 2018-06-18 11:50:17 +0000 | [diff] [blame] | 356 | // |
| 357 | // struct Base { int x; }; |
| 358 | // struct D1 : Base {}; struct D2 : Base {}; |
| 359 | // |
| 360 | // struct MostDerived : D1, D2 { |
| 361 | // MostDerived() {} |
| 362 | // } |
| 363 | // |
| 364 | // A call to MostDerived::MostDerived() will cause two notes that say |
| 365 | // "uninitialized field 'this->x'", but we can't refer to 'x' directly, |
| 366 | // we need an explicit namespace resolution whether the uninit field was |
| 367 | // 'D1::x' or 'D2::x'. |
Kristof Umann | 015b059 | 2018-08-13 18:43:08 +0000 | [diff] [blame] | 368 | void FieldChainInfo::printNoteMsg(llvm::raw_ostream &Out) const { |
Kristof Umann | 30f0865 | 2018-06-18 11:50:17 +0000 | [diff] [blame] | 369 | if (Chain.isEmpty()) |
| 370 | return; |
| 371 | |
Kristof Umann | a37bba4 | 2018-08-13 18:22:22 +0000 | [diff] [blame] | 372 | const FieldChainImpl *L = Chain.getInternalPointer(); |
Kristof Umann | 015b059 | 2018-08-13 18:43:08 +0000 | [diff] [blame] | 373 | const FieldNode &LastField = L->getHead(); |
| 374 | |
| 375 | LastField.printNoteMsg(Out); |
| 376 | Out << '\''; |
| 377 | |
| 378 | for (const FieldNode &Node : Chain) |
| 379 | Node.printPrefix(Out); |
| 380 | |
| 381 | Out << "this->"; |
Kristof Umann | 30f0865 | 2018-06-18 11:50:17 +0000 | [diff] [blame] | 382 | printTail(Out, L->getTail()); |
Kristof Umann | 015b059 | 2018-08-13 18:43:08 +0000 | [diff] [blame] | 383 | LastField.printNode(Out); |
| 384 | Out << '\''; |
Kristof Umann | 30f0865 | 2018-06-18 11:50:17 +0000 | [diff] [blame] | 385 | } |
| 386 | |
Kristof Umann | a37bba4 | 2018-08-13 18:22:22 +0000 | [diff] [blame] | 387 | static void printTail(llvm::raw_ostream &Out, |
| 388 | const FieldChainInfo::FieldChainImpl *L) { |
Kristof Umann | 30f0865 | 2018-06-18 11:50:17 +0000 | [diff] [blame] | 389 | if (!L) |
| 390 | return; |
| 391 | |
| 392 | printTail(Out, L->getTail()); |
Kristof Umann | 015b059 | 2018-08-13 18:43:08 +0000 | [diff] [blame] | 393 | |
| 394 | L->getHead().printNode(Out); |
| 395 | L->getHead().printSeparator(Out); |
Kristof Umann | 30f0865 | 2018-06-18 11:50:17 +0000 | [diff] [blame] | 396 | } |
| 397 | |
| 398 | //===----------------------------------------------------------------------===// |
| 399 | // Utility functions. |
| 400 | //===----------------------------------------------------------------------===// |
| 401 | |
Kristof Umann | cc85244 | 2018-07-12 13:13:46 +0000 | [diff] [blame] | 402 | static Optional<nonloc::LazyCompoundVal> |
Kristof Umann | 30f0865 | 2018-06-18 11:50:17 +0000 | [diff] [blame] | 403 | getObjectVal(const CXXConstructorDecl *CtorDecl, CheckerContext &Context) { |
| 404 | |
| 405 | Loc ThisLoc = Context.getSValBuilder().getCXXThis(CtorDecl->getParent(), |
| 406 | Context.getStackFrame()); |
| 407 | // Getting the value for 'this'. |
| 408 | SVal This = Context.getState()->getSVal(ThisLoc); |
| 409 | |
| 410 | // Getting the value for '*this'. |
| 411 | SVal Object = Context.getState()->getSVal(This.castAs<Loc>()); |
| 412 | |
| 413 | return Object.getAs<nonloc::LazyCompoundVal>(); |
| 414 | } |
| 415 | |
Kristof Umann | 0735cfb | 2018-08-08 12:23:02 +0000 | [diff] [blame] | 416 | static bool willObjectBeAnalyzedLater(const CXXConstructorDecl *Ctor, |
Kristof Umann | a37bba4 | 2018-08-13 18:22:22 +0000 | [diff] [blame] | 417 | CheckerContext &Context) { |
Kristof Umann | 30f0865 | 2018-06-18 11:50:17 +0000 | [diff] [blame] | 418 | |
Kristof Umann | 0735cfb | 2018-08-08 12:23:02 +0000 | [diff] [blame] | 419 | Optional<nonloc::LazyCompoundVal> CurrentObject = getObjectVal(Ctor, Context); |
| 420 | if (!CurrentObject) |
| 421 | return false; |
| 422 | |
| 423 | const LocationContext *LC = Context.getLocationContext(); |
| 424 | while ((LC = LC->getParent())) { |
| 425 | |
| 426 | // If \p Ctor was called by another constructor. |
| 427 | const auto *OtherCtor = dyn_cast<CXXConstructorDecl>(LC->getDecl()); |
| 428 | if (!OtherCtor) |
| 429 | continue; |
| 430 | |
| 431 | Optional<nonloc::LazyCompoundVal> OtherObject = |
| 432 | getObjectVal(OtherCtor, Context); |
| 433 | if (!OtherObject) |
| 434 | continue; |
| 435 | |
| 436 | // If the CurrentObject is a subregion of OtherObject, it will be analyzed |
| 437 | // during the analysis of OtherObject. |
| 438 | if (CurrentObject->getRegion()->isSubRegionOf(OtherObject->getRegion())) |
Kristof Umann | 30f0865 | 2018-06-18 11:50:17 +0000 | [diff] [blame] | 439 | return true; |
Kristof Umann | 30f0865 | 2018-06-18 11:50:17 +0000 | [diff] [blame] | 440 | } |
Kristof Umann | 0735cfb | 2018-08-08 12:23:02 +0000 | [diff] [blame] | 441 | |
Kristof Umann | 30f0865 | 2018-06-18 11:50:17 +0000 | [diff] [blame] | 442 | return false; |
| 443 | } |
| 444 | |
Kristof Umann | f0dd101 | 2018-09-14 08:58:21 +0000 | [diff] [blame] | 445 | std::string clang::ento::getVariableName(const FieldDecl *Field) { |
Kristof Umann | 8c11909 | 2018-07-13 12:54:47 +0000 | [diff] [blame] | 446 | // If Field is a captured lambda variable, Field->getName() will return with |
| 447 | // an empty string. We can however acquire it's name from the lambda's |
| 448 | // captures. |
| 449 | const auto *CXXParent = dyn_cast<CXXRecordDecl>(Field->getParent()); |
| 450 | |
| 451 | if (CXXParent && CXXParent->isLambda()) { |
| 452 | assert(CXXParent->captures_begin()); |
| 453 | auto It = CXXParent->captures_begin() + Field->getFieldIndex(); |
Kristof Umann | f0dd101 | 2018-09-14 08:58:21 +0000 | [diff] [blame] | 454 | |
| 455 | if (It->capturesVariable()) |
| 456 | return llvm::Twine("/*captured variable*/" + |
| 457 | It->getCapturedVar()->getName()) |
| 458 | .str(); |
| 459 | |
| 460 | if (It->capturesThis()) |
| 461 | return "/*'this' capture*/"; |
| 462 | |
| 463 | llvm_unreachable("No other capture type is expected!"); |
Kristof Umann | 8c11909 | 2018-07-13 12:54:47 +0000 | [diff] [blame] | 464 | } |
| 465 | |
| 466 | return Field->getName(); |
| 467 | } |
| 468 | |
Kristof Umann | 30f0865 | 2018-06-18 11:50:17 +0000 | [diff] [blame] | 469 | void ento::registerUninitializedObjectChecker(CheckerManager &Mgr) { |
| 470 | auto Chk = Mgr.registerChecker<UninitializedObjectChecker>(); |
Kristof Umann | ceb5f65 | 2018-09-14 09:07:40 +0000 | [diff] [blame] | 471 | |
Kristof Umann | 6cec6c4 | 2018-09-14 09:39:26 +0000 | [diff] [blame^] | 472 | AnalyzerOptions &AnOpts = Mgr.getAnalyzerOptions(); |
| 473 | UninitObjCheckerOptions &ChOpts = Chk->Opts; |
| 474 | |
| 475 | ChOpts.IsPedantic = AnOpts.getBooleanOption( |
Kristof Umann | 30f0865 | 2018-06-18 11:50:17 +0000 | [diff] [blame] | 476 | "Pedantic", /*DefaultVal*/ false, Chk); |
Kristof Umann | 6cec6c4 | 2018-09-14 09:39:26 +0000 | [diff] [blame^] | 477 | ChOpts.ShouldConvertNotesToWarnings = AnOpts.getBooleanOption( |
Kristof Umann | 9bd4439 | 2018-06-29 11:25:24 +0000 | [diff] [blame] | 478 | "NotesAsWarnings", /*DefaultVal*/ false, Chk); |
Kristof Umann | 6cec6c4 | 2018-09-14 09:39:26 +0000 | [diff] [blame^] | 479 | ChOpts.CheckPointeeInitialization = AnOpts.getBooleanOption( |
Kristof Umann | a3f7b58 | 2018-08-07 12:55:26 +0000 | [diff] [blame] | 480 | "CheckPointeeInitialization", /*DefaultVal*/ false, Chk); |
Kristof Umann | 30f0865 | 2018-06-18 11:50:17 +0000 | [diff] [blame] | 481 | } |