[clang] eddf9cf - [clang][Interp] Fix failure to allocate local variables

Timm Bäder via cfe-commits cfe-commits at lists.llvm.org
Mon Feb 19 02:54:08 PST 2024


Author: Timm Bäder
Date: 2024-02-19T11:52:13+01:00
New Revision: eddf9cf38a116bcce016f147cab216fef98721ab

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

LOG: [clang][Interp] Fix failure to allocate local variables

We were incorrectly returning true when the allocateLocal() call
failed.

Added: 
    

Modified: 
    clang/lib/AST/Interp/ByteCodeExprGen.cpp
    clang/test/AST/Interp/arrays.cpp

Removed: 
    


################################################################################
diff  --git a/clang/lib/AST/Interp/ByteCodeExprGen.cpp b/clang/lib/AST/Interp/ByteCodeExprGen.cpp
index 4316596bb32a56..e9a58289f6fc07 100644
--- a/clang/lib/AST/Interp/ByteCodeExprGen.cpp
+++ b/clang/lib/AST/Interp/ByteCodeExprGen.cpp
@@ -2749,10 +2749,9 @@ bool ByteCodeExprGen<Emitter>::visitVarDecl(const VarDecl *VD) {
         return this->emitSetLocal(*VarT, Offset, VD);
       }
     } else {
-      if (std::optional<unsigned> Offset = this->allocateLocal(VD)) {
-        if (Init)
-          return this->visitLocalInitializer(Init, *Offset);
-      }
+      if (std::optional<unsigned> Offset = this->allocateLocal(VD))
+        return !Init || this->visitLocalInitializer(Init, *Offset);
+      return false;
     }
     return true;
   }

diff  --git a/clang/test/AST/Interp/arrays.cpp b/clang/test/AST/Interp/arrays.cpp
index a9450d827f6be6..e1af2e80e3ad77 100644
--- a/clang/test/AST/Interp/arrays.cpp
+++ b/clang/test/AST/Interp/arrays.cpp
@@ -545,3 +545,22 @@ namespace LocalIndex {
     array[const_subscript] = 0;  // both-warning {{array index 3 is past the end of the array (that has type 'int[2]')}}
   }
 }
+
+namespace LocalVLA {
+  struct Foo {
+    int x;
+    Foo(int x) : x(x) {}
+  };
+  struct Elidable {
+    Elidable();
+  };
+
+  void foo(int size) {
+    Elidable elidableDynArray[size];
+#if __cplusplus >= 202002L
+     // both-note at -3 {{declared here}}
+     // both-warning at -3 {{variable length array}}
+     // both-note at -4 {{function parameter 'size' with unknown value}}
+#endif
+  }
+}


        


More information about the cfe-commits mailing list