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