From: Douglas Gregor Date: Mon, 10 Oct 2011 22:41:00 +0000 (+0000) Subject: When performing a user-defined conversion via a constructor, be sure X-Git-Url: https://granicus.if.org/sourcecode?a=commitdiff_plain;h=13e1bca90dc227e1e9c30900841f8bf976c0c83e;p=clang When performing a user-defined conversion via a constructor, be sure to check whether the constructor is accessible. Fixes . git-svn-id: https://llvm.org/svn/llvm-project/cfe/trunk@141588 91177308-0d34-0410-b5e6-96231b3b80d8 --- diff --git a/lib/Sema/SemaExprCXX.cpp b/lib/Sema/SemaExprCXX.cpp index 16d9ae94f2..0db8cd494e 100644 --- a/lib/Sema/SemaExprCXX.cpp +++ b/lib/Sema/SemaExprCXX.cpp @@ -2057,18 +2057,22 @@ static ExprResult BuildCXXCastArgument(Sema &S, switch (Kind) { default: llvm_unreachable("Unhandled cast kind!"); case CK_ConstructorConversion: { + CXXConstructorDecl *Constructor = cast(Method); ASTOwningVector ConstructorArgs(S); - if (S.CompleteConstructorCall(cast(Method), + if (S.CompleteConstructorCall(Constructor, MultiExprArg(&From, 1), CastLoc, ConstructorArgs)) return ExprError(); - ExprResult Result = - S.BuildCXXConstructExpr(CastLoc, Ty, cast(Method), - move_arg(ConstructorArgs), HadMultipleCandidates, - /*ZeroInit*/ false, CXXConstructExpr::CK_Complete, - SourceRange()); + S.CheckConstructorAccess(CastLoc, Constructor, Constructor->getAccess(), + S.PDiag(diag::err_access_ctor)); + + ExprResult Result + = S.BuildCXXConstructExpr(CastLoc, Ty, cast(Method), + move_arg(ConstructorArgs), + HadMultipleCandidates, /*ZeroInit*/ false, + CXXConstructExpr::CK_Complete, SourceRange()); if (Result.isInvalid()) return ExprError(); diff --git a/test/CodeGenCXX/constructor-convert.cpp b/test/CodeGenCXX/constructor-convert.cpp index 9122dae128..7feeaa900a 100644 --- a/test/CodeGenCXX/constructor-convert.cpp +++ b/test/CodeGenCXX/constructor-convert.cpp @@ -2,6 +2,7 @@ // PR5775 class Twine { +public: Twine(const char *Str) { } }; diff --git a/test/SemaCXX/user-defined-conversions.cpp b/test/SemaCXX/user-defined-conversions.cpp index 5de7f44be9..43ec5a3d4a 100644 --- a/test/SemaCXX/user-defined-conversions.cpp +++ b/test/SemaCXX/user-defined-conversions.cpp @@ -82,3 +82,18 @@ float &f(...); void g(X2 b) { int &ir = f(b); // expected-error{{no viable constructor copying parameter of type 'X1'}} } + +namespace rdar10202900 { + class A { + public: + A(); + + private: + A(int i); // expected-note{{declared private here}} + }; + + void testA(A a) { + int b = 10; + a = b; // expected-error{{calling a private constructor of class 'rdar10202900::A'}} + } +}