r189616 - Consumed analysis: track function parameters.

DeLesley Hutchins delesley at google.com
Thu Aug 29 15:36:05 PDT 2013


Author: delesley
Date: Thu Aug 29 17:36:05 2013
New Revision: 189616

URL: http://llvm.org/viewvc/llvm-project?rev=189616&view=rev
Log:
Consumed analysis: track function parameters.
Patch by chris.wailes at gmail.com.

Modified:
    cfe/trunk/lib/Analysis/Consumed.cpp
    cfe/trunk/test/SemaCXX/warn-consumed-analysis-strict.cpp
    cfe/trunk/test/SemaCXX/warn-consumed-analysis.cpp

Modified: cfe/trunk/lib/Analysis/Consumed.cpp
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/lib/Analysis/Consumed.cpp?rev=189616&r1=189615&r2=189616&view=diff
==============================================================================
--- cfe/trunk/lib/Analysis/Consumed.cpp (original)
+++ cfe/trunk/lib/Analysis/Consumed.cpp Thu Aug 29 17:36:05 2013
@@ -44,8 +44,6 @@
 //       (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;
@@ -266,11 +264,13 @@ public:
   void VisitDeclStmt(const DeclStmt *DelcS);
   void VisitMaterializeTemporaryExpr(const MaterializeTemporaryExpr *Temp);
   void VisitMemberExpr(const MemberExpr *MExpr);
+  void VisitParmVarDecl(const ParmVarDecl *Param);
   void VisitUnaryOperator(const UnaryOperator *UOp);
   void VisitVarDecl(const VarDecl *Var);
 
-  ConsumedStmtVisitor(AnalysisDeclContext &AC, ConsumedAnalyzer &Analyzer)
-      : AC(AC), Analyzer(Analyzer), StateMap(NULL) {}
+  ConsumedStmtVisitor(AnalysisDeclContext &AC, ConsumedAnalyzer &Analyzer,
+                      ConsumedStateMap *StateMap)
+      : AC(AC), Analyzer(Analyzer), StateMap(StateMap) {}
   
   PropagationInfo getInfo(const Stmt *StmtNode) const {
     ConstInfoEntry Entry = PropagationMap.find(StmtNode);
@@ -664,6 +664,12 @@ void ConsumedStmtVisitor::VisitMemberExp
   forwardInfo(MExpr->getBase(), MExpr);
 }
 
+
+void ConsumedStmtVisitor::VisitParmVarDecl(const ParmVarDecl *Param) {
+  if (Analyzer.isConsumableType(Param->getType()))
+    StateMap->setState(Param, consumed::CS_Unknown);
+}
+
 void ConsumedStmtVisitor::VisitUnaryOperator(const UnaryOperator *UOp) {
   InfoEntry Entry = PropagationMap.find(UOp->getSubExpr()->IgnoreParens());
   if (Entry == PropagationMap.end()) return;
@@ -685,11 +691,16 @@ void ConsumedStmtVisitor::VisitUnaryOper
 
 void ConsumedStmtVisitor::VisitVarDecl(const VarDecl *Var) {
   if (Analyzer.isConsumableType(Var->getType())) {
-    PropagationInfo PInfo =
-      PropagationMap.find(Var->getInit())->second;
-    
-    StateMap->setState(Var, PInfo.isVar() ?
-      StateMap->getState(PInfo.getVar()) : PInfo.getState());
+    if (Var->hasInit()) {
+      PropagationInfo PInfo =
+        PropagationMap.find(Var->getInit())->second;
+      
+      StateMap->setState(Var, PInfo.isVar() ?
+        StateMap->getState(PInfo.getVar()) : PInfo.getState());
+      
+    } else {
+      StateMap->setState(Var, consumed::CS_Unknown);
+    }
   }
 }
 }} // end clang::consumed::ConsumedStmtVisitor
@@ -1022,7 +1033,13 @@ void ConsumedAnalyzer::run(AnalysisDeclC
   PostOrderCFGView *SortedGraph = AC.getAnalysis<PostOrderCFGView>();
   
   CurrStates = new ConsumedStateMap();
-  ConsumedStmtVisitor Visitor(AC, *this);
+  ConsumedStmtVisitor Visitor(AC, *this, CurrStates);
+  
+  // Add all trackable parameters to the state map.
+  for (FunctionDecl::param_const_iterator PI = D->param_begin(),
+       PE = D->param_end(); PI != PE; ++PI) {
+    Visitor.VisitParmVarDecl(*PI);
+  }
   
   // Visit all of the function's basic blocks.
   for (PostOrderCFGView::iterator I = SortedGraph->begin(),

Modified: cfe/trunk/test/SemaCXX/warn-consumed-analysis-strict.cpp
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/test/SemaCXX/warn-consumed-analysis-strict.cpp?rev=189616&r1=189615&r2=189616&view=diff
==============================================================================
--- cfe/trunk/test/SemaCXX/warn-consumed-analysis-strict.cpp (original)
+++ cfe/trunk/test/SemaCXX/warn-consumed-analysis-strict.cpp Thu Aug 29 17:36:05 2013
@@ -192,6 +192,10 @@ void testNoWarnTestFromMacroExpansion()
   }
 }
 
+void testFunctionParam(ConsumableClass<int> param) {
+  *param; // expected-warning {{invocation of method 'operator*' on object 'param' while it is in an unknown state}}
+}
+
 void testSimpleForLoop() {
   ConsumableClass<int> var;
   

Modified: cfe/trunk/test/SemaCXX/warn-consumed-analysis.cpp
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/test/SemaCXX/warn-consumed-analysis.cpp?rev=189616&r1=189615&r2=189616&view=diff
==============================================================================
--- cfe/trunk/test/SemaCXX/warn-consumed-analysis.cpp (original)
+++ cfe/trunk/test/SemaCXX/warn-consumed-analysis.cpp Thu Aug 29 17:36:05 2013
@@ -224,6 +224,18 @@ void testStateChangeInBranch() {
   *var;
 }
 
+void testFunctionParam(ConsumableClass<int> param) {
+  
+  if (param.isValid()) {
+    *param;
+  } else {
+    *param; // expected-warning {{invocation of method 'operator*' on object 'param' while it is in the 'consumed' state}}
+  }
+  
+  param = nullptr;
+  *param; // expected-warning {{invocation of method 'operator*' on object 'param' while it is in the 'consumed' state}}
+}
+
 void testCallingConventions() {
   ConsumableClass<int> var(42);
   





More information about the cfe-commits mailing list