Files
clice/tests/unit/test/tester.h
ykiko 9c89d20e76 feat(tests): add compile_with_modules helper to Tester (#420)
## Summary
- Add `add_module()` and `compile_with_modules()` to the `Tester` test
framework
- Supports both separate `add_module()` calls and single-string
`#[filename]` syntax via `add_files()`
- Automatically scans module dependencies with `scan_precise`,
topologically sorts, builds PCMs in order, then compiles the main file
- Temporary PCM files cleaned up automatically in destructor
- Migrated `ModuleImport` and `ModuleReexport` semantic tokens tests to
use the new API

## Test plan
- [x] All 505 unit tests pass
- [x] All 113 integration tests pass
- [x] All 2 smoke tests pass

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

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

* **Tests**
* Centralized, module-aware test compilation with automatic module
discovery, dependency ordering, and cycle detection.
* Unified "compile with modules" flow; tests now add module sources
directly and no longer manage temporary module artifacts manually.
* Reduced duplicated compile/diagnostic logic and improved cleanup of
generated artifacts.
<!-- end of auto-generated comment: release notes by coderabbit.ai -->

---------

Co-authored-by: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
2026-04-11 17:16:53 +08:00

89 lines
2.4 KiB
C++

#pragma once
#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");
/// 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();
};
} // namespace clice::testing