}
}
- // Sometimes, the expression passed in lacks the casts that are used
- // 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 or reference type.
- // Doing so is a way to silence uninitialized warnings.
- if (isRecordType || isReferenceType)
- if (DeclRefExpr *DRE = dyn_cast<DeclRefExpr>(E))
- HandleDeclRefExpr(DRE);
-
- if (ConditionalOperator *CO = dyn_cast<ConditionalOperator>(E)) {
- HandleValue(CO->getTrueExpr());
- HandleValue(CO->getFalseExpr());
- }
-
- Visit(E);
- }
-
// For most expressions, the cast is directly above the DeclRefExpr.
// For conditional operators, the cast can be outside the conditional
// operator if both expressions are DeclRefExpr's.
void HandleValue(Expr *E) {
+ if (isReferenceType)
+ return;
E = E->IgnoreParenImpCasts();
if (DeclRefExpr* DRE = dyn_cast<DeclRefExpr>(E)) {
HandleDeclRefExpr(DRE);
}
}
+ // Reference types are handled here since all uses of references are
+ // bad, not just r-value uses.
+ void VisitDeclRefExpr(DeclRefExpr *E) {
+ if (isReferenceType)
+ HandleDeclRefExpr(E);
+ }
+
void VisitImplicitCastExpr(ImplicitCastExpr *E) {
if ((!isRecordType && E->getCastKind() == CK_LValueToRValue) ||
(isRecordType && E->getCastKind() == CK_NoOp))
<< DRE->getSourceRange());
}
};
-}
-/// CheckSelfReference - Warns if OrigDecl is used in expression E.
-void Sema::CheckSelfReference(Decl* OrigDecl, Expr *E) {
- SelfReferenceChecker(*this, OrigDecl).HandleExpr(E);
+ /// CheckSelfReference - Warns if OrigDecl is used in expression E.
+ static void CheckSelfReference(Sema &S, Decl* OrigDecl, Expr *E,
+ bool DirectInit) {
+ // Parameters arguments are occassionially constructed with itself,
+ // for instance, in recursive functions. Skip them.
+ if (isa<ParmVarDecl>(OrigDecl))
+ return;
+
+ E = E->IgnoreParens();
+
+ // Skip checking T a = a where T is not a record or reference type.
+ // Doing so is a way to silence uninitialized warnings.
+ if (!DirectInit && !cast<VarDecl>(OrigDecl)->getType()->isRecordType())
+ if (ImplicitCastExpr *ICE = dyn_cast<ImplicitCastExpr>(E))
+ if (ICE->getCastKind() == CK_LValueToRValue)
+ if (DeclRefExpr *DRE = dyn_cast<DeclRefExpr>(ICE->getSubExpr()))
+ if (DRE->getDecl() == OrigDecl)
+ return;
+
+ SelfReferenceChecker(S, OrigDecl).Visit(E);
+ }
}
/// AddInitializerToDecl - Adds the initializer Init to the
return;
}
- // Check for self-references within variable initializers.
- // 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() || VDecl->getType()->isReferenceType()) &&
- (isa<InitListExpr>(Init) || !VDecl->getType()->isRecordType()))
- CheckSelfReference(RealDecl, Init);
-
ParenListExpr *CXXDirectInit = dyn_cast<ParenListExpr>(Init);
// C++11 [decl.spec.auto]p6. Deduce the type which 'auto' stands in for.
Init = Result.takeAs<Expr>();
}
+ // Check for self-references within variable initializers.
+ // Variables declared within a function/method body (except for references)
+ // are handled by a dataflow analysis.
+ if (!VDecl->hasLocalStorage() || VDecl->getType()->isRecordType() ||
+ VDecl->getType()->isReferenceType()) {
+ CheckSelfReference(*this, RealDecl, Init, DirectInit);
+ }
+
// If the type changed, it means we had an incomplete type that was
// completed by the initializer. For example:
// int ary[] = { 1, 3, 5 };
assert((!InitListSyntax || (NumArgs == 1 && isa<InitListExpr>(Args[0]))) &&
"InitListSyntax must come with a single initializer list argument.");
- // Check constructor arguments for self reference.
- if (DeclaratorDecl *DD = Entity.getDecl())
- // Parameters arguments are occassionially constructed with itself,
- // for instance, in recursive functions. Skip them.
- if (!isa<ParmVarDecl>(DD))
- for (unsigned i = 0; i < NumArgs; ++i)
- S.CheckSelfReference(DD, Args[i]);
-
// The type we're constructing needs to be complete.
if (S.RequireCompleteType(Kind.getLocation(), DestType, 0)) {
Sequence.setIncompleteTypeFailure(DestType);
A a17(a17.get2()); // expected-warning {{variable 'a17' is uninitialized when used within its own initialization}}
A a18 = x ? a18 : a17; // expected-warning {{variable 'a18' is uninitialized when used within its own initialization}}
A a19 = getA(x ? a19 : a17); // expected-warning {{variable 'a19' is uninitialized when used within its own initialization}}
+ A a20{a20}; // expected-warning {{variable 'a20' is uninitialized when used within its own initialization}}
+ A a21 = {a21}; // expected-warning {{variable 'a21' is uninitialized when used within its own initialization}}
}
bool x;
A a17(a17.get2()); // expected-warning {{variable 'a17' is uninitialized when used within its own initialization}}
A a18 = x ? a18 : a17; // expected-warning {{variable 'a18' is uninitialized when used within its own initialization}}
A a19 = getA(x ? a19 : a17); // expected-warning {{variable 'a19' is uninitialized when used within its own initialization}}
+A a20{a20}; // expected-warning {{variable 'a20' is uninitialized when used within its own initialization}}
+A a21 = {a21}; // expected-warning {{variable 'a21' is uninitialized when used within its own initialization}}
struct B {
// POD struct.
namespace references {
int &a = a; // expected-warning{{reference 'a' is not yet bound to a value when used within its own initialization}}
+ int &b(b); // expected-warning{{reference 'b' is not yet bound to a value when used within its own initialization}}
+ int &c = a ? b : c; // expected-warning{{reference 'c' is not yet bound to a value when used within its own initialization}}
+ int &d{d}; // expected-warning{{reference 'd' is not yet bound to a value when used within its own initialization}}
struct S {
S() : a(a) {} // expected-warning{{reference 'a' is not yet bound to a value when used here}}
void f() {
int &a = a; // expected-warning{{reference 'a' is not yet bound to a value when used within its own initialization}}
+ int &b(b); // expected-warning{{reference 'b' is not yet bound to a value when used within its own initialization}}
+ int &c = a ? b : c; // expected-warning{{reference 'c' is not yet bound to a value when used within its own initialization}}
+ int &d{d}; // expected-warning{{reference 'd' is not yet bound to a value when used within its own initialization}}
}
struct T {