Skip to content

Commit

Permalink
[cling] Emit const variables only once
Browse files Browse the repository at this point in the history
Otherwise they are emitted as interal and we get double-construction
and -destruction on the same memory address due to the way we promote
interal declarations in KeepLocalGVPass.

Fixes root-project#13429
  • Loading branch information
hahnjo committed Sep 6, 2023
1 parent 18bbc88 commit 2cda91b
Show file tree
Hide file tree
Showing 2 changed files with 48 additions and 0 deletions.
37 changes: 37 additions & 0 deletions interpreter/cling/test/CodeGeneration/const.C
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
//------------------------------------------------------------------------------
// CLING - the C++ LLVM-based InterpreterG :)
//
// This file is dual-licensed: you can choose to license it under the University
// of Illinois Open Source License or the GNU Lesser General Public License. See
// LICENSE.TXT for details.
//------------------------------------------------------------------------------

// RUN: cat %s | %cling 2>&1 | FileCheck %s

extern "C" int printf(const char*, ...);

struct A {
int val;
A(int v) : val(v) {
printf("A(%d), this = %p\n", val, this);
}
~A() {
printf("~A(%d), this = %p\n", val, this);
}
int getVal() const { return val; }
};

const A a(1);
// CHECK: A(1), this = [[PTR:0x[0-9a-f]+]]

a.val
// CHECK-NEXT: (const int) 1
a.getVal()
// CHECK-NEXT: (int) 1
a.val
// CHECK-NEXT: (const int) 1
a.getVal()
// CHECK-NEXT: (int) 1

// CHECK-NEXT: ~A(1), this = [[PTR]]
// CHECK-NOT: ~A
11 changes: 11 additions & 0 deletions interpreter/llvm-project/clang/lib/AST/ASTContext.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -10831,6 +10831,17 @@ GVALinkage ASTContext::GetGVALinkageForFunction(const FunctionDecl *FD) const {

static GVALinkage basicGVALinkageForVariable(const ASTContext &Context,
const VarDecl *VD) {
// As an extension for interactive REPLs, make sure constant variables are
// only emitted once instead of LinkageComputer::getLVForNamespaceScopeDecl
// marking them as internal.
if (Context.getLangOpts().CPlusPlus &&
VD->getType().isConstQualified() &&
!VD->getType().isVolatileQualified() &&
!VD->isInline() &&
!isa<VarTemplateSpecializationDecl>(VD) &&
!VD->getDescribedVarTemplate())
return GVA_DiscardableODR;

if (!VD->isExternallyVisible())
return GVA_Internal;

Expand Down

0 comments on commit 2cda91b

Please sign in to comment.