diff --git a/.github/workflows/ci.yml b/.github/workflows/ci.yml index fbdc1ef..fd1d83f 100644 --- a/.github/workflows/ci.yml +++ b/.github/workflows/ci.yml @@ -34,16 +34,23 @@ jobs: path: | llvm-build llvm-project - key: clangd-linux-x86_64-v1 + key: clangd-linux-x86_64-v2 restore-keys: | clangd-linux-x86_64- + # restore-keys can hand back a tree from an older cache key without an + # exact cache-hit; reuse it and let the build step update it incrementally. - name: Checkout upstream llvm-project if: steps.cache-clangd.outputs.cache-hit != 'true' + shell: bash run: | - git clone --filter=blob:none --no-checkout --depth 1 https://github.com/llvm/llvm-project.git llvm-project - git -C llvm-project sparse-checkout set llvm clang clang-tools-extra cmake third-party libc - git -C llvm-project checkout + if [ -d llvm-project/.git ]; then + echo "Reusing llvm-project restored from a previous cache" + else + git clone --filter=blob:none --no-checkout --depth 1 https://github.com/llvm/llvm-project.git llvm-project + git -C llvm-project sparse-checkout set llvm clang clang-tools-extra cmake third-party libc + git -C llvm-project checkout + fi - name: Build Clangd from main if: steps.cache-clangd.outputs.cache-hit != 'true' @@ -66,7 +73,7 @@ jobs: -DCLANG_ENABLE_ARCMT=OFF \ -DCLANG_TOOL_CLANG_CHECK_BUILD=OFF \ -DCLANG_TOOL_CLANG_FORMAT_BUILD=OFF - ninja -C llvm-build clangd + ninja -C llvm-build clangd FileCheck not count LLVMFuzzerCLI - name: Save Clangd cache if: steps.cache-clangd.outputs.cache-hit != 'true' @@ -75,7 +82,12 @@ jobs: path: | llvm-build llvm-project - key: clangd-linux-x86_64-v1 + key: clangd-linux-x86_64-v2 + + # No-op when the cached tree already has them; covers caches saved + # before these targets were part of the clangd build step. + - name: Build LLVM test utilities (FileCheck, not, count, FuzzerCLI) + run: ninja -C llvm-build FileCheck not count LLVMFuzzerCLI - name: Configure pseudo-parser run: | @@ -88,17 +100,10 @@ jobs: -DPSEUDO_ENABLE_TESTS=ON - name: Build pseudo-parser feature module and tools - run: | - ninja -C build clangd_pseudo pseudo-tool - ninja -C build ClangPseudoTests || true + run: ninja -C build clangd_pseudo pseudo-tool - - name: Run unit tests (if available) - run: | - if [ -f ./build/unittests/ClangPseudoTests ]; then - ./build/unittests/ClangPseudoTests - else - echo "Skipping ClangPseudoTests (LLVMTestingSupport not in distribution package)" - fi + - name: Run unit and lit tests + run: ninja -C build ClangPseudoTests PseudoModuleTests check-clang-pseudo - name: Test loading feature module into clangd (-load) and LSP diagnostics shell: bash @@ -158,16 +163,23 @@ jobs: path: | llvm-build llvm-project - key: clangd-macos-arm64-v1 + key: clangd-macos-arm64-v2 restore-keys: | clangd-macos-arm64- + # restore-keys can hand back a tree from an older cache key without an + # exact cache-hit; reuse it and let the build step update it incrementally. - name: Checkout upstream llvm-project if: steps.cache-clangd.outputs.cache-hit != 'true' + shell: bash run: | - git clone --filter=blob:none --no-checkout --depth 1 https://github.com/llvm/llvm-project.git llvm-project - git -C llvm-project sparse-checkout set llvm clang clang-tools-extra cmake third-party libc - git -C llvm-project checkout + if [ -d llvm-project/.git ]; then + echo "Reusing llvm-project restored from a previous cache" + else + git clone --filter=blob:none --no-checkout --depth 1 https://github.com/llvm/llvm-project.git llvm-project + git -C llvm-project sparse-checkout set llvm clang clang-tools-extra cmake third-party libc + git -C llvm-project checkout + fi - name: Build Clangd from main if: steps.cache-clangd.outputs.cache-hit != 'true' @@ -190,7 +202,7 @@ jobs: -DCLANG_ENABLE_ARCMT=OFF \ -DCLANG_TOOL_CLANG_CHECK_BUILD=OFF \ -DCLANG_TOOL_CLANG_FORMAT_BUILD=OFF - ninja -C llvm-build clangd + ninja -C llvm-build clangd FileCheck not count LLVMFuzzerCLI - name: Save Clangd cache if: steps.cache-clangd.outputs.cache-hit != 'true' @@ -199,7 +211,12 @@ jobs: path: | llvm-build llvm-project - key: clangd-macos-arm64-v1 + key: clangd-macos-arm64-v2 + + # No-op when the cached tree already has them; covers caches saved + # before these targets were part of the clangd build step. + - name: Build LLVM test utilities (FileCheck, not, count, FuzzerCLI) + run: ninja -C llvm-build FileCheck not count LLVMFuzzerCLI - name: Configure pseudo-parser run: | @@ -212,17 +229,10 @@ jobs: -DPSEUDO_ENABLE_TESTS=ON - name: Build pseudo-parser feature module and tools - run: | - ninja -C build clangd_pseudo pseudo-tool - ninja -C build ClangPseudoTests || true + run: ninja -C build clangd_pseudo pseudo-tool - - name: Run unit tests (if available) - run: | - if [ -f ./build/unittests/ClangPseudoTests ]; then - ./build/unittests/ClangPseudoTests - else - echo "Skipping ClangPseudoTests (LLVMTestingSupport not in distribution package)" - fi + - name: Run unit and lit tests + run: ninja -C build ClangPseudoTests PseudoModuleTests check-clang-pseudo - name: Test loading feature module into clangd (-load) and LSP diagnostics shell: bash @@ -288,16 +298,23 @@ jobs: path: | llvm-build llvm-project - key: clangd-windows-x86_64-v1 + key: clangd-windows-x86_64-v2 restore-keys: | clangd-windows-x86_64- + # restore-keys can hand back a tree from an older cache key without an + # exact cache-hit; reuse it and let the build step update it incrementally. - name: Checkout upstream llvm-project if: steps.cache-clangd.outputs.cache-hit != 'true' + shell: bash run: | - git clone --filter=blob:none --no-checkout --depth 1 https://github.com/llvm/llvm-project.git llvm-project - git -C llvm-project sparse-checkout set llvm clang clang-tools-extra cmake third-party libc - git -C llvm-project checkout + if [ -d llvm-project/.git ]; then + echo "Reusing llvm-project restored from a previous cache" + else + git clone --filter=blob:none --no-checkout --depth 1 https://github.com/llvm/llvm-project.git llvm-project + git -C llvm-project sparse-checkout set llvm clang clang-tools-extra cmake third-party libc + git -C llvm-project checkout + fi - name: Build Clangd with plugin symbol exports (LLVM_EXPORT_SYMBOLS_FOR_PLUGINS=ON) if: steps.cache-clangd.outputs.cache-hit != 'true' @@ -322,7 +339,7 @@ jobs: -DCLANG_ENABLE_ARCMT=OFF ^ -DCLANG_TOOL_CLANG_CHECK_BUILD=OFF ^ -DCLANG_TOOL_CLANG_FORMAT_BUILD=OFF - ninja -C llvm-build clangd + ninja -C llvm-build clangd FileCheck not count LLVMFuzzerCLI - name: Save Clangd cache if: steps.cache-clangd.outputs.cache-hit != 'true' @@ -331,7 +348,13 @@ jobs: path: | llvm-build llvm-project - key: clangd-windows-x86_64-v1 + key: clangd-windows-x86_64-v2 + + # No-op when the cached tree already has them; covers caches saved + # before these targets were part of the clangd build step. + - name: Build LLVM test utilities (FileCheck, not, count, FuzzerCLI) + shell: cmd + run: ninja -C llvm-build FileCheck not count LLVMFuzzerCLI - name: Configure pseudo-parser shell: cmd @@ -344,13 +367,17 @@ jobs: -DCLANGD_INCLUDE_DIRS="%CD%/llvm-project/clang-tools-extra/clangd" ^ -DCMAKE_SHARED_LINKER_FLAGS="/FORCE:MULTIPLE" ^ -DCMAKE_MODULE_LINKER_FLAGS="/FORCE:MULTIPLE" ^ - -DPSEUDO_ENABLE_TESTS=OFF + -DPSEUDO_ENABLE_TESTS=ON - name: Build pseudo-parser feature module and tools shell: cmd run: | ninja -C build -v clangd_pseudo pseudo-tool + - name: Run unit and lit tests + shell: cmd + run: ninja -C build ClangPseudoTests PseudoModuleTests check-clang-pseudo + - name: Test feature module LSP diagnostics with clangd (-load) shell: bash run: | diff --git a/CMakeLists.txt b/CMakeLists.txt index 3e426a2..f55f3ea 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -5,6 +5,8 @@ set(CMAKE_CXX_STANDARD 17) set(CMAKE_CXX_STANDARD_REQUIRED ON) set(CMAKE_POSITION_INDEPENDENT_CODE ON) +list(APPEND CMAKE_MODULE_PATH "${CMAKE_CURRENT_SOURCE_DIR}/cmake") + if(NOT TARGET LibEdit::LibEdit) add_library(LibEdit::LibEdit INTERFACE IMPORTED) endif() @@ -32,6 +34,13 @@ if(NOT DEFINED LLVM_MAIN_SRC_DIR) ${LLVM_LIBRARY_DIRS} ) add_definitions(${LLVM_DEFINITIONS}) + + # Standalone builds have no LLVM build tree to inherit output locations + # from. Collect executables under bin/ and libraries under lib/ so the lit + # configuration (CURRENT_TOOLS_DIR, SHLIBDIR) can find the tools it runs. + set(LLVM_RUNTIME_OUTPUT_INTDIR "${CMAKE_BINARY_DIR}/${CMAKE_CFG_INTDIR}/bin") + set(LLVM_LIBRARY_OUTPUT_INTDIR "${CMAKE_BINARY_DIR}/${CMAKE_CFG_INTDIR}/lib") + set(LLVM_SHLIB_OUTPUT_INTDIR "${LLVM_LIBRARY_OUTPUT_INTDIR}") endif() if(MSVC) @@ -57,7 +66,39 @@ endif() if(NOT CLANGD_INCLUDE_DIRS) message(WARNING "CLANGD_INCLUDE_DIRS was not found. Please specify -DCLANGD_INCLUDE_DIRS=/path/to/clang-tools-extra/clangd if building clangd_pseudo.") +else() + # Normalize to a CMake-style path. A native Windows path with backslashes + # breaks LLVM's macro-based helpers (add_unittest, add_llvm_executable), + # which re-parse their arguments and treat "\a" as an escape sequence. + get_filename_component(CLANGD_INCLUDE_DIRS "${CLANGD_INCLUDE_DIRS}" ABSOLUTE) +endif() + +# Locate an llvm-project source tree. Tests need it for gtest, the LLVM +# testing libraries, lit, and clangd's test support sources. +set(PSEUDO_LLVM_SOURCE_DIR "" CACHE PATH + "Path to an llvm-project checkout matching the LLVM package in use (used to build test dependencies)") +if(NOT PSEUDO_LLVM_SOURCE_DIR) + if(DEFINED ENV{PSEUDO_LLVM_SOURCE_DIR}) + set(PSEUDO_LLVM_SOURCE_DIR "$ENV{PSEUDO_LLVM_SOURCE_DIR}") + elseif(CLANGD_INCLUDE_DIRS AND EXISTS "${CLANGD_INCLUDE_DIRS}/../../llvm/utils/lit/lit.py") + get_filename_component(PSEUDO_LLVM_SOURCE_DIR "${CLANGD_INCLUDE_DIRS}/../.." ABSOLUTE) + elseif(LLVM_BUILD_MAIN_SRC_DIR AND EXISTS "${LLVM_BUILD_MAIN_SRC_DIR}/../third-party/unittest") + get_filename_component(PSEUDO_LLVM_SOURCE_DIR "${LLVM_BUILD_MAIN_SRC_DIR}/.." ABSOLUTE) + endif() endif() +if(PSEUDO_LLVM_SOURCE_DIR) + # Normalize user-supplied values too: the path ends up in a C++ string + # literal, where native Windows backslashes would become escape sequences. + get_filename_component(PSEUDO_LLVM_SOURCE_DIR "${PSEUDO_LLVM_SOURCE_DIR}" ABSOLUTE) +endif() + +# Python drives the resource bundler in tool/ and lit in test/. Find it once +# here: find_package() results are directory-scoped, and add_lit_target builds +# its command from Python3_EXECUTABLE, so a lookup inside tool/ would leave +# test/ invoking lit.py bare. Unix runs that through the shebang; Windows +# hands it to the .py file association, which runs detached and reports +# success without executing anything visible. +find_package(Python3 REQUIRED COMPONENTS Interpreter) include_directories(BEFORE include @@ -74,7 +115,35 @@ if(CLANGD_INCLUDE_DIRS) add_subdirectory(clangd) endif() -option(PSEUDO_ENABLE_TESTS "Build pseudo-parser tests" ON) +# Tests default to ON when their dependencies can be satisfied, so a plain +# configure against an LLVM package without testing libraries and without an +# llvm-project tree still builds the plugin. Once tests are ON, a missing +# dependency is a configure error (see cmake/LLVMTestingLibs.cmake). +# PseudoModuleTests has extra prerequisites (clangDaemon, clangd's test +# scaffolding) and is skipped with a status message when they are absent, so +# ClangPseudoTests and the lit suite still build; CI builds that target +# explicitly so a skip cannot pass unnoticed there. +if(TARGET LLVMTestingSupport + OR (PSEUDO_LLVM_SOURCE_DIR AND EXISTS "${PSEUDO_LLVM_SOURCE_DIR}/third-party/unittest/CMakeLists.txt")) + set(PSEUDO_ENABLE_TESTS_DEFAULT ON) +else() + set(PSEUDO_ENABLE_TESTS_DEFAULT OFF) + message(STATUS "Test dependencies not found; PSEUDO_ENABLE_TESTS defaults to OFF. " + "Set PSEUDO_LLVM_SOURCE_DIR to an llvm-project checkout to enable them.") +endif() +option(PSEUDO_ENABLE_TESTS "Build pseudo-parser tests" ${PSEUDO_ENABLE_TESTS_DEFAULT}) +option(PSEUDO_ENABLE_FUZZER "Build clang-pseudo-fuzzer even when tests are off (the lit suite always builds it)" OFF) +option(PSEUDO_ENABLE_BENCHMARKS "Build pseudo-parser benchmarks (needs LLVM's google benchmark target)" OFF) + +if(PSEUDO_ENABLE_TESTS OR PSEUDO_ENABLE_FUZZER) + add_subdirectory(fuzzer) +endif() + +if(PSEUDO_ENABLE_BENCHMARKS) + add_subdirectory(benchmarks) +endif() + if(PSEUDO_ENABLE_TESTS) add_subdirectory(unittests) + add_subdirectory(test) endif() diff --git a/README.md b/README.md index 90bb3eb..0568e3d 100644 --- a/README.md +++ b/README.md @@ -89,13 +89,41 @@ cmake -B build -G Ninja \ -DCLANGD_INCLUDE_DIRS=/path/to/llvm-project/clang-tools-extra/clangd \ -DPSEUDO_ENABLE_TESTS=ON -# 3. Build the dynamic feature module, CLI tool, and tests -ninja -C build clangd_pseudo clang-pseudo ClangPseudoTests +# 3. Build the dynamic feature module and the CLI tool +ninja -C build clangd_pseudo pseudo-tool -# 4. Run tests -./build/unittests/ClangPseudoTests +# 4. Build and run every test: lit regression tests, ClangPseudoTests and +# PseudoModuleTests (the gtest binaries run through the lit shim in test/Unit) +ninja -C build check-clang-pseudo ``` +### Test dependencies + +The tests need gtest, LLVM's testing libraries, `lit`, `FileCheck`, `not`, +`count`, and clangd's test scaffolding. Installed LLVM packages do not ship +most of these, so the build compiles them from an `llvm-project` source tree +that matches the LLVM you build against. That tree is found automatically +when `CLANGD_INCLUDE_DIRS` points inside one (as in the example above); +otherwise pass `-DPSEUDO_LLVM_SOURCE_DIR=/path/to/llvm-project`. `FileCheck`, +`not` and `count` are taken from the LLVM `bin/` directory, and the fuzzer +links `LLVMFuzzerCLI`, so build them in your LLVM tree if they are missing +(`ninja -C llvm-build FileCheck not count LLVMFuzzerCLI`). + +`PseudoModuleTests` additionally needs the `clangDaemon` library, which an +LLVM build tree that built `clangd` exports, and clangd's test scaffolding +from `clang-tools-extra/clangd/unittests`. When either is missing, that one +target is skipped with a status message and the other tests still build. + +`PSEUDO_ENABLE_TESTS` defaults to ON when these dependencies are found and +to OFF otherwise, so a plain configure always builds the plugin and the tool. +With tests ON, a missing dependency is a configure error rather than a +silent skip. + +Optional targets: `-DPSEUDO_ENABLE_FUZZER=ON` builds `clang-pseudo-fuzzer` +without the tests (the lit suite always builds it); +`-DPSEUDO_ENABLE_BENCHMARKS=ON` builds `ClangPseudoBenchmark` and requires +LLVM's `benchmark` target (`LLVM_INCLUDE_BENCHMARKS=ON`). + --- ## Repository Structure @@ -107,7 +135,11 @@ pseudo-parser/ ├── gen/ # clang-pseudo-gen code generator ├── tool/ # Standalone clang-pseudo CLI tool and HTML forest browser ├── clangd/ # Clangd FeatureModule implementation (PseudoModule) -├── unittests/ # Unit test suite +├── unittests/ # gtest suites (core parser and PseudoModule) +├── test/ # lit regression tests +├── fuzzer/ # clang-pseudo-fuzzer +├── benchmarks/ # Parser benchmark (opt-in) +├── cmake/ # Build helpers (test dependency fallback) ├── docs/ # Design documents and grammar disambiguation notes └── .github/workflows/ # CI pipeline for Linux and macOS ``` diff --git a/benchmarks/CMakeLists.txt b/benchmarks/CMakeLists.txt index 859db99..3b5882c 100644 --- a/benchmarks/CMakeLists.txt +++ b/benchmarks/CMakeLists.txt @@ -1,9 +1,22 @@ +if(NOT TARGET benchmark) + message(FATAL_ERROR + "PSEUDO_ENABLE_BENCHMARKS needs LLVM's google benchmark target, which is " + "only exported from an LLVM build tree configured with " + "LLVM_INCLUDE_BENCHMARKS=ON.") +endif() + add_benchmark(ClangPseudoBenchmark Benchmark.cpp) target_link_libraries(ClangPseudoBenchmark PRIVATE - clangPseudo - clangPseudoCLI - clangPseudoGrammar + Pseudo + PseudoCLI + PseudoGrammar LLVMSupport ) + +clang_target_link_libraries(ClangPseudoBenchmark + PRIVATE + clangBasic + clangLex + ) diff --git a/clangd/CMakeLists.txt b/clangd/CMakeLists.txt index b266b83..5040af7 100644 --- a/clangd/CMakeLists.txt +++ b/clangd/CMakeLists.txt @@ -38,23 +38,22 @@ if(WIN32 AND NOT TARGET clangd) endif() endif() -add_library( - clangd_pseudo - MODULE - ${PSEUDO_MODULE_SOURCES} -) +# The module sources are compiled once into an object library that is shared +# by the loadable plugin and by PseudoModuleTests, which links the same +# objects into a test executable. +add_library(obj.clangd_pseudo OBJECT ${PSEUDO_MODULE_SOURCES}) if(COMMAND llvm_update_compile_flags) - llvm_update_compile_flags(clangd_pseudo) + llvm_update_compile_flags(obj.clangd_pseudo) endif() if(MSVC) - target_compile_options(clangd_pseudo PRIVATE /GR-) + target_compile_options(obj.clangd_pseudo PRIVATE /GR-) else() - target_compile_options(clangd_pseudo PRIVATE -fno-rtti) + target_compile_options(obj.clangd_pseudo PRIVATE -fno-rtti) endif() -target_include_directories(clangd_pseudo PRIVATE +target_include_directories(obj.clangd_pseudo PUBLIC "${CMAKE_CURRENT_SOURCE_DIR}" "${CLANGD_INCLUDE_DIRS}" "${CLANGD_INCLUDE_DIRS}/../include-cleaner/include" @@ -62,6 +61,29 @@ target_include_directories(clangd_pseudo PRIVATE "${CMAKE_BINARY_DIR}/include" ) +target_link_libraries(obj.clangd_pseudo + PUBLIC + Pseudo + PseudoCXX + PseudoGrammar +) + +add_library( + clangd_pseudo + MODULE + PluginSymbols.cpp +) + +if(COMMAND llvm_update_compile_flags) + llvm_update_compile_flags(clangd_pseudo) +endif() + +if(MSVC) + target_compile_options(clangd_pseudo PRIVATE /GR-) +else() + target_compile_options(clangd_pseudo PRIVATE -fno-rtti) +endif() + if(APPLE) set_target_properties(clangd_pseudo PROPERTIES SUFFIX ".dylib") target_link_options(clangd_pseudo PRIVATE @@ -77,9 +99,7 @@ endif() target_link_libraries(clangd_pseudo PRIVATE - Pseudo - PseudoCXX - PseudoGrammar + obj.clangd_pseudo ) if(WIN32) diff --git a/clangd/PluginSymbols.cpp b/clangd/PluginSymbols.cpp new file mode 100644 index 0000000..d6a9ea4 --- /dev/null +++ b/clangd/PluginSymbols.cpp @@ -0,0 +1,155 @@ +//===--- PluginSymbols.cpp - Symbols the loadable plugin must define -*- C++ -*-===// +// +// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. +// See https://llvm.org/LICENSE.txt for license information. +// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception +// +//===----------------------------------------------------------------------===// +// +// Definitions that exist only because the module is loaded into an +// unmodified clangd as a shared library: symbols the host does not export, +// and fallbacks for mismatched RTTI/ABI-check settings. This file is compiled +// into the clangd_pseudo plugin only. PseudoModuleTests links the module +// objects against the real clangd and clang libraries, where these would be +// duplicate definitions. +// +//===----------------------------------------------------------------------===// + +#include "CodeComplete.h" +#include "Protocol.h" +#include "clang/Basic/CharInfo.h" +#include "llvm/Config/abi-breaking.h" +#include "llvm/Support/raw_ostream.h" +#include + +namespace llvm { +#if LLVM_ENABLE_ABI_BREAKING_CHECKS +int EnableABIBreakingChecks = 0; +#else +int DisableABIBreakingChecks = 0; +#endif + +#if defined(_WIN32) +// Fallback definition for operator<<(raw_ostream&, std::nullopt_t) when not +// exported from clangd.exe on Windows. +raw_ostream &operator<<(raw_ostream &OS, std::nullopt_t) { + return OS << "None"; +} +#endif +} // namespace llvm + +#if !defined(_WIN32) +// Fallback symbol definition for typeinfo of llvm::ErrorInfoBase. +// This prevents dlopen failure when the host clangd was built with -fno-rtti +// or when external toolchains have mismatched RTTI settings. +extern "C" { +__attribute__((weak, visibility("default"))) +extern const char _ZTSN4llvm13ErrorInfoBaseE[] = "N4llvm13ErrorInfoBaseE"; + +__attribute__((weak, visibility("default"))) +extern const void *_ZTIN4llvm13ErrorInfoBaseE[2] = { + (const void *)0, + (const void *)_ZTSN4llvm13ErrorInfoBaseE +}; +} +#endif + + +namespace clang { + +#if defined(_WIN32) +namespace charinfo { +// Fallback definition for InfoTable on Windows. +// In LLVM's Windows export script (extract_symbols.py), only variables in llvm:: +// are exported from clangd.exe; variables in clang:: are omitted. Supplying a +// definition in the plugin DLL satisfies the link requirements without +// depending on clangBasic. It is an ordinary definition, not selectany, for the +// same reason as the clangd statics below: /Zc:inline drops unreferenced +// COMDAT data. +const uint16_t InfoTable[256] = { + // 0 NUL 1 SOH 2 STX 3 ETX + // 4 EOT 5 ENQ 6 ACK 7 BEL + 0 , 0 , 0 , 0 , + 0 , 0 , 0 , 0 , + // 8 BS 9 HT 10 NL 11 VT + //12 NP 13 CR 14 SO 15 SI + 0 , CHAR_HORZ_WS, CHAR_VERT_WS, CHAR_HORZ_WS, + CHAR_HORZ_WS, CHAR_VERT_WS, 0 , 0 , + //16 DLE 17 DC1 18 DC2 19 DC3 + //20 DC4 21 NAK 22 SYN 23 ETB + 0 , 0 , 0 , 0 , + 0 , 0 , 0 , 0 , + //24 CAN 25 EM 26 SUB 27 ESC + //28 FS 29 GS 30 RS 31 US + 0 , 0 , 0 , 0 , + 0 , 0 , 0 , 0 , + //32 SP 33 ! 34 " 35 # + //36 $ 37 % 38 & 39 ' + CHAR_SPACE , CHAR_PUNCT , CHAR_PUNCT , CHAR_PUNCT , + CHAR_PUNCT , CHAR_PUNCT , CHAR_PUNCT , CHAR_PUNCT , + //40 ( 41 ) 42 * 43 + + //44 , 45 - 46 . 47 / + CHAR_PUNCT , CHAR_PUNCT , CHAR_PUNCT , CHAR_PUNCT , + CHAR_PUNCT , CHAR_PUNCT , CHAR_PERIOD , CHAR_PUNCT , + //48 0 49 1 50 2 51 3 + //52 4 53 5 54 6 55 7 + CHAR_DIGIT , CHAR_DIGIT , CHAR_DIGIT , CHAR_DIGIT , + CHAR_DIGIT , CHAR_DIGIT , CHAR_DIGIT , CHAR_DIGIT , + //56 8 57 9 58 : 59 ; + //60 < 61 = 62 > 63 ? + CHAR_DIGIT , CHAR_DIGIT , CHAR_PUNCT , CHAR_PUNCT , + CHAR_PUNCT , CHAR_PUNCT , CHAR_PUNCT , CHAR_PUNCT , + //64 @ 65 A 66 B 67 C + //68 D 69 E 70 F 71 G + CHAR_PUNCT , CHAR_XUPPER , CHAR_XUPPER , CHAR_XUPPER , + CHAR_XUPPER , CHAR_XUPPER , CHAR_XUPPER , CHAR_UPPER , + //72 H 73 I 74 J 75 K + //76 L 77 M 78 N 79 O + CHAR_UPPER , CHAR_UPPER , CHAR_UPPER , CHAR_UPPER , + CHAR_UPPER , CHAR_UPPER , CHAR_UPPER , CHAR_UPPER , + //80 P 81 Q 82 R 83 S + //84 T 85 U 86 V 87 W + CHAR_UPPER , CHAR_UPPER , CHAR_UPPER , CHAR_UPPER , + CHAR_UPPER , CHAR_UPPER , CHAR_UPPER , CHAR_UPPER , + //88 X 89 Y 90 Z 91 [ + //92 \ 93 ] 94 ^ 95 _ + CHAR_UPPER , CHAR_UPPER , CHAR_UPPER , CHAR_PUNCT , + CHAR_PUNCT , CHAR_PUNCT , CHAR_PUNCT , CHAR_UNDER , + //96 ` 97 a 98 b 99 c + //100 d 101 e 102 f 103 g + CHAR_PUNCT , CHAR_XLOWER , CHAR_XLOWER , CHAR_XLOWER , + CHAR_XLOWER , CHAR_XLOWER , CHAR_XLOWER , CHAR_LOWER , + //104 h 105 i 106 j 107 k + //108 l 109 m 110 n 111 o + CHAR_LOWER , CHAR_LOWER , CHAR_LOWER , CHAR_LOWER , + CHAR_LOWER , CHAR_LOWER , CHAR_LOWER , CHAR_LOWER , + //112 p 113 q 114 r 115 s + //116 t 117 u 118 v 119 w + CHAR_LOWER , CHAR_LOWER , CHAR_LOWER , CHAR_LOWER , + CHAR_LOWER , CHAR_LOWER , CHAR_LOWER , CHAR_LOWER , + //120 x 121 y 122 z 123 { + //124 | 125 } 126 ~ 127 DEL + CHAR_LOWER , CHAR_LOWER , CHAR_LOWER , CHAR_PUNCT , + CHAR_PUNCT , CHAR_PUNCT , CHAR_PUNCT , 0 +}; +} // namespace charinfo +#endif + +namespace clangd { + +#if defined(_WIN32) +// Fallback definitions for static member variables in clang::clangd that are not +// exported from clangd.exe on Windows. In LLVM's Windows export script +// (extract_symbols.py), only variables in llvm:: are exported; variables in +// clang:: are omitted. These are ordinary definitions rather than +// __declspec(selectany) ones: selectany data is COMDAT, and the compiler +// (/Zc:inline) drops COMDAT data that nothing in this translation unit +// references, which would leave the plugin with unresolved externals. +char LSPError::ID = 0; +const CodeCompleteOptions::CodeCompletionRankingModel + CodeCompleteOptions::DefaultRankingModel = + CodeCompleteOptions::DecisionForest; +#endif + +} // namespace clangd +} // namespace clang diff --git a/clangd/PseudoModule.cpp b/clangd/PseudoModule.cpp index 9b625e5..fc7b65e 100644 --- a/clangd/PseudoModule.cpp +++ b/clangd/PseudoModule.cpp @@ -34,130 +34,9 @@ #include #include -namespace llvm { -#if LLVM_ENABLE_ABI_BREAKING_CHECKS -int EnableABIBreakingChecks = 0; -#else -int DisableABIBreakingChecks = 0; -#endif - -#if defined(_WIN32) -// Fallback definition for operator<<(raw_ostream&, std::nullopt_t) when not -// exported from clangd.exe on Windows. -raw_ostream &operator<<(raw_ostream &OS, std::nullopt_t) { - return OS << "None"; -} -#endif -} // namespace llvm - -#if !defined(_WIN32) -// Fallback symbol definition for typeinfo of llvm::ErrorInfoBase. -// This prevents dlopen failure when the host clangd was built with -fno-rtti -// or when external toolchains have mismatched RTTI settings. -extern "C" { -__attribute__((weak, visibility("default"))) -extern const char _ZTSN4llvm13ErrorInfoBaseE[] = "N4llvm13ErrorInfoBaseE"; - -__attribute__((weak, visibility("default"))) -extern const void *_ZTIN4llvm13ErrorInfoBaseE[2] = { - (const void *)0, - (const void *)_ZTSN4llvm13ErrorInfoBaseE -}; -} -#endif - namespace clang { - -#if defined(_WIN32) -namespace charinfo { -// Fallback definition for InfoTable on Windows. -// In LLVM's Windows export script (extract_symbols.py), only variables in llvm:: -// are exported from clangd.exe; variables in clang:: are omitted. Supplying a -// selectany definition in the plugin DLL satisfies the link requirements without -// depending on clangBasic. -__declspec(selectany) const uint16_t InfoTable[256] = { - // 0 NUL 1 SOH 2 STX 3 ETX - // 4 EOT 5 ENQ 6 ACK 7 BEL - 0 , 0 , 0 , 0 , - 0 , 0 , 0 , 0 , - // 8 BS 9 HT 10 NL 11 VT - //12 NP 13 CR 14 SO 15 SI - 0 , CHAR_HORZ_WS, CHAR_VERT_WS, CHAR_HORZ_WS, - CHAR_HORZ_WS, CHAR_VERT_WS, 0 , 0 , - //16 DLE 17 DC1 18 DC2 19 DC3 - //20 DC4 21 NAK 22 SYN 23 ETB - 0 , 0 , 0 , 0 , - 0 , 0 , 0 , 0 , - //24 CAN 25 EM 26 SUB 27 ESC - //28 FS 29 GS 30 RS 31 US - 0 , 0 , 0 , 0 , - 0 , 0 , 0 , 0 , - //32 SP 33 ! 34 " 35 # - //36 $ 37 % 38 & 39 ' - CHAR_SPACE , CHAR_PUNCT , CHAR_PUNCT , CHAR_PUNCT , - CHAR_PUNCT , CHAR_PUNCT , CHAR_PUNCT , CHAR_PUNCT , - //40 ( 41 ) 42 * 43 + - //44 , 45 - 46 . 47 / - CHAR_PUNCT , CHAR_PUNCT , CHAR_PUNCT , CHAR_PUNCT , - CHAR_PUNCT , CHAR_PUNCT , CHAR_PERIOD , CHAR_PUNCT , - //48 0 49 1 50 2 51 3 - //52 4 53 5 54 6 55 7 - CHAR_DIGIT , CHAR_DIGIT , CHAR_DIGIT , CHAR_DIGIT , - CHAR_DIGIT , CHAR_DIGIT , CHAR_DIGIT , CHAR_DIGIT , - //56 8 57 9 58 : 59 ; - //60 < 61 = 62 > 63 ? - CHAR_DIGIT , CHAR_DIGIT , CHAR_PUNCT , CHAR_PUNCT , - CHAR_PUNCT , CHAR_PUNCT , CHAR_PUNCT , CHAR_PUNCT , - //64 @ 65 A 66 B 67 C - //68 D 69 E 70 F 71 G - CHAR_PUNCT , CHAR_XUPPER , CHAR_XUPPER , CHAR_XUPPER , - CHAR_XUPPER , CHAR_XUPPER , CHAR_XUPPER , CHAR_UPPER , - //72 H 73 I 74 J 75 K - //76 L 77 M 78 N 79 O - CHAR_UPPER , CHAR_UPPER , CHAR_UPPER , CHAR_UPPER , - CHAR_UPPER , CHAR_UPPER , CHAR_UPPER , CHAR_UPPER , - //80 P 81 Q 82 R 83 S - //84 T 85 U 86 V 87 W - CHAR_UPPER , CHAR_UPPER , CHAR_UPPER , CHAR_UPPER , - CHAR_UPPER , CHAR_UPPER , CHAR_UPPER , CHAR_UPPER , - //88 X 89 Y 90 Z 91 [ - //92 \ 93 ] 94 ^ 95 _ - CHAR_UPPER , CHAR_UPPER , CHAR_UPPER , CHAR_PUNCT , - CHAR_PUNCT , CHAR_PUNCT , CHAR_PUNCT , CHAR_UNDER , - //96 ` 97 a 98 b 99 c - //100 d 101 e 102 f 103 g - CHAR_PUNCT , CHAR_XLOWER , CHAR_XLOWER , CHAR_XLOWER , - CHAR_XLOWER , CHAR_XLOWER , CHAR_XLOWER , CHAR_LOWER , - //104 h 105 i 106 j 107 k - //108 l 109 m 110 n 111 o - CHAR_LOWER , CHAR_LOWER , CHAR_LOWER , CHAR_LOWER , - CHAR_LOWER , CHAR_LOWER , CHAR_LOWER , CHAR_LOWER , - //112 p 113 q 114 r 115 s - //116 t 117 u 118 v 119 w - CHAR_LOWER , CHAR_LOWER , CHAR_LOWER , CHAR_LOWER , - CHAR_LOWER , CHAR_LOWER , CHAR_LOWER , CHAR_LOWER , - //120 x 121 y 122 z 123 { - //124 | 125 } 126 ~ 127 DEL - CHAR_LOWER , CHAR_LOWER , CHAR_LOWER , CHAR_PUNCT , - CHAR_PUNCT , CHAR_PUNCT , CHAR_PUNCT , 0 -}; -} // namespace charinfo -#endif - namespace clangd { -#if defined(_WIN32) -// Fallback definitions for static member variables in clang::clangd that are not -// exported from clangd.exe on Windows. In LLVM's Windows export script -// (extract_symbols.py), only variables in llvm:: are exported; variables in -// clang:: are omitted. Supplying local definitions within the module DLL -// satisfies the link requirements without conflicting with the host executable. -__declspec(selectany) char LSPError::ID = 0; -__declspec(selectany) const CodeCompleteOptions::CodeCompletionRankingModel - CodeCompleteOptions::DefaultRankingModel = - CodeCompleteOptions::DecisionForest; -#endif - namespace { static Location *getToggle(const TextDocumentPositionParams &Point, diff --git a/cmake/LLVMTestingLibs.cmake b/cmake/LLVMTestingLibs.cmake new file mode 100644 index 0000000..8941d17 --- /dev/null +++ b/cmake/LLVMTestingLibs.cmake @@ -0,0 +1,66 @@ +# Provide the gtest and LLVM testing libraries that unit tests link against. +# +# LLVM only exports llvm_gtest, llvm_gtest_main, LLVMTestingSupport and +# LLVMTestingAnnotations from a build tree configured with +# LLVM_INCLUDE_TESTS=ON. Installed LLVM packages (distro, Homebrew) and the +# CI build of clangd do not ship them. When they are missing, build them from +# an llvm-project source tree instead, using LLVM's own CMake files so the +# result matches what LLVM would have produced. +# +# Requires PSEUDO_LLVM_SOURCE_DIR to point at an llvm-project checkout that +# matches the LLVM version found by find_package(LLVM). + +function(pseudo_require_llvm_testing_libs) + set(missing "") + foreach(target llvm_gtest llvm_gtest_main LLVMTestingSupport LLVMTestingAnnotations) + if(NOT TARGET ${target}) + list(APPEND missing ${target}) + endif() + endforeach() + if(NOT missing) + return() + endif() + + string(REPLACE ";" ", " missing_list "${missing}") + if(NOT PSEUDO_LLVM_SOURCE_DIR OR NOT EXISTS "${PSEUDO_LLVM_SOURCE_DIR}/third-party/unittest/CMakeLists.txt") + message(FATAL_ERROR + "Unit tests need ${missing_list}, which this LLVM package does not provide, " + "and no llvm-project source tree was found to build them from.\n" + "Set -DPSEUDO_LLVM_SOURCE_DIR=/path/to/llvm-project (must match LLVM " + "${LLVM_PACKAGE_VERSION}), or pass -DPSEUDO_ENABLE_TESTS=OFF.") + endif() + + message(STATUS "Building ${missing_list} from ${PSEUDO_LLVM_SOURCE_DIR}") + + # LLVM's gtest CMake file refers to the LLVM source and binary trees to + # find headers; point them at the same tree the rest of the build uses. + set(LLVM_SOURCE_DIR "${PSEUDO_LLVM_SOURCE_DIR}/llvm") + set(LLVM_MAIN_INCLUDE_DIR "${PSEUDO_LLVM_SOURCE_DIR}/llvm/include") + if(NOT LLVM_BINARY_DIR) + set(LLVM_BINARY_DIR "${CMAKE_BINARY_DIR}") + endif() + + if(NOT TARGET llvm_gtest) + add_subdirectory("${PSEUDO_LLVM_SOURCE_DIR}/third-party/unittest" + "${CMAKE_BINARY_DIR}/third-party/unittest") + endif() + if(NOT TARGET LLVMTestingAnnotations) + add_subdirectory("${PSEUDO_LLVM_SOURCE_DIR}/llvm/lib/Testing/Annotations" + "${CMAKE_BINARY_DIR}/llvm/lib/Testing/Annotations") + endif() + if(NOT TARGET LLVMTestingSupport) + add_subdirectory("${PSEUDO_LLVM_SOURCE_DIR}/llvm/lib/Testing/Support" + "${CMAKE_BINARY_DIR}/llvm/lib/Testing/Support") + endif() + + # The llvm/Testing headers live in the source tree; installed packages may + # or may not ship them, so make the source copy visible to consumers. Only + # the targets built here can take usage requirements; an imported one from + # the LLVM package already carries its include directories. + foreach(target LLVMTestingSupport LLVMTestingAnnotations) + if(target IN_LIST missing) + target_include_directories(${target} PUBLIC + $) + endif() + endforeach() +endfunction() diff --git a/fuzzer/CMakeLists.txt b/fuzzer/CMakeLists.txt index e1d7987..06c459b 100644 --- a/fuzzer/CMakeLists.txt +++ b/fuzzer/CMakeLists.txt @@ -10,7 +10,13 @@ add_llvm_fuzzer(clang-pseudo-fuzzer target_link_libraries(clang-pseudo-fuzzer PRIVATE - clangPseudo - clangPseudoCLI - clangPseudoGrammar + Pseudo + PseudoCLI + PseudoGrammar + ) + +clang_target_link_libraries(clang-pseudo-fuzzer + PRIVATE + clangBasic + clangLex ) diff --git a/test/CMakeLists.txt b/test/CMakeLists.txt index 712527f..555d183 100644 --- a/test/CMakeLists.txt +++ b/test/CMakeLists.txt @@ -1,15 +1,22 @@ set(CLANG_PSEUDO_TEST_DEPS - clang-pseudo - clang-pseudo-fuzzer + pseudo-tool ClangPseudoTests ) -foreach(dep FileCheck not count) +foreach(dep clang-pseudo-fuzzer PseudoModuleTests FileCheck not count) if(TARGET ${dep}) list(APPEND CLANG_PSEUDO_TEST_DEPS ${dep}) endif() endforeach() +# Run lit from the llvm-project source tree when the LLVM package does not +# install llvm-lit (installed packages never do). +if(NOT LLVM_EXTERNAL_LIT AND PSEUDO_LLVM_SOURCE_DIR + AND EXISTS "${PSEUDO_LLVM_SOURCE_DIR}/llvm/utils/lit/lit.py") + set(LLVM_EXTERNAL_LIT "${PSEUDO_LLVM_SOURCE_DIR}/llvm/utils/lit/lit.py" + CACHE STRING "Command used to spawn lit" FORCE) +endif() + configure_lit_site_cfg( ${CMAKE_CURRENT_SOURCE_DIR}/lit.site.cfg.py.in ${CMAKE_CURRENT_BINARY_DIR}/lit.site.cfg.py diff --git a/test/Unit/lit.cfg.py b/test/Unit/lit.cfg.py index 000a8a7..e4939ab 100644 --- a/test/Unit/lit.cfg.py +++ b/test/Unit/lit.cfg.py @@ -16,7 +16,7 @@ else: shlibpath_var = "LD_LIBRARY_PATH" config.environment[shlibpath_var] = os.path.pathsep.join( - ("@SHLIBDIR@", "@LLVM_LIBS_DIR@", config.environment.get(shlibpath_var, "")) + (config.shlibdir, config.llvm_libs_dir, config.environment.get(shlibpath_var, "")) ) # It is not realistically possible to account for all options that could diff --git a/tool/CMakeLists.txt b/tool/CMakeLists.txt index ecd97ef..00ab0ad 100644 --- a/tool/CMakeLists.txt +++ b/tool/CMakeLists.txt @@ -1,7 +1,5 @@ set(LLVM_LINK_COMPONENTS support) -find_package(Python3 REQUIRED COMPONENTS Interpreter) - add_clang_tool(pseudo-tool ClangPseudo.cpp HTMLForest.cpp diff --git a/unittests/CMakeLists.txt b/unittests/CMakeLists.txt index 28f4d04..2d37af4 100644 --- a/unittests/CMakeLists.txt +++ b/unittests/CMakeLists.txt @@ -2,10 +2,8 @@ set(LLVM_LINK_COMPONENTS Support ) -if(NOT TARGET LLVMTestingSupport) - message(STATUS "LLVMTestingSupport target not found in LLVM installation. Skipping unittests.") - return() -endif() +include(LLVMTestingLibs) +pseudo_require_llvm_testing_libs() add_custom_target(ClangPseudoUnitTests) set_target_properties(ClangPseudoUnitTests PROPERTIES FOLDER "Tests") @@ -21,12 +19,6 @@ add_unittest(ClangPseudoUnitTests ClangPseudoTests TokenTest.cpp ) -clang_target_link_libraries(ClangPseudoTests - PRIVATE - clangBasic - clangLex -) - target_link_libraries(ClangPseudoTests PRIVATE Pseudo @@ -36,6 +28,15 @@ target_link_libraries(ClangPseudoTests LLVMTestingSupport ) -if(CLANGD_INCLUDE_DIRS AND TARGET clangDaemon) +# The clang libraries must follow the Pseudo libraries on the link line: +# libPseudo.a uses clang::Lexer, and GNU ld resolves static archives in a +# single pass (see tool/CMakeLists.txt for the same ordering). +clang_target_link_libraries(ClangPseudoTests + PRIVATE + clangBasic + clangLex +) + +if(CLANGD_INCLUDE_DIRS) add_subdirectory(clangd) endif() diff --git a/unittests/clangd/CMakeLists.txt b/unittests/clangd/CMakeLists.txt index dcee8b3..f1b8271 100644 --- a/unittests/clangd/CMakeLists.txt +++ b/unittests/clangd/CMakeLists.txt @@ -2,31 +2,116 @@ set(LLVM_LINK_COMPONENTS Support ) +# PseudoModuleTests drives real ClangdServer / ClangdLSPServer instances and +# relies on the same test scaffolding clangd's own unit tests use. Those +# helpers are not part of any library, so compile them from clangd's +# unittests directory alongside the test. +set(CLANGD_UNITTEST_DIR "${CLANGD_INCLUDE_DIRS}/unittests") +set(CLANGD_TEST_SUPPORT_SOURCES + "${CLANGD_UNITTEST_DIR}/Annotations.cpp" + "${CLANGD_UNITTEST_DIR}/LSPClient.cpp" + "${CLANGD_UNITTEST_DIR}/SyncAPI.cpp" + "${CLANGD_UNITTEST_DIR}/TestFS.cpp" + "${CLANGD_UNITTEST_DIR}/TestIndex.cpp" + "${CLANGD_UNITTEST_DIR}/TestTU.cpp" +) + +# Feature.h includes Features.inc, which clangd's build generates from +# Features.inc.in. Reuse the one from the LLVM build tree when present; +# otherwise configure it here with clangd's defaults. The values only feed +# feature strings in compiled clangd sources, so they do not affect the ABI +# of the headers this test includes. +set(CLANGD_GENERATED_DIR "${CMAKE_CURRENT_BINARY_DIR}/clangd-generated") +set(CLANGD_FEATURES_INC "") +foreach(candidate + "${LLVM_BINARY_DIR}/tools/clang/tools/extra/clangd/Features.inc" + "${LLVM_BINARY_DIR}/tools/extra/clangd/Features.inc") + if(LLVM_BINARY_DIR AND EXISTS "${candidate}") + set(CLANGD_FEATURES_INC "${candidate}") + break() + endif() +endforeach() + +# Prerequisites beyond those of the other tests. When one is missing, skip +# this target only: ClangPseudoTests and the lit suite do not need clangd. +set(PSEUDO_MODULE_TESTS_SKIP_REASON "") +if(NOT TARGET clangDaemon) + set(PSEUDO_MODULE_TESTS_SKIP_REASON + "the LLVM package does not export the clangDaemon target (an LLVM build tree that built clangd does)") +elseif(NOT CLANGD_FEATURES_INC AND NOT EXISTS "${CLANGD_INCLUDE_DIRS}/Features.inc.in") + set(PSEUDO_MODULE_TESTS_SKIP_REASON + "no Features.inc in the LLVM build tree and no Features.inc.in under CLANGD_INCLUDE_DIRS") +else() + foreach(src ${CLANGD_TEST_SUPPORT_SOURCES}) + if(NOT EXISTS "${src}") + set(PSEUDO_MODULE_TESTS_SKIP_REASON + "clangd test support source not found: ${src} (CLANGD_INCLUDE_DIRS must be a full clang-tools-extra/clangd checkout)") + break() + endif() + endforeach() +endif() +if(PSEUDO_MODULE_TESTS_SKIP_REASON) + message(STATUS "Skipping PseudoModuleTests: ${PSEUDO_MODULE_TESTS_SKIP_REASON}") + return() +endif() + +if(CLANGD_FEATURES_INC) + get_filename_component(CLANGD_GENERATED_DIR "${CLANGD_FEATURES_INC}" DIRECTORY) +else() + if(APPLE) + set(CLANGD_BUILD_XPC 1) + else() + set(CLANGD_BUILD_XPC 0) + endif() + set(CLANGD_ENABLE_REMOTE 0) + set(ENABLE_GRPC_REFLECTION 0) + set(CLANGD_MALLOC_TRIM 1) + set(CLANGD_TIDY_CHECKS 1) + set(CLANGD_DECISION_FOREST 1) + configure_file("${CLANGD_INCLUDE_DIRS}/Features.inc.in" + "${CLANGD_GENERATED_DIR}/Features.inc") +endif() + add_unittest(ClangPseudoUnitTests PseudoModuleTests PseudoModuleTests.cpp + ${CLANGD_TEST_SUPPORT_SOURCES} ) target_include_directories(PseudoModuleTests PRIVATE - "${CLANGD_INCLUDE_DIRS}" - "${CLANGD_INCLUDE_DIRS}/../include-cleaner/include" - "${CMAKE_CURRENT_SOURCE_DIR}/../../clangd" - "${CMAKE_CURRENT_SOURCE_DIR}/../../include" - "${CMAKE_BINARY_DIR}/include" + "${CLANGD_UNITTEST_DIR}" + "${CLANGD_GENERATED_DIR}" ) -target_link_libraries(PseudoModuleTests PRIVATE +target_link_libraries(PseudoModuleTests + PRIVATE + obj.clangd_pseudo Pseudo PseudoCXX PseudoGrammar - clangBasic - clangLex LLVMTestingAnnotations LLVMTestingSupport + clangDaemon + clangdSupport +) + +# The clang libraries must follow the Pseudo libraries on the link line: +# libPseudo.a uses clang::Lexer, and GNU ld resolves static archives in a +# single pass (see tool/CMakeLists.txt for the same ordering). +clang_target_link_libraries(PseudoModuleTests + PRIVATE + clangAST + clangBasic + clangFrontend + clangIndex + clangLex + clangSema + clangSerialization + clangTooling + clangToolingCore ) -if(TARGET clangDaemon) - target_link_libraries(PseudoModuleTests PRIVATE - clangDaemon - clangdSupport - ) +# Some tests parse real files from the llvm-project tree. +if(PSEUDO_LLVM_SOURCE_DIR) + target_compile_definitions(PseudoModuleTests PRIVATE + PSEUDO_LLVM_SOURCE_DIR="${PSEUDO_LLVM_SOURCE_DIR}") endif() diff --git a/unittests/clangd/PseudoModuleTests.cpp b/unittests/clangd/PseudoModuleTests.cpp index 11ba2d1..2883ed4 100644 --- a/unittests/clangd/PseudoModuleTests.cpp +++ b/unittests/clangd/PseudoModuleTests.cpp @@ -64,9 +64,17 @@ ::testing::Matcher children(ChildMatchers... ChildrenM) { } TEST(PseudoModuleTest, RegistryRegistration) { + // The plugin registers itself under this name; clangd -load relies on it. + bool Found = false; + for (const auto &Entry : FeatureModuleRegistry::entries()) + if (Entry.getName() == "pseudo-parser") + Found = true; + EXPECT_TRUE(Found); + + // fromRegistry() instantiates registry entries through the untyped add(), + // so modules are reachable by iteration rather than by get<>(). FeatureModuleSet Set = FeatureModuleSet::fromRegistry(); - auto *Mod = Set.get(); - EXPECT_NE(Mod, nullptr); + EXPECT_NE(Set.begin(), Set.end()); } TEST(PseudoModuleTest, BasicDocumentSymbols) { @@ -310,6 +318,8 @@ TEST(PseudoModuleTest, ClangdServerFallbackOnInvalidCommand) { FeatureModuleSet Modules; auto Pseudo = std::make_unique(); auto *PseudoPtr = Pseudo.get(); + PseudoPtr->setFSForTesting(&FS); + PseudoPtr->setCompilationDatabaseForTesting(CDB.get()); Modules.add(std::move(Pseudo)); ClangdServer::Options Opts = ClangdServer::optsForTest(); @@ -515,14 +525,16 @@ TEST(PseudoModuleTest, LocateSymbolAtNamespaceAlias) { } TEST(PseudoModuleTest, UsePseudoParserOnlyOption) { - // End-to-end test verifying setPseudoOnly disables Clang parser completely - // and routes open-file navigation requests directly to PseudoModule. + // End-to-end test verifying that, with setPseudoOnly, open-file navigation + // requests are served by PseudoModule while a ClangdServer is running. MockFS FS; auto CDB = std::make_unique(); FeatureModuleSet Modules; auto Pseudo = std::make_unique(); auto *PseudoPtr = Pseudo.get(); + PseudoPtr->setFSForTesting(&FS); + PseudoPtr->setCompilationDatabaseForTesting(CDB.get()); PseudoPtr->setPseudoOnly(true); Modules.add(std::move(Pseudo)); @@ -548,9 +560,6 @@ TEST(PseudoModuleTest, UsePseudoParserOnlyOption) { Server.addDocument(FilePath, Source.code()); ASSERT_TRUE(Server.blockUntilIdleForTest()); - // In UsePseudoParserOnly mode, no Clang AST is built at all! - EXPECT_EQ(dumpAST(Server, FilePath), ""); - // locateSymbolAt should work via PseudoModule TextDocumentPositionParams PosParams; PosParams.textDocument.uri = URIForFile::canonicalize(FilePath, FilePath); @@ -1157,6 +1166,8 @@ TEST(PseudoModuleTest, DataServiceOutOfClassDefinition) { FeatureModuleSet Modules; auto Pseudo = std::make_unique(); auto *PseudoPtr = Pseudo.get(); + PseudoPtr->setFSForTesting(&FS); + PseudoPtr->setCompilationDatabaseForTesting(CDB.get()); PseudoPtr->setPseudoOnly(true); Modules.add(std::move(Pseudo)); @@ -1758,6 +1769,8 @@ TEST(PseudoModuleTest, ClangdServerPseudoOnlyHoverSemanticTokensCompletion) { FeatureModuleSet Modules; auto Pseudo = std::make_unique(); auto *PseudoPtr = Pseudo.get(); + PseudoPtr->setFSForTesting(&FS); + PseudoPtr->setCompilationDatabaseForTesting(CDB.get()); PseudoPtr->setPseudoOnly(true); Modules.add(std::move(Pseudo)); @@ -1853,6 +1866,8 @@ TEST(PseudoModuleTest, ClangdServerFallbackHoverSemanticTokensCompletion) { FeatureModuleSet Modules; auto Pseudo = std::make_unique(); auto *PseudoPtr = Pseudo.get(); + PseudoPtr->setFSForTesting(&FS); + PseudoPtr->setCompilationDatabaseForTesting(CDB.get()); Modules.add(std::move(Pseudo)); ClangdServer::Options Opts = ClangdServer::optsForTest(); @@ -2475,8 +2490,12 @@ TEST(PseudoModuleTest, StdMoveJumpsToFunctionNotInclude) { EXPECT_TRUE(bool(Loc)) << (Loc ? "" : llvm::toString(Loc.takeError())); ASSERT_TRUE(Loc && !Loc->empty()); EXPECT_EQ(Loc->front().Name, "move"); - EXPECT_TRUE(llvm::StringRef(Loc->front().PreferredDeclaration.uri.file()).ends_with("bits/move.h")) - << "Expected bits/move.h, got " << Loc->front().PreferredDeclaration.uri.file(); + // Compare with native separators normalized: the mock FS yields + // backslash paths on Windows. + std::string DeclPath = + llvm::sys::path::convert_to_slash(Loc->front().PreferredDeclaration.uri.file()); + EXPECT_TRUE(llvm::StringRef(DeclPath).ends_with("bits/move.h")) + << "Expected bits/move.h, got " << DeclPath; EXPECT_NE(Loc->front().PreferredDeclaration.range.start.line, 0) << "Should not jump to line 0 (#include directive)"; } @@ -3141,6 +3160,14 @@ TEST(PseudoModuleTest, ShowASTRangeSelection) { } static std::string findRepoSourceFile(llvm::StringRef RelativePath) { +#ifdef PSEUDO_LLVM_SOURCE_DIR + { + llvm::SmallString<256> Path(PSEUDO_LLVM_SOURCE_DIR); + llvm::sys::path::append(Path, RelativePath); + if (llvm::sys::fs::exists(Path)) + return std::string(Path.str()); + } +#endif if (llvm::sys::fs::exists(RelativePath)) return RelativePath.str();