[clang] 57f599d - [clang][Interp] Improve handling of external variables
Timm Bäder via cfe-commits
cfe-commits at lists.llvm.org
Sat Mar 2 09:27:57 PST 2024
Author: Timm Bäder
Date: 2024-03-02T17:40:16+01:00
New Revision: 57f599d6443a910a213094646e7e26837a1d4417
URL: https://github.com/llvm/llvm-project/commit/57f599d6443a910a213094646e7e26837a1d4417
DIFF: https://github.com/llvm/llvm-project/commit/57f599d6443a910a213094646e7e26837a1d4417.diff
LOG: [clang][Interp] Improve handling of external variables
Further down in this function, we assert that the variable has
an initializer, which didn't work for external declarations.
Added:
Modified:
clang/lib/AST/Interp/ByteCodeExprGen.cpp
clang/test/AST/Interp/literals.cpp
Removed:
################################################################################
diff --git a/clang/lib/AST/Interp/ByteCodeExprGen.cpp b/clang/lib/AST/Interp/ByteCodeExprGen.cpp
index 63ab80f59dac46..b4110d5856d512 100644
--- a/clang/lib/AST/Interp/ByteCodeExprGen.cpp
+++ b/clang/lib/AST/Interp/ByteCodeExprGen.cpp
@@ -2663,6 +2663,11 @@ bool ByteCodeExprGen<Emitter>::visitVarDecl(const VarDecl *VD) {
if (P.getGlobal(VD))
return true;
+ // Ignore external declarations. We will instead emit a dummy
+ // pointer when we see a DeclRefExpr for them.
+ if (VD->hasExternalStorage())
+ return true;
+
std::optional<unsigned> GlobalIndex = P.createGlobal(VD, Init);
if (!GlobalIndex)
diff --git a/clang/test/AST/Interp/literals.cpp b/clang/test/AST/Interp/literals.cpp
index 10b687c1408ac3..d86609108ca446 100644
--- a/clang/test/AST/Interp/literals.cpp
+++ b/clang/test/AST/Interp/literals.cpp
@@ -1168,3 +1168,10 @@ namespace incdecbool {
}
+
+#if __cplusplus >= 201402L
+constexpr int externvar1() { // both-error {{never produces a constant expression}}
+ extern char arr[]; // both-note {{declared here}}
+ return arr[0]; // both-note {{read of non-constexpr variable 'arr'}}
+}
+#endif
More information about the cfe-commits
mailing list