Files
clice/tests/unit/server/pch_worker_tests.cpp
ykiko bb0b160a28 refactor(server): extract Indexer and Compiler from MasterServer (#403)
## Summary

- **Extract `Indexer` class** — owns all index state (ProjectIndex,
MergedIndex shards, OpenFileIndex) and query methods (definition,
references, call/type hierarchy, workspace symbol search)
- **Extract `Compiler` class** — owns document state, PCH/PCM cache,
compile argument resolution, header context, `ensure_compiled`, and
worker forwarding
- **MasterServer is now a pure LSP handler registration layer** (~700
lines, down from ~3200)
- **`MergedIndexShard`** wraps `index::MergedIndex` with a lazily-cached
PositionMapper; `OpenFileIndex` gains matching
`find_occurrence()`/`find_relations()` APIs — callers get pre-converted
LSP ranges directly
- **Indexer returns typed values** (`vector<Location>`,
`vector<CallHierarchyIncomingCall>`, etc.) instead of pre-serialized
JSON, fixing the references handler from JSON string surgery to simple
vector concatenation
- **Fix**: duplicate `workspace/symbol` loop in the original code

## Test plan

- [x] 465 unit tests pass
- [x] 113 integration tests pass
- [x] 2/2 smoke tests pass
- [x] `clang-format` applied

🤖 Generated with [Claude Code](https://claude.com/claude-code)

<!-- This is an auto-generated comment: release notes by coderabbit.ai
-->
## Summary by CodeRabbit

* **New Features**
* Server-side C++ compilation orchestration (module & precompiled header
builds) with LSP-integrated document handling.

* **Improvements**
* Deterministic, persistent, dependency-aware caching to avoid redundant
rebuilds and speed up incremental work.
* Better cross-file indexing and navigation, improved diagnostics and
more reliable include/import-aware completions.

* **Tests**
  * Unit tests updated to the unified worker query/build request shapes.
<!-- end of auto-generated comment: release notes by coderabbit.ai -->

---------

Co-authored-by: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
2026-04-07 13:30:12 +08:00

156 lines
4.5 KiB
C++

#include <string>
#include <vector>
#include "test/test.h"
#include "server/protocol.h"
#include "server/worker_test_helpers.h"
#include "syntax/scan.h"
namespace clice::testing {
namespace {
namespace et = eventide;
// ============================================================================
// End-to-end PCH compilation through real workers:
// 1. Stateless worker builds PCH for preamble headers
// 2. Stateful worker compiles a file using the PCH
// ============================================================================
TEST_SUITE(PCHWorker) {
TEST_CASE(BuildPCHThenCompile) {
TempDir tmp;
tmp.touch("common.h", R"cpp(struct Point { int x, y; };)cpp" "\n");
auto header = tmp.path("common.h");
std::string main_text = "#include \"common.h\"\nPoint p{1,2};\n";
tmp.touch("main.cpp", main_text);
auto main_file = tmp.path("main.cpp");
auto dir = std::string(tmp.root);
// --- Phase 1: Build PCH via stateless worker ---
WorkerHandle sl;
ASSERT_TRUE(sl.spawn("stateless-worker"));
std::string pch_path;
bool phase1_done = false;
sl.run([&]() -> et::task<> {
worker::BuildParams params;
params.kind = worker::BuildKind::BuildPCH;
params.file = main_file;
params.directory = dir;
params.arguments = {"clang++",
"-resource-dir",
std::string(resource_dir()),
"-x",
"c++-header",
"-I",
dir,
main_file};
params.text = main_text;
params.output_path = tmp.path("preamble.pch");
auto result = co_await sl.peer->send_request(params);
CO_ASSERT_TRUE(result.has_value());
CO_ASSERT_TRUE(result.value().success);
pch_path = result.value().output_path;
EXPECT_FALSE(pch_path.empty());
phase1_done = true;
sl.peer->close_output();
});
ASSERT_TRUE(phase1_done);
ASSERT_FALSE(pch_path.empty());
// Verify the PCH file exists on disk.
ASSERT_TRUE(llvm::sys::fs::exists(pch_path));
// --- Phase 2: Compile with PCH via stateful worker ---
WorkerHandle sf;
ASSERT_TRUE(sf.spawn("stateful-worker"));
bool phase2_done = false;
auto preamble_bound = compute_preamble_bound(main_text);
sf.run([&]() -> et::task<> {
worker::CompileParams params;
params.path = main_file;
params.version = 1;
params.text = main_text;
params.directory = dir;
params.arguments = {"clang++",
"-resource-dir",
std::string(resource_dir()),
"-fsyntax-only",
"-I",
dir,
main_file};
params.pch = {pch_path, preamble_bound};
auto result = co_await sf.peer->send_request(params);
CO_ASSERT_TRUE(result.has_value());
EXPECT_EQ(result.value().version, 1);
phase2_done = true;
sf.peer->close_output();
});
ASSERT_TRUE(phase2_done);
// Cleanup PCH temp file.
std::remove(pch_path.c_str());
}
TEST_CASE(CompileWithoutPCHStillWorks) {
TempDir tmp;
tmp.touch("common.h", R"cpp(struct Point { int x, y; };)cpp" "\n");
std::string main_text = "#include \"common.h\"\nPoint p{1,2};\n";
tmp.touch("main.cpp", main_text);
auto main_file = tmp.path("main.cpp");
auto dir = std::string(tmp.root);
WorkerHandle sf;
ASSERT_TRUE(sf.spawn("stateful-worker"));
bool compile_done = false;
sf.run([&]() -> et::task<> {
worker::CompileParams params;
params.path = main_file;
params.version = 1;
params.text = main_text;
params.directory = dir;
params.arguments = {"clang++",
"-resource-dir",
std::string(resource_dir()),
"-fsyntax-only",
"-I",
dir,
main_file};
// pch left as default (empty path, 0 bound).
auto result = co_await sf.peer->send_request(params);
CO_ASSERT_TRUE(result.has_value());
EXPECT_EQ(result.value().version, 1);
compile_done = true;
sf.peer->close_output();
});
ASSERT_TRUE(compile_done);
}
}; // TEST_SUITE(PCHWorker)
} // namespace
} // namespace clice::testing