#include "clang/Analysis/UninitializedValues.h"
#include "clang/Analysis/CFGVarDeclVisitor.h"
#include "clang/Analysis/CFGStmtVisitor.h"
+#include "clang/Analysis/LocalCheckers.h"
+#include "clang/Basic/Diagnostic.h"
+#include "clang/AST/ASTContext.h"
#include "DataflowSolver.h"
+#include "llvm/ADT/SmallPtrSet.h"
+
using namespace clang;
-//===--------------------------------------------------------------------===//
+//===----------------------------------------------------------------------===//
// Dataflow initialization logic.
-//===--------------------------------------------------------------------===//
+//===----------------------------------------------------------------------===//
namespace {
V.ExprBV.resize(getAnalysisData().NumBlockExprs);
}
-//===--------------------------------------------------------------------===//
+//===----------------------------------------------------------------------===//
// Transfer functions.
-//===--------------------------------------------------------------------===//
+//===----------------------------------------------------------------------===//
namespace {
bool VisitStmt(Stmt* S);
bool VisitCallExpr(CallExpr* C);
bool BlockStmt_VisitExpr(Expr* E);
+ bool VisitDeclStmt(DeclStmt* D);
static inline bool Initialized() { return true; }
- static inline bool Unintialized() { return false; }
+ static inline bool Uninitialized() { return false; }
};
bool TransferFuncs::VisitDeclRefExpr(DeclRefExpr* DR) {
if (VarDecl* VD = dyn_cast<VarDecl>(DR->getDecl())) {
assert ( AD.VMap.find(VD) != AD.VMap.end() && "Unknown VarDecl.");
+ if (AD.Observer)
+ AD.Observer->ObserveDeclRefExpr(V,AD,DR,VD);
+
return V.DeclBV[ AD.VMap[VD] ];
}
else
assert ( AD.EMap.find(B) != AD.EMap.end() && "Unknown block-level expr.");
return V.ExprBV[ AD.EMap[B] ];
}
-
+
+ if (B->isAssignmentOp()) {
+ // Get the Decl for the LHS, if any
+ for (Stmt* S = B->getLHS() ;; ) {
+ if (ParenExpr* P = dyn_cast<ParenExpr>(S))
+ S = P->getSubExpr();
+ else if (DeclRefExpr* DR = dyn_cast<DeclRefExpr>(S))
+ if (VarDecl* VD = dyn_cast<VarDecl>(DR->getDecl())) {
+ assert ( AD.VMap.find(VD) != AD.VMap.end() && "Unknown VarDecl.");
+ return V.DeclBV[ AD.VMap[VD] ] = Visit(B->getRHS());
+ }
+
+ break;
+ }
+ }
+
return VisitStmt(B);
}
+bool TransferFuncs::VisitDeclStmt(DeclStmt* S) {
+ bool x = Initialized();
+
+ for (ScopedDecl* D = S->getDecl(); D != NULL; D = D->getNextDeclarator())
+ if (VarDecl* VD = dyn_cast<VarDecl>(D))
+ if (Stmt* I = VD->getInit()) {
+ assert ( AD.VMap.find(VD) != AD.VMap.end() && "Unknown VarDecl.");
+ x = V.DeclBV[ AD.VMap[VD] ] = Visit(I);
+ }
+
+ return x;
+}
+
bool TransferFuncs::VisitCallExpr(CallExpr* C) {
VisitStmt(C);
return Initialized();
// evaluating some subexpressions may result in propogating "Uninitialized"
// or "Initialized" to variables referenced in the other subexpressions.
for (Stmt::child_iterator I=S->child_begin(), E=S->child_end(); I!=E; ++I)
- if (Visit(*I) == Unintialized())
- x = Unintialized();
+ if (Visit(*I) == Uninitialized())
+ x = Uninitialized();
return x;
}
} // end anonymous namespace
-//===--------------------------------------------------------------------===//
+//===----------------------------------------------------------------------===//
// Merge operator.
//
// In our transfer functions we take the approach that any
// in a predecessor we treat it as uninitalized at the confluence point.
// The reason we do this is because dataflow values for tracked Exprs are
// not as control-dependent as dataflow values for tracked Decls.
-//===--------------------------------------------------------------------===//
+//===----------------------------------------------------------------------===//
namespace {
struct Merge {
};
} // end anonymous namespace
-//===--------------------------------------------------------------------===//
-// External interface (driver logic).
-//===--------------------------------------------------------------------===//
+//===----------------------------------------------------------------------===//
+// Unitialized values checker. Scan an AST and flag variable uses
+//===----------------------------------------------------------------------===//
-void UninitializedValues::CheckUninitializedValues(const CFG& cfg) {
+UninitializedValues_ValueTypes::ObserverTy::~ObserverTy() {}
- typedef DataflowSolver<UninitializedValues,TransferFuncs,Merge> Solver;
+namespace {
- UninitializedValues U;
+class UninitializedValuesChecker : public UninitializedValues::ObserverTy {
+ ASTContext &Ctx;
+ Diagnostic &Diags;
+ llvm::SmallPtrSet<VarDecl*,10> AlreadyWarned;
- { // Compute the unitialized values information.
- Solver S(U);
- S.runOnCFG(cfg);
+public:
+ UninitializedValuesChecker(ASTContext &ctx, Diagnostic &diags)
+ : Ctx(ctx), Diags(diags) {}
+
+ virtual void ObserveDeclRefExpr(UninitializedValues::ValTy& V,
+ UninitializedValues::AnalysisDataTy& AD,
+ DeclRefExpr* DR, VarDecl* VD) {
+
+ assert ( AD.VMap.find(VD) != AD.VMap.end() && "Unknown VarDecl.");
+ if (V.DeclBV[ AD.VMap[VD] ] == TransferFuncs::Uninitialized())
+ if (AlreadyWarned.insert(VD))
+ Diags.Report(DR->getSourceRange().Begin(), diag::warn_uninit_val);
}
+};
+
+} // end anonymous namespace
-// WarnObserver O;
+
+void CheckUninitializedValues(CFG& cfg, ASTContext &Ctx, Diagnostic &Diags) {
+
+ typedef DataflowSolver<UninitializedValues,TransferFuncs,Merge> Solver;
+
+ // Compute the unitialized values information.
+ UninitializedValues U;
Solver S(U);
-
- for (CFG::const_iterator I=cfg.begin(), E=cfg.end(); I!=E; ++I)
+ S.runOnCFG(cfg);
+
+ // Scan for DeclRefExprs that use uninitialized values.
+ UninitializedValuesChecker Observer(Ctx,Diags);
+ U.getAnalysisData().Observer = &Observer;
+
+ for (CFG::iterator I=cfg.begin(), E=cfg.end(); I!=E; ++I)
S.runOnBlock(&*I);
}
class VarDecl;
class Expr;
+ class DeclRefExpr;
/// UninitializedValues_ValueTypes - Utility class to wrap type declarations
/// for dataflow values and dataflow analysis state for the
/// Unitialized Values analysis.
class UninitializedValues_ValueTypes {
public:
+
+ //===--------------------------------------------------------------------===//
+ // ValTy - Dataflow value.
+ //===--------------------------------------------------------------------===//
+
struct ValTy {
llvm::BitVector DeclBV;
llvm::BitVector ExprBV;
}
};
+ //===--------------------------------------------------------------------===//
+ // AnalysisDataTy - Whole-function meta data used by the transfer function
+ // logic.
+ //===--------------------------------------------------------------------===//
+
+ struct ObserverTy;
+
struct AnalysisDataTy {
llvm::DenseMap<const VarDecl*, unsigned > VMap;
llvm::DenseMap<const Expr*, unsigned > EMap;
unsigned NumDecls;
unsigned NumBlockExprs;
+ ObserverTy* Observer;
- AnalysisDataTy() : NumDecls(0), NumBlockExprs(0) {}
+ AnalysisDataTy() : NumDecls(0), NumBlockExprs(0), Observer(NULL) {}
};
+
+ //===--------------------------------------------------------------------===//
+ // ObserverTy - Observer for querying DeclRefExprs that use an uninitalized
+ // value.
+ //===--------------------------------------------------------------------===//
+
+ struct ObserverTy {
+ virtual ~ObserverTy();
+ virtual void ObserveDeclRefExpr(ValTy& Val, AnalysisDataTy& AD,
+ DeclRefExpr* DR, VarDecl* VD) = 0;
+ };
};
/// UninitializedValues - Objects of this class encapsulate dataflow analysis
/// information regarding what variable declarations in a function are
/// potentially unintialized.
class UninitializedValues :
- public DataflowValues<UninitializedValues_ValueTypes> {
-
- //===--------------------------------------------------------------------===//
- // Public interface.
- //===--------------------------------------------------------------------===//
-
- static void CheckUninitializedValues(const CFG& cfg);
+ public DataflowValues<UninitializedValues_ValueTypes> {
+public:
+ typedef UninitializedValues_ValueTypes::ObserverTy ObserverTy;
- //===--------------------------------------------------------------------===//
- // Internal logic.
- //===--------------------------------------------------------------------===//
-
-private:
UninitializedValues() {}
-public:
/// IntializeValues - Create initial dataflow values and meta data for
/// a given CFG. This is intended to be called by the dataflow solver.
void InitializeValues(const CFG& cfg);