From fed31c4594d349a73eedddf5f43c4457222ba72a Mon Sep 17 00:00:00 2001 From: yp9522 Date: Tue, 9 Sep 2025 15:19:40 +0800 Subject: [PATCH] test Issue: https://gitee.com/openharmony/arkcompiler_ets_frontend/issues/ICVK36 Signed-off-by: yp9522 Change-Id: Iab35ff68f7e3bb28321e8c6c5b01d4dd8c78cdf1 --- ets2panda/lsp/include/node_matchers.h | 14 ++ ets2panda/lsp/src/node_matchers.cpp | 224 ++++++++------------------ 2 files changed, 85 insertions(+), 153 deletions(-) diff --git a/ets2panda/lsp/include/node_matchers.h b/ets2panda/lsp/include/node_matchers.h index 6fdadbebb2..1dacc07bf5 100644 --- a/ets2panda/lsp/include/node_matchers.h +++ b/ets2panda/lsp/include/node_matchers.h @@ -21,6 +21,20 @@ #include "ir/astNode.h" #include "api.h" +#define DEFINE_SIMPLE_MATCHER(FunctionName, NodeType, NameAccessor) \ + bool FunctionName(ir::AstNode *childNode, const NodeInfo *info) \ + { \ + if (childNode->Is##NodeType() && \ + std::string(childNode->As##NodeType()->NameAccessor()->Name()) == info->name) { \ + /* CC-OFFNXT(G.PRE.05) The macro is used to generate a function. Return is needed */ \ + /* CC-OFF(G.PRE.02) name part*/ \ + return true; \ + } \ + /* CC-OFFNXT(G.PRE.05) The macro is used to generate a function. Return is needed */ \ + /* CC-OFF(G.PRE.02) name part*/ \ + return false; \ + } + #define DEFINE_SIMPLE_HANDLER(FunctionName, NodeType, NameAccessor, NodeTypeEnum) \ void FunctionName(ir::AstNode *node, std::vector &result) \ { \ diff --git a/ets2panda/lsp/src/node_matchers.cpp b/ets2panda/lsp/src/node_matchers.cpp index 33ce4f1c86..506fd4bdc1 100644 --- a/ets2panda/lsp/src/node_matchers.cpp +++ b/ets2panda/lsp/src/node_matchers.cpp @@ -22,89 +22,90 @@ #include "ir/statements/annotationDeclaration.h" #include "ir/statements/annotationUsage.h" +#define DEFINE_SIMPLE_MATCHER_1 \ + DEFINE_SIMPLE_MATCHER(MatchClassDefinition, ClassDefinition, Ident) \ + DEFINE_SIMPLE_MATCHER(MatchClassProperty, ClassProperty, Id) \ + DEFINE_SIMPLE_MATCHER(MatchMethodDefinition, MethodDefinition, Function()->Id) \ + DEFINE_SIMPLE_MATCHER(MatchProperty, Property, Key()->AsIdentifier) \ + DEFINE_SIMPLE_MATCHER(MatchTSEnumDeclaration, TSEnumDeclaration, Key) \ + DEFINE_SIMPLE_MATCHER(MatchTSInterfaceDeclaration, TSInterfaceDeclaration, Id) \ + DEFINE_SIMPLE_MATCHER(MatchTSTypeAliasDeclaration, TSTypeAliasDeclaration, Id) \ + DEFINE_SIMPLE_MATCHER(MatchVariableDeclarator, VariableDeclarator, Id()->AsIdentifier) + +#define DEFINE_SIMPLE_MATCHER_2 \ + DEFINE_SIMPLE_MATCHER(MatchImportSpecifier, ImportSpecifier, Imported) \ + DEFINE_SIMPLE_MATCHER(MatchImportDefaultSpecifier, ImportDefaultSpecifier, Local) \ + DEFINE_SIMPLE_MATCHER(MatchImportNamespaceSpecifier, ImportNamespaceSpecifier, Local) \ + DEFINE_SIMPLE_MATCHER(MatchTSTypeParameter, TSTypeParameter, Name) \ + DEFINE_SIMPLE_MATCHER(MatchEtsParameterExpression, ETSParameterExpression, Ident) \ + DEFINE_SIMPLE_MATCHER(MatchEtsStructDeclaration, ETSStructDeclaration, Definition()->Ident) \ + DEFINE_SIMPLE_MATCHER(MatchClassDeclaration, ClassDeclaration, Definition()->Ident) \ + DEFINE_SIMPLE_MATCHER(MatchAnnotationDeclaration, AnnotationDeclaration, GetBaseName) + +#define DEFINE_SIMPLE_MATCHER_3 \ + DEFINE_SIMPLE_MATCHER(MatchAnnotationUsage, AnnotationUsage, GetBaseName) \ + DEFINE_SIMPLE_MATCHER(MatchScriptFunction, ScriptFunction, Id) \ + DEFINE_SIMPLE_MATCHER(MatchFunctionDeclaration, FunctionDeclaration, Function()->Id) \ + DEFINE_SIMPLE_MATCHER(MatchTSClassImplements, TSClassImplements, Expr()->AsETSTypeReference()->Part()->GetIdent) + +#define DEFINE_SIMPLE_HANDLER_1 \ + DEFINE_SIMPLE_HANDLER(HandleClassDefinition, ClassDefinition, Ident, ir::AstNodeType::CLASS_DEFINITION) \ + DEFINE_SIMPLE_HANDLER(HandleClassProperty, ClassProperty, Id, ir::AstNodeType::CLASS_PROPERTY) \ + DEFINE_SIMPLE_HANDLER(HandleProperty, Property, Key()->AsIdentifier, ir::AstNodeType::PROPERTY) \ + DEFINE_SIMPLE_HANDLER(HandleTSInterfaceDeclaration, TSInterfaceDeclaration, Id, \ + ir::AstNodeType::TS_INTERFACE_DECLARATION) \ + DEFINE_SIMPLE_HANDLER(HandleTSTypeAliasDeclaration, TSTypeAliasDeclaration, Id, \ + ir::AstNodeType::TS_TYPE_ALIAS_DECLARATION) \ + DEFINE_SIMPLE_HANDLER(HandleImportSpecifier, ImportSpecifier, Imported, ir::AstNodeType::IMPORT_SPECIFIER) \ + DEFINE_SIMPLE_HANDLER(HandleImportDefaultSpecifier, ImportDefaultSpecifier, Local, \ + ir::AstNodeType::IMPORT_DEFAULT_SPECIFIER) + +#define DEFINE_SIMPLE_HANDLER_2 \ + DEFINE_SIMPLE_HANDLER(HandleImportNamespaceSpecifier, ImportNamespaceSpecifier, Local, \ + ir::AstNodeType::IMPORT_NAMESPACE_SPECIFIER) \ + DEFINE_SIMPLE_HANDLER(HandleStructDeclaration, ETSStructDeclaration, Definition()->Ident, \ + ir::AstNodeType::STRUCT_DECLARATION) \ + DEFINE_SIMPLE_HANDLER(HandleClassDeclaration, ClassDeclaration, Definition()->Ident, \ + ir::AstNodeType::CLASS_DECLARATION) \ + DEFINE_SIMPLE_HANDLER(HanleScriptFunction, ScriptFunction, Id, ir::AstNodeType::SCRIPT_FUNCTION) \ + DEFINE_SIMPLE_HANDLER(HandleFunctionDeclaration, FunctionDeclaration, Function()->Id, \ + ir::AstNodeType::FUNCTION_DECLARATION) \ + DEFINE_SIMPLE_HANDLER(HandleMethodDefinition, MethodDefinition, Function()->Id, \ + ir::AstNodeType::METHOD_DEFINITION) \ + DEFINE_SIMPLE_HANDLER(HanleTSEnumDeclaration, TSEnumDeclaration, Key, ir::AstNodeType::TS_ENUM_DECLARATION) + +#define DEFINE_SIMPLE_HANDLER_3 \ + DEFINE_SIMPLE_HANDLER(HandleVariableDeclaration, VariableDeclaration, Declarators()[0]->Id()->AsIdentifier, \ + ir::AstNodeType::VARIABLE_DECLARATION) \ + DEFINE_SIMPLE_HANDLER(HandleVariableDeclarator, VariableDeclarator, Id()->AsIdentifier, \ + ir::AstNodeType::VARIABLE_DECLARATOR) \ + DEFINE_SIMPLE_HANDLER(HandleTSClassImplements, TSClassImplements, Expr()->AsETSTypeReference()->Part()->GetIdent, \ + ir::AstNodeType::TS_CLASS_IMPLEMENTS) \ + DEFINE_SIMPLE_HANDLER(HandleAnnotationDeclaration, AnnotationDeclaration, GetBaseName, \ + ir::AstNodeType::ANNOTATION_DECLARATION) \ + DEFINE_SIMPLE_HANDLER(HandleAnnotationUsage, AnnotationUsage, GetBaseName, ir::AstNodeType::ANNOTATION_USAGE) \ + DEFINE_SIMPLE_HANDLER(HandleAwitExpression, AwaitExpression, Argument()->AsIdentifier, \ + ir::AstNodeType::AWAIT_EXPRESSION) + namespace ark::es2panda::lsp { +DEFINE_SIMPLE_MATCHER_1 +DEFINE_SIMPLE_MATCHER_2 +DEFINE_SIMPLE_MATCHER_3 -DEFINE_SIMPLE_HANDLER(HandleClassDefinition, ClassDefinition, Ident, ir::AstNodeType::CLASS_DEFINITION) -DEFINE_SIMPLE_HANDLER(HandleClassProperty, ClassProperty, Id, ir::AstNodeType::CLASS_PROPERTY) -DEFINE_SIMPLE_HANDLER(HandleProperty, Property, Key()->AsIdentifier, ir::AstNodeType::PROPERTY) -DEFINE_SIMPLE_HANDLER(HandleTSInterfaceDeclaration, TSInterfaceDeclaration, Id, - ir::AstNodeType::TS_INTERFACE_DECLARATION) -DEFINE_SIMPLE_HANDLER(HandleTSTypeAliasDeclaration, TSTypeAliasDeclaration, Id, - ir::AstNodeType::TS_TYPE_ALIAS_DECLARATION) -DEFINE_SIMPLE_HANDLER(HandleImportSpecifier, ImportSpecifier, Imported, ir::AstNodeType::IMPORT_SPECIFIER) -DEFINE_SIMPLE_HANDLER(HandleImportDefaultSpecifier, ImportDefaultSpecifier, Local, - ir::AstNodeType::IMPORT_DEFAULT_SPECIFIER) -DEFINE_SIMPLE_HANDLER(HandleImportNamespaceSpecifier, ImportNamespaceSpecifier, Local, - ir::AstNodeType::IMPORT_NAMESPACE_SPECIFIER) -DEFINE_SIMPLE_HANDLER(HandleStructDeclaration, ETSStructDeclaration, Definition()->Ident, - ir::AstNodeType::STRUCT_DECLARATION) -DEFINE_SIMPLE_HANDLER(HandleClassDeclaration, ClassDeclaration, Definition()->Ident, ir::AstNodeType::CLASS_DECLARATION) -DEFINE_SIMPLE_HANDLER(HanleScriptFunction, ScriptFunction, Id, ir::AstNodeType::SCRIPT_FUNCTION) -DEFINE_SIMPLE_HANDLER(HandleFunctionDeclaration, FunctionDeclaration, Function()->Id, - ir::AstNodeType::FUNCTION_DECLARATION) -DEFINE_SIMPLE_HANDLER(HandleMethodDefinition, MethodDefinition, Function()->Id, ir::AstNodeType::METHOD_DEFINITION) -DEFINE_SIMPLE_HANDLER(HanleTSEnumDeclaration, TSEnumDeclaration, Key, ir::AstNodeType::TS_ENUM_DECLARATION) -DEFINE_SIMPLE_HANDLER(HandleVariableDeclaration, VariableDeclaration, Declarators()[0]->Id()->AsIdentifier, - ir::AstNodeType::VARIABLE_DECLARATION) -DEFINE_SIMPLE_HANDLER(HandleVariableDeclarator, VariableDeclarator, Id()->AsIdentifier, - ir::AstNodeType::VARIABLE_DECLARATOR) -DEFINE_SIMPLE_HANDLER(HandleTSClassImplements, TSClassImplements, Expr()->AsETSTypeReference()->Part()->GetIdent, - ir::AstNodeType::TS_CLASS_IMPLEMENTS) -DEFINE_SIMPLE_HANDLER(HandleAnnotationDeclaration, AnnotationDeclaration, GetBaseName, - ir::AstNodeType::ANNOTATION_DECLARATION) -DEFINE_SIMPLE_HANDLER(HandleAnnotationUsage, AnnotationUsage, GetBaseName, ir::AstNodeType::ANNOTATION_USAGE) -DEFINE_SIMPLE_HANDLER(HandleAwitExpression, AwaitExpression, Argument()->AsIdentifier, - ir::AstNodeType::AWAIT_EXPRESSION) - -bool MatchClassDefinition(ir::AstNode *childNode, const NodeInfo *info) -{ - return childNode->IsClassDefinition() && std::string(childNode->AsClassDefinition()->Ident()->Name()) == info->name; -} +DEFINE_SIMPLE_HANDLER_1 +DEFINE_SIMPLE_HANDLER_2 +DEFINE_SIMPLE_HANDLER_3 bool MatchIdentifier(ir::AstNode *childNode, const NodeInfo *info) { return childNode->IsIdentifier() && std::string(childNode->AsIdentifier()->Name()) == info->name; } -bool MatchClassProperty(ir::AstNode *childNode, const NodeInfo *info) -{ - return childNode->IsClassProperty() && std::string(childNode->AsClassProperty()->Id()->Name()) == info->name; -} - -bool MatchProperty(ir::AstNode *childNode, const NodeInfo *info) -{ - return childNode->IsProperty() && std::string(childNode->AsProperty()->Key()->AsIdentifier()->Name()) == info->name; -} - -bool MatchMethodDefinition(ir::AstNode *childNode, const NodeInfo *info) -{ - return childNode->IsMethodDefinition() && - std::string(childNode->AsMethodDefinition()->Function()->Id()->Name()) == info->name; -} - -bool MatchTSEnumDeclaration(ir::AstNode *childNode, const NodeInfo *info) -{ - return childNode->IsTSEnumDeclaration() && - std::string(childNode->AsTSEnumDeclaration()->Key()->Name()) == info->name; -} - bool MatchTSEnumMember(ir::AstNode *childNode, const NodeInfo *info) { return childNode->IsTSEnumMember() && std::string(childNode->AsTSEnumMember()->Name()) == info->name; } -bool MatchTSInterfaceDeclaration(ir::AstNode *childNode, const NodeInfo *info) -{ - return childNode->IsTSInterfaceDeclaration() && - std::string(childNode->AsTSInterfaceDeclaration()->Id()->Name()) == info->name; -} - -bool MatchTSTypeAliasDeclaration(ir::AstNode *childNode, const NodeInfo *info) -{ - return childNode->IsTSTypeAliasDeclaration() && - std::string(childNode->AsTSTypeAliasDeclaration()->Id()->Name()) == info->name; -} - bool MatchExportSpecifier(ir::AstNode *childNode, const NodeInfo *info) { if (!childNode->IsETSReExportDeclaration()) { @@ -132,15 +133,6 @@ bool MatchMemberExpression(ir::AstNode *childNode, const NodeInfo *info) return childNode->IsMemberExpression() && childNode->AsMemberExpression()->Property()->ToString() == info->name; } -bool MatchTSClassImplements(ir::AstNode *childNode, const NodeInfo *info) -{ - if (!childNode->IsTSClassImplements()) { - return false; - } - auto dd = childNode->AsTSClassImplements()->Expr()->AsETSTypeReference()->Part(); - return std::string(dd->GetIdent()->Name()) == info->name; -} - bool MatchCallExpression(ir::AstNode *childNode, const NodeInfo *info) { if (childNode->IsCallExpression()) { @@ -174,11 +166,6 @@ bool MatchTsTypeReference(ir::AstNode *childNode, const NodeInfo *info) return false; } -bool MatchScriptFunction(ir::AstNode *childNode, const NodeInfo *info) -{ - return childNode->IsScriptFunction() && std::string(childNode->AsScriptFunction()->Id()->Name()) == info->name; -} - ir::AstNode *ExtractExportSpecifierIdentifier(ir::AstNode *node, const NodeInfo *info) { if (!node->IsETSReExportDeclaration()) { @@ -328,14 +315,6 @@ bool MatchEtsNewClassInstanceExpression(ir::AstNode *childNode, const NodeInfo * std::string(typeRef->AsETSTypeReference()->Part()->Name()->ToString()) == info->name; } -bool MatchEtsStructDeclaration(ir::AstNode *childNode, const NodeInfo *info) -{ - if (!childNode->IsETSStructDeclaration()) { - return false; - } - return std::string(childNode->AsETSStructDeclaration()->Definition()->Ident()->Name()) == info->name; -} - bool MatchSpreadElement(ir::AstNode *childNode, const NodeInfo *info) { if (!childNode->IsSpreadElement()) { @@ -374,12 +353,6 @@ ir::AstNode *ExtractCallExpressionIdentifier(ir::AstNode *node, const NodeInfo * return node; } -bool MatchVariableDeclarator(ir::AstNode *childNode, const NodeInfo *info) -{ - return childNode->IsVariableDeclarator() && - std::string(childNode->AsVariableDeclarator()->Id()->AsIdentifier()->Name()) == info->name; -} - bool MatchVariableDeclaration(ir::AstNode *childNode, const NodeInfo *info) { return childNode->IsVariableDeclaration() && @@ -387,24 +360,6 @@ bool MatchVariableDeclaration(ir::AstNode *childNode, const NodeInfo *info) info->name; } -bool MatchClassDeclaration(ir::AstNode *childNode, const NodeInfo *info) -{ - return childNode->IsClassDeclaration() && - std::string(childNode->AsClassDeclaration()->Definition()->Ident()->Name()) == info->name; -} - -bool MatchAnnotationDeclaration(ir::AstNode *childNode, const NodeInfo *info) -{ - return childNode->IsAnnotationDeclaration() && - std::string(childNode->AsAnnotationDeclaration()->GetBaseName()->Name()) == info->name; -} - -bool MatchAnnotationUsage(ir::AstNode *childNode, const NodeInfo *info) -{ - return childNode->IsAnnotationUsage() && - std::string(childNode->AsAnnotationUsage()->GetBaseName()->Name()) == info->name; -} - bool MatchAwaitExpression(ir::AstNode *childNode, const NodeInfo *info) { if (!childNode->IsAwaitExpression()) { @@ -443,35 +398,6 @@ ir::AstNode *ExtractAwaitExpressionIdentifier(ir::AstNode *node, [[maybe_unused] return node; } -bool MatchImportSpecifier(ir::AstNode *childNode, const NodeInfo *info) -{ - return childNode->IsImportSpecifier() && - std::string(childNode->AsImportSpecifier()->Imported()->Name()) == info->name; -} - -bool MatchImportDefaultSpecifier(ir::AstNode *childNode, const NodeInfo *info) -{ - return childNode->IsImportDefaultSpecifier() && - std::string(childNode->AsImportDefaultSpecifier()->Local()->Name()) == info->name; -} - -bool MatchImportNamespaceSpecifier(ir::AstNode *childNode, const NodeInfo *info) -{ - return childNode->IsImportNamespaceSpecifier() && - std::string(childNode->AsImportNamespaceSpecifier()->Local()->Name()) == info->name; -} - -bool MatchTSTypeParameter(ir::AstNode *childNode, const NodeInfo *info) -{ - return childNode->IsTSTypeParameter() && std::string(childNode->AsTSTypeParameter()->Name()->Name()) == info->name; -} - -bool MatchEtsParameterExpression(ir::AstNode *childNode, const NodeInfo *info) -{ - return childNode->IsETSParameterExpression() && - std::string(childNode->AsETSParameterExpression()->Ident()->Name()) == info->name; -} - bool MatchSwitchStatement(ir::AstNode *childNode, const NodeInfo *info) { if (!childNode->IsSwitchStatement()) { @@ -509,14 +435,6 @@ bool MatchTsNonNullExpression(ir::AstNode *childNode, const NodeInfo *info) return false; } -bool MatchFunctionDeclaration(ir::AstNode *childNode, const NodeInfo *info) -{ - if (!childNode->IsFunctionDeclaration()) { - return false; - } - return std::string(childNode->AsFunctionDeclaration()->Function()->Id()->Name()) == info->name; -} - ir::AstNode *ExtractIdentifierFromNode(ir::AstNode *node, const NodeInfo *info) { if (node == nullptr) { -- Gitee