diff options
-rw-r--r-- | clang/lib/Sema/JumpDiagnostics.cpp | 12 | ||||
-rw-r--r-- | clang/test/SemaCXX/scope-check.cpp | 21 |
2 files changed, 27 insertions, 6 deletions
diff --git a/clang/lib/Sema/JumpDiagnostics.cpp b/clang/lib/Sema/JumpDiagnostics.cpp index 8e446f8b9bf..d3de1732766 100644 --- a/clang/lib/Sema/JumpDiagnostics.cpp +++ b/clang/lib/Sema/JumpDiagnostics.cpp @@ -172,10 +172,6 @@ static ScopePair GetDiagForGotoScopeDecl(ASTContext &Context, const Decl *D) { if (EWC) Init = EWC->getSubExpr(); - // FIXME: Why are we looking through reference initialization? - // This causes us to incorrectly accept invalid code such as: - // struct S { int n; }; - // int f() { goto x; S &&s = S(); x: return x.n; } const MaterializeTemporaryExpr *M = NULL; Init = Init->findMaterializedTemporary(M); @@ -184,7 +180,7 @@ static ScopePair GetDiagForGotoScopeDecl(ASTContext &Context, const Decl *D) { Init = Init->skipRValueSubobjectAdjustments(CommaLHSs, Adjustments); QualType QT = Init->getType(); - if (QT.isNull() || !CommaLHSs.empty()) + if (QT.isNull()) return ScopePair(diag::note_protected_by_variable_init, 0); const Type *T = QT.getTypePtr(); @@ -203,7 +199,11 @@ static ScopePair GetDiagForGotoScopeDecl(ASTContext &Context, const Decl *D) { if (const CXXConstructExpr *cce = dyn_cast<CXXConstructExpr>(Init)) { const CXXConstructorDecl *ctor = cce->getConstructor(); - if (ctor->isTrivial() && ctor->isDefaultConstructor()) { + // For a variable declared without an initializer, we will have + // call-style initialization and the initializer will be the + // CXXConstructExpr with no intervening nodes. + if (ctor->isTrivial() && ctor->isDefaultConstructor() && + VD->getInit() == Init && VD->getInitStyle() == VarDecl::CallInit) { if (OutDiag) InDiag = diag::note_protected_by_variable_nontriv_destructor; else if (!Record->isPOD()) diff --git a/clang/test/SemaCXX/scope-check.cpp b/clang/test/SemaCXX/scope-check.cpp index de276ae3d3d..90c9317ecdf 100644 --- a/clang/test/SemaCXX/scope-check.cpp +++ b/clang/test/SemaCXX/scope-check.cpp @@ -276,6 +276,27 @@ namespace test15 { } namespace test16 { + struct S { int n; }; + int f() { + goto x; // expected-error {{goto into protected scope}} + const S &s = S(); // expected-note {{jump bypasses variable initialization}} +x: return s.n; + } +} + +#if __cplusplus >= 201103L +namespace test17 { + struct S { int get(); private: int n; }; + int f() { + goto x; // expected-error {{goto into protected scope}} + S s = {}; // expected-note {{jump bypasses variable initialization}} +x: return s.get(); + } +} +#endif + +// This test must be last, because the error prohibits further jump diagnostics. +namespace testInvalid { Invalid inv; // expected-error {{unknown type name}} // Make sure this doesn't assert. void fn() |