[clang] 744a968 - [clang][Interp] Fix return statements with expresssion in void functions

Timm Bäder via cfe-commits cfe-commits at lists.llvm.org
Wed Jul 26 02:54:08 PDT 2023


Author: Timm Bäder
Date: 2023-07-26T11:50:07+02:00
New Revision: 744a968f91f7bb92594a422c1b71f03a47c2415d

URL: https://github.com/llvm/llvm-project/commit/744a968f91f7bb92594a422c1b71f03a47c2415d
DIFF: https://github.com/llvm/llvm-project/commit/744a968f91f7bb92594a422c1b71f03a47c2415d.diff

LOG: [clang][Interp] Fix return statements with expresssion in void functions

If the return type of a function is void, ReturnType is not set, but we
used to emit a RVOPtr instruction, which doesn't make sense for a
function returning void.

Differential Revision: https://reviews.llvm.org/D153649

Added: 
    

Modified: 
    clang/lib/AST/Interp/ByteCodeStmtGen.cpp
    clang/test/AST/Interp/functions.cpp

Removed: 
    


################################################################################
diff  --git a/clang/lib/AST/Interp/ByteCodeStmtGen.cpp b/clang/lib/AST/Interp/ByteCodeStmtGen.cpp
index e5ca5c2ae4c33b..e54805cd931aef 100644
--- a/clang/lib/AST/Interp/ByteCodeStmtGen.cpp
+++ b/clang/lib/AST/Interp/ByteCodeStmtGen.cpp
@@ -309,6 +309,9 @@ bool ByteCodeStmtGen<Emitter>::visitReturnStmt(const ReturnStmt *RS) {
         return false;
       this->emitCleanup();
       return this->emitRet(*ReturnType, RS);
+    } else if (RE->getType()->isVoidType()) {
+      if (!this->visit(RE))
+        return false;
     } else {
       // RVO - construct the value in the return location.
       if (!this->emitRVOPtr(RE))

diff  --git a/clang/test/AST/Interp/functions.cpp b/clang/test/AST/Interp/functions.cpp
index 4bb8791de8f4e5..3540791917fab1 100644
--- a/clang/test/AST/Interp/functions.cpp
+++ b/clang/test/AST/Interp/functions.cpp
@@ -291,3 +291,12 @@ namespace ReturnLocalPtr {
                                  // ref-note {{read of variable whose lifetime has ended}} \
                                  // expected-error {{not an integral constant expression}}
 }
+
+namespace VoidReturn {
+  /// ReturnStmt with an expression in a void function used to cause problems.
+  constexpr void bar() {}
+  constexpr void foo() {
+    return bar();
+  }
+  static_assert((foo(),1) == 1, "");
+}


        


More information about the cfe-commits mailing list