namespace clang {
class Stmt;
+class Expr;
class ParentMap {
void* Impl;
bool hasParent(Stmt* S) const {
return getParent(S) != 0;
}
+
+ bool isConsumedExpr(Expr *E) const;
+
+ bool isConsumedExpr(const Expr *E) const {
+ return isConsumedExpr(const_cast<Expr*>(E));
+ }
};
} // end clang namespace
// to lazily evaluate such logic. The downside is that it eagerly
// bifurcates paths.
const bool EagerlyAssume;
-
+
public:
typedef llvm::SmallPtrSet<NodeTy*,2> ErrorNodes;
typedef llvm::DenseMap<NodeTy*, Expr*> UndefArgsTy;
MapTy::iterator I = M->find(S);
return I == M->end() ? 0 : I->second;
}
+
+bool ParentMap::isConsumedExpr(Expr* E) const {
+ Stmt *P = getParent(E);
+ Stmt *DirectChild = E;
+
+ // Ignore parents that are parentheses or casts.
+ while (P && (isa<ParenExpr>(E) || isa<CastExpr>(E))) {
+ DirectChild = P;
+ P = getParent(P);
+ }
+
+ if (!P)
+ return false;
+
+ switch (P->getStmtClass()) {
+ default:
+ return isa<Expr>(P);
+ case Stmt::DeclStmtClass:
+ return true;
+ case Stmt::BinaryOperatorClass: {
+ BinaryOperator *BE = cast<BinaryOperator>(P);
+ return BE->getOpcode()==BinaryOperator::Comma && DirectChild==BE->getLHS();
+ }
+ case Stmt::ForStmtClass:
+ return DirectChild == cast<ForStmt>(P)->getCond();
+ case Stmt::WhileStmtClass:
+ return DirectChild == cast<WhileStmt>(P)->getCond();
+ case Stmt::DoStmtClass:
+ return DirectChild == cast<DoStmt>(P)->getCond();
+ case Stmt::IfStmtClass:
+ return DirectChild == cast<IfStmt>(P)->getCond();
+ case Stmt::IndirectGotoStmtClass:
+ return DirectChild == cast<IndirectGotoStmt>(P)->getTarget();
+ case Stmt::SwitchStmtClass:
+ return DirectChild == cast<SwitchStmt>(P)->getCond();
+ case Stmt::ReturnStmtClass:
+ return true;
+ }
+}
+
: Ctx(ctx), BR(br), Parents(parents) {}
virtual ~DeadStoreObs() {}
-
- bool isConsumedExpr(Expr* E) const;
-
-
+
void Report(VarDecl* V, DeadStoreKind dsk, SourceLocation L, SourceRange R) {
std::string name = V->getNameAsString();
return;
// Otherwise, issue a warning.
- DeadStoreKind dsk = isConsumedExpr(B)
+ DeadStoreKind dsk = Parents.isConsumedExpr(B)
? Enclosing
: (isIncrement(VD,B) ? DeadIncrement : Standard);
// about preincrements to dead variables when the preincrement occurs
// as a subexpression. This can lead to false negatives, e.g. "(++x);"
// A generalized dead code checker should find such issues.
- if (U->isPrefix() && isConsumedExpr(U))
+ if (U->isPrefix() && Parents.isConsumedExpr(U))
return;
Expr *Ex = U->getSubExpr()->IgnoreParenCasts();
} // end anonymous namespace
-bool DeadStoreObs::isConsumedExpr(Expr* E) const {
- Stmt *P = Parents.getParent(E);
- Stmt *DirectChild = E;
-
- // Ignore parents that are parentheses or casts.
- while (P && (isa<ParenExpr>(E) || isa<CastExpr>(E))) {
- DirectChild = P;
- P = Parents.getParent(P);
- }
-
- if (!P)
- return false;
-
- switch (P->getStmtClass()) {
- default:
- return isa<Expr>(P);
- case Stmt::BinaryOperatorClass: {
- BinaryOperator *BE = cast<BinaryOperator>(P);
- return BE->getOpcode()==BinaryOperator::Comma && DirectChild==BE->getLHS();
- }
- case Stmt::ForStmtClass:
- return DirectChild == cast<ForStmt>(P)->getCond();
- case Stmt::WhileStmtClass:
- return DirectChild == cast<WhileStmt>(P)->getCond();
- case Stmt::DoStmtClass:
- return DirectChild == cast<DoStmt>(P)->getCond();
- case Stmt::IfStmtClass:
- return DirectChild == cast<IfStmt>(P)->getCond();
- case Stmt::IndirectGotoStmtClass:
- return DirectChild == cast<IndirectGotoStmt>(P)->getTarget();
- case Stmt::SwitchStmtClass:
- return DirectChild == cast<SwitchStmt>(P)->getCond();
- case Stmt::ReturnStmtClass:
- return true;
- }
-}
-
//===----------------------------------------------------------------------===//
// Driver function to invoke the Dead-Stores checker on a CFG.
//===----------------------------------------------------------------------===//
//
//===----------------------------------------------------------------------===//
+#include "clang/AST/ParentMap.h"
#include "clang/Analysis/PathSensitive/GRExprEngine.h"
#include "clang/Analysis/PathSensitive/GRExprEngineBuilders.h"
-
#include "clang/Analysis/PathSensitive/BugReporter.h"
#include "clang/Basic/SourceManager.h"
#include "clang/Basic/PrettyStackTrace.h"
if (isFeasibleNull) {
// Check if the receiver was nil and the return value a struct.
- if (ME->getType()->isRecordType()) {
+ if (ME->getType()->isRecordType() &&
+ BR.getParentMap().isConsumedExpr(ME)) {
// The [0 ...] expressions will return garbage. Flag either an
// explicit or implicit error. Because of the structure of this
// function we currently do not bifurfacte the state graph at
Bar f = [obj foo]; // expected-warning{{The receiver in the message expression is 'nil' and results in the returned value (of type 'Bar') to be garbage or otherwise undefined.}}
}
+void createFoo2() {
+ MyClass *obj = 0;
+ [obj foo]; // no-warning
+ Bar f = [obj foo]; // expected-warning{{The receiver in the message expression is 'nil' and results in the returned value (of type 'Bar') to be garbage or otherwise undefined.}}
+}
+