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