From 9c2208ec4b4635e87a13f400c47ae913dffe0507 Mon Sep 17 00:00:00 2001 From: ekkoruse Date: Mon, 14 Jul 2025 21:26:28 +0800 Subject: [PATCH] provide real coloum number for capi provide reak ciloum number for capi Issue: https://gitee.com/openharmony/arkcompiler_ets_frontend/issues/ICM9DO?from=project-issue Signed-off-by: ekkoruse --- ets2panda/public/es2panda_lib.cpp | 12 +++ ets2panda/public/es2panda_lib.h | 1 + ets2panda/test/unit/plugin/CMakeLists.txt | 1 + .../use_plugin_to_test_column_number.cpp | 78 +++++++++++++++++++ 4 files changed, 92 insertions(+) create mode 100644 ets2panda/test/unit/plugin/use_plugin_to_test_column_number.cpp diff --git a/ets2panda/public/es2panda_lib.cpp b/ets2panda/public/es2panda_lib.cpp index 0b0638b0f7..04039bb900 100644 --- a/ets2panda/public/es2panda_lib.cpp +++ b/ets2panda/public/es2panda_lib.cpp @@ -14,6 +14,7 @@ */ #include "es2panda_lib.h" +#include #include #include @@ -1030,6 +1031,16 @@ extern "C" bool IsAnyError(es2panda_Context *context) return reinterpret_cast(context)->diagnosticEngine->IsAnyError(); } +extern "C" size_t SourcePositionCol([[maybe_unused]] es2panda_Context *context, es2panda_SourcePosition *position) +{ + static const size_t EMPTY = 1; + auto es2pandaPosition = reinterpret_cast(position); + if (es2pandaPosition->Program() == nullptr) { + return EMPTY; + } + return es2pandaPosition->ToLocation().col; +} + extern "C" size_t SourcePositionIndex([[maybe_unused]] es2panda_Context *context, es2panda_SourcePosition *position) { return reinterpret_cast(position)->index; @@ -1357,6 +1368,7 @@ es2panda_Impl g_impl = { AllocMemory, CreateSourcePosition, CreateSourceRange, + SourcePositionCol, SourcePositionIndex, SourcePositionLine, SourceRangeStart, diff --git a/ets2panda/public/es2panda_lib.h b/ets2panda/public/es2panda_lib.h index cbc9e0a411..f5380df394 100644 --- a/ets2panda/public/es2panda_lib.h +++ b/ets2panda/public/es2panda_lib.h @@ -233,6 +233,7 @@ struct CAPI_EXPORT es2panda_Impl { es2panda_SourcePosition *(*CreateSourcePosition)(es2panda_Context *context, size_t index, size_t line); es2panda_SourceRange *(*CreateSourceRange)(es2panda_Context *context, es2panda_SourcePosition *start, es2panda_SourcePosition *end); + size_t (*SourcePositionCol)(es2panda_Context *context, es2panda_SourcePosition *position); size_t (*SourcePositionIndex)(es2panda_Context *context, es2panda_SourcePosition *position); size_t (*SourcePositionLine)(es2panda_Context *context, es2panda_SourcePosition *position); es2panda_SourcePosition *(*SourceRangeStart)(es2panda_Context *context, es2panda_SourceRange *range); diff --git a/ets2panda/test/unit/plugin/CMakeLists.txt b/ets2panda/test/unit/plugin/CMakeLists.txt index c71712ea39..9a17105ec4 100644 --- a/ets2panda/test/unit/plugin/CMakeLists.txt +++ b/ets2panda/test/unit/plugin/CMakeLists.txt @@ -105,6 +105,7 @@ set(PLUGIN_TESTS "plugin_proceed_to_state_update_function_expression compile.ets ${COMPILE_MODE} cpp ${EXECUTABLE_PLUGIN}" "plugin_proceed_to_state_create_ets_new_expression compile.ets ${COMPILE_MODE} cpp ${EXECUTABLE_PLUGIN}" "plugin_proceed_to_state_log_diagnostic_with_suggestion compile.ets ${COMPILE_MODE} cpp ${EXECUTABLE_PLUGIN}" + "use_plugin_to_test_column_number compile.ets ${COMPILE_MODE} cpp ${EXECUTABLE_PLUGIN}" "plugin_proceed_to_state_check_jsdoc compile.ets ${COMPILE_MODE} cpp ${EXECUTABLE_PLUGIN}" "plugin_proceed_to_state_test_global_func_call_dump compile.ets ${COMPILE_MODE} cpp ${EXECUTABLE_PLUGIN}" "plugin_proceed_to_state_test_interface_duplicate_setter compile.ets ${COMPILE_MODE} cpp ${EXECUTABLE_PLUGIN}" diff --git a/ets2panda/test/unit/plugin/use_plugin_to_test_column_number.cpp b/ets2panda/test/unit/plugin/use_plugin_to_test_column_number.cpp new file mode 100644 index 0000000000..95d9221741 --- /dev/null +++ b/ets2panda/test/unit/plugin/use_plugin_to_test_column_number.cpp @@ -0,0 +1,78 @@ +/** + * 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 "macros.h" +#include "os/file.h" +#include "public/es2panda_lib.h" +#include "util.h" +#include "parser/program/program.h" +#include "ir/statements/blockStatement.h" +#include "ir/astNode.h" +#include "ir/statements/classDeclaration.h" +#include "ir/base/classDefinition.h" +#include "ir/base/methodDefinition.h" +#include "util/options.h" +#include "ir/statements/functionDeclaration.h" + +// NOLINTBEGIN + +static es2panda_Impl *impl = nullptr; + +namespace { +constexpr std::size_t COL_EXPECT = 13; + +const std::string SOURCE_CODE = + " function foo():void {\n" + " }\n" + " class A{}\n"; + +} // namespace + +int main(int argc, char **argv) +{ + if (argc < MIN_ARGC) { + return INVALID_ARGC_ERROR_CODE; + } + + if (GetImpl() == nullptr) { + return NULLPTR_IMPL_ERROR_CODE; + } + impl = GetImpl(); + const char **args = const_cast(&(argv[1])); + auto config = impl->CreateConfig(argc - 1, args); + auto context = impl->CreateContextFromString(config, SOURCE_CODE.data(), argv[argc - 1]); + if (context == nullptr) { + return NULLPTR_CONTEXT_ERROR_CODE; + } + + impl->ProceedToState(context, ES2PANDA_STATE_PARSED); + CheckForErrors("PARSE", context); + + auto program = impl->ContextProgram(context); + auto programPtr = reinterpret_cast(program); + + auto range = programPtr->Ast()->Statements()[0]->AsFunctionDeclaration()->Function()->Id()->Range(); + int res = 0; + if (COL_EXPECT != impl->SourcePositionCol(context, (es2panda_SourcePosition *)(&range.start))) { + res = TEST_ERROR_CODE; + } + + impl->DestroyConfig(config); + return res; +} + +// NOLINTEND \ No newline at end of file -- Gitee