blob: c8b491a21682cc07ecaa0462dc007a7d25fe4a3e [file] [log] [blame]
Tom Caredb34ab72010-08-23 19:51:57 +00001//== PseudoConstantAnalysis.cpp - Find Pseudoconstants in the AST-*- C++ -*-==//
Tom Care245adab2010-08-18 21:17:24 +00002//
3// The LLVM Compiler Infrastructure
4//
5// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
7//
8//===----------------------------------------------------------------------===//
9//
10// This file tracks the usage of variables in a Decl body to see if they are
11// never written to, implying that they constant. This is useful in static
12// analysis to see if a developer might have intended a variable to be const.
13//
14//===----------------------------------------------------------------------===//
15
Tom Caredb34ab72010-08-23 19:51:57 +000016#include "clang/Analysis/Analyses/PseudoConstantAnalysis.h"
Tom Care245adab2010-08-18 21:17:24 +000017#include "clang/AST/Decl.h"
18#include "clang/AST/Expr.h"
19#include "clang/AST/Stmt.h"
20#include <deque>
21
22using namespace clang;
23
Tom Caredb34ab72010-08-23 19:51:57 +000024// The number of ValueDecls we want to keep track of by default (per-function)
25#define VARDECL_SET_SIZE 256
26typedef llvm::SmallPtrSet<const VarDecl*, VARDECL_SET_SIZE> VarDeclSet;
27
28PseudoConstantAnalysis::PseudoConstantAnalysis(const Stmt *DeclBody) :
29 DeclBody(DeclBody), Analyzed(false) {
30 NonConstantsImpl = new VarDeclSet;
Tom Careef52bcb2010-08-24 21:09:07 +000031 UsedVarsImpl = new VarDeclSet;
Tom Caredb34ab72010-08-23 19:51:57 +000032}
33
34PseudoConstantAnalysis::~PseudoConstantAnalysis() {
35 delete (VarDeclSet*)NonConstantsImpl;
Tom Careef52bcb2010-08-24 21:09:07 +000036 delete (VarDeclSet*)UsedVarsImpl;
Tom Caredb34ab72010-08-23 19:51:57 +000037}
38
Tom Care245adab2010-08-18 21:17:24 +000039// Returns true if the given ValueDecl is never written to in the given DeclBody
Tom Caredb34ab72010-08-23 19:51:57 +000040bool PseudoConstantAnalysis::isPseudoConstant(const VarDecl *VD) {
41 // Only local and static variables can be pseudoconstants
42 if (!VD->hasLocalStorage() && !VD->isStaticLocal())
43 return false;
44
Tom Care245adab2010-08-18 21:17:24 +000045 if (!Analyzed) {
46 RunAnalysis();
47 Analyzed = true;
48 }
49
Tom Caredb34ab72010-08-23 19:51:57 +000050 VarDeclSet *NonConstants = (VarDeclSet*)NonConstantsImpl;
51
52 return !NonConstants->count(VD);
Tom Care245adab2010-08-18 21:17:24 +000053}
54
Tom Careef52bcb2010-08-24 21:09:07 +000055// Returns true if the variable was used (self assignments don't count)
56bool PseudoConstantAnalysis::wasReferenced(const VarDecl *VD) {
57 if (!Analyzed) {
58 RunAnalysis();
59 Analyzed = true;
60 }
61
62 VarDeclSet *UsedVars = (VarDeclSet*)UsedVarsImpl;
63
64 return UsedVars->count(VD);
65}
66
Tom Care967fea62010-08-25 22:37:26 +000067// Returns a Decl from a (Block)DeclRefExpr (if any)
68const Decl *PseudoConstantAnalysis::getDecl(const Expr *E) {
69 if (const DeclRefExpr *DR = dyn_cast<DeclRefExpr>(E))
70 return DR->getDecl();
Tom Care967fea62010-08-25 22:37:26 +000071 else
72 return 0;
73}
74
Tom Caredb34ab72010-08-23 19:51:57 +000075void PseudoConstantAnalysis::RunAnalysis() {
Tom Care245adab2010-08-18 21:17:24 +000076 std::deque<const Stmt *> WorkList;
Tom Caredb34ab72010-08-23 19:51:57 +000077 VarDeclSet *NonConstants = (VarDeclSet*)NonConstantsImpl;
Tom Careef52bcb2010-08-24 21:09:07 +000078 VarDeclSet *UsedVars = (VarDeclSet*)UsedVarsImpl;
Tom Care245adab2010-08-18 21:17:24 +000079
80 // Start with the top level statement of the function
81 WorkList.push_back(DeclBody);
82
83 while (!WorkList.empty()) {
Ted Kremenek9c378f72011-08-12 23:37:29 +000084 const Stmt *Head = WorkList.front();
Tom Care245adab2010-08-18 21:17:24 +000085 WorkList.pop_front();
86
Ted Kremenek892697d2010-12-16 07:46:53 +000087 if (const Expr *Ex = dyn_cast<Expr>(Head))
88 Head = Ex->IgnoreParenCasts();
89
Tom Care245adab2010-08-18 21:17:24 +000090 switch (Head->getStmtClass()) {
Tom Care967fea62010-08-25 22:37:26 +000091 // Case 1: Assignment operators modifying VarDecls
Tom Care245adab2010-08-18 21:17:24 +000092 case Stmt::BinaryOperatorClass: {
93 const BinaryOperator *BO = cast<BinaryOperator>(Head);
Tom Care967fea62010-08-25 22:37:26 +000094 // Look for a Decl on the LHS
95 const Decl *LHSDecl = getDecl(BO->getLHS()->IgnoreParenCasts());
Tom Care967fea62010-08-25 22:37:26 +000096 if (!LHSDecl)
Tom Care245adab2010-08-18 21:17:24 +000097 break;
98
99 // We found a binary operator with a DeclRefExpr on the LHS. We now check
100 // for any of the assignment operators, implying that this Decl is being
101 // written to.
102 switch (BO->getOpcode()) {
Tom Care967fea62010-08-25 22:37:26 +0000103 // Self-assignments don't count as use of a variable
John McCall2de56d12010-08-25 11:45:40 +0000104 case BO_Assign: {
Tom Care967fea62010-08-25 22:37:26 +0000105 // Look for a DeclRef on the RHS
106 const Decl *RHSDecl = getDecl(BO->getRHS()->IgnoreParenCasts());
107
108 // If the Decls match, we have self-assignment
109 if (LHSDecl == RHSDecl)
110 // Do not visit the children
111 continue;
Tom Careef52bcb2010-08-24 21:09:07 +0000112
113 }
John McCall2de56d12010-08-25 11:45:40 +0000114 case BO_AddAssign:
115 case BO_SubAssign:
116 case BO_MulAssign:
117 case BO_DivAssign:
118 case BO_AndAssign:
119 case BO_OrAssign:
120 case BO_XorAssign:
121 case BO_ShlAssign:
122 case BO_ShrAssign: {
Tom Care967fea62010-08-25 22:37:26 +0000123 const VarDecl *VD = dyn_cast<VarDecl>(LHSDecl);
Tom Care245adab2010-08-18 21:17:24 +0000124 // The DeclRefExpr is being assigned to - mark it as non-constant
Tom Caredb34ab72010-08-23 19:51:57 +0000125 if (VD)
126 NonConstants->insert(VD);
127 break;
128 }
Tom Care245adab2010-08-18 21:17:24 +0000129
130 default:
131 break;
132 }
133 break;
134 }
135
136 // Case 2: Pre/post increment/decrement and address of
137 case Stmt::UnaryOperatorClass: {
138 const UnaryOperator *UO = cast<UnaryOperator>(Head);
Tom Care245adab2010-08-18 21:17:24 +0000139
Tom Care967fea62010-08-25 22:37:26 +0000140 // Look for a DeclRef in the subexpression
141 const Decl *D = getDecl(UO->getSubExpr()->IgnoreParenCasts());
Tom Care916d0542010-08-25 22:46:03 +0000142 if (!D)
143 break;
Tom Care245adab2010-08-18 21:17:24 +0000144
Tom Care967fea62010-08-25 22:37:26 +0000145 // We found a unary operator with a DeclRef as a subexpression. We now
Tom Care245adab2010-08-18 21:17:24 +0000146 // check for any of the increment/decrement operators, as well as
147 // addressOf.
148 switch (UO->getOpcode()) {
John McCall2de56d12010-08-25 11:45:40 +0000149 case UO_PostDec:
150 case UO_PostInc:
151 case UO_PreDec:
152 case UO_PreInc:
Tom Care967fea62010-08-25 22:37:26 +0000153 // The DeclRef is being changed - mark it as non-constant
John McCall2de56d12010-08-25 11:45:40 +0000154 case UO_AddrOf: {
Tom Care245adab2010-08-18 21:17:24 +0000155 // If we are taking the address of the DeclRefExpr, assume it is
156 // non-constant.
Tom Care967fea62010-08-25 22:37:26 +0000157 const VarDecl *VD = dyn_cast<VarDecl>(D);
Tom Caredb34ab72010-08-23 19:51:57 +0000158 if (VD)
159 NonConstants->insert(VD);
160 break;
161 }
Tom Care245adab2010-08-18 21:17:24 +0000162
163 default:
164 break;
165 }
166 break;
167 }
168
Tom Caredb34ab72010-08-23 19:51:57 +0000169 // Case 3: Reference Declarations
170 case Stmt::DeclStmtClass: {
171 const DeclStmt *DS = cast<DeclStmt>(Head);
172 // Iterate over each decl and see if any of them contain reference decls
Tom Care967fea62010-08-25 22:37:26 +0000173 for (DeclStmt::const_decl_iterator I = DS->decl_begin(),
174 E = DS->decl_end(); I != E; ++I) {
Tom Caredb34ab72010-08-23 19:51:57 +0000175 // We only care about VarDecls
176 const VarDecl *VD = dyn_cast<VarDecl>(*I);
177 if (!VD)
178 continue;
179
180 // We found a VarDecl; make sure it is a reference type
181 if (!VD->getType().getTypePtr()->isReferenceType())
182 continue;
183
Tom Care967fea62010-08-25 22:37:26 +0000184 // Try to find a Decl in the initializer
185 const Decl *D = getDecl(VD->getInit()->IgnoreParenCasts());
Tom Care916d0542010-08-25 22:46:03 +0000186 if (!D)
187 break;
Tom Care967fea62010-08-25 22:37:26 +0000188
Tom Caredb34ab72010-08-23 19:51:57 +0000189 // If the reference is to another var, add the var to the non-constant
190 // list
Tom Care967fea62010-08-25 22:37:26 +0000191 if (const VarDecl *RefVD = dyn_cast<VarDecl>(D)) {
192 NonConstants->insert(RefVD);
193 continue;
194 }
Tom Caredb34ab72010-08-23 19:51:57 +0000195 }
Tom Careef52bcb2010-08-24 21:09:07 +0000196 break;
197 }
198
John McCallf4b88a42012-03-10 09:33:50 +0000199 // Case 4: Variable references
Tom Careef52bcb2010-08-24 21:09:07 +0000200 case Stmt::DeclRefExprClass: {
201 const DeclRefExpr *DR = cast<DeclRefExpr>(Head);
202 if (const VarDecl *VD = dyn_cast<VarDecl>(DR->getDecl())) {
Tom Care967fea62010-08-25 22:37:26 +0000203 // Add the Decl to the used list
Tom Careef52bcb2010-08-24 21:09:07 +0000204 UsedVars->insert(VD);
205 continue;
206 }
207 break;
Tom Caredb34ab72010-08-23 19:51:57 +0000208 }
209
John McCallf4b88a42012-03-10 09:33:50 +0000210 // Case 5: Block expressions
Tom Care967fea62010-08-25 22:37:26 +0000211 case Stmt::BlockExprClass: {
212 const BlockExpr *B = cast<BlockExpr>(Head);
213 // Add the body of the block to the list
214 WorkList.push_back(B->getBody());
215 continue;
216 }
217
Ted Kremenek892697d2010-12-16 07:46:53 +0000218 default:
219 break;
Tom Care245adab2010-08-18 21:17:24 +0000220 } // switch (head->getStmtClass())
221
222 // Add all substatements to the worklist
John McCall7502c1d2011-02-13 04:07:26 +0000223 for (Stmt::const_child_range I = Head->children(); I; ++I)
Tom Care245adab2010-08-18 21:17:24 +0000224 if (*I)
225 WorkList.push_back(*I);
226 } // while (!WorkList.empty())
227}