[cfe-commits] r123735 - in /cfe/trunk: include/clang/Basic/DiagnosticSemaKinds.td lib/CodeGen/CGExprCXX.cpp lib/Parse/ParseExpr.cpp lib/Sema/SemaExpr.cpp lib/Sema/SemaOverload.cpp test/CodeGenCXX/constructor-direct-call.cpp test/Parser/MicrosoftExtensions.cpp
Francois Pichet
pichet2000 at gmail.com
Mon Jan 17 21:04:39 PST 2011
Author: fpichet
Date: Mon Jan 17 23:04:39 2011
New Revision: 123735
URL: http://llvm.org/viewvc/llvm-project?rev=123735&view=rev
Log:
Add support for explicit constructor calls in Microsoft mode.
For example:
class A{
public:
A& operator=(const A& that) {
if (this != &that) {
this->A::~A();
this->A::A(that); // <=== explicit constructor call.
}
return *this;
}
};
More work will be needed to support an explicit call to a template constructor.
Added:
cfe/trunk/test/CodeGenCXX/constructor-direct-call.cpp
Modified:
cfe/trunk/include/clang/Basic/DiagnosticSemaKinds.td
cfe/trunk/lib/CodeGen/CGExprCXX.cpp
cfe/trunk/lib/Parse/ParseExpr.cpp
cfe/trunk/lib/Sema/SemaExpr.cpp
cfe/trunk/lib/Sema/SemaOverload.cpp
cfe/trunk/test/Parser/MicrosoftExtensions.cpp
Modified: cfe/trunk/include/clang/Basic/DiagnosticSemaKinds.td
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/include/clang/Basic/DiagnosticSemaKinds.td?rev=123735&r1=123734&r2=123735&view=diff
==============================================================================
--- cfe/trunk/include/clang/Basic/DiagnosticSemaKinds.td (original)
+++ cfe/trunk/include/clang/Basic/DiagnosticSemaKinds.td Mon Jan 17 23:04:39 2011
@@ -738,6 +738,8 @@
"initialize its non-modifiable members">;
def note_refconst_member_not_initialized : Note<
"%select{const|reference}0 member %1 will never be initialized">;
+def ext_ms_explicit_constructor_call : ExtWarn<
+ "explicit constructor calls are a Microsoft extension">, InGroup<Microsoft>;
// C++ destructors
def err_destructor_not_member : Error<
Modified: cfe/trunk/lib/CodeGen/CGExprCXX.cpp
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/lib/CodeGen/CGExprCXX.cpp?rev=123735&r1=123734&r2=123735&view=diff
==============================================================================
--- cfe/trunk/lib/CodeGen/CGExprCXX.cpp (original)
+++ cfe/trunk/lib/CodeGen/CGExprCXX.cpp Mon Jan 17 23:04:39 2011
@@ -93,6 +93,8 @@
return false;
}
+// Note: This function also emit constructor calls to support a MSVC
+// extensions allowing explicit constructor function call.
RValue CodeGenFunction::EmitCXXMemberCallExpr(const CXXMemberCallExpr *CE,
ReturnValueSlot ReturnValue) {
if (isa<BinaryOperator>(CE->getCallee()->IgnoreParens()))
@@ -127,25 +129,42 @@
if (MD->isTrivial()) {
if (isa<CXXDestructorDecl>(MD)) return RValue::get(0);
-
- assert(MD->isCopyAssignmentOperator() && "unknown trivial member function");
- // We don't like to generate the trivial copy assignment operator when
- // it isn't necessary; just produce the proper effect here.
- llvm::Value *RHS = EmitLValue(*CE->arg_begin()).getAddress();
- EmitAggregateCopy(This, RHS, CE->getType());
- return RValue::get(This);
+ if (isa<CXXConstructorDecl>(MD) &&
+ cast<CXXConstructorDecl>(MD)->isDefaultConstructor())
+ return RValue::get(0);
+
+ if (MD->isCopyAssignmentOperator()) {
+ // We don't like to generate the trivial copy assignment operator when
+ // it isn't necessary; just produce the proper effect here.
+ llvm::Value *RHS = EmitLValue(*CE->arg_begin()).getAddress();
+ EmitAggregateCopy(This, RHS, CE->getType());
+ return RValue::get(This);
+ }
+
+ if (isa<CXXConstructorDecl>(MD) &&
+ cast<CXXConstructorDecl>(MD)->isCopyConstructor()) {
+ llvm::Value *RHS = EmitLValue(*CE->arg_begin()).getAddress();
+ EmitSynthesizedCXXCopyCtorCall(cast<CXXConstructorDecl>(MD), This, RHS,
+ CE->arg_begin(), CE->arg_end());
+ return RValue::get(This);
+ }
+ llvm_unreachable("unknown trivial member function");
}
// Compute the function type we're calling.
- const CGFunctionInfo &FInfo =
- (isa<CXXDestructorDecl>(MD)
- ? CGM.getTypes().getFunctionInfo(cast<CXXDestructorDecl>(MD),
- Dtor_Complete)
- : CGM.getTypes().getFunctionInfo(MD));
+ const CGFunctionInfo *FInfo = 0;
+ if (isa<CXXDestructorDecl>(MD))
+ FInfo = &CGM.getTypes().getFunctionInfo(cast<CXXDestructorDecl>(MD),
+ Dtor_Complete);
+ else if (isa<CXXConstructorDecl>(MD))
+ FInfo = &CGM.getTypes().getFunctionInfo(cast<CXXConstructorDecl>(MD),
+ Ctor_Complete);
+ else
+ FInfo = &CGM.getTypes().getFunctionInfo(MD);
const FunctionProtoType *FPT = MD->getType()->getAs<FunctionProtoType>();
const llvm::Type *Ty
- = CGM.getTypes().GetFunctionType(FInfo, FPT->isVariadic());
+ = CGM.getTypes().GetFunctionType(*FInfo, FPT->isVariadic());
// C++ [class.virtual]p12:
// Explicit qualification with the scope operator (5.1) suppresses the
@@ -163,6 +182,9 @@
} else {
Callee = CGM.GetAddrOfFunction(GlobalDecl(Dtor, Dtor_Complete), Ty);
}
+ } else if (const CXXConstructorDecl *Ctor =
+ dyn_cast<CXXConstructorDecl>(MD)) {
+ Callee = CGM.GetAddrOfFunction(GlobalDecl(Ctor, Ctor_Complete), Ty);
} else if (UseVirtualCall) {
Callee = BuildVirtualCall(MD, This, Ty);
} else {
Modified: cfe/trunk/lib/Parse/ParseExpr.cpp
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/lib/Parse/ParseExpr.cpp?rev=123735&r1=123734&r2=123735&view=diff
==============================================================================
--- cfe/trunk/lib/Parse/ParseExpr.cpp (original)
+++ cfe/trunk/lib/Parse/ParseExpr.cpp Mon Jan 17 23:04:39 2011
@@ -1171,11 +1171,13 @@
// expression), or we didn't see a '~' in the right place. We
// can still parse a destructor name here, but in that case it
// names a real destructor.
+ // Allow explicit constructor calls in Microsoft mode.
+ // FIXME: Add support for explicit call of template constructor.
UnqualifiedId Name;
if (ParseUnqualifiedId(SS,
/*EnteringContext=*/false,
/*AllowDestructorName=*/true,
- /*AllowConstructorName=*/false,
+ /*AllowConstructorName=*/ getLang().Microsoft,
ObjectType,
Name))
LHS = ExprError();
Modified: cfe/trunk/lib/Sema/SemaExpr.cpp
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/lib/Sema/SemaExpr.cpp?rev=123735&r1=123734&r2=123735&view=diff
==============================================================================
--- cfe/trunk/lib/Sema/SemaExpr.cpp (original)
+++ cfe/trunk/lib/Sema/SemaExpr.cpp Mon Jan 17 23:04:39 2011
@@ -3902,6 +3902,12 @@
if (SS.isSet() && SS.isInvalid())
return ExprError();
+ // Warn about the explicit constructor calls Microsoft extension.
+ if (getLangOptions().Microsoft &&
+ Id.getKind() == UnqualifiedId::IK_ConstructorName)
+ Diag(Id.getSourceRange().getBegin(),
+ diag::ext_ms_explicit_constructor_call);
+
TemplateArgumentListInfo TemplateArgsBuffer;
// Decompose the name into its component parts.
Modified: cfe/trunk/lib/Sema/SemaOverload.cpp
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/lib/Sema/SemaOverload.cpp?rev=123735&r1=123734&r2=123735&view=diff
==============================================================================
--- cfe/trunk/lib/Sema/SemaOverload.cpp (original)
+++ cfe/trunk/lib/Sema/SemaOverload.cpp Mon Jan 17 23:04:39 2011
@@ -7904,7 +7904,11 @@
if (isa<UsingShadowDecl>(Func))
Func = cast<UsingShadowDecl>(Func)->getTargetDecl();
- if ((Method = dyn_cast<CXXMethodDecl>(Func))) {
+ // Microsoft supports direct constructor calls.
+ if (getLangOptions().Microsoft && isa<CXXConstructorDecl>(Func)) {
+ AddOverloadCandidate(cast<CXXConstructorDecl>(Func), I.getPair(), Args, NumArgs,
+ CandidateSet);
+ } else if ((Method = dyn_cast<CXXMethodDecl>(Func))) {
// If explicit template arguments were provided, we can't call a
// non-template member function.
if (TemplateArgs)
Added: cfe/trunk/test/CodeGenCXX/constructor-direct-call.cpp
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/test/CodeGenCXX/constructor-direct-call.cpp?rev=123735&view=auto
==============================================================================
--- cfe/trunk/test/CodeGenCXX/constructor-direct-call.cpp (added)
+++ cfe/trunk/test/CodeGenCXX/constructor-direct-call.cpp Mon Jan 17 23:04:39 2011
@@ -0,0 +1,60 @@
+// RUN: %clang_cc1 -fms-extensions -Wmicrosoft %s -emit-llvm -o - | FileCheck %s
+
+class Test1 {
+public:
+ int a;
+};
+
+void f1() {
+ Test1 var;
+ var.Test1::Test1();
+
+ // CHECK: call void @llvm.memcpy.p0i8.p0i8.i32(i8* %{{.*}}, i8* %{{.*}}, i32 4, i32 4, i1 false)
+ var.Test1::Test1(var);
+}
+
+class Test2 {
+public:
+ Test2() { a = 10; b = 10; }
+ int a;
+ int b;
+};
+
+void f2() {
+ // CHECK: %var = alloca %class.Test2, align 4
+ // CHECK-NEXT: call void @_ZN5Test2C1Ev(%class.Test2* %var)
+ Test2 var;
+
+ // CHECK-NEXT: call void @_ZN5Test2C1Ev(%class.Test2* %var)
+ var.Test2::Test2();
+
+ // CHECK: call void @llvm.memcpy.p0i8.p0i8.i32(i8* %{{.*}}, i8* %{{.*}}, i32 8, i32 4, i1 false)
+ var.Test2::Test2(var);
+}
+
+
+
+
+class Test3 {
+public:
+ Test3() { a = 10; b = 15; c = 20; }
+ Test3(const Test3& that) { a = that.a; b = that.b; c = that.c; }
+ int a;
+ int b;
+ int c;
+};
+
+void f3() {
+ // CHECK: call void @_ZN5Test3C1Ev(%class.Test3* %var)
+ Test3 var;
+
+ // CHECK-NEXT: call void @_ZN5Test3C1Ev(%class.Test3* %var2)
+ Test3 var2;
+
+ // CHECK-NEXT: call void @_ZN5Test3C1Ev(%class.Test3* %var)
+ var.Test3::Test3();
+
+ // CHECK-NEXT: call void @_ZN5Test3C1ERKS_(%class.Test3* %var, %class.Test3* %var2)
+ var.Test3::Test3(var2);
+}
+
Modified: cfe/trunk/test/Parser/MicrosoftExtensions.cpp
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/test/Parser/MicrosoftExtensions.cpp?rev=123735&r1=123734&r2=123735&view=diff
==============================================================================
--- cfe/trunk/test/Parser/MicrosoftExtensions.cpp (original)
+++ cfe/trunk/test/Parser/MicrosoftExtensions.cpp Mon Jan 17 23:04:39 2011
@@ -85,3 +85,21 @@
__uuidof(T);
__uuidof(expr);
}
+
+
+
+class CtorCall {
+public:
+ CtorCall& operator=(const CtorCall& that);
+
+ int a;
+};
+
+CtorCall& CtorCall::operator=(const CtorCall& that)
+{
+ if (this != &that) {
+ this->CtorCall::~CtorCall();
+ this->CtorCall::CtorCall(that); // expected-warning {{explicit constructor calls are a Microsoft extension}}
+ }
+ return *this;
+}
More information about the cfe-commits
mailing list