[clang] 7b91bb2 - [clang][bytecode] Fix redeclaring global externs without initializer (#164409)

via cfe-commits cfe-commits at lists.llvm.org
Tue Oct 21 06:14:30 PDT 2025


Author: Timm Baeder
Date: 2025-10-21T15:14:26+02:00
New Revision: 7b91bb2046eda8c3ce2a97a70fc2485a0a1fcb96

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

LOG: [clang][bytecode] Fix redeclaring global externs without initializer (#164409)

Return the same value, whether we've already allocated the variable or
not.

Added: 
    

Modified: 
    clang/lib/AST/ByteCode/Compiler.cpp
    clang/test/AST/ByteCode/builtin-functions.cpp

Removed: 
    


################################################################################
diff  --git a/clang/lib/AST/ByteCode/Compiler.cpp b/clang/lib/AST/ByteCode/Compiler.cpp
index 28b9d84f6a76d..f7731f06b664c 100644
--- a/clang/lib/AST/ByteCode/Compiler.cpp
+++ b/clang/lib/AST/ByteCode/Compiler.cpp
@@ -4872,7 +4872,7 @@ Compiler<Emitter>::visitVarDecl(const VarDecl *VD, const Expr *Init,
 
       // The previous attempt at initialization might've been unsuccessful,
       // so let's try this one.
-      return Init && checkDecl() && initGlobal(*GlobalIndex);
+      return !Init || (checkDecl() && initGlobal(*GlobalIndex));
     }
 
     UnsignedOrNone GlobalIndex = P.createGlobal(VD, Init);

diff  --git a/clang/test/AST/ByteCode/builtin-functions.cpp b/clang/test/AST/ByteCode/builtin-functions.cpp
index f47bc49d9a1a8..0b7d51be8d824 100644
--- a/clang/test/AST/ByteCode/builtin-functions.cpp
+++ b/clang/test/AST/ByteCode/builtin-functions.cpp
@@ -63,6 +63,19 @@ constexpr int test_address_of_incomplete_array_type() { // both-error {{never pr
 static_assert(test_address_of_incomplete_array_type() == 1234, ""); // both-error {{constant}} \
                                                                     // both-note {{in call}}
 
+namespace LocalExternRedecl {
+  constexpr int externRedecl1() {
+    extern int arr[];
+    return 0;
+  }
+  constexpr int externRedecl2() { // both-error {{never produces a constant expression}}
+    extern int arr[];
+    __builtin_memmove(&arr, &arr, 4 * sizeof(arr[0])); // both-note 2{{incomplete type}}
+    return 1234;
+  }
+  static_assert(externRedecl2() == 1234); // both-error {{not an integral constant expression}} \
+                                          // both-note {{in call to}}
+}
 
   struct NonTrivial {
     constexpr NonTrivial() : n(0) {}


        


More information about the cfe-commits mailing list