cranelift_reader/
testfile.rs

1//! Data structures representing a parsed test file.
2//!
3//! A test file is a `.clif` file which contains test commands and settings for running a
4//! file-based test case.
5//!
6
7use crate::error::Location;
8use crate::isaspec::IsaSpec;
9use crate::sourcemap::SourceMap;
10use crate::testcommand::TestCommand;
11use cranelift_codegen::ir::entities::AnyEntity;
12use cranelift_codegen::ir::Function;
13
14/// A parsed test case.
15///
16/// This is the result of parsing a `.clif` file which contains a number of test commands and ISA
17/// specs followed by the functions that should be tested.
18pub struct TestFile<'a> {
19    /// `test foo ...` lines.
20    pub commands: Vec<TestCommand<'a>>,
21    /// `isa bar ...` lines.
22    pub isa_spec: IsaSpec,
23    /// `feature ...` lines
24    pub features: Vec<Feature<'a>>,
25    /// Comments appearing before the first function.
26    /// These are all tagged as 'Function' scope for lack of a better entity.
27    pub preamble_comments: Vec<Comment<'a>>,
28    /// Parsed functions and additional details about each function.
29    pub functions: Vec<(Function, Details<'a>)>,
30}
31
32/// Additional details about a function parsed from a text string.
33/// These are useful for detecting test commands embedded in comments etc.
34/// The details to not affect the semantics of the function.
35#[derive(Debug)]
36pub struct Details<'a> {
37    /// Location of the `function` keyword that begins this function.
38    pub location: Location,
39    /// Annotation comments that appeared inside or after the function.
40    pub comments: Vec<Comment<'a>>,
41    /// Mapping of entity numbers to source locations.
42    pub map: SourceMap,
43}
44
45/// A comment in a parsed function.
46///
47/// The comment belongs to the immediately preceding entity, whether that is a block header, and
48/// instruction, or one of the preamble declarations.
49///
50/// Comments appearing inside the function but before the preamble, as well as comments appearing
51/// after the function are tagged as `AnyEntity::Function`.
52#[derive(Clone, PartialEq, Eq, Debug)]
53pub struct Comment<'a> {
54    /// The entity this comment is attached to.
55    /// Comments always follow their entity.
56    pub entity: AnyEntity,
57    /// Text of the comment, including the leading `;`.
58    pub text: &'a str,
59}
60
61/// A cranelift feature in a test file preamble.
62///
63/// This represents the expectation of the test case. Before running any of the
64/// functions of the test file, the feature set should be compared with the
65/// feature set used to compile Cranelift. If there is any differences, then the
66/// test file should be skipped.
67#[derive(PartialEq, Eq, Debug)]
68pub enum Feature<'a> {
69    /// `feature "..."` lines
70    With(&'a str),
71    /// `feature ! "..."` lines.
72    Without(&'a str),
73}