llvm-project/clang/test/Interpreter/execute-stmts.cpp
Stefan Gränitz 4b70d17bcf
[clang-repl] Names declared in if conditions and for-init statements are local to the inner context (#84150)
Make TopLevelStmtDecl a DeclContext so that variables defined in statements
are attached to the TopLevelDeclContext. This fixes redefinition errors
from variables declared in if conditions and for-init statements. These
must be local to the inner context (C++ 3.3.2p4), but they had generated
definitions on global scope instead.

This PR makes the TopLevelStmtDecl looking more like a FunctionDecl and
that's fine because the FunctionDecl is very close in terms of semantics.

Additionally, ActOnForStmt() requires a CompoundScope when processing a
NullStmt body.

---------

Co-authored-by: Vassil Vassilev <v.g.vassilev@gmail.com>
2024-03-07 14:27:04 +01:00

66 lines
1.5 KiB
C++

// REQUIRES: host-supports-jit
// UNSUPPORTED: system-aix
// RUN: cat %s | clang-repl -Xcc -Xclang -Xcc -verify | FileCheck %s
// RUN: %clang_cc1 -verify -fincremental-extensions -emit-llvm -o - %s \
// RUN: | FileCheck --check-prefix=CODEGEN-CHECK %s
// expected-no-diagnostics
//CODEGEN-CHECK-COUNT-2: define internal void @__stmts__
//CODEGEN-CHECK-NOT: define internal void @__stmts__
extern "C" int printf(const char*,...);
template <typename T> T call() { printf("called\n"); return T(); }
call<int>();
// CHECK: called
int i = 1;
++i;
printf("i = %d\n", i);
// CHECK: i = 2
namespace Ns { void f(){ i++; } }
Ns::f();
void g() { ++i; }
g();
::g();
printf("i = %d\n", i);
// CHECK-NEXT: i = 5
for (; i > 4; --i) printf("i = %d\n", i);
// CHECK-NEXT: i = 5
{++i;}
for (; i > 4; --i) { printf("i = %d\n", i); };
// CHECK-NEXT: i = 5
int j = i; printf("j = %d\n", j);
// CHECK-NEXT: j = 4
{i = 0; printf("i = %d (global scope)\n", i);}
// CHECK-NEXT: i = 0
while (int i = 1) { printf("i = %d (while condition)\n", i--); break; }
// CHECK-NEXT: i = 1
if (int i = 2) printf("i = %d (if condition)\n", i);
// CHECK-NEXT: i = 2
switch (int i = 3) { default: printf("i = %d (switch condition)\n", i); }
// CHECK-NEXT: i = 3
for (int i = 4; i > 3; --i) printf("i = %d (for-init)\n", i);
// CHECK-NEXT: i = 4
for (const auto &i : "5") printf("i = %c (range-based for-init)\n", i);
// CHECK-NEXT: i = 5
int *aa=nullptr;
if (auto *b=aa) *b += 1;
while (auto *b=aa) ;
for (auto *b=aa; b; *b+=1) ;