blob: 2383ecff2295bfe76073df0ee79d51638dca3a3c [file] [log] [blame]
Kristof Umann30f08652018-06-18 11:50:17 +00001//===----- 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 Umanna3f7b582018-08-07 12:55:26 +000013// This checker has several options:
Kristof Umann9bd44392018-06-29 11:25:24 +000014// - "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 Umanna3f7b582018-08-07 12:55:26 +000018// `-analyzer-config alpha.cplusplus.UninitializedObject:Pedantic=true`.
Kristof Umann9bd44392018-06-29 11:25:24 +000019//
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 Umanna3f7b582018-08-07 12:55:26 +000025// `-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 Umann30f08652018-06-18 11:50:17 +000040//
41//===----------------------------------------------------------------------===//
42
43#include "ClangSACheckers.h"
44#include "clang/StaticAnalyzer/Core/BugReporter/BugType.h"
45#include "clang/StaticAnalyzer/Core/Checker.h"
46#include "clang/StaticAnalyzer/Core/PathSensitive/CheckerContext.h"
47#include <algorithm>
48
49using namespace clang;
50using namespace clang::ento;
51
52namespace {
53
54class UninitializedObjectChecker : public Checker<check::EndFunction> {
55 std::unique_ptr<BuiltinBug> BT_uninitField;
56
57public:
Kristof Umann9bd44392018-06-29 11:25:24 +000058 // These fields will be initialized when registering the checker.
59 bool IsPedantic;
60 bool ShouldConvertNotesToWarnings;
Kristof Umanna3f7b582018-08-07 12:55:26 +000061 bool CheckPointeeInitialization;
Kristof Umann30f08652018-06-18 11:50:17 +000062
63 UninitializedObjectChecker()
64 : BT_uninitField(new BuiltinBug(this, "Uninitialized fields")) {}
Reka Kovacsed8c05c2018-07-16 20:47:45 +000065 void checkEndFunction(const ReturnStmt *RS, CheckerContext &C) const;
Kristof Umann30f08652018-06-18 11:50:17 +000066};
67
Kristof Umann30f08652018-06-18 11:50:17 +000068/// Represents a field chain. A field chain is a vector of fields where the
69/// first element of the chain is the object under checking (not stored), and
70/// every other element is a field, and the element that precedes it is the
71/// object that contains it.
72///
73/// Note that this class is immutable, and new fields may only be added through
74/// constructor calls.
75class FieldChainInfo {
76 using FieldChain = llvm::ImmutableList<const FieldRegion *>;
77
78 FieldChain Chain;
79
80 const bool IsDereferenced = false;
81
82public:
83 FieldChainInfo() = default;
84
85 FieldChainInfo(const FieldChainInfo &Other, const bool IsDereferenced)
86 : Chain(Other.Chain), IsDereferenced(IsDereferenced) {}
87
88 FieldChainInfo(const FieldChainInfo &Other, const FieldRegion *FR,
89 const bool IsDereferenced = false);
90
91 bool contains(const FieldRegion *FR) const { return Chain.contains(FR); }
92 bool isPointer() const;
93
94 /// If this is a fieldchain whose last element is an uninitialized region of a
95 /// pointer type, `IsDereferenced` will store whether the pointer itself or
96 /// the pointee is uninitialized.
97 bool isDereferenced() const;
98 const FieldDecl *getEndOfChain() const;
99 void print(llvm::raw_ostream &Out) const;
100
101private:
102 /// Prints every element except the last to `Out`. Since ImmutableLists store
103 /// elements in reverse order, and have no reverse iterators, we use a
104 /// recursive function to print the fieldchain correctly. The last element in
105 /// the chain is to be printed by `print`.
106 static void printTail(llvm::raw_ostream &Out,
107 const llvm::ImmutableListImpl<const FieldRegion *> *L);
108 friend struct FieldChainInfoComparator;
109};
110
111struct FieldChainInfoComparator {
Steven Wub3684db2018-06-22 16:51:17 +0000112 bool operator()(const FieldChainInfo &lhs, const FieldChainInfo &rhs) const {
Kristof Umann30f08652018-06-18 11:50:17 +0000113 assert(!lhs.Chain.isEmpty() && !rhs.Chain.isEmpty() &&
114 "Attempted to store an empty fieldchain!");
115 return *lhs.Chain.begin() < *rhs.Chain.begin();
116 }
117};
118
119using UninitFieldSet = std::set<FieldChainInfo, FieldChainInfoComparator>;
120
121/// Searches for and stores uninitialized fields in a non-union object.
122class FindUninitializedFields {
123 ProgramStateRef State;
124 const TypedValueRegion *const ObjectR;
125
126 const bool IsPedantic;
Kristof Umanna3f7b582018-08-07 12:55:26 +0000127 const bool CheckPointeeInitialization;
128
Kristof Umann30f08652018-06-18 11:50:17 +0000129 bool IsAnyFieldInitialized = false;
130
131 UninitFieldSet UninitFields;
132
133public:
134 FindUninitializedFields(ProgramStateRef State,
Kristof Umanna3f7b582018-08-07 12:55:26 +0000135 const TypedValueRegion *const R, bool IsPedantic,
136 bool CheckPointeeInitialization);
Kristof Umann30f08652018-06-18 11:50:17 +0000137 const UninitFieldSet &getUninitFields();
138
139private:
140 /// Adds a FieldChainInfo object to UninitFields. Return true if an insertion
141 /// took place.
142 bool addFieldToUninits(FieldChainInfo LocalChain);
143
144 // For the purposes of this checker, we'll regard the object under checking as
145 // a directed tree, where
146 // * the root is the object under checking
147 // * every node is an object that is
148 // - a union
149 // - a non-union record
150 // - a pointer/reference
151 // - an array
Kristof Umann7212cc02018-07-13 12:21:38 +0000152 // - of a primitive type, which we'll define later in a helper function.
Kristof Umann30f08652018-06-18 11:50:17 +0000153 // * the parent of each node is the object that contains it
Kristof Umann7212cc02018-07-13 12:21:38 +0000154 // * every leaf is an array, a primitive object, a nullptr or an undefined
155 // pointer.
Kristof Umann30f08652018-06-18 11:50:17 +0000156 //
157 // Example:
158 //
159 // struct A {
160 // struct B {
161 // int x, y = 0;
162 // };
163 // B b;
164 // int *iptr = new int;
165 // B* bptr;
166 //
167 // A() {}
168 // };
169 //
170 // The directed tree:
171 //
172 // ->x
173 // /
174 // ->b--->y
175 // /
176 // A-->iptr->(int value)
177 // \
178 // ->bptr
179 //
180 // From this we'll construct a vector of fieldchains, where each fieldchain
181 // represents an uninitialized field. An uninitialized field may be a
Kristof Umann7212cc02018-07-13 12:21:38 +0000182 // primitive object, a pointer, a pointee or a union without a single
183 // initialized field.
Kristof Umann30f08652018-06-18 11:50:17 +0000184 // In the above example, for the default constructor call we'll end up with
185 // these fieldchains:
186 //
187 // this->b.x
188 // this->iptr (pointee uninit)
189 // this->bptr (pointer uninit)
190 //
191 // We'll traverse each node of the above graph with the appropiate one of
192 // these methods:
193
194 /// This method checks a region of a union object, and returns true if no
195 /// field is initialized within the region.
196 bool isUnionUninit(const TypedValueRegion *R);
197
198 /// This method checks a region of a non-union object, and returns true if
199 /// an uninitialized field is found within the region.
200 bool isNonUnionUninit(const TypedValueRegion *R, FieldChainInfo LocalChain);
201
202 /// This method checks a region of a pointer or reference object, and returns
203 /// true if the ptr/ref object itself or any field within the pointee's region
204 /// is uninitialized.
205 bool isPointerOrReferenceUninit(const FieldRegion *FR,
206 FieldChainInfo LocalChain);
207
Kristof Umann30f08652018-06-18 11:50:17 +0000208 /// This method returns true if the value of a primitive object is
209 /// uninitialized.
210 bool isPrimitiveUninit(const SVal &V);
211
212 // Note that we don't have a method for arrays -- the elements of an array are
213 // often left uninitialized intentionally even when it is of a C++ record
214 // type, so we'll assume that an array is always initialized.
215 // TODO: Add a support for nonloc::LocAsInteger.
216};
217
Kristof Umanncc852442018-07-12 13:13:46 +0000218} // end of anonymous namespace
219
220// Static variable instantionations.
221
222static llvm::ImmutableListFactory<const FieldRegion *> Factory;
223
Kristof Umann30f08652018-06-18 11:50:17 +0000224// Utility function declarations.
225
226/// Returns the object that was constructed by CtorDecl, or None if that isn't
227/// possible.
Kristof Umanncc852442018-07-12 13:13:46 +0000228static Optional<nonloc::LazyCompoundVal>
Kristof Umann30f08652018-06-18 11:50:17 +0000229getObjectVal(const CXXConstructorDecl *CtorDecl, CheckerContext &Context);
230
231/// Checks whether the constructor under checking is called by another
232/// constructor.
Kristof Umanncc852442018-07-12 13:13:46 +0000233static bool isCalledByConstructor(const CheckerContext &Context);
Kristof Umann30f08652018-06-18 11:50:17 +0000234
235/// Returns whether FD can be (transitively) dereferenced to a void pointer type
236/// (void*, void**, ...). The type of the region behind a void pointer isn't
237/// known, and thus FD can not be analyzed.
Kristof Umanncc852442018-07-12 13:13:46 +0000238static bool isVoidPointer(const FieldDecl *FD);
Kristof Umann30f08652018-06-18 11:50:17 +0000239
Kristof Umann7212cc02018-07-13 12:21:38 +0000240/// Returns true if T is a primitive type. We defined this type so that for
241/// objects that we'd only like analyze as much as checking whether their
242/// value is undefined or not, such as ints and doubles, can be analyzed with
243/// ease. This also helps ensuring that every special field type is handled
244/// correctly.
Kristof Umanncc852442018-07-12 13:13:46 +0000245static bool isPrimitiveType(const QualType &T) {
Kristof Umann7212cc02018-07-13 12:21:38 +0000246 return T->isBuiltinType() || T->isEnumeralType() || T->isMemberPointerType();
Kristof Umann30f08652018-06-18 11:50:17 +0000247}
248
Kristof Umann9bd44392018-06-29 11:25:24 +0000249/// Constructs a note message for a given FieldChainInfo object.
Kristof Umanncc852442018-07-12 13:13:46 +0000250static void printNoteMessage(llvm::raw_ostream &Out,
251 const FieldChainInfo &Chain);
Kristof Umann30f08652018-06-18 11:50:17 +0000252
Kristof Umann8c119092018-07-13 12:54:47 +0000253/// Returns with Field's name. This is a helper function to get the correct name
254/// even if Field is a captured lambda variable.
255static StringRef getVariableName(const FieldDecl *Field);
256
Kristof Umann30f08652018-06-18 11:50:17 +0000257//===----------------------------------------------------------------------===//
258// Methods for UninitializedObjectChecker.
259//===----------------------------------------------------------------------===//
260
261void UninitializedObjectChecker::checkEndFunction(
Reka Kovacsed8c05c2018-07-16 20:47:45 +0000262 const ReturnStmt *RS, CheckerContext &Context) const {
Kristof Umann30f08652018-06-18 11:50:17 +0000263
264 const auto *CtorDecl = dyn_cast_or_null<CXXConstructorDecl>(
265 Context.getLocationContext()->getDecl());
266 if (!CtorDecl)
267 return;
268
269 if (!CtorDecl->isUserProvided())
270 return;
271
272 if (CtorDecl->getParent()->isUnion())
273 return;
274
275 // This avoids essentially the same error being reported multiple times.
276 if (isCalledByConstructor(Context))
277 return;
278
279 Optional<nonloc::LazyCompoundVal> Object = getObjectVal(CtorDecl, Context);
280 if (!Object)
281 return;
282
Kristof Umanna3f7b582018-08-07 12:55:26 +0000283 FindUninitializedFields F(Context.getState(), Object->getRegion(), IsPedantic,
284 CheckPointeeInitialization);
Kristof Umann30f08652018-06-18 11:50:17 +0000285
286 const UninitFieldSet &UninitFields = F.getUninitFields();
287
288 if (UninitFields.empty())
289 return;
290
291 // There are uninitialized fields in the record.
292
293 ExplodedNode *Node = Context.generateNonFatalErrorNode(Context.getState());
294 if (!Node)
295 return;
296
297 PathDiagnosticLocation LocUsedForUniqueing;
298 const Stmt *CallSite = Context.getStackFrame()->getCallSite();
299 if (CallSite)
300 LocUsedForUniqueing = PathDiagnosticLocation::createBegin(
301 CallSite, Context.getSourceManager(), Node->getLocationContext());
302
Kristof Umann9bd44392018-06-29 11:25:24 +0000303 // For Plist consumers that don't support notes just yet, we'll convert notes
304 // to warnings.
305 if (ShouldConvertNotesToWarnings) {
306 for (const auto &Chain : UninitFields) {
307 SmallString<100> WarningBuf;
308 llvm::raw_svector_ostream WarningOS(WarningBuf);
309
310 printNoteMessage(WarningOS, Chain);
311
312 auto Report = llvm::make_unique<BugReport>(
313 *BT_uninitField, WarningOS.str(), Node, LocUsedForUniqueing,
314 Node->getLocationContext()->getDecl());
315 Context.emitReport(std::move(Report));
316 }
317 return;
318 }
319
Kristof Umann30f08652018-06-18 11:50:17 +0000320 SmallString<100> WarningBuf;
321 llvm::raw_svector_ostream WarningOS(WarningBuf);
322 WarningOS << UninitFields.size() << " uninitialized field"
323 << (UninitFields.size() == 1 ? "" : "s")
324 << " at the end of the constructor call";
325
326 auto Report = llvm::make_unique<BugReport>(
327 *BT_uninitField, WarningOS.str(), Node, LocUsedForUniqueing,
328 Node->getLocationContext()->getDecl());
329
Kristof Umann9bd44392018-06-29 11:25:24 +0000330 for (const auto &Chain : UninitFields) {
Kristof Umann30f08652018-06-18 11:50:17 +0000331 SmallString<200> NoteBuf;
332 llvm::raw_svector_ostream NoteOS(NoteBuf);
333
Kristof Umann9bd44392018-06-29 11:25:24 +0000334 printNoteMessage(NoteOS, Chain);
Kristof Umann30f08652018-06-18 11:50:17 +0000335
336 Report->addNote(NoteOS.str(),
Kristof Umann9bd44392018-06-29 11:25:24 +0000337 PathDiagnosticLocation::create(Chain.getEndOfChain(),
Kristof Umann30f08652018-06-18 11:50:17 +0000338 Context.getSourceManager()));
339 }
Kristof Umann30f08652018-06-18 11:50:17 +0000340 Context.emitReport(std::move(Report));
341}
342
343//===----------------------------------------------------------------------===//
344// Methods for FindUninitializedFields.
345//===----------------------------------------------------------------------===//
346
347FindUninitializedFields::FindUninitializedFields(
Kristof Umanna3f7b582018-08-07 12:55:26 +0000348 ProgramStateRef State, const TypedValueRegion *const R, bool IsPedantic,
349 bool CheckPointeeInitialization)
350 : State(State), ObjectR(R), IsPedantic(IsPedantic),
351 CheckPointeeInitialization(CheckPointeeInitialization) {}
Kristof Umann30f08652018-06-18 11:50:17 +0000352
353const UninitFieldSet &FindUninitializedFields::getUninitFields() {
354 isNonUnionUninit(ObjectR, FieldChainInfo());
355
356 if (!IsPedantic && !IsAnyFieldInitialized)
357 UninitFields.clear();
358
359 return UninitFields;
360}
361
362bool FindUninitializedFields::addFieldToUninits(FieldChainInfo Chain) {
363 if (State->getStateManager().getContext().getSourceManager().isInSystemHeader(
364 Chain.getEndOfChain()->getLocation()))
365 return false;
366
367 return UninitFields.insert(Chain).second;
368}
369
370bool FindUninitializedFields::isNonUnionUninit(const TypedValueRegion *R,
371 FieldChainInfo LocalChain) {
372 assert(R->getValueType()->isRecordType() &&
373 !R->getValueType()->isUnionType() &&
374 "This method only checks non-union record objects!");
375
376 const RecordDecl *RD =
377 R->getValueType()->getAs<RecordType>()->getDecl()->getDefinition();
378 assert(RD && "Referred record has no definition");
379
380 bool ContainsUninitField = false;
381
382 // Are all of this non-union's fields initialized?
383 for (const FieldDecl *I : RD->fields()) {
384
385 const auto FieldVal =
386 State->getLValue(I, loc::MemRegionVal(R)).castAs<loc::MemRegionVal>();
387 const auto *FR = FieldVal.getRegionAs<FieldRegion>();
388 QualType T = I->getType();
389
390 // If LocalChain already contains FR, then we encountered a cyclic
391 // reference. In this case, region FR is already under checking at an
392 // earlier node in the directed tree.
393 if (LocalChain.contains(FR))
394 return false;
395
396 if (T->isStructureOrClassType()) {
397 if (isNonUnionUninit(FR, {LocalChain, FR}))
398 ContainsUninitField = true;
399 continue;
400 }
401
402 if (T->isUnionType()) {
403 if (isUnionUninit(FR)) {
404 if (addFieldToUninits({LocalChain, FR}))
405 ContainsUninitField = true;
406 } else
407 IsAnyFieldInitialized = true;
408 continue;
409 }
410
411 if (T->isArrayType()) {
412 IsAnyFieldInitialized = true;
413 continue;
414 }
415
Kristof Umann30f08652018-06-18 11:50:17 +0000416 if (T->isPointerType() || T->isReferenceType()) {
417 if (isPointerOrReferenceUninit(FR, LocalChain))
418 ContainsUninitField = true;
419 continue;
420 }
421
Kristof Umann20e85ba2018-06-19 08:35:02 +0000422 if (isPrimitiveType(T)) {
423 SVal V = State->getSVal(FieldVal);
Kristof Umann30f08652018-06-18 11:50:17 +0000424
Kristof Umann20e85ba2018-06-19 08:35:02 +0000425 if (isPrimitiveUninit(V)) {
426 if (addFieldToUninits({LocalChain, FR}))
427 ContainsUninitField = true;
428 }
429 continue;
Kristof Umann30f08652018-06-18 11:50:17 +0000430 }
Kristof Umann20e85ba2018-06-19 08:35:02 +0000431
432 llvm_unreachable("All cases are handled!");
Kristof Umann30f08652018-06-18 11:50:17 +0000433 }
434
435 // Checking bases.
436 // FIXME: As of now, because of `isCalledByConstructor`, objects whose type
437 // is a descendant of another type will emit warnings for uninitalized
438 // inherited members.
439 // This is not the only way to analyze bases of an object -- if we didn't
440 // filter them out, and didn't analyze the bases, this checker would run for
441 // each base of the object in order of base initailization and in theory would
442 // find every uninitalized field. This approach could also make handling
443 // diamond inheritances more easily.
444 //
445 // This rule (that a descendant type's cunstructor is responsible for
446 // initializing inherited data members) is not obvious, and should it should
447 // be.
448 const auto *CXXRD = dyn_cast<CXXRecordDecl>(RD);
449 if (!CXXRD)
450 return ContainsUninitField;
451
452 for (const CXXBaseSpecifier &BaseSpec : CXXRD->bases()) {
453 const auto *BaseRegion = State->getLValue(BaseSpec, R)
454 .castAs<loc::MemRegionVal>()
455 .getRegionAs<TypedValueRegion>();
456
457 if (isNonUnionUninit(BaseRegion, LocalChain))
458 ContainsUninitField = true;
459 }
460
461 return ContainsUninitField;
462}
463
464bool FindUninitializedFields::isUnionUninit(const TypedValueRegion *R) {
465 assert(R->getValueType()->isUnionType() &&
466 "This method only checks union objects!");
467 // TODO: Implement support for union fields.
468 return false;
469}
470
471// Note that pointers/references don't contain fields themselves, so in this
472// function we won't add anything to LocalChain.
473bool FindUninitializedFields::isPointerOrReferenceUninit(
474 const FieldRegion *FR, FieldChainInfo LocalChain) {
475
476 assert((FR->getDecl()->getType()->isPointerType() ||
477 FR->getDecl()->getType()->isReferenceType()) &&
478 "This method only checks pointer/reference objects!");
479
480 SVal V = State->getSVal(FR);
481
482 if (V.isUnknown() || V.isZeroConstant()) {
483 IsAnyFieldInitialized = true;
484 return false;
485 }
486
487 if (V.isUndef()) {
488 return addFieldToUninits({LocalChain, FR});
489 }
490
Kristof Umanna3f7b582018-08-07 12:55:26 +0000491 if (!CheckPointeeInitialization) {
492 IsAnyFieldInitialized = true;
493 return false;
494 }
495
Kristof Umann30f08652018-06-18 11:50:17 +0000496 const FieldDecl *FD = FR->getDecl();
497
498 // TODO: The dynamic type of a void pointer may be retrieved with
499 // `getDynamicTypeInfo`.
500 if (isVoidPointer(FD)) {
501 IsAnyFieldInitialized = true;
502 return false;
503 }
504
505 assert(V.getAs<Loc>() && "V should be Loc at this point!");
506
507 // At this point the pointer itself is initialized and points to a valid
508 // location, we'll now check the pointee.
509 SVal DerefdV = State->getSVal(V.castAs<Loc>());
510
511 // TODO: Dereferencing should be done according to the dynamic type.
512 while (Optional<Loc> L = DerefdV.getAs<Loc>()) {
513 DerefdV = State->getSVal(*L);
514 }
515
516 // If V is a pointer pointing to a record type.
517 if (Optional<nonloc::LazyCompoundVal> RecordV =
518 DerefdV.getAs<nonloc::LazyCompoundVal>()) {
519
520 const TypedValueRegion *R = RecordV->getRegion();
521
522 // We can't reason about symbolic regions, assume its initialized.
523 // Note that this also avoids a potential infinite recursion, because
524 // constructors for list-like classes are checked without being called, and
525 // the Static Analyzer will construct a symbolic region for Node *next; or
526 // similar code snippets.
527 if (R->getSymbolicBase()) {
528 IsAnyFieldInitialized = true;
529 return false;
530 }
531
532 const QualType T = R->getValueType();
533
534 if (T->isStructureOrClassType())
535 return isNonUnionUninit(R, {LocalChain, FR});
536
537 if (T->isUnionType()) {
538 if (isUnionUninit(R)) {
539 return addFieldToUninits({LocalChain, FR, /*IsDereferenced*/ true});
540 } else {
541 IsAnyFieldInitialized = true;
542 return false;
543 }
544 }
545
546 if (T->isArrayType()) {
547 IsAnyFieldInitialized = true;
548 return false;
549 }
550
551 llvm_unreachable("All cases are handled!");
552 }
553
554 // TODO: If possible, it should be asserted that the DerefdV at this point is
555 // primitive.
556
557 if (isPrimitiveUninit(DerefdV))
558 return addFieldToUninits({LocalChain, FR, /*IsDereferenced*/ true});
559
560 IsAnyFieldInitialized = true;
561 return false;
562}
563
Kristof Umann30f08652018-06-18 11:50:17 +0000564bool FindUninitializedFields::isPrimitiveUninit(const SVal &V) {
565 if (V.isUndef())
566 return true;
567
568 IsAnyFieldInitialized = true;
569 return false;
570}
571
572//===----------------------------------------------------------------------===//
573// Methods for FieldChainInfo.
574//===----------------------------------------------------------------------===//
575
576FieldChainInfo::FieldChainInfo(const FieldChainInfo &Other,
577 const FieldRegion *FR, const bool IsDereferenced)
578 : FieldChainInfo(Other, IsDereferenced) {
579 assert(!contains(FR) && "Can't add a field that is already a part of the "
580 "fieldchain! Is this a cyclic reference?");
581 Chain = Factory.add(FR, Other.Chain);
582}
583
584bool FieldChainInfo::isPointer() const {
585 assert(!Chain.isEmpty() && "Empty fieldchain!");
586 return (*Chain.begin())->getDecl()->getType()->isPointerType();
587}
588
589bool FieldChainInfo::isDereferenced() const {
590 assert(isPointer() && "Only pointers may or may not be dereferenced!");
591 return IsDereferenced;
592}
593
594const FieldDecl *FieldChainInfo::getEndOfChain() const {
595 assert(!Chain.isEmpty() && "Empty fieldchain!");
596 return (*Chain.begin())->getDecl();
597}
598
599// TODO: This function constructs an incorrect fieldchain string in the
600// following case:
601//
602// struct Base { int x; };
603// struct D1 : Base {}; struct D2 : Base {};
604//
605// struct MostDerived : D1, D2 {
606// MostDerived() {}
607// }
608//
609// A call to MostDerived::MostDerived() will cause two notes that say
610// "uninitialized field 'this->x'", but we can't refer to 'x' directly,
611// we need an explicit namespace resolution whether the uninit field was
612// 'D1::x' or 'D2::x'.
Kristof Umann30f08652018-06-18 11:50:17 +0000613void FieldChainInfo::print(llvm::raw_ostream &Out) const {
614 if (Chain.isEmpty())
615 return;
616
617 const llvm::ImmutableListImpl<const FieldRegion *> *L =
618 Chain.getInternalPointer();
619 printTail(Out, L->getTail());
Kristof Umann8c119092018-07-13 12:54:47 +0000620 Out << getVariableName(L->getHead()->getDecl());
Kristof Umann30f08652018-06-18 11:50:17 +0000621}
622
623void FieldChainInfo::printTail(
624 llvm::raw_ostream &Out,
625 const llvm::ImmutableListImpl<const FieldRegion *> *L) {
626 if (!L)
627 return;
628
629 printTail(Out, L->getTail());
630 const FieldDecl *Field = L->getHead()->getDecl();
Kristof Umann8c119092018-07-13 12:54:47 +0000631 Out << getVariableName(Field);
Kristof Umann30f08652018-06-18 11:50:17 +0000632 Out << (Field->getType()->isPointerType() ? "->" : ".");
633}
634
635//===----------------------------------------------------------------------===//
636// Utility functions.
637//===----------------------------------------------------------------------===//
638
Kristof Umanncc852442018-07-12 13:13:46 +0000639static bool isVoidPointer(const FieldDecl *FD) {
Kristof Umann30f08652018-06-18 11:50:17 +0000640 QualType T = FD->getType();
641
642 while (!T.isNull()) {
643 if (T->isVoidPointerType())
644 return true;
645 T = T->getPointeeType();
646 }
647 return false;
648}
649
Kristof Umanncc852442018-07-12 13:13:46 +0000650static Optional<nonloc::LazyCompoundVal>
Kristof Umann30f08652018-06-18 11:50:17 +0000651getObjectVal(const CXXConstructorDecl *CtorDecl, CheckerContext &Context) {
652
653 Loc ThisLoc = Context.getSValBuilder().getCXXThis(CtorDecl->getParent(),
654 Context.getStackFrame());
655 // Getting the value for 'this'.
656 SVal This = Context.getState()->getSVal(ThisLoc);
657
658 // Getting the value for '*this'.
659 SVal Object = Context.getState()->getSVal(This.castAs<Loc>());
660
661 return Object.getAs<nonloc::LazyCompoundVal>();
662}
663
664// TODO: We should also check that if the constructor was called by another
665// constructor, whether those two are in any relation to one another. In it's
666// current state, this introduces some false negatives.
Kristof Umanncc852442018-07-12 13:13:46 +0000667static bool isCalledByConstructor(const CheckerContext &Context) {
Kristof Umann30f08652018-06-18 11:50:17 +0000668 const LocationContext *LC = Context.getLocationContext()->getParent();
669
670 while (LC) {
671 if (isa<CXXConstructorDecl>(LC->getDecl()))
672 return true;
673
674 LC = LC->getParent();
675 }
676 return false;
677}
678
Kristof Umanncc852442018-07-12 13:13:46 +0000679static void printNoteMessage(llvm::raw_ostream &Out,
680 const FieldChainInfo &Chain) {
Kristof Umann9bd44392018-06-29 11:25:24 +0000681 if (Chain.isPointer()) {
682 if (Chain.isDereferenced())
683 Out << "uninitialized pointee 'this->";
684 else
685 Out << "uninitialized pointer 'this->";
686 } else
687 Out << "uninitialized field 'this->";
688 Chain.print(Out);
689 Out << "'";
690}
691
Kristof Umann8c119092018-07-13 12:54:47 +0000692static StringRef getVariableName(const FieldDecl *Field) {
693 // If Field is a captured lambda variable, Field->getName() will return with
694 // an empty string. We can however acquire it's name from the lambda's
695 // captures.
696 const auto *CXXParent = dyn_cast<CXXRecordDecl>(Field->getParent());
697
698 if (CXXParent && CXXParent->isLambda()) {
699 assert(CXXParent->captures_begin());
700 auto It = CXXParent->captures_begin() + Field->getFieldIndex();
701 return It->getCapturedVar()->getName();
702 }
703
704 return Field->getName();
705}
706
Kristof Umann30f08652018-06-18 11:50:17 +0000707void ento::registerUninitializedObjectChecker(CheckerManager &Mgr) {
708 auto Chk = Mgr.registerChecker<UninitializedObjectChecker>();
709 Chk->IsPedantic = Mgr.getAnalyzerOptions().getBooleanOption(
710 "Pedantic", /*DefaultVal*/ false, Chk);
Kristof Umann9bd44392018-06-29 11:25:24 +0000711 Chk->ShouldConvertNotesToWarnings = Mgr.getAnalyzerOptions().getBooleanOption(
712 "NotesAsWarnings", /*DefaultVal*/ false, Chk);
Kristof Umanna3f7b582018-08-07 12:55:26 +0000713 Chk->CheckPointeeInitialization = Mgr.getAnalyzerOptions().getBooleanOption(
714 "CheckPointeeInitialization", /*DefaultVal*/ false, Chk);
Kristof Umann30f08652018-06-18 11:50:17 +0000715}