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