InGroup<Uninitialized>;
def warn_uninit_var : Warning<"variable %0 is possibly uninitialized when used here">,
InGroup<DiagGroup<"uninitialized-experimental">>, DefaultIgnore;
+def warn_maybe_uninit_var :
+ Warning<"variable %0 is possibly uninitialized when used here">,
+ InGroup<UninitializedMaybe>, DefaultIgnore;
def note_uninit_var_def : Note<
"variable %0 is declared here">;
def warn_uninit_var_captured_by_block : Warning<
"variable %0 is possibly uninitialized when captured by block">,
InGroup<DiagGroup<"uninitialized-experimental">>, DefaultIgnore;
+def warn_maybe_uninit_var_captured_by_block : Warning<
+ "variable %0 is possibly uninitialized when captured by block">,
+ InGroup<UninitializedMaybe>, DefaultIgnore;
def note_var_fixit_add_initialization : Note<
"add initialization to silence this warning">;
def err_init_incomplete_type : Error<"initialization of incomplete type %0">;
// CFGBlockValues: dataflow values for CFG blocks.
//====------------------------------------------------------------------------//
-enum Value { Initialized = 0, Uninitialized = 1 };
+// These values are defined in such a way that a merge can be done using
+// a bitwise OR.
+enum Value { Unknown = 0x0, /* 00 */
+ Initialized = 0x1, /* 01 */
+ Uninitialized = 0x2, /* 10 */
+ MayUninitialized = 0x3 /* 11 */ };
+
+static bool isUninitialized(const Value v) {
+ return v >= Uninitialized;
+}
+static bool isAlwaysUninit(const Value v) {
+ return v == Uninitialized;
+}
class ValueVector {
llvm::BitVector vec;
public:
ValueVector() {}
- ValueVector(unsigned size) : vec(size) {}
- void resize(unsigned n) { vec.resize(n); }
+ ValueVector(unsigned size) : vec(size << 1) {}
+ void resize(unsigned n) { vec.resize(n << 1); }
void merge(const ValueVector &rhs) { vec |= rhs.vec; }
bool operator!=(const ValueVector &rhs) const { return vec != rhs.vec; }
void reset() { vec.reset(); }
~reference() {}
reference &operator=(Value v) {
- vv.vec[idx] = (v == Initialized ? false : true);
+ vv.vec[idx << 1] = (((unsigned) v) & 0x1) ? true : false;
+ vv.vec[(idx << 1) | 1] = (((unsigned) v) & 0x2) ? true : false;
return *this;
}
+ operator Value() {
+ unsigned x = (vv.vec[idx << 1] ? 1 : 0) | (vv.vec[(idx << 1) | 1] ? 2 :0);
+ return (Value) x;
+ }
+
bool operator==(Value v) {
- return vv.vec[idx] == (v == Initialized ? false : true);
+ return v = operator Value();
}
};
currentVoidCast(0), flagBlockUses(flagBlockUses) {}
const CFG &getCFG() { return cfg; }
- void reportUninit(const DeclRefExpr *ex, const VarDecl *vd);
+ void reportUninit(const DeclRefExpr *ex, const VarDecl *vd,
+ bool isAlwaysUninit);
void VisitBlockExpr(BlockExpr *be);
void VisitDeclStmt(DeclStmt *ds);
}
void TransferFunctions::reportUninit(const DeclRefExpr *ex,
- const VarDecl *vd) {
- if (handler) handler->handleUseOfUninitVariable(ex, vd);
+ const VarDecl *vd, bool isAlwaysUnit) {
+ if (handler) handler->handleUseOfUninitVariable(ex, vd, isAlwaysUnit);
}
FindVarResult TransferFunctions::findBlockVarDecl(Expr* ex) {
if (vd->getAttr<BlocksAttr>() || !vd->hasLocalStorage() ||
!isTrackedVar(vd))
continue;
- if (vals[vd] == Uninitialized)
- handler->handleUseOfUninitVariable(be, vd);
+ Value v = vals[vd];
+ if (isUninitialized(v))
+ handler->handleUseOfUninitVariable(be, vd, isAlwaysUninit(v));
}
}
Visit(bo->getLHS());
ValueVector::reference val = vals[vd];
- if (val == Uninitialized) {
+ if (isUninitialized(val)) {
if (bo->getOpcode() != BO_Assign)
- reportUninit(res.getDeclRefExpr(), vd);
+ reportUninit(res.getDeclRefExpr(), vd, isAlwaysUninit(val));
val = Initialized;
}
return;
res.getDeclRefExpr());
Visit(uo->getSubExpr());
- ValueVector::reference bit = vals[vd];
- if (bit == Uninitialized) {
- reportUninit(res.getDeclRefExpr(), vd);
- bit = Initialized;
+ ValueVector::reference val = vals[vd];
+ if (isUninitialized(val)) {
+ reportUninit(res.getDeclRefExpr(), vd, isAlwaysUninit(val));
+ // Don't cascade warnings.
+ val = Initialized;
}
return;
}
SaveAndRestore<const DeclRefExpr*> lastDR(currentDR,
res.getDeclRefExpr());
Visit(ce->getSubExpr());
- if (currentVoidCast != ce && vals[vd] == Uninitialized) {
- reportUninit(res.getDeclRefExpr(), vd);
- // Don't cascade warnings.
- vals[vd] = Initialized;
+ if (currentVoidCast != ce) {
+ Value val = vals[vd];
+ if (isUninitialized(val)) {
+ reportUninit(res.getDeclRefExpr(), vd, isAlwaysUninit(val));
+ // Don't cascade warnings.
+ vals[vd] = Initialized;
+ }
}
return;
}
// -Wuninitialized
//===----------------------------------------------------------------------===//
+typedef std::pair<const Expr*, bool> UninitUse;
+
namespace {
struct SLocSort {
- bool operator()(const Expr *a, const Expr *b) {
- SourceLocation aLoc = a->getLocStart();
- SourceLocation bLoc = b->getLocStart();
+ bool operator()(const UninitUse &a, const UninitUse &b) {
+ SourceLocation aLoc = a.first->getLocStart();
+ SourceLocation bLoc = b.first->getLocStart();
return aLoc.getRawEncoding() < bLoc.getRawEncoding();
}
};
class UninitValsDiagReporter : public UninitVariablesHandler {
Sema &S;
- typedef llvm::SmallVector<const Expr *, 2> UsesVec;
+ typedef llvm::SmallVector<UninitUse, 2> UsesVec;
typedef llvm::DenseMap<const VarDecl *, UsesVec*> UsesMap;
UsesMap *uses;
flushDiagnostics();
}
- void handleUseOfUninitVariable(const Expr *ex, const VarDecl *vd) {
+ void handleUseOfUninitVariable(const Expr *ex, const VarDecl *vd,
+ bool isAlwaysUninit) {
if (!uses)
uses = new UsesMap();
if (!vec)
vec = new UsesVec();
- vec->push_back(ex);
+ vec->push_back(std::make_pair(ex, isAlwaysUninit));
}
void flushDiagnostics() {
for (UsesVec::iterator vi = vec->begin(), ve = vec->end(); vi != ve; ++vi)
{
- if (const DeclRefExpr *dr = dyn_cast<DeclRefExpr>(*vi)) {
- S.Diag(dr->getLocStart(), diag::warn_uninit_var)
+ const bool isAlwaysUninit = vi->second;
+ if (const DeclRefExpr *dr = dyn_cast<DeclRefExpr>(vi->first)) {
+ S.Diag(dr->getLocStart(),
+ isAlwaysUninit ? diag::warn_uninit_var
+ : diag::warn_maybe_uninit_var)
<< vd->getDeclName() << dr->getSourceRange();
}
else {
- const BlockExpr *be = cast<BlockExpr>(*vi);
- S.Diag(be->getLocStart(), diag::warn_uninit_var_captured_by_block)
+ const BlockExpr *be = cast<BlockExpr>(vi->first);
+ S.Diag(be->getLocStart(),
+ isAlwaysUninit ? diag::warn_uninit_var_captured_by_block
+ : diag::warn_maybe_uninit_var_captured_by_block)
<< vd->getDeclName();
}