clang  6.0.0svn
Consumed.cpp
Go to the documentation of this file.
1 //===- Consumed.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 // A intra-procedural analysis for checking consumed properties. This is based,
11 // in part, on research on linear types.
12 //
13 //===----------------------------------------------------------------------===//
14 
16 #include "clang/AST/ASTContext.h"
17 #include "clang/AST/Attr.h"
18 #include "clang/AST/DeclCXX.h"
19 #include "clang/AST/ExprCXX.h"
21 #include "clang/AST/StmtCXX.h"
22 #include "clang/AST/StmtVisitor.h"
23 #include "clang/AST/Type.h"
26 #include "clang/Analysis/CFG.h"
29 #include "llvm/ADT/DenseMap.h"
30 #include <memory>
31 
32 // TODO: Adjust states of args to constructors in the same way that arguments to
33 // function calls are handled.
34 // TODO: Use information from tests in for- and while-loop conditional.
35 // TODO: Add notes about the actual and expected state for
36 // TODO: Correctly identify unreachable blocks when chaining boolean operators.
37 // TODO: Adjust the parser and AttributesList class to support lists of
38 // identifiers.
39 // TODO: Warn about unreachable code.
40 // TODO: Switch to using a bitmap to track unreachable blocks.
41 // TODO: Handle variable definitions, e.g. bool valid = x.isValid();
42 // if (valid) ...; (Deferred)
43 // TODO: Take notes on state transitions to provide better warning messages.
44 // (Deferred)
45 // TODO: Test nested conditionals: A) Checking the same value multiple times,
46 // and 2) Checking different values. (Deferred)
47 
48 using namespace clang;
49 using namespace consumed;
50 
51 // Key method definition
53 
54 static SourceLocation getFirstStmtLoc(const CFGBlock *Block) {
55  // Find the source location of the first statement in the block, if the block
56  // is not empty.
57  for (const auto &B : *Block)
58  if (Optional<CFGStmt> CS = B.getAs<CFGStmt>())
59  return CS->getStmt()->getLocStart();
60 
61  // Block is empty.
62  // If we have one successor, return the first statement in that block
63  if (Block->succ_size() == 1 && *Block->succ_begin())
64  return getFirstStmtLoc(*Block->succ_begin());
65 
66  return SourceLocation();
67 }
68 
69 static SourceLocation getLastStmtLoc(const CFGBlock *Block) {
70  // Find the source location of the last statement in the block, if the block
71  // is not empty.
72  if (const Stmt *StmtNode = Block->getTerminator()) {
73  return StmtNode->getLocStart();
74  } else {
75  for (CFGBlock::const_reverse_iterator BI = Block->rbegin(),
76  BE = Block->rend(); BI != BE; ++BI) {
77  if (Optional<CFGStmt> CS = BI->getAs<CFGStmt>())
78  return CS->getStmt()->getLocStart();
79  }
80  }
81 
82  // If we have one successor, return the first statement in that block
83  SourceLocation Loc;
84  if (Block->succ_size() == 1 && *Block->succ_begin())
85  Loc = getFirstStmtLoc(*Block->succ_begin());
86  if (Loc.isValid())
87  return Loc;
88 
89  // If we have one predecessor, return the last statement in that block
90  if (Block->pred_size() == 1 && *Block->pred_begin())
91  return getLastStmtLoc(*Block->pred_begin());
92 
93  return Loc;
94 }
95 
97  switch (State) {
98  case CS_Unconsumed:
99  return CS_Consumed;
100  case CS_Consumed:
101  return CS_Unconsumed;
102  case CS_None:
103  return CS_None;
104  case CS_Unknown:
105  return CS_Unknown;
106  }
107  llvm_unreachable("invalid enum");
108 }
109 
110 static bool isCallableInState(const CallableWhenAttr *CWAttr,
112 
113  for (const auto &S : CWAttr->callableStates()) {
114  ConsumedState MappedAttrState = CS_None;
115 
116  switch (S) {
118  MappedAttrState = CS_Unknown;
119  break;
120 
121  case CallableWhenAttr::Unconsumed:
122  MappedAttrState = CS_Unconsumed;
123  break;
124 
125  case CallableWhenAttr::Consumed:
126  MappedAttrState = CS_Consumed;
127  break;
128  }
129 
130  if (MappedAttrState == State)
131  return true;
132  }
133 
134  return false;
135 }
136 
137 
138 static bool isConsumableType(const QualType &QT) {
139  if (QT->isPointerType() || QT->isReferenceType())
140  return false;
141 
142  if (const CXXRecordDecl *RD = QT->getAsCXXRecordDecl())
143  return RD->hasAttr<ConsumableAttr>();
144 
145  return false;
146 }
147 
148 static bool isAutoCastType(const QualType &QT) {
149  if (QT->isPointerType() || QT->isReferenceType())
150  return false;
151 
152  if (const CXXRecordDecl *RD = QT->getAsCXXRecordDecl())
153  return RD->hasAttr<ConsumableAutoCastAttr>();
154 
155  return false;
156 }
157 
158 static bool isSetOnReadPtrType(const QualType &QT) {
159  if (const CXXRecordDecl *RD = QT->getPointeeCXXRecordDecl())
160  return RD->hasAttr<ConsumableSetOnReadAttr>();
161  return false;
162 }
163 
164 
166  switch (State) {
167  case CS_Unconsumed:
168  case CS_Consumed:
169  return true;
170  case CS_None:
171  case CS_Unknown:
172  return false;
173  }
174  llvm_unreachable("invalid enum");
175 }
176 
177 static bool isRValueRef(QualType ParamType) {
178  return ParamType->isRValueReferenceType();
179 }
180 
181 static bool isTestingFunction(const FunctionDecl *FunDecl) {
182  return FunDecl->hasAttr<TestTypestateAttr>();
183 }
184 
185 static bool isPointerOrRef(QualType ParamType) {
186  return ParamType->isPointerType() || ParamType->isReferenceType();
187 }
188 
190  assert(isConsumableType(QT));
191 
192  const ConsumableAttr *CAttr =
193  QT->getAsCXXRecordDecl()->getAttr<ConsumableAttr>();
194 
195  switch (CAttr->getDefaultState()) {
197  return CS_Unknown;
198  case ConsumableAttr::Unconsumed:
199  return CS_Unconsumed;
200  case ConsumableAttr::Consumed:
201  return CS_Consumed;
202  }
203  llvm_unreachable("invalid enum");
204 }
205 
206 static ConsumedState
207 mapParamTypestateAttrState(const ParamTypestateAttr *PTAttr) {
208  switch (PTAttr->getParamState()) {
210  return CS_Unknown;
211  case ParamTypestateAttr::Unconsumed:
212  return CS_Unconsumed;
213  case ParamTypestateAttr::Consumed:
214  return CS_Consumed;
215  }
216  llvm_unreachable("invalid_enum");
217 }
218 
219 static ConsumedState
220 mapReturnTypestateAttrState(const ReturnTypestateAttr *RTSAttr) {
221  switch (RTSAttr->getState()) {
223  return CS_Unknown;
224  case ReturnTypestateAttr::Unconsumed:
225  return CS_Unconsumed;
226  case ReturnTypestateAttr::Consumed:
227  return CS_Consumed;
228  }
229  llvm_unreachable("invalid enum");
230 }
231 
232 static ConsumedState mapSetTypestateAttrState(const SetTypestateAttr *STAttr) {
233  switch (STAttr->getNewState()) {
235  return CS_Unknown;
236  case SetTypestateAttr::Unconsumed:
237  return CS_Unconsumed;
238  case SetTypestateAttr::Consumed:
239  return CS_Consumed;
240  }
241  llvm_unreachable("invalid_enum");
242 }
243 
244 static StringRef stateToString(ConsumedState State) {
245  switch (State) {
246  case consumed::CS_None:
247  return "none";
248 
250  return "unknown";
251 
253  return "unconsumed";
254 
256  return "consumed";
257  }
258  llvm_unreachable("invalid enum");
259 }
260 
261 static ConsumedState testsFor(const FunctionDecl *FunDecl) {
262  assert(isTestingFunction(FunDecl));
263  switch (FunDecl->getAttr<TestTypestateAttr>()->getTestState()) {
264  case TestTypestateAttr::Unconsumed:
265  return CS_Unconsumed;
266  case TestTypestateAttr::Consumed:
267  return CS_Consumed;
268  }
269  llvm_unreachable("invalid enum");
270 }
271 
272 namespace {
273 struct VarTestResult {
274  const VarDecl *Var;
275  ConsumedState TestsFor;
276 };
277 } // end anonymous::VarTestResult
278 
279 namespace clang {
280 namespace consumed {
281 
285 };
286 
288  enum {
289  IT_None,
290  IT_State,
291  IT_VarTest,
292  IT_BinTest,
293  IT_Var,
294  IT_Tmp
295  } InfoType;
296 
297  struct BinTestTy {
298  const BinaryOperator *Source;
299  EffectiveOp EOp;
300  VarTestResult LTest;
301  VarTestResult RTest;
302  };
303 
304  union {
306  VarTestResult VarTest;
307  const VarDecl *Var;
309  BinTestTy BinTest;
310  };
311 
312 public:
313  PropagationInfo() : InfoType(IT_None) {}
314 
315  PropagationInfo(const VarTestResult &VarTest)
316  : InfoType(IT_VarTest), VarTest(VarTest) {}
317 
318  PropagationInfo(const VarDecl *Var, ConsumedState TestsFor)
319  : InfoType(IT_VarTest) {
320 
321  VarTest.Var = Var;
322  VarTest.TestsFor = TestsFor;
323  }
324 
326  const VarTestResult &LTest, const VarTestResult &RTest)
327  : InfoType(IT_BinTest) {
328 
329  BinTest.Source = Source;
330  BinTest.EOp = EOp;
331  BinTest.LTest = LTest;
332  BinTest.RTest = RTest;
333  }
334 
336  const VarDecl *LVar, ConsumedState LTestsFor,
337  const VarDecl *RVar, ConsumedState RTestsFor)
338  : InfoType(IT_BinTest) {
339 
340  BinTest.Source = Source;
341  BinTest.EOp = EOp;
342  BinTest.LTest.Var = LVar;
343  BinTest.LTest.TestsFor = LTestsFor;
344  BinTest.RTest.Var = RVar;
345  BinTest.RTest.TestsFor = RTestsFor;
346  }
347 
349  : InfoType(IT_State), State(State) {}
350 
351  PropagationInfo(const VarDecl *Var) : InfoType(IT_Var), Var(Var) {}
353  : InfoType(IT_Tmp), Tmp(Tmp) {}
354 
355  const ConsumedState & getState() const {
356  assert(InfoType == IT_State);
357  return State;
358  }
359 
360  const VarTestResult & getVarTest() const {
361  assert(InfoType == IT_VarTest);
362  return VarTest;
363  }
364 
365  const VarTestResult & getLTest() const {
366  assert(InfoType == IT_BinTest);
367  return BinTest.LTest;
368  }
369 
370  const VarTestResult & getRTest() const {
371  assert(InfoType == IT_BinTest);
372  return BinTest.RTest;
373  }
374 
375  const VarDecl * getVar() const {
376  assert(InfoType == IT_Var);
377  return Var;
378  }
379 
380  const CXXBindTemporaryExpr * getTmp() const {
381  assert(InfoType == IT_Tmp);
382  return Tmp;
383  }
384 
385  ConsumedState getAsState(const ConsumedStateMap *StateMap) const {
386  assert(isVar() || isTmp() || isState());
387 
388  if (isVar())
389  return StateMap->getState(Var);
390  else if (isTmp())
391  return StateMap->getState(Tmp);
392  else if (isState())
393  return State;
394  else
395  return CS_None;
396  }
397 
399  assert(InfoType == IT_BinTest);
400  return BinTest.EOp;
401  }
402 
403  const BinaryOperator * testSourceNode() const {
404  assert(InfoType == IT_BinTest);
405  return BinTest.Source;
406  }
407 
408  inline bool isValid() const { return InfoType != IT_None; }
409  inline bool isState() const { return InfoType == IT_State; }
410  inline bool isVarTest() const { return InfoType == IT_VarTest; }
411  inline bool isBinTest() const { return InfoType == IT_BinTest; }
412  inline bool isVar() const { return InfoType == IT_Var; }
413  inline bool isTmp() const { return InfoType == IT_Tmp; }
414 
415  bool isTest() const {
416  return InfoType == IT_VarTest || InfoType == IT_BinTest;
417  }
418 
419  bool isPointerToValue() const {
420  return InfoType == IT_Var || InfoType == IT_Tmp;
421  }
422 
424  assert(InfoType == IT_VarTest || InfoType == IT_BinTest);
425 
426  if (InfoType == IT_VarTest) {
427  return PropagationInfo(VarTest.Var,
428  invertConsumedUnconsumed(VarTest.TestsFor));
429 
430  } else if (InfoType == IT_BinTest) {
431  return PropagationInfo(BinTest.Source,
432  BinTest.EOp == EO_And ? EO_Or : EO_And,
433  BinTest.LTest.Var, invertConsumedUnconsumed(BinTest.LTest.TestsFor),
434  BinTest.RTest.Var, invertConsumedUnconsumed(BinTest.RTest.TestsFor));
435  } else {
436  return PropagationInfo();
437  }
438  }
439 };
440 
441 static inline void
444 
445  assert(PInfo.isVar() || PInfo.isTmp());
446 
447  if (PInfo.isVar())
448  StateMap->setState(PInfo.getVar(), State);
449  else
450  StateMap->setState(PInfo.getTmp(), State);
451 }
452 
453 class ConsumedStmtVisitor : public ConstStmtVisitor<ConsumedStmtVisitor> {
454 
455  typedef llvm::DenseMap<const Stmt *, PropagationInfo> MapType;
456  typedef std::pair<const Stmt *, PropagationInfo> PairType;
457  typedef MapType::iterator InfoEntry;
458  typedef MapType::const_iterator ConstInfoEntry;
459 
461  ConsumedAnalyzer &Analyzer;
462  ConsumedStateMap *StateMap;
463  MapType PropagationMap;
464 
465  InfoEntry findInfo(const Expr *E) {
466  if (auto Cleanups = dyn_cast<ExprWithCleanups>(E))
467  if (!Cleanups->cleanupsHaveSideEffects())
468  E = Cleanups->getSubExpr();
469  return PropagationMap.find(E->IgnoreParens());
470  }
471  ConstInfoEntry findInfo(const Expr *E) const {
472  if (auto Cleanups = dyn_cast<ExprWithCleanups>(E))
473  if (!Cleanups->cleanupsHaveSideEffects())
474  E = Cleanups->getSubExpr();
475  return PropagationMap.find(E->IgnoreParens());
476  }
477  void insertInfo(const Expr *E, const PropagationInfo &PI) {
478  PropagationMap.insert(PairType(E->IgnoreParens(), PI));
479  }
480 
481  void forwardInfo(const Expr *From, const Expr *To);
482  void copyInfo(const Expr *From, const Expr *To, ConsumedState CS);
483  ConsumedState getInfo(const Expr *From);
484  void setInfo(const Expr *To, ConsumedState NS);
485  void propagateReturnType(const Expr *Call, const FunctionDecl *Fun);
486 
487 public:
488  void checkCallability(const PropagationInfo &PInfo,
489  const FunctionDecl *FunDecl,
490  SourceLocation BlameLoc);
491  bool handleCall(const CallExpr *Call, const Expr *ObjArg,
492  const FunctionDecl *FunD);
493 
494  void VisitBinaryOperator(const BinaryOperator *BinOp);
495  void VisitCallExpr(const CallExpr *Call);
496  void VisitCastExpr(const CastExpr *Cast);
497  void VisitCXXBindTemporaryExpr(const CXXBindTemporaryExpr *Temp);
498  void VisitCXXConstructExpr(const CXXConstructExpr *Call);
499  void VisitCXXMemberCallExpr(const CXXMemberCallExpr *Call);
500  void VisitCXXOperatorCallExpr(const CXXOperatorCallExpr *Call);
501  void VisitDeclRefExpr(const DeclRefExpr *DeclRef);
502  void VisitDeclStmt(const DeclStmt *DelcS);
503  void VisitMaterializeTemporaryExpr(const MaterializeTemporaryExpr *Temp);
504  void VisitMemberExpr(const MemberExpr *MExpr);
505  void VisitParmVarDecl(const ParmVarDecl *Param);
506  void VisitReturnStmt(const ReturnStmt *Ret);
507  void VisitUnaryOperator(const UnaryOperator *UOp);
508  void VisitVarDecl(const VarDecl *Var);
509 
511  ConsumedStateMap *StateMap)
512  : AC(AC), Analyzer(Analyzer), StateMap(StateMap) {}
513 
514  PropagationInfo getInfo(const Expr *StmtNode) const {
515  ConstInfoEntry Entry = findInfo(StmtNode);
516 
517  if (Entry != PropagationMap.end())
518  return Entry->second;
519  else
520  return PropagationInfo();
521  }
522 
523  void reset(ConsumedStateMap *NewStateMap) {
524  StateMap = NewStateMap;
525  }
526 };
527 
528 
529 void ConsumedStmtVisitor::forwardInfo(const Expr *From, const Expr *To) {
530  InfoEntry Entry = findInfo(From);
531  if (Entry != PropagationMap.end())
532  insertInfo(To, Entry->second);
533 }
534 
535 
536 // Create a new state for To, which is initialized to the state of From.
537 // If NS is not CS_None, sets the state of From to NS.
538 void ConsumedStmtVisitor::copyInfo(const Expr *From, const Expr *To,
539  ConsumedState NS) {
540  InfoEntry Entry = findInfo(From);
541  if (Entry != PropagationMap.end()) {
542  PropagationInfo& PInfo = Entry->second;
543  ConsumedState CS = PInfo.getAsState(StateMap);
544  if (CS != CS_None)
545  insertInfo(To, PropagationInfo(CS));
546  if (NS != CS_None && PInfo.isPointerToValue())
547  setStateForVarOrTmp(StateMap, PInfo, NS);
548  }
549 }
550 
551 
552 // Get the ConsumedState for From
553 ConsumedState ConsumedStmtVisitor::getInfo(const Expr *From) {
554  InfoEntry Entry = findInfo(From);
555  if (Entry != PropagationMap.end()) {
556  PropagationInfo& PInfo = Entry->second;
557  return PInfo.getAsState(StateMap);
558  }
559  return CS_None;
560 }
561 
562 
563 // If we already have info for To then update it, otherwise create a new entry.
564 void ConsumedStmtVisitor::setInfo(const Expr *To, ConsumedState NS) {
565  InfoEntry Entry = findInfo(To);
566  if (Entry != PropagationMap.end()) {
567  PropagationInfo& PInfo = Entry->second;
568  if (PInfo.isPointerToValue())
569  setStateForVarOrTmp(StateMap, PInfo, NS);
570  } else if (NS != CS_None) {
571  insertInfo(To, PropagationInfo(NS));
572  }
573 }
574 
575 
576 
578  const FunctionDecl *FunDecl,
579  SourceLocation BlameLoc) {
580  assert(!PInfo.isTest());
581 
582  const CallableWhenAttr *CWAttr = FunDecl->getAttr<CallableWhenAttr>();
583  if (!CWAttr)
584  return;
585 
586  if (PInfo.isVar()) {
587  ConsumedState VarState = StateMap->getState(PInfo.getVar());
588 
589  if (VarState == CS_None || isCallableInState(CWAttr, VarState))
590  return;
591 
592  Analyzer.WarningsHandler.warnUseInInvalidState(
593  FunDecl->getNameAsString(), PInfo.getVar()->getNameAsString(),
594  stateToString(VarState), BlameLoc);
595 
596  } else {
597  ConsumedState TmpState = PInfo.getAsState(StateMap);
598 
599  if (TmpState == CS_None || isCallableInState(CWAttr, TmpState))
600  return;
601 
602  Analyzer.WarningsHandler.warnUseOfTempInInvalidState(
603  FunDecl->getNameAsString(), stateToString(TmpState), BlameLoc);
604  }
605 }
606 
607 
608 // Factors out common behavior for function, method, and operator calls.
609 // Check parameters and set parameter state if necessary.
610 // Returns true if the state of ObjArg is set, or false otherwise.
611 bool ConsumedStmtVisitor::handleCall(const CallExpr *Call, const Expr *ObjArg,
612  const FunctionDecl *FunD) {
613  unsigned Offset = 0;
614  if (isa<CXXOperatorCallExpr>(Call) && isa<CXXMethodDecl>(FunD))
615  Offset = 1; // first argument is 'this'
616 
617  // check explicit parameters
618  for (unsigned Index = Offset; Index < Call->getNumArgs(); ++Index) {
619  // Skip variable argument lists.
620  if (Index - Offset >= FunD->getNumParams())
621  break;
622 
623  const ParmVarDecl *Param = FunD->getParamDecl(Index - Offset);
624  QualType ParamType = Param->getType();
625 
626  InfoEntry Entry = findInfo(Call->getArg(Index));
627 
628  if (Entry == PropagationMap.end() || Entry->second.isTest())
629  continue;
630  PropagationInfo PInfo = Entry->second;
631 
632  // Check that the parameter is in the correct state.
633  if (ParamTypestateAttr *PTA = Param->getAttr<ParamTypestateAttr>()) {
634  ConsumedState ParamState = PInfo.getAsState(StateMap);
635  ConsumedState ExpectedState = mapParamTypestateAttrState(PTA);
636 
637  if (ParamState != ExpectedState)
638  Analyzer.WarningsHandler.warnParamTypestateMismatch(
639  Call->getArg(Index)->getExprLoc(),
640  stateToString(ExpectedState), stateToString(ParamState));
641  }
642 
643  if (!(Entry->second.isVar() || Entry->second.isTmp()))
644  continue;
645 
646  // Adjust state on the caller side.
647  if (isRValueRef(ParamType))
648  setStateForVarOrTmp(StateMap, PInfo, consumed::CS_Consumed);
649  else if (ReturnTypestateAttr *RT = Param->getAttr<ReturnTypestateAttr>())
650  setStateForVarOrTmp(StateMap, PInfo, mapReturnTypestateAttrState(RT));
651  else if (isPointerOrRef(ParamType) &&
652  (!ParamType->getPointeeType().isConstQualified() ||
653  isSetOnReadPtrType(ParamType)))
654  setStateForVarOrTmp(StateMap, PInfo, consumed::CS_Unknown);
655  }
656 
657  if (!ObjArg)
658  return false;
659 
660  // check implicit 'self' parameter, if present
661  InfoEntry Entry = findInfo(ObjArg);
662  if (Entry != PropagationMap.end()) {
663  PropagationInfo PInfo = Entry->second;
664  checkCallability(PInfo, FunD, Call->getExprLoc());
665 
666  if (SetTypestateAttr *STA = FunD->getAttr<SetTypestateAttr>()) {
667  if (PInfo.isVar()) {
668  StateMap->setState(PInfo.getVar(), mapSetTypestateAttrState(STA));
669  return true;
670  }
671  else if (PInfo.isTmp()) {
672  StateMap->setState(PInfo.getTmp(), mapSetTypestateAttrState(STA));
673  return true;
674  }
675  }
676  else if (isTestingFunction(FunD) && PInfo.isVar()) {
677  PropagationMap.insert(PairType(Call,
678  PropagationInfo(PInfo.getVar(), testsFor(FunD))));
679  }
680  }
681  return false;
682 }
683 
684 
685 void ConsumedStmtVisitor::propagateReturnType(const Expr *Call,
686  const FunctionDecl *Fun) {
687  QualType RetType = Fun->getCallResultType();
688  if (RetType->isReferenceType())
689  RetType = RetType->getPointeeType();
690 
691  if (isConsumableType(RetType)) {
692  ConsumedState ReturnState;
693  if (ReturnTypestateAttr *RTA = Fun->getAttr<ReturnTypestateAttr>())
694  ReturnState = mapReturnTypestateAttrState(RTA);
695  else
696  ReturnState = mapConsumableAttrState(RetType);
697 
698  PropagationMap.insert(PairType(Call, PropagationInfo(ReturnState)));
699  }
700 }
701 
702 
704  switch (BinOp->getOpcode()) {
705  case BO_LAnd:
706  case BO_LOr : {
707  InfoEntry LEntry = findInfo(BinOp->getLHS()),
708  REntry = findInfo(BinOp->getRHS());
709 
710  VarTestResult LTest, RTest;
711 
712  if (LEntry != PropagationMap.end() && LEntry->second.isVarTest()) {
713  LTest = LEntry->second.getVarTest();
714 
715  } else {
716  LTest.Var = nullptr;
717  LTest.TestsFor = CS_None;
718  }
719 
720  if (REntry != PropagationMap.end() && REntry->second.isVarTest()) {
721  RTest = REntry->second.getVarTest();
722 
723  } else {
724  RTest.Var = nullptr;
725  RTest.TestsFor = CS_None;
726  }
727 
728  if (!(LTest.Var == nullptr && RTest.Var == nullptr))
729  PropagationMap.insert(PairType(BinOp, PropagationInfo(BinOp,
730  static_cast<EffectiveOp>(BinOp->getOpcode() == BO_LOr), LTest, RTest)));
731 
732  break;
733  }
734 
735  case BO_PtrMemD:
736  case BO_PtrMemI:
737  forwardInfo(BinOp->getLHS(), BinOp);
738  break;
739 
740  default:
741  break;
742  }
743 }
744 
746  const FunctionDecl *FunDecl = Call->getDirectCallee();
747  if (!FunDecl)
748  return;
749 
750  // Special case for the std::move function.
751  // TODO: Make this more specific. (Deferred)
752  if (Call->isCallToStdMove()) {
753  copyInfo(Call->getArg(0), Call, CS_Consumed);
754  return;
755  }
756 
757  handleCall(Call, nullptr, FunDecl);
758  propagateReturnType(Call, FunDecl);
759 }
760 
762  forwardInfo(Cast->getSubExpr(), Cast);
763 }
764 
766  const CXXBindTemporaryExpr *Temp) {
767 
768  InfoEntry Entry = findInfo(Temp->getSubExpr());
769 
770  if (Entry != PropagationMap.end() && !Entry->second.isTest()) {
771  StateMap->setState(Temp, Entry->second.getAsState(StateMap));
772  PropagationMap.insert(PairType(Temp, PropagationInfo(Temp)));
773  }
774 }
775 
777  CXXConstructorDecl *Constructor = Call->getConstructor();
778 
779  ASTContext &CurrContext = AC.getASTContext();
780  QualType ThisType = Constructor->getThisType(CurrContext)->getPointeeType();
781 
782  if (!isConsumableType(ThisType))
783  return;
784 
785  // FIXME: What should happen if someone annotates the move constructor?
786  if (ReturnTypestateAttr *RTA = Constructor->getAttr<ReturnTypestateAttr>()) {
787  // TODO: Adjust state of args appropriately.
789  PropagationMap.insert(PairType(Call, PropagationInfo(RetState)));
790  } else if (Constructor->isDefaultConstructor()) {
791  PropagationMap.insert(PairType(Call,
793  } else if (Constructor->isMoveConstructor()) {
794  copyInfo(Call->getArg(0), Call, CS_Consumed);
795  } else if (Constructor->isCopyConstructor()) {
796  // Copy state from arg. If setStateOnRead then set arg to CS_Unknown.
797  ConsumedState NS =
798  isSetOnReadPtrType(Constructor->getThisType(CurrContext)) ?
800  copyInfo(Call->getArg(0), Call, NS);
801  } else {
802  // TODO: Adjust state of args appropriately.
803  ConsumedState RetState = mapConsumableAttrState(ThisType);
804  PropagationMap.insert(PairType(Call, PropagationInfo(RetState)));
805  }
806 }
807 
808 
810  const CXXMemberCallExpr *Call) {
811  CXXMethodDecl* MD = Call->getMethodDecl();
812  if (!MD)
813  return;
814 
815  handleCall(Call, Call->getImplicitObjectArgument(), MD);
816  propagateReturnType(Call, MD);
817 }
818 
819 
821  const CXXOperatorCallExpr *Call) {
822 
823  const FunctionDecl *FunDecl =
824  dyn_cast_or_null<FunctionDecl>(Call->getDirectCallee());
825  if (!FunDecl) return;
826 
827  if (Call->getOperator() == OO_Equal) {
828  ConsumedState CS = getInfo(Call->getArg(1));
829  if (!handleCall(Call, Call->getArg(0), FunDecl))
830  setInfo(Call->getArg(0), CS);
831  return;
832  }
833 
834  if (const CXXMemberCallExpr *MCall = dyn_cast<CXXMemberCallExpr>(Call))
835  handleCall(MCall, MCall->getImplicitObjectArgument(), FunDecl);
836  else
837  handleCall(Call, Call->getArg(0), FunDecl);
838 
839  propagateReturnType(Call, FunDecl);
840 }
841 
843  if (const VarDecl *Var = dyn_cast_or_null<VarDecl>(DeclRef->getDecl()))
844  if (StateMap->getState(Var) != consumed::CS_None)
845  PropagationMap.insert(PairType(DeclRef, PropagationInfo(Var)));
846 }
847 
849  for (const auto *DI : DeclS->decls())
850  if (isa<VarDecl>(DI))
851  VisitVarDecl(cast<VarDecl>(DI));
852 
853  if (DeclS->isSingleDecl())
854  if (const VarDecl *Var = dyn_cast_or_null<VarDecl>(DeclS->getSingleDecl()))
855  PropagationMap.insert(PairType(DeclS, PropagationInfo(Var)));
856 }
857 
859  const MaterializeTemporaryExpr *Temp) {
860 
861  forwardInfo(Temp->GetTemporaryExpr(), Temp);
862 }
863 
865  forwardInfo(MExpr->getBase(), MExpr);
866 }
867 
868 
870  QualType ParamType = Param->getType();
871  ConsumedState ParamState = consumed::CS_None;
872 
873  if (const ParamTypestateAttr *PTA = Param->getAttr<ParamTypestateAttr>())
874  ParamState = mapParamTypestateAttrState(PTA);
875  else if (isConsumableType(ParamType))
876  ParamState = mapConsumableAttrState(ParamType);
877  else if (isRValueRef(ParamType) &&
878  isConsumableType(ParamType->getPointeeType()))
879  ParamState = mapConsumableAttrState(ParamType->getPointeeType());
880  else if (ParamType->isReferenceType() &&
881  isConsumableType(ParamType->getPointeeType()))
882  ParamState = consumed::CS_Unknown;
883 
884  if (ParamState != CS_None)
885  StateMap->setState(Param, ParamState);
886 }
887 
889  ConsumedState ExpectedState = Analyzer.getExpectedReturnState();
890 
891  if (ExpectedState != CS_None) {
892  InfoEntry Entry = findInfo(Ret->getRetValue());
893 
894  if (Entry != PropagationMap.end()) {
895  ConsumedState RetState = Entry->second.getAsState(StateMap);
896 
897  if (RetState != ExpectedState)
898  Analyzer.WarningsHandler.warnReturnTypestateMismatch(
899  Ret->getReturnLoc(), stateToString(ExpectedState),
900  stateToString(RetState));
901  }
902  }
903 
904  StateMap->checkParamsForReturnTypestate(Ret->getLocStart(),
905  Analyzer.WarningsHandler);
906 }
907 
909  InfoEntry Entry = findInfo(UOp->getSubExpr());
910  if (Entry == PropagationMap.end()) return;
911 
912  switch (UOp->getOpcode()) {
913  case UO_AddrOf:
914  PropagationMap.insert(PairType(UOp, Entry->second));
915  break;
916 
917  case UO_LNot:
918  if (Entry->second.isTest())
919  PropagationMap.insert(PairType(UOp, Entry->second.invertTest()));
920  break;
921 
922  default:
923  break;
924  }
925 }
926 
927 // TODO: See if I need to check for reference types here.
929  if (isConsumableType(Var->getType())) {
930  if (Var->hasInit()) {
931  MapType::iterator VIT = findInfo(Var->getInit()->IgnoreImplicit());
932  if (VIT != PropagationMap.end()) {
933  PropagationInfo PInfo = VIT->second;
934  ConsumedState St = PInfo.getAsState(StateMap);
935 
936  if (St != consumed::CS_None) {
937  StateMap->setState(Var, St);
938  return;
939  }
940  }
941  }
942  // Otherwise
943  StateMap->setState(Var, consumed::CS_Unknown);
944  }
945 }
946 }} // end clang::consumed::ConsumedStmtVisitor
947 
948 namespace clang {
949 namespace consumed {
950 
951 static void splitVarStateForIf(const IfStmt *IfNode, const VarTestResult &Test,
952  ConsumedStateMap *ThenStates,
953  ConsumedStateMap *ElseStates) {
954  ConsumedState VarState = ThenStates->getState(Test.Var);
955 
956  if (VarState == CS_Unknown) {
957  ThenStates->setState(Test.Var, Test.TestsFor);
958  ElseStates->setState(Test.Var, invertConsumedUnconsumed(Test.TestsFor));
959 
960  } else if (VarState == invertConsumedUnconsumed(Test.TestsFor)) {
961  ThenStates->markUnreachable();
962 
963  } else if (VarState == Test.TestsFor) {
964  ElseStates->markUnreachable();
965  }
966 }
967 
968 static void splitVarStateForIfBinOp(const PropagationInfo &PInfo,
969  ConsumedStateMap *ThenStates,
970  ConsumedStateMap *ElseStates) {
971  const VarTestResult &LTest = PInfo.getLTest(),
972  &RTest = PInfo.getRTest();
973 
974  ConsumedState LState = LTest.Var ? ThenStates->getState(LTest.Var) : CS_None,
975  RState = RTest.Var ? ThenStates->getState(RTest.Var) : CS_None;
976 
977  if (LTest.Var) {
978  if (PInfo.testEffectiveOp() == EO_And) {
979  if (LState == CS_Unknown) {
980  ThenStates->setState(LTest.Var, LTest.TestsFor);
981 
982  } else if (LState == invertConsumedUnconsumed(LTest.TestsFor)) {
983  ThenStates->markUnreachable();
984 
985  } else if (LState == LTest.TestsFor && isKnownState(RState)) {
986  if (RState == RTest.TestsFor)
987  ElseStates->markUnreachable();
988  else
989  ThenStates->markUnreachable();
990  }
991 
992  } else {
993  if (LState == CS_Unknown) {
994  ElseStates->setState(LTest.Var,
995  invertConsumedUnconsumed(LTest.TestsFor));
996 
997  } else if (LState == LTest.TestsFor) {
998  ElseStates->markUnreachable();
999 
1000  } else if (LState == invertConsumedUnconsumed(LTest.TestsFor) &&
1001  isKnownState(RState)) {
1002 
1003  if (RState == RTest.TestsFor)
1004  ElseStates->markUnreachable();
1005  else
1006  ThenStates->markUnreachable();
1007  }
1008  }
1009  }
1010 
1011  if (RTest.Var) {
1012  if (PInfo.testEffectiveOp() == EO_And) {
1013  if (RState == CS_Unknown)
1014  ThenStates->setState(RTest.Var, RTest.TestsFor);
1015  else if (RState == invertConsumedUnconsumed(RTest.TestsFor))
1016  ThenStates->markUnreachable();
1017 
1018  } else {
1019  if (RState == CS_Unknown)
1020  ElseStates->setState(RTest.Var,
1021  invertConsumedUnconsumed(RTest.TestsFor));
1022  else if (RState == RTest.TestsFor)
1023  ElseStates->markUnreachable();
1024  }
1025  }
1026 }
1027 
1029  const CFGBlock *TargetBlock) {
1030 
1031  assert(CurrBlock && "Block pointer must not be NULL");
1032  assert(TargetBlock && "TargetBlock pointer must not be NULL");
1033 
1034  unsigned int CurrBlockOrder = VisitOrder[CurrBlock->getBlockID()];
1035  for (CFGBlock::const_pred_iterator PI = TargetBlock->pred_begin(),
1036  PE = TargetBlock->pred_end(); PI != PE; ++PI) {
1037  if (*PI && CurrBlockOrder < VisitOrder[(*PI)->getBlockID()] )
1038  return false;
1039  }
1040  return true;
1041 }
1042 
1044  const CFGBlock *Block, ConsumedStateMap *StateMap,
1045  std::unique_ptr<ConsumedStateMap> &OwnedStateMap) {
1046 
1047  assert(Block && "Block pointer must not be NULL");
1048 
1049  auto &Entry = StateMapsArray[Block->getBlockID()];
1050 
1051  if (Entry) {
1052  Entry->intersect(*StateMap);
1053  } else if (OwnedStateMap)
1054  Entry = std::move(OwnedStateMap);
1055  else
1056  Entry = llvm::make_unique<ConsumedStateMap>(*StateMap);
1057 }
1058 
1060  std::unique_ptr<ConsumedStateMap> StateMap) {
1061 
1062  assert(Block && "Block pointer must not be NULL");
1063 
1064  auto &Entry = StateMapsArray[Block->getBlockID()];
1065 
1066  if (Entry) {
1067  Entry->intersect(*StateMap);
1068  } else {
1069  Entry = std::move(StateMap);
1070  }
1071 }
1072 
1074  assert(Block && "Block pointer must not be NULL");
1075  assert(StateMapsArray[Block->getBlockID()] && "Block has no block info");
1076 
1077  return StateMapsArray[Block->getBlockID()].get();
1078 }
1079 
1081  StateMapsArray[Block->getBlockID()] = nullptr;
1082 }
1083 
1084 std::unique_ptr<ConsumedStateMap>
1086  assert(Block && "Block pointer must not be NULL");
1087 
1088  auto &Entry = StateMapsArray[Block->getBlockID()];
1089  return isBackEdgeTarget(Block) ? llvm::make_unique<ConsumedStateMap>(*Entry)
1090  : std::move(Entry);
1091 }
1092 
1093 bool ConsumedBlockInfo::isBackEdge(const CFGBlock *From, const CFGBlock *To) {
1094  assert(From && "From block must not be NULL");
1095  assert(To && "From block must not be NULL");
1096 
1097  return VisitOrder[From->getBlockID()] > VisitOrder[To->getBlockID()];
1098 }
1099 
1101  assert(Block && "Block pointer must not be NULL");
1102 
1103  // Anything with less than two predecessors can't be the target of a back
1104  // edge.
1105  if (Block->pred_size() < 2)
1106  return false;
1107 
1108  unsigned int BlockVisitOrder = VisitOrder[Block->getBlockID()];
1109  for (CFGBlock::const_pred_iterator PI = Block->pred_begin(),
1110  PE = Block->pred_end(); PI != PE; ++PI) {
1111  if (*PI && BlockVisitOrder < VisitOrder[(*PI)->getBlockID()])
1112  return true;
1113  }
1114  return false;
1115 }
1116 
1118  ConsumedWarningsHandlerBase &WarningsHandler) const {
1119 
1120  for (const auto &DM : VarMap) {
1121  if (isa<ParmVarDecl>(DM.first)) {
1122  const ParmVarDecl *Param = cast<ParmVarDecl>(DM.first);
1123  const ReturnTypestateAttr *RTA = Param->getAttr<ReturnTypestateAttr>();
1124 
1125  if (!RTA)
1126  continue;
1127 
1128  ConsumedState ExpectedState = mapReturnTypestateAttrState(RTA);
1129  if (DM.second != ExpectedState)
1130  WarningsHandler.warnParamReturnTypestateMismatch(BlameLoc,
1131  Param->getNameAsString(), stateToString(ExpectedState),
1132  stateToString(DM.second));
1133  }
1134  }
1135 }
1136 
1138  TmpMap.clear();
1139 }
1140 
1142  VarMapType::const_iterator Entry = VarMap.find(Var);
1143 
1144  if (Entry != VarMap.end())
1145  return Entry->second;
1146 
1147  return CS_None;
1148 }
1149 
1152  TmpMapType::const_iterator Entry = TmpMap.find(Tmp);
1153 
1154  if (Entry != TmpMap.end())
1155  return Entry->second;
1156 
1157  return CS_None;
1158 }
1159 
1161  ConsumedState LocalState;
1162 
1163  if (this->From && this->From == Other.From && !Other.Reachable) {
1164  this->markUnreachable();
1165  return;
1166  }
1167 
1168  for (const auto &DM : Other.VarMap) {
1169  LocalState = this->getState(DM.first);
1170 
1171  if (LocalState == CS_None)
1172  continue;
1173 
1174  if (LocalState != DM.second)
1175  VarMap[DM.first] = CS_Unknown;
1176  }
1177 }
1178 
1180  const CFGBlock *LoopBack, const ConsumedStateMap *LoopBackStates,
1181  ConsumedWarningsHandlerBase &WarningsHandler) {
1182 
1183  ConsumedState LocalState;
1184  SourceLocation BlameLoc = getLastStmtLoc(LoopBack);
1185 
1186  for (const auto &DM : LoopBackStates->VarMap) {
1187  LocalState = this->getState(DM.first);
1188 
1189  if (LocalState == CS_None)
1190  continue;
1191 
1192  if (LocalState != DM.second) {
1193  VarMap[DM.first] = CS_Unknown;
1194  WarningsHandler.warnLoopStateMismatch(BlameLoc,
1195  DM.first->getNameAsString());
1196  }
1197  }
1198 }
1199 
1201  this->Reachable = false;
1202  VarMap.clear();
1203  TmpMap.clear();
1204 }
1205 
1207  VarMap[Var] = State;
1208 }
1209 
1211  ConsumedState State) {
1212  TmpMap[Tmp] = State;
1213 }
1214 
1216  TmpMap.erase(Tmp);
1217 }
1218 
1220  for (const auto &DM : Other->VarMap)
1221  if (this->getState(DM.first) != DM.second)
1222  return true;
1223  return false;
1224 }
1225 
1226 void ConsumedAnalyzer::determineExpectedReturnState(AnalysisDeclContext &AC,
1227  const FunctionDecl *D) {
1228  QualType ReturnType;
1229  if (const CXXConstructorDecl *Constructor = dyn_cast<CXXConstructorDecl>(D)) {
1230  ASTContext &CurrContext = AC.getASTContext();
1231  ReturnType = Constructor->getThisType(CurrContext)->getPointeeType();
1232  } else
1233  ReturnType = D->getCallResultType();
1234 
1235  if (const ReturnTypestateAttr *RTSAttr = D->getAttr<ReturnTypestateAttr>()) {
1236  const CXXRecordDecl *RD = ReturnType->getAsCXXRecordDecl();
1237  if (!RD || !RD->hasAttr<ConsumableAttr>()) {
1238  // FIXME: This should be removed when template instantiation propagates
1239  // attributes at template specialization definition, not
1240  // declaration. When it is removed the test needs to be enabled
1241  // in SemaDeclAttr.cpp.
1242  WarningsHandler.warnReturnTypestateForUnconsumableType(
1243  RTSAttr->getLocation(), ReturnType.getAsString());
1244  ExpectedReturnState = CS_None;
1245  } else
1246  ExpectedReturnState = mapReturnTypestateAttrState(RTSAttr);
1247  } else if (isConsumableType(ReturnType)) {
1248  if (isAutoCastType(ReturnType)) // We can auto-cast the state to the
1249  ExpectedReturnState = CS_None; // expected state.
1250  else
1251  ExpectedReturnState = mapConsumableAttrState(ReturnType);
1252  }
1253  else
1254  ExpectedReturnState = CS_None;
1255 }
1256 
1257 bool ConsumedAnalyzer::splitState(const CFGBlock *CurrBlock,
1258  const ConsumedStmtVisitor &Visitor) {
1259 
1260  std::unique_ptr<ConsumedStateMap> FalseStates(
1261  new ConsumedStateMap(*CurrStates));
1262  PropagationInfo PInfo;
1263 
1264  if (const IfStmt *IfNode =
1265  dyn_cast_or_null<IfStmt>(CurrBlock->getTerminator().getStmt())) {
1266 
1267  const Expr *Cond = IfNode->getCond();
1268 
1269  PInfo = Visitor.getInfo(Cond);
1270  if (!PInfo.isValid() && isa<BinaryOperator>(Cond))
1271  PInfo = Visitor.getInfo(cast<BinaryOperator>(Cond)->getRHS());
1272 
1273  if (PInfo.isVarTest()) {
1274  CurrStates->setSource(Cond);
1275  FalseStates->setSource(Cond);
1276  splitVarStateForIf(IfNode, PInfo.getVarTest(), CurrStates.get(),
1277  FalseStates.get());
1278 
1279  } else if (PInfo.isBinTest()) {
1280  CurrStates->setSource(PInfo.testSourceNode());
1281  FalseStates->setSource(PInfo.testSourceNode());
1282  splitVarStateForIfBinOp(PInfo, CurrStates.get(), FalseStates.get());
1283 
1284  } else {
1285  return false;
1286  }
1287 
1288  } else if (const BinaryOperator *BinOp =
1289  dyn_cast_or_null<BinaryOperator>(CurrBlock->getTerminator().getStmt())) {
1290 
1291  PInfo = Visitor.getInfo(BinOp->getLHS());
1292  if (!PInfo.isVarTest()) {
1293  if ((BinOp = dyn_cast_or_null<BinaryOperator>(BinOp->getLHS()))) {
1294  PInfo = Visitor.getInfo(BinOp->getRHS());
1295 
1296  if (!PInfo.isVarTest())
1297  return false;
1298 
1299  } else {
1300  return false;
1301  }
1302  }
1303 
1304  CurrStates->setSource(BinOp);
1305  FalseStates->setSource(BinOp);
1306 
1307  const VarTestResult &Test = PInfo.getVarTest();
1308  ConsumedState VarState = CurrStates->getState(Test.Var);
1309 
1310  if (BinOp->getOpcode() == BO_LAnd) {
1311  if (VarState == CS_Unknown)
1312  CurrStates->setState(Test.Var, Test.TestsFor);
1313  else if (VarState == invertConsumedUnconsumed(Test.TestsFor))
1314  CurrStates->markUnreachable();
1315 
1316  } else if (BinOp->getOpcode() == BO_LOr) {
1317  if (VarState == CS_Unknown)
1318  FalseStates->setState(Test.Var,
1319  invertConsumedUnconsumed(Test.TestsFor));
1320  else if (VarState == Test.TestsFor)
1321  FalseStates->markUnreachable();
1322  }
1323 
1324  } else {
1325  return false;
1326  }
1327 
1328  CFGBlock::const_succ_iterator SI = CurrBlock->succ_begin();
1329 
1330  if (*SI)
1331  BlockInfo.addInfo(*SI, std::move(CurrStates));
1332  else
1333  CurrStates = nullptr;
1334 
1335  if (*++SI)
1336  BlockInfo.addInfo(*SI, std::move(FalseStates));
1337 
1338  return true;
1339 }
1340 
1342  const FunctionDecl *D = dyn_cast_or_null<FunctionDecl>(AC.getDecl());
1343  if (!D)
1344  return;
1345 
1346  CFG *CFGraph = AC.getCFG();
1347  if (!CFGraph)
1348  return;
1349 
1350  determineExpectedReturnState(AC, D);
1351 
1352  PostOrderCFGView *SortedGraph = AC.getAnalysis<PostOrderCFGView>();
1353  // AC.getCFG()->viewCFG(LangOptions());
1354 
1355  BlockInfo = ConsumedBlockInfo(CFGraph->getNumBlockIDs(), SortedGraph);
1356 
1357  CurrStates = llvm::make_unique<ConsumedStateMap>();
1358  ConsumedStmtVisitor Visitor(AC, *this, CurrStates.get());
1359 
1360  // Add all trackable parameters to the state map.
1361  for (const auto *PI : D->parameters())
1362  Visitor.VisitParmVarDecl(PI);
1363 
1364  // Visit all of the function's basic blocks.
1365  for (const auto *CurrBlock : *SortedGraph) {
1366  if (!CurrStates)
1367  CurrStates = BlockInfo.getInfo(CurrBlock);
1368 
1369  if (!CurrStates) {
1370  continue;
1371 
1372  } else if (!CurrStates->isReachable()) {
1373  CurrStates = nullptr;
1374  continue;
1375  }
1376 
1377  Visitor.reset(CurrStates.get());
1378 
1379  // Visit all of the basic block's statements.
1380  for (const auto &B : *CurrBlock) {
1381  switch (B.getKind()) {
1382  case CFGElement::Statement:
1383  Visitor.Visit(B.castAs<CFGStmt>().getStmt());
1384  break;
1385 
1387  const CFGTemporaryDtor &DTor = B.castAs<CFGTemporaryDtor>();
1388  const CXXBindTemporaryExpr *BTE = DTor.getBindTemporaryExpr();
1389 
1390  Visitor.checkCallability(PropagationInfo(BTE),
1391  DTor.getDestructorDecl(AC.getASTContext()),
1392  BTE->getExprLoc());
1393  CurrStates->remove(BTE);
1394  break;
1395  }
1396 
1398  const CFGAutomaticObjDtor &DTor = B.castAs<CFGAutomaticObjDtor>();
1399  SourceLocation Loc = DTor.getTriggerStmt()->getLocEnd();
1400  const VarDecl *Var = DTor.getVarDecl();
1401 
1402  Visitor.checkCallability(PropagationInfo(Var),
1403  DTor.getDestructorDecl(AC.getASTContext()),
1404  Loc);
1405  break;
1406  }
1407 
1408  default:
1409  break;
1410  }
1411  }
1412 
1413  // TODO: Handle other forms of branching with precision, including while-
1414  // and for-loops. (Deferred)
1415  if (!splitState(CurrBlock, Visitor)) {
1416  CurrStates->setSource(nullptr);
1417 
1418  if (CurrBlock->succ_size() > 1 ||
1419  (CurrBlock->succ_size() == 1 &&
1420  (*CurrBlock->succ_begin())->pred_size() > 1)) {
1421 
1422  auto *RawState = CurrStates.get();
1423 
1424  for (CFGBlock::const_succ_iterator SI = CurrBlock->succ_begin(),
1425  SE = CurrBlock->succ_end(); SI != SE; ++SI) {
1426 
1427  if (*SI == nullptr) continue;
1428 
1429  if (BlockInfo.isBackEdge(CurrBlock, *SI)) {
1430  BlockInfo.borrowInfo(*SI)->intersectAtLoopHead(
1431  *SI, CurrBlock, RawState, WarningsHandler);
1432 
1433  if (BlockInfo.allBackEdgesVisited(CurrBlock, *SI))
1434  BlockInfo.discardInfo(*SI);
1435  } else {
1436  BlockInfo.addInfo(*SI, RawState, CurrStates);
1437  }
1438  }
1439 
1440  CurrStates = nullptr;
1441  }
1442  }
1443 
1444  if (CurrBlock == &AC.getCFG()->getExit() &&
1445  D->getCallResultType()->isVoidType())
1446  CurrStates->checkParamsForReturnTypestate(D->getLocation(),
1447  WarningsHandler);
1448  } // End of block iterator.
1449 
1450  // Delete the last existing state map.
1451  CurrStates = nullptr;
1452 
1453  WarningsHandler.emitDiagnostics();
1454 }
1455 }} // end namespace clang::consumed
A call to an overloaded operator written using operator syntax.
Definition: ExprCXX.h:52
Defines the clang::ASTContext interface.
bool isCallToStdMove() const
Definition: Expr.h:2351
FunctionDecl - An instance of this class is created to represent a function declaration or definition...
Definition: Decl.h:1631
const ConsumedState & getState() const
Definition: Consumed.cpp:355
bool operator!=(const ConsumedStateMap *Other) const
Tests to see if there is a mismatch in the states stored in two maps.
Definition: Consumed.cpp:1219
pred_iterator pred_end()
Definition: CFG.h:584
A (possibly-)qualified type.
Definition: Type.h:614
Expr * getArg(unsigned Arg)
getArg - Return the specified argument.
Definition: Expr.h:2278
const Stmt * getStmt() const
Definition: CFG.h:122
ConstStmtVisitor - This class implements a simple visitor for Stmt subclasses.
Definition: StmtVisitor.h:189
void VisitCastExpr(const CastExpr *Cast)
Definition: Consumed.cpp:761
succ_iterator succ_begin()
Definition: CFG.h:600
Stmt - This represents one statement.
Definition: Stmt.h:60
unsigned getNumArgs() const
getNumArgs - Return the number of actual arguments to this call.
Definition: Expr.h:2266
IfStmt - This represents an if/then/else.
Definition: Stmt.h:905
QualType getPointeeType() const
If this is a pointer, ObjC object pointer, or block pointer, this returns the respective pointee...
Definition: Type.cpp:435
C Language Family Type Representation.
void addInfo(const CFGBlock *Block, ConsumedStateMap *StateMap, std::unique_ptr< ConsumedStateMap > &OwnedStateMap)
Definition: Consumed.cpp:1043
Expr * getBase() const
Definition: Expr.h:2477
unsigned getBlockID() const
Definition: CFG.h:704
Expr * getImplicitObjectArgument() const
Retrieves the implicit object argument for the member call.
Definition: ExprCXX.cpp:491
virtual void warnParamReturnTypestateMismatch(SourceLocation Loc, StringRef VariableName, StringRef ExpectedState, StringRef ObservedState)
Warn about parameter typestate mismatches upon return.
Definition: Consumed.h:71
Opcode getOpcode() const
Definition: Expr.h:3027
void VisitUnaryOperator(const UnaryOperator *UOp)
Definition: Consumed.cpp:908
bool handleCall(const CallExpr *Call, const Expr *ObjArg, const FunctionDecl *FunD)
Definition: Consumed.cpp:611
Represents a call to a C++ constructor.
Definition: ExprCXX.h:1177
void run(AnalysisDeclContext &AC)
Check a function&#39;s CFG for consumed violations.
Definition: Consumed.cpp:1341
QualType getCallResultType() const
Determine the type of an expression that calls this function.
Definition: Decl.h:2137
T castAs() const
Convert to the specified CFGElement type, asserting that this CFGElement is of the desired type...
Definition: CFG.h:90
SourceLocation getLocStart() const LLVM_READONLY
Definition: Stmt.h:1422
Represents a C++ constructor within a class.
Definition: DeclCXX.h:2344
Represents a prvalue temporary that is written into memory so that a reference can bind to it...
Definition: ExprCXX.h:3951
static ConsumedState testsFor(const FunctionDecl *FunDecl)
Definition: Consumed.cpp:261
unsigned succ_size() const
Definition: CFG.h:617
const CXXDestructorDecl * getDestructorDecl(ASTContext &astContext) const
Definition: CFG.cpp:4173
bool isDefaultConstructor() const
Whether this constructor is a default constructor (C++ [class.ctor]p5), which can be used to default-...
Definition: DeclCXX.cpp:2077
void setState(const VarDecl *Var, ConsumedState State)
Set the consumed state of a given variable.
Definition: Consumed.cpp:1206
VarDecl - An instance of this class is created to represent a variable declaration or definition...
Definition: Decl.h:771
void VisitCXXMemberCallExpr(const CXXMemberCallExpr *Call)
Definition: Consumed.cpp:809
ASTContext & getASTContext() const
Expr * IgnoreImplicit() LLVM_READONLY
IgnoreImplicit - Skip past any implicit AST nodes which might surround this expression.
Definition: Expr.h:734
const CXXBindTemporaryExpr * getTmp() const
Definition: Consumed.cpp:380
const Stmt * getTriggerStmt() const
Definition: CFG.h:252
void remove(const CXXBindTemporaryExpr *Tmp)
Remove the temporary value from our state map.
Definition: Consumed.cpp:1215
ParmVarDecl - Represents a parameter to a function.
Definition: Decl.h:1447
Defines the clang::Expr interface and subclasses for C++ expressions.
PropagationInfo(const VarTestResult &VarTest)
Definition: Consumed.cpp:315
PropagationInfo(const VarDecl *Var)
Definition: Consumed.cpp:351
void intersectAtLoopHead(const CFGBlock *LoopHead, const CFGBlock *LoopBack, const ConsumedStateMap *LoopBackStates, ConsumedWarningsHandlerBase &WarningsHandler)
Definition: Consumed.cpp:1179
Expr * GetTemporaryExpr() const
Retrieve the temporary-generating subexpression whose value will be materialized into a glvalue...
Definition: ExprCXX.h:3992
Holds long-lived AST nodes (such as types and decls) that can be referred to throughout the semantic ...
Definition: ASTContext.h:128
LineState State
ConsumedStmtVisitor(AnalysisDeclContext &AC, ConsumedAnalyzer &Analyzer, ConsumedStateMap *StateMap)
Definition: Consumed.cpp:510
AnalysisDeclContext contains the context data for the function or method under analysis.
CFGAutomaticObjDtor - Represents C++ object destructor implicitly generated for automatic object or t...
Definition: CFG.h:242
const Expr * getRetValue() const
Definition: Stmt.cpp:905
const BinaryOperator * testSourceNode() const
Definition: Consumed.cpp:403
const VarTestResult & getRTest() const
Definition: Consumed.cpp:370
bool isReferenceType() const
Definition: Type.h:5775
PropagationInfo(const BinaryOperator *Source, EffectiveOp EOp, const VarDecl *LVar, ConsumedState LTestsFor, const VarDecl *RVar, ConsumedState RTestsFor)
Definition: Consumed.cpp:335
static void splitVarStateForIf(const IfStmt *IfNode, const VarTestResult &Test, ConsumedStateMap *ThenStates, ConsumedStateMap *ElseStates)
Definition: Consumed.cpp:951
PropagationInfo(const CXXBindTemporaryExpr *Tmp)
Definition: Consumed.cpp:352
static ConsumedState mapReturnTypestateAttrState(const ReturnTypestateAttr *RTSAttr)
Definition: Consumed.cpp:220
Expr * getSubExpr()
Definition: Expr.h:2762
void markUnreachable()
Mark the block as unreachable.
Definition: Consumed.cpp:1200
ArrayRef< ParmVarDecl * > parameters() const
Definition: Decl.h:2082
bool allBackEdgesVisited(const CFGBlock *CurrBlock, const CFGBlock *TargetBlock)
Definition: Consumed.cpp:1028
QualType getThisType(ASTContext &C) const
Returns the type of the this pointer.
Definition: DeclCXX.cpp:1913
EffectiveOp testEffectiveOp() const
Definition: Consumed.cpp:398
uint32_t Offset
Definition: CacheTokens.cpp:43
const CXXBindTemporaryExpr * Tmp
Definition: Consumed.cpp:308
const VarTestResult & getVarTest() const
Definition: Consumed.cpp:360
T * getAnalysis()
Return the specified analysis object, lazily running the analysis if necessary.
void VisitCXXBindTemporaryExpr(const CXXBindTemporaryExpr *Temp)
Definition: Consumed.cpp:765
A builtin binary operation expression such as "x + y" or "x <= y".
Definition: Expr.h:2986
std::string getAsString() const
Definition: Type.h:940
bool isRValueReferenceType() const
Definition: Type.h:5781
std::unique_ptr< ConsumedStateMap > getInfo(const CFGBlock *Block)
Definition: Consumed.cpp:1085
const VarDecl * getVarDecl() const
Definition: CFG.h:247
CastExpr - Base class for type casts, including both implicit casts (ImplicitCastExpr) and explicit c...
Definition: Expr.h:2710
Represents binding an expression to a temporary.
Definition: ExprCXX.h:1134
reverse_iterator rend()
Definition: CFG.h:559
static bool isConsumableType(const QualType &QT)
Definition: Consumed.cpp:138
void checkParamsForReturnTypestate(SourceLocation BlameLoc, ConsumedWarningsHandlerBase &WarningsHandler) const
Warn if any of the parameters being tracked are not in the state they were declared to be in upon ret...
Definition: Consumed.cpp:1117
bool hasAttr() const
Definition: DeclBase.h:521
CXXRecordDecl * getAsCXXRecordDecl() const
Retrieves the CXXRecordDecl that this type refers to, either because the type is a RecordType or beca...
Definition: Type.cpp:1569
static void splitVarStateForIfBinOp(const PropagationInfo &PInfo, ConsumedStateMap *ThenStates, ConsumedStateMap *ElseStates)
Definition: Consumed.cpp:968
CXXConstructorDecl * getConstructor() const
Get the constructor that this expression will (ultimately) call.
Definition: ExprCXX.h:1240
SourceLocation getLocEnd() const LLVM_READONLY
Definition: Stmt.cpp:270
const CXXBindTemporaryExpr * getBindTemporaryExpr() const
Definition: CFG.h:333
CFGBlock - Represents a single basic block in a source-level CFG.
Definition: CFG.h:401
virtual void warnLoopStateMismatch(SourceLocation Loc, StringRef VariableName)
Warn that a variable&#39;s state doesn&#39;t match at the entry and exit of a loop.
Definition: Consumed.h:59
static bool isPointerOrRef(QualType ParamType)
Definition: Consumed.cpp:185
bool isBackEdge(const CFGBlock *From, const CFGBlock *To)
Definition: Consumed.cpp:1093
void clearTemporaries()
Clear the TmpMap.
Definition: Consumed.cpp:1137
Expr - This represents one expression.
Definition: Expr.h:106
static StringRef stateToString(ConsumedState State)
Definition: Consumed.cpp:244
CFG - Represents a source-level, intra-procedural CFG that represents the control-flow of a Stmt...
Definition: CFG.h:808
const CXXRecordDecl * getPointeeCXXRecordDecl() const
If this is a pointer or reference to a RecordType, return the CXXRecordDecl that that type refers to...
Definition: Type.cpp:1554
const VarDecl * getVar() const
Definition: Consumed.cpp:375
Defines an enumeration for C++ overloaded operators.
void VisitDeclRefExpr(const DeclRefExpr *DeclRef)
Definition: Consumed.cpp:842
PropagationInfo(ConsumedState State)
Definition: Consumed.cpp:348
AdjacentBlocks::const_iterator const_pred_iterator
Definition: CFG.h:570
ReturnStmt - This represents a return, optionally of an expression: return; return 4;...
Definition: Stmt.h:1392
UnaryOperator - This represents the unary-expression&#39;s (except sizeof and alignof), the postinc/postdec operators from postfix-expression, and various extensions.
Definition: Expr.h:1717
CXXMethodDecl * getMethodDecl() const
Retrieves the declaration of the called method.
Definition: ExprCXX.cpp:503
ValueDecl * getDecl()
Definition: Expr.h:1041
const Expr * getSubExpr() const
Definition: ExprCXX.h:1158
void checkCallability(const PropagationInfo &PInfo, const FunctionDecl *FunDecl, SourceLocation BlameLoc)
Definition: Consumed.cpp:577
reverse_iterator rbegin()
Definition: CFG.h:558
void VisitVarDecl(const VarDecl *Var)
Definition: Consumed.cpp:928
void VisitReturnStmt(const ReturnStmt *Ret)
Definition: Consumed.cpp:888
void VisitDeclStmt(const DeclStmt *DelcS)
Definition: Consumed.cpp:848
bool isConstQualified() const
Determine whether this type is const-qualified.
Definition: Type.h:5602
static ConsumedState mapConsumableAttrState(const QualType QT)
Definition: Consumed.cpp:189
PropagationInfo invertTest() const
Definition: Consumed.cpp:423
CFGTerminator getTerminator()
Definition: CFG.h:688
Encodes a location in the source.
Expr * getSubExpr() const
Definition: Expr.h:1744
std::string getNameAsString() const
getNameAsString - Get a human-readable name for the declaration, even if it is one of the special kin...
Definition: Decl.h:252
Represents a call to a member function that may be written either with member call syntax (e...
Definition: ExprCXX.h:136
DeclStmt - Adaptor class for mixing declarations with statements and expressions. ...
Definition: Stmt.h:467
Represents a static or instance method of a struct/union/class.
Definition: DeclCXX.h:1918
static bool isTestingFunction(const FunctionDecl *FunDecl)
Definition: Consumed.cpp:181
static bool isKnownState(ConsumedState State)
Definition: Consumed.cpp:165
const ParmVarDecl * getParamDecl(unsigned i) const
Definition: Decl.h:2104
PropagationInfo(const BinaryOperator *Source, EffectiveOp EOp, const VarTestResult &LTest, const VarTestResult &RTest)
Definition: Consumed.cpp:325
const Decl * getDecl() const
void VisitMemberExpr(const MemberExpr *MExpr)
Definition: Consumed.cpp:864
static bool isSetOnReadPtrType(const QualType &QT)
Definition: Consumed.cpp:158
unsigned getNumBlockIDs() const
getNumBlockIDs - Returns the total number of BlockIDs allocated (which start at 0).
Definition: CFG.h:975
void VisitCXXOperatorCallExpr(const CXXOperatorCallExpr *Call)
Definition: Consumed.cpp:820
const VarTestResult & getLTest() const
Definition: Consumed.cpp:365
AdjacentBlocks::const_iterator const_succ_iterator
Definition: CFG.h:577
SourceLocation getExprLoc() const LLVM_READONLY
getExprLoc - Return the preferred location for the arrow when diagnosing a problem with a generic exp...
Definition: Expr.cpp:216
Expr * getLHS() const
Definition: Expr.h:3030
pred_iterator pred_begin()
Definition: CFG.h:583
void reset(ConsumedStateMap *NewStateMap)
Definition: Consumed.cpp:523
Dataflow Directional Tag Classes.
bool isValid() const
Return true if this is a valid SourceLocation object.
OverloadedOperatorKind getOperator() const
Returns the kind of overloaded operator that this expression refers to.
Definition: ExprCXX.h:75
FunctionDecl * getDirectCallee()
If the callee is a FunctionDecl, return it. Otherwise return 0.
Definition: Expr.cpp:1216
static void setStateForVarOrTmp(ConsumedStateMap *StateMap, const PropagationInfo &PInfo, ConsumedState State)
Definition: Consumed.cpp:442
bool isCopyConstructor(unsigned &TypeQuals) const
Whether this constructor is a copy constructor (C++ [class.copy]p2, which can be used to copy the cla...
Definition: DeclCXX.cpp:2086
const Expr * getInit() const
Definition: Decl.h:1159
unsigned pred_size() const
Definition: CFG.h:620
bool isMoveConstructor(unsigned &TypeQuals) const
Determine whether this constructor is a move constructor (C++11 [class.copy]p3), which can be used to...
Definition: DeclCXX.cpp:2091
const Decl * getSingleDecl() const
Definition: Stmt.h:485
A class that handles the analysis of uniqueness violations.
Definition: Consumed.h:233
bool isSingleDecl() const
isSingleDecl - This method returns true if this DeclStmt refers to a single Decl. ...
Definition: Stmt.h:481
Stmt * getStmt()
Definition: CFG.h:358
static bool isRValueRef(QualType ParamType)
Definition: Consumed.cpp:177
static const TypeInfo & getInfo(unsigned id)
Definition: Types.cpp:34
PropagationInfo getInfo(const Expr *StmtNode) const
Definition: Consumed.cpp:514
void VisitParmVarDecl(const ParmVarDecl *Param)
Definition: Consumed.cpp:869
ConsumedStateMap * borrowInfo(const CFGBlock *Block)
Definition: Consumed.cpp:1073
T * getAttr() const
Definition: DeclBase.h:518
PropagationInfo(const VarDecl *Var, ConsumedState TestsFor)
Definition: Consumed.cpp:318
Opcode getOpcode() const
Definition: Expr.h:1741
Expr * getArg(unsigned Arg)
Return the specified argument.
Definition: ExprCXX.h:1303
decl_range decls()
Definition: Stmt.h:515
void discardInfo(const CFGBlock *Block)
Definition: Consumed.cpp:1080
ConsumedState getAsState(const ConsumedStateMap *StateMap) const
Definition: Consumed.cpp:385
Defines the C++ Decl subclasses, other than those for templates (found in DeclTemplate.h) and friends (in DeclFriend.h).
static SourceLocation getFirstStmtLoc(const CFGBlock *Block)
Definition: Consumed.cpp:54
MemberExpr - [C99 6.5.2.3] Structure and Union Members.
Definition: Expr.h:2387
Defines the clang::SourceLocation class and associated facilities.
void VisitCallExpr(const CallExpr *Call)
Definition: Consumed.cpp:745
void VisitMaterializeTemporaryExpr(const MaterializeTemporaryExpr *Temp)
Definition: Consumed.cpp:858
Represents a C++ struct/union/class.
Definition: DeclCXX.h:266
bool isVoidType() const
Definition: Type.h:5963
static bool isAutoCastType(const QualType &QT)
Definition: Consumed.cpp:148
void VisitBinaryOperator(const BinaryOperator *BinOp)
Definition: Consumed.cpp:703
CFGElement - Represents a top-level expression in a basic block.
Definition: CFG.h:54
bool isBackEdgeTarget(const CFGBlock *Block)
Definition: Consumed.cpp:1100
CallExpr - Represents a function call (C99 6.5.2.2, C++ [expr.call]).
Definition: Expr.h:2209
RetTy Visit(PTR(Stmt) S, ParamTys... P)
Definition: StmtVisitor.h:42
static ConsumedState mapParamTypestateAttrState(const ParamTypestateAttr *PTAttr)
Definition: Consumed.cpp:207
ConsumedState getState(const VarDecl *Var) const
Get the consumed state of a given variable.
Definition: Consumed.cpp:1141
static bool isCallableInState(const CallableWhenAttr *CWAttr, ConsumedState State)
Definition: Consumed.cpp:110
A reference to a declared variable, function, enum, etc.
Definition: Expr.h:956
void VisitCXXConstructExpr(const CXXConstructExpr *Call)
Definition: Consumed.cpp:776
Expr * getRHS() const
Definition: Expr.h:3032
bool isPointerType() const
Definition: Type.h:5766
static SourceLocation getLastStmtLoc(const CFGBlock *Block)
Definition: Consumed.cpp:69
QualType getType() const
Definition: Decl.h:602
static ConsumedState mapSetTypestateAttrState(const SetTypestateAttr *STAttr)
Definition: Consumed.cpp:232
unsigned getNumParams() const
getNumParams - Return the number of parameters this function must have based on its FunctionType...
Definition: Decl.cpp:2877
bool hasInit() const
Definition: Decl.cpp:2086
CFGTemporaryDtor - Represents C++ object destructor implicitly generated at the end of full expressio...
Definition: CFG.h:328
SourceLocation getReturnLoc() const
Definition: Stmt.h:1411
SourceLocation getLocation() const
Definition: DeclBase.h:407
static ConsumedState invertConsumedUnconsumed(ConsumedState State)
Definition: Consumed.cpp:96
Expr * IgnoreParens() LLVM_READONLY
IgnoreParens - Ignore parentheses.
Definition: Expr.cpp:2431
void intersect(const ConsumedStateMap &Other)
Merge this state map with another map.
Definition: Consumed.cpp:1160
CFGBlock & getExit()
Definition: CFG.h:902