Tom Care | db34ab7 | 2010-08-23 19:51:57 +0000 | [diff] [blame] | 1 | //== PseudoConstantAnalysis.cpp - Find Pseudoconstants in the AST-*- C++ -*-==// |
Tom Care | 245adab | 2010-08-18 21:17:24 +0000 | [diff] [blame] | 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 | // 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 Care | db34ab7 | 2010-08-23 19:51:57 +0000 | [diff] [blame] | 16 | #include "clang/Analysis/Analyses/PseudoConstantAnalysis.h" |
Tom Care | 245adab | 2010-08-18 21:17:24 +0000 | [diff] [blame] | 17 | #include "clang/AST/Decl.h" |
| 18 | #include "clang/AST/Expr.h" |
| 19 | #include "clang/AST/Stmt.h" |
| 20 | #include <deque> |
| 21 | |
| 22 | using namespace clang; |
| 23 | |
Tom Care | db34ab7 | 2010-08-23 19:51:57 +0000 | [diff] [blame] | 24 | // The number of ValueDecls we want to keep track of by default (per-function) |
| 25 | #define VARDECL_SET_SIZE 256 |
| 26 | typedef llvm::SmallPtrSet<const VarDecl*, VARDECL_SET_SIZE> VarDeclSet; |
| 27 | |
| 28 | PseudoConstantAnalysis::PseudoConstantAnalysis(const Stmt *DeclBody) : |
| 29 | DeclBody(DeclBody), Analyzed(false) { |
| 30 | NonConstantsImpl = new VarDeclSet; |
Tom Care | ef52bcb | 2010-08-24 21:09:07 +0000 | [diff] [blame] | 31 | UsedVarsImpl = new VarDeclSet; |
Tom Care | db34ab7 | 2010-08-23 19:51:57 +0000 | [diff] [blame] | 32 | } |
| 33 | |
| 34 | PseudoConstantAnalysis::~PseudoConstantAnalysis() { |
| 35 | delete (VarDeclSet*)NonConstantsImpl; |
Tom Care | ef52bcb | 2010-08-24 21:09:07 +0000 | [diff] [blame] | 36 | delete (VarDeclSet*)UsedVarsImpl; |
Tom Care | db34ab7 | 2010-08-23 19:51:57 +0000 | [diff] [blame] | 37 | } |
| 38 | |
Tom Care | 245adab | 2010-08-18 21:17:24 +0000 | [diff] [blame] | 39 | // Returns true if the given ValueDecl is never written to in the given DeclBody |
Tom Care | db34ab7 | 2010-08-23 19:51:57 +0000 | [diff] [blame] | 40 | bool 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 Care | 245adab | 2010-08-18 21:17:24 +0000 | [diff] [blame] | 45 | if (!Analyzed) { |
| 46 | RunAnalysis(); |
| 47 | Analyzed = true; |
| 48 | } |
| 49 | |
Tom Care | db34ab7 | 2010-08-23 19:51:57 +0000 | [diff] [blame] | 50 | VarDeclSet *NonConstants = (VarDeclSet*)NonConstantsImpl; |
| 51 | |
| 52 | return !NonConstants->count(VD); |
Tom Care | 245adab | 2010-08-18 21:17:24 +0000 | [diff] [blame] | 53 | } |
| 54 | |
Tom Care | ef52bcb | 2010-08-24 21:09:07 +0000 | [diff] [blame] | 55 | // Returns true if the variable was used (self assignments don't count) |
| 56 | bool 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 Care | 967fea6 | 2010-08-25 22:37:26 +0000 | [diff] [blame] | 67 | // Returns a Decl from a (Block)DeclRefExpr (if any) |
| 68 | const Decl *PseudoConstantAnalysis::getDecl(const Expr *E) { |
| 69 | if (const DeclRefExpr *DR = dyn_cast<DeclRefExpr>(E)) |
| 70 | return DR->getDecl(); |
| 71 | else if (const BlockDeclRefExpr *BDR = dyn_cast<BlockDeclRefExpr>(E)) |
| 72 | return BDR->getDecl(); |
| 73 | else |
| 74 | return 0; |
| 75 | } |
| 76 | |
Tom Care | db34ab7 | 2010-08-23 19:51:57 +0000 | [diff] [blame] | 77 | void PseudoConstantAnalysis::RunAnalysis() { |
Tom Care | 245adab | 2010-08-18 21:17:24 +0000 | [diff] [blame] | 78 | std::deque<const Stmt *> WorkList; |
Tom Care | db34ab7 | 2010-08-23 19:51:57 +0000 | [diff] [blame] | 79 | VarDeclSet *NonConstants = (VarDeclSet*)NonConstantsImpl; |
Tom Care | ef52bcb | 2010-08-24 21:09:07 +0000 | [diff] [blame] | 80 | VarDeclSet *UsedVars = (VarDeclSet*)UsedVarsImpl; |
Tom Care | 245adab | 2010-08-18 21:17:24 +0000 | [diff] [blame] | 81 | |
| 82 | // Start with the top level statement of the function |
| 83 | WorkList.push_back(DeclBody); |
| 84 | |
| 85 | while (!WorkList.empty()) { |
Ted Kremenek | 9c378f7 | 2011-08-12 23:37:29 +0000 | [diff] [blame] | 86 | const Stmt *Head = WorkList.front(); |
Tom Care | 245adab | 2010-08-18 21:17:24 +0000 | [diff] [blame] | 87 | WorkList.pop_front(); |
| 88 | |
Ted Kremenek | 892697d | 2010-12-16 07:46:53 +0000 | [diff] [blame] | 89 | if (const Expr *Ex = dyn_cast<Expr>(Head)) |
| 90 | Head = Ex->IgnoreParenCasts(); |
| 91 | |
Tom Care | 245adab | 2010-08-18 21:17:24 +0000 | [diff] [blame] | 92 | switch (Head->getStmtClass()) { |
Tom Care | 967fea6 | 2010-08-25 22:37:26 +0000 | [diff] [blame] | 93 | // Case 1: Assignment operators modifying VarDecls |
Tom Care | 245adab | 2010-08-18 21:17:24 +0000 | [diff] [blame] | 94 | case Stmt::BinaryOperatorClass: { |
| 95 | const BinaryOperator *BO = cast<BinaryOperator>(Head); |
Tom Care | 967fea6 | 2010-08-25 22:37:26 +0000 | [diff] [blame] | 96 | // Look for a Decl on the LHS |
| 97 | const Decl *LHSDecl = getDecl(BO->getLHS()->IgnoreParenCasts()); |
Tom Care | 967fea6 | 2010-08-25 22:37:26 +0000 | [diff] [blame] | 98 | if (!LHSDecl) |
Tom Care | 245adab | 2010-08-18 21:17:24 +0000 | [diff] [blame] | 99 | break; |
| 100 | |
| 101 | // We found a binary operator with a DeclRefExpr on the LHS. We now check |
| 102 | // for any of the assignment operators, implying that this Decl is being |
| 103 | // written to. |
| 104 | switch (BO->getOpcode()) { |
Tom Care | 967fea6 | 2010-08-25 22:37:26 +0000 | [diff] [blame] | 105 | // Self-assignments don't count as use of a variable |
John McCall | 2de56d1 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 106 | case BO_Assign: { |
Tom Care | 967fea6 | 2010-08-25 22:37:26 +0000 | [diff] [blame] | 107 | // Look for a DeclRef on the RHS |
| 108 | const Decl *RHSDecl = getDecl(BO->getRHS()->IgnoreParenCasts()); |
| 109 | |
| 110 | // If the Decls match, we have self-assignment |
| 111 | if (LHSDecl == RHSDecl) |
| 112 | // Do not visit the children |
| 113 | continue; |
Tom Care | ef52bcb | 2010-08-24 21:09:07 +0000 | [diff] [blame] | 114 | |
| 115 | } |
John McCall | 2de56d1 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 116 | case BO_AddAssign: |
| 117 | case BO_SubAssign: |
| 118 | case BO_MulAssign: |
| 119 | case BO_DivAssign: |
| 120 | case BO_AndAssign: |
| 121 | case BO_OrAssign: |
| 122 | case BO_XorAssign: |
| 123 | case BO_ShlAssign: |
| 124 | case BO_ShrAssign: { |
Tom Care | 967fea6 | 2010-08-25 22:37:26 +0000 | [diff] [blame] | 125 | const VarDecl *VD = dyn_cast<VarDecl>(LHSDecl); |
Tom Care | 245adab | 2010-08-18 21:17:24 +0000 | [diff] [blame] | 126 | // The DeclRefExpr is being assigned to - mark it as non-constant |
Tom Care | db34ab7 | 2010-08-23 19:51:57 +0000 | [diff] [blame] | 127 | if (VD) |
| 128 | NonConstants->insert(VD); |
| 129 | break; |
| 130 | } |
Tom Care | 245adab | 2010-08-18 21:17:24 +0000 | [diff] [blame] | 131 | |
| 132 | default: |
| 133 | break; |
| 134 | } |
| 135 | break; |
| 136 | } |
| 137 | |
| 138 | // Case 2: Pre/post increment/decrement and address of |
| 139 | case Stmt::UnaryOperatorClass: { |
| 140 | const UnaryOperator *UO = cast<UnaryOperator>(Head); |
Tom Care | 245adab | 2010-08-18 21:17:24 +0000 | [diff] [blame] | 141 | |
Tom Care | 967fea6 | 2010-08-25 22:37:26 +0000 | [diff] [blame] | 142 | // Look for a DeclRef in the subexpression |
| 143 | const Decl *D = getDecl(UO->getSubExpr()->IgnoreParenCasts()); |
Tom Care | 916d054 | 2010-08-25 22:46:03 +0000 | [diff] [blame] | 144 | if (!D) |
| 145 | break; |
Tom Care | 245adab | 2010-08-18 21:17:24 +0000 | [diff] [blame] | 146 | |
Tom Care | 967fea6 | 2010-08-25 22:37:26 +0000 | [diff] [blame] | 147 | // We found a unary operator with a DeclRef as a subexpression. We now |
Tom Care | 245adab | 2010-08-18 21:17:24 +0000 | [diff] [blame] | 148 | // check for any of the increment/decrement operators, as well as |
| 149 | // addressOf. |
| 150 | switch (UO->getOpcode()) { |
John McCall | 2de56d1 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 151 | case UO_PostDec: |
| 152 | case UO_PostInc: |
| 153 | case UO_PreDec: |
| 154 | case UO_PreInc: |
Tom Care | 967fea6 | 2010-08-25 22:37:26 +0000 | [diff] [blame] | 155 | // The DeclRef is being changed - mark it as non-constant |
John McCall | 2de56d1 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 156 | case UO_AddrOf: { |
Tom Care | 245adab | 2010-08-18 21:17:24 +0000 | [diff] [blame] | 157 | // If we are taking the address of the DeclRefExpr, assume it is |
| 158 | // non-constant. |
Tom Care | 967fea6 | 2010-08-25 22:37:26 +0000 | [diff] [blame] | 159 | const VarDecl *VD = dyn_cast<VarDecl>(D); |
Tom Care | db34ab7 | 2010-08-23 19:51:57 +0000 | [diff] [blame] | 160 | if (VD) |
| 161 | NonConstants->insert(VD); |
| 162 | break; |
| 163 | } |
Tom Care | 245adab | 2010-08-18 21:17:24 +0000 | [diff] [blame] | 164 | |
| 165 | default: |
| 166 | break; |
| 167 | } |
| 168 | break; |
| 169 | } |
| 170 | |
Tom Care | db34ab7 | 2010-08-23 19:51:57 +0000 | [diff] [blame] | 171 | // Case 3: Reference Declarations |
| 172 | case Stmt::DeclStmtClass: { |
| 173 | const DeclStmt *DS = cast<DeclStmt>(Head); |
| 174 | // Iterate over each decl and see if any of them contain reference decls |
Tom Care | 967fea6 | 2010-08-25 22:37:26 +0000 | [diff] [blame] | 175 | for (DeclStmt::const_decl_iterator I = DS->decl_begin(), |
| 176 | E = DS->decl_end(); I != E; ++I) { |
Tom Care | db34ab7 | 2010-08-23 19:51:57 +0000 | [diff] [blame] | 177 | // We only care about VarDecls |
| 178 | const VarDecl *VD = dyn_cast<VarDecl>(*I); |
| 179 | if (!VD) |
| 180 | continue; |
| 181 | |
| 182 | // We found a VarDecl; make sure it is a reference type |
| 183 | if (!VD->getType().getTypePtr()->isReferenceType()) |
| 184 | continue; |
| 185 | |
Tom Care | 967fea6 | 2010-08-25 22:37:26 +0000 | [diff] [blame] | 186 | // Try to find a Decl in the initializer |
| 187 | const Decl *D = getDecl(VD->getInit()->IgnoreParenCasts()); |
Tom Care | 916d054 | 2010-08-25 22:46:03 +0000 | [diff] [blame] | 188 | if (!D) |
| 189 | break; |
Tom Care | 967fea6 | 2010-08-25 22:37:26 +0000 | [diff] [blame] | 190 | |
Tom Care | db34ab7 | 2010-08-23 19:51:57 +0000 | [diff] [blame] | 191 | // If the reference is to another var, add the var to the non-constant |
| 192 | // list |
Tom Care | 967fea6 | 2010-08-25 22:37:26 +0000 | [diff] [blame] | 193 | if (const VarDecl *RefVD = dyn_cast<VarDecl>(D)) { |
| 194 | NonConstants->insert(RefVD); |
| 195 | continue; |
| 196 | } |
Tom Care | db34ab7 | 2010-08-23 19:51:57 +0000 | [diff] [blame] | 197 | } |
Tom Care | ef52bcb | 2010-08-24 21:09:07 +0000 | [diff] [blame] | 198 | break; |
| 199 | } |
| 200 | |
| 201 | // Case 4: Block variable references |
| 202 | case Stmt::BlockDeclRefExprClass: { |
Tom Care | ef52bcb | 2010-08-24 21:09:07 +0000 | [diff] [blame] | 203 | const BlockDeclRefExpr *BDR = cast<BlockDeclRefExpr>(Head); |
| 204 | if (const VarDecl *VD = dyn_cast<VarDecl>(BDR->getDecl())) { |
Tom Care | 967fea6 | 2010-08-25 22:37:26 +0000 | [diff] [blame] | 205 | // Add the Decl to the used list |
Tom Care | ef52bcb | 2010-08-24 21:09:07 +0000 | [diff] [blame] | 206 | UsedVars->insert(VD); |
| 207 | continue; |
| 208 | } |
| 209 | break; |
| 210 | } |
| 211 | |
| 212 | // Case 5: Variable references |
| 213 | case Stmt::DeclRefExprClass: { |
| 214 | const DeclRefExpr *DR = cast<DeclRefExpr>(Head); |
| 215 | if (const VarDecl *VD = dyn_cast<VarDecl>(DR->getDecl())) { |
Tom Care | 967fea6 | 2010-08-25 22:37:26 +0000 | [diff] [blame] | 216 | // Add the Decl to the used list |
Tom Care | ef52bcb | 2010-08-24 21:09:07 +0000 | [diff] [blame] | 217 | UsedVars->insert(VD); |
| 218 | continue; |
| 219 | } |
| 220 | break; |
Tom Care | db34ab7 | 2010-08-23 19:51:57 +0000 | [diff] [blame] | 221 | } |
| 222 | |
Tom Care | 967fea6 | 2010-08-25 22:37:26 +0000 | [diff] [blame] | 223 | // Case 6: Block expressions |
| 224 | case Stmt::BlockExprClass: { |
| 225 | const BlockExpr *B = cast<BlockExpr>(Head); |
| 226 | // Add the body of the block to the list |
| 227 | WorkList.push_back(B->getBody()); |
| 228 | continue; |
| 229 | } |
| 230 | |
Ted Kremenek | 892697d | 2010-12-16 07:46:53 +0000 | [diff] [blame] | 231 | default: |
| 232 | break; |
Tom Care | 245adab | 2010-08-18 21:17:24 +0000 | [diff] [blame] | 233 | } // switch (head->getStmtClass()) |
| 234 | |
| 235 | // Add all substatements to the worklist |
John McCall | 7502c1d | 2011-02-13 04:07:26 +0000 | [diff] [blame] | 236 | for (Stmt::const_child_range I = Head->children(); I; ++I) |
Tom Care | 245adab | 2010-08-18 21:17:24 +0000 | [diff] [blame] | 237 | if (*I) |
| 238 | WorkList.push_back(*I); |
| 239 | } // while (!WorkList.empty()) |
| 240 | } |