From d54b6ac2f4f6f0bd0076cbfa885b57277066f06c Mon Sep 17 00:00:00 2001 From: Eli Friedman Date: Wed, 27 May 2009 04:56:12 +0000 Subject: [PATCH] Add IRGen support for return statements in functions with reference type. git-svn-id: https://llvm.org/svn/llvm-project/cfe/trunk@72459 91177308-0d34-0410-b5e6-96231b3b80d8 --- lib/CodeGen/CGStmt.cpp | 4 ++++ test/CodeGenCXX/references.cpp | 3 +++ 2 files changed, 7 insertions(+) diff --git a/lib/CodeGen/CGStmt.cpp b/lib/CodeGen/CGStmt.cpp index b13b9e529d..b67996c676 100644 --- a/lib/CodeGen/CGStmt.cpp +++ b/lib/CodeGen/CGStmt.cpp @@ -498,6 +498,10 @@ void CodeGenFunction::EmitReturnStmt(const ReturnStmt &S) { EmitAnyExpr(RV); } else if (RV == 0) { // Do nothing (return value is left uninitialized) + } else if (FnRetTy->isReferenceType()) { + // If this function returns a reference, take the address of the expression + // rather than the value. + Builder.CreateStore(EmitLValue(RV).getAddress(), ReturnValue); } else if (!hasAggregateLLVMType(RV->getType())) { Builder.CreateStore(EmitScalarExpr(RV), ReturnValue); } else if (RV->getType()->isAnyComplexType()) { diff --git a/test/CodeGenCXX/references.cpp b/test/CodeGenCXX/references.cpp index e5db446fae..5c5518b38b 100644 --- a/test/CodeGenCXX/references.cpp +++ b/test/CodeGenCXX/references.cpp @@ -75,3 +75,6 @@ void test_aggregate() { aggregate_reference_return().a = 10; } +int& reference_return() { + return g; +} -- 2.40.0