From 76731638e0a03e71ff3871e40a36ff206c49c61d Mon Sep 17 00:00:00 2001 From: duoge <1518199740@qq.com> Date: Mon, 15 Sep 2025 10:12:56 +0800 Subject: [PATCH] [LSP]: fix clang-tidy Signed-off-by: duoge <1518199740@qq.com> --- ets2panda/ir/srcDump.h | 4 +- ets2panda/lsp/include/node_matchers.h | 19 +- ets2panda/lsp/src/node_matchers.cpp | 228 +++++++++++++++--- .../test/unit/lsp/quick_info_api_test.cpp | 7 +- 4 files changed, 212 insertions(+), 46 deletions(-) diff --git a/ets2panda/ir/srcDump.h b/ets2panda/ir/srcDump.h index d48a42604b..eec663fac4 100644 --- a/ets2panda/ir/srcDump.h +++ b/ets2panda/ir/srcDump.h @@ -57,7 +57,7 @@ public: public: NO_COPY_SEMANTIC(Releaser); Releaser &operator=(Releaser &&other) = delete; - explicit Releaser(Releaser &&other) : lock_ {other.lock_}, prevAcquired_ {other.prevAcquired_} + Releaser(Releaser &&other) : lock_ {other.lock_}, prevAcquired_ {other.prevAcquired_} { other.lock_ = nullptr; } @@ -180,7 +180,7 @@ public: { ES2PANDA_UNREACHABLE(); } - explicit SrcDumper([[maybe_unused]] const ir::AstNode *node, [[maybe_unused]] bool isDeclgen) + explicit SrcDumper([[maybe_unused]] const ir::AstNode *node, [[maybe_unused]] bool isDeclgen): dg_(nullptr) { ES2PANDA_UNREACHABLE(); } diff --git a/ets2panda/lsp/include/node_matchers.h b/ets2panda/lsp/include/node_matchers.h index c523431858..2a78fd8881 100644 --- a/ets2panda/lsp/include/node_matchers.h +++ b/ets2panda/lsp/include/node_matchers.h @@ -21,13 +21,20 @@ #include "ir/astNode.h" #include "api.h" // NOLINTNEXTLINE (cppcoreguidelines-macro-usage) -#define DEFINE_SIMPLE_HANDLER(FunctionName, NodeType, NameAccessor, NodeTypeEnum) \ - void FunctionName(ir::AstNode *node, std::vector &result) \ - { \ - if (auto ident = node->As##NodeType()->NameAccessor()) { \ - result.emplace_back(std::string(ident->Name()), NodeTypeEnum); \ - } \ +template +void HandleNode(ark::es2panda::ir::AstNode *node, + std::vector &result, + Getter getter, + KindT kind) +{ + if (auto *typed = node->As()) { + if (auto *ident = getter(typed)) { + result.emplace_back(std::string(ident->Name()), kind); + } } +} + + namespace ark::es2panda::lsp { using NodeMatcher = std::function; diff --git a/ets2panda/lsp/src/node_matchers.cpp b/ets2panda/lsp/src/node_matchers.cpp index 33ce4f1c86..efab80dbf8 100644 --- a/ets2panda/lsp/src/node_matchers.cpp +++ b/ets2panda/lsp/src/node_matchers.cpp @@ -24,37 +24,197 @@ namespace ark::es2panda::lsp { -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) +// 原: DEFINE_SIMPLE_HANDLER(HandleClassDefinition, ClassDefinition, Ident, ir::AstNodeType::CLASS_DEFINITION) +void HandleClassDefinition(ir::AstNode *node, std::vector &result) +{ + HandleNode(node, result, + [](ir::ClassDefinition *n) { return n->Ident(); }, + ir::AstNodeType::CLASS_DEFINITION); +} + +// 原: DEFINE_SIMPLE_HANDLER(HandleClassProperty, ClassProperty, Id, ir::AstNodeType::CLASS_PROPERTY) +void HandleClassProperty(ir::AstNode *node, std::vector &result) +{ + HandleNode(node, result, + [](ir::ClassProperty *n) { return n->Id(); }, + ir::AstNodeType::CLASS_PROPERTY); +} + +// 原: DEFINE_SIMPLE_HANDLER(HandleProperty, Property, Key()->AsIdentifier, ir::AstNodeType::PROPERTY) +void HandleProperty(ir::AstNode *node, std::vector &result) +{ + HandleNode(node, result, + [](ir::Property *n) { + auto *k = n->Key(); + return k ? k->AsIdentifier() : nullptr; + }, + ir::AstNodeType::PROPERTY); +} + +// 原: DEFINE_SIMPLE_HANDLER(HandleTSInterfaceDeclaration, TSInterfaceDeclaration, Id, ...) +void HandleTSInterfaceDeclaration(ir::AstNode *node, std::vector &result) +{ + HandleNode(node, result, + [](ir::TSInterfaceDeclaration *n) { return n->Id(); }, + ir::AstNodeType::TS_INTERFACE_DECLARATION); +} + +// 原: DEFINE_SIMPLE_HANDLER(HandleTSTypeAliasDeclaration, TSTypeAliasDeclaration, Id, ...) +void HandleTSTypeAliasDeclaration(ir::AstNode *node, std::vector &result) +{ + HandleNode(node, result, + [](ir::TSTypeAliasDeclaration *n) { return n->Id(); }, + ir::AstNodeType::TS_TYPE_ALIAS_DECLARATION); +} + +// 原: DEFINE_SIMPLE_HANDLER(HandleImportSpecifier, ImportSpecifier, Imported, ...) +void HandleImportSpecifier(ir::AstNode *node, std::vector &result) +{ + HandleNode(node, result, + [](ir::ImportSpecifier *n) { return n->Imported(); }, + ir::AstNodeType::IMPORT_SPECIFIER); +} + +// 原: DEFINE_SIMPLE_HANDLER(HandleImportDefaultSpecifier, ImportDefaultSpecifier, Local, ...) +void HandleImportDefaultSpecifier(ir::AstNode *node, std::vector &result) +{ + HandleNode(node, result, + [](ir::ImportDefaultSpecifier *n) { return n->Local(); }, + ir::AstNodeType::IMPORT_DEFAULT_SPECIFIER); +} + +// 原: DEFINE_SIMPLE_HANDLER(HandleImportNamespaceSpecifier, ImportNamespaceSpecifier, Local, ...) +void HandleImportNamespaceSpecifier(ir::AstNode *node, std::vector &result) +{ + HandleNode(node, result, + [](ir::ImportNamespaceSpecifier *n) { return n->Local(); }, + ir::AstNodeType::IMPORT_NAMESPACE_SPECIFIER); +} + +// 原: DEFINE_SIMPLE_HANDLER(HandleStructDeclaration, ETSStructDeclaration, Definition()->Ident, ...) +void HandleStructDeclaration(ir::AstNode *node, std::vector &result) +{ + HandleNode(node, result, + [](ir::ETSStructDeclaration *n) { + auto *def = n->Definition(); + return def ? def->Ident() : nullptr; + }, + ir::AstNodeType::STRUCT_DECLARATION); +} + +// 原: DEFINE_SIMPLE_HANDLER(HandleClassDeclaration, ClassDeclaration, Definition()->Ident, ...) +void HandleClassDeclaration(ir::AstNode *node, std::vector &result) +{ + HandleNode(node, result, + [](ir::ClassDeclaration *n) { + auto *def = n->Definition(); + return def ? def->Ident() : nullptr; + }, + ir::AstNodeType::CLASS_DECLARATION); +} + +// 原: DEFINE_SIMPLE_HANDLER(HanleScriptFunction, ScriptFunction, Id, ...) +void HandleScriptFunction(ir::AstNode *node, std::vector &result) // 修正拼写:Handle... +{ + HandleNode(node, result, + [](ir::ScriptFunction *n) { return n->Id(); }, + ir::AstNodeType::SCRIPT_FUNCTION); +} + +// 原: DEFINE_SIMPLE_HANDLER(HandleFunctionDeclaration, FunctionDeclaration, Function()->Id, ...) +void HandleFunctionDeclaration(ir::AstNode *node, std::vector &result) +{ + HandleNode(node, result, + [](ir::FunctionDeclaration *n) { + auto *fn = n->Function(); + return fn ? fn->Id() : nullptr; + }, + ir::AstNodeType::FUNCTION_DECLARATION); +} + +// 原: DEFINE_SIMPLE_HANDLER(HandleMethodDefinition, MethodDefinition, Function()->Id, ...) +void HandleMethodDefinition(ir::AstNode *node, std::vector &result) +{ + HandleNode(node, result, + [](ir::MethodDefinition *n) { + auto *fn = n->Function(); + return fn ? fn->Id() : nullptr; + }, + ir::AstNodeType::METHOD_DEFINITION); +} + +// 原: DEFINE_SIMPLE_HANDLER(HanleTSEnumDeclaration, TSEnumDeclaration, Key, ...) +void HandleTSEnumDeclaration(ir::AstNode *node, std::vector &result) // 修正拼写:Handle... +{ + HandleNode(node, result, + [](ir::TSEnumDeclaration *n) { return n->Key(); }, + ir::AstNodeType::TS_ENUM_DECLARATION); +} + +// 原: DEFINE_SIMPLE_HANDLER(HandleVariableDeclaration, VariableDeclaration, Declarators()[0]->Id()->AsIdentifier, ...) +void HandleVariableDeclaration(ir::AstNode *node, std::vector &result) +{ + HandleNode(node, result, + [](ir::VariableDeclaration *n) { + auto &ds = n->Declarators(); + if (ds.empty() || ds[0] == nullptr) return static_cast(nullptr); + auto *id = ds[0]->Id(); + return id ? id->AsIdentifier() : nullptr; + }, + ir::AstNodeType::VARIABLE_DECLARATION); +} + +// 原: DEFINE_SIMPLE_HANDLER(HandleVariableDeclarator, VariableDeclarator, Id()->AsIdentifier, ...) +void HandleVariableDeclarator(ir::AstNode *node, std::vector &result) +{ + HandleNode(node, result, + [](ir::VariableDeclarator *n) { + auto *id = n->Id(); + return id ? id->AsIdentifier() : nullptr; + }, + ir::AstNodeType::VARIABLE_DECLARATOR); +} + +// 原: DEFINE_SIMPLE_HANDLER(HandleTSClassImplements, TSClassImplements, Expr()->AsETSTypeReference()->Part()->GetIdent, ...) +void HandleTSClassImplements(ir::AstNode *node, std::vector &result) +{ + HandleNode(node, result, + [](ir::TSClassImplements *n) { + auto *e = n->Expr(); + if (!e) return static_cast(nullptr); + auto *t = e->AsETSTypeReference(); + if (!t || !t->Part()) return static_cast(nullptr); + return t->Part()->GetIdent(); + }, + ir::AstNodeType::TS_CLASS_IMPLEMENTS); +} + +// 原: DEFINE_SIMPLE_HANDLER(HandleAnnotationDeclaration, AnnotationDeclaration, GetBaseName, ...) +void HandleAnnotationDeclaration(ir::AstNode *node, std::vector &result) +{ + HandleNode(node, result, + [](ir::AnnotationDeclaration *n) { return n->GetBaseName(); }, + ir::AstNodeType::ANNOTATION_DECLARATION); +} + +// 原: DEFINE_SIMPLE_HANDLER(HandleAnnotationUsage, AnnotationUsage, GetBaseName, ...) +void HandleAnnotationUsage(ir::AstNode *node, std::vector &result) +{ + HandleNode(node, result, + [](ir::AnnotationUsage *n) { return n->GetBaseName(); }, + ir::AstNodeType::ANNOTATION_USAGE); +} + +// 原: DEFINE_SIMPLE_HANDLER(HandleAwitExpression, AwaitExpression, Argument()->AsIdentifier, ...) +void HandleAwaitExpression(ir::AstNode *node, std::vector &result) // 修正拼写:Await +{ + HandleNode(node, result, + [](ir::AwaitExpression *n) { + auto *arg = n->Argument(); + return arg ? arg->AsIdentifier() : nullptr; + }, + ir::AstNodeType::AWAIT_EXPRESSION); +} bool MatchClassDefinition(ir::AstNode *childNode, const NodeInfo *info) { @@ -819,14 +979,14 @@ const std::unordered_map &GetNodeInfoHandlers( {ir::AstNodeType::SPREAD_ELEMENT, HandleSpeadeElement}, {ir::AstNodeType::STRUCT_DECLARATION, HandleStructDeclaration}, {ir::AstNodeType::CLASS_DECLARATION, HandleClassDeclaration}, - {ir::AstNodeType::SCRIPT_FUNCTION, HanleScriptFunction}, + {ir::AstNodeType::SCRIPT_FUNCTION, HandleScriptFunction}, {ir::AstNodeType::FUNCTION_DECLARATION, HandleFunctionDeclaration}, {ir::AstNodeType::METHOD_DEFINITION, HandleMethodDefinition}, - {ir::AstNodeType::TS_ENUM_DECLARATION, HanleTSEnumDeclaration}, + {ir::AstNodeType::TS_ENUM_DECLARATION, HandleTSEnumDeclaration}, {ir::AstNodeType::TS_ENUM_MEMBER, HandleTSEnumMember}, {ir::AstNodeType::CALL_EXPRESSION, HandleCallExpression}, {ir::AstNodeType::ANNOTATION_DECLARATION, HandleAnnotationDeclaration}, - {ir::AstNodeType::AWAIT_EXPRESSION, HandleAwitExpression}, + {ir::AstNodeType::AWAIT_EXPRESSION, HandleAwaitExpression}, {ir::AstNodeType::ANNOTATION_USAGE, HandleAnnotationUsage}}; return NODE_INFO_HANDLERS; } diff --git a/ets2panda/test/unit/lsp/quick_info_api_test.cpp b/ets2panda/test/unit/lsp/quick_info_api_test.cpp index a340cc3668..82db313010 100644 --- a/ets2panda/test/unit/lsp/quick_info_api_test.cpp +++ b/ets2panda/test/unit/lsp/quick_info_api_test.cpp @@ -472,10 +472,9 @@ TEST_F(LspQuickInfoTests, GetQuickInfoAtPositionClass) auto context = reinterpret_cast(ctx); const ark::es2panda::ir::AstNode *ast = context->parserProgram->Ast(); auto *structDefNode = ast->FindChild( - [](const auto *node) { return node->IsClassDefinition() && node->Parent()->IsETSStructDeclaration(); }); - initializer.ClassDefinitionSetFromStructModifier(ctx, reinterpret_cast(structDefNode)); - auto isFromStruct = - initializer.ClassDefinitionIsFromStructConst(ctx, reinterpret_cast(structDefNode)); + [](auto *node) { return node->IsClassDefinition() && node->Parent()->IsETSStructDeclaration(); }); + structDefNode->AsClassDefinition()->SetFromStructModifier(); + auto isFromStruct = structDefNode->AsClassDefinition()->IsFromStruct(); ASSERT_EQ(isFromStruct, true); auto quickInfo3 = lspApi->getQuickInfoAtPosition("GetQuickInfoAtPositionClass.ets", ctx, offset1); AssertQuickInfo(expectedQuickInfo1, quickInfo3); -- Gitee