Decl *OrigDecl;
bool isRecordType;
bool isPODType;
+ bool isReferenceType;
public:
typedef EvaluatedExprVisitor<SelfReferenceChecker> Inherited;
S(S), OrigDecl(OrigDecl) {
isPODType = false;
isRecordType = false;
+ isReferenceType = false;
if (ValueDecl *VD = dyn_cast<ValueDecl>(OrigDecl)) {
isPODType = VD->getType().isPODType(S.Context);
isRecordType = VD->getType()->isRecordType();
+ isReferenceType = VD->getType()->isReferenceType();
}
}
// to determine which DeclRefExpr's to check. Assume that the casts
// are present and continue visiting the expression.
void HandleExpr(Expr *E) {
- // Skip checking T a = a where T is not a record type. Doing so is a
- // way to silence uninitialized warnings.
- if (isRecordType)
+ // Skip checking T a = a where T is not a record or reference type.
+ // Doing so is a way to silence uninitialized warnings.
+ if (isRecordType || isReferenceType)
if (DeclRefExpr *DRE = dyn_cast<DeclRefExpr>(E))
HandleDeclRefExpr(DRE);
}
// Check for self-references within variable initializers.
- // Variables declared within a function/method body are handled
- // by a dataflow analysis.
+ // Variables declared within a function/method body (except for references)
+ // are handled by a dataflow analysis.
// Record types initialized by initializer list are handled here.
// Initialization by constructors are handled in TryConstructorInitialization.
- if (!VDecl->hasLocalStorage() &&
+ if ((!VDecl->hasLocalStorage() || VDecl->getType()->isReferenceType()) &&
(isa<InitListExpr>(Init) || !VDecl->getType()->isRecordType()))
CheckSelfReference(RealDecl, Init);
}
}
}
+
+namespace references {
+ int &a = a; // expected-warning{{variable 'a' is uninitialized when used within its own initialization}}
+
+ struct S {
+ S() : a(a) {} // expected-warning{{field is uninitialized when used here}}
+ int &a;
+ };
+
+ void f() {
+ int &a = a; // expected-warning{{variable 'a' is uninitialized when used within its own initialization}}
+ }
+
+ struct T {
+ T() : a(b), b(a) {} // FIXME: Warn here.
+ int &a, &b;
+ int &c = c; // FIXME: Warn here.
+ };
+}