Files
clice/tests/unit/test/tester.h
ykiko cc5b25d5c3 refactor: public feature types and snapshot testing infrastructure (#442)
## Summary

- **Public feature types**: Move `SemanticToken`, `FoldingRange`,
`DocumentSymbol`, `InlayHint`, and `HintCategory` from internal `.cpp`
files to `feature.h` as public API types. Each feature now exposes two
overloads: a raw overload returning offset-based types and a protocol
overload that converts to LSP wire-format with explicit
`PositionEncoding`.
- **Snapshot testing**: Add corpus-driven snapshot tests using
`ASSERT_SNAPSHOT_GLOB` for semantic tokens, folding ranges, inlay hints,
document symbols, and TU index. Tests compile real C++ corpus files,
format output as YAML flow mappings, and diff against `.snap.yml`
baselines.
- **Test infrastructure**: Add `compile_file()` to `Tester`,
`yaml_str()` utility, `--corpus-dir` / `--snapshot-dir` CLI options, and
`--verbose` flag for unit tests. Migrate to kotatsu's unified
`kota::zest::Options` API.
- **Toolchain robustness**: Filter unknown cc1 args via
`clang::driver::getDriverOptTable()` to handle system compilers newer
than embedded LLVM.
- **Dependency bump**: Update kotatsu to 7381404 (unified zest Options,
out-param `from_json` API).

## Details

### Feature type changes
All five feature modules (`semantic_tokens`, `folding_ranges`,
`document_symbols`, `inlay_hints`, `document_links`) now follow the same
two-overload pattern. The raw overload returns offset-based structs
suitable for indexing and testing; the protocol overload adds
`PositionEncoding` conversion for LSP responses. `stateful_worker.cpp`
explicitly passes `PositionEncoding::UTF16` at every call site.

### Snapshot tests
Corpus files live in `tests/corpus/` (organized by language construct).
Snapshot baselines live in `tests/snapshots/<feature>/`. Format lambdas
are inlined directly in test bodies — no separate format functions for
single-use formatters. YAML output uses flow mappings (`- { key: value
}`) for compact, diffable baselines.

### cc1 arg filtering
`src/command/toolchain.cpp` now parses the cc1 argument list through
LLVM's driver option table and drops any args classified as
`UnknownClass`. This prevents compilation failures when the system
compiler emits flags that the embedded LLVM version doesn't recognize.

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

---------

Co-authored-by: Claude Opus 4.6 <noreply@anthropic.com>
2026-05-24 19:36:27 +08:00

117 lines
3.3 KiB
C++

#pragma once
#include <format>
#include <optional>
#include <string>
#include <vector>
#include "test/annotation.h"
#include "test/test.h"
#include "command/command.h"
#include "compile/compilation.h"
#include "support/logging.h"
namespace clice::testing {
struct Tester {
CompilationParams params;
CompilationDatabase database;
std::optional<CompilationUnit> unit;
std::string src_path;
AnnotatedSources sources;
/// Owns argument strings so that params.arguments (const char*) remains valid.
std::vector<std::string> owned_args;
/// The VFS used for compilation.
llvm::IntrusiveRefCntPtr<TestVFS> vfs;
struct ModuleFile {
std::string filename;
std::string content;
};
std::vector<ModuleFile> module_files;
std::vector<std::string> pcm_paths;
~Tester();
void add_main(llvm::StringRef file, llvm::StringRef content) {
src_path = file.str();
sources.add_source(file, content);
}
void add_file(llvm::StringRef name, llvm::StringRef content) {
sources.add_source(name, content);
}
void add_files(llvm::StringRef main_file, llvm::StringRef content) {
src_path = main_file.str();
sources.add_sources(content);
}
void add_module(llvm::StringRef filename, llvm::StringRef content) {
module_files.push_back({filename.str(), content.str()});
}
/// Fast VFS-only path: uses -cc1 directly, no system headers.
void prepare(llvm::StringRef standard = "-std=c++20");
bool compile(llvm::StringRef standard = "-std=c++20");
bool compile_with_pch(llvm::StringRef standard = "-std=c++20");
bool compile_with_modules(llvm::StringRef standard = "-std=c++20");
/// Read a file from disk and compile it directly (no VFS content needed).
bool compile_file(llvm::StringRef path, llvm::StringRef standard = "-std=c++20");
/// Driver path: uses CompilationDatabase + toolchain cache, has system headers.
void prepare_driver(llvm::StringRef standard = "-std=c++20");
bool compile_driver(llvm::StringRef standard = "-std=c++20");
bool compile_driver_with_pch(llvm::StringRef standard = "-std=c++20");
bool try_compile();
std::uint32_t operator[](llvm::StringRef file, llvm::StringRef pos) {
return sources.all_files.lookup(file).offsets.lookup(pos);
}
std::uint32_t point(llvm::StringRef name = "", llvm::StringRef file = "");
llvm::ArrayRef<std::uint32_t> nameless_points(llvm::StringRef file = "");
LocalSourceRange range(llvm::StringRef name = "", llvm::StringRef file = "");
void clear();
};
inline std::string yaml_str(llvm::StringRef s) {
std::string result;
result.reserve(s.size() + 2);
result += '"';
for(char c: s) {
switch(c) {
case '"': result += "\\\""; break;
case '\\': result += "\\\\"; break;
case '\n': result += "\\n"; break;
case '\r': result += "\\r"; break;
case '\t': result += "\\t"; break;
default:
if(static_cast<unsigned char>(c) < 0x20) {
result += std::format("\\x{:02x}", static_cast<unsigned char>(c));
} else {
result += c;
}
break;
}
}
result += '"';
return result;
}
} // namespace clice::testing