https://github.com/mcbarton updated https://github.com/llvm/llvm-project/pull/150977
>From 43da46e79877361fce099ccbcf84745493c8cfa5 Mon Sep 17 00:00:00 2001 From: mcbarton <150042563+mcbar...@users.noreply.github.com> Date: Mon, 28 Jul 2025 16:33:29 +0100 Subject: [PATCH 1/8] Enable running ClangReplInterpreterTests in an Emscripten environment --- clang/unittests/Interpreter/CMakeLists.txt | 59 +++++++++++++++---- .../Interpreter/CodeCompletionTest.cpp | 2 + .../IncrementalCompilerBuilderTest.cpp | 3 + .../Interpreter/InterpreterExtensionsTest.cpp | 3 +- .../unittests/Interpreter/InterpreterTest.cpp | 9 +++ .../Interpreter/InterpreterTestFixture.h | 2 + llvm/cmake/modules/AddLLVM.cmake | 4 +- 7 files changed, 68 insertions(+), 14 deletions(-) diff --git a/clang/unittests/Interpreter/CMakeLists.txt b/clang/unittests/Interpreter/CMakeLists.txt index 1dda9024075a1..347eb4b639ef3 100644 --- a/clang/unittests/Interpreter/CMakeLists.txt +++ b/clang/unittests/Interpreter/CMakeLists.txt @@ -1,3 +1,34 @@ +if(EMSCRIPTEN) +set(LLVM_COMPONENTS_TO_LINK + "" + ) +set(LLVM_LIBS_TO_LINK + "" + ) +set(CLANG_LIBS_TO_LINK + clangInterpreter + ) +else() +set(LLVM_COMPONENTS_TO_LINK + ${LLVM_TARGETS_TO_BUILD} + Core + MC + OrcJIT + Support + TargetParser + ) +set(LLVM_LIBS_TO_LINK + LLVMTestingSupport + ) +set(CLANG_LIBS_TO_LINK + clangAST + clangBasic + clangInterpreter + clangFrontend + clangSema + ) +endif() + add_distinct_clang_unittest(ClangReplInterpreterTests IncrementalCompilerBuilderTest.cpp IncrementalProcessingTest.cpp @@ -8,24 +39,28 @@ add_distinct_clang_unittest(ClangReplInterpreterTests EXPORT_SYMBOLS CLANG_LIBS - clangAST - clangBasic - clangInterpreter - clangFrontend - clangSema + ${CLANG_LIBS_TO_LINK} LINK_LIBS - LLVMTestingSupport + ${LLVM_LIBS_TO_LINK} LLVM_COMPONENTS - ${LLVM_TARGETS_TO_BUILD} - Core - MC - OrcJIT - Support - TargetParser + ${LLVM_COMPONENTS_TO_LINK} ) +if(EMSCRIPTEN) +target_link_options(ClangReplInterpreterTests + PUBLIC "SHELL: -s MAIN_MODULE=1" + PUBLIC "SHELL: -s ALLOW_MEMORY_GROWTH=1" + PUBLIC "SHELL: -s STACK_SIZE=32mb" + PUBLIC "SHELL: -s INITIAL_MEMORY=128mb" + PUBLIC "SHELL: --emrun" +) +set_target_properties(ClangReplInterpreterTests PROPERTIES + SUFFIX ".html" +) +endif() + # Exceptions on Windows are not yet supported. if(NOT WIN32) add_subdirectory(ExceptionTests) diff --git a/clang/unittests/Interpreter/CodeCompletionTest.cpp b/clang/unittests/Interpreter/CodeCompletionTest.cpp index 23cfc469695d2..813a7d1429251 100644 --- a/clang/unittests/Interpreter/CodeCompletionTest.cpp +++ b/clang/unittests/Interpreter/CodeCompletionTest.cpp @@ -29,8 +29,10 @@ class CodeCompletionTest : public InterpreterTestBase { std::unique_ptr<clang::Interpreter> Interp; void SetUp() override { +#ifndef __EMSCRIPTEN__ if (!HostSupportsJIT()) GTEST_SKIP(); +#endif std::unique_ptr<CompilerInstance> CI = cantFail(CB.CreateCpp()); this->Interp = cantFail(clang::Interpreter::create(std::move(CI))); } diff --git a/clang/unittests/Interpreter/IncrementalCompilerBuilderTest.cpp b/clang/unittests/Interpreter/IncrementalCompilerBuilderTest.cpp index c4a40071f55cf..b67a1f01be4b6 100644 --- a/clang/unittests/Interpreter/IncrementalCompilerBuilderTest.cpp +++ b/clang/unittests/Interpreter/IncrementalCompilerBuilderTest.cpp @@ -37,6 +37,9 @@ TEST(IncrementalCompilerBuilder, SetCompilerArgs) { } TEST(IncrementalCompilerBuilder, SetTargetTriple) { +#ifdef __EMSCRIPTEN__ + GTEST_SKIP() << "Test fails for Emscipten builds"; +#endif auto CB = clang::IncrementalCompilerBuilder(); CB.SetTargetTriple("armv6-none-eabi"); auto CI = cantFail(CB.CreateCpp()); diff --git a/clang/unittests/Interpreter/InterpreterExtensionsTest.cpp b/clang/unittests/Interpreter/InterpreterExtensionsTest.cpp index 1c27cfb2c48fa..7c66e56160e38 100644 --- a/clang/unittests/Interpreter/InterpreterExtensionsTest.cpp +++ b/clang/unittests/Interpreter/InterpreterExtensionsTest.cpp @@ -75,9 +75,10 @@ struct OutOfProcInterpreter : public Interpreter { }; TEST_F(InterpreterExtensionsTest, FindRuntimeInterface) { +#ifndef __EMSCRIPTEN__ if (!HostSupportsJIT()) GTEST_SKIP(); - +#endif clang::IncrementalCompilerBuilder CB; llvm::Error ErrOut = llvm::Error::success(); auto CI = cantFail(CB.CreateCpp()); diff --git a/clang/unittests/Interpreter/InterpreterTest.cpp b/clang/unittests/Interpreter/InterpreterTest.cpp index 768058b954d49..52d2b83fdb36f 100644 --- a/clang/unittests/Interpreter/InterpreterTest.cpp +++ b/clang/unittests/Interpreter/InterpreterTest.cpp @@ -147,6 +147,9 @@ TEST_F(InterpreterTest, DeclsAndStatements) { } TEST_F(InterpreterTest, UndoCommand) { +#ifdef __EMSCRIPTEN__ + GTEST_SKIP() << "Test fails for Emscipten builds"; +#endif Args ExtraArgs = {"-Xclang", "-diagnostic-log-file", "-Xclang", "-"}; // Create the diagnostic engine with unowned consumer. @@ -256,6 +259,9 @@ static NamedDecl *LookupSingleName(Interpreter &Interp, const char *Name) { } TEST_F(InterpreterTest, InstantiateTemplate) { +#ifdef __EMSCRIPTEN__ + GTEST_SKIP() << "Test fails for Emscipten builds"; +#endif // FIXME: We cannot yet handle delayed template parsing. If we run with // -fdelayed-template-parsing we try adding the newly created decl to the // active PTU which causes an assert. @@ -295,6 +301,9 @@ TEST_F(InterpreterTest, InstantiateTemplate) { } TEST_F(InterpreterTest, Value) { +#ifdef __EMSCRIPTEN__ + GTEST_SKIP() << "Test fails for Emscipten builds"; +#endif std::vector<const char *> Args = {"-fno-sized-deallocation"}; std::unique_ptr<Interpreter> Interp = createInterpreter(Args); diff --git a/clang/unittests/Interpreter/InterpreterTestFixture.h b/clang/unittests/Interpreter/InterpreterTestFixture.h index 113599ff98894..8d11c835374bd 100644 --- a/clang/unittests/Interpreter/InterpreterTestFixture.h +++ b/clang/unittests/Interpreter/InterpreterTestFixture.h @@ -38,8 +38,10 @@ class InterpreterTestBase : public ::testing::Test { } void SetUp() override { +#ifndef __EMSCRIPTEN__ if (!HostSupportsJIT()) GTEST_SKIP(); +#endif } void TearDown() override {} diff --git a/llvm/cmake/modules/AddLLVM.cmake b/llvm/cmake/modules/AddLLVM.cmake index 83772ed8d2b13..40614d2b47868 100644 --- a/llvm/cmake/modules/AddLLVM.cmake +++ b/llvm/cmake/modules/AddLLVM.cmake @@ -1764,7 +1764,9 @@ function(add_unittest test_suite test_name) set(LLVM_REQUIRES_RTTI OFF) endif() - list(APPEND LLVM_LINK_COMPONENTS Support) # gtest needs it for raw_ostream + if(NOT EMSCRIPTEN) + list(APPEND LLVM_LINK_COMPONENTS Support) # gtest needs it for raw_ostream + endif() add_llvm_executable(${test_name} IGNORE_EXTERNALIZE_DEBUGINFO NO_INSTALL_RPATH ${ARGN}) get_subproject_title(subproject_title) set_target_properties(${test_name} PROPERTIES FOLDER "${subproject_title}/Tests/Unit") >From 92d02e182ce189530b20142250687baa98bc8655 Mon Sep 17 00:00:00 2001 From: mcbarton <matthew.c.bar...@hotmail.co.uk> Date: Fri, 8 Aug 2025 14:53:06 +0100 Subject: [PATCH 2/8] Fix CMakeLists.txt with endif() --- clang/unittests/Interpreter/CMakeLists.txt | 1 + 1 file changed, 1 insertion(+) diff --git a/clang/unittests/Interpreter/CMakeLists.txt b/clang/unittests/Interpreter/CMakeLists.txt index bca9b30e63a72..f33071c59a0e4 100644 --- a/clang/unittests/Interpreter/CMakeLists.txt +++ b/clang/unittests/Interpreter/CMakeLists.txt @@ -71,6 +71,7 @@ target_link_options(ClangReplInterpreterTests set_target_properties(ClangReplInterpreterTests PROPERTIES SUFFIX ".html" ) +endif() if(TARGET compiler-rt) add_dependencies(ClangReplInterpreterTests >From 8e6d526c2ced7b634926c325202cfa814acf460e Mon Sep 17 00:00:00 2001 From: mcbarton <150042563+mcbar...@users.noreply.github.com> Date: Wed, 13 Aug 2025 10:57:27 +0100 Subject: [PATCH 3/8] Remove LLVMSupport through properties of ClangReplInterpreterTests instead of AddLLVM.cmake modification --- clang/unittests/Interpreter/CMakeLists.txt | 5 +++++ llvm/cmake/modules/AddLLVM.cmake | 4 +--- 2 files changed, 6 insertions(+), 3 deletions(-) diff --git a/clang/unittests/Interpreter/CMakeLists.txt b/clang/unittests/Interpreter/CMakeLists.txt index 347eb4b639ef3..b6825f9461ad7 100644 --- a/clang/unittests/Interpreter/CMakeLists.txt +++ b/clang/unittests/Interpreter/CMakeLists.txt @@ -49,6 +49,11 @@ add_distinct_clang_unittest(ClangReplInterpreterTests ) if(EMSCRIPTEN) +# Without the above you try to link to LLVMSupport twice, and end +# up with a duplicate symbol error when creating the main module +get_target_property(LINKED_LIBS ClangReplInterpreterTests LINK_LIBRARIES) +list(REMOVE_ITEM LINKED_LIBS LLVMSupport) +set_target_properties(ClangReplInterpreterTests PROPERTIES LINK_LIBRARIES "${LINKED_LIBS}") target_link_options(ClangReplInterpreterTests PUBLIC "SHELL: -s MAIN_MODULE=1" PUBLIC "SHELL: -s ALLOW_MEMORY_GROWTH=1" diff --git a/llvm/cmake/modules/AddLLVM.cmake b/llvm/cmake/modules/AddLLVM.cmake index 40614d2b47868..83772ed8d2b13 100644 --- a/llvm/cmake/modules/AddLLVM.cmake +++ b/llvm/cmake/modules/AddLLVM.cmake @@ -1764,9 +1764,7 @@ function(add_unittest test_suite test_name) set(LLVM_REQUIRES_RTTI OFF) endif() - if(NOT EMSCRIPTEN) - list(APPEND LLVM_LINK_COMPONENTS Support) # gtest needs it for raw_ostream - endif() + list(APPEND LLVM_LINK_COMPONENTS Support) # gtest needs it for raw_ostream add_llvm_executable(${test_name} IGNORE_EXTERNALIZE_DEBUGINFO NO_INSTALL_RPATH ${ARGN}) get_subproject_title(subproject_title) set_target_properties(${test_name} PROPERTIES FOLDER "${subproject_title}/Tests/Unit") >From 8779794e25eccb9aec071eb75ed934f0de694c5f Mon Sep 17 00:00:00 2001 From: mcbarton <matthew.c.bar...@hotmail.co.uk> Date: Wed, 13 Aug 2025 19:29:44 +0100 Subject: [PATCH 4/8] Enable InterpreterTestInstantiateTemplate Emscripten build --- clang/unittests/Interpreter/InterpreterTest.cpp | 3 --- 1 file changed, 3 deletions(-) diff --git a/clang/unittests/Interpreter/InterpreterTest.cpp b/clang/unittests/Interpreter/InterpreterTest.cpp index 52d2b83fdb36f..2b6d6dbbe30d8 100644 --- a/clang/unittests/Interpreter/InterpreterTest.cpp +++ b/clang/unittests/Interpreter/InterpreterTest.cpp @@ -259,9 +259,6 @@ static NamedDecl *LookupSingleName(Interpreter &Interp, const char *Name) { } TEST_F(InterpreterTest, InstantiateTemplate) { -#ifdef __EMSCRIPTEN__ - GTEST_SKIP() << "Test fails for Emscipten builds"; -#endif // FIXME: We cannot yet handle delayed template parsing. If we run with // -fdelayed-template-parsing we try adding the newly created decl to the // active PTU which causes an assert. >From 97fee89d4de04ca3dc220d01838dc17ee4272c59 Mon Sep 17 00:00:00 2001 From: mcbarton <150042563+mcbar...@users.noreply.github.com> Date: Wed, 13 Aug 2025 23:18:47 +0100 Subject: [PATCH 5/8] Disable InterpreterTestInstantiateTemplate Emscripten build for PR as failing again after merging main into PR --- clang/unittests/Interpreter/InterpreterTest.cpp | 3 +++ 1 file changed, 3 insertions(+) diff --git a/clang/unittests/Interpreter/InterpreterTest.cpp b/clang/unittests/Interpreter/InterpreterTest.cpp index 2b6d6dbbe30d8..52d2b83fdb36f 100644 --- a/clang/unittests/Interpreter/InterpreterTest.cpp +++ b/clang/unittests/Interpreter/InterpreterTest.cpp @@ -259,6 +259,9 @@ static NamedDecl *LookupSingleName(Interpreter &Interp, const char *Name) { } TEST_F(InterpreterTest, InstantiateTemplate) { +#ifdef __EMSCRIPTEN__ + GTEST_SKIP() << "Test fails for Emscipten builds"; +#endif // FIXME: We cannot yet handle delayed template parsing. If we run with // -fdelayed-template-parsing we try adding the newly created decl to the // active PTU which causes an assert. >From 77f954e1f06ac1d9043d862c9d22a77a6bd56050 Mon Sep 17 00:00:00 2001 From: mcbarton <matthew.c.bar...@hotmail.co.uk> Date: Thu, 14 Aug 2025 08:19:56 +0100 Subject: [PATCH 6/8] Add fixmes --- clang/unittests/Interpreter/CodeCompletionTest.cpp | 4 ++++ .../Interpreter/IncrementalCompilerBuilderTest.cpp | 4 ++++ .../Interpreter/InterpreterExtensionsTest.cpp | 4 ++++ clang/unittests/Interpreter/InterpreterTest.cpp | 12 ++++++++++++ clang/unittests/Interpreter/InterpreterTestFixture.h | 4 ++++ 5 files changed, 28 insertions(+) diff --git a/clang/unittests/Interpreter/CodeCompletionTest.cpp b/clang/unittests/Interpreter/CodeCompletionTest.cpp index 813a7d1429251..1726b06be9e73 100644 --- a/clang/unittests/Interpreter/CodeCompletionTest.cpp +++ b/clang/unittests/Interpreter/CodeCompletionTest.cpp @@ -29,6 +29,10 @@ class CodeCompletionTest : public InterpreterTestBase { std::unique_ptr<clang::Interpreter> Interp; void SetUp() override { +#FIXME : WebAssembly doesn't currently support Jit (see +#https: // github.com/llvm/llvm-project/pull/150977#discussion_r2237521095). +#so this check of HostSupportsJIT has been skipped +#over until support is added, and HostSupportsJIT can return true. #ifndef __EMSCRIPTEN__ if (!HostSupportsJIT()) GTEST_SKIP(); diff --git a/clang/unittests/Interpreter/IncrementalCompilerBuilderTest.cpp b/clang/unittests/Interpreter/IncrementalCompilerBuilderTest.cpp index b67a1f01be4b6..e3aeff367ef51 100644 --- a/clang/unittests/Interpreter/IncrementalCompilerBuilderTest.cpp +++ b/clang/unittests/Interpreter/IncrementalCompilerBuilderTest.cpp @@ -37,6 +37,10 @@ TEST(IncrementalCompilerBuilder, SetCompilerArgs) { } TEST(IncrementalCompilerBuilder, SetTargetTriple) { +#FIXME : This test doesn't current work for Emscripten builds. +#It should be possible to make it work.For details on how it fails and +#the current progress to enable this test see +#the following Github issue https: // github.com/llvm/llvm-project/issues/153461 #ifdef __EMSCRIPTEN__ GTEST_SKIP() << "Test fails for Emscipten builds"; #endif diff --git a/clang/unittests/Interpreter/InterpreterExtensionsTest.cpp b/clang/unittests/Interpreter/InterpreterExtensionsTest.cpp index 7c66e56160e38..3945b40464a5a 100644 --- a/clang/unittests/Interpreter/InterpreterExtensionsTest.cpp +++ b/clang/unittests/Interpreter/InterpreterExtensionsTest.cpp @@ -75,6 +75,10 @@ struct OutOfProcInterpreter : public Interpreter { }; TEST_F(InterpreterExtensionsTest, FindRuntimeInterface) { +#FIXME : WebAssembly doesn't currently support Jit (see +#https: // github.com/llvm/llvm-project/pull/150977#discussion_r2237521095). +#so this check of HostSupportsJIT has been skipped +#over until support is added, and HostSupportsJIT can return true. #ifndef __EMSCRIPTEN__ if (!HostSupportsJIT()) GTEST_SKIP(); diff --git a/clang/unittests/Interpreter/InterpreterTest.cpp b/clang/unittests/Interpreter/InterpreterTest.cpp index 52d2b83fdb36f..c877b6bf0a450 100644 --- a/clang/unittests/Interpreter/InterpreterTest.cpp +++ b/clang/unittests/Interpreter/InterpreterTest.cpp @@ -147,6 +147,10 @@ TEST_F(InterpreterTest, DeclsAndStatements) { } TEST_F(InterpreterTest, UndoCommand) { +#FIXME : This test doesn't current work for Emscripten builds. +#It should be possible to make it work.For details on how it fails and +#the current progress to enable this test see +#the following Github issue https: // github.com/llvm/llvm-project/issues/153461 #ifdef __EMSCRIPTEN__ GTEST_SKIP() << "Test fails for Emscipten builds"; #endif @@ -259,6 +263,10 @@ static NamedDecl *LookupSingleName(Interpreter &Interp, const char *Name) { } TEST_F(InterpreterTest, InstantiateTemplate) { +#FIXME : This test doesn't current work for Emscripten builds. +#It should be possible to make it work.For details on how it fails and +#the current progress to enable this test see +#the following Github issue https: // github.com/llvm/llvm-project/issues/153461 #ifdef __EMSCRIPTEN__ GTEST_SKIP() << "Test fails for Emscipten builds"; #endif @@ -301,6 +309,10 @@ TEST_F(InterpreterTest, InstantiateTemplate) { } TEST_F(InterpreterTest, Value) { +#FIXME : This test doesn't current work for Emscripten builds. +#It should be possible to make it work.For details on how it fails and +#the current progress to enable this test see +#the following Github issue https: // github.com/llvm/llvm-project/issues/153461 #ifdef __EMSCRIPTEN__ GTEST_SKIP() << "Test fails for Emscipten builds"; #endif diff --git a/clang/unittests/Interpreter/InterpreterTestFixture.h b/clang/unittests/Interpreter/InterpreterTestFixture.h index 8d11c835374bd..2708c556761f6 100644 --- a/clang/unittests/Interpreter/InterpreterTestFixture.h +++ b/clang/unittests/Interpreter/InterpreterTestFixture.h @@ -38,6 +38,10 @@ class InterpreterTestBase : public ::testing::Test { } void SetUp() override { +#FIXME : WebAssembly doesn't currently support Jit (see +#https: // github.com/llvm/llvm-project/pull/150977#discussion_r2237521095). +#so this check of HostSupportsJIT has been skipped +#over until support is added, and HostSupportsJIT can return true. #ifndef __EMSCRIPTEN__ if (!HostSupportsJIT()) GTEST_SKIP(); >From e08f20b7bc2aa161768c52563afc3c379c3f740f Mon Sep 17 00:00:00 2001 From: mcbarton <150042563+mcbar...@users.noreply.github.com> Date: Thu, 14 Aug 2025 08:55:59 +0100 Subject: [PATCH 7/8] Fix comments --- .../Interpreter/CodeCompletionTest.cpp | 8 +- .../IncrementalCompilerBuilderTest.cpp | 9 +- .../Interpreter/InterpreterExtensionsTest.cpp | 8 +- .../unittests/Interpreter/InterpreterTest.cpp | 227 +++++++++++++++++- .../Interpreter/InterpreterTestFixture.h | 8 +- 5 files changed, 232 insertions(+), 28 deletions(-) diff --git a/clang/unittests/Interpreter/CodeCompletionTest.cpp b/clang/unittests/Interpreter/CodeCompletionTest.cpp index 1726b06be9e73..ceb683497ac74 100644 --- a/clang/unittests/Interpreter/CodeCompletionTest.cpp +++ b/clang/unittests/Interpreter/CodeCompletionTest.cpp @@ -29,10 +29,10 @@ class CodeCompletionTest : public InterpreterTestBase { std::unique_ptr<clang::Interpreter> Interp; void SetUp() override { -#FIXME : WebAssembly doesn't currently support Jit (see -#https: // github.com/llvm/llvm-project/pull/150977#discussion_r2237521095). -#so this check of HostSupportsJIT has been skipped -#over until support is added, and HostSupportsJIT can return true. +// FIXME : WebAssembly doesn't currently support Jit (see +// https: // github.com/llvm/llvm-project/pull/150977#discussion_r2237521095). +// so this check of HostSupportsJIT has been skipped +// over until support is added, and HostSupportsJIT can return true. #ifndef __EMSCRIPTEN__ if (!HostSupportsJIT()) GTEST_SKIP(); diff --git a/clang/unittests/Interpreter/IncrementalCompilerBuilderTest.cpp b/clang/unittests/Interpreter/IncrementalCompilerBuilderTest.cpp index e3aeff367ef51..7b4633bfc9e7a 100644 --- a/clang/unittests/Interpreter/IncrementalCompilerBuilderTest.cpp +++ b/clang/unittests/Interpreter/IncrementalCompilerBuilderTest.cpp @@ -37,10 +37,11 @@ TEST(IncrementalCompilerBuilder, SetCompilerArgs) { } TEST(IncrementalCompilerBuilder, SetTargetTriple) { -#FIXME : This test doesn't current work for Emscripten builds. -#It should be possible to make it work.For details on how it fails and -#the current progress to enable this test see -#the following Github issue https: // github.com/llvm/llvm-project/issues/153461 +// FIXME : This test doesn't current work for Emscripten builds. +// It should be possible to make it work.For details on how it fails and +// the current progress to enable this test see +// the following Github issue https: // +// github.com/llvm/llvm-project/issues/153461 #ifdef __EMSCRIPTEN__ GTEST_SKIP() << "Test fails for Emscipten builds"; #endif diff --git a/clang/unittests/Interpreter/InterpreterExtensionsTest.cpp b/clang/unittests/Interpreter/InterpreterExtensionsTest.cpp index 3945b40464a5a..f50f6e320776d 100644 --- a/clang/unittests/Interpreter/InterpreterExtensionsTest.cpp +++ b/clang/unittests/Interpreter/InterpreterExtensionsTest.cpp @@ -75,10 +75,10 @@ struct OutOfProcInterpreter : public Interpreter { }; TEST_F(InterpreterExtensionsTest, FindRuntimeInterface) { -#FIXME : WebAssembly doesn't currently support Jit (see -#https: // github.com/llvm/llvm-project/pull/150977#discussion_r2237521095). -#so this check of HostSupportsJIT has been skipped -#over until support is added, and HostSupportsJIT can return true. +// FIXME : WebAssembly doesn't currently support Jit (see +// https: // github.com/llvm/llvm-project/pull/150977#discussion_r2237521095). +// so this check of HostSupportsJIT has been skipped +// over until support is added, and HostSupportsJIT can return true. #ifndef __EMSCRIPTEN__ if (!HostSupportsJIT()) GTEST_SKIP(); diff --git a/clang/unittests/Interpreter/InterpreterTest.cpp b/clang/unittests/Interpreter/InterpreterTest.cpp index c877b6bf0a450..307b1ac40d4e7 100644 --- a/clang/unittests/Interpreter/InterpreterTest.cpp +++ b/clang/unittests/Interpreter/InterpreterTest.cpp @@ -147,10 +147,11 @@ TEST_F(InterpreterTest, DeclsAndStatements) { } TEST_F(InterpreterTest, UndoCommand) { -#FIXME : This test doesn't current work for Emscripten builds. -#It should be possible to make it work.For details on how it fails and -#the current progress to enable this test see -#the following Github issue https: // github.com/llvm/llvm-project/issues/153461 +// FIXME : This test doesn't current work for Emscripten builds. +// It should be possible to make it work.For details on how it fails and +// the current progress to enable this test see +// the following Github issue https: // +// github.com/llvm/llvm-project/issues/153461 #ifdef __EMSCRIPTEN__ GTEST_SKIP() << "Test fails for Emscipten builds"; #endif @@ -263,10 +264,11 @@ static NamedDecl *LookupSingleName(Interpreter &Interp, const char *Name) { } TEST_F(InterpreterTest, InstantiateTemplate) { -#FIXME : This test doesn't current work for Emscripten builds. -#It should be possible to make it work.For details on how it fails and -#the current progress to enable this test see -#the following Github issue https: // github.com/llvm/llvm-project/issues/153461 +// FIXME : This test doesn't current work for Emscripten builds. +// It should be possible to make it work.For details on how it fails and +// the current progress to enable this test see +// the following Github issue https: // +// github.com/llvm/llvm-project/issues/153461 #ifdef __EMSCRIPTEN__ GTEST_SKIP() << "Test fails for Emscipten builds"; #endif @@ -309,10 +311,211 @@ TEST_F(InterpreterTest, InstantiateTemplate) { } TEST_F(InterpreterTest, Value) { -#FIXME : This test doesn't current work for Emscripten builds. -#It should be possible to make it work.For details on how it fails and -#the current progress to enable this test see -#the following Github issue https: // github.com/llvm/llvm-project/issues/153461 + // FIXME : This test doesn't current work for Emscripten builds. + // It should be possible to make it work.For details on how it fails and + // the current progress to enable this test see + // the following Github issue https: // + // github.com/llvm/llvm-project/issues/153461 + ECT_TRUE(!!RecoverErr); +} + +// Here we test whether the user can mix declarations and statements. The +// interpreter should be smart enough to recognize the declarations from the +// statements and wrap the latter into a declaration, producing valid code. + +TEST_F(InterpreterTest, DeclsAndStatements) { + Args ExtraArgs = {"-Xclang", "-diagnostic-log-file", "-Xclang", "-"}; + + // Create the diagnostic engine with unowned consumer. + std::string DiagnosticOutput; + llvm::raw_string_ostream DiagnosticsOS(DiagnosticOutput); + DiagnosticOptions DiagOpts; + auto DiagPrinter = + std::make_unique<TextDiagnosticPrinter>(DiagnosticsOS, DiagOpts); + + auto Interp = createInterpreter(ExtraArgs, DiagPrinter.get()); + auto R1 = Interp->Parse( + "int var1 = 42; extern \"C\" int printf(const char*, ...);"); + // gtest doesn't expand into explicit bool conversions. + EXPECT_TRUE(!!R1); + + auto *PTU1 = R1->TUPart; + EXPECT_EQ(2U, DeclsSize(PTU1)); + + auto R2 = Interp->Parse("var1++; printf(\"var1 value %d\\n\", var1);"); + EXPECT_TRUE(!!R2); +} + +TEST_F(InterpreterTest, UndoCommand) { +// FIXME : This test doesn't current work for Emscripten builds. +// It should be possible to make it work.For details on how it fails and +// the current progress to enable this test see +// the following Github issue https: // +// github.com/llvm/llvm-project/issues/153461 +#ifdef __EMSCRIPTEN__ + GTEST_SKIP() << "Test fails for Emscipten builds"; +#endif + Args ExtraArgs = {"-Xclang", "-diagnostic-log-file", "-Xclang", "-"}; + + // Create the diagnostic engine with unowned consumer. + std::string DiagnosticOutput; + llvm::raw_string_ostream DiagnosticsOS(DiagnosticOutput); + DiagnosticOptions DiagOpts; + auto DiagPrinter = + std::make_unique<TextDiagnosticPrinter>(DiagnosticsOS, DiagOpts); + + auto Interp = createInterpreter(ExtraArgs, DiagPrinter.get()); + + // Fail to undo. + auto Err1 = Interp->Undo(); + EXPECT_EQ("Operation failed. No input left to undo", + llvm::toString(std::move(Err1))); + auto Err2 = Interp->Parse("int foo = 42;"); + EXPECT_TRUE(!!Err2); + auto Err3 = Interp->Undo(2); + EXPECT_EQ("Operation failed. Wanted to undo 2 inputs, only have 1.", + llvm::toString(std::move(Err3))); + + // Succeed to undo. + auto Err4 = Interp->Parse("int x = 42;"); + EXPECT_TRUE(!!Err4); + auto Err5 = Interp->Undo(); + EXPECT_FALSE(Err5); + auto Err6 = Interp->Parse("int x = 24;"); + EXPECT_TRUE(!!Err6); + auto Err7 = Interp->Parse("#define X 42"); + EXPECT_TRUE(!!Err7); + auto Err8 = Interp->Undo(); + EXPECT_FALSE(Err8); + auto Err9 = Interp->Parse("#define X 24"); + EXPECT_TRUE(!!Err9); + + // Undo input contains errors. + auto Err10 = Interp->Parse("int y = ;"); + EXPECT_FALSE(!!Err10); + EXPECT_EQ("Parsing failed.", llvm::toString(Err10.takeError())); + auto Err11 = Interp->Parse("int y = 42;"); + EXPECT_TRUE(!!Err11); + auto Err12 = Interp->Undo(); + EXPECT_FALSE(Err12); +} + +static std::string MangleName(NamedDecl *ND) { + ASTContext &C = ND->getASTContext(); + std::unique_ptr<MangleContext> MangleC(C.createMangleContext()); + std::string mangledName; + llvm::raw_string_ostream RawStr(mangledName); + MangleC->mangleName(ND, RawStr); + return mangledName; +} + +TEST_F(InterpreterTest, FindMangledNameSymbol) { + std::unique_ptr<Interpreter> Interp = createInterpreter(); + + auto &PTU(cantFail(Interp->Parse("int f(const char*) {return 0;}"))); + EXPECT_EQ(1U, DeclsSize(PTU.TUPart)); + auto R1DeclRange = PTU.TUPart->decls(); + + NamedDecl *FD = cast<FunctionDecl>(*R1DeclRange.begin()); + // Lower the PTU + if (llvm::Error Err = Interp->Execute(PTU)) { + // We cannot execute on the platform. + consumeError(std::move(Err)); + return; + } + + std::string MangledName = MangleName(FD); + auto Addr = Interp->getSymbolAddress(MangledName); + EXPECT_FALSE(!Addr); + EXPECT_NE(0U, Addr->getValue()); + GlobalDecl GD(FD); + EXPECT_EQ(*Addr, cantFail(Interp->getSymbolAddress(GD))); + cantFail( + Interp->ParseAndExecute("extern \"C\" int printf(const char*,...);")); + Addr = Interp->getSymbolAddress("printf"); + EXPECT_FALSE(!Addr); + + // FIXME: Re-enable when we investigate the way we handle dllimports on Win. +#ifndef _WIN32 + EXPECT_EQ((uintptr_t)&printf, Addr->getValue()); +#endif // _WIN32 +} + +static Value AllocateObject(TypeDecl *TD, Interpreter &Interp) { + std::string Name = TD->getQualifiedNameAsString(); + Value Addr; + // FIXME: Consider providing an option in clang::Value to take ownership of + // the memory created from the interpreter. + // cantFail(Interp.ParseAndExecute("new " + Name + "()", &Addr)); + + // The lifetime of the temporary is extended by the clang::Value. + cantFail(Interp.ParseAndExecute(Name + "()", &Addr)); + return Addr; +} + +static NamedDecl *LookupSingleName(Interpreter &Interp, const char *Name) { + Sema &SemaRef = Interp.getCompilerInstance()->getSema(); + ASTContext &C = SemaRef.getASTContext(); + DeclarationName DeclName = &C.Idents.get(Name); + LookupResult R(SemaRef, DeclName, SourceLocation(), Sema::LookupOrdinaryName); + SemaRef.LookupName(R, SemaRef.TUScope); + assert(!R.empty()); + return R.getFoundDecl(); +} + +TEST_F(InterpreterTest, InstantiateTemplate) { +// FIXME : This test doesn't current work for Emscripten builds. +// It should be possible to make it work.For details on how it fails and +// the current progress to enable this test see +// the following Github issue https: // +// github.com/llvm/llvm-project/issues/153461 +#ifdef __EMSCRIPTEN__ + GTEST_SKIP() << "Test fails for Emscipten builds"; +#endif + // FIXME: We cannot yet handle delayed template parsing. If we run with + // -fdelayed-template-parsing we try adding the newly created decl to the + // active PTU which causes an assert. + std::vector<const char *> Args = {"-fno-delayed-template-parsing"}; + std::unique_ptr<Interpreter> Interp = createInterpreter(Args); + + llvm::cantFail(Interp->Parse("extern \"C\" int printf(const char*,...);" + "class A {};" + "struct B {" + " template<typename T>" + " static int callme(T) { return 42; }" + "};")); + auto &PTU = llvm::cantFail(Interp->Parse("auto _t = &B::callme<A*>;")); + auto PTUDeclRange = PTU.TUPart->decls(); + EXPECT_EQ(1, std::distance(PTUDeclRange.begin(), PTUDeclRange.end())); + + // Lower the PTU + if (llvm::Error Err = Interp->Execute(PTU)) { + // We cannot execute on the platform. + consumeError(std::move(Err)); + return; + } + + TypeDecl *TD = cast<TypeDecl>(LookupSingleName(*Interp, "A")); + Value NewA = AllocateObject(TD, *Interp); + + // Find back the template specialization + VarDecl *VD = static_cast<VarDecl *>(*PTUDeclRange.begin()); + UnaryOperator *UO = llvm::cast<UnaryOperator>(VD->getInit()); + NamedDecl *TmpltSpec = llvm::cast<DeclRefExpr>(UO->getSubExpr())->getDecl(); + + std::string MangledName = MangleName(TmpltSpec); + typedef int (*TemplateSpecFn)(void *); + auto fn = + cantFail(Interp->getSymbolAddress(MangledName)).toPtr<TemplateSpecFn>(); + EXPECT_EQ(42, fn(NewA.getPtr())); +} + +TEST_F(InterpreterTest, Value) { +// FIXME : This test doesn't current work for Emscripten builds. +// It should be possible to make it work.For details on how it fails and +// the current progress to enable this test see +// the following Github issue https: // +// github.com/llvm/llvm-project/issues/153461 #ifdef __EMSCRIPTEN__ GTEST_SKIP() << "Test fails for Emscipten builds"; #endif diff --git a/clang/unittests/Interpreter/InterpreterTestFixture.h b/clang/unittests/Interpreter/InterpreterTestFixture.h index 2708c556761f6..b088fa4a5f896 100644 --- a/clang/unittests/Interpreter/InterpreterTestFixture.h +++ b/clang/unittests/Interpreter/InterpreterTestFixture.h @@ -38,10 +38,10 @@ class InterpreterTestBase : public ::testing::Test { } void SetUp() override { -#FIXME : WebAssembly doesn't currently support Jit (see -#https: // github.com/llvm/llvm-project/pull/150977#discussion_r2237521095). -#so this check of HostSupportsJIT has been skipped -#over until support is added, and HostSupportsJIT can return true. +// FIXME : WebAssembly doesn't currently support Jit (see +// https: // github.com/llvm/llvm-project/pull/150977#discussion_r2237521095). +// so this check of HostSupportsJIT has been skipped +// over until support is added, and HostSupportsJIT can return true. #ifndef __EMSCRIPTEN__ if (!HostSupportsJIT()) GTEST_SKIP(); >From fab8aae1192ae862473c0094858fb464526467fc Mon Sep 17 00:00:00 2001 From: mcbarton <matthew.c.bar...@hotmail.co.uk> Date: Thu, 14 Aug 2025 09:10:52 +0100 Subject: [PATCH 8/8] Fix diff --- .../unittests/Interpreter/InterpreterTest.cpp | 200 ------------------ 1 file changed, 200 deletions(-) diff --git a/clang/unittests/Interpreter/InterpreterTest.cpp b/clang/unittests/Interpreter/InterpreterTest.cpp index 307b1ac40d4e7..8639fb668f1fe 100644 --- a/clang/unittests/Interpreter/InterpreterTest.cpp +++ b/clang/unittests/Interpreter/InterpreterTest.cpp @@ -310,206 +310,6 @@ TEST_F(InterpreterTest, InstantiateTemplate) { EXPECT_EQ(42, fn(NewA.getPtr())); } -TEST_F(InterpreterTest, Value) { - // FIXME : This test doesn't current work for Emscripten builds. - // It should be possible to make it work.For details on how it fails and - // the current progress to enable this test see - // the following Github issue https: // - // github.com/llvm/llvm-project/issues/153461 - ECT_TRUE(!!RecoverErr); -} - -// Here we test whether the user can mix declarations and statements. The -// interpreter should be smart enough to recognize the declarations from the -// statements and wrap the latter into a declaration, producing valid code. - -TEST_F(InterpreterTest, DeclsAndStatements) { - Args ExtraArgs = {"-Xclang", "-diagnostic-log-file", "-Xclang", "-"}; - - // Create the diagnostic engine with unowned consumer. - std::string DiagnosticOutput; - llvm::raw_string_ostream DiagnosticsOS(DiagnosticOutput); - DiagnosticOptions DiagOpts; - auto DiagPrinter = - std::make_unique<TextDiagnosticPrinter>(DiagnosticsOS, DiagOpts); - - auto Interp = createInterpreter(ExtraArgs, DiagPrinter.get()); - auto R1 = Interp->Parse( - "int var1 = 42; extern \"C\" int printf(const char*, ...);"); - // gtest doesn't expand into explicit bool conversions. - EXPECT_TRUE(!!R1); - - auto *PTU1 = R1->TUPart; - EXPECT_EQ(2U, DeclsSize(PTU1)); - - auto R2 = Interp->Parse("var1++; printf(\"var1 value %d\\n\", var1);"); - EXPECT_TRUE(!!R2); -} - -TEST_F(InterpreterTest, UndoCommand) { -// FIXME : This test doesn't current work for Emscripten builds. -// It should be possible to make it work.For details on how it fails and -// the current progress to enable this test see -// the following Github issue https: // -// github.com/llvm/llvm-project/issues/153461 -#ifdef __EMSCRIPTEN__ - GTEST_SKIP() << "Test fails for Emscipten builds"; -#endif - Args ExtraArgs = {"-Xclang", "-diagnostic-log-file", "-Xclang", "-"}; - - // Create the diagnostic engine with unowned consumer. - std::string DiagnosticOutput; - llvm::raw_string_ostream DiagnosticsOS(DiagnosticOutput); - DiagnosticOptions DiagOpts; - auto DiagPrinter = - std::make_unique<TextDiagnosticPrinter>(DiagnosticsOS, DiagOpts); - - auto Interp = createInterpreter(ExtraArgs, DiagPrinter.get()); - - // Fail to undo. - auto Err1 = Interp->Undo(); - EXPECT_EQ("Operation failed. No input left to undo", - llvm::toString(std::move(Err1))); - auto Err2 = Interp->Parse("int foo = 42;"); - EXPECT_TRUE(!!Err2); - auto Err3 = Interp->Undo(2); - EXPECT_EQ("Operation failed. Wanted to undo 2 inputs, only have 1.", - llvm::toString(std::move(Err3))); - - // Succeed to undo. - auto Err4 = Interp->Parse("int x = 42;"); - EXPECT_TRUE(!!Err4); - auto Err5 = Interp->Undo(); - EXPECT_FALSE(Err5); - auto Err6 = Interp->Parse("int x = 24;"); - EXPECT_TRUE(!!Err6); - auto Err7 = Interp->Parse("#define X 42"); - EXPECT_TRUE(!!Err7); - auto Err8 = Interp->Undo(); - EXPECT_FALSE(Err8); - auto Err9 = Interp->Parse("#define X 24"); - EXPECT_TRUE(!!Err9); - - // Undo input contains errors. - auto Err10 = Interp->Parse("int y = ;"); - EXPECT_FALSE(!!Err10); - EXPECT_EQ("Parsing failed.", llvm::toString(Err10.takeError())); - auto Err11 = Interp->Parse("int y = 42;"); - EXPECT_TRUE(!!Err11); - auto Err12 = Interp->Undo(); - EXPECT_FALSE(Err12); -} - -static std::string MangleName(NamedDecl *ND) { - ASTContext &C = ND->getASTContext(); - std::unique_ptr<MangleContext> MangleC(C.createMangleContext()); - std::string mangledName; - llvm::raw_string_ostream RawStr(mangledName); - MangleC->mangleName(ND, RawStr); - return mangledName; -} - -TEST_F(InterpreterTest, FindMangledNameSymbol) { - std::unique_ptr<Interpreter> Interp = createInterpreter(); - - auto &PTU(cantFail(Interp->Parse("int f(const char*) {return 0;}"))); - EXPECT_EQ(1U, DeclsSize(PTU.TUPart)); - auto R1DeclRange = PTU.TUPart->decls(); - - NamedDecl *FD = cast<FunctionDecl>(*R1DeclRange.begin()); - // Lower the PTU - if (llvm::Error Err = Interp->Execute(PTU)) { - // We cannot execute on the platform. - consumeError(std::move(Err)); - return; - } - - std::string MangledName = MangleName(FD); - auto Addr = Interp->getSymbolAddress(MangledName); - EXPECT_FALSE(!Addr); - EXPECT_NE(0U, Addr->getValue()); - GlobalDecl GD(FD); - EXPECT_EQ(*Addr, cantFail(Interp->getSymbolAddress(GD))); - cantFail( - Interp->ParseAndExecute("extern \"C\" int printf(const char*,...);")); - Addr = Interp->getSymbolAddress("printf"); - EXPECT_FALSE(!Addr); - - // FIXME: Re-enable when we investigate the way we handle dllimports on Win. -#ifndef _WIN32 - EXPECT_EQ((uintptr_t)&printf, Addr->getValue()); -#endif // _WIN32 -} - -static Value AllocateObject(TypeDecl *TD, Interpreter &Interp) { - std::string Name = TD->getQualifiedNameAsString(); - Value Addr; - // FIXME: Consider providing an option in clang::Value to take ownership of - // the memory created from the interpreter. - // cantFail(Interp.ParseAndExecute("new " + Name + "()", &Addr)); - - // The lifetime of the temporary is extended by the clang::Value. - cantFail(Interp.ParseAndExecute(Name + "()", &Addr)); - return Addr; -} - -static NamedDecl *LookupSingleName(Interpreter &Interp, const char *Name) { - Sema &SemaRef = Interp.getCompilerInstance()->getSema(); - ASTContext &C = SemaRef.getASTContext(); - DeclarationName DeclName = &C.Idents.get(Name); - LookupResult R(SemaRef, DeclName, SourceLocation(), Sema::LookupOrdinaryName); - SemaRef.LookupName(R, SemaRef.TUScope); - assert(!R.empty()); - return R.getFoundDecl(); -} - -TEST_F(InterpreterTest, InstantiateTemplate) { -// FIXME : This test doesn't current work for Emscripten builds. -// It should be possible to make it work.For details on how it fails and -// the current progress to enable this test see -// the following Github issue https: // -// github.com/llvm/llvm-project/issues/153461 -#ifdef __EMSCRIPTEN__ - GTEST_SKIP() << "Test fails for Emscipten builds"; -#endif - // FIXME: We cannot yet handle delayed template parsing. If we run with - // -fdelayed-template-parsing we try adding the newly created decl to the - // active PTU which causes an assert. - std::vector<const char *> Args = {"-fno-delayed-template-parsing"}; - std::unique_ptr<Interpreter> Interp = createInterpreter(Args); - - llvm::cantFail(Interp->Parse("extern \"C\" int printf(const char*,...);" - "class A {};" - "struct B {" - " template<typename T>" - " static int callme(T) { return 42; }" - "};")); - auto &PTU = llvm::cantFail(Interp->Parse("auto _t = &B::callme<A*>;")); - auto PTUDeclRange = PTU.TUPart->decls(); - EXPECT_EQ(1, std::distance(PTUDeclRange.begin(), PTUDeclRange.end())); - - // Lower the PTU - if (llvm::Error Err = Interp->Execute(PTU)) { - // We cannot execute on the platform. - consumeError(std::move(Err)); - return; - } - - TypeDecl *TD = cast<TypeDecl>(LookupSingleName(*Interp, "A")); - Value NewA = AllocateObject(TD, *Interp); - - // Find back the template specialization - VarDecl *VD = static_cast<VarDecl *>(*PTUDeclRange.begin()); - UnaryOperator *UO = llvm::cast<UnaryOperator>(VD->getInit()); - NamedDecl *TmpltSpec = llvm::cast<DeclRefExpr>(UO->getSubExpr())->getDecl(); - - std::string MangledName = MangleName(TmpltSpec); - typedef int (*TemplateSpecFn)(void *); - auto fn = - cantFail(Interp->getSymbolAddress(MangledName)).toPtr<TemplateSpecFn>(); - EXPECT_EQ(42, fn(NewA.getPtr())); -} - TEST_F(InterpreterTest, Value) { // FIXME : This test doesn't current work for Emscripten builds. // It should be possible to make it work.For details on how it fails and _______________________________________________ cfe-commits mailing list cfe-commits@lists.llvm.org https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits