From 81d309f6f698992ed79b96c274a162f3b2918885 Mon Sep 17 00:00:00 2001 From: qibao Date: Mon, 30 Dec 2024 15:11:59 +0800 Subject: [PATCH] Add plugins API to provide do print compiling msg Issue:https://gitee.com/openharmony/arkcompiler_ets_frontend/issues/IBD7PU?from=project-issue Reason: Plugins API can not print compiling msg Description: Provide 3 interface LogTypeError/LogWarning/LogSyntaxError to print compiler result msg Tests: ninja all tests ninja es2panda-plugin-test Signed-off-by: qibao --- ets2panda/public/es2panda_lib.cpp | 30 ++++++- ets2panda/public/es2panda_lib.h | 5 +- ets2panda/test/unit/plugin/CMakeLists.txt | 9 +- .../unit/plugin/plugin_proceed_to_state.cpp | 49 ++-------- ...in_proceed_to_state_change_call_lambda.cpp | 48 ++-------- .../plugin_proceed_to_state_change_func.cpp | 48 ++-------- ...oceed_to_state_find_import_declaration.cpp | 49 ++-------- ...roceed_to_state_test_annotation_change.cpp | 34 ++----- ...gin_proceed_to_state_update_statements.cpp | 49 ++-------- ...ceed_to_state_update_statements_lambda.cpp | 49 ++-------- .../plugin/plugin_test_print_err_msg_func.cpp | 89 +++++++++++++++++++ ets2panda/test/unit/plugin/util.cpp | 61 +++++++++++++ ets2panda/test/unit/plugin/util.h | 32 +++++++ 13 files changed, 257 insertions(+), 295 deletions(-) create mode 100644 ets2panda/test/unit/plugin/plugin_test_print_err_msg_func.cpp create mode 100644 ets2panda/test/unit/plugin/util.cpp create mode 100644 ets2panda/test/unit/plugin/util.h diff --git a/ets2panda/public/es2panda_lib.cpp b/ets2panda/public/es2panda_lib.cpp index c7d7cc8926..8936a2df43 100644 --- a/ets2panda/public/es2panda_lib.cpp +++ b/ets2panda/public/es2panda_lib.cpp @@ -1,5 +1,5 @@ /* - * Copyright (c) 2024 Huawei Device Co., Ltd. + * Copyright (c) 2025 Huawei Device Co., Ltd. * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at @@ -661,6 +661,31 @@ extern "C" es2panda_SourceRange *CreateSourceRange(es2panda_Context *context, es return reinterpret_cast(allocator->New(startE2p, endE2p)); } +extern "C" void LogTypeError(es2panda_Context *context, const char *errorMsg, es2panda_SourcePosition *pos) +{ + auto ctx = reinterpret_cast(context); + auto posE2p = *(reinterpret_cast(pos)); + ctx->checker->Initialize(ctx->parserProgram->VarBinder()); + ctx->checker->ErrorLogger()->SetOstream(&std::cerr); + ctx->checker->LogTypeError(errorMsg, posE2p); +} + +extern "C" void LogWarning(es2panda_Context *context, const char *warnMsg, es2panda_SourcePosition *pos) +{ + auto ctx = reinterpret_cast(context); + auto posE2p = *(reinterpret_cast(pos)); + ctx->checker->Initialize(ctx->parserProgram->VarBinder()); + ctx->checker->ErrorLogger()->SetOstream(&std::cout); + ctx->checker->Warning(warnMsg, posE2p); +} + +extern "C" void LogSyntaxError(es2panda_Context *context, const char *errorMsg, es2panda_SourcePosition *pos) +{ + auto *parser = reinterpret_cast(context)->parser; + auto posE2p = *(reinterpret_cast(pos)); + parser->LogSyntaxError(errorMsg, posE2p); +} + extern "C" size_t SourcePositionIndex([[maybe_unused]] es2panda_Context *context, es2panda_SourcePosition *position) { return reinterpret_cast(position)->index; @@ -717,6 +742,9 @@ es2panda_Impl g_impl = { SourcePositionLine, SourceRangeStart, SourceRangeEnd, + LogTypeError, + LogWarning, + LogSyntaxError, #include "generated/es2panda_lib/es2panda_lib_list.inc" diff --git a/ets2panda/public/es2panda_lib.h b/ets2panda/public/es2panda_lib.h index 9608bc7223..042f905bcc 100644 --- a/ets2panda/public/es2panda_lib.h +++ b/ets2panda/public/es2panda_lib.h @@ -1,5 +1,5 @@ /** - * Copyright (c) 2024 Huawei Device Co., Ltd. + * Copyright (c) 2025 Huawei Device Co., Ltd. * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at @@ -156,6 +156,9 @@ struct es2panda_Impl { size_t (*SourcePositionLine)(es2panda_Context *context, es2panda_SourcePosition *position); es2panda_SourcePosition *(*SourceRangeStart)(es2panda_Context *context, es2panda_SourceRange *range); es2panda_SourcePosition *(*SourceRangeEnd)(es2panda_Context *context, es2panda_SourceRange *range); + void (*LogTypeError)(es2panda_Context *context, const char *errorMsg, es2panda_SourcePosition *pos); + void (*LogWarning)(es2panda_Context *context, const char *warnMsg, es2panda_SourcePosition *pos); + void (*LogSyntaxError)(es2panda_Context *context, const char *errorMsg, es2panda_SourcePosition *pos); // CC-OFFNXT(G.INC.08) project code style #include "generated/es2panda_lib/es2panda_lib_decl.inc" diff --git a/ets2panda/test/unit/plugin/CMakeLists.txt b/ets2panda/test/unit/plugin/CMakeLists.txt index e62fe623df..550fb14b0b 100644 --- a/ets2panda/test/unit/plugin/CMakeLists.txt +++ b/ets2panda/test/unit/plugin/CMakeLists.txt @@ -25,6 +25,10 @@ set(EXPECTED_MODE "EXPECTED") set(LIBRARY_PLUGIN "LIB") set(EXECUTABLE_PLUGIN "EXE") +set(COMMON_SOURCE_FILES + "util.cpp" +) + set(PLUGIN_TESTS #"test_name test_sts_file how_to_test_mode plugin_file_extension(c|cpp) how_to_compile_mode" "e2p_test_plugin_dump_json compile.sts ${RUNTIME_MODE} c ${LIBRARY_PLUGIN}" @@ -43,6 +47,7 @@ set(PLUGIN_TESTS "plugin_proceed_to_state_change_func runtime_change_func_call.sts ${RUNTIME_MODE} cpp ${EXECUTABLE_PLUGIN}" "plugin_proceed_to_state_change_call_lambda compile.sts ${COMPILE_MODE} cpp ${EXECUTABLE_PLUGIN}" "plugin_proceed_to_state_test_annotation_change compile.sts ${COMPILE_MODE} cpp ${EXECUTABLE_PLUGIN}" + "plugin_test_print_err_msg_func compile.sts ${COMPILE_MODE} cpp ${EXECUTABLE_PLUGIN}" ) set(RUNTIME_ARGUMENTS @@ -59,9 +64,9 @@ foreach(TEST_DATA IN ITEMS ${PLUGIN_TESTS}) list(GET TEST_DATA_ELEM 3 EXTENSION) list(GET TEST_DATA_ELEM 4 PLUGIN_MODE) if(${PLUGIN_MODE} STREQUAL ${LIBRARY_PLUGIN}) - panda_add_library(${TEST_NAME} SHARED ${TEST_NAME}.${EXTENSION}) + panda_add_library(${TEST_NAME} SHARED ${TEST_NAME}.${EXTENSION} ${COMMON_SOURCE_FILES}) else() - panda_add_executable(${TEST_NAME} ${TEST_NAME}.${EXTENSION} OUTPUT_DIRECTORY ${CMAKE_CURRENT_BINARY_DIR}) + panda_add_executable(${TEST_NAME} ${TEST_NAME}.${EXTENSION} ${COMMON_SOURCE_FILES} OUTPUT_DIRECTORY ${CMAKE_CURRENT_BINARY_DIR}) panda_add_sanitizers(TARGET ${TEST_NAME} SANITIZERS ${PANDA_SANITIZERS_LIST}) endif() panda_target_include_directories(${TEST_NAME} diff --git a/ets2panda/test/unit/plugin/plugin_proceed_to_state.cpp b/ets2panda/test/unit/plugin/plugin_proceed_to_state.cpp index aa01e0db1a..0658ff3f53 100644 --- a/ets2panda/test/unit/plugin/plugin_proceed_to_state.cpp +++ b/ets2panda/test/unit/plugin/plugin_proceed_to_state.cpp @@ -1,5 +1,5 @@ /** - * Copyright (c) 2024 Huawei Device Co., Ltd. + * Copyright (c) 2025 Huawei Device Co., Ltd. * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at @@ -16,55 +16,16 @@ #include #include #include -#include "public/es2panda_lib.h" + #include "os/library_loader.h" -// NOLINTBEGIN +#include "public/es2panda_lib.h" +#include "util.h" -static const char *LIBNAME = "es2panda-public"; -static const int MIN_ARGC = 3; -static const int NULLPTR_IMPL_ERROR_CODE = 2; +// NOLINTBEGIN static es2panda_Impl *impl = nullptr; -es2panda_Impl *GetImpl() -{ - if (impl != nullptr) { - return impl; - } - - std::string soName = ark::os::library_loader::DYNAMIC_LIBRARY_PREFIX + std::string(LIBNAME) + - ark::os::library_loader::DYNAMIC_LIBRARY_SUFFIX; - auto libraryRes = ark::os::library_loader::Load(soName); - if (!libraryRes.HasValue()) { - std::cout << "Error in load lib" << std::endl; - return nullptr; - } - - auto library = std::move(libraryRes.Value()); - auto getImpl = ark::os::library_loader::ResolveSymbol(library, "es2panda_GetImpl"); - if (!getImpl.HasValue()) { - std::cout << "Error in load func get impl" << std::endl; - return nullptr; - } - - auto getImplFunc = reinterpret_cast(getImpl.Value()); - if (getImplFunc != nullptr) { - return const_cast(getImplFunc(ES2PANDA_LIB_VERSION)); - } - return nullptr; -} - -void CheckForErrors(std::string StateName, es2panda_Context *context) -{ - if (impl->ContextState(context) == ES2PANDA_STATE_ERROR) { - std::cout << "PROCEED TO " << StateName << " ERROR" << std::endl; - std::cout << impl->ContextErrorMessage << std::endl; - } else { - std::cout << "PROCEED TO " << StateName << " SUCCESS" << std::endl; - } -} - int main(int argc, char **argv) { if (argc < MIN_ARGC) { diff --git a/ets2panda/test/unit/plugin/plugin_proceed_to_state_change_call_lambda.cpp b/ets2panda/test/unit/plugin/plugin_proceed_to_state_change_call_lambda.cpp index 011dd27527..114458623d 100644 --- a/ets2panda/test/unit/plugin/plugin_proceed_to_state_change_call_lambda.cpp +++ b/ets2panda/test/unit/plugin/plugin_proceed_to_state_change_call_lambda.cpp @@ -1,5 +1,5 @@ /** - * Copyright (c) 2024 Huawei Device Co., Ltd. + * Copyright (c) 2025 Huawei Device Co., Ltd. * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at @@ -17,46 +17,18 @@ #include #include #include -#include "public/es2panda_lib.h" + #include "os/library_loader.h" +#include "public/es2panda_lib.h" +#include "util.h" + // NOLINTBEGIN -static const char *LIBNAME = "es2panda-public"; -static const int MIN_ARGC = 3; -static const int NULLPTR_IMPL_ERROR_CODE = 2; static const std::string funcName = "foo"; static es2panda_Impl *impl = nullptr; -es2panda_Impl *GetImpl() -{ - if (impl != nullptr) { - return impl; - } - - std::string soName = ark::os::library_loader::DYNAMIC_LIBRARY_PREFIX + std::string(LIBNAME) + - ark::os::library_loader::DYNAMIC_LIBRARY_SUFFIX; - auto libraryRes = ark::os::library_loader::Load(soName); - if (!libraryRes.HasValue()) { - std::cout << "Error in load lib" << std::endl; - return nullptr; - } - - auto library = std::move(libraryRes.Value()); - auto getImpl = ark::os::library_loader::ResolveSymbol(library, "es2panda_GetImpl"); - if (!getImpl.HasValue()) { - std::cout << "Error in load func get impl" << std::endl; - return nullptr; - } - - auto getImplFunc = reinterpret_cast(getImpl.Value()); - if (getImplFunc != nullptr) { - return const_cast(getImplFunc(ES2PANDA_LIB_VERSION)); - } - return nullptr; -} - es2panda_AstNode *parNode; es2panda_Context *newCtx; @@ -101,16 +73,6 @@ static void UpdateCall(es2panda_AstNode *ast, es2panda_Context *ctx) impl->AstNodeForEach(ast, SetRightParent, ctx); } -void CheckForErrors(std::string StateName, es2panda_Context *context) -{ - if (impl->ContextState(context) == ES2PANDA_STATE_ERROR) { - std::cout << "PROCEED TO " << StateName << " ERROR" << std::endl; - std::cout << impl->ContextErrorMessage << std::endl; - } else { - std::cout << "PROCEED TO " << StateName << " SUCCESS" << std::endl; - } -} - int main(int argc, char **argv) { if (argc < MIN_ARGC) { diff --git a/ets2panda/test/unit/plugin/plugin_proceed_to_state_change_func.cpp b/ets2panda/test/unit/plugin/plugin_proceed_to_state_change_func.cpp index ea08cf5450..1be29522fd 100644 --- a/ets2panda/test/unit/plugin/plugin_proceed_to_state_change_func.cpp +++ b/ets2panda/test/unit/plugin/plugin_proceed_to_state_change_func.cpp @@ -1,5 +1,5 @@ /** - * Copyright (c) 2024 Huawei Device Co., Ltd. + * Copyright (c) 2025 Huawei Device Co., Ltd. * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at @@ -17,14 +17,14 @@ #include #include #include -#include "public/es2panda_lib.h" + #include "os/library_loader.h" +#include "public/es2panda_lib.h" +#include "util.h" + // NOLINTBEGIN -static const char *LIBNAME = "es2panda-public"; -static const int MIN_ARGC = 3; -static const int NULLPTR_IMPL_ERROR_CODE = 2; const int CANT_FIND_FUNC_ERROR = 3; static const std::string mainName = "main"; static const std::string funcName1 = "foo"; @@ -32,44 +32,6 @@ static const std::string funcName2 = "goo"; static es2panda_Impl *impl = nullptr; -void CheckForErrors(std::string StateName, es2panda_Context *context) -{ - if (impl->ContextState(context) == ES2PANDA_STATE_ERROR) { - std::cout << "PROCEED TO " << StateName << " ERROR" << std::endl; - std::cout << impl->ContextErrorMessage << std::endl; - } else { - std::cout << "PROCEED TO " << StateName << " SUCCESS" << std::endl; - } -} - -es2panda_Impl *GetImpl() -{ - if (impl != nullptr) { - return impl; - } - - std::string soName = ark::os::library_loader::DYNAMIC_LIBRARY_PREFIX + std::string(LIBNAME) + - ark::os::library_loader::DYNAMIC_LIBRARY_SUFFIX; - auto libraryRes = ark::os::library_loader::Load(soName); - if (!libraryRes.HasValue()) { - std::cout << "Error in load lib" << std::endl; - return nullptr; - } - - auto library = std::move(libraryRes.Value()); - auto getImpl = ark::os::library_loader::ResolveSymbol(library, "es2panda_GetImpl"); - if (!getImpl.HasValue()) { - std::cout << "Error in load func get impl" << std::endl; - return nullptr; - } - - auto getImplFunc = reinterpret_cast(getImpl.Value()); - if (getImplFunc != nullptr) { - return const_cast(getImplFunc(ES2PANDA_LIB_VERSION)); - } - return nullptr; -} - static es2panda_AstNode *FindMain(es2panda_AstNode *ast, es2panda_Context *ctx) { size_t n = 0; diff --git a/ets2panda/test/unit/plugin/plugin_proceed_to_state_find_import_declaration.cpp b/ets2panda/test/unit/plugin/plugin_proceed_to_state_find_import_declaration.cpp index a957df8554..23563b7177 100644 --- a/ets2panda/test/unit/plugin/plugin_proceed_to_state_find_import_declaration.cpp +++ b/ets2panda/test/unit/plugin/plugin_proceed_to_state_find_import_declaration.cpp @@ -1,5 +1,5 @@ /** - * Copyright (c) 2024 Huawei Device Co., Ltd. + * Copyright (c) 2025 Huawei Device Co., Ltd. * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at @@ -19,46 +19,17 @@ #include #include #include -#include "public/es2panda_lib.h" + #include "os/library_loader.h" -// NOLINTBEGIN +#include "public/es2panda_lib.h" +#include "util.h" -static const char *LIBNAME = "es2panda-public"; -static const int MIN_ARGC = 3; -static const int NULLPTR_IMPL_ERROR_CODE = 2; +// NOLINTBEGIN static es2panda_Impl *impl = nullptr; static std::vector importDeclarationIdentifiers {}; -es2panda_Impl *GetImpl() -{ - if (impl != nullptr) { - return impl; - } - - std::string soName = ark::os::library_loader::DYNAMIC_LIBRARY_PREFIX + std::string(LIBNAME) + - ark::os::library_loader::DYNAMIC_LIBRARY_SUFFIX; - auto libraryRes = ark::os::library_loader::Load(soName); - if (!libraryRes.HasValue()) { - std::cout << "Error in load lib" << std::endl; - return nullptr; - } - - auto library = std::move(libraryRes.Value()); - auto getImpl = ark::os::library_loader::ResolveSymbol(library, "es2panda_GetImpl"); - if (!getImpl.HasValue()) { - std::cout << "Error in load func get impl" << std::endl; - return nullptr; - } - - auto getImplFunc = reinterpret_cast(getImpl.Value()); - if (getImplFunc != nullptr) { - return const_cast(getImplFunc(ES2PANDA_LIB_VERSION)); - } - return nullptr; -} - void CheckForImportDeclaration(es2panda_AstNode *node, void *arg) { auto *context = static_cast(arg); @@ -123,16 +94,6 @@ void FindImportDeclarations(es2panda_Context *context, es2panda_AstNode *ast) impl->AstNodeForEach(ast, CheckForImportIdentifier, context); } -void CheckForErrors(std::string StateName, es2panda_Context *context) -{ - if (impl->ContextState(context) == ES2PANDA_STATE_ERROR) { - std::cout << "PROCEED TO " << StateName << " ERROR" << std::endl; - std::cout << impl->ContextErrorMessage << std::endl; - } else { - std::cout << "PROCEED TO " << StateName << " SUCCESS" << std::endl; - } -} - int main(int argc, char **argv) { if (argc < MIN_ARGC) { diff --git a/ets2panda/test/unit/plugin/plugin_proceed_to_state_test_annotation_change.cpp b/ets2panda/test/unit/plugin/plugin_proceed_to_state_test_annotation_change.cpp index 3366dd9d91..a5d604f8e2 100644 --- a/ets2panda/test/unit/plugin/plugin_proceed_to_state_test_annotation_change.cpp +++ b/ets2panda/test/unit/plugin/plugin_proceed_to_state_test_annotation_change.cpp @@ -1,5 +1,5 @@ /** - * Copyright (c) 2024 Huawei Device Co., Ltd. + * Copyright (c) 2025 Huawei Device Co., Ltd. * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at @@ -16,44 +16,20 @@ #include #include #include -#include "public/es2panda_lib.h" + #include "os/library_loader.h" +#include "public/es2panda_lib.h" +#include "util.h" + // NOLINTBEGIN -static const char *LIBNAME = "es2panda-public"; -constexpr int MIN_ARGC = 3; constexpr int NUMBER_OF_ANNO_DECLARATIONS = 2; -constexpr int NULLPTR_IMPL_ERROR_CODE = 2; constexpr int PROCEED_ERROR_CODE = 3; constexpr int TEST_ERROR_CODE = 4; static es2panda_Impl *impl = nullptr; -es2panda_Impl *GetImpl() -{ - std::string soName = ark::os::library_loader::DYNAMIC_LIBRARY_PREFIX + std::string(LIBNAME) + - ark::os::library_loader::DYNAMIC_LIBRARY_SUFFIX; - auto libraryRes = ark::os::library_loader::Load(soName); - if (!libraryRes.HasValue()) { - std::cout << "Error in load lib" << std::endl; - return nullptr; - } - - auto library = std::move(libraryRes.Value()); - auto getImpl = ark::os::library_loader::ResolveSymbol(library, "es2panda_GetImpl"); - if (!getImpl.HasValue()) { - std::cout << "Error in load func get impl" << std::endl; - return nullptr; - } - - auto getImplFunc = reinterpret_cast(getImpl.Value()); - if (getImplFunc != nullptr) { - return const_cast(getImplFunc(ES2PANDA_LIB_VERSION)); - } - return nullptr; -} - static std::string source = R"( // Annotation declaration: @interface FuncAuthor { diff --git a/ets2panda/test/unit/plugin/plugin_proceed_to_state_update_statements.cpp b/ets2panda/test/unit/plugin/plugin_proceed_to_state_update_statements.cpp index 8b384ad12c..75dd54a936 100644 --- a/ets2panda/test/unit/plugin/plugin_proceed_to_state_update_statements.cpp +++ b/ets2panda/test/unit/plugin/plugin_proceed_to_state_update_statements.cpp @@ -1,5 +1,5 @@ /** - * Copyright (c) 2024 Huawei Device Co., Ltd. + * Copyright (c) 2025 Huawei Device Co., Ltd. * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at @@ -17,45 +17,16 @@ #include #include #include -#include "public/es2panda_lib.h" + #include "os/library_loader.h" -// NOLINTBEGIN +#include "public/es2panda_lib.h" +#include "util.h" -static const char *LIBNAME = "es2panda-public"; -static const int MIN_ARGC = 3; -static const int NULLPTR_IMPL_ERROR_CODE = 2; +// NOLINTBEGIN static es2panda_Impl *impl = nullptr; -es2panda_Impl *GetImpl() -{ - if (impl != nullptr) { - return impl; - } - - std::string soName = ark::os::library_loader::DYNAMIC_LIBRARY_PREFIX + std::string(LIBNAME) + - ark::os::library_loader::DYNAMIC_LIBRARY_SUFFIX; - auto libraryRes = ark::os::library_loader::Load(soName); - if (!libraryRes.HasValue()) { - std::cout << "Error in load lib" << std::endl; - return nullptr; - } - - auto library = std::move(libraryRes.Value()); - auto getImpl = ark::os::library_loader::ResolveSymbol(library, "es2panda_GetImpl"); - if (!getImpl.HasValue()) { - std::cout << "Error in load func get impl" << std::endl; - return nullptr; - } - - auto getImplFunc = reinterpret_cast(getImpl.Value()); - if (getImplFunc != nullptr) { - return const_cast(getImplFunc(ES2PANDA_LIB_VERSION)); - } - return nullptr; -} - void createClassDeclaration(es2panda_Context *context, char *className, es2panda_AstNode *program) { impl = GetImpl(); @@ -78,16 +49,6 @@ void createClassDeclaration(es2panda_Context *context, char *className, es2panda impl->AstNodeSetParent(context, classDeclaration, program); } -void CheckForErrors(std::string StateName, es2panda_Context *context) -{ - if (impl->ContextState(context) == ES2PANDA_STATE_ERROR) { - std::cout << "PROCEED TO " << StateName << " ERROR" << std::endl; - std::cout << impl->ContextErrorMessage << std::endl; - } else { - std::cout << "PROCEED TO " << StateName << " SUCCESS" << std::endl; - } -} - int main(int argc, char **argv) { if (argc < MIN_ARGC) { diff --git a/ets2panda/test/unit/plugin/plugin_proceed_to_state_update_statements_lambda.cpp b/ets2panda/test/unit/plugin/plugin_proceed_to_state_update_statements_lambda.cpp index bcb4a12e6a..187359b54c 100644 --- a/ets2panda/test/unit/plugin/plugin_proceed_to_state_update_statements_lambda.cpp +++ b/ets2panda/test/unit/plugin/plugin_proceed_to_state_update_statements_lambda.cpp @@ -1,5 +1,5 @@ /** - * Copyright (c) 2024 Huawei Device Co., Ltd. + * Copyright (c) 2025 Huawei Device Co., Ltd. * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at @@ -17,61 +17,22 @@ #include #include #include -#include "public/es2panda_lib.h" + #include "os/library_loader.h" -// NOLINTBEGIN +#include "public/es2panda_lib.h" +#include "util.h" -static const char *LIBNAME = "es2panda-public"; -static const int MIN_ARGC = 3; -static const int NULLPTR_IMPL_ERROR_CODE = 2; +// NOLINTBEGIN static es2panda_Impl *impl = nullptr; -es2panda_Impl *GetImpl() -{ - if (impl != nullptr) { - return impl; - } - - std::string soName = ark::os::library_loader::DYNAMIC_LIBRARY_PREFIX + std::string(LIBNAME) + - ark::os::library_loader::DYNAMIC_LIBRARY_SUFFIX; - auto libraryRes = ark::os::library_loader::Load(soName); - if (!libraryRes.HasValue()) { - std::cout << "Error in load lib" << std::endl; - return nullptr; - } - - auto library = std::move(libraryRes.Value()); - auto getImpl = ark::os::library_loader::ResolveSymbol(library, "es2panda_GetImpl"); - if (!getImpl.HasValue()) { - std::cout << "Error in load func get impl" << std::endl; - return nullptr; - } - - auto getImplFunc = reinterpret_cast(getImpl.Value()); - if (getImplFunc != nullptr) { - return const_cast(getImplFunc(ES2PANDA_LIB_VERSION)); - } - return nullptr; -} - static auto source = std::string( " function foo(lambda: (instance: string) => string):void {\n" " console.log(lambda(\"ABC\"))\n" " }\n" " foo((instance: string) => { return instance })\n"); -void CheckForErrors(std::string StateName, es2panda_Context *context) -{ - if (impl->ContextState(context) == ES2PANDA_STATE_ERROR) { - std::cout << "PROCEED TO " << StateName << " ERROR" << std::endl; - std::cout << impl->ContextErrorMessage << std::endl; - } else { - std::cout << "PROCEED TO " << StateName << " SUCCESS" << std::endl; - } -} - int main(int argc, char **argv) { if (argc < MIN_ARGC) { diff --git a/ets2panda/test/unit/plugin/plugin_test_print_err_msg_func.cpp b/ets2panda/test/unit/plugin/plugin_test_print_err_msg_func.cpp new file mode 100644 index 0000000000..43f21019d3 --- /dev/null +++ b/ets2panda/test/unit/plugin/plugin_test_print_err_msg_func.cpp @@ -0,0 +1,89 @@ +/** + * Copyright (c) 2025 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +#include +#include +#include +#include +#include + +#include "os/library_loader.h" + +#include "public/es2panda_lib.h" +#include "util.h" + +// NOLINTBEGIN + +static es2panda_Impl *g_impl = nullptr; + +int PrintErr(const std::string &errStr, es2panda_Context *context) +{ + std::cout << "PROCEED TO " << errStr << " ERROR" << std::endl; + std::cout << g_impl->ContextErrorMessage(context) << std::endl; + return 1; // 1: Exit abnormally +} + +int main(int argc, char **argv) +{ + if (argc < MIN_ARGC) { + return 1; + } + g_impl = GetImpl(); + if (g_impl == nullptr) { + return NULLPTR_IMPL_ERROR_CODE; + } + auto config = g_impl->CreateConfig(argc - 1, argv + 1); + auto src = std::string("function foo(builder: () => void) {}\nfoo(() => {})"); + auto context = g_impl->CreateContextFromString(config, src.c_str(), argv[argc - 1]); + if (context != nullptr) { + std::cout << "CREATE CONTEXT SUCCESS" << std::endl; + } + std::stringstream ss; + std::streambuf *buf = std::cerr.rdbuf(); + std::cerr.rdbuf(ss.rdbuf()); + es2panda_SourcePosition *pos = g_impl->CreateSourcePosition(context, 2, 5); + std::string errMsg = "test LogTypeError"; + g_impl->LogTypeError(context, errMsg.c_str(), pos); + std::cerr.rdbuf(buf); + CheckForErrors(errMsg, context); + size_t foundPos = ss.str().find(errMsg); + if (foundPos == std::string::npos) { + return PrintErr(errMsg, context); + } + ss.clear(); + buf = std::cout.rdbuf(); + std::cout.rdbuf(ss.rdbuf()); + std::string warnMsg = "test LogWarning"; + g_impl->LogWarning(context, warnMsg.c_str(), pos); + std::cout.rdbuf(buf); + CheckForErrors(warnMsg, context); + foundPos = ss.str().find(warnMsg); + if (foundPos == std::string::npos) { + return PrintErr(warnMsg, context); + } + ss.clear(); + std::cout.rdbuf(ss.rdbuf()); + std::string synMsg = "test LogSyntaxError"; + g_impl->LogSyntaxError(context, synMsg.c_str(), pos); + std::cout.rdbuf(buf); + CheckForErrors(synMsg, context); + foundPos = ss.str().find(synMsg); + if (foundPos == std::string::npos) { + return PrintErr(synMsg, context); + } + return 0; +} + +// NOLINTEND diff --git a/ets2panda/test/unit/plugin/util.cpp b/ets2panda/test/unit/plugin/util.cpp new file mode 100644 index 0000000000..711bd2a221 --- /dev/null +++ b/ets2panda/test/unit/plugin/util.cpp @@ -0,0 +1,61 @@ +/** + * Copyright (c) 2025 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +#include "util.h" + +#include +#include +#include + +static es2panda_Impl *g_implPtr = nullptr; + +es2panda_Impl *GetImpl() +{ + if (g_implPtr != nullptr) { + return g_implPtr; + } + + std::string soName = ark::os::library_loader::DYNAMIC_LIBRARY_PREFIX + std::string("es2panda-public") + + ark::os::library_loader::DYNAMIC_LIBRARY_SUFFIX; + auto libraryRes = ark::os::library_loader::Load(soName); + if (!libraryRes.HasValue()) { + std::cout << "Error in load lib" << std::endl; + return nullptr; + } + + auto library = std::move(libraryRes.Value()); + auto getImpl = ark::os::library_loader::ResolveSymbol(library, "es2panda_GetImpl"); + if (!getImpl.HasValue()) { + std::cout << "Error in load func get impl" << std::endl; + return nullptr; + } + + auto getImplFunc = reinterpret_cast(getImpl.Value()); + if (getImplFunc != nullptr) { + g_implPtr = const_cast(getImplFunc(ES2PANDA_LIB_VERSION)); + return g_implPtr; + } + return nullptr; +} + +void CheckForErrors(const std::string &stateName, es2panda_Context *context) +{ + if (g_implPtr->ContextState(context) == ES2PANDA_STATE_ERROR) { + std::cout << "PROCEED TO " << stateName << " ERROR" << std::endl; + std::cout << g_implPtr->ContextErrorMessage(context) << std::endl; + } else { + std::cout << "PROCEED TO " << stateName << " SUCCESS" << std::endl; + } +} diff --git a/ets2panda/test/unit/plugin/util.h b/ets2panda/test/unit/plugin/util.h new file mode 100644 index 0000000000..e8832fdf15 --- /dev/null +++ b/ets2panda/test/unit/plugin/util.h @@ -0,0 +1,32 @@ +/** + * Copyright (c) 2025 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +#ifndef ES2PANDA_TEST_UNIT_PLUGIN_UTIL_H +#define ES2PANDA_TEST_UNIT_PLUGIN_UTIL_H + +#include + +#include "os/library_loader.h" + +#include "public/es2panda_lib.h" + +const int MIN_ARGC = 3; +const int NULLPTR_IMPL_ERROR_CODE = 2; + +es2panda_Impl *GetImpl(); + +void CheckForErrors(const std::string &stateName, es2panda_Context *context); + +#endif -- Gitee