blob: 94f664ab93bdb2cedc0e46f38d5de2ecd57f5d17 [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
Kristof Umann4ff77692018-11-18 11:34:10 +000031/// We'll mark fields (and pointee of fields) that are confirmed to be
32/// uninitialized as already analyzed.
33REGISTER_SET_WITH_PROGRAMSTATE(AnalyzedRegions, const MemRegion *)
34
Kristof Umann30f08652018-06-18 11:50:17 +000035namespace {
36
Kristof Umann4ff77692018-11-18 11:34:10 +000037class UninitializedObjectChecker
38 : public Checker<check::EndFunction, check::DeadSymbols> {
Kristof Umann30f08652018-06-18 11:50:17 +000039 std::unique_ptr<BuiltinBug> BT_uninitField;
40
41public:
Kristof Umann6cec6c42018-09-14 09:39:26 +000042 // The fields of this struct will be initialized when registering the checker.
43 UninitObjCheckerOptions Opts;
Kristof Umann30f08652018-06-18 11:50:17 +000044
45 UninitializedObjectChecker()
46 : BT_uninitField(new BuiltinBug(this, "Uninitialized fields")) {}
Kristof Umann4ff77692018-11-18 11:34:10 +000047
Reka Kovacsed8c05c2018-07-16 20:47:45 +000048 void checkEndFunction(const ReturnStmt *RS, CheckerContext &C) const;
Kristof Umann4ff77692018-11-18 11:34:10 +000049 void checkDeadSymbols(SymbolReaper &SR, CheckerContext &C) const;
Kristof Umann30f08652018-06-18 11:50:17 +000050};
51
Kristof Umann015b0592018-08-13 18:43:08 +000052/// A basic field type, that is not a pointer or a reference, it's dynamic and
53/// static type is the same.
Richard Smith651d6832018-08-13 22:07:11 +000054class RegularField final : public FieldNode {
Kristof Umann015b0592018-08-13 18:43:08 +000055public:
56 RegularField(const FieldRegion *FR) : FieldNode(FR) {}
57
58 virtual void printNoteMsg(llvm::raw_ostream &Out) const override {
59 Out << "uninitialized field ";
60 }
61
62 virtual void printPrefix(llvm::raw_ostream &Out) const override {}
63
Richard Smith651d6832018-08-13 22:07:11 +000064 virtual void printNode(llvm::raw_ostream &Out) const override {
Kristof Umann015b0592018-08-13 18:43:08 +000065 Out << getVariableName(getDecl());
66 }
67
68 virtual void printSeparator(llvm::raw_ostream &Out) const override {
69 Out << '.';
70 }
71};
72
Kristof Umannb59b45e2018-08-21 12:16:59 +000073/// Represents that the FieldNode that comes after this is declared in a base
Kristof Umannceb5f652018-09-14 09:07:40 +000074/// of the previous FieldNode. As such, this descendant doesn't wrap a
75/// FieldRegion, and is purely a tool to describe a relation between two other
76/// FieldRegion wrapping descendants.
Kristof Umannb59b45e2018-08-21 12:16:59 +000077class BaseClass final : public FieldNode {
78 const QualType BaseClassT;
79
80public:
81 BaseClass(const QualType &T) : FieldNode(nullptr), BaseClassT(T) {
82 assert(!T.isNull());
83 assert(T->getAsCXXRecordDecl());
84 }
85
86 virtual void printNoteMsg(llvm::raw_ostream &Out) const override {
87 llvm_unreachable("This node can never be the final node in the "
88 "fieldchain!");
89 }
90
91 virtual void printPrefix(llvm::raw_ostream &Out) const override {}
92
93 virtual void printNode(llvm::raw_ostream &Out) const override {
94 Out << BaseClassT->getAsCXXRecordDecl()->getName() << "::";
95 }
96
97 virtual void printSeparator(llvm::raw_ostream &Out) const override {}
98
Kristof Umann06209cb2018-08-21 15:09:22 +000099 virtual bool isBase() const override { return true; }
Kristof Umannb59b45e2018-08-21 12:16:59 +0000100};
101
Kristof Umanncc852442018-07-12 13:13:46 +0000102} // end of anonymous namespace
103
Kristof Umann30f08652018-06-18 11:50:17 +0000104// Utility function declarations.
105
Kristof Umanndbabdfa2018-10-21 23:30:01 +0000106/// Returns the region that was constructed by CtorDecl, or nullptr if that
107/// isn't possible.
108static const TypedValueRegion *
109getConstructedRegion(const CXXConstructorDecl *CtorDecl,
110 CheckerContext &Context);
Kristof Umann30f08652018-06-18 11:50:17 +0000111
Kristof Umann0735cfb2018-08-08 12:23:02 +0000112/// Checks whether the object constructed by \p Ctor will be analyzed later
113/// (e.g. if the object is a field of another object, in which case we'd check
114/// it multiple times).
115static bool willObjectBeAnalyzedLater(const CXXConstructorDecl *Ctor,
Kristof Umanna37bba42018-08-13 18:22:22 +0000116 CheckerContext &Context);
Kristof Umann30f08652018-06-18 11:50:17 +0000117
Kristof Umannd6145d92018-09-14 10:10:09 +0000118/// Checks whether RD contains a field with a name or type name that matches
119/// \p Pattern.
120static bool shouldIgnoreRecord(const RecordDecl *RD, StringRef Pattern);
121
Kristof Umann30f08652018-06-18 11:50:17 +0000122//===----------------------------------------------------------------------===//
123// Methods for UninitializedObjectChecker.
124//===----------------------------------------------------------------------===//
125
126void UninitializedObjectChecker::checkEndFunction(
Reka Kovacsed8c05c2018-07-16 20:47:45 +0000127 const ReturnStmt *RS, CheckerContext &Context) const {
Kristof Umann30f08652018-06-18 11:50:17 +0000128
129 const auto *CtorDecl = dyn_cast_or_null<CXXConstructorDecl>(
130 Context.getLocationContext()->getDecl());
131 if (!CtorDecl)
132 return;
133
134 if (!CtorDecl->isUserProvided())
135 return;
136
137 if (CtorDecl->getParent()->isUnion())
138 return;
139
140 // This avoids essentially the same error being reported multiple times.
Kristof Umann0735cfb2018-08-08 12:23:02 +0000141 if (willObjectBeAnalyzedLater(CtorDecl, Context))
Kristof Umann30f08652018-06-18 11:50:17 +0000142 return;
143
Kristof Umanndbabdfa2018-10-21 23:30:01 +0000144 const TypedValueRegion *R = getConstructedRegion(CtorDecl, Context);
145 if (!R)
Kristof Umann30f08652018-06-18 11:50:17 +0000146 return;
147
Kristof Umanndbabdfa2018-10-21 23:30:01 +0000148 FindUninitializedFields F(Context.getState(), R, Opts);
Kristof Umann30f08652018-06-18 11:50:17 +0000149
Kristof Umann4ff77692018-11-18 11:34:10 +0000150 std::pair<ProgramStateRef, const UninitFieldMap &> UninitInfo =
151 F.getResults();
Kristof Umann30f08652018-06-18 11:50:17 +0000152
Kristof Umann4ff77692018-11-18 11:34:10 +0000153 ProgramStateRef UpdatedState = UninitInfo.first;
154 const UninitFieldMap &UninitFields = UninitInfo.second;
155
156 if (UninitFields.empty()) {
157 Context.addTransition(UpdatedState);
Kristof Umann30f08652018-06-18 11:50:17 +0000158 return;
Kristof Umann4ff77692018-11-18 11:34:10 +0000159 }
Kristof Umann30f08652018-06-18 11:50:17 +0000160
161 // There are uninitialized fields in the record.
162
Kristof Umann4ff77692018-11-18 11:34:10 +0000163 ExplodedNode *Node = Context.generateNonFatalErrorNode(UpdatedState);
Kristof Umann30f08652018-06-18 11:50:17 +0000164 if (!Node)
165 return;
166
167 PathDiagnosticLocation LocUsedForUniqueing;
168 const Stmt *CallSite = Context.getStackFrame()->getCallSite();
169 if (CallSite)
170 LocUsedForUniqueing = PathDiagnosticLocation::createBegin(
171 CallSite, Context.getSourceManager(), Node->getLocationContext());
172
Kristof Umann9bd44392018-06-29 11:25:24 +0000173 // For Plist consumers that don't support notes just yet, we'll convert notes
174 // to warnings.
Kristof Umann6cec6c42018-09-14 09:39:26 +0000175 if (Opts.ShouldConvertNotesToWarnings) {
Kristof Umann015b0592018-08-13 18:43:08 +0000176 for (const auto &Pair : UninitFields) {
Kristof Umann9bd44392018-06-29 11:25:24 +0000177
178 auto Report = llvm::make_unique<BugReport>(
Kristof Umann015b0592018-08-13 18:43:08 +0000179 *BT_uninitField, Pair.second, Node, LocUsedForUniqueing,
Kristof Umann9bd44392018-06-29 11:25:24 +0000180 Node->getLocationContext()->getDecl());
181 Context.emitReport(std::move(Report));
182 }
183 return;
184 }
185
Kristof Umann30f08652018-06-18 11:50:17 +0000186 SmallString<100> WarningBuf;
187 llvm::raw_svector_ostream WarningOS(WarningBuf);
188 WarningOS << UninitFields.size() << " uninitialized field"
189 << (UninitFields.size() == 1 ? "" : "s")
190 << " at the end of the constructor call";
191
192 auto Report = llvm::make_unique<BugReport>(
193 *BT_uninitField, WarningOS.str(), Node, LocUsedForUniqueing,
194 Node->getLocationContext()->getDecl());
195
Kristof Umann015b0592018-08-13 18:43:08 +0000196 for (const auto &Pair : UninitFields) {
197 Report->addNote(Pair.second,
198 PathDiagnosticLocation::create(Pair.first->getDecl(),
Kristof Umann30f08652018-06-18 11:50:17 +0000199 Context.getSourceManager()));
200 }
Kristof Umann30f08652018-06-18 11:50:17 +0000201 Context.emitReport(std::move(Report));
202}
203
Kristof Umann4ff77692018-11-18 11:34:10 +0000204void UninitializedObjectChecker::checkDeadSymbols(SymbolReaper &SR,
205 CheckerContext &C) const {
206 ProgramStateRef State = C.getState();
207 for (const MemRegion *R : State->get<AnalyzedRegions>()) {
208 if (!SR.isLiveRegion(R))
209 State = State->remove<AnalyzedRegions>(R);
210 }
211}
212
Kristof Umann30f08652018-06-18 11:50:17 +0000213//===----------------------------------------------------------------------===//
214// Methods for FindUninitializedFields.
215//===----------------------------------------------------------------------===//
216
217FindUninitializedFields::FindUninitializedFields(
Kristof Umann23ca9662018-08-13 18:48:34 +0000218 ProgramStateRef State, const TypedValueRegion *const R,
Kristof Umann6cec6c42018-09-14 09:39:26 +0000219 const UninitObjCheckerOptions &Opts)
220 : State(State), ObjectR(R), Opts(Opts) {
Kristof Umann30f08652018-06-18 11:50:17 +0000221
Kristof Umann015b0592018-08-13 18:43:08 +0000222 isNonUnionUninit(ObjectR, FieldChainInfo(ChainFactory));
Kristof Umann6cec6c42018-09-14 09:39:26 +0000223
224 // In non-pedantic mode, if ObjectR doesn't contain a single initialized
225 // field, we'll assume that Object was intentionally left uninitialized.
226 if (!Opts.IsPedantic && !isAnyFieldInitialized())
227 UninitFields.clear();
Kristof Umann30f08652018-06-18 11:50:17 +0000228}
229
Kristof Umann4ff77692018-11-18 11:34:10 +0000230bool FindUninitializedFields::addFieldToUninits(FieldChainInfo Chain,
231 const MemRegion *PointeeR) {
232 const FieldRegion *FR = Chain.getUninitRegion();
233
234 assert((PointeeR || !isDereferencableType(FR->getDecl()->getType())) &&
235 "One must also pass the pointee region as a parameter for "
Raphael Isemannb23ccec2018-12-10 12:37:46 +0000236 "dereferenceable fields!");
Kristof Umann4ff77692018-11-18 11:34:10 +0000237
238 if (State->contains<AnalyzedRegions>(FR))
239 return false;
240
241 if (PointeeR) {
242 if (State->contains<AnalyzedRegions>(PointeeR)) {
243 return false;
244 }
245 State = State->add<AnalyzedRegions>(PointeeR);
246 }
247
248 State = State->add<AnalyzedRegions>(FR);
249
Kristof Umann30f08652018-06-18 11:50:17 +0000250 if (State->getStateManager().getContext().getSourceManager().isInSystemHeader(
Kristof Umann4ff77692018-11-18 11:34:10 +0000251 FR->getDecl()->getLocation()))
Kristof Umann30f08652018-06-18 11:50:17 +0000252 return false;
253
Kristof Umann015b0592018-08-13 18:43:08 +0000254 UninitFieldMap::mapped_type NoteMsgBuf;
255 llvm::raw_svector_ostream OS(NoteMsgBuf);
256 Chain.printNoteMsg(OS);
Kristof Umann4ff77692018-11-18 11:34:10 +0000257 return UninitFields.insert({FR, std::move(NoteMsgBuf)}).second;
Kristof Umann30f08652018-06-18 11:50:17 +0000258}
259
260bool FindUninitializedFields::isNonUnionUninit(const TypedValueRegion *R,
261 FieldChainInfo LocalChain) {
262 assert(R->getValueType()->isRecordType() &&
263 !R->getValueType()->isUnionType() &&
264 "This method only checks non-union record objects!");
265
Kristof Umannf0dd1012018-09-14 08:58:21 +0000266 const RecordDecl *RD = R->getValueType()->getAsRecordDecl()->getDefinition();
267
268 if (!RD) {
269 IsAnyFieldInitialized = true;
270 return true;
271 }
Kristof Umann30f08652018-06-18 11:50:17 +0000272
Kristof Umannd6145d92018-09-14 10:10:09 +0000273 if (!Opts.IgnoredRecordsWithFieldPattern.empty() &&
274 shouldIgnoreRecord(RD, Opts.IgnoredRecordsWithFieldPattern)) {
275 IsAnyFieldInitialized = true;
276 return false;
277 }
278
Kristof Umann30f08652018-06-18 11:50:17 +0000279 bool ContainsUninitField = false;
280
281 // Are all of this non-union's fields initialized?
282 for (const FieldDecl *I : RD->fields()) {
283
284 const auto FieldVal =
285 State->getLValue(I, loc::MemRegionVal(R)).castAs<loc::MemRegionVal>();
286 const auto *FR = FieldVal.getRegionAs<FieldRegion>();
287 QualType T = I->getType();
288
289 // If LocalChain already contains FR, then we encountered a cyclic
290 // reference. In this case, region FR is already under checking at an
291 // earlier node in the directed tree.
292 if (LocalChain.contains(FR))
293 return false;
294
295 if (T->isStructureOrClassType()) {
Kristof Umann015b0592018-08-13 18:43:08 +0000296 if (isNonUnionUninit(FR, LocalChain.add(RegularField(FR))))
Kristof Umann30f08652018-06-18 11:50:17 +0000297 ContainsUninitField = true;
298 continue;
299 }
300
301 if (T->isUnionType()) {
302 if (isUnionUninit(FR)) {
Kristof Umann015b0592018-08-13 18:43:08 +0000303 if (addFieldToUninits(LocalChain.add(RegularField(FR))))
Kristof Umann30f08652018-06-18 11:50:17 +0000304 ContainsUninitField = true;
305 } else
306 IsAnyFieldInitialized = true;
307 continue;
308 }
309
310 if (T->isArrayType()) {
311 IsAnyFieldInitialized = true;
312 continue;
313 }
314
Kristof Umannf0513792018-09-14 10:18:26 +0000315 SVal V = State->getSVal(FieldVal);
316
317 if (isDereferencableType(T) || V.getAs<nonloc::LocAsInteger>()) {
Kristof Umannceb5f652018-09-14 09:07:40 +0000318 if (isDereferencableUninit(FR, LocalChain))
Kristof Umann30f08652018-06-18 11:50:17 +0000319 ContainsUninitField = true;
320 continue;
321 }
322
Kristof Umann20e85ba2018-06-19 08:35:02 +0000323 if (isPrimitiveType(T)) {
Kristof Umann20e85ba2018-06-19 08:35:02 +0000324 if (isPrimitiveUninit(V)) {
Kristof Umann015b0592018-08-13 18:43:08 +0000325 if (addFieldToUninits(LocalChain.add(RegularField(FR))))
Kristof Umann20e85ba2018-06-19 08:35:02 +0000326 ContainsUninitField = true;
327 }
328 continue;
Kristof Umann30f08652018-06-18 11:50:17 +0000329 }
Kristof Umann20e85ba2018-06-19 08:35:02 +0000330
331 llvm_unreachable("All cases are handled!");
Kristof Umann30f08652018-06-18 11:50:17 +0000332 }
333
Kristof Umannceb5f652018-09-14 09:07:40 +0000334 // Checking bases. The checker will regard inherited data members as direct
335 // fields.
Kristof Umann30f08652018-06-18 11:50:17 +0000336 const auto *CXXRD = dyn_cast<CXXRecordDecl>(RD);
337 if (!CXXRD)
338 return ContainsUninitField;
339
340 for (const CXXBaseSpecifier &BaseSpec : CXXRD->bases()) {
341 const auto *BaseRegion = State->getLValue(BaseSpec, R)
342 .castAs<loc::MemRegionVal>()
343 .getRegionAs<TypedValueRegion>();
344
Kristof Umannb59b45e2018-08-21 12:16:59 +0000345 // If the head of the list is also a BaseClass, we'll overwrite it to avoid
346 // note messages like 'this->A::B::x'.
347 if (!LocalChain.isEmpty() && LocalChain.getHead().isBase()) {
348 if (isNonUnionUninit(BaseRegion, LocalChain.replaceHead(
349 BaseClass(BaseSpec.getType()))))
350 ContainsUninitField = true;
351 } else {
352 if (isNonUnionUninit(BaseRegion,
353 LocalChain.add(BaseClass(BaseSpec.getType()))))
354 ContainsUninitField = true;
355 }
Kristof Umann30f08652018-06-18 11:50:17 +0000356 }
357
358 return ContainsUninitField;
359}
360
361bool FindUninitializedFields::isUnionUninit(const TypedValueRegion *R) {
362 assert(R->getValueType()->isUnionType() &&
363 "This method only checks union objects!");
364 // TODO: Implement support for union fields.
365 return false;
366}
367
Kristof Umann30f08652018-06-18 11:50:17 +0000368bool FindUninitializedFields::isPrimitiveUninit(const SVal &V) {
369 if (V.isUndef())
370 return true;
371
372 IsAnyFieldInitialized = true;
373 return false;
374}
375
376//===----------------------------------------------------------------------===//
377// Methods for FieldChainInfo.
378//===----------------------------------------------------------------------===//
379
Kristof Umann015b0592018-08-13 18:43:08 +0000380bool FieldChainInfo::contains(const FieldRegion *FR) const {
381 for (const FieldNode &Node : Chain) {
382 if (Node.isSameRegion(FR))
383 return true;
384 }
385 return false;
Kristof Umann30f08652018-06-18 11:50:17 +0000386}
387
Kristof Umanna37bba42018-08-13 18:22:22 +0000388/// Prints every element except the last to `Out`. Since ImmutableLists store
389/// elements in reverse order, and have no reverse iterators, we use a
390/// recursive function to print the fieldchain correctly. The last element in
Kristof Umannceb5f652018-09-14 09:07:40 +0000391/// the chain is to be printed by `FieldChainInfo::print`.
Kristof Umanna37bba42018-08-13 18:22:22 +0000392static void printTail(llvm::raw_ostream &Out,
Kristof Umann82eeca32018-09-23 09:16:27 +0000393 const FieldChainInfo::FieldChain L);
Kristof Umanna37bba42018-08-13 18:22:22 +0000394
Kristof Umannceb5f652018-09-14 09:07:40 +0000395// FIXME: This function constructs an incorrect string in the following case:
Kristof Umann30f08652018-06-18 11:50:17 +0000396//
397// struct Base { int x; };
398// struct D1 : Base {}; struct D2 : Base {};
399//
400// struct MostDerived : D1, D2 {
401// MostDerived() {}
402// }
403//
404// A call to MostDerived::MostDerived() will cause two notes that say
405// "uninitialized field 'this->x'", but we can't refer to 'x' directly,
406// we need an explicit namespace resolution whether the uninit field was
407// 'D1::x' or 'D2::x'.
Kristof Umann015b0592018-08-13 18:43:08 +0000408void FieldChainInfo::printNoteMsg(llvm::raw_ostream &Out) const {
Kristof Umann30f08652018-06-18 11:50:17 +0000409 if (Chain.isEmpty())
410 return;
411
Kristof Umann82eeca32018-09-23 09:16:27 +0000412 const FieldNode &LastField = getHead();
Kristof Umann015b0592018-08-13 18:43:08 +0000413
414 LastField.printNoteMsg(Out);
415 Out << '\'';
416
417 for (const FieldNode &Node : Chain)
418 Node.printPrefix(Out);
419
420 Out << "this->";
Kristof Umann82eeca32018-09-23 09:16:27 +0000421 printTail(Out, Chain.getTail());
Kristof Umann015b0592018-08-13 18:43:08 +0000422 LastField.printNode(Out);
423 Out << '\'';
Kristof Umann30f08652018-06-18 11:50:17 +0000424}
425
Kristof Umanna37bba42018-08-13 18:22:22 +0000426static void printTail(llvm::raw_ostream &Out,
Kristof Umann82eeca32018-09-23 09:16:27 +0000427 const FieldChainInfo::FieldChain L) {
428 if (L.isEmpty())
Kristof Umann30f08652018-06-18 11:50:17 +0000429 return;
430
Kristof Umann82eeca32018-09-23 09:16:27 +0000431 printTail(Out, L.getTail());
Kristof Umann015b0592018-08-13 18:43:08 +0000432
Kristof Umann82eeca32018-09-23 09:16:27 +0000433 L.getHead().printNode(Out);
434 L.getHead().printSeparator(Out);
Kristof Umann30f08652018-06-18 11:50:17 +0000435}
436
437//===----------------------------------------------------------------------===//
438// Utility functions.
439//===----------------------------------------------------------------------===//
440
Kristof Umanndbabdfa2018-10-21 23:30:01 +0000441static const TypedValueRegion *
442getConstructedRegion(const CXXConstructorDecl *CtorDecl,
443 CheckerContext &Context) {
Kristof Umann30f08652018-06-18 11:50:17 +0000444
Kristof Umanndbabdfa2018-10-21 23:30:01 +0000445 Loc ThisLoc = Context.getSValBuilder().getCXXThis(CtorDecl,
Kristof Umann30f08652018-06-18 11:50:17 +0000446 Context.getStackFrame());
Kristof Umann30f08652018-06-18 11:50:17 +0000447
Kristof Umanndbabdfa2018-10-21 23:30:01 +0000448 SVal ObjectV = Context.getState()->getSVal(ThisLoc);
Kristof Umann30f08652018-06-18 11:50:17 +0000449
Kristof Umanndbabdfa2018-10-21 23:30:01 +0000450 auto *R = ObjectV.getAsRegion()->getAs<TypedValueRegion>();
451 if (R && !R->getValueType()->getAsCXXRecordDecl())
452 return nullptr;
453
454 return R;
Kristof Umann30f08652018-06-18 11:50:17 +0000455}
456
Kristof Umann0735cfb2018-08-08 12:23:02 +0000457static bool willObjectBeAnalyzedLater(const CXXConstructorDecl *Ctor,
Kristof Umanna37bba42018-08-13 18:22:22 +0000458 CheckerContext &Context) {
Kristof Umann30f08652018-06-18 11:50:17 +0000459
Kristof Umanndbabdfa2018-10-21 23:30:01 +0000460 const TypedValueRegion *CurrRegion = getConstructedRegion(Ctor, Context);
461 if (!CurrRegion)
Kristof Umann0735cfb2018-08-08 12:23:02 +0000462 return false;
463
464 const LocationContext *LC = Context.getLocationContext();
465 while ((LC = LC->getParent())) {
466
467 // If \p Ctor was called by another constructor.
468 const auto *OtherCtor = dyn_cast<CXXConstructorDecl>(LC->getDecl());
469 if (!OtherCtor)
470 continue;
471
Kristof Umanndbabdfa2018-10-21 23:30:01 +0000472 const TypedValueRegion *OtherRegion =
473 getConstructedRegion(OtherCtor, Context);
474 if (!OtherRegion)
Kristof Umann0735cfb2018-08-08 12:23:02 +0000475 continue;
476
Kristof Umanndbabdfa2018-10-21 23:30:01 +0000477 // If the CurrRegion is a subregion of OtherRegion, it will be analyzed
478 // during the analysis of OtherRegion.
479 if (CurrRegion->isSubRegionOf(OtherRegion))
Kristof Umann30f08652018-06-18 11:50:17 +0000480 return true;
Kristof Umann30f08652018-06-18 11:50:17 +0000481 }
Kristof Umann0735cfb2018-08-08 12:23:02 +0000482
Kristof Umann30f08652018-06-18 11:50:17 +0000483 return false;
484}
485
Kristof Umannd6145d92018-09-14 10:10:09 +0000486static bool shouldIgnoreRecord(const RecordDecl *RD, StringRef Pattern) {
487 llvm::Regex R(Pattern);
488
489 for (const FieldDecl *FD : RD->fields()) {
490 if (R.match(FD->getType().getAsString()))
491 return true;
492 if (R.match(FD->getName()))
493 return true;
494 }
495
496 return false;
497}
498
Kristof Umannf0dd1012018-09-14 08:58:21 +0000499std::string clang::ento::getVariableName(const FieldDecl *Field) {
Kristof Umann8c119092018-07-13 12:54:47 +0000500 // If Field is a captured lambda variable, Field->getName() will return with
501 // an empty string. We can however acquire it's name from the lambda's
502 // captures.
503 const auto *CXXParent = dyn_cast<CXXRecordDecl>(Field->getParent());
504
505 if (CXXParent && CXXParent->isLambda()) {
506 assert(CXXParent->captures_begin());
507 auto It = CXXParent->captures_begin() + Field->getFieldIndex();
Kristof Umannf0dd1012018-09-14 08:58:21 +0000508
509 if (It->capturesVariable())
510 return llvm::Twine("/*captured variable*/" +
511 It->getCapturedVar()->getName())
512 .str();
513
514 if (It->capturesThis())
515 return "/*'this' capture*/";
516
517 llvm_unreachable("No other capture type is expected!");
Kristof Umann8c119092018-07-13 12:54:47 +0000518 }
519
520 return Field->getName();
521}
522
Kristof Umann30f08652018-06-18 11:50:17 +0000523void ento::registerUninitializedObjectChecker(CheckerManager &Mgr) {
524 auto Chk = Mgr.registerChecker<UninitializedObjectChecker>();
Kristof Umannceb5f652018-09-14 09:07:40 +0000525
Kristof Umann6cec6c42018-09-14 09:39:26 +0000526 AnalyzerOptions &AnOpts = Mgr.getAnalyzerOptions();
527 UninitObjCheckerOptions &ChOpts = Chk->Opts;
528
Kristof Umannd6145d92018-09-14 10:10:09 +0000529 ChOpts.IsPedantic =
Kristof Umann0a1f91c2018-11-05 03:50:37 +0000530 AnOpts.getCheckerBooleanOption("Pedantic", /*DefaultVal*/ false, Chk);
Kristof Umannd6145d92018-09-14 10:10:09 +0000531 ChOpts.ShouldConvertNotesToWarnings =
Kristof Umann0a1f91c2018-11-05 03:50:37 +0000532 AnOpts.getCheckerBooleanOption("NotesAsWarnings", /*DefaultVal*/ false, Chk);
533 ChOpts.CheckPointeeInitialization = AnOpts.getCheckerBooleanOption(
Kristof Umanna3f7b582018-08-07 12:55:26 +0000534 "CheckPointeeInitialization", /*DefaultVal*/ false, Chk);
Kristof Umannd6145d92018-09-14 10:10:09 +0000535 ChOpts.IgnoredRecordsWithFieldPattern =
Kristof Umann0a1f91c2018-11-05 03:50:37 +0000536 AnOpts.getCheckerStringOption("IgnoreRecordsWithField",
Kristof Umannd6145d92018-09-14 10:10:09 +0000537 /*DefaultVal*/ "", Chk);
Kristof Umann30f08652018-06-18 11:50:17 +0000538}