blob: b7c16dc3d59b63cc5f66ad30a8c99a609fefc526 [file] [log] [blame]
Kristof Umann30f08652018-06-18 11:50:17 +00001//===----- UninitializedObjectChecker.cpp ------------------------*- C++ -*-==//
2//
Chandler Carruth2946cd72019-01-19 08:50:56 +00003// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4// See https://llvm.org/LICENSE.txt for license information.
5// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
Kristof Umann30f08652018-06-18 11:50:17 +00006//
7//===----------------------------------------------------------------------===//
8//
9// This file defines a checker that reports uninitialized fields in objects
10// created after a constructor call.
11//
Kristof Umann6cec6c42018-09-14 09:39:26 +000012// To read about command line options and how the checker works, refer to the
13// top of the file and inline comments in UninitializedObject.h.
Kristof Umann56963ae2018-08-13 18:17:05 +000014//
15// Some of the logic is implemented in UninitializedPointee.cpp, to reduce the
16// complexity of this file.
17//
Kristof Umann30f08652018-06-18 11:50:17 +000018//===----------------------------------------------------------------------===//
19
Kristof Umann76a21502018-12-15 16:23:51 +000020#include "clang/StaticAnalyzer/Checkers/BuiltinCheckerRegistration.h"
Kristof Umanna37bba42018-08-13 18:22:22 +000021#include "UninitializedObject.h"
Kristof Umann30f08652018-06-18 11:50:17 +000022#include "clang/StaticAnalyzer/Core/BugReporter/BugType.h"
23#include "clang/StaticAnalyzer/Core/Checker.h"
24#include "clang/StaticAnalyzer/Core/PathSensitive/CheckerContext.h"
Kristof Umannef9af052018-08-08 13:18:53 +000025#include "clang/StaticAnalyzer/Core/PathSensitive/DynamicTypeMap.h"
Kristof Umann30f08652018-06-18 11:50:17 +000026
27using namespace clang;
28using namespace clang::ento;
29
Kristof Umann4ff77692018-11-18 11:34:10 +000030/// We'll mark fields (and pointee of fields) that are confirmed to be
31/// uninitialized as already analyzed.
32REGISTER_SET_WITH_PROGRAMSTATE(AnalyzedRegions, const MemRegion *)
33
Kristof Umann30f08652018-06-18 11:50:17 +000034namespace {
35
Kristof Umann4ff77692018-11-18 11:34:10 +000036class UninitializedObjectChecker
37 : public Checker<check::EndFunction, check::DeadSymbols> {
Kristof Umann30f08652018-06-18 11:50:17 +000038 std::unique_ptr<BuiltinBug> BT_uninitField;
39
40public:
Kristof Umann6cec6c42018-09-14 09:39:26 +000041 // The fields of this struct will be initialized when registering the checker.
42 UninitObjCheckerOptions Opts;
Kristof Umann30f08652018-06-18 11:50:17 +000043
44 UninitializedObjectChecker()
45 : BT_uninitField(new BuiltinBug(this, "Uninitialized fields")) {}
Kristof Umann4ff77692018-11-18 11:34:10 +000046
Reka Kovacsed8c05c2018-07-16 20:47:45 +000047 void checkEndFunction(const ReturnStmt *RS, CheckerContext &C) const;
Kristof Umann4ff77692018-11-18 11:34:10 +000048 void checkDeadSymbols(SymbolReaper &SR, CheckerContext &C) const;
Kristof Umann30f08652018-06-18 11:50:17 +000049};
50
Kristof Umann015b0592018-08-13 18:43:08 +000051/// A basic field type, that is not a pointer or a reference, it's dynamic and
52/// static type is the same.
Richard Smith651d6832018-08-13 22:07:11 +000053class RegularField final : public FieldNode {
Kristof Umann015b0592018-08-13 18:43:08 +000054public:
55 RegularField(const FieldRegion *FR) : FieldNode(FR) {}
56
57 virtual void printNoteMsg(llvm::raw_ostream &Out) const override {
58 Out << "uninitialized field ";
59 }
60
61 virtual void printPrefix(llvm::raw_ostream &Out) const override {}
62
Richard Smith651d6832018-08-13 22:07:11 +000063 virtual void printNode(llvm::raw_ostream &Out) const override {
Kristof Umann015b0592018-08-13 18:43:08 +000064 Out << getVariableName(getDecl());
65 }
66
67 virtual void printSeparator(llvm::raw_ostream &Out) const override {
68 Out << '.';
69 }
70};
71
Kristof Umannb59b45e2018-08-21 12:16:59 +000072/// Represents that the FieldNode that comes after this is declared in a base
Kristof Umannceb5f652018-09-14 09:07:40 +000073/// of the previous FieldNode. As such, this descendant doesn't wrap a
74/// FieldRegion, and is purely a tool to describe a relation between two other
75/// FieldRegion wrapping descendants.
Kristof Umannb59b45e2018-08-21 12:16:59 +000076class BaseClass final : public FieldNode {
77 const QualType BaseClassT;
78
79public:
80 BaseClass(const QualType &T) : FieldNode(nullptr), BaseClassT(T) {
81 assert(!T.isNull());
82 assert(T->getAsCXXRecordDecl());
83 }
84
85 virtual void printNoteMsg(llvm::raw_ostream &Out) const override {
86 llvm_unreachable("This node can never be the final node in the "
87 "fieldchain!");
88 }
89
90 virtual void printPrefix(llvm::raw_ostream &Out) const override {}
91
92 virtual void printNode(llvm::raw_ostream &Out) const override {
93 Out << BaseClassT->getAsCXXRecordDecl()->getName() << "::";
94 }
95
96 virtual void printSeparator(llvm::raw_ostream &Out) const override {}
97
Kristof Umann06209cb2018-08-21 15:09:22 +000098 virtual bool isBase() const override { return true; }
Kristof Umannb59b45e2018-08-21 12:16:59 +000099};
100
Kristof Umanncc852442018-07-12 13:13:46 +0000101} // end of anonymous namespace
102
Kristof Umann30f08652018-06-18 11:50:17 +0000103// Utility function declarations.
104
Kristof Umanndbabdfa2018-10-21 23:30:01 +0000105/// Returns the region that was constructed by CtorDecl, or nullptr if that
106/// isn't possible.
107static const TypedValueRegion *
108getConstructedRegion(const CXXConstructorDecl *CtorDecl,
109 CheckerContext &Context);
Kristof Umann30f08652018-06-18 11:50:17 +0000110
Kristof Umann0735cfb2018-08-08 12:23:02 +0000111/// Checks whether the object constructed by \p Ctor will be analyzed later
112/// (e.g. if the object is a field of another object, in which case we'd check
113/// it multiple times).
114static bool willObjectBeAnalyzedLater(const CXXConstructorDecl *Ctor,
Kristof Umanna37bba42018-08-13 18:22:22 +0000115 CheckerContext &Context);
Kristof Umann30f08652018-06-18 11:50:17 +0000116
Kristof Umannd6145d92018-09-14 10:10:09 +0000117/// Checks whether RD contains a field with a name or type name that matches
118/// \p Pattern.
119static bool shouldIgnoreRecord(const RecordDecl *RD, StringRef Pattern);
120
Kristof Umann30f08652018-06-18 11:50:17 +0000121//===----------------------------------------------------------------------===//
122// Methods for UninitializedObjectChecker.
123//===----------------------------------------------------------------------===//
124
125void UninitializedObjectChecker::checkEndFunction(
Reka Kovacsed8c05c2018-07-16 20:47:45 +0000126 const ReturnStmt *RS, CheckerContext &Context) const {
Kristof Umann30f08652018-06-18 11:50:17 +0000127
128 const auto *CtorDecl = dyn_cast_or_null<CXXConstructorDecl>(
129 Context.getLocationContext()->getDecl());
130 if (!CtorDecl)
131 return;
132
133 if (!CtorDecl->isUserProvided())
134 return;
135
136 if (CtorDecl->getParent()->isUnion())
137 return;
138
139 // This avoids essentially the same error being reported multiple times.
Kristof Umann0735cfb2018-08-08 12:23:02 +0000140 if (willObjectBeAnalyzedLater(CtorDecl, Context))
Kristof Umann30f08652018-06-18 11:50:17 +0000141 return;
142
Kristof Umanndbabdfa2018-10-21 23:30:01 +0000143 const TypedValueRegion *R = getConstructedRegion(CtorDecl, Context);
144 if (!R)
Kristof Umann30f08652018-06-18 11:50:17 +0000145 return;
146
Kristof Umanndbabdfa2018-10-21 23:30:01 +0000147 FindUninitializedFields F(Context.getState(), R, Opts);
Kristof Umann30f08652018-06-18 11:50:17 +0000148
Kristof Umann4ff77692018-11-18 11:34:10 +0000149 std::pair<ProgramStateRef, const UninitFieldMap &> UninitInfo =
150 F.getResults();
Kristof Umann30f08652018-06-18 11:50:17 +0000151
Kristof Umann4ff77692018-11-18 11:34:10 +0000152 ProgramStateRef UpdatedState = UninitInfo.first;
153 const UninitFieldMap &UninitFields = UninitInfo.second;
154
155 if (UninitFields.empty()) {
156 Context.addTransition(UpdatedState);
Kristof Umann30f08652018-06-18 11:50:17 +0000157 return;
Kristof Umann4ff77692018-11-18 11:34:10 +0000158 }
Kristof Umann30f08652018-06-18 11:50:17 +0000159
160 // There are uninitialized fields in the record.
161
Kristof Umann4ff77692018-11-18 11:34:10 +0000162 ExplodedNode *Node = Context.generateNonFatalErrorNode(UpdatedState);
Kristof Umann30f08652018-06-18 11:50:17 +0000163 if (!Node)
164 return;
165
166 PathDiagnosticLocation LocUsedForUniqueing;
167 const Stmt *CallSite = Context.getStackFrame()->getCallSite();
168 if (CallSite)
169 LocUsedForUniqueing = PathDiagnosticLocation::createBegin(
170 CallSite, Context.getSourceManager(), Node->getLocationContext());
171
Kristof Umann9bd44392018-06-29 11:25:24 +0000172 // For Plist consumers that don't support notes just yet, we'll convert notes
173 // to warnings.
Kristof Umann6cec6c42018-09-14 09:39:26 +0000174 if (Opts.ShouldConvertNotesToWarnings) {
Kristof Umann015b0592018-08-13 18:43:08 +0000175 for (const auto &Pair : UninitFields) {
Kristof Umann9bd44392018-06-29 11:25:24 +0000176
177 auto Report = llvm::make_unique<BugReport>(
Kristof Umann015b0592018-08-13 18:43:08 +0000178 *BT_uninitField, Pair.second, Node, LocUsedForUniqueing,
Kristof Umann9bd44392018-06-29 11:25:24 +0000179 Node->getLocationContext()->getDecl());
180 Context.emitReport(std::move(Report));
181 }
182 return;
183 }
184
Kristof Umann30f08652018-06-18 11:50:17 +0000185 SmallString<100> WarningBuf;
186 llvm::raw_svector_ostream WarningOS(WarningBuf);
187 WarningOS << UninitFields.size() << " uninitialized field"
188 << (UninitFields.size() == 1 ? "" : "s")
189 << " at the end of the constructor call";
190
191 auto Report = llvm::make_unique<BugReport>(
192 *BT_uninitField, WarningOS.str(), Node, LocUsedForUniqueing,
193 Node->getLocationContext()->getDecl());
194
Kristof Umann015b0592018-08-13 18:43:08 +0000195 for (const auto &Pair : UninitFields) {
196 Report->addNote(Pair.second,
197 PathDiagnosticLocation::create(Pair.first->getDecl(),
Kristof Umann30f08652018-06-18 11:50:17 +0000198 Context.getSourceManager()));
199 }
Kristof Umann30f08652018-06-18 11:50:17 +0000200 Context.emitReport(std::move(Report));
201}
202
Kristof Umann4ff77692018-11-18 11:34:10 +0000203void UninitializedObjectChecker::checkDeadSymbols(SymbolReaper &SR,
204 CheckerContext &C) const {
205 ProgramStateRef State = C.getState();
206 for (const MemRegion *R : State->get<AnalyzedRegions>()) {
207 if (!SR.isLiveRegion(R))
208 State = State->remove<AnalyzedRegions>(R);
209 }
210}
211
Kristof Umann30f08652018-06-18 11:50:17 +0000212//===----------------------------------------------------------------------===//
213// Methods for FindUninitializedFields.
214//===----------------------------------------------------------------------===//
215
216FindUninitializedFields::FindUninitializedFields(
Kristof Umann23ca9662018-08-13 18:48:34 +0000217 ProgramStateRef State, const TypedValueRegion *const R,
Kristof Umann6cec6c42018-09-14 09:39:26 +0000218 const UninitObjCheckerOptions &Opts)
219 : State(State), ObjectR(R), Opts(Opts) {
Kristof Umann30f08652018-06-18 11:50:17 +0000220
Kristof Umann015b0592018-08-13 18:43:08 +0000221 isNonUnionUninit(ObjectR, FieldChainInfo(ChainFactory));
Kristof Umann6cec6c42018-09-14 09:39:26 +0000222
223 // In non-pedantic mode, if ObjectR doesn't contain a single initialized
224 // field, we'll assume that Object was intentionally left uninitialized.
225 if (!Opts.IsPedantic && !isAnyFieldInitialized())
226 UninitFields.clear();
Kristof Umann30f08652018-06-18 11:50:17 +0000227}
228
Kristof Umann4ff77692018-11-18 11:34:10 +0000229bool FindUninitializedFields::addFieldToUninits(FieldChainInfo Chain,
230 const MemRegion *PointeeR) {
231 const FieldRegion *FR = Chain.getUninitRegion();
232
233 assert((PointeeR || !isDereferencableType(FR->getDecl()->getType())) &&
234 "One must also pass the pointee region as a parameter for "
Raphael Isemannb23ccec2018-12-10 12:37:46 +0000235 "dereferenceable fields!");
Kristof Umann4ff77692018-11-18 11:34:10 +0000236
237 if (State->contains<AnalyzedRegions>(FR))
238 return false;
239
240 if (PointeeR) {
241 if (State->contains<AnalyzedRegions>(PointeeR)) {
242 return false;
243 }
244 State = State->add<AnalyzedRegions>(PointeeR);
245 }
246
247 State = State->add<AnalyzedRegions>(FR);
248
Kristof Umann30f08652018-06-18 11:50:17 +0000249 if (State->getStateManager().getContext().getSourceManager().isInSystemHeader(
Kristof Umann4ff77692018-11-18 11:34:10 +0000250 FR->getDecl()->getLocation()))
Kristof Umann30f08652018-06-18 11:50:17 +0000251 return false;
252
Kristof Umann015b0592018-08-13 18:43:08 +0000253 UninitFieldMap::mapped_type NoteMsgBuf;
254 llvm::raw_svector_ostream OS(NoteMsgBuf);
255 Chain.printNoteMsg(OS);
Kristof Umann4ff77692018-11-18 11:34:10 +0000256 return UninitFields.insert({FR, std::move(NoteMsgBuf)}).second;
Kristof Umann30f08652018-06-18 11:50:17 +0000257}
258
259bool FindUninitializedFields::isNonUnionUninit(const TypedValueRegion *R,
260 FieldChainInfo LocalChain) {
261 assert(R->getValueType()->isRecordType() &&
262 !R->getValueType()->isUnionType() &&
263 "This method only checks non-union record objects!");
264
Kristof Umannf0dd1012018-09-14 08:58:21 +0000265 const RecordDecl *RD = R->getValueType()->getAsRecordDecl()->getDefinition();
266
267 if (!RD) {
268 IsAnyFieldInitialized = true;
269 return true;
270 }
Kristof Umann30f08652018-06-18 11:50:17 +0000271
Kristof Umannd6145d92018-09-14 10:10:09 +0000272 if (!Opts.IgnoredRecordsWithFieldPattern.empty() &&
273 shouldIgnoreRecord(RD, Opts.IgnoredRecordsWithFieldPattern)) {
274 IsAnyFieldInitialized = true;
275 return false;
276 }
277
Kristof Umann30f08652018-06-18 11:50:17 +0000278 bool ContainsUninitField = false;
279
280 // Are all of this non-union's fields initialized?
281 for (const FieldDecl *I : RD->fields()) {
282
283 const auto FieldVal =
284 State->getLValue(I, loc::MemRegionVal(R)).castAs<loc::MemRegionVal>();
285 const auto *FR = FieldVal.getRegionAs<FieldRegion>();
286 QualType T = I->getType();
287
288 // If LocalChain already contains FR, then we encountered a cyclic
289 // reference. In this case, region FR is already under checking at an
290 // earlier node in the directed tree.
291 if (LocalChain.contains(FR))
292 return false;
293
294 if (T->isStructureOrClassType()) {
Kristof Umann015b0592018-08-13 18:43:08 +0000295 if (isNonUnionUninit(FR, LocalChain.add(RegularField(FR))))
Kristof Umann30f08652018-06-18 11:50:17 +0000296 ContainsUninitField = true;
297 continue;
298 }
299
300 if (T->isUnionType()) {
301 if (isUnionUninit(FR)) {
Kristof Umann015b0592018-08-13 18:43:08 +0000302 if (addFieldToUninits(LocalChain.add(RegularField(FR))))
Kristof Umann30f08652018-06-18 11:50:17 +0000303 ContainsUninitField = true;
304 } else
305 IsAnyFieldInitialized = true;
306 continue;
307 }
308
309 if (T->isArrayType()) {
310 IsAnyFieldInitialized = true;
311 continue;
312 }
313
Kristof Umannf0513792018-09-14 10:18:26 +0000314 SVal V = State->getSVal(FieldVal);
315
316 if (isDereferencableType(T) || V.getAs<nonloc::LocAsInteger>()) {
Kristof Umannceb5f652018-09-14 09:07:40 +0000317 if (isDereferencableUninit(FR, LocalChain))
Kristof Umann30f08652018-06-18 11:50:17 +0000318 ContainsUninitField = true;
319 continue;
320 }
321
Kristof Umann20e85ba2018-06-19 08:35:02 +0000322 if (isPrimitiveType(T)) {
Kristof Umann20e85ba2018-06-19 08:35:02 +0000323 if (isPrimitiveUninit(V)) {
Kristof Umann015b0592018-08-13 18:43:08 +0000324 if (addFieldToUninits(LocalChain.add(RegularField(FR))))
Kristof Umann20e85ba2018-06-19 08:35:02 +0000325 ContainsUninitField = true;
326 }
327 continue;
Kristof Umann30f08652018-06-18 11:50:17 +0000328 }
Kristof Umann20e85ba2018-06-19 08:35:02 +0000329
330 llvm_unreachable("All cases are handled!");
Kristof Umann30f08652018-06-18 11:50:17 +0000331 }
332
Kristof Umannceb5f652018-09-14 09:07:40 +0000333 // Checking bases. The checker will regard inherited data members as direct
334 // fields.
Kristof Umann30f08652018-06-18 11:50:17 +0000335 const auto *CXXRD = dyn_cast<CXXRecordDecl>(RD);
336 if (!CXXRD)
337 return ContainsUninitField;
338
339 for (const CXXBaseSpecifier &BaseSpec : CXXRD->bases()) {
340 const auto *BaseRegion = State->getLValue(BaseSpec, R)
341 .castAs<loc::MemRegionVal>()
342 .getRegionAs<TypedValueRegion>();
343
Kristof Umannb59b45e2018-08-21 12:16:59 +0000344 // If the head of the list is also a BaseClass, we'll overwrite it to avoid
345 // note messages like 'this->A::B::x'.
346 if (!LocalChain.isEmpty() && LocalChain.getHead().isBase()) {
347 if (isNonUnionUninit(BaseRegion, LocalChain.replaceHead(
348 BaseClass(BaseSpec.getType()))))
349 ContainsUninitField = true;
350 } else {
351 if (isNonUnionUninit(BaseRegion,
352 LocalChain.add(BaseClass(BaseSpec.getType()))))
353 ContainsUninitField = true;
354 }
Kristof Umann30f08652018-06-18 11:50:17 +0000355 }
356
357 return ContainsUninitField;
358}
359
360bool FindUninitializedFields::isUnionUninit(const TypedValueRegion *R) {
361 assert(R->getValueType()->isUnionType() &&
362 "This method only checks union objects!");
363 // TODO: Implement support for union fields.
364 return false;
365}
366
Kristof Umann30f08652018-06-18 11:50:17 +0000367bool FindUninitializedFields::isPrimitiveUninit(const SVal &V) {
368 if (V.isUndef())
369 return true;
370
371 IsAnyFieldInitialized = true;
372 return false;
373}
374
375//===----------------------------------------------------------------------===//
376// Methods for FieldChainInfo.
377//===----------------------------------------------------------------------===//
378
Kristof Umann015b0592018-08-13 18:43:08 +0000379bool FieldChainInfo::contains(const FieldRegion *FR) const {
380 for (const FieldNode &Node : Chain) {
381 if (Node.isSameRegion(FR))
382 return true;
383 }
384 return false;
Kristof Umann30f08652018-06-18 11:50:17 +0000385}
386
Kristof Umanna37bba42018-08-13 18:22:22 +0000387/// Prints every element except the last to `Out`. Since ImmutableLists store
388/// elements in reverse order, and have no reverse iterators, we use a
389/// recursive function to print the fieldchain correctly. The last element in
Kristof Umannceb5f652018-09-14 09:07:40 +0000390/// the chain is to be printed by `FieldChainInfo::print`.
Kristof Umanna37bba42018-08-13 18:22:22 +0000391static void printTail(llvm::raw_ostream &Out,
Kristof Umann82eeca32018-09-23 09:16:27 +0000392 const FieldChainInfo::FieldChain L);
Kristof Umanna37bba42018-08-13 18:22:22 +0000393
Kristof Umannceb5f652018-09-14 09:07:40 +0000394// FIXME: This function constructs an incorrect string in the following case:
Kristof Umann30f08652018-06-18 11:50:17 +0000395//
396// struct Base { int x; };
397// struct D1 : Base {}; struct D2 : Base {};
398//
399// struct MostDerived : D1, D2 {
400// MostDerived() {}
401// }
402//
403// A call to MostDerived::MostDerived() will cause two notes that say
404// "uninitialized field 'this->x'", but we can't refer to 'x' directly,
405// we need an explicit namespace resolution whether the uninit field was
406// 'D1::x' or 'D2::x'.
Kristof Umann015b0592018-08-13 18:43:08 +0000407void FieldChainInfo::printNoteMsg(llvm::raw_ostream &Out) const {
Kristof Umann30f08652018-06-18 11:50:17 +0000408 if (Chain.isEmpty())
409 return;
410
Kristof Umann82eeca32018-09-23 09:16:27 +0000411 const FieldNode &LastField = getHead();
Kristof Umann015b0592018-08-13 18:43:08 +0000412
413 LastField.printNoteMsg(Out);
414 Out << '\'';
415
416 for (const FieldNode &Node : Chain)
417 Node.printPrefix(Out);
418
419 Out << "this->";
Kristof Umann82eeca32018-09-23 09:16:27 +0000420 printTail(Out, Chain.getTail());
Kristof Umann015b0592018-08-13 18:43:08 +0000421 LastField.printNode(Out);
422 Out << '\'';
Kristof Umann30f08652018-06-18 11:50:17 +0000423}
424
Kristof Umanna37bba42018-08-13 18:22:22 +0000425static void printTail(llvm::raw_ostream &Out,
Kristof Umann82eeca32018-09-23 09:16:27 +0000426 const FieldChainInfo::FieldChain L) {
427 if (L.isEmpty())
Kristof Umann30f08652018-06-18 11:50:17 +0000428 return;
429
Kristof Umann82eeca32018-09-23 09:16:27 +0000430 printTail(Out, L.getTail());
Kristof Umann015b0592018-08-13 18:43:08 +0000431
Kristof Umann82eeca32018-09-23 09:16:27 +0000432 L.getHead().printNode(Out);
433 L.getHead().printSeparator(Out);
Kristof Umann30f08652018-06-18 11:50:17 +0000434}
435
436//===----------------------------------------------------------------------===//
437// Utility functions.
438//===----------------------------------------------------------------------===//
439
Kristof Umanndbabdfa2018-10-21 23:30:01 +0000440static const TypedValueRegion *
441getConstructedRegion(const CXXConstructorDecl *CtorDecl,
442 CheckerContext &Context) {
Kristof Umann30f08652018-06-18 11:50:17 +0000443
Kristof Umanndbabdfa2018-10-21 23:30:01 +0000444 Loc ThisLoc = Context.getSValBuilder().getCXXThis(CtorDecl,
Kristof Umann30f08652018-06-18 11:50:17 +0000445 Context.getStackFrame());
Kristof Umann30f08652018-06-18 11:50:17 +0000446
Kristof Umanndbabdfa2018-10-21 23:30:01 +0000447 SVal ObjectV = Context.getState()->getSVal(ThisLoc);
Kristof Umann30f08652018-06-18 11:50:17 +0000448
Kristof Umanndbabdfa2018-10-21 23:30:01 +0000449 auto *R = ObjectV.getAsRegion()->getAs<TypedValueRegion>();
450 if (R && !R->getValueType()->getAsCXXRecordDecl())
451 return nullptr;
452
453 return R;
Kristof Umann30f08652018-06-18 11:50:17 +0000454}
455
Kristof Umann0735cfb2018-08-08 12:23:02 +0000456static bool willObjectBeAnalyzedLater(const CXXConstructorDecl *Ctor,
Kristof Umanna37bba42018-08-13 18:22:22 +0000457 CheckerContext &Context) {
Kristof Umann30f08652018-06-18 11:50:17 +0000458
Kristof Umanndbabdfa2018-10-21 23:30:01 +0000459 const TypedValueRegion *CurrRegion = getConstructedRegion(Ctor, Context);
460 if (!CurrRegion)
Kristof Umann0735cfb2018-08-08 12:23:02 +0000461 return false;
462
463 const LocationContext *LC = Context.getLocationContext();
464 while ((LC = LC->getParent())) {
465
466 // If \p Ctor was called by another constructor.
467 const auto *OtherCtor = dyn_cast<CXXConstructorDecl>(LC->getDecl());
468 if (!OtherCtor)
469 continue;
470
Kristof Umanndbabdfa2018-10-21 23:30:01 +0000471 const TypedValueRegion *OtherRegion =
472 getConstructedRegion(OtherCtor, Context);
473 if (!OtherRegion)
Kristof Umann0735cfb2018-08-08 12:23:02 +0000474 continue;
475
Kristof Umanndbabdfa2018-10-21 23:30:01 +0000476 // If the CurrRegion is a subregion of OtherRegion, it will be analyzed
477 // during the analysis of OtherRegion.
478 if (CurrRegion->isSubRegionOf(OtherRegion))
Kristof Umann30f08652018-06-18 11:50:17 +0000479 return true;
Kristof Umann30f08652018-06-18 11:50:17 +0000480 }
Kristof Umann0735cfb2018-08-08 12:23:02 +0000481
Kristof Umann30f08652018-06-18 11:50:17 +0000482 return false;
483}
484
Kristof Umannd6145d92018-09-14 10:10:09 +0000485static bool shouldIgnoreRecord(const RecordDecl *RD, StringRef Pattern) {
486 llvm::Regex R(Pattern);
487
488 for (const FieldDecl *FD : RD->fields()) {
489 if (R.match(FD->getType().getAsString()))
490 return true;
491 if (R.match(FD->getName()))
492 return true;
493 }
494
495 return false;
496}
497
Kristof Umannf0dd1012018-09-14 08:58:21 +0000498std::string clang::ento::getVariableName(const FieldDecl *Field) {
Kristof Umann8c119092018-07-13 12:54:47 +0000499 // If Field is a captured lambda variable, Field->getName() will return with
500 // an empty string. We can however acquire it's name from the lambda's
501 // captures.
502 const auto *CXXParent = dyn_cast<CXXRecordDecl>(Field->getParent());
503
504 if (CXXParent && CXXParent->isLambda()) {
505 assert(CXXParent->captures_begin());
506 auto It = CXXParent->captures_begin() + Field->getFieldIndex();
Kristof Umannf0dd1012018-09-14 08:58:21 +0000507
508 if (It->capturesVariable())
509 return llvm::Twine("/*captured variable*/" +
510 It->getCapturedVar()->getName())
511 .str();
512
513 if (It->capturesThis())
514 return "/*'this' capture*/";
515
516 llvm_unreachable("No other capture type is expected!");
Kristof Umann8c119092018-07-13 12:54:47 +0000517 }
518
519 return Field->getName();
520}
521
Kristof Umann30f08652018-06-18 11:50:17 +0000522void ento::registerUninitializedObjectChecker(CheckerManager &Mgr) {
523 auto Chk = Mgr.registerChecker<UninitializedObjectChecker>();
Kristof Umannceb5f652018-09-14 09:07:40 +0000524
Kristof Umann6cec6c42018-09-14 09:39:26 +0000525 AnalyzerOptions &AnOpts = Mgr.getAnalyzerOptions();
526 UninitObjCheckerOptions &ChOpts = Chk->Opts;
527
Kristof Umannd6145d92018-09-14 10:10:09 +0000528 ChOpts.IsPedantic =
Kristof Umann0a1f91c2018-11-05 03:50:37 +0000529 AnOpts.getCheckerBooleanOption("Pedantic", /*DefaultVal*/ false, Chk);
Kristof Umannd6145d92018-09-14 10:10:09 +0000530 ChOpts.ShouldConvertNotesToWarnings =
Kristof Umann0a1f91c2018-11-05 03:50:37 +0000531 AnOpts.getCheckerBooleanOption("NotesAsWarnings", /*DefaultVal*/ false, Chk);
532 ChOpts.CheckPointeeInitialization = AnOpts.getCheckerBooleanOption(
Kristof Umanna3f7b582018-08-07 12:55:26 +0000533 "CheckPointeeInitialization", /*DefaultVal*/ false, Chk);
Kristof Umannd6145d92018-09-14 10:10:09 +0000534 ChOpts.IgnoredRecordsWithFieldPattern =
Kristof Umann0a1f91c2018-11-05 03:50:37 +0000535 AnOpts.getCheckerStringOption("IgnoreRecordsWithField",
Kristof Umannd6145d92018-09-14 10:10:09 +0000536 /*DefaultVal*/ "", Chk);
Kristof Umann30f08652018-06-18 11:50:17 +0000537}
Kristof Umann058a7a42019-01-26 14:23:08 +0000538
539bool ento::shouldRegisterUninitializedObjectChecker(const LangOptions &LO) {
540 return true;
541}