Skip to content

Commit

Permalink
[clang][Interp] Allow visiting extern variables
Browse files Browse the repository at this point in the history
If the variable is additionally const(expr), visit them like normal
but omit the initializer.
  • Loading branch information
tbaederr committed Mar 14, 2024
1 parent 486332a commit 2cd19df
Show file tree
Hide file tree
Showing 2 changed files with 8 additions and 8 deletions.
9 changes: 2 additions & 7 deletions clang/lib/AST/Interp/ByteCodeExprGen.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2665,18 +2665,12 @@ 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)
return false;

assert(Init);
{
if (Init) {
DeclScope<Emitter> LocalScope(this, VD);

if (VarT) {
Expand All @@ -2686,6 +2680,7 @@ bool ByteCodeExprGen<Emitter>::visitVarDecl(const VarDecl *VD) {
}
return this->visitGlobalInitializer(Init, *GlobalIndex);
}
return true;
} else {
VariableScope<Emitter> LocalScope(this);
if (VarT) {
Expand Down
7 changes: 6 additions & 1 deletion clang/test/AST/Interp/literals.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1190,6 +1190,11 @@ namespace incdecbool {
constexpr int externvar1() { // both-error {{never produces a constant expression}}
extern char arr[]; // ref-note {{declared here}}
return arr[0]; // ref-note {{read of non-constexpr variable 'arr'}} \
// expected-note {{indexing of array without known bound}}
// expected-note {{array-to-pointer decay of array member without known bound is not supported}}
}
#endif

namespace Extern {
constexpr extern char Oops = 1;
static_assert(Oops == 1, "");
}

0 comments on commit 2cd19df

Please sign in to comment.