Repository navigation
Remove duplicated blocks from test and demo files (1 of 4) - #31
Conversation
First batch of the 41 files that were left unformatted because the linter's copy/paste detector rejects them. Each duplicated block is replaced by a helper in the same file; no suppression markers are used. - demo_external_lsp.cpp: the clangd and pyright walkthroughs were two copies of one routine; they now share testLanguageServer() and small helpers for printing the command, waiting on a flag and the completion / hover requests. - demo_gpu_acceleration.cpp, test_gpu_simple.cpp, test_gpu_standalone.cpp: one backendTypeName() instead of repeated switch statements. - demo_lsp_editor_integration.cpp, demo_lsp_integration.cpp: one plugin-lookup helper instead of the same extension loop in each request handler. - demo_multi_cursor.cpp: printCursorPositions(). - test/test_plugin_system.cpp: the two test plugins share a base class. - test/test_model_loader.cpp, test/test_bpe_tokenizer.cpp, test_benchmark_suite.cpp, test_performance_profiler.cpp: shared helpers for reading a report file, the test vocabulary and the tensor checks. The files are then formatted and made cpplint-clean like the rest. Output of the five demo programs was compared before and after and is unchanged. Three of these files (test_gpu_standalone.cpp, test/test_bpe_tokenizer.cpp, test_benchmark_suite.cpp) are not part of any build target and did not compile before this change either; they show the same errors as before and no new ones. Co-Authored-By: Claude Fable 5.2 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_011vmiZ7F3rQu7dith9sQw71
|
Integration Tests failed once on 77dc01a and passed on a single re-run. The first attempt failed on one item of the smoke test: Why I don't think it comes from this PR: the diff does not touch What is not known: the cause. Exit status 5 is not one of the program's own exit codes that I can find, and the job log shows an orphaned Generated by Claude Code |
| std::this_thread::sleep_for(std::chrono::milliseconds(100)); | ||
| } | ||
|
|
||
| waitForFlag(completionReceived, std::chrono::seconds(5)); |
There was a problem hiding this comment.
🔴 Late LSP responses corrupt expired stack storage
If a response arrives after requestCompletion times out and returns, its captured result and flag no longer exist. LspClient::messageProcessorFunc still invokes the callback, which writes into expired stack storage. requestHover has the same regression, allowing delayed responses to corrupt memory or crash the demo.
Learn more
The extracted helpers capture their local result objects and completion flags by reference. Returning after the five-second wait destroys those objects without removing the callback registered by LspClient::sendRequest. The message processor invokes that callback when the response eventually arrives. The remaining session keeps the client running during hover and shutdown, so delayed responses can still reach the destroyed objects. requestHover has the same problem. Previously, the captured objects remained in the session's scope until disconnect joined the message processor.
Example: clangd takes six seconds to answer completion. At five seconds, the completion helper returns and the demo starts waiting for hover. At six seconds, the completion callback assigns to the destroyed completion list and flag rather than live session variables.
Recommended fix: Store each request's result and completion status in shared, heap-owned state captured by value, with mutex/condition-variable synchronization. Alternatively, implement cancellation that removes the callback and waits for any in-flight invocation before allowing the captured state to die. Cover both completion and hover with a delayed-response test.
Was this helpful? React with 👍 or 👎 to provide feedback.
| // Requests completion at the given position and prints the first few items. | ||
| void requestCompletion(LspClient& client, const std::string& uri, const Position& position) { |
There was a problem hiding this comment.
🔍 New helpers lack Doxygen documentation
New helpers such as requestCompletion use ordinary // descriptions, not Doxygen comments. The code style guidelines require Doxygen-style documentation; this pattern repeats across the extracted helpers.
Was this helpful? React with 👍 or 👎 to provide feedback.
There was a problem hiding this comment.
🟡 Changes recommended
Two LSP request helpers leave callbacks referencing destroyed stack state after a timeout, allowing late responses to crash the process or corrupt memory.
1 open issue
What changed in this PR
This first follow-up to #29 removes duplicated blocks from 12 test and demo files so they can join the codebase’s formatting and lint cleanup.
Changes:
- Extracts file-local helpers for language-server sessions, plugin lookup, GPU backend names, and cursor output.
- Consolidates test plugin setup, vocabulary data, tensor checks, and report reading.
- Applies formatting and lint-related cleanup across all 12 files.
| File | Description |
|---|---|
test/test_plugin_system.cpp |
Shares plugin metadata and lifecycle handling through a base class. |
test/test_model_loader.cpp |
Extracts assertion, model-filtering, and tensor-check helpers. |
test/test_bpe_tokenizer.cpp |
Reuses a shared test vocabulary. |
test_performance_profiler.cpp |
Centralizes exported-file reading. |
test_gpu_standalone.cpp |
Consolidates GPU backend-name formatting. |
test_gpu_simple.cpp |
Reuses a backend-name helper while preserving empty fallback output. |
test_benchmark_suite.cpp |
Centralizes report-file reading. |
demo_multi_cursor.cpp |
Reuses cursor-position output logic. |
demo_lsp_integration.cpp |
Shares plugin file-extension matching. |
demo_lsp_editor_integration.cpp |
Centralizes plugin lookup by file type. |
demo_gpu_acceleration.cpp |
Replaces repeated backend switches with a helper. |
demo_external_lsp.cpp |
Unifies clangd and pyright sessions and extracts request helpers. |
Review effort: Balanced
| client.completion(completionParams, [&completions, &completionReceived](const CompletionList& result) { | ||
| completions = result; | ||
| completionReceived = true; |

Description
First of four pull requests that refactor the 41 files left unformatted in #29. Each of those files contains duplicated blocks that the linter's copy/paste detector rejects whenever the file is changed. This batch covers the 12 test and demo files; the editor/LSP, AI and remaining files follow separately.
Type of Change
Related Issues
Related to #29
Changes Made
Each duplicated block is replaced by a helper in the same file. No suppression markers are used.
demo_external_lsp.cpp: the clangd and pyright walkthroughs were two copies of one routine; they now sharetestLanguageServer()plus small helpers for printing the command, waiting on a flag, and the completion and hover requests.demo_gpu_acceleration.cpp,test_gpu_simple.cpp,test_gpu_standalone.cpp: onebackendTypeName()instead of repeatedswitchstatements.demo_lsp_editor_integration.cpp,demo_lsp_integration.cpp: one plugin-lookup helper instead of the same extension loop in each request handler.demo_multi_cursor.cpp:printCursorPositions().test/test_plugin_system.cpp: the two test plugins share a base class.test/test_model_loader.cpp,test/test_bpe_tokenizer.cpp,test_benchmark_suite.cpp,test_performance_profiler.cpp: shared helpers for reading a report file, the test vocabulary and the tensor checks.The files are then formatted with clang-format and made cpplint-clean like the rest of the code base.
Testing
All existing tests pass
Manual testing performed
Locally on Linux (GCC): 98/98 CTest tests;
scripts/smoke_test.shpasses.The five demo programs were built before and after and their output compared: identical, apart from timestamps and thread ids in one log. clangd is not installed in the test environment, so the clangd branch of
demo_external_lsponly exercised its "not available" path.clang-format 15.0.7, cpplint 1.6.1 and jscpd 3.5.3 report nothing on the 12 files.
Reviewer Notes
test_gpu_standalone.cpp,test/test_bpe_tokenizer.cppandtest_benchmark_suite.cpp. They show the same errors as before and no new ones, so their refactoring is checked by the linters and by reading only.test_benchmark_suite.cppandtest_performance_profiler.cppthe "file opened" assertion now lives in the helper, so a failure there reports the helper's line number; and intest_gpu_simple.cppthe sharedbackendTypeName()returns an empty string for an unknown backend where the originalswitchprinted nothing.Generated by Claude Code