blob: 0bbd8786098175137fb9959a02fa5c5c21aefa46 [file] [log] [blame]
Ted Kremenek1ed6d2e2007-09-06 23:01:46 +00001//==- DeadStores.cpp - Check for stores to dead variables --------*- C++ -*-==//
2//
3// The LLVM Compiler Infrastructure
4//
Chris Lattner0bc735f2007-12-29 19:59:25 +00005// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
Ted Kremenek1ed6d2e2007-09-06 23:01:46 +00007//
8//===----------------------------------------------------------------------===//
9//
Gabor Greif843e9342008-03-06 10:40:09 +000010// This file defines a DeadStores, a flow-sensitive checker that looks for
Ted Kremenek1ed6d2e2007-09-06 23:01:46 +000011// stores to variables that are no longer live.
12//
13//===----------------------------------------------------------------------===//
14
Ted Kremenek1ed6d2e2007-09-06 23:01:46 +000015#include "clang/Analysis/LocalCheckers.h"
Ted Kremenekcf6e41b2007-12-21 21:42:19 +000016#include "clang/Analysis/Analyses/LiveVariables.h"
Ted Kremenekfdd225e2007-09-25 04:31:27 +000017#include "clang/Analysis/Visitors/CFGRecStmtVisitor.h"
Ted Kremenekd2f642b2008-04-14 17:39:48 +000018#include "clang/Analysis/PathSensitive/BugReporter.h"
19#include "clang/Analysis/PathSensitive/GRExprEngine.h"
Ted Kremenek1ed6d2e2007-09-06 23:01:46 +000020#include "clang/Basic/Diagnostic.h"
Ted Kremenekce1cab92007-09-11 17:24:14 +000021#include "clang/AST/ASTContext.h"
Ted Kremenek1a654b62008-06-20 21:45:25 +000022#include "clang/AST/ParentMap.h"
Ted Kremenekc2b51d82008-01-08 18:19:08 +000023#include "llvm/Support/Compiler.h"
Ted Kremenek1ed6d2e2007-09-06 23:01:46 +000024
25using namespace clang;
26
27namespace {
Ted Kremenek1a654b62008-06-20 21:45:25 +000028
Ted Kremenekc2b51d82008-01-08 18:19:08 +000029class VISIBILITY_HIDDEN DeadStoreObs : public LiveVariables::ObserverTy {
Chris Lattnerc0508f92007-09-15 23:21:08 +000030 ASTContext &Ctx;
Ted Kremenek8f269862008-07-14 20:56:04 +000031 BugReporter& BR;
Ted Kremenek1a654b62008-06-20 21:45:25 +000032 ParentMap& Parents;
33
Ted Kremenek1ed6d2e2007-09-06 23:01:46 +000034public:
Ted Kremenek8f269862008-07-14 20:56:04 +000035 DeadStoreObs(ASTContext &ctx, BugReporter& br, ParentMap& parents)
36 : Ctx(ctx), BR(br), Parents(parents) {}
Ted Kremenekd2f642b2008-04-14 17:39:48 +000037
Ted Kremenekfdd225e2007-09-25 04:31:27 +000038 virtual ~DeadStoreObs() {}
39
Ted Kremenek8f269862008-07-14 20:56:04 +000040 void Report(VarDecl* V, bool inEnclosing, SourceLocation L, SourceRange R) {
41
42 std::string name(V->getName());
Ted Kremenek1a654b62008-06-20 21:45:25 +000043 std::string msg = inEnclosing
44 ? "Although the value stored to '" + name +
Ted Kremeneke2075582008-07-02 23:16:33 +000045 "' is used in the enclosing expression, the value is never actually"
46 " read from '" + name + "'"
Ted Kremenek1a654b62008-06-20 21:45:25 +000047 : "Value stored to '" + name + "' is never read";
48
Ted Kremenek8f269862008-07-14 20:56:04 +000049 BR.EmitBasicReport("dead store", msg.c_str(), L, R);
Ted Kremenek1a654b62008-06-20 21:45:25 +000050 }
51
52 void CheckVarDecl(VarDecl* VD, Expr* Ex, Expr* Val,
53 bool hasEnclosing,
54 const LiveVariables::AnalysisDataTy& AD,
55 const LiveVariables::ValTy& Live) {
56
Ted Kremenek8f269862008-07-14 20:56:04 +000057 if (VD->hasLocalStorage() && !Live(VD, AD))
58 Report(VD, hasEnclosing, Ex->getSourceRange().getBegin(),
59 Val->getSourceRange());
Ted Kremenek3eb817e2008-05-21 22:59:16 +000060 }
61
Ted Kremeneka23157e2008-05-05 23:12:21 +000062 void CheckDeclRef(DeclRefExpr* DR, Expr* Val,
63 const LiveVariables::AnalysisDataTy& AD,
64 const LiveVariables::ValTy& Live) {
65
66 if (VarDecl* VD = dyn_cast<VarDecl>(DR->getDecl()))
Ted Kremenek1a654b62008-06-20 21:45:25 +000067 CheckVarDecl(VD, DR, Val, false, AD, Live);
Ted Kremeneka23157e2008-05-05 23:12:21 +000068 }
69
Ted Kremenekfdd225e2007-09-25 04:31:27 +000070 virtual void ObserveStmt(Stmt* S,
71 const LiveVariables::AnalysisDataTy& AD,
72 const LiveVariables::ValTy& Live) {
Ted Kremenekce1cab92007-09-11 17:24:14 +000073
Ted Kremenek1c86b152008-04-14 18:28:25 +000074 // Skip statements in macros.
75 if (S->getLocStart().isMacroID())
76 return;
77
Ted Kremenek1ed6d2e2007-09-06 23:01:46 +000078 if (BinaryOperator* B = dyn_cast<BinaryOperator>(S)) {
Ted Kremenekfdd225e2007-09-25 04:31:27 +000079 if (!B->isAssignmentOp()) return; // Skip non-assignments.
Ted Kremenek1ed6d2e2007-09-06 23:01:46 +000080
Ted Kremenekc0576ca2007-09-10 17:36:42 +000081 if (DeclRefExpr* DR = dyn_cast<DeclRefExpr>(B->getLHS()))
Ted Kremenek1a654b62008-06-20 21:45:25 +000082 if (VarDecl *VD = dyn_cast<VarDecl>(DR->getDecl())) {
83
84 // Special case: check for assigning null to a pointer. This
85 // is a common form of defensive programming.
86 // FIXME: Make this optional?
87
88 Expr* Val = B->getRHS();
89 llvm::APSInt Result(Ctx.getTypeSize(Val->getType()));
90
91 if (VD->getType()->isPointerType() &&
92 Val->IgnoreParenCasts()->isIntegerConstantExpr(Result, Ctx, 0))
93 if (Result == 0)
94 return;
95
96 CheckVarDecl(VD, DR, Val, Parents.isSubExpr(B), AD, Live);
97 }
Ted Kremenek1ed6d2e2007-09-06 23:01:46 +000098 }
Ted Kremeneka23157e2008-05-05 23:12:21 +000099 else if (UnaryOperator* U = dyn_cast<UnaryOperator>(S)) {
100 if (!U->isIncrementOp())
101 return;
102
103 Expr *Ex = U->getSubExpr()->IgnoreParenCasts();
104
105 if (DeclRefExpr* DR = dyn_cast<DeclRefExpr>(Ex))
106 CheckDeclRef(DR, U, AD, Live);
107 }
108 else if (DeclStmt* DS = dyn_cast<DeclStmt>(S))
Ted Kremenekfdd225e2007-09-25 04:31:27 +0000109 // Iterate through the decls. Warn if any initializers are complex
110 // expressions that are not live (never used).
Ted Kremenekc967c9d2008-04-14 17:52:13 +0000111 for (ScopedDecl* SD = DS->getDecl(); SD; SD = SD->getNextDeclarator()) {
112
113 VarDecl* V = dyn_cast<VarDecl>(SD);
114 if (!V) continue;
115
Ted Kremenekc6a1faf2007-09-28 20:48:41 +0000116 if (V->hasLocalStorage())
Ted Kremenek8f269862008-07-14 20:56:04 +0000117 if (Expr* E = V->getInit())
Ted Kremenek9d7af512008-04-15 04:11:48 +0000118 if (!Live(V, AD)) {
Ted Kremenekc6a1faf2007-09-28 20:48:41 +0000119 // Special case: check for initializations with constants.
120 //
121 // e.g. : int x = 0;
122 //
123 // If x is EVER assigned a new value later, don't issue
124 // a warning. This is because such initialization can be
125 // due to defensive programming.
Ted Kremenek8f269862008-07-14 20:56:04 +0000126 if (!E->isConstantExpr(Ctx,NULL))
127 Report(V, false, V->getLocation(), E->getSourceRange());
Ted Kremenekce1cab92007-09-11 17:24:14 +0000128 }
Ted Kremenekfdd225e2007-09-25 04:31:27 +0000129 }
Ted Kremenek1ed6d2e2007-09-06 23:01:46 +0000130 }
131};
132
133} // end anonymous namespace
134
Ted Kremenekd2f642b2008-04-14 17:39:48 +0000135//===----------------------------------------------------------------------===//
Ted Kremeneke2075582008-07-02 23:16:33 +0000136// Driver function to invoke the Dead-Stores checker on a CFG.
137//===----------------------------------------------------------------------===//
138
139void clang::CheckDeadStores(LiveVariables& L, BugReporter& BR) {
Ted Kremenek8f269862008-07-14 20:56:04 +0000140 DeadStoreObs A(BR.getContext(), BR, BR.getParentMap());
Ted Kremenek7032f462008-07-03 05:26:14 +0000141 L.runOnAllBlocks(*BR.getCFG(), &A);
Ted Kremenekd2f642b2008-04-14 17:39:48 +0000142}