aboutsummaryrefslogtreecommitdiff
path: root/clang/unittests/Interpreter/InterpreterTest.cpp
diff options
context:
space:
mode:
authorVassil Vassilev <v.g.vassilev@gmail.com>2024-01-18 16:06:04 +0200
committerGitHub <noreply@github.com>2024-01-18 16:06:04 +0200
commit1566f1ffc6b52bee659071d460123c1c4a358d01 (patch)
treeefc4ab60b9841879dde13bf9d0d52adf3b4fc8d8 /clang/unittests/Interpreter/InterpreterTest.cpp
parente6a6a90fe739d5f792645bf237eb0e540fad8c69 (diff)
downloadllvm-1566f1ffc6b52bee659071d460123c1c4a358d01.zip
llvm-1566f1ffc6b52bee659071d460123c1c4a358d01.tar.gz
llvm-1566f1ffc6b52bee659071d460123c1c4a358d01.tar.bz2
[clang-repl] Add a interpreter-specific overload of operator new for C++ (#76218)
This patch brings back the basic support for C by inserting the required for value printing runtime only when we are in C++ mode. Additionally, it defines a new overload of operator placement new because we can't really forward declare it in a library-agnostic way. Fixes the issue described in llvm/llvm-project#69072.
Diffstat (limited to 'clang/unittests/Interpreter/InterpreterTest.cpp')
-rw-r--r--clang/unittests/Interpreter/InterpreterTest.cpp29
1 files changed, 5 insertions, 24 deletions
diff --git a/clang/unittests/Interpreter/InterpreterTest.cpp b/clang/unittests/Interpreter/InterpreterTest.cpp
index 5f2911e..1e0854b 100644
--- a/clang/unittests/Interpreter/InterpreterTest.cpp
+++ b/clang/unittests/Interpreter/InterpreterTest.cpp
@@ -248,28 +248,10 @@ TEST(IncrementalProcessing, FindMangledNameSymbol) {
#endif // _WIN32
}
-static void *AllocateObject(TypeDecl *TD, Interpreter &Interp) {
+static Value AllocateObject(TypeDecl *TD, Interpreter &Interp) {
std::string Name = TD->getQualifiedNameAsString();
- const clang::Type *RDTy = TD->getTypeForDecl();
- clang::ASTContext &C = Interp.getCompilerInstance()->getASTContext();
- size_t Size = C.getTypeSize(RDTy);
- void *Addr = malloc(Size);
-
- // Tell the interpreter to call the default ctor with this memory. Synthesize:
- // new (loc) ClassName;
- static unsigned Counter = 0;
- std::stringstream SS;
- SS << "auto _v" << Counter++ << " = "
- << "new ((void*)"
- // Windows needs us to prefix the hexadecimal value of a pointer with '0x'.
- << std::hex << std::showbase << (size_t)Addr << ")" << Name << "();";
-
- auto R = Interp.ParseAndExecute(SS.str());
- if (!R) {
- free(Addr);
- return nullptr;
- }
-
+ Value Addr;
+ cantFail(Interp.ParseAndExecute("new " + Name + "()", &Addr));
return Addr;
}
@@ -317,7 +299,7 @@ TEST(IncrementalProcessing, InstantiateTemplate) {
}
TypeDecl *TD = cast<TypeDecl>(LookupSingleName(*Interp, "A"));
- void *NewA = AllocateObject(TD, *Interp);
+ Value NewA = AllocateObject(TD, *Interp);
// Find back the template specialization
VarDecl *VD = static_cast<VarDecl *>(*PTUDeclRange.begin());
@@ -328,8 +310,7 @@ TEST(IncrementalProcessing, InstantiateTemplate) {
typedef int (*TemplateSpecFn)(void *);
auto fn =
cantFail(Interp->getSymbolAddress(MangledName)).toPtr<TemplateSpecFn>();
- EXPECT_EQ(42, fn(NewA));
- free(NewA);
+ EXPECT_EQ(42, fn(NewA.getPtr()));
}
#ifdef CLANG_INTERPRETER_NO_SUPPORT_EXEC