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