blob: e4a17a9dd28ca6d98c0044dd08a3f27a19ec35ac [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 Umann6cec6c42018-09-14 09:39:26 +000013// 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 Umann56963ae2018-08-13 18:17:05 +000015//
16// Some of the logic is implemented in UninitializedPointee.cpp, to reduce the
17// complexity of this file.
18//
Kristof Umann30f08652018-06-18 11:50:17 +000019//===----------------------------------------------------------------------===//
20
Richard Smith651d6832018-08-13 22:07:11 +000021#include "../ClangSACheckers.h"
Kristof Umanna37bba42018-08-13 18:22:22 +000022#include "UninitializedObject.h"
Kristof Umann30f08652018-06-18 11:50:17 +000023#include "clang/StaticAnalyzer/Core/BugReporter/BugType.h"
24#include "clang/StaticAnalyzer/Core/Checker.h"
25#include "clang/StaticAnalyzer/Core/PathSensitive/CheckerContext.h"
Kristof Umannef9af052018-08-08 13:18:53 +000026#include "clang/StaticAnalyzer/Core/PathSensitive/DynamicTypeMap.h"
Kristof Umann30f08652018-06-18 11:50:17 +000027
28using namespace clang;
29using namespace clang::ento;
30
31namespace {
32
33class UninitializedObjectChecker : public Checker<check::EndFunction> {
34 std::unique_ptr<BuiltinBug> BT_uninitField;
35
36public:
Kristof Umann6cec6c42018-09-14 09:39:26 +000037 // The fields of this struct will be initialized when registering the checker.
38 UninitObjCheckerOptions Opts;
Kristof Umann30f08652018-06-18 11:50:17 +000039
40 UninitializedObjectChecker()
41 : BT_uninitField(new BuiltinBug(this, "Uninitialized fields")) {}
Reka Kovacsed8c05c2018-07-16 20:47:45 +000042 void checkEndFunction(const ReturnStmt *RS, CheckerContext &C) const;
Kristof Umann30f08652018-06-18 11:50:17 +000043};
44
Kristof Umann015b0592018-08-13 18:43:08 +000045/// A basic field type, that is not a pointer or a reference, it's dynamic and
46/// static type is the same.
Richard Smith651d6832018-08-13 22:07:11 +000047class RegularField final : public FieldNode {
Kristof Umann015b0592018-08-13 18:43:08 +000048public:
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 Smith651d6832018-08-13 22:07:11 +000057 virtual void printNode(llvm::raw_ostream &Out) const override {
Kristof Umann015b0592018-08-13 18:43:08 +000058 Out << getVariableName(getDecl());
59 }
60
61 virtual void printSeparator(llvm::raw_ostream &Out) const override {
62 Out << '.';
63 }
64};
65
Kristof Umannb59b45e2018-08-21 12:16:59 +000066/// Represents that the FieldNode that comes after this is declared in a base
Kristof Umannceb5f652018-09-14 09:07:40 +000067/// 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 Umannb59b45e2018-08-21 12:16:59 +000070class BaseClass final : public FieldNode {
71 const QualType BaseClassT;
72
73public:
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 Umann06209cb2018-08-21 15:09:22 +000092 virtual bool isBase() const override { return true; }
Kristof Umannb59b45e2018-08-21 12:16:59 +000093};
94
Kristof Umanncc852442018-07-12 13:13:46 +000095} // end of anonymous namespace
96
Kristof Umann30f08652018-06-18 11:50:17 +000097// Utility function declarations.
98
Kristof Umanndbabdfa2018-10-21 23:30:01 +000099/// Returns the region that was constructed by CtorDecl, or nullptr if that
100/// isn't possible.
101static const TypedValueRegion *
102getConstructedRegion(const CXXConstructorDecl *CtorDecl,
103 CheckerContext &Context);
Kristof Umann30f08652018-06-18 11:50:17 +0000104
Kristof Umann0735cfb2018-08-08 12:23:02 +0000105/// 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).
108static bool willObjectBeAnalyzedLater(const CXXConstructorDecl *Ctor,
Kristof Umanna37bba42018-08-13 18:22:22 +0000109 CheckerContext &Context);
Kristof Umann30f08652018-06-18 11:50:17 +0000110
Kristof Umannd6145d92018-09-14 10:10:09 +0000111/// Checks whether RD contains a field with a name or type name that matches
112/// \p Pattern.
113static bool shouldIgnoreRecord(const RecordDecl *RD, StringRef Pattern);
114
Kristof Umann30f08652018-06-18 11:50:17 +0000115//===----------------------------------------------------------------------===//
116// Methods for UninitializedObjectChecker.
117//===----------------------------------------------------------------------===//
118
119void UninitializedObjectChecker::checkEndFunction(
Reka Kovacsed8c05c2018-07-16 20:47:45 +0000120 const ReturnStmt *RS, CheckerContext &Context) const {
Kristof Umann30f08652018-06-18 11:50:17 +0000121
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 Umann0735cfb2018-08-08 12:23:02 +0000134 if (willObjectBeAnalyzedLater(CtorDecl, Context))
Kristof Umann30f08652018-06-18 11:50:17 +0000135 return;
136
Kristof Umanndbabdfa2018-10-21 23:30:01 +0000137 const TypedValueRegion *R = getConstructedRegion(CtorDecl, Context);
138 if (!R)
Kristof Umann30f08652018-06-18 11:50:17 +0000139 return;
140
Kristof Umanndbabdfa2018-10-21 23:30:01 +0000141 FindUninitializedFields F(Context.getState(), R, Opts);
Kristof Umann30f08652018-06-18 11:50:17 +0000142
Kristof Umann015b0592018-08-13 18:43:08 +0000143 const UninitFieldMap &UninitFields = F.getUninitFields();
Kristof Umann30f08652018-06-18 11:50:17 +0000144
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 Umann9bd44392018-06-29 11:25:24 +0000160 // For Plist consumers that don't support notes just yet, we'll convert notes
161 // to warnings.
Kristof Umann6cec6c42018-09-14 09:39:26 +0000162 if (Opts.ShouldConvertNotesToWarnings) {
Kristof Umann015b0592018-08-13 18:43:08 +0000163 for (const auto &Pair : UninitFields) {
Kristof Umann9bd44392018-06-29 11:25:24 +0000164
165 auto Report = llvm::make_unique<BugReport>(
Kristof Umann015b0592018-08-13 18:43:08 +0000166 *BT_uninitField, Pair.second, Node, LocUsedForUniqueing,
Kristof Umann9bd44392018-06-29 11:25:24 +0000167 Node->getLocationContext()->getDecl());
168 Context.emitReport(std::move(Report));
169 }
170 return;
171 }
172
Kristof Umann30f08652018-06-18 11:50:17 +0000173 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 Umann015b0592018-08-13 18:43:08 +0000183 for (const auto &Pair : UninitFields) {
184 Report->addNote(Pair.second,
185 PathDiagnosticLocation::create(Pair.first->getDecl(),
Kristof Umann30f08652018-06-18 11:50:17 +0000186 Context.getSourceManager()));
187 }
Kristof Umann30f08652018-06-18 11:50:17 +0000188 Context.emitReport(std::move(Report));
189}
190
191//===----------------------------------------------------------------------===//
192// Methods for FindUninitializedFields.
193//===----------------------------------------------------------------------===//
194
195FindUninitializedFields::FindUninitializedFields(
Kristof Umann23ca9662018-08-13 18:48:34 +0000196 ProgramStateRef State, const TypedValueRegion *const R,
Kristof Umann6cec6c42018-09-14 09:39:26 +0000197 const UninitObjCheckerOptions &Opts)
198 : State(State), ObjectR(R), Opts(Opts) {
Kristof Umann30f08652018-06-18 11:50:17 +0000199
Kristof Umann015b0592018-08-13 18:43:08 +0000200 isNonUnionUninit(ObjectR, FieldChainInfo(ChainFactory));
Kristof Umann6cec6c42018-09-14 09:39:26 +0000201
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 Umann30f08652018-06-18 11:50:17 +0000206}
207
208bool FindUninitializedFields::addFieldToUninits(FieldChainInfo Chain) {
209 if (State->getStateManager().getContext().getSourceManager().isInSystemHeader(
Kristof Umann015b0592018-08-13 18:43:08 +0000210 Chain.getUninitRegion()->getDecl()->getLocation()))
Kristof Umann30f08652018-06-18 11:50:17 +0000211 return false;
212
Kristof Umann015b0592018-08-13 18:43:08 +0000213 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 Umann30f08652018-06-18 11:50:17 +0000219}
220
221bool 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 Umannf0dd1012018-09-14 08:58:21 +0000227 const RecordDecl *RD = R->getValueType()->getAsRecordDecl()->getDefinition();
228
229 if (!RD) {
230 IsAnyFieldInitialized = true;
231 return true;
232 }
Kristof Umann30f08652018-06-18 11:50:17 +0000233
Kristof Umannd6145d92018-09-14 10:10:09 +0000234 if (!Opts.IgnoredRecordsWithFieldPattern.empty() &&
235 shouldIgnoreRecord(RD, Opts.IgnoredRecordsWithFieldPattern)) {
236 IsAnyFieldInitialized = true;
237 return false;
238 }
239
Kristof Umann30f08652018-06-18 11:50:17 +0000240 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 Umann015b0592018-08-13 18:43:08 +0000257 if (isNonUnionUninit(FR, LocalChain.add(RegularField(FR))))
Kristof Umann30f08652018-06-18 11:50:17 +0000258 ContainsUninitField = true;
259 continue;
260 }
261
262 if (T->isUnionType()) {
263 if (isUnionUninit(FR)) {
Kristof Umann015b0592018-08-13 18:43:08 +0000264 if (addFieldToUninits(LocalChain.add(RegularField(FR))))
Kristof Umann30f08652018-06-18 11:50:17 +0000265 ContainsUninitField = true;
266 } else
267 IsAnyFieldInitialized = true;
268 continue;
269 }
270
271 if (T->isArrayType()) {
272 IsAnyFieldInitialized = true;
273 continue;
274 }
275
Kristof Umannf0513792018-09-14 10:18:26 +0000276 SVal V = State->getSVal(FieldVal);
277
278 if (isDereferencableType(T) || V.getAs<nonloc::LocAsInteger>()) {
Kristof Umannceb5f652018-09-14 09:07:40 +0000279 if (isDereferencableUninit(FR, LocalChain))
Kristof Umann30f08652018-06-18 11:50:17 +0000280 ContainsUninitField = true;
281 continue;
282 }
283
Kristof Umann20e85ba2018-06-19 08:35:02 +0000284 if (isPrimitiveType(T)) {
Kristof Umann20e85ba2018-06-19 08:35:02 +0000285 if (isPrimitiveUninit(V)) {
Kristof Umann015b0592018-08-13 18:43:08 +0000286 if (addFieldToUninits(LocalChain.add(RegularField(FR))))
Kristof Umann20e85ba2018-06-19 08:35:02 +0000287 ContainsUninitField = true;
288 }
289 continue;
Kristof Umann30f08652018-06-18 11:50:17 +0000290 }
Kristof Umann20e85ba2018-06-19 08:35:02 +0000291
292 llvm_unreachable("All cases are handled!");
Kristof Umann30f08652018-06-18 11:50:17 +0000293 }
294
Kristof Umannceb5f652018-09-14 09:07:40 +0000295 // Checking bases. The checker will regard inherited data members as direct
296 // fields.
Kristof Umann30f08652018-06-18 11:50:17 +0000297 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 Umannb59b45e2018-08-21 12:16:59 +0000306 // 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 Umann30f08652018-06-18 11:50:17 +0000317 }
318
319 return ContainsUninitField;
320}
321
322bool 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 Umann30f08652018-06-18 11:50:17 +0000329bool 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 Umann015b0592018-08-13 18:43:08 +0000341bool 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 Umann30f08652018-06-18 11:50:17 +0000347}
348
Kristof Umanna37bba42018-08-13 18:22:22 +0000349/// 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 Umannceb5f652018-09-14 09:07:40 +0000352/// the chain is to be printed by `FieldChainInfo::print`.
Kristof Umanna37bba42018-08-13 18:22:22 +0000353static void printTail(llvm::raw_ostream &Out,
Kristof Umann82eeca32018-09-23 09:16:27 +0000354 const FieldChainInfo::FieldChain L);
Kristof Umanna37bba42018-08-13 18:22:22 +0000355
Kristof Umannceb5f652018-09-14 09:07:40 +0000356// FIXME: This function constructs an incorrect string in the following case:
Kristof Umann30f08652018-06-18 11:50:17 +0000357//
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 Umann015b0592018-08-13 18:43:08 +0000369void FieldChainInfo::printNoteMsg(llvm::raw_ostream &Out) const {
Kristof Umann30f08652018-06-18 11:50:17 +0000370 if (Chain.isEmpty())
371 return;
372
Kristof Umann82eeca32018-09-23 09:16:27 +0000373 const FieldNode &LastField = getHead();
Kristof Umann015b0592018-08-13 18:43:08 +0000374
375 LastField.printNoteMsg(Out);
376 Out << '\'';
377
378 for (const FieldNode &Node : Chain)
379 Node.printPrefix(Out);
380
381 Out << "this->";
Kristof Umann82eeca32018-09-23 09:16:27 +0000382 printTail(Out, Chain.getTail());
Kristof Umann015b0592018-08-13 18:43:08 +0000383 LastField.printNode(Out);
384 Out << '\'';
Kristof Umann30f08652018-06-18 11:50:17 +0000385}
386
Kristof Umanna37bba42018-08-13 18:22:22 +0000387static void printTail(llvm::raw_ostream &Out,
Kristof Umann82eeca32018-09-23 09:16:27 +0000388 const FieldChainInfo::FieldChain L) {
389 if (L.isEmpty())
Kristof Umann30f08652018-06-18 11:50:17 +0000390 return;
391
Kristof Umann82eeca32018-09-23 09:16:27 +0000392 printTail(Out, L.getTail());
Kristof Umann015b0592018-08-13 18:43:08 +0000393
Kristof Umann82eeca32018-09-23 09:16:27 +0000394 L.getHead().printNode(Out);
395 L.getHead().printSeparator(Out);
Kristof Umann30f08652018-06-18 11:50:17 +0000396}
397
398//===----------------------------------------------------------------------===//
399// Utility functions.
400//===----------------------------------------------------------------------===//
401
Kristof Umanndbabdfa2018-10-21 23:30:01 +0000402static const TypedValueRegion *
403getConstructedRegion(const CXXConstructorDecl *CtorDecl,
404 CheckerContext &Context) {
Kristof Umann30f08652018-06-18 11:50:17 +0000405
Kristof Umanndbabdfa2018-10-21 23:30:01 +0000406 Loc ThisLoc = Context.getSValBuilder().getCXXThis(CtorDecl,
Kristof Umann30f08652018-06-18 11:50:17 +0000407 Context.getStackFrame());
Kristof Umann30f08652018-06-18 11:50:17 +0000408
Kristof Umanndbabdfa2018-10-21 23:30:01 +0000409 SVal ObjectV = Context.getState()->getSVal(ThisLoc);
Kristof Umann30f08652018-06-18 11:50:17 +0000410
Kristof Umanndbabdfa2018-10-21 23:30:01 +0000411 auto *R = ObjectV.getAsRegion()->getAs<TypedValueRegion>();
412 if (R && !R->getValueType()->getAsCXXRecordDecl())
413 return nullptr;
414
415 return R;
Kristof Umann30f08652018-06-18 11:50:17 +0000416}
417
Kristof Umann0735cfb2018-08-08 12:23:02 +0000418static bool willObjectBeAnalyzedLater(const CXXConstructorDecl *Ctor,
Kristof Umanna37bba42018-08-13 18:22:22 +0000419 CheckerContext &Context) {
Kristof Umann30f08652018-06-18 11:50:17 +0000420
Kristof Umanndbabdfa2018-10-21 23:30:01 +0000421 const TypedValueRegion *CurrRegion = getConstructedRegion(Ctor, Context);
422 if (!CurrRegion)
Kristof Umann0735cfb2018-08-08 12:23:02 +0000423 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 Umanndbabdfa2018-10-21 23:30:01 +0000433 const TypedValueRegion *OtherRegion =
434 getConstructedRegion(OtherCtor, Context);
435 if (!OtherRegion)
Kristof Umann0735cfb2018-08-08 12:23:02 +0000436 continue;
437
Kristof Umanndbabdfa2018-10-21 23:30:01 +0000438 // If the CurrRegion is a subregion of OtherRegion, it will be analyzed
439 // during the analysis of OtherRegion.
440 if (CurrRegion->isSubRegionOf(OtherRegion))
Kristof Umann30f08652018-06-18 11:50:17 +0000441 return true;
Kristof Umann30f08652018-06-18 11:50:17 +0000442 }
Kristof Umann0735cfb2018-08-08 12:23:02 +0000443
Kristof Umann30f08652018-06-18 11:50:17 +0000444 return false;
445}
446
Kristof Umannd6145d92018-09-14 10:10:09 +0000447static 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 Umannf0dd1012018-09-14 08:58:21 +0000460std::string clang::ento::getVariableName(const FieldDecl *Field) {
Kristof Umann8c119092018-07-13 12:54:47 +0000461 // 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 Umannf0dd1012018-09-14 08:58:21 +0000469
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 Umann8c119092018-07-13 12:54:47 +0000479 }
480
481 return Field->getName();
482}
483
Kristof Umann30f08652018-06-18 11:50:17 +0000484void ento::registerUninitializedObjectChecker(CheckerManager &Mgr) {
485 auto Chk = Mgr.registerChecker<UninitializedObjectChecker>();
Kristof Umannceb5f652018-09-14 09:07:40 +0000486
Kristof Umann6cec6c42018-09-14 09:39:26 +0000487 AnalyzerOptions &AnOpts = Mgr.getAnalyzerOptions();
488 UninitObjCheckerOptions &ChOpts = Chk->Opts;
489
Kristof Umannd6145d92018-09-14 10:10:09 +0000490 ChOpts.IsPedantic =
Kristof Umann0a1f91c2018-11-05 03:50:37 +0000491 AnOpts.getCheckerBooleanOption("Pedantic", /*DefaultVal*/ false, Chk);
Kristof Umannd6145d92018-09-14 10:10:09 +0000492 ChOpts.ShouldConvertNotesToWarnings =
Kristof Umann0a1f91c2018-11-05 03:50:37 +0000493 AnOpts.getCheckerBooleanOption("NotesAsWarnings", /*DefaultVal*/ false, Chk);
494 ChOpts.CheckPointeeInitialization = AnOpts.getCheckerBooleanOption(
Kristof Umanna3f7b582018-08-07 12:55:26 +0000495 "CheckPointeeInitialization", /*DefaultVal*/ false, Chk);
Kristof Umannd6145d92018-09-14 10:10:09 +0000496 ChOpts.IgnoredRecordsWithFieldPattern =
Kristof Umann0a1f91c2018-11-05 03:50:37 +0000497 AnOpts.getCheckerStringOption("IgnoreRecordsWithField",
Kristof Umannd6145d92018-09-14 10:10:09 +0000498 /*DefaultVal*/ "", Chk);
Kristof Umann30f08652018-06-18 11:50:17 +0000499}