From cd142046aacc0a77bdd3d3f06cc8babcc5b84069 Mon Sep 17 00:00:00 2001 From: xingshunxiang Date: Thu, 31 Jul 2025 19:40:09 +0800 Subject: [PATCH] Correct the source range of ambient function Issue: https://gitee.com/openharmony/arkcompiler_ets_frontend/issues/ICQ6ZE?from=project-issue Description: the end position of the ambient function declaration is in correct. Reason: Correct the source range of ambient function Tests: ninja tests passed tests/tests-u-runner/runner.sh --ets-cts --show-progress --build-dir x64.release --processes=all passed tests/tests-u-runner/runner.sh --ets-func-tests --show-progress --build-dir x64.release --processes=all passed tests/tests-u-runner/runner.sh --astchecker --show-progress --build-dir x64.release --processes=all passed tests/tests-u-runner/runner.sh --ets-runtime --show-progress --build-dir x64.release --processes=all passed tests/tests-u-runner/runner.sh --parser --no-js --show-progress --build-dir x64.release --processes=all passed Signed-off-by: xingshunxiang --- ets2panda/parser/ETSparser.cpp | 5 ++ .../ets/ambient_declaration-expected.txt | 24 +++---- .../ets/ambient_call_signature-expected.txt | 6 +- .../ets/ambient_object_iterable-expected.txt | 12 ++-- .../parser/ets/declare_class-expected.txt | 12 ++-- .../test/parser/ets/declare_func-expected.txt | 6 +- .../modules/module-expected.txt | 72 +++++++++---------- ...d_to_state_parse_and_verifier_position.cpp | 45 ++++++++++-- 8 files changed, 112 insertions(+), 70 deletions(-) diff --git a/ets2panda/parser/ETSparser.cpp b/ets2panda/parser/ETSparser.cpp index d1dc137a33..7a70ba3041 100644 --- a/ets2panda/parser/ETSparser.cpp +++ b/ets2panda/parser/ETSparser.cpp @@ -563,6 +563,11 @@ ir::ScriptFunction *ETSParser::ParseFunction(ParserStatus newStatus) LogError(diagnostic::ASYNC_IN_AMBIENT_CONTEXT); } + if (isDeclare && signature.ReturnType() != nullptr) { + // Note: if the return type annotation is null, the error handler will set later. + endLoc = signature.ReturnType()->Range().end; + } + // clang-format off ir::ModifierFlags mFlags = isDeclare ? ir::ModifierFlags::DECLARE : ir::ModifierFlags::NONE; ir::ScriptFunctionFlags funcFlags = diff --git a/ets2panda/test/compiler/ets/ambient_declaration-expected.txt b/ets2panda/test/compiler/ets/ambient_declaration-expected.txt index f7106d19ff..a8179e6b22 100644 --- a/ets2panda/test/compiler/ets/ambient_declaration-expected.txt +++ b/ets2panda/test/compiler/ets/ambient_declaration-expected.txt @@ -342,7 +342,7 @@ }, "end": { "line": 16, - "column": 21, + "column": 50, "program": "ambient_declaration.ets" } } @@ -359,7 +359,7 @@ }, "end": { "line": 16, - "column": 21, + "column": 50, "program": "ambient_declaration.ets" } } @@ -372,7 +372,7 @@ }, "end": { "line": 16, - "column": 21, + "column": 50, "program": "ambient_declaration.ets" } } @@ -385,7 +385,7 @@ }, "end": { "line": 16, - "column": 21, + "column": 50, "program": "ambient_declaration.ets" } } @@ -398,7 +398,7 @@ }, "end": { "line": 16, - "column": 21, + "column": 50, "program": "ambient_declaration.ets" } } @@ -418,7 +418,7 @@ }, "end": { "line": 16, - "column": 21, + "column": 50, "program": "ambient_declaration.ets" } } @@ -434,7 +434,7 @@ }, "end": { "line": 16, - "column": 21, + "column": 50, "program": "ambient_declaration.ets" } } @@ -454,7 +454,7 @@ }, "end": { "line": 16, - "column": 21, + "column": 50, "program": "ambient_declaration.ets" } } @@ -468,7 +468,7 @@ }, "end": { "line": 16, - "column": 21, + "column": 50, "program": "ambient_declaration.ets" } } @@ -482,7 +482,7 @@ }, "end": { "line": 16, - "column": 21, + "column": 50, "program": "ambient_declaration.ets" } } @@ -495,7 +495,7 @@ }, "end": { "line": 16, - "column": 21, + "column": 50, "program": "ambient_declaration.ets" } } @@ -510,7 +510,7 @@ }, "end": { "line": 16, - "column": 21, + "column": 50, "program": "ambient_declaration.ets" } } diff --git a/ets2panda/test/parser/ets/ambient_call_signature-expected.txt b/ets2panda/test/parser/ets/ambient_call_signature-expected.txt index cac03c53ac..1ac57e4dae 100644 --- a/ets2panda/test/parser/ets/ambient_call_signature-expected.txt +++ b/ets2panda/test/parser/ets/ambient_call_signature-expected.txt @@ -209,7 +209,7 @@ }, "end": { "line": 17, - "column": 5, + "column": 22, "program": "ambient_call_signature.ets" } } @@ -222,7 +222,7 @@ }, "end": { "line": 17, - "column": 5, + "column": 22, "program": "ambient_call_signature.ets" } } @@ -237,7 +237,7 @@ }, "end": { "line": 17, - "column": 5, + "column": 22, "program": "ambient_call_signature.ets" } } diff --git a/ets2panda/test/parser/ets/ambient_object_iterable-expected.txt b/ets2panda/test/parser/ets/ambient_object_iterable-expected.txt index b63cb1f234..f72f09baaa 100644 --- a/ets2panda/test/parser/ets/ambient_object_iterable-expected.txt +++ b/ets2panda/test/parser/ets/ambient_object_iterable-expected.txt @@ -633,7 +633,7 @@ }, "end": { "line": 23, - "column": 16, + "column": 42, "program": "ambient_object_iterable.ets" } } @@ -646,7 +646,7 @@ }, "end": { "line": 23, - "column": 16, + "column": 42, "program": "ambient_object_iterable.ets" } } @@ -661,7 +661,7 @@ }, "end": { "line": 23, - "column": 16, + "column": 42, "program": "ambient_object_iterable.ets" } } @@ -972,7 +972,7 @@ }, "end": { "line": 29, - "column": 20, + "column": 33, "program": "ambient_object_iterable.ets" } } @@ -985,7 +985,7 @@ }, "end": { "line": 29, - "column": 20, + "column": 33, "program": "ambient_object_iterable.ets" } } @@ -1000,7 +1000,7 @@ }, "end": { "line": 29, - "column": 20, + "column": 33, "program": "ambient_object_iterable.ets" } } diff --git a/ets2panda/test/parser/ets/declare_class-expected.txt b/ets2panda/test/parser/ets/declare_class-expected.txt index 3d702f66a6..de2146e910 100644 --- a/ets2panda/test/parser/ets/declare_class-expected.txt +++ b/ets2panda/test/parser/ets/declare_class-expected.txt @@ -319,7 +319,7 @@ }, "end": { "line": 19, - "column": 8, + "column": 27, "program": "declare_class.ets" } } @@ -332,7 +332,7 @@ }, "end": { "line": 19, - "column": 8, + "column": 27, "program": "declare_class.ets" } } @@ -347,7 +347,7 @@ }, "end": { "line": 19, - "column": 8, + "column": 27, "program": "declare_class.ets" } } @@ -473,7 +473,7 @@ }, "end": { "line": 20, - "column": 15, + "column": 32, "program": "declare_class.ets" } } @@ -486,7 +486,7 @@ }, "end": { "line": 20, - "column": 15, + "column": 32, "program": "declare_class.ets" } } @@ -501,7 +501,7 @@ }, "end": { "line": 20, - "column": 15, + "column": 32, "program": "declare_class.ets" } } diff --git a/ets2panda/test/parser/ets/declare_func-expected.txt b/ets2panda/test/parser/ets/declare_func-expected.txt index 383ca52e3a..16e244d829 100644 --- a/ets2panda/test/parser/ets/declare_func-expected.txt +++ b/ets2panda/test/parser/ets/declare_func-expected.txt @@ -177,7 +177,7 @@ }, "end": { "line": 16, - "column": 21, + "column": 38, "program": "declare_func.ets" } } @@ -190,7 +190,7 @@ }, "end": { "line": 16, - "column": 21, + "column": 38, "program": "declare_func.ets" } } @@ -205,7 +205,7 @@ }, "end": { "line": 16, - "column": 21, + "column": 38, "program": "declare_func.ets" } } diff --git a/ets2panda/test/parser/ets/dynamic_import_tests/modules/module-expected.txt b/ets2panda/test/parser/ets/dynamic_import_tests/modules/module-expected.txt index 8e6226d91a..5bdd4b315a 100644 --- a/ets2panda/test/parser/ets/dynamic_import_tests/modules/module-expected.txt +++ b/ets2panda/test/parser/ets/dynamic_import_tests/modules/module-expected.txt @@ -629,7 +629,7 @@ }, "end": { "line": 25, - "column": 8, + "column": 27, "program": "module.ets" } } @@ -642,7 +642,7 @@ }, "end": { "line": 25, - "column": 8, + "column": 27, "program": "module.ets" } } @@ -657,7 +657,7 @@ }, "end": { "line": 25, - "column": 8, + "column": 27, "program": "module.ets" } } @@ -735,7 +735,7 @@ }, "end": { "line": 26, - "column": 15, + "column": 25, "program": "module.ets" } } @@ -748,7 +748,7 @@ }, "end": { "line": 26, - "column": 15, + "column": 25, "program": "module.ets" } } @@ -763,7 +763,7 @@ }, "end": { "line": 26, - "column": 15, + "column": 25, "program": "module.ets" } } @@ -933,7 +933,7 @@ }, "end": { "line": 27, - "column": 17, + "column": 37, "program": "module.ets" } } @@ -950,7 +950,7 @@ }, "end": { "line": 27, - "column": 17, + "column": 37, "program": "module.ets" } } @@ -963,7 +963,7 @@ }, "end": { "line": 27, - "column": 17, + "column": 37, "program": "module.ets" } } @@ -976,7 +976,7 @@ }, "end": { "line": 27, - "column": 17, + "column": 37, "program": "module.ets" } } @@ -989,7 +989,7 @@ }, "end": { "line": 27, - "column": 17, + "column": 37, "program": "module.ets" } } @@ -1009,7 +1009,7 @@ }, "end": { "line": 27, - "column": 17, + "column": 37, "program": "module.ets" } } @@ -1025,7 +1025,7 @@ }, "end": { "line": 27, - "column": 17, + "column": 37, "program": "module.ets" } } @@ -1045,7 +1045,7 @@ }, "end": { "line": 27, - "column": 17, + "column": 37, "program": "module.ets" } } @@ -1059,7 +1059,7 @@ }, "end": { "line": 27, - "column": 17, + "column": 37, "program": "module.ets" } } @@ -1073,7 +1073,7 @@ }, "end": { "line": 27, - "column": 17, + "column": 37, "program": "module.ets" } } @@ -1086,7 +1086,7 @@ }, "end": { "line": 27, - "column": 17, + "column": 37, "program": "module.ets" } } @@ -1101,7 +1101,7 @@ }, "end": { "line": 27, - "column": 17, + "column": 37, "program": "module.ets" } } @@ -1959,7 +1959,7 @@ }, "end": { "line": 36, - "column": 18, + "column": 26, "program": "module.ets" } } @@ -1972,7 +1972,7 @@ }, "end": { "line": 36, - "column": 18, + "column": 26, "program": "module.ets" } } @@ -1987,7 +1987,7 @@ }, "end": { "line": 36, - "column": 18, + "column": 26, "program": "module.ets" } } @@ -2685,7 +2685,7 @@ }, "end": { "line": 16, - "column": 28, + "column": 40, "program": "module.ets" } } @@ -2698,7 +2698,7 @@ }, "end": { "line": 16, - "column": 28, + "column": 40, "program": "module.ets" } } @@ -2713,7 +2713,7 @@ }, "end": { "line": 16, - "column": 28, + "column": 40, "program": "module.ets" } } @@ -2883,7 +2883,7 @@ }, "end": { "line": 17, - "column": 37, + "column": 50, "program": "module.ets" } } @@ -2900,7 +2900,7 @@ }, "end": { "line": 17, - "column": 37, + "column": 50, "program": "module.ets" } } @@ -2913,7 +2913,7 @@ }, "end": { "line": 17, - "column": 37, + "column": 50, "program": "module.ets" } } @@ -2926,7 +2926,7 @@ }, "end": { "line": 17, - "column": 37, + "column": 50, "program": "module.ets" } } @@ -2939,7 +2939,7 @@ }, "end": { "line": 17, - "column": 37, + "column": 50, "program": "module.ets" } } @@ -2959,7 +2959,7 @@ }, "end": { "line": 17, - "column": 37, + "column": 50, "program": "module.ets" } } @@ -2975,7 +2975,7 @@ }, "end": { "line": 17, - "column": 37, + "column": 50, "program": "module.ets" } } @@ -2995,7 +2995,7 @@ }, "end": { "line": 17, - "column": 37, + "column": 50, "program": "module.ets" } } @@ -3009,7 +3009,7 @@ }, "end": { "line": 17, - "column": 37, + "column": 50, "program": "module.ets" } } @@ -3023,7 +3023,7 @@ }, "end": { "line": 17, - "column": 37, + "column": 50, "program": "module.ets" } } @@ -3036,7 +3036,7 @@ }, "end": { "line": 17, - "column": 37, + "column": 50, "program": "module.ets" } } @@ -3051,7 +3051,7 @@ }, "end": { "line": 17, - "column": 37, + "column": 50, "program": "module.ets" } } diff --git a/ets2panda/test/unit/plugin/plugin_proceed_to_state_parse_and_verifier_position.cpp b/ets2panda/test/unit/plugin/plugin_proceed_to_state_parse_and_verifier_position.cpp index 664bd9ab17..5146723f04 100644 --- a/ets2panda/test/unit/plugin/plugin_proceed_to_state_parse_and_verifier_position.cpp +++ b/ets2panda/test/unit/plugin/plugin_proceed_to_state_parse_and_verifier_position.cpp @@ -85,6 +85,11 @@ export { ClassOutside, val, foo } export { PI, E } from "std/math" export NS + +export declare function fooDecl( + options: number, + myop: string +): void )"; constexpr size_t NS_START_LINE = 11; @@ -114,6 +119,7 @@ constexpr size_t INTERFACE_FOO4_START_LINE = 37; constexpr size_t FUNCTION_PARAM1_START_LINE = 55; constexpr size_t FUNCTION_PARAM2_START_LINE = 56; constexpr size_t FUNCTION_PARAM3_START_LINE = 43; +constexpr size_t FUNCTION_DECL_START_LINE = 66; constexpr size_t NS_END_LINE = 30; constexpr size_t INNERNS_END_LINE = 29; @@ -142,6 +148,7 @@ constexpr size_t INTERFACE_FOO4_END_LINE = 37; constexpr size_t FUNCTION_PARAM1_END_LINE = 55; constexpr size_t FUNCTION_PARAM2_END_LINE = 56; constexpr size_t FUNCTION_PARAM3_END_LINE = 43; +constexpr size_t FUNCTION_DECL_END_LINE = 69; constexpr size_t NS_START_COL = 1; constexpr size_t INNERNS_START_COL = 3; @@ -170,6 +177,7 @@ constexpr size_t INTERFACE_FOO4_START_COL = 5; constexpr size_t FUNCTION_PARAM1_START_COL = 3; constexpr size_t FUNCTION_PARAM2_START_COL = 3; constexpr size_t FUNCTION_PARAM3_START_COL = 11; +constexpr size_t FUNCTION_DECL_START_COL = 8; constexpr size_t NS_END_COL = 2; constexpr size_t INNERNS_END_COL = 23; @@ -198,6 +206,7 @@ constexpr size_t INTERFACE_FOO4_END_COL = 25; constexpr size_t FUNCTION_PARAM1_END_COL = 16; constexpr size_t FUNCTION_PARAM2_END_COL = 16; constexpr size_t FUNCTION_PARAM3_END_COL = 21; +constexpr size_t FUNCTION_DECL_END_COL = 8; static std::map startLineMap = {{"exportNamedDecl", EXPORT_NAMED_DECL_START_LINE}, {"exportSingleNamedDecl", EXPORT_SINGLE_NAMED_DECL_START_LINE}, @@ -225,7 +234,8 @@ static std::map startLineMap = {{"exportNamedDecl", EXPORT_ {"interfaceFoo4", INTERFACE_FOO4_START_LINE}, {"fooP1", FUNCTION_PARAM1_START_LINE}, {"fooP2", FUNCTION_PARAM2_START_LINE}, - {"p3", FUNCTION_PARAM3_START_LINE}}; + {"p3", FUNCTION_PARAM3_START_LINE}, + {"fooDecl", FUNCTION_DECL_START_LINE}}; static std::map startColMap = {{"exportNamedDecl", EXPORT_NAMED_DECL_START_COL}, {"exportSingleNamedDecl", EXPORT_SINGLE_NAMED_DECL_START_COL}, @@ -253,7 +263,8 @@ static std::map startColMap = {{"exportNamedDecl", EXPORT_N {"interfaceFoo4", INTERFACE_FOO4_START_COL}, {"fooP1", FUNCTION_PARAM1_START_COL}, {"fooP2", FUNCTION_PARAM2_START_COL}, - {"p3", FUNCTION_PARAM3_START_COL}}; + {"p3", FUNCTION_PARAM3_START_COL}, + {"fooDecl", FUNCTION_DECL_START_COL}}; static std::map endLineMap = {{"exportNamedDecl", EXPORT_NAMED_DECL_END_LINE}, {"exportSingleNamedDecl", EXPORT_SINGLE_NAMED_DECL_END_LINE}, @@ -281,7 +292,8 @@ static std::map endLineMap = {{"exportNamedDecl", EXPORT_NA {"interfaceFoo4", INTERFACE_FOO4_END_LINE}, {"fooP1", FUNCTION_PARAM1_END_LINE}, {"fooP2", FUNCTION_PARAM2_END_LINE}, - {"p3", FUNCTION_PARAM3_END_LINE}}; + {"p3", FUNCTION_PARAM3_END_LINE}, + {"fooDecl", FUNCTION_DECL_END_LINE}}; static std::map endColMap = {{"exportNamedDecl", EXPORT_NAMED_DECL_END_COL}, {"exportSingleNamedDecl", EXPORT_SINGLE_NAMED_DECL_END_COL}, @@ -309,11 +321,29 @@ static std::map endColMap = {{"exportNamedDecl", EXPORT_NAM {"interfaceFoo4", INTERFACE_FOO4_END_COL}, {"fooP1", FUNCTION_PARAM1_END_COL}, {"fooP2", FUNCTION_PARAM2_END_COL}, - {"p3", FUNCTION_PARAM3_END_COL}}; + {"p3", FUNCTION_PARAM3_END_COL}, + {"fooDecl", FUNCTION_DECL_END_COL}}; static es2panda_Impl *impl = nullptr; es2panda_Context *context = nullptr; es2panda_AstNode *fooDecl = nullptr; +static void FindFunctionDecl(es2panda_AstNode *ast, [[maybe_unused]] void *ctx) +{ + if (!impl->IsFunctionDeclaration(ast)) { + return; + } + auto scriptFunc = impl->FunctionDeclarationFunction(context, ast); + + auto *ident = impl->ScriptFunctionId(context, scriptFunc); + if (ident == nullptr) { + return; + } + + auto name = std::string(impl->IdentifierName(context, ident)); + if (name == "fooDecl") { + fooDecl = ast; + } +} static std::map namespaceDecl = {{"NS", nullptr}, {"InnerNS", nullptr}}; static void FindNamespaceDecl(es2panda_AstNode *ast, [[maybe_unused]] void *ctx) @@ -475,12 +505,18 @@ static void FindTargetAst(es2panda_AstNode *ast, [[maybe_unused]] void *ctx) impl->AstNodeForEach(ast, FindMethodDef, context); impl->AstNodeForEach(ast, FindETSParamDecl, context); impl->AstNodeForEach(ast, FindEnumMember, context); + impl->AstNodeForEach(ast, FindFunctionDecl, context); } static bool CheckLineAndCol(es2panda_AstNode *ast, std::string name) { auto start = impl->AstNodeStartConst(context, ast); auto end = impl->AstNodeEndConst(context, ast); + [[maybe_unused]] auto a = impl->SourcePositionLine(context, start); + [[maybe_unused]] auto b = impl->SourcePositionCol(context, start); + [[maybe_unused]] auto c = impl->SourcePositionLine(context, end); + [[maybe_unused]] auto d = impl->SourcePositionCol(context, end); + auto res = startLineMap[name] == impl->SourcePositionLine(context, start); ASSERT(startLineMap[name] == impl->SourcePositionLine(context, start)); @@ -501,6 +537,7 @@ static bool CheckAllNode() res &= CheckLineAndCol(exportSingleNamedDecl, "exportSingleNamedDecl"); res &= CheckLineAndCol(reExportedDecl, "reExportedDecl"); res &= CheckLineAndCol(importDecl, "importDecl"); + res &= CheckLineAndCol(fooDecl, "fooDecl"); for (const auto &[name, targetAst] : namespaceDecl) { res &= CheckLineAndCol(targetAst, name); } -- Gitee