2010-08-24 03:51:57 +08:00
|
|
|
//== PseudoConstantAnalysis.cpp - Find Pseudoconstants in the AST-*- C++ -*-==//
|
2010-08-19 05:17:24 +08:00
|
|
|
//
|
|
|
|
// The LLVM Compiler Infrastructure
|
|
|
|
//
|
|
|
|
// This file is distributed under the University of Illinois Open Source
|
|
|
|
// License. See LICENSE.TXT for details.
|
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
//
|
|
|
|
// This file tracks the usage of variables in a Decl body to see if they are
|
|
|
|
// never written to, implying that they constant. This is useful in static
|
|
|
|
// analysis to see if a developer might have intended a variable to be const.
|
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
2010-08-24 03:51:57 +08:00
|
|
|
#include "clang/Analysis/Analyses/PseudoConstantAnalysis.h"
|
2010-08-19 05:17:24 +08:00
|
|
|
#include "clang/AST/Decl.h"
|
|
|
|
#include "clang/AST/Expr.h"
|
|
|
|
#include "clang/AST/Stmt.h"
|
|
|
|
#include <deque>
|
|
|
|
|
|
|
|
using namespace clang;
|
|
|
|
|
2010-08-24 03:51:57 +08:00
|
|
|
// The number of ValueDecls we want to keep track of by default (per-function)
|
|
|
|
#define VARDECL_SET_SIZE 256
|
|
|
|
typedef llvm::SmallPtrSet<const VarDecl*, VARDECL_SET_SIZE> VarDeclSet;
|
|
|
|
|
|
|
|
PseudoConstantAnalysis::PseudoConstantAnalysis(const Stmt *DeclBody) :
|
|
|
|
DeclBody(DeclBody), Analyzed(false) {
|
|
|
|
NonConstantsImpl = new VarDeclSet;
|
2010-08-25 05:09:07 +08:00
|
|
|
UsedVarsImpl = new VarDeclSet;
|
2010-08-24 03:51:57 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
PseudoConstantAnalysis::~PseudoConstantAnalysis() {
|
|
|
|
delete (VarDeclSet*)NonConstantsImpl;
|
2010-08-25 05:09:07 +08:00
|
|
|
delete (VarDeclSet*)UsedVarsImpl;
|
2010-08-24 03:51:57 +08:00
|
|
|
}
|
|
|
|
|
2010-08-19 05:17:24 +08:00
|
|
|
// Returns true if the given ValueDecl is never written to in the given DeclBody
|
2010-08-24 03:51:57 +08:00
|
|
|
bool PseudoConstantAnalysis::isPseudoConstant(const VarDecl *VD) {
|
|
|
|
// Only local and static variables can be pseudoconstants
|
|
|
|
if (!VD->hasLocalStorage() && !VD->isStaticLocal())
|
|
|
|
return false;
|
|
|
|
|
2010-08-19 05:17:24 +08:00
|
|
|
if (!Analyzed) {
|
|
|
|
RunAnalysis();
|
|
|
|
Analyzed = true;
|
|
|
|
}
|
|
|
|
|
2010-08-24 03:51:57 +08:00
|
|
|
VarDeclSet *NonConstants = (VarDeclSet*)NonConstantsImpl;
|
|
|
|
|
|
|
|
return !NonConstants->count(VD);
|
2010-08-19 05:17:24 +08:00
|
|
|
}
|
|
|
|
|
2010-08-25 05:09:07 +08:00
|
|
|
// Returns true if the variable was used (self assignments don't count)
|
|
|
|
bool PseudoConstantAnalysis::wasReferenced(const VarDecl *VD) {
|
|
|
|
if (!Analyzed) {
|
|
|
|
RunAnalysis();
|
|
|
|
Analyzed = true;
|
|
|
|
}
|
|
|
|
|
|
|
|
VarDeclSet *UsedVars = (VarDeclSet*)UsedVarsImpl;
|
|
|
|
|
|
|
|
return UsedVars->count(VD);
|
|
|
|
}
|
|
|
|
|
2010-08-26 06:37:26 +08:00
|
|
|
// Returns a Decl from a (Block)DeclRefExpr (if any)
|
|
|
|
const Decl *PseudoConstantAnalysis::getDecl(const Expr *E) {
|
|
|
|
if (const DeclRefExpr *DR = dyn_cast<DeclRefExpr>(E))
|
|
|
|
return DR->getDecl();
|
|
|
|
else if (const BlockDeclRefExpr *BDR = dyn_cast<BlockDeclRefExpr>(E))
|
|
|
|
return BDR->getDecl();
|
|
|
|
else
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2010-08-24 03:51:57 +08:00
|
|
|
void PseudoConstantAnalysis::RunAnalysis() {
|
2010-08-19 05:17:24 +08:00
|
|
|
std::deque<const Stmt *> WorkList;
|
2010-08-24 03:51:57 +08:00
|
|
|
VarDeclSet *NonConstants = (VarDeclSet*)NonConstantsImpl;
|
2010-08-25 05:09:07 +08:00
|
|
|
VarDeclSet *UsedVars = (VarDeclSet*)UsedVarsImpl;
|
2010-08-19 05:17:24 +08:00
|
|
|
|
|
|
|
// Start with the top level statement of the function
|
|
|
|
WorkList.push_back(DeclBody);
|
|
|
|
|
|
|
|
while (!WorkList.empty()) {
|
2011-08-13 07:37:29 +08:00
|
|
|
const Stmt *Head = WorkList.front();
|
2010-08-19 05:17:24 +08:00
|
|
|
WorkList.pop_front();
|
|
|
|
|
2010-12-16 15:46:53 +08:00
|
|
|
if (const Expr *Ex = dyn_cast<Expr>(Head))
|
|
|
|
Head = Ex->IgnoreParenCasts();
|
|
|
|
|
2010-08-19 05:17:24 +08:00
|
|
|
switch (Head->getStmtClass()) {
|
2010-08-26 06:37:26 +08:00
|
|
|
// Case 1: Assignment operators modifying VarDecls
|
2010-08-19 05:17:24 +08:00
|
|
|
case Stmt::BinaryOperatorClass: {
|
|
|
|
const BinaryOperator *BO = cast<BinaryOperator>(Head);
|
2010-08-26 06:37:26 +08:00
|
|
|
// Look for a Decl on the LHS
|
|
|
|
const Decl *LHSDecl = getDecl(BO->getLHS()->IgnoreParenCasts());
|
|
|
|
if (!LHSDecl)
|
2010-08-19 05:17:24 +08:00
|
|
|
break;
|
|
|
|
|
|
|
|
// We found a binary operator with a DeclRefExpr on the LHS. We now check
|
|
|
|
// for any of the assignment operators, implying that this Decl is being
|
|
|
|
// written to.
|
|
|
|
switch (BO->getOpcode()) {
|
2010-08-26 06:37:26 +08:00
|
|
|
// Self-assignments don't count as use of a variable
|
2010-08-25 19:45:40 +08:00
|
|
|
case BO_Assign: {
|
2010-08-26 06:37:26 +08:00
|
|
|
// Look for a DeclRef on the RHS
|
|
|
|
const Decl *RHSDecl = getDecl(BO->getRHS()->IgnoreParenCasts());
|
|
|
|
|
|
|
|
// If the Decls match, we have self-assignment
|
|
|
|
if (LHSDecl == RHSDecl)
|
|
|
|
// Do not visit the children
|
|
|
|
continue;
|
2010-08-25 05:09:07 +08:00
|
|
|
|
|
|
|
}
|
2010-08-25 19:45:40 +08:00
|
|
|
case BO_AddAssign:
|
|
|
|
case BO_SubAssign:
|
|
|
|
case BO_MulAssign:
|
|
|
|
case BO_DivAssign:
|
|
|
|
case BO_AndAssign:
|
|
|
|
case BO_OrAssign:
|
|
|
|
case BO_XorAssign:
|
|
|
|
case BO_ShlAssign:
|
|
|
|
case BO_ShrAssign: {
|
2010-08-26 06:37:26 +08:00
|
|
|
const VarDecl *VD = dyn_cast<VarDecl>(LHSDecl);
|
2010-08-19 05:17:24 +08:00
|
|
|
// The DeclRefExpr is being assigned to - mark it as non-constant
|
2010-08-24 03:51:57 +08:00
|
|
|
if (VD)
|
|
|
|
NonConstants->insert(VD);
|
|
|
|
break;
|
|
|
|
}
|
2010-08-19 05:17:24 +08:00
|
|
|
|
|
|
|
default:
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Case 2: Pre/post increment/decrement and address of
|
|
|
|
case Stmt::UnaryOperatorClass: {
|
|
|
|
const UnaryOperator *UO = cast<UnaryOperator>(Head);
|
|
|
|
|
2010-08-26 06:37:26 +08:00
|
|
|
// Look for a DeclRef in the subexpression
|
|
|
|
const Decl *D = getDecl(UO->getSubExpr()->IgnoreParenCasts());
|
2010-08-26 06:46:03 +08:00
|
|
|
if (!D)
|
|
|
|
break;
|
2010-08-19 05:17:24 +08:00
|
|
|
|
2010-08-26 06:37:26 +08:00
|
|
|
// We found a unary operator with a DeclRef as a subexpression. We now
|
2010-08-19 05:17:24 +08:00
|
|
|
// check for any of the increment/decrement operators, as well as
|
|
|
|
// addressOf.
|
|
|
|
switch (UO->getOpcode()) {
|
2010-08-25 19:45:40 +08:00
|
|
|
case UO_PostDec:
|
|
|
|
case UO_PostInc:
|
|
|
|
case UO_PreDec:
|
|
|
|
case UO_PreInc:
|
2010-08-26 06:37:26 +08:00
|
|
|
// The DeclRef is being changed - mark it as non-constant
|
2010-08-25 19:45:40 +08:00
|
|
|
case UO_AddrOf: {
|
2010-08-19 05:17:24 +08:00
|
|
|
// If we are taking the address of the DeclRefExpr, assume it is
|
|
|
|
// non-constant.
|
2010-08-26 06:37:26 +08:00
|
|
|
const VarDecl *VD = dyn_cast<VarDecl>(D);
|
2010-08-24 03:51:57 +08:00
|
|
|
if (VD)
|
|
|
|
NonConstants->insert(VD);
|
|
|
|
break;
|
|
|
|
}
|
2010-08-19 05:17:24 +08:00
|
|
|
|
|
|
|
default:
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
2010-08-24 03:51:57 +08:00
|
|
|
// Case 3: Reference Declarations
|
|
|
|
case Stmt::DeclStmtClass: {
|
|
|
|
const DeclStmt *DS = cast<DeclStmt>(Head);
|
|
|
|
// Iterate over each decl and see if any of them contain reference decls
|
2010-08-26 06:37:26 +08:00
|
|
|
for (DeclStmt::const_decl_iterator I = DS->decl_begin(),
|
|
|
|
E = DS->decl_end(); I != E; ++I) {
|
2010-08-24 03:51:57 +08:00
|
|
|
// We only care about VarDecls
|
|
|
|
const VarDecl *VD = dyn_cast<VarDecl>(*I);
|
|
|
|
if (!VD)
|
|
|
|
continue;
|
|
|
|
|
|
|
|
// We found a VarDecl; make sure it is a reference type
|
|
|
|
if (!VD->getType().getTypePtr()->isReferenceType())
|
|
|
|
continue;
|
|
|
|
|
2010-08-26 06:37:26 +08:00
|
|
|
// Try to find a Decl in the initializer
|
|
|
|
const Decl *D = getDecl(VD->getInit()->IgnoreParenCasts());
|
2010-08-26 06:46:03 +08:00
|
|
|
if (!D)
|
|
|
|
break;
|
2010-08-26 06:37:26 +08:00
|
|
|
|
2010-08-24 03:51:57 +08:00
|
|
|
// If the reference is to another var, add the var to the non-constant
|
|
|
|
// list
|
2010-08-26 06:37:26 +08:00
|
|
|
if (const VarDecl *RefVD = dyn_cast<VarDecl>(D)) {
|
|
|
|
NonConstants->insert(RefVD);
|
|
|
|
continue;
|
|
|
|
}
|
2010-08-25 05:09:07 +08:00
|
|
|
}
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Case 4: Block variable references
|
|
|
|
case Stmt::BlockDeclRefExprClass: {
|
|
|
|
const BlockDeclRefExpr *BDR = cast<BlockDeclRefExpr>(Head);
|
|
|
|
if (const VarDecl *VD = dyn_cast<VarDecl>(BDR->getDecl())) {
|
2010-08-26 06:37:26 +08:00
|
|
|
// Add the Decl to the used list
|
2010-08-25 05:09:07 +08:00
|
|
|
UsedVars->insert(VD);
|
|
|
|
continue;
|
2010-08-24 03:51:57 +08:00
|
|
|
}
|
2010-08-25 05:09:07 +08:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Case 5: Variable references
|
|
|
|
case Stmt::DeclRefExprClass: {
|
|
|
|
const DeclRefExpr *DR = cast<DeclRefExpr>(Head);
|
|
|
|
if (const VarDecl *VD = dyn_cast<VarDecl>(DR->getDecl())) {
|
2010-08-26 06:37:26 +08:00
|
|
|
// Add the Decl to the used list
|
2010-08-25 05:09:07 +08:00
|
|
|
UsedVars->insert(VD);
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
break;
|
2010-08-24 03:51:57 +08:00
|
|
|
}
|
|
|
|
|
2010-08-26 06:37:26 +08:00
|
|
|
// Case 6: Block expressions
|
|
|
|
case Stmt::BlockExprClass: {
|
|
|
|
const BlockExpr *B = cast<BlockExpr>(Head);
|
|
|
|
// Add the body of the block to the list
|
|
|
|
WorkList.push_back(B->getBody());
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
2010-12-16 15:46:53 +08:00
|
|
|
default:
|
|
|
|
break;
|
2010-08-19 05:17:24 +08:00
|
|
|
} // switch (head->getStmtClass())
|
|
|
|
|
|
|
|
// Add all substatements to the worklist
|
2011-02-13 12:07:26 +08:00
|
|
|
for (Stmt::const_child_range I = Head->children(); I; ++I)
|
2010-08-19 05:17:24 +08:00
|
|
|
if (*I)
|
|
|
|
WorkList.push_back(*I);
|
|
|
|
} // while (!WorkList.empty())
|
|
|
|
}
|