From bc60ed69b45d5589bfde7599a59adb212399b906 Mon Sep 17 00:00:00 2001 From: ykiko Date: Sun, 30 Jun 2024 19:37:26 +0800 Subject: [PATCH] some refactors. --- .clang-format | 102 +++++++++++++++++++++++++++++++++++ CMakeLists.txt | 3 +- clice.md | 4 +- include/Clang/ParsedAST.h | 7 +++ include/Clang/Preamble.h | 5 ++ include/LSP/SemanticTokens.h | 68 +++++++++++------------ include/LSP/Server.h | 42 +++------------ src/LSP/Server.cpp | 43 ++++++++++++++- src/main.cpp | 9 ++-- 9 files changed, 206 insertions(+), 77 deletions(-) create mode 100644 .clang-format create mode 100644 include/Clang/ParsedAST.h create mode 100644 include/Clang/Preamble.h diff --git a/.clang-format b/.clang-format new file mode 100644 index 00000000..ae764419 --- /dev/null +++ b/.clang-format @@ -0,0 +1,102 @@ +# clang-format configuration +# compatible with clang-format 18 + +UseTab: Never +ColumnLimit: 100 + +# Indent +IndentWidth: 4 +BracedInitializerIndentWidth: 4 +AccessModifierOffset: -4 +IndentAccessModifiers: false +IndentCaseLabels: true +IndentExternBlock: Indent +IndentGotoLabels: true +IndentRequiresClause: true +IndentWrappedFunctionNames: true +NamespaceIndentation: None +LambdaBodyIndentation: Signature +BitFieldColonSpacing: Both + +# Insert +InsertBraces: false +InsertNewlineAtEOF: true +KeepEmptyLinesAtEOF: true + +# Align +AlignAfterOpenBracket: true +AlignTrailingComments: + Kind: Always + +AlignArrayOfStructures: Left +PointerAlignment: Left + +BreakAfterAttributes: Leave +BreakBeforeBinaryOperators: None +BreakBeforeConceptDeclarations: Always +BreakBeforeInlineASMColon: OnlyMultiline +BreakBeforeTernaryOperators: true +BreakConstructorInitializers: AfterColon +BreakInheritanceList: AfterColon +BreakAdjacentStringLiterals: false +BreakStringLiterals: false +CompactNamespaces: false +Cpp11BracedListStyle: true +EmptyLineAfterAccessModifier: Never +EmptyLineBeforeAccessModifier: Always + +AllowAllArgumentsOnNextLine: false +AllowAllParametersOfDeclarationOnNextLine: false +AllowBreakBeforeNoexceptSpecifier: Never +AllowShortBlocksOnASingleLine: Always +AllowShortCaseLabelsOnASingleLine: true +AllowShortCompoundRequirementOnASingleLine: true +AllowShortEnumsOnASingleLine: true +AllowShortFunctionsOnASingleLine: All +AllowShortIfStatementsOnASingleLine: Never +AllowShortLambdasOnASingleLine: None +AllowShortLoopsOnASingleLine: false +AlwaysBreakAfterReturnType: None +AlwaysBreakBeforeMultilineStrings: true +AlwaysBreakTemplateDeclarations: Yes +RequiresClausePosition: OwnLine +BinPackArguments: false +BinPackParameters: false + +# Space +SeparateDefinitionBlocks: Always +SpaceBeforeParens: Custom +SpaceBeforeParensOptions: + AfterControlStatements: false + AfterForeachMacros: false + AfterFunctionDeclarationName: false + AfterFunctionDefinitionName: false + AfterIfMacros: false + AfterOverloadedOperator: true + AfterRequiresInClause: true + AfterRequiresInExpression: false + BeforeNonEmptyParentheses: false + +SpaceBeforeRangeBasedForLoopColon: false +SpaceBeforeSquareBrackets: false +SpaceInEmptyBlock: false +SpacesBeforeTrailingComments: 2 +SpacesInAngles: Never + +SpacesInParens: Custom +SpacesInParensOptions: + InConditionalStatements: false + InCStyleCasts: false + InEmptyParentheses: false + Other: false + +SpacesInSquareBrackets: false + +# Order +QualifierAlignment: Custom +QualifierOrder: ["constexpr", "const", "inline", "static", "type"] +SortIncludes: Never +SortUsingDeclarations: LexicographicNumeric +IncludeBlocks: Merge + +WhitespaceSensitiveMacros: ["PK_PROTECTED", "LUA_PROTECTED"] diff --git a/CMakeLists.txt b/CMakeLists.txt index 751ee028..f2016cd3 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -17,7 +17,8 @@ set(PCH_HEADER "${CMAKE_SOURCE_DIR}/include/Clang/Clang.h") add_subdirectory(external/libuv) add_subdirectory(external/simdjson) -add_executable(libtooling src/main.cpp) +file(GLOB_RECURSE SOURCES src/*.cpp) +add_executable(libtooling ${SOURCES}) target_include_directories(libtooling PRIVATE "${CMAKE_SOURCE_DIR}/include") target_include_directories(libtooling PRIVATE ${LLVM_INCLUDE_DIRS}) diff --git a/clice.md b/clice.md index 76c37954..4700feeb 100644 --- a/clice.md +++ b/clice.md @@ -75,4 +75,6 @@ 这些任务从最终实现的角度来说可以主要分成三种: 1. CodeCompletion 这个需要利用 CodeCompletionConsumer 调用 Clang 提供的接口来实现,然而我们实际上可以做一些更加复杂的分析(clangd 目前没有做)。比如判断当前的是不是在 Template 语境下从而决定补全`sizeof`的时候要不要补全`...`。在补全成员的时候,似乎我们也可以获取`expr.f`中的父对象的类型,从而根据它的类型来做一些补全。有待进一步研究。 2. Semantic Tokens 等基于当前 AST 的操作,则是遍历 AST 渲染 Token 即可。 -3. 剩下很多的,例如 Find References 等等等查询功能,都是在已经索引好的文件中进行查询,不需要对语法树进行什么改动。 \ No newline at end of file +3. 剩下很多的,例如 Find References 等等等查询功能,都是在已经索引好的文件中进行查询,不需要对语法树进行什么改动。 + +### \ No newline at end of file diff --git a/include/Clang/ParsedAST.h b/include/Clang/ParsedAST.h new file mode 100644 index 00000000..e4e9ed8d --- /dev/null +++ b/include/Clang/ParsedAST.h @@ -0,0 +1,7 @@ +namespace clice { + +class ParsedAST { +private: +}; + +} // namespace clice diff --git a/include/Clang/Preamble.h b/include/Clang/Preamble.h new file mode 100644 index 00000000..70b763e7 --- /dev/null +++ b/include/Clang/Preamble.h @@ -0,0 +1,5 @@ +namespace { + +class Preamble {}; + +} // namespace diff --git a/include/LSP/SemanticTokens.h b/include/LSP/SemanticTokens.h index 9c2f8377..e881148b 100644 --- a/include/LSP/SemanticTokens.h +++ b/include/LSP/SemanticTokens.h @@ -6,42 +6,42 @@ namespace clice { // allowed to extend these and announce the values they support in the // corresponding client capability. enum class SemanticTokenTypes { - Namespace, - Type, - Class, - Enum, - Interface, - Struct, - TypeParameter, - Parameter, - Variable, - Property, - EnumMember, - Event, - Function, - Method, - Macro, - Keyword, - Modifier, - Comment, - String, - Number, - Regexp, - Operator, - Decorator // @since 3.17.0 + Namespace, + Type, + Class, + Enum, + Interface, + Struct, + TypeParameter, + Parameter, + Variable, + Property, + EnumMember, + Event, + Function, + Method, + Macro, + Keyword, + Modifier, + Comment, + String, + Number, + Regexp, + Operator, + Decorator // @since 3.17.0 }; enum SemanticTokenModifiers { - Declaration, - Definition, - Readonly, - Static, - Deprecated, - Abstract, - Async, - Modification, - Documentation, - DefaultLibrary + Declaration, + Definition, + Readonly, + Static, + Deprecated, + Abstract, + Async, + Modification, + Documentation, + DefaultLibrary }; -} // namespace clice +} // namespace clice diff --git a/include/LSP/Server.h b/include/LSP/Server.h index 02627504..321ea609 100644 --- a/include/LSP/Server.h +++ b/include/LSP/Server.h @@ -1,45 +1,15 @@ #include + namespace clice { /// core class responsible for starting the server class Server { -private: - uv_loop_t *loop; - uv_pipe_t stdin_pipe; - -private: - static void on_read(uv_stream_t *stream, ssize_t nread, const uv_buf_t *buf) { - if (nread > 0) { - printf("Read: %s\n", buf->base); - } else if (nread < 0) { - if (nread != UV_EOF) - fprintf(stderr, "Read error: %s\n", uv_err_name(nread)); - if (!uv_is_closing((uv_handle_t *)&pipe)) { - uv_close((uv_handle_t *)&pipe, NULL); - } - } - if (buf->base) { - free(buf->base); - } - } - - static void alloc_buffer(uv_handle_t *handle, size_t suggested_size, - uv_buf_t *buf) { - buf->base = (char *)malloc(suggested_size); - buf->len = suggested_size; - } + static uv_loop_t* loop; + static uv_pipe_t stdin_pipe; public: - Server() { - loop = uv_default_loop(); - uv_pipe_init(loop, &stdin_pipe, 0); - uv_pipe_open(&stdin_pipe, 0); - uv_read_start((uv_stream_t *)&stdin_pipe, alloc_buffer, on_read); - } - - ~Server() { uv_loop_close(loop); } - - int run() { return uv_run(loop, UV_RUN_DEFAULT); } + static int Initialize(); + static int Exit(); }; -} // namespace clice \ No newline at end of file +} // namespace clice diff --git a/src/LSP/Server.cpp b/src/LSP/Server.cpp index fdffe2b0..f4ef593a 100644 --- a/src/LSP/Server.cpp +++ b/src/LSP/Server.cpp @@ -1,3 +1,44 @@ #include -namespace clice {} // namespace clice \ No newline at end of file +namespace clice { + +uv_loop_t* Server::loop; +uv_pipe_t Server::stdin_pipe; + +static void on_read(uv_stream_t* stream, ssize_t nread, const uv_buf_t* buf) { + if(nread > 0) { + printf("Read: %s\n", buf->base); + } else if(nread < 0) { + if(nread != UV_EOF) { + fprintf(stderr, "Read error: %s\n", uv_err_name(nread)); + } + + if(!uv_is_closing((uv_handle_t*)&pipe)) { + uv_close((uv_handle_t*)&pipe, NULL); + } + } + + if(buf->base) { + free(buf->base); + } +} + +static void alloc_buffer(uv_handle_t* handle, size_t suggested_size, uv_buf_t* buf) { + buf->base = (char*)malloc(suggested_size); + buf->len = suggested_size; +} + +int Server::Initialize() { + loop = uv_default_loop(); + uv_pipe_init(loop, &stdin_pipe, 0); + uv_pipe_open(&stdin_pipe, 0); + uv_read_start((uv_stream_t*)&stdin_pipe, alloc_buffer, on_read); + return uv_run(loop, UV_RUN_DEFAULT); +} + +int Server::Exit() { + uv_close((uv_handle_t*)&stdin_pipe, NULL); + return uv_run(loop, UV_RUN_DEFAULT); +} + +} // namespace clice diff --git a/src/main.cpp b/src/main.cpp index 10922568..d444f66c 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -1,7 +1,8 @@ #include int main() { - clice::Server server; - server.run(); - return 0; -} \ No newline at end of file + using namespace clice; + Server::Initialize(); + Server::Exit(); + return 0; +}