1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
|
//===- Consumed.cpp --------------------------------------------*- C++ --*-===//
//
// The LLVM Compiler Infrastructure
//
// This file is distributed under the University of Illinois Open Source
// License. See LICENSE.TXT for details.
//
//===----------------------------------------------------------------------===//
//
// A intra-procedural analysis for checking consumed properties. This is based,
// in part, on research on linear types.
//
//===----------------------------------------------------------------------===//
#include "clang/AST/ASTContext.h"
#include "clang/AST/Attr.h"
#include "clang/AST/DeclCXX.h"
#include "clang/AST/ExprCXX.h"
#include "clang/AST/RecursiveASTVisitor.h"
#include "clang/AST/StmtVisitor.h"
#include "clang/AST/StmtCXX.h"
#include "clang/AST/Type.h"
#include "clang/Analysis/Analyses/PostOrderCFGView.h"
#include "clang/Analysis/AnalysisContext.h"
#include "clang/Analysis/CFG.h"
#include "clang/Analysis/Analyses/Consumed.h"
#include "clang/Basic/OperatorKinds.h"
#include "clang/Basic/SourceLocation.h"
#include "llvm/ADT/DenseMap.h"
#include "llvm/ADT/SmallVector.h"
#include "llvm/Support/raw_ostream.h"
// TODO: Mark variables as Unknown going into while- or for-loops only if they
// are referenced inside that block. (Deferred)
// TODO: Add a method(s) to identify which method calls perform what state
// transitions. (Deferred)
// TODO: Take notes on state transitions to provide better warning messages.
// (Deferred)
// TODO: Test nested conditionals: A) Checking the same value multiple times,
// and 2) Checking different values. (Deferred)
// TODO: Test IsFalseVisitor with values in the unknown state. (Deferred)
// TODO: Look into combining IsFalseVisitor and TestedVarsVisitor. (Deferred)
using namespace clang;
using namespace consumed;
// Key method definition
ConsumedWarningsHandlerBase::~ConsumedWarningsHandlerBase() {}
static bool isTestingFunction(const FunctionDecl *FunDecl) {
return FunDecl->hasAttr<TestsUnconsumedAttr>();
}
static StringRef stateToString(ConsumedState State) {
switch (State) {
case consumed::CS_None:
return "none";
case consumed::CS_Unknown:
return "unknown";
case consumed::CS_Unconsumed:
return "unconsumed";
case consumed::CS_Consumed:
return "consumed";
}
llvm_unreachable("invalid enum");
}
namespace {
class ConsumedStmtVisitor : public ConstStmtVisitor<ConsumedStmtVisitor> {
union PropagationUnion {
ConsumedState State;
const VarDecl *Var;
};
class PropagationInfo {
PropagationUnion StateOrVar;
public:
bool IsVar;
PropagationInfo() : IsVar(false) {
StateOrVar.State = consumed::CS_None;
}
PropagationInfo(ConsumedState State) : IsVar(false) {
StateOrVar.State = State;
}
PropagationInfo(const VarDecl *Var) : IsVar(true) {
StateOrVar.Var = Var;
}
ConsumedState getState() const { return StateOrVar.State; }
const VarDecl * getVar() const { return IsVar ? StateOrVar.Var : NULL; }
};
typedef llvm::DenseMap<const Stmt *, PropagationInfo> MapType;
typedef std::pair<const Stmt *, PropagationInfo> PairType;
typedef MapType::iterator InfoEntry;
AnalysisDeclContext &AC;
ConsumedAnalyzer &Analyzer;
ConsumedStateMap *StateMap;
MapType PropagationMap;
void checkCallability(const PropagationInfo &PState,
const FunctionDecl *FunDecl,
const CallExpr *Call);
void forwardInfo(const Stmt *From, const Stmt *To);
bool isLikeMoveAssignment(const CXXMethodDecl *MethodDecl);
public:
void Visit(const Stmt *StmtNode);
void VisitBinaryOperator(const BinaryOperator *BinOp);
void VisitCallExpr(const CallExpr *Call);
void VisitCastExpr(const CastExpr *Cast);
void VisitCXXConstructExpr(const CXXConstructExpr *Call);
void VisitCXXMemberCallExpr(const CXXMemberCallExpr *Call);
void VisitCXXOperatorCallExpr(const CXXOperatorCallExpr *Call);
void VisitDeclRefExpr(const DeclRefExpr *DeclRef);
void VisitDeclStmt(const DeclStmt *DelcS);
void VisitMaterializeTemporaryExpr(const MaterializeTemporaryExpr *Temp);
void VisitMemberExpr(const MemberExpr *MExpr);
void VisitUnaryOperator(const UnaryOperator *UOp);
void VisitVarDecl(const VarDecl *Var);
ConsumedStmtVisitor(AnalysisDeclContext &AC, ConsumedAnalyzer &Analyzer,
ConsumedStateMap *StateMap)
: AC(AC), Analyzer(Analyzer), StateMap(StateMap) {}
void reset() {
PropagationMap.clear();
}
};
// TODO: When we support CallableWhenConsumed this will have to check for
// the different attributes and change the behavior bellow. (Deferred)
void ConsumedStmtVisitor::checkCallability(const PropagationInfo &PState,
const FunctionDecl *FunDecl,
const CallExpr *Call) {
if (!FunDecl->hasAttr<CallableWhenUnconsumedAttr>()) return;
if (PState.IsVar) {
const VarDecl *Var = PState.getVar();
switch (StateMap->getState(Var)) {
case CS_Consumed:
Analyzer.WarningsHandler.warnUseWhileConsumed(
FunDecl->getNameAsString(), Var->getNameAsString(),
Call->getExprLoc());
break;
case CS_Unknown:
Analyzer.WarningsHandler.warnUseInUnknownState(
FunDecl->getNameAsString(), Var->getNameAsString(),
Call->getExprLoc());
break;
case CS_None:
case CS_Unconsumed:
break;
}
} else {
switch (PState.getState()) {
case CS_Consumed:
Analyzer.WarningsHandler.warnUseOfTempWhileConsumed(
FunDecl->getNameAsString(), Call->getExprLoc());
break;
case CS_Unknown:
Analyzer.WarningsHandler.warnUseOfTempInUnknownState(
FunDecl->getNameAsString(), Call->getExprLoc());
break;
case CS_None:
case CS_Unconsumed:
break;
}
}
}
void ConsumedStmtVisitor::forwardInfo(const Stmt *From, const Stmt *To) {
InfoEntry Entry = PropagationMap.find(From);
if (Entry != PropagationMap.end()) {
PropagationMap.insert(PairType(To, PropagationInfo(Entry->second)));
}
}
bool ConsumedStmtVisitor::isLikeMoveAssignment(
const CXXMethodDecl *MethodDecl) {
return MethodDecl->isMoveAssignmentOperator() ||
(MethodDecl->getOverloadedOperator() == OO_Equal &&
MethodDecl->getNumParams() == 1 &&
MethodDecl->getParamDecl(0)->getType()->isRValueReferenceType());
}
void ConsumedStmtVisitor::VisitBinaryOperator(const BinaryOperator *BinOp) {
switch (BinOp->getOpcode()) {
case BO_PtrMemD:
case BO_PtrMemI:
forwardInfo(BinOp->getLHS(), BinOp);
break;
default:
break;
}
}
void ConsumedStmtVisitor::Visit(const Stmt *StmtNode) {
ConstStmtVisitor<ConsumedStmtVisitor>::Visit(StmtNode);
for (Stmt::const_child_iterator CI = StmtNode->child_begin(),
CE = StmtNode->child_end(); CI != CE; ++CI) {
PropagationMap.erase(*CI);
}
}
void ConsumedStmtVisitor::VisitCallExpr(const CallExpr *Call) {
if (const FunctionDecl *FunDecl =
dyn_cast_or_null<FunctionDecl>(Call->getDirectCallee())) {
// Special case for the std::move function.
// TODO: Make this more specific. (Deferred)
if (FunDecl->getNameAsString() == "move") {
InfoEntry Entry = PropagationMap.find(Call->getArg(0));
if (Entry != PropagationMap.end()) {
PropagationMap.insert(PairType(Call, Entry->second));
}
return;
}
unsigned Offset = Call->getNumArgs() - FunDecl->getNumParams();
for (unsigned Index = Offset; Index < Call->getNumArgs(); ++Index) {
QualType ParamType = FunDecl->getParamDecl(Index - Offset)->getType();
InfoEntry Entry = PropagationMap.find(Call->getArg(Index));
if (Entry == PropagationMap.end() || !Entry->second.IsVar) {
continue;
}
PropagationInfo PState = Entry->second;
if (ParamType->isRValueReferenceType() ||
(ParamType->isLValueReferenceType() &&
!cast<LValueReferenceType>(*ParamType).isSpelledAsLValue())) {
StateMap->setState(PState.getVar(), consumed::CS_Consumed);
} else if (!(ParamType.isConstQualified() ||
((ParamType->isReferenceType() ||
ParamType->isPointerType()) &&
ParamType->getPointeeType().isConstQualified()))) {
StateMap->setState(PState.getVar(), consumed::CS_Unknown);
}
}
}
}
void ConsumedStmtVisitor::VisitCastExpr(const CastExpr *Cast) {
InfoEntry Entry = PropagationMap.find(Cast->getSubExpr());
if (Entry != PropagationMap.end())
PropagationMap.insert(PairType(Cast, Entry->second));
}
void ConsumedStmtVisitor::VisitCXXConstructExpr(const CXXConstructExpr *Call) {
CXXConstructorDecl *Constructor = Call->getConstructor();
ASTContext &CurrContext = AC.getASTContext();
QualType ThisType = Constructor->getThisType(CurrContext)->getPointeeType();
if (Analyzer.isConsumableType(ThisType)) {
if (Constructor->hasAttr<ConsumesAttr>() ||
Constructor->isDefaultConstructor()) {
PropagationMap.insert(PairType(Call,
PropagationInfo(consumed::CS_Consumed)));
} else if (Constructor->isMoveConstructor()) {
PropagationInfo PState =
PropagationMap.find(Call->getArg(0))->second;
if (PState.IsVar) {
const VarDecl* Var = PState.getVar();
PropagationMap.insert(PairType(Call,
PropagationInfo(StateMap->getState(Var))));
StateMap->setState(Var, consumed::CS_Consumed);
} else {
PropagationMap.insert(PairType(Call, PState));
}
} else if (Constructor->isCopyConstructor()) {
MapType::iterator Entry = PropagationMap.find(Call->getArg(0));
if (Entry != PropagationMap.end())
PropagationMap.insert(PairType(Call, Entry->second));
} else {
PropagationMap.insert(PairType(Call,
PropagationInfo(consumed::CS_Unconsumed)));
}
}
}
void ConsumedStmtVisitor::VisitCXXMemberCallExpr(
const CXXMemberCallExpr *Call) {
VisitCallExpr(Call);
InfoEntry Entry = PropagationMap.find(Call->getCallee()->IgnoreParens());
if (Entry != PropagationMap.end()) {
PropagationInfo PState = Entry->second;
const CXXMethodDecl *MethodDecl = Call->getMethodDecl();
checkCallability(PState, MethodDecl, Call);
if (PState.IsVar) {
if (MethodDecl->hasAttr<ConsumesAttr>())
StateMap->setState(PState.getVar(), consumed::CS_Consumed);
else if (!MethodDecl->isConst())
StateMap->setState(PState.getVar(), consumed::CS_Unknown);
}
}
}
void ConsumedStmtVisitor::VisitCXXOperatorCallExpr(
const CXXOperatorCallExpr *Call) {
const FunctionDecl *FunDecl =
dyn_cast_or_null<FunctionDecl>(Call->getDirectCallee());
if (!FunDecl) return;
if (isa<CXXMethodDecl>(FunDecl) &&
isLikeMoveAssignment(cast<CXXMethodDecl>(FunDecl))) {
InfoEntry LEntry = PropagationMap.find(Call->getArg(0));
InfoEntry REntry = PropagationMap.find(Call->getArg(1));
PropagationInfo LPState, RPState;
if (LEntry != PropagationMap.end() &&
REntry != PropagationMap.end()) {
LPState = LEntry->second;
RPState = REntry->second;
if (LPState.IsVar && RPState.IsVar) {
StateMap->setState(LPState.getVar(),
StateMap->getState(RPState.getVar()));
StateMap->setState(RPState.getVar(), consumed::CS_Consumed);
PropagationMap.insert(PairType(Call, LPState));
} else if (LPState.IsVar && !RPState.IsVar) {
StateMap->setState(LPState.getVar(), RPState.getState());
PropagationMap.insert(PairType(Call, LPState));
} else if (!LPState.IsVar && RPState.IsVar) {
PropagationMap.insert(PairType(Call,
PropagationInfo(StateMap->getState(RPState.getVar()))));
StateMap->setState(RPState.getVar(), consumed::CS_Consumed);
} else {
PropagationMap.insert(PairType(Call, RPState));
}
} else if (LEntry != PropagationMap.end() &&
REntry == PropagationMap.end()) {
LPState = LEntry->second;
if (LPState.IsVar) {
StateMap->setState(LPState.getVar(), consumed::CS_Unknown);
PropagationMap.insert(PairType(Call, LPState));
} else {
PropagationMap.insert(PairType(Call,
PropagationInfo(consumed::CS_Unknown)));
}
} else if (LEntry == PropagationMap.end() &&
REntry != PropagationMap.end()) {
RPState = REntry->second;
if (RPState.IsVar) {
const VarDecl *Var = RPState.getVar();
PropagationMap.insert(PairType(Call,
PropagationInfo(StateMap->getState(Var))));
StateMap->setState(Var, consumed::CS_Consumed);
} else {
PropagationMap.insert(PairType(Call, RPState));
}
}
} else {
VisitCallExpr(Call);
InfoEntry Entry = PropagationMap.find(Call->getArg(0));
if (Entry != PropagationMap.end()) {
PropagationInfo PState = Entry->second;
checkCallability(PState, FunDecl, Call);
if (PState.IsVar) {
if (FunDecl->hasAttr<ConsumesAttr>()) {
// Handle consuming operators.
StateMap->setState(PState.getVar(), consumed::CS_Consumed);
} else if (const CXXMethodDecl *MethodDecl =
dyn_cast_or_null<CXXMethodDecl>(FunDecl)) {
// Handle non-constant member operators.
if (!MethodDecl->isConst())
StateMap->setState(PState.getVar(), consumed::CS_Unknown);
}
}
}
}
}
void ConsumedStmtVisitor::VisitDeclRefExpr(const DeclRefExpr *DeclRef) {
if (const VarDecl *Var = dyn_cast_or_null<VarDecl>(DeclRef->getDecl()))
if (StateMap->getState(Var) != consumed::CS_None)
PropagationMap.insert(PairType(DeclRef, PropagationInfo(Var)));
}
void ConsumedStmtVisitor::VisitDeclStmt(const DeclStmt *DeclS) {
for (DeclStmt::const_decl_iterator DI = DeclS->decl_begin(),
DE = DeclS->decl_end(); DI != DE; ++DI) {
if (isa<VarDecl>(*DI)) VisitVarDecl(cast<VarDecl>(*DI));
}
if (DeclS->isSingleDecl())
if (const VarDecl *Var = dyn_cast_or_null<VarDecl>(DeclS->getSingleDecl()))
PropagationMap.insert(PairType(DeclS, PropagationInfo(Var)));
}
void ConsumedStmtVisitor::VisitMaterializeTemporaryExpr(
const MaterializeTemporaryExpr *Temp) {
InfoEntry Entry = PropagationMap.find(Temp->GetTemporaryExpr());
if (Entry != PropagationMap.end())
PropagationMap.insert(PairType(Temp, Entry->second));
}
void ConsumedStmtVisitor::VisitMemberExpr(const MemberExpr *MExpr) {
forwardInfo(MExpr->getBase(), MExpr);
}
void ConsumedStmtVisitor::VisitUnaryOperator(const UnaryOperator *UOp) {
if (UOp->getOpcode() == UO_AddrOf) {
InfoEntry Entry = PropagationMap.find(UOp->getSubExpr());
if (Entry != PropagationMap.end())
PropagationMap.insert(PairType(UOp, Entry->second));
}
}
void ConsumedStmtVisitor::VisitVarDecl(const VarDecl *Var) {
if (Analyzer.isConsumableType(Var->getType())) {
PropagationInfo PState =
PropagationMap.find(Var->getInit())->second;
StateMap->setState(Var, PState.IsVar ?
StateMap->getState(PState.getVar()) : PState.getState());
}
}
} // end anonymous::ConsumedStmtVisitor
namespace {
// TODO: Handle variable definitions, e.g. bool valid = x.isValid();
// if (valid) ...; (Deferred)
class TestedVarsVisitor : public RecursiveASTVisitor<TestedVarsVisitor> {
bool Invert;
SourceLocation CurrTestLoc;
ConsumedStateMap *StateMap;
public:
bool IsUsefulConditional;
VarTestResult Test;
TestedVarsVisitor(ConsumedStateMap *StateMap) : Invert(false),
StateMap(StateMap), IsUsefulConditional(false) {}
bool VisitCallExpr(CallExpr *Call);
bool VisitDeclRefExpr(DeclRefExpr *DeclRef);
bool VisitUnaryOperator(UnaryOperator *UnaryOp);
};
bool TestedVarsVisitor::VisitCallExpr(CallExpr *Call) {
if (const FunctionDecl *FunDecl =
dyn_cast_or_null<FunctionDecl>(Call->getDirectCallee())) {
if (isTestingFunction(FunDecl)) {
CurrTestLoc = Call->getExprLoc();
IsUsefulConditional = true;
return true;
}
IsUsefulConditional = false;
}
return false;
}
bool TestedVarsVisitor::VisitDeclRefExpr(DeclRefExpr *DeclRef) {
if (const VarDecl *Var = dyn_cast_or_null<VarDecl>(DeclRef->getDecl()))
if (StateMap->getState(Var) != consumed::CS_None)
Test = VarTestResult(Var, CurrTestLoc, !Invert);
return true;
}
bool TestedVarsVisitor::VisitUnaryOperator(UnaryOperator *UnaryOp) {
if (UnaryOp->getOpcode() == UO_LNot) {
Invert = true;
TraverseStmt(UnaryOp->getSubExpr());
} else {
IsUsefulConditional = false;
}
return false;
}
} // end anonymouse::TestedVarsVisitor
namespace clang {
namespace consumed {
void ConsumedBlockInfo::addInfo(const CFGBlock *Block,
ConsumedStateMap *StateMap,
bool &AlreadyOwned) {
if (VisitedBlocks.alreadySet(Block)) return;
ConsumedStateMap *Entry = StateMapsArray[Block->getBlockID()];
if (Entry) {
Entry->intersect(StateMap);
} else if (AlreadyOwned) {
StateMapsArray[Block->getBlockID()] = new ConsumedStateMap(*StateMap);
} else {
StateMapsArray[Block->getBlockID()] = StateMap;
AlreadyOwned = true;
}
}
void ConsumedBlockInfo::addInfo(const CFGBlock *Block,
ConsumedStateMap *StateMap) {
if (VisitedBlocks.alreadySet(Block)) {
delete StateMap;
return;
}
ConsumedStateMap *Entry = StateMapsArray[Block->getBlockID()];
if (Entry) {
Entry->intersect(StateMap);
delete StateMap;
} else {
StateMapsArray[Block->getBlockID()] = StateMap;
}
}
ConsumedStateMap* ConsumedBlockInfo::getInfo(const CFGBlock *Block) {
return StateMapsArray[Block->getBlockID()];
}
void ConsumedBlockInfo::markVisited(const CFGBlock *Block) {
VisitedBlocks.insert(Block);
}
ConsumedState ConsumedStateMap::getState(const VarDecl *Var) {
MapType::const_iterator Entry = Map.find(Var);
if (Entry != Map.end()) {
return Entry->second;
} else {
return CS_None;
}
}
void ConsumedStateMap::intersect(const ConsumedStateMap *Other) {
ConsumedState LocalState;
for (MapType::const_iterator DMI = Other->Map.begin(),
DME = Other->Map.end(); DMI != DME; ++DMI) {
LocalState = this->getState(DMI->first);
if (LocalState != CS_None && LocalState != DMI->second)
setState(DMI->first, CS_Unknown);
}
}
void ConsumedStateMap::makeUnknown() {
PairType Pair;
for (MapType::const_iterator DMI = Map.begin(), DME = Map.end(); DMI != DME;
++DMI) {
Pair = *DMI;
Map.erase(Pair.first);
Map.insert(PairType(Pair.first, CS_Unknown));
}
}
void ConsumedStateMap::setState(const VarDecl *Var, ConsumedState State) {
Map[Var] = State;
}
void ConsumedStateMap::remove(const VarDecl *Var) {
Map.erase(Var);
}
bool ConsumedAnalyzer::isConsumableType(QualType Type) {
const CXXRecordDecl *RD =
dyn_cast_or_null<CXXRecordDecl>(Type->getAsCXXRecordDecl());
if (!RD) return false;
std::pair<CacheMapType::iterator, bool> Entry =
ConsumableTypeCache.insert(std::make_pair(RD, false));
if (Entry.second)
Entry.first->second = hasConsumableAttributes(RD);
return Entry.first->second;
}
// TODO: Walk the base classes to see if any of them are unique types.
// (Deferred)
bool ConsumedAnalyzer::hasConsumableAttributes(const CXXRecordDecl *RD) {
for (CXXRecordDecl::method_iterator MI = RD->method_begin(),
ME = RD->method_end(); MI != ME; ++MI) {
for (Decl::attr_iterator AI = (*MI)->attr_begin(), AE = (*MI)->attr_end();
AI != AE; ++AI) {
switch ((*AI)->getKind()) {
case attr::CallableWhenUnconsumed:
case attr::TestsUnconsumed:
return true;
default:
break;
}
}
}
return false;
}
// TODO: Handle other forms of branching with precision, including while- and
// for-loops. (Deferred)
void ConsumedAnalyzer::splitState(const CFGBlock *CurrBlock,
const IfStmt *Terminator) {
TestedVarsVisitor Visitor(CurrStates);
Visitor.TraverseStmt(const_cast<Expr*>(Terminator->getCond()));
bool HasElse = Terminator->getElse() != NULL;
ConsumedStateMap *ElseOrMergeStates = new ConsumedStateMap(*CurrStates);
if (Visitor.IsUsefulConditional) {
ConsumedState VarState = CurrStates->getState(Visitor.Test.Var);
if (VarState != CS_Unknown) {
// FIXME: Make this not warn if the test is from a macro expansion.
// (Deferred)
WarningsHandler.warnUnnecessaryTest(Visitor.Test.Var->getNameAsString(),
stateToString(VarState), Visitor.Test.Loc);
}
if (Visitor.Test.UnconsumedInTrueBranch) {
CurrStates->setState(Visitor.Test.Var, CS_Unconsumed);
if (HasElse) ElseOrMergeStates->setState(Visitor.Test.Var, CS_Consumed);
} else {
CurrStates->setState(Visitor.Test.Var, CS_Consumed);
if (HasElse) ElseOrMergeStates->setState(Visitor.Test.Var, CS_Unconsumed);
}
}
CFGBlock::const_succ_iterator SI = CurrBlock->succ_begin();
if (*SI) BlockInfo.addInfo(*SI, CurrStates);
if (*++SI) BlockInfo.addInfo(*SI, ElseOrMergeStates);
}
void ConsumedAnalyzer::run(AnalysisDeclContext &AC) {
const FunctionDecl *D = dyn_cast_or_null<FunctionDecl>(AC.getDecl());
if (!D) return;
BlockInfo = ConsumedBlockInfo(AC.getCFG());
PostOrderCFGView *SortedGraph = AC.getAnalysis<PostOrderCFGView>();
CurrStates = new ConsumedStateMap();
// Visit all of the function's basic blocks.
for (PostOrderCFGView::iterator I = SortedGraph->begin(),
E = SortedGraph->end(); I != E; ++I) {
const CFGBlock *CurrBlock = *I;
BlockInfo.markVisited(CurrBlock);
if (CurrStates == NULL)
CurrStates = BlockInfo.getInfo(CurrBlock);
ConsumedStmtVisitor Visitor(AC, *this, CurrStates);
// Visit all of the basic block's statements.
for (CFGBlock::const_iterator BI = CurrBlock->begin(),
BE = CurrBlock->end(); BI != BE; ++BI) {
switch (BI->getKind()) {
case CFGElement::Statement:
Visitor.Visit(BI->castAs<CFGStmt>().getStmt());
break;
case CFGElement::AutomaticObjectDtor:
CurrStates->remove(BI->castAs<CFGAutomaticObjDtor>().getVarDecl());
default:
break;
}
}
if (const IfStmt *Terminator =
dyn_cast_or_null<IfStmt>(CurrBlock->getTerminator().getStmt())) {
splitState(CurrBlock, Terminator);
CurrStates = NULL;
} else if (CurrBlock->succ_size() > 1) {
CurrStates->makeUnknown();
bool OwnershipTaken = false;
for (CFGBlock::const_succ_iterator SI = CurrBlock->succ_begin(),
SE = CurrBlock->succ_end(); SI != SE; ++SI) {
if (*SI) BlockInfo.addInfo(*SI, CurrStates, OwnershipTaken);
}
if (!OwnershipTaken)
delete CurrStates;
CurrStates = NULL;
} else if (CurrBlock->succ_size() == 1 &&
(*CurrBlock->succ_begin())->pred_size() > 1) {
BlockInfo.addInfo(*CurrBlock->succ_begin(), CurrStates);
CurrStates = NULL;
}
Visitor.reset();
} // End of block iterator.
// Delete the last existing state map.
delete CurrStates;
WarningsHandler.emitDiagnostics();
}
}} // end namespace clang::consumed
|