blob: 84c18869d74f8aec1c90b298f4b9212d07f162ff [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//
Kristof Umann56963ae2018-08-13 18:17:05 +000041// 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 Umann30f08652018-06-18 11:50:17 +000047//===----------------------------------------------------------------------===//
48
Richard Smith651d6832018-08-13 22:07:11 +000049#include "../ClangSACheckers.h"
Kristof Umanna37bba42018-08-13 18:22:22 +000050#include "UninitializedObject.h"
Kristof Umann30f08652018-06-18 11:50:17 +000051#include "clang/StaticAnalyzer/Core/BugReporter/BugType.h"
52#include "clang/StaticAnalyzer/Core/Checker.h"
53#include "clang/StaticAnalyzer/Core/PathSensitive/CheckerContext.h"
Kristof Umannef9af052018-08-08 13:18:53 +000054#include "clang/StaticAnalyzer/Core/PathSensitive/DynamicTypeMap.h"
Kristof Umann30f08652018-06-18 11:50:17 +000055
56using namespace clang;
57using namespace clang::ento;
58
59namespace {
60
61class UninitializedObjectChecker : public Checker<check::EndFunction> {
62 std::unique_ptr<BuiltinBug> BT_uninitField;
63
64public:
Kristof Umann9bd44392018-06-29 11:25:24 +000065 // These fields will be initialized when registering the checker.
66 bool IsPedantic;
67 bool ShouldConvertNotesToWarnings;
Kristof Umanna3f7b582018-08-07 12:55:26 +000068 bool CheckPointeeInitialization;
Kristof Umann30f08652018-06-18 11:50:17 +000069
70 UninitializedObjectChecker()
71 : BT_uninitField(new BuiltinBug(this, "Uninitialized fields")) {}
Reka Kovacsed8c05c2018-07-16 20:47:45 +000072 void checkEndFunction(const ReturnStmt *RS, CheckerContext &C) const;
Kristof Umann30f08652018-06-18 11:50:17 +000073};
74
Kristof Umann015b0592018-08-13 18:43:08 +000075/// A basic field type, that is not a pointer or a reference, it's dynamic and
76/// static type is the same.
Richard Smith651d6832018-08-13 22:07:11 +000077class RegularField final : public FieldNode {
Kristof Umann015b0592018-08-13 18:43:08 +000078public:
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 Smith651d6832018-08-13 22:07:11 +000087 virtual void printNode(llvm::raw_ostream &Out) const override {
Kristof Umann015b0592018-08-13 18:43:08 +000088 Out << getVariableName(getDecl());
89 }
90
91 virtual void printSeparator(llvm::raw_ostream &Out) const override {
92 Out << '.';
93 }
94};
95
Kristof Umannb59b45e2018-08-21 12:16:59 +000096/// Represents that the FieldNode that comes after this is declared in a base
97/// of the previous FieldNode.
98class BaseClass final : public FieldNode {
99 const QualType BaseClassT;
100
101public:
102 BaseClass(const QualType &T) : FieldNode(nullptr), BaseClassT(T) {
103 assert(!T.isNull());
104 assert(T->getAsCXXRecordDecl());
105 }
106
107 virtual void printNoteMsg(llvm::raw_ostream &Out) const override {
108 llvm_unreachable("This node can never be the final node in the "
109 "fieldchain!");
110 }
111
112 virtual void printPrefix(llvm::raw_ostream &Out) const override {}
113
114 virtual void printNode(llvm::raw_ostream &Out) const override {
115 Out << BaseClassT->getAsCXXRecordDecl()->getName() << "::";
116 }
117
118 virtual void printSeparator(llvm::raw_ostream &Out) const override {}
119
Kristof Umann06209cb2018-08-21 15:09:22 +0000120 virtual bool isBase() const override { return true; }
Kristof Umannb59b45e2018-08-21 12:16:59 +0000121};
122
Kristof Umanncc852442018-07-12 13:13:46 +0000123} // end of anonymous namespace
124
Kristof Umann30f08652018-06-18 11:50:17 +0000125// Utility function declarations.
126
127/// Returns the object that was constructed by CtorDecl, or None if that isn't
128/// possible.
Kristof Umann0735cfb2018-08-08 12:23:02 +0000129// TODO: Refactor this function so that it returns the constructed object's
130// region.
Kristof Umanncc852442018-07-12 13:13:46 +0000131static Optional<nonloc::LazyCompoundVal>
Kristof Umann30f08652018-06-18 11:50:17 +0000132getObjectVal(const CXXConstructorDecl *CtorDecl, CheckerContext &Context);
133
Kristof Umann0735cfb2018-08-08 12:23:02 +0000134/// Checks whether the object constructed by \p Ctor will be analyzed later
135/// (e.g. if the object is a field of another object, in which case we'd check
136/// it multiple times).
137static bool willObjectBeAnalyzedLater(const CXXConstructorDecl *Ctor,
Kristof Umanna37bba42018-08-13 18:22:22 +0000138 CheckerContext &Context);
Kristof Umann30f08652018-06-18 11:50:17 +0000139
Kristof Umann30f08652018-06-18 11:50:17 +0000140//===----------------------------------------------------------------------===//
141// Methods for UninitializedObjectChecker.
142//===----------------------------------------------------------------------===//
143
144void UninitializedObjectChecker::checkEndFunction(
Reka Kovacsed8c05c2018-07-16 20:47:45 +0000145 const ReturnStmt *RS, CheckerContext &Context) const {
Kristof Umann30f08652018-06-18 11:50:17 +0000146
147 const auto *CtorDecl = dyn_cast_or_null<CXXConstructorDecl>(
148 Context.getLocationContext()->getDecl());
149 if (!CtorDecl)
150 return;
151
152 if (!CtorDecl->isUserProvided())
153 return;
154
155 if (CtorDecl->getParent()->isUnion())
156 return;
157
158 // This avoids essentially the same error being reported multiple times.
Kristof Umann0735cfb2018-08-08 12:23:02 +0000159 if (willObjectBeAnalyzedLater(CtorDecl, Context))
Kristof Umann30f08652018-06-18 11:50:17 +0000160 return;
161
162 Optional<nonloc::LazyCompoundVal> Object = getObjectVal(CtorDecl, Context);
163 if (!Object)
164 return;
165
Kristof Umann23ca9662018-08-13 18:48:34 +0000166 FindUninitializedFields F(Context.getState(), Object->getRegion(),
Kristof Umanna3f7b582018-08-07 12:55:26 +0000167 CheckPointeeInitialization);
Kristof Umann30f08652018-06-18 11:50:17 +0000168
Kristof Umann015b0592018-08-13 18:43:08 +0000169 const UninitFieldMap &UninitFields = F.getUninitFields();
Kristof Umann30f08652018-06-18 11:50:17 +0000170
171 if (UninitFields.empty())
172 return;
173
Kristof Umann23ca9662018-08-13 18:48:34 +0000174 // In non-pedantic mode, if Object's region doesn't contain a single
175 // initialized field, we'll assume that Object was intentionally left
176 // uninitialized.
177 if (!IsPedantic && !F.isAnyFieldInitialized())
178 return;
179
Kristof Umann30f08652018-06-18 11:50:17 +0000180 // There are uninitialized fields in the record.
181
182 ExplodedNode *Node = Context.generateNonFatalErrorNode(Context.getState());
183 if (!Node)
184 return;
185
186 PathDiagnosticLocation LocUsedForUniqueing;
187 const Stmt *CallSite = Context.getStackFrame()->getCallSite();
188 if (CallSite)
189 LocUsedForUniqueing = PathDiagnosticLocation::createBegin(
190 CallSite, Context.getSourceManager(), Node->getLocationContext());
191
Kristof Umann9bd44392018-06-29 11:25:24 +0000192 // For Plist consumers that don't support notes just yet, we'll convert notes
193 // to warnings.
194 if (ShouldConvertNotesToWarnings) {
Kristof Umann015b0592018-08-13 18:43:08 +0000195 for (const auto &Pair : UninitFields) {
Kristof Umann9bd44392018-06-29 11:25:24 +0000196
197 auto Report = llvm::make_unique<BugReport>(
Kristof Umann015b0592018-08-13 18:43:08 +0000198 *BT_uninitField, Pair.second, Node, LocUsedForUniqueing,
Kristof Umann9bd44392018-06-29 11:25:24 +0000199 Node->getLocationContext()->getDecl());
200 Context.emitReport(std::move(Report));
201 }
202 return;
203 }
204
Kristof Umann30f08652018-06-18 11:50:17 +0000205 SmallString<100> WarningBuf;
206 llvm::raw_svector_ostream WarningOS(WarningBuf);
207 WarningOS << UninitFields.size() << " uninitialized field"
208 << (UninitFields.size() == 1 ? "" : "s")
209 << " at the end of the constructor call";
210
211 auto Report = llvm::make_unique<BugReport>(
212 *BT_uninitField, WarningOS.str(), Node, LocUsedForUniqueing,
213 Node->getLocationContext()->getDecl());
214
Kristof Umann015b0592018-08-13 18:43:08 +0000215 for (const auto &Pair : UninitFields) {
216 Report->addNote(Pair.second,
217 PathDiagnosticLocation::create(Pair.first->getDecl(),
Kristof Umann30f08652018-06-18 11:50:17 +0000218 Context.getSourceManager()));
219 }
Kristof Umann30f08652018-06-18 11:50:17 +0000220 Context.emitReport(std::move(Report));
221}
222
223//===----------------------------------------------------------------------===//
224// Methods for FindUninitializedFields.
225//===----------------------------------------------------------------------===//
226
227FindUninitializedFields::FindUninitializedFields(
Kristof Umann23ca9662018-08-13 18:48:34 +0000228 ProgramStateRef State, const TypedValueRegion *const R,
Kristof Umanna3f7b582018-08-07 12:55:26 +0000229 bool CheckPointeeInitialization)
Kristof Umann23ca9662018-08-13 18:48:34 +0000230 : State(State), ObjectR(R),
231 CheckPointeeInitialization(CheckPointeeInitialization) {
Kristof Umann30f08652018-06-18 11:50:17 +0000232
Kristof Umann015b0592018-08-13 18:43:08 +0000233 isNonUnionUninit(ObjectR, FieldChainInfo(ChainFactory));
Kristof Umann30f08652018-06-18 11:50:17 +0000234}
235
236bool FindUninitializedFields::addFieldToUninits(FieldChainInfo Chain) {
237 if (State->getStateManager().getContext().getSourceManager().isInSystemHeader(
Kristof Umann015b0592018-08-13 18:43:08 +0000238 Chain.getUninitRegion()->getDecl()->getLocation()))
Kristof Umann30f08652018-06-18 11:50:17 +0000239 return false;
240
Kristof Umann015b0592018-08-13 18:43:08 +0000241 UninitFieldMap::mapped_type NoteMsgBuf;
242 llvm::raw_svector_ostream OS(NoteMsgBuf);
243 Chain.printNoteMsg(OS);
244 return UninitFields
245 .insert(std::make_pair(Chain.getUninitRegion(), std::move(NoteMsgBuf)))
246 .second;
Kristof Umann30f08652018-06-18 11:50:17 +0000247}
248
249bool FindUninitializedFields::isNonUnionUninit(const TypedValueRegion *R,
250 FieldChainInfo LocalChain) {
251 assert(R->getValueType()->isRecordType() &&
252 !R->getValueType()->isUnionType() &&
253 "This method only checks non-union record objects!");
254
Kristof Umannf0dd1012018-09-14 08:58:21 +0000255 const RecordDecl *RD = R->getValueType()->getAsRecordDecl()->getDefinition();
256
257 if (!RD) {
258 IsAnyFieldInitialized = true;
259 return true;
260 }
Kristof Umann30f08652018-06-18 11:50:17 +0000261
262 bool ContainsUninitField = false;
263
264 // Are all of this non-union's fields initialized?
265 for (const FieldDecl *I : RD->fields()) {
266
267 const auto FieldVal =
268 State->getLValue(I, loc::MemRegionVal(R)).castAs<loc::MemRegionVal>();
269 const auto *FR = FieldVal.getRegionAs<FieldRegion>();
270 QualType T = I->getType();
271
272 // If LocalChain already contains FR, then we encountered a cyclic
273 // reference. In this case, region FR is already under checking at an
274 // earlier node in the directed tree.
275 if (LocalChain.contains(FR))
276 return false;
277
278 if (T->isStructureOrClassType()) {
Kristof Umann015b0592018-08-13 18:43:08 +0000279 if (isNonUnionUninit(FR, LocalChain.add(RegularField(FR))))
Kristof Umann30f08652018-06-18 11:50:17 +0000280 ContainsUninitField = true;
281 continue;
282 }
283
284 if (T->isUnionType()) {
285 if (isUnionUninit(FR)) {
Kristof Umann015b0592018-08-13 18:43:08 +0000286 if (addFieldToUninits(LocalChain.add(RegularField(FR))))
Kristof Umann30f08652018-06-18 11:50:17 +0000287 ContainsUninitField = true;
288 } else
289 IsAnyFieldInitialized = true;
290 continue;
291 }
292
293 if (T->isArrayType()) {
294 IsAnyFieldInitialized = true;
295 continue;
296 }
297
Kristof Umannf0dd1012018-09-14 08:58:21 +0000298 if (isDereferencableType(T)) {
Kristof Umann30f08652018-06-18 11:50:17 +0000299 if (isPointerOrReferenceUninit(FR, LocalChain))
300 ContainsUninitField = true;
301 continue;
302 }
303
Kristof Umann20e85ba2018-06-19 08:35:02 +0000304 if (isPrimitiveType(T)) {
305 SVal V = State->getSVal(FieldVal);
Kristof Umann30f08652018-06-18 11:50:17 +0000306
Kristof Umann20e85ba2018-06-19 08:35:02 +0000307 if (isPrimitiveUninit(V)) {
Kristof Umann015b0592018-08-13 18:43:08 +0000308 if (addFieldToUninits(LocalChain.add(RegularField(FR))))
Kristof Umann20e85ba2018-06-19 08:35:02 +0000309 ContainsUninitField = true;
310 }
311 continue;
Kristof Umann30f08652018-06-18 11:50:17 +0000312 }
Kristof Umann20e85ba2018-06-19 08:35:02 +0000313
314 llvm_unreachable("All cases are handled!");
Kristof Umann30f08652018-06-18 11:50:17 +0000315 }
316
317 // Checking bases.
Kristof Umann30f08652018-06-18 11:50:17 +0000318 const auto *CXXRD = dyn_cast<CXXRecordDecl>(RD);
319 if (!CXXRD)
320 return ContainsUninitField;
321
322 for (const CXXBaseSpecifier &BaseSpec : CXXRD->bases()) {
323 const auto *BaseRegion = State->getLValue(BaseSpec, R)
324 .castAs<loc::MemRegionVal>()
325 .getRegionAs<TypedValueRegion>();
326
Kristof Umannb59b45e2018-08-21 12:16:59 +0000327 // If the head of the list is also a BaseClass, we'll overwrite it to avoid
328 // note messages like 'this->A::B::x'.
329 if (!LocalChain.isEmpty() && LocalChain.getHead().isBase()) {
330 if (isNonUnionUninit(BaseRegion, LocalChain.replaceHead(
331 BaseClass(BaseSpec.getType()))))
332 ContainsUninitField = true;
333 } else {
334 if (isNonUnionUninit(BaseRegion,
335 LocalChain.add(BaseClass(BaseSpec.getType()))))
336 ContainsUninitField = true;
337 }
Kristof Umann30f08652018-06-18 11:50:17 +0000338 }
339
340 return ContainsUninitField;
341}
342
343bool FindUninitializedFields::isUnionUninit(const TypedValueRegion *R) {
344 assert(R->getValueType()->isUnionType() &&
345 "This method only checks union objects!");
346 // TODO: Implement support for union fields.
347 return false;
348}
349
Kristof Umann30f08652018-06-18 11:50:17 +0000350bool FindUninitializedFields::isPrimitiveUninit(const SVal &V) {
351 if (V.isUndef())
352 return true;
353
354 IsAnyFieldInitialized = true;
355 return false;
356}
357
358//===----------------------------------------------------------------------===//
359// Methods for FieldChainInfo.
360//===----------------------------------------------------------------------===//
361
Kristof Umann015b0592018-08-13 18:43:08 +0000362const FieldRegion *FieldChainInfo::getUninitRegion() const {
Kristof Umann30f08652018-06-18 11:50:17 +0000363 assert(!Chain.isEmpty() && "Empty fieldchain!");
Kristof Umann015b0592018-08-13 18:43:08 +0000364 return (*Chain.begin()).getRegion();
Kristof Umann30f08652018-06-18 11:50:17 +0000365}
366
Kristof Umann015b0592018-08-13 18:43:08 +0000367bool FieldChainInfo::contains(const FieldRegion *FR) const {
368 for (const FieldNode &Node : Chain) {
369 if (Node.isSameRegion(FR))
370 return true;
371 }
372 return false;
Kristof Umann30f08652018-06-18 11:50:17 +0000373}
374
Kristof Umanna37bba42018-08-13 18:22:22 +0000375/// Prints every element except the last to `Out`. Since ImmutableLists store
376/// elements in reverse order, and have no reverse iterators, we use a
377/// recursive function to print the fieldchain correctly. The last element in
378/// the chain is to be printed by `print`.
379static void printTail(llvm::raw_ostream &Out,
380 const FieldChainInfo::FieldChainImpl *L);
381
Kristof Umannef9af052018-08-08 13:18:53 +0000382// TODO: This function constructs an incorrect string if a void pointer is a
383// part of the chain:
384//
385// struct B { int x; }
386//
387// struct A {
388// void *vptr;
389// A(void* vptr) : vptr(vptr) {}
390// };
391//
392// void f() {
393// B b;
394// A a(&b);
395// }
396//
397// The note message will be "uninitialized field 'this->vptr->x'", even though
398// void pointers can't be dereferenced. This should be changed to "uninitialized
399// field 'static_cast<B*>(this->vptr)->x'".
400//
Kristof Umann30f08652018-06-18 11:50:17 +0000401// TODO: This function constructs an incorrect fieldchain string in the
402// following case:
403//
404// struct Base { int x; };
405// struct D1 : Base {}; struct D2 : Base {};
406//
407// struct MostDerived : D1, D2 {
408// MostDerived() {}
409// }
410//
411// A call to MostDerived::MostDerived() will cause two notes that say
412// "uninitialized field 'this->x'", but we can't refer to 'x' directly,
413// we need an explicit namespace resolution whether the uninit field was
414// 'D1::x' or 'D2::x'.
Kristof Umann015b0592018-08-13 18:43:08 +0000415void FieldChainInfo::printNoteMsg(llvm::raw_ostream &Out) const {
Kristof Umann30f08652018-06-18 11:50:17 +0000416 if (Chain.isEmpty())
417 return;
418
Kristof Umanna37bba42018-08-13 18:22:22 +0000419 const FieldChainImpl *L = Chain.getInternalPointer();
Kristof Umann015b0592018-08-13 18:43:08 +0000420 const FieldNode &LastField = L->getHead();
421
422 LastField.printNoteMsg(Out);
423 Out << '\'';
424
425 for (const FieldNode &Node : Chain)
426 Node.printPrefix(Out);
427
428 Out << "this->";
Kristof Umann30f08652018-06-18 11:50:17 +0000429 printTail(Out, L->getTail());
Kristof Umann015b0592018-08-13 18:43:08 +0000430 LastField.printNode(Out);
431 Out << '\'';
Kristof Umann30f08652018-06-18 11:50:17 +0000432}
433
Kristof Umanna37bba42018-08-13 18:22:22 +0000434static void printTail(llvm::raw_ostream &Out,
435 const FieldChainInfo::FieldChainImpl *L) {
Kristof Umann30f08652018-06-18 11:50:17 +0000436 if (!L)
437 return;
438
439 printTail(Out, L->getTail());
Kristof Umann015b0592018-08-13 18:43:08 +0000440
441 L->getHead().printNode(Out);
442 L->getHead().printSeparator(Out);
Kristof Umann30f08652018-06-18 11:50:17 +0000443}
444
445//===----------------------------------------------------------------------===//
446// Utility functions.
447//===----------------------------------------------------------------------===//
448
Kristof Umanncc852442018-07-12 13:13:46 +0000449static Optional<nonloc::LazyCompoundVal>
Kristof Umann30f08652018-06-18 11:50:17 +0000450getObjectVal(const CXXConstructorDecl *CtorDecl, CheckerContext &Context) {
451
452 Loc ThisLoc = Context.getSValBuilder().getCXXThis(CtorDecl->getParent(),
453 Context.getStackFrame());
454 // Getting the value for 'this'.
455 SVal This = Context.getState()->getSVal(ThisLoc);
456
457 // Getting the value for '*this'.
458 SVal Object = Context.getState()->getSVal(This.castAs<Loc>());
459
460 return Object.getAs<nonloc::LazyCompoundVal>();
461}
462
Kristof Umann0735cfb2018-08-08 12:23:02 +0000463static bool willObjectBeAnalyzedLater(const CXXConstructorDecl *Ctor,
Kristof Umanna37bba42018-08-13 18:22:22 +0000464 CheckerContext &Context) {
Kristof Umann30f08652018-06-18 11:50:17 +0000465
Kristof Umann0735cfb2018-08-08 12:23:02 +0000466 Optional<nonloc::LazyCompoundVal> CurrentObject = getObjectVal(Ctor, Context);
467 if (!CurrentObject)
468 return false;
469
470 const LocationContext *LC = Context.getLocationContext();
471 while ((LC = LC->getParent())) {
472
473 // If \p Ctor was called by another constructor.
474 const auto *OtherCtor = dyn_cast<CXXConstructorDecl>(LC->getDecl());
475 if (!OtherCtor)
476 continue;
477
478 Optional<nonloc::LazyCompoundVal> OtherObject =
479 getObjectVal(OtherCtor, Context);
480 if (!OtherObject)
481 continue;
482
483 // If the CurrentObject is a subregion of OtherObject, it will be analyzed
484 // during the analysis of OtherObject.
485 if (CurrentObject->getRegion()->isSubRegionOf(OtherObject->getRegion()))
Kristof Umann30f08652018-06-18 11:50:17 +0000486 return true;
Kristof Umann30f08652018-06-18 11:50:17 +0000487 }
Kristof Umann0735cfb2018-08-08 12:23:02 +0000488
Kristof Umann30f08652018-06-18 11:50:17 +0000489 return false;
490}
491
Kristof Umannf0dd1012018-09-14 08:58:21 +0000492std::string clang::ento::getVariableName(const FieldDecl *Field) {
Kristof Umann8c119092018-07-13 12:54:47 +0000493 // If Field is a captured lambda variable, Field->getName() will return with
494 // an empty string. We can however acquire it's name from the lambda's
495 // captures.
496 const auto *CXXParent = dyn_cast<CXXRecordDecl>(Field->getParent());
497
498 if (CXXParent && CXXParent->isLambda()) {
499 assert(CXXParent->captures_begin());
500 auto It = CXXParent->captures_begin() + Field->getFieldIndex();
Kristof Umannf0dd1012018-09-14 08:58:21 +0000501
502 if (It->capturesVariable())
503 return llvm::Twine("/*captured variable*/" +
504 It->getCapturedVar()->getName())
505 .str();
506
507 if (It->capturesThis())
508 return "/*'this' capture*/";
509
510 llvm_unreachable("No other capture type is expected!");
Kristof Umann8c119092018-07-13 12:54:47 +0000511 }
512
513 return Field->getName();
514}
515
Kristof Umann30f08652018-06-18 11:50:17 +0000516void ento::registerUninitializedObjectChecker(CheckerManager &Mgr) {
517 auto Chk = Mgr.registerChecker<UninitializedObjectChecker>();
518 Chk->IsPedantic = Mgr.getAnalyzerOptions().getBooleanOption(
519 "Pedantic", /*DefaultVal*/ false, Chk);
Kristof Umann9bd44392018-06-29 11:25:24 +0000520 Chk->ShouldConvertNotesToWarnings = Mgr.getAnalyzerOptions().getBooleanOption(
521 "NotesAsWarnings", /*DefaultVal*/ false, Chk);
Kristof Umanna3f7b582018-08-07 12:55:26 +0000522 Chk->CheckPointeeInitialization = Mgr.getAnalyzerOptions().getBooleanOption(
523 "CheckPointeeInitialization", /*DefaultVal*/ false, Chk);
Kristof Umann30f08652018-06-18 11:50:17 +0000524}